Пример #1
0
        /// <summary>
        /// Produces Opus encoded audio from PCM samples.
        /// </summary>
        /// <param name="inputPcmSamples">PCM samples to encode.</param>
        /// <param name="sampleLength">How many bytes to encode.</param>
        /// <param name="encodedLength">Set to length of encoded audio.</param>
        /// <returns>Opus encoded audio buffer.</returns>
        public unsafe int Encode(MemoryStream input, MemoryStream output, int frameSize)
        {
            if (disposed)
            {
                throw new ObjectDisposedException("OpusEncoder");
            }

            output.SetLength(0);
            IntPtr encodedPtr;

            byte[] encoded = output.GetBuffer();
            int    res;

            fixed(byte *benc = encoded)
            {
                encodedPtr = new IntPtr((void *)benc);
                res        = OpusLibrary.opus_encode(_encoder, input.ToArray(), frameSize, encodedPtr, output.Capacity);
            }

            if ((Errors)res != Errors.OK)
            {
                throw new Exception("Encoding failed - " + ((Errors)res).ToString());
            }
            output.Position = res;
            output.SetLength(output.Position);
            output.Position = 0;

            return(res);
        }
Пример #2
0
        /// <summary>
        /// Decodes the Opus encoded bytes to PCM audio samples
        /// </summary>
        /// <param name="input">The memorystream with the bytes</param>
        /// <param name="output">The memorystream to put the decoded bytes to</param>
        /// <returns>The amount of samples done</returns>
        public unsafe int Decode(MemoryStream input, MemoryStream output)
        {
            if (disposed)
            {
                throw new ObjectDisposedException("OpusDecoder");
            }

            IntPtr decodedPtr;

            output.SetLength(0);
            byte[] buffer = output.GetBuffer();
            int    res;

            fixed(byte *add = buffer)
            {
                decodedPtr = new IntPtr((void *)add);
                res        = OpusLibrary.opus_decode(_decoder, input.ToArray(), Convert.ToInt32(input.Length - input.Position), decodedPtr, Convert.ToInt32(output.Length - output.Position) / channels, 0);
            }

            if ((Errors)res != Errors.OK)
            {
                throw new InvalidOperationException("Error decoding");
            }
            output.Position = res * channels;
            output.SetLength(output.Position);
            output.Position = 0;
            return(res);
        }
Пример #3
0
        public OpusEncoder(int sampleRate, int channels, int quality)
        {
            IntPtr error;

            _encoder = OpusLibrary.opus_encoder_create(sampleRate, channels, (int)Application.Audio, out error);

            if ((Errors)error != Errors.OK)
            {
                throw new InvalidOperationException("Unable to create decoder of sampleRate " + sampleRate + " and channels " + channels);
            }
        }
Пример #4
0
        public OpusDecoder(int sampleRate, int channels)
        {
            IntPtr error;

            _decoder      = OpusLibrary.opus_decoder_create(sampleRate, channels, out error);
            this.channels = channels;

            if ((Errors)error != Errors.OK)
            {
                throw new InvalidOperationException("Unable to create decoder of sampleRate " + sampleRate + " and channels " + channels);
            }
        }
Пример #5
0
        /// <summary>
        /// Disposes all the resources used to clean the memory
        /// </summary>
        public void Dispose()
        {
            if (disposed)
            {
                return;
            }

            GC.SuppressFinalize(this);
            if (_decoder != IntPtr.Zero)
            {
                OpusLibrary.opus_decoder_destroy(_decoder);
                _decoder = IntPtr.Zero;
            }
            disposed = true;
        }