Exemplo n.º 1
0
            /// <summary>
            /// Encode audio samples.
            /// </summary>
            /// <returns>The total number of bytes written to dstOutputBuffer.</returns>
            public int EncodeFloats(ArraySegment <float> sourcePcm, ArraySegment <byte> dstEncoded)
            {
                if (sourcePcm.Array == null)
                {
                    throw new ArgumentNullException("sourcePcm", Log.PossibleBugMessage("source pcm must not be null", "58AE3110-8F9A-4C36-9520-B7F3383096EC"));
                }

                if (dstEncoded.Array == null)
                {
                    throw new ArgumentNullException("dstEncoded", Log.PossibleBugMessage("destination must not be null", "36C327BB-A128-400D-AFB3-FF760A1562C1"));
                }

                int encodedLen;

                using (var encoder = _encoder.Lock())
                {
                    if (encoder.Value == IntPtr.Zero)
                    {
                        throw new DissonanceException(Log.PossibleBugMessage("Attempted to access a null Opus encoder", "647001C3-39BB-418D-99EF-1D66B8EA633C"));
                    }

                    using (var srcHandle = sourcePcm.Pin())
                        using (var dstHandle = dstEncoded.Pin())
                        {
                            encodedLen = OpusNativeMethods.opus_encode_float(encoder.Value, srcHandle.Ptr, sourcePcm.Count, dstHandle.Ptr, dstEncoded.Count);
                        }
                }

                if (encodedLen < 0)
                {
                    throw new OpusException(Log.PossibleBugMessage(string.Format("Encoding failed: {0}", (OpusErrors)encodedLen), "9C923F57-146B-47CB-8EEE-5BF129FA3124"));
                }

                return(encodedLen);
            }
Exemplo n.º 2
0
            /// <summary>
            /// Encode audio samples.
            /// </summary>
            /// <returns>The total number of bytes written to dstOutputBuffer.</returns>
            public int EncodeFloats(ArraySegment <float> sourcePcm, ArraySegment <byte> dstEncoded)
            {
                if (sourcePcm.Array == null)
                {
                    throw new ArgumentNullException("sourcePcm", Log.PossibleBugMessage("source pcm must not be null", "58AE3110-8F9A-4C36-9520-B7F3383096EC"));
                }
                if (dstEncoded.Array == null)
                {
                    throw new ArgumentNullException("dstEncoded", Log.PossibleBugMessage("destination must not be null", "36C327BB-A128-400D-AFB3-FF760A1562C1"));
                }
                if (Array.IndexOf(PermittedFrameSizes, sourcePcm.Count) == -1)
                {
                    throw new ArgumentException(Log.PossibleBugMessage(string.Format("Incorrect frame size '{0}'", sourcePcm.Count), "6AFD9ADF-1D15-4197-99E9-5A19ECB8CD20"), "sourcePcm");
                }

                int encodedLen;

                using (var srcHandle = sourcePcm.Pin())
                    using (var dstHandle = dstEncoded.Pin())
                    {
                        encodedLen = OpusNativeMethods.opus_encode_float(_encoder, srcHandle.Ptr, sourcePcm.Count, dstHandle.Ptr, dstEncoded.Count);
                    }

                if (encodedLen < 0)
                {
                    throw new OpusException(Log.PossibleBugMessage(string.Format("Encoding failed: {0}", (OpusErrors)encodedLen), "9C923F57-146B-47CB-8EEE-5BF129FA3124"));
                }
                return(encodedLen);
            }