public static OpusDecoderNative Create(int sampleRate, int channel)
        {
            var error = OpusPInvoke.ErrorCodes.OK;
            var temp  = OpusPInvoke.OpusDecoderCreate(sampleRate, channel, ref error);

            if (error < OpusPInvoke.ErrorCodes.OK)
            {
                throw new Exception(OpusPInvoke.GetMessage(error));
            }
            return(temp);
        }
        public int GetSamplesNumber(byte[] data, int offset, int len)
        {
            var dataHandle = GCHandle.Alloc(data, GCHandleType.Pinned);

            try
            {
                int number = OpusPInvoke.OpusDecoderGetNbSamples(
                    handle,
                    IntPtr.Add(dataHandle.AddrOfPinnedObject(), offset), len);
                if (number < (int)OpusPInvoke.ErrorCodes.OK)
                {
                    throw new Exception(OpusPInvoke.GetMessage((OpusPInvoke.ErrorCodes)number));
                }
                return(number);
            }
            finally
            {
                dataHandle.Free();
            }
        }
        public int Decode(byte[] data, int dataOffset, int dataLen, short[] pcm, int pcmOffset, int pcmLength, bool decodeFec)
        {
            var dataHandle = GCHandle.Alloc(data, GCHandleType.Pinned);
            var pcmHandle  = GCHandle.Alloc(pcm, GCHandleType.Pinned);

            try
            {
                int decoded = OpusPInvoke.OpusDecode(
                    handle,
                    IntPtr.Add(dataHandle.AddrOfPinnedObject(), dataOffset), dataLen,
                    IntPtr.Add(pcmHandle.AddrOfPinnedObject(), pcmOffset * sizeof(short)), pcmLength,
                    decodeFec ? 1 : 0);
                if (decoded < (int)OpusPInvoke.ErrorCodes.OK)
                {
                    throw new Exception(OpusPInvoke.GetMessage((OpusPInvoke.ErrorCodes)decoded));
                }
                return(decoded);
            }
            finally
            {
                dataHandle.Free();
                pcmHandle.Free();
            }
        }
 protected override bool ReleaseHandle()
 {
     OpusPInvoke.OpusDecoderDestroy(handle);
     handle = IntPtr.Zero;
     return(IsInvalid);
 }