Ejemplo n.º 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);
        }
Ejemplo n.º 2
0
 public void Dispose()
 {
     if (disposed)
     {
         return;
     }
     GC.SuppressFinalize(this);
     if (_encoder != IntPtr.Zero)
     {
         OpusAPI.opus_encoder_destroy(_encoder);
         _encoder = IntPtr.Zero;
     }
     disposed = true;
 }
Ejemplo n.º 3
0
        /// <summary>
        /// Produces Opus encoded audio from PCM samples.
        /// </summary>
        /// <param name="inputPcmSamples">PCM samples to encode.</param>
        /// <returns>Opus encoded audio buffer.</returns>
        public byte[] Encode(byte[] inputPcmSamples)
        {
            int    frames     = FrameCount(inputPcmSamples);
            IntPtr encodedPtr = Marshal.AllocHGlobal(MaxDataBytes);
            IntPtr inputPtr   = Marshal.AllocHGlobal(inputPcmSamples.Length);

            Marshal.Copy(inputPcmSamples, 0, inputPtr, inputPcmSamples.Length);
            int length = OpusAPI.opus_encode(_encoder, inputPtr, frames, encodedPtr, MaxDataBytes);

            Marshal.FreeHGlobal(inputPtr);
            byte[] encodedBytes = new byte[length];
            Marshal.Copy(encodedPtr, encodedBytes, 0, length);
            Marshal.FreeHGlobal(encodedPtr);
            if (length < 0)
            {
                throw new Exception("Encoding failed - " + ((Errors)length).ToString());
            }
            return(encodedBytes);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Creates a new Opus encoder.
        /// </summary>
        /// <param name="inputSamplingRate">Sampling rate of the input signal (Hz). This must be one of 8000, 12000, 16000, 24000, or 48000.</param>
        /// <param name="inputChannels">Number of channels (1 or 2) in input signal.</param>
        /// <param name="application">Coding mode.</param>
        /// <returns>A new <c>OpusEncoder</c></returns>
        public static OpusEncoder Create(int inputSamplingRate, int inputChannels, Application application)
        {
            if (inputSamplingRate != 8000 &&
                inputSamplingRate != 12000 &&
                inputSamplingRate != 16000 &&
                inputSamplingRate != 24000 &&
                inputSamplingRate != 48000)
            {
                throw new ArgumentOutOfRangeException("inputSamplingRate");
            }
            if (inputChannels != 1 && inputChannels != 2)
            {
                throw new ArgumentOutOfRangeException("inputChannels");
            }

            IntPtr error;
            IntPtr encoder = OpusAPI.opus_encoder_create(inputSamplingRate, inputChannels, (int)application, out error);

            if ((Errors)error != Errors.OK)
            {
                throw new Exception("Exception occured while creating encoder");
            }
            return(new OpusEncoder(encoder, inputSamplingRate, inputChannels, application));
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Creates a new Opus decoder.
        /// </summary>
        /// <param name="outputSampleRate">Sample rate to decode at (Hz). This must be one of 8000, 12000, 16000, 24000, or 48000.</param>
        /// <param name="outputChannels">Number of channels to decode.</param>
        /// <returns>A new <c>OpusDecoder</c>.</returns>
        public static OpusDecoder Create(int outputSampleRate, int outputChannels)
        {
            if (outputSampleRate != 8000 &&
                outputSampleRate != 12000 &&
                outputSampleRate != 16000 &&
                outputSampleRate != 24000 &&
                outputSampleRate != 48000)
            {
                throw new ArgumentOutOfRangeException("inputSamplingRate");
            }
            if (outputChannels != 1 && outputChannels != 2)
            {
                throw new ArgumentOutOfRangeException("inputChannels");
            }

            IntPtr error;
            IntPtr decoder = OpusAPI.opus_decoder_create(outputSampleRate, outputChannels, out error);

            if ((Errors)error != Errors.OK)
            {
                throw new Exception("Exception occured while creating decoder");
            }
            return(new OpusDecoder(decoder, outputSampleRate, outputChannels));
        }