Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(lib)mpg123 use of function mpg123_index in C#

Tags:

c

c#

pointers

mono

I'm using mpg123 inside my project to decode mp3. I spoke with the developer (Thomas Orgis) about a performance issue when seeking and he had a great idea: Scan the file and afterwards set the scanned index to the playback handle. So I want to use mpg123_index and mpg123_set_index inside my C# project and use a wrapper around libmpg123.

The wrapper is not self written, I also contacted the developer, but he seems to be unavailable. So maybe someone has knowledge of pointer programming, I also did some things myself, but currently I only get a AccessViolationException when calling the method.

Here some Code: Wrapper specification (maybe this is wrong?!):

[DllImport(Mpg123Dll)]public static extern int mpg123_index(IntPtr mh, IntPtr offsets, IntPtr step, IntPtr fill);

Specification on Website (C):

https://www.mpg123.de/api/group__mpg123__seek.shtml#gae1d174ac632ec72df7dead94c04865fb

MPG123_EXPORT int mpg123_index ( mpg123_handle * mh, off_t ** offsets, off_t * step, size_t * fill )

Give access to the frame index table that is managed for seeking. You are asked not to modify the values... Use mpg123_set_index to set the seek index

Parameters mh handle offsets pointer to the index array step one index byte offset advances this many MPEG frames fill number of recorded index offsets; size of the array

Returns MPG123_OK on success

My try to use the function in C#:

IntPtr pIndex = IntPtr.Zero;
IntPtr pFill = IntPtr.Zero;
IntPtr pStep = IntPtr.Zero;
if (MPGImport.mpg123_index(this.mp3Handle,pIndex,pStep,pFill) != (int)MPGImport.mpg123_errors.MPG123_OK)) 
{
    log.error("Failed!");
}

So, has anyone an idea, how to use the function properly on C# side? From the author of mpg123 I just got the information, that the pIndex will be a pointer to the internal index. So this pointer will be changed by this function.

Thanks for your help.

Sincerely Sven

Edit: Now the following code almost works:

[DllImport(Mpg123Dll)]public unsafe static extern int mpg123_index(IntPtr mh, long **offsets, long *step, ulong *fill);
[DllImport(Mpg123Dll)]public unsafe static extern int mpg123_set_index(IntPtr mh, long* offsets, long step, ulong fill);

public static Boolean CopyIndex(IntPtr _sourceHandle,IntPtr _destinationHandle)
{
    Boolean copiedIndex = false;
    unsafe
    {
        long* offsets = null; 
        long step = 0;
        ulong fill = 0;
        int result = MPGImport.mpg123_index(_sourceHandle, &offsets, &step, &fill);
        if (result == (int)MPGImport.mpg123_errors.MPG123_OK)
        {
            result = MPGImport.mpg123_set_index(_destinationHandle, offsets, step, fill);
            if (result == (int)mpg123_errors.MPG123_OK)
            {
                copiedIndex = true;
            }
        }
    }
    return copiedIndex;
}

On result = MPGImport.mpg123_set_index(_destinationHandle, offsets, step, fill); the result is -1, which is MPG123_ERR = -1, /**< Generic Error */.

like image 391
Sven Avatar asked Oct 28 '22 17:10

Sven


1 Answers

Working code:

[DllImport(Mpg123Dll)]public unsafe static extern int mpg123_index(IntPtr mh, long **offsets, long *step, ulong *fill);
[DllImport(Mpg123Dll)]public unsafe static extern int mpg123_set_index(IntPtr mh, long* offsets, long step, ulong fill);

public static Boolean CopyIndex(IntPtr _sourceHandle,IntPtr _destinationHandle)
{
    Boolean copiedIndex = false;
    unsafe
    {
        long* offsets = null; 
        long step = 0;
        ulong fill = 0;
        int result = MPGImport.mpg123_index(_sourceHandle, &offsets, &step, &fill);
        if (result == (int)MPGImport.mpg123_errors.MPG123_OK)
        {
            result = MPGImport.mpg123_set_index(_destinationHandle, offsets, step, fill);
            if (result == (int)mpg123_errors.MPG123_OK)
            {
                copiedIndex = true;
            }
        }
    }
    return copiedIndex;
}

_sourceHandle and _destinationHandle may not be IntPtr.Zero, that was my mistake. Thanks for Stargateur, really nice help!

like image 157
Sven Avatar answered Nov 15 '22 06:11

Sven