Beispiel #1
0
        /// <summary>
        /// Produces PCM samples from Opus encoded data.
        /// </summary>
        /// <param name="inputOpusData">Opus encoded data to decode, null for dropped packet.</param>
        /// <param name="dataLength">Length of data to decode.</param>
        /// <returns>PCM audio samples.</returns>
        public byte[] Decode(byte[] inputOpusData, int dataLength)
        {
            int    frameCount = FrameCount(MaxDataBytes);
            int    length;
            IntPtr decodedPtr = Marshal.AllocHGlobal(MaxDataBytes);

            if (inputOpusData == null)
            {
                length = OpusAPI.opus_decode(_decoder, IntPtr.Zero, 0, decodedPtr, frameCount, (ForwardErrorCorrection) ? 1 : 0);
            }
            else
            {
                IntPtr inputPtr = Marshal.AllocHGlobal(inputOpusData.Length);
                Marshal.Copy(inputOpusData, 0, inputPtr, inputOpusData.Length);
                length = OpusAPI.opus_decode(_decoder, inputPtr, dataLength, decodedPtr, frameCount, 0);
                Marshal.FreeHGlobal(inputPtr);
            }
            if (length < 0)
            {
                throw new Exception("Decoding failed - " + ((Errors)length).ToString());
            }
            byte[] decoded = new byte[length * 2];
            Marshal.Copy(decodedPtr, decoded, 0, length * 2);
            Marshal.FreeHGlobal(decodedPtr);
            return(decoded);
        }