Beispiel #1
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);
        }