示例#1
0
        /// <summary> Creates a new Opus encoder. </summary>
        /// <param name="samplingRate">Sampling rate of the input signal (Hz). Supported Values:  8000, 12000, 16000, 24000, or 48000</param>
        /// <param name="channels">Number of channels in input signal. Supported Values: 1 or 2</param>
        /// <param name="frameLength">Length, in milliseconds, that each frame takes. Supported Values: 2.5, 5, 10, 20, 40, 60</param>
        /// <param name="bitrate">Bitrate (kbit/s) used for this encoder. Supported Values: 1-512. Null will use the recommended bitrate. </param>
        /// <param name="application">Coding mode.</param>
        public OpusEncoder(int samplingRate, int channels, int frameLength, int?bitrate, OpusApplication application)
            : base(samplingRate, channels, frameLength)
        {
            if (bitrate != null && (bitrate < 1 || bitrate > 320)) //idk good averagee i guess
            {
                throw new ArgumentOutOfRangeException(nameof(bitrate));
            }

            BitRate     = bitrate;
            Application = application;

            OpusError error;

            _ptr = UnsafeNativeMethods.CreateEncoder(samplingRate, channels, (int)application, out error);
            if (error != OpusError.OK)
            {
                throw new InvalidOperationException($"Error occured while creating encoder: {error}");
            }

            SetForwardErrorCorrection(true);
            if (bitrate != null)
            {
                SetBitrate(bitrate.Value);
            }
        }
示例#2
0
		/// <summary> Creates a new Opus encoder. </summary>
		/// <param name="samplingRate">Sampling rate of the input signal (Hz). Supported Values:  8000, 12000, 16000, 24000, or 48000.</param>
		/// <param name="channels">Number of channels (1 or 2) in input signal.</param>
		/// <param name="frameLength">Length, in milliseconds, that each frame takes. Supported Values: 2.5, 5, 10, 20, 40, 60</param>
		/// <param name="bitrate">Bitrate (kbit/s) used for this encoder. Supported Values: 1-512. Null will use the recommended bitrate. </param>
		/// <param name="application">Coding mode.</param>
		/// <returns>A new <c>OpusEncoder</c></returns>
		public OpusEncoder(int samplingRate, int channels, int frameLength, int? bitrate, OpusApplication application)
		{
			if (samplingRate != 8000 && samplingRate != 12000 &&
				samplingRate != 16000 && samplingRate != 24000 &&
				samplingRate != 48000)
				throw new ArgumentOutOfRangeException(nameof(samplingRate));
			if (channels != 1 && channels != 2)
				throw new ArgumentOutOfRangeException(nameof(channels));
			if (bitrate != null && (bitrate < 1 || bitrate > 512))
				throw new ArgumentOutOfRangeException(nameof(bitrate));

			InputSamplingRate = samplingRate;
			InputChannels = channels;
			Application = application;
			FrameLength = frameLength;
			SampleSize = (BitsPerSample / 8) * channels;
			SamplesPerFrame = samplingRate / 1000 * FrameLength;
			FrameSize = SamplesPerFrame * SampleSize;
			BitRate = bitrate;

			OpusError error;
			_ptr = UnsafeNativeMethods.CreateEncoder(samplingRate, channels, (int)application, out error);
			if (error != OpusError.OK)
				throw new InvalidOperationException($"Error occured while creating encoder: {error}");

			SetForwardErrorCorrection(true);
			if (bitrate != null)
				SetBitrate(bitrate.Value);
		}
示例#3
0
 public void UpdateApplication(OpusApplication app)
 {
     _codecParamLock.WaitOne();
     _application       = app;
     _codecParamChanged = true;
     _codecParamLock.ReleaseMutex();
 }
示例#4
0
        /// <summary>
        /// Creates a multichannel Opus encoder using the "new API". This constructor allows you to use predefined Vorbis channel mappings, or specify your own.
        /// </summary>
        /// <param name="Fs">The samples rate of the input</param>
        /// <param name="channels">The total number of channels to encode (1 - 255)</param>
        /// <param name="mapping_family">The mapping family to use. 0 = mono/stereo, 1 = use Vorbis mappings, 255 = use raw channel mapping</param>
        /// <param name="streams">The number of streams to encode</param>
        /// <param name="coupled_streams">The number of coupled streams</param>
        /// <param name="mapping">A raw mapping of input/output channels</param>
        /// <param name="application">The application to use for the encoders</param>
        public static OpusMSEncoder CreateSurround(
            int Fs,
            int channels,
            int mapping_family,
            out int streams,
            out int coupled_streams,
            byte[] mapping,
            OpusApplication application
            )
        {
            int           ret;
            OpusMSEncoder st;

            if ((channels > 255) || (channels < 1) || application == OpusApplication.OPUS_APPLICATION_UNIMPLEMENTED)
            {
                throw new ArgumentException("Invalid channel count or application");
            }
            BoxedValueInt nb_streams         = new BoxedValueInt();
            BoxedValueInt nb_coupled_streams = new BoxedValueInt();

            GetStreamCount(channels, mapping_family, nb_streams, nb_coupled_streams);

            st  = new OpusMSEncoder(nb_streams.Val, nb_coupled_streams.Val);
            ret = st.opus_multistream_surround_encoder_init(Fs, channels, mapping_family, out streams, out coupled_streams, mapping, application);
            if (ret != OpusError.OPUS_OK)
            {
                if (ret == OpusError.OPUS_BAD_ARG)
                {
                    throw new ArgumentException("Bad argument passed to CreateSurround");
                }
                throw new OpusException("Could not create multistream encoder", ret);
            }
            return(st);
        }
示例#5
0
        /// <summary>
        /// Creates a new multichannel Opus encoder using the "old API".
        /// </summary>
        /// <param name="Fs">The sample rate of the input signal</param>
        /// <param name="channels">The number of channels to encode (1 - 255)</param>
        /// <param name="streams">The number of streams to encode</param>
        /// <param name="coupled_streams">The number of coupled streams</param>
        /// <param name="mapping">A raw mapping between input and output channels</param>
        /// <param name="application">The application to use for the encoder</param>
        public static OpusMSEncoder Create(
            int Fs,
            int channels,
            int streams,
            int coupled_streams,
            byte[] mapping,
            OpusApplication application
            )
        {
            int ret;

            if ((channels > 255) || (channels < 1) || (coupled_streams > streams) ||
                (streams < 1) || (coupled_streams < 0) || (streams > 255 - coupled_streams))
            {
                throw new ArgumentException("Invalid channel / stream configuration");
            }
            OpusMSEncoder st = new OpusMSEncoder(streams, coupled_streams);

            ret = st.opus_multistream_encoder_init(Fs, channels, streams, coupled_streams, mapping, application, 0);
            if (ret != OpusError.OPUS_OK)
            {
                if (ret == OpusError.OPUS_BAD_ARG)
                {
                    throw new ArgumentException("OPUS_BAD_ARG when creating MS encoder");
                }
                throw new OpusException("Could not create MS encoder", ret);
            }
            return(st);
        }
示例#6
0
 public OpusEncoder(int samples, int bit_rate, int channels, OpusApplication application, IAudioEncoderCallback encoder_callback)
 {
     this.samples        = samples;
     bitRate             = bit_rate;
     this.channels       = channels;
     opusApplication     = application;
     frameSize           = CodecTools.getPcmFrameByteSize(samples, 16, channels) * 20;
     encodedDataCallback = encoder_callback;
 }
示例#7
0
        internal OpusEncodeStream(AudioClient audioClient, byte[] secretKey, int samplesPerFrame, uint ssrc, int?bitrate = null,
                                  OpusApplication application = OpusApplication.MusicOrMixed, int bufferSize = 4000)
            : base(audioClient, secretKey, samplesPerFrame, ssrc, bufferSize)
        {
            _encoder = new OpusEncoder(SampleRate, Channels);

            _encoder.SetForwardErrorCorrection(true);
            if (bitrate != null)
            {
                _encoder.SetBitrate(bitrate.Value);
            }
        }
示例#8
0
 private static string PrintApplication(OpusApplication app)
 {
     if (app == OpusApplication.OPUS_APPLICATION_AUDIO)
     {
         return("Music   ");
     }
     else if (app == OpusApplication.OPUS_APPLICATION_VOIP)
     {
         return("Voip    ");
     }
     return("LowDelay");
 }
示例#9
0
        public OpusEncoder(int samplingRate, int channels, OpusApplication application = OpusApplication.MusicOrMixed)
            : base(samplingRate, channels)
        {
            Application = application;

            OpusError error;

            _ptr = CreateEncoder(samplingRate, channels, (int)application, out error);
            if (error != OpusError.OK)
            {
                throw new Exception($"Opus Error: {error}");
            }
        }
示例#10
0
        /// <summary> Creates a new Opus encoder. </summary>
        /// <param name="samplingRate">Sampling rate of the input signal (Hz). Supported Values:  8000, 12000, 16000, 24000, or 48000</param>
        /// <param name="channels">Number of channels in input signal. Supported Values: 1 or 2</param>
        /// <param name="frameLength">Length, in milliseconds, that each frame takes. Supported Values: 2.5, 5, 10, 20, 40, 60</param>
        /// <param name="bitrate">Bitrate (kbit/s) used for this encoder. Supported Values: 1-512. Null will use the recommended bitrate. </param>
        /// <param name="application">Coding mode.</param>
        public OpusEncoder(int samplingRate, int channels, int frameLength, int? bitrate, OpusApplication application)
            : base(samplingRate, channels, frameLength)
		{
			if (bitrate != null && (bitrate < 1 || bitrate > 512))
				throw new ArgumentOutOfRangeException(nameof(bitrate));

			BitRate = bitrate;
            Application = application;

            OpusError error;
			_ptr = UnsafeNativeMethods.CreateEncoder(samplingRate, channels, (int)application, out error);
			if (error != OpusError.OK)
				throw new InvalidOperationException($"Error occured while creating encoder: {error}");

			SetForwardErrorCorrection(true);
			if (bitrate != null)
				SetBitrate(bitrate.Value);
		}
示例#11
0
        internal static IntPtr opus_encoder_create(int sampleRate, int channelCount, OpusApplication application, out OpusErrors error)
        {
            int    size = opus_encoder_get_size(channelCount);
            IntPtr ptr  = Marshal.AllocHGlobal(size);

            error = opus_encoder_init(ptr, sampleRate, channelCount, (int)application);

            if (error != OpusErrors.Ok)
            {
                if (ptr != IntPtr.Zero)
                {
                    destroy_opus(ptr);
                    ptr = IntPtr.Zero;
                }
            }

            return(ptr);
        }
示例#12
0
        public Encoder(
            SamplingFrequency samplingFrequency,
            NumChannels channels,
            OpusApplication application)
        {
            this.channels = channels;
            ErrorCode error;

            encoder = Library.OpusEncoderCreate(
                samplingFrequency,
                channels,
                application,
                out error);
            if (error != ErrorCode.OK)
            {
                UnityEngine.Debug.LogError("[UnityOpus] Failed to init encoder. Error code: " + error.ToString());
                encoder = IntPtr.Zero;
            }
        }
示例#13
0
        /// <summary> Creates a new Opus encoder. </summary>
        /// <param name="samplingRate">Sampling rate of the input signal (Hz). Supported Values:  8000, 12000, 16000, 24000, or 48000.</param>
        /// <param name="channels">Number of channels (1 or 2) in input signal.</param>
        /// <param name="frameLength">Length, in milliseconds, that each frame takes. Supported Values: 2.5, 5, 10, 20, 40, 60</param>
        /// <param name="bitrate">Bitrate (kbit/s) used for this encoder. Supported Values: 1-512. Null will use the recommended bitrate. </param>
        /// <param name="application">Coding mode.</param>
        /// <returns>A new <c>OpusEncoder</c></returns>
        public OpusEncoder(int samplingRate, int channels, int frameLength, int?bitrate, OpusApplication application)
        {
            if (samplingRate != 8000 && samplingRate != 12000 &&
                samplingRate != 16000 && samplingRate != 24000 &&
                samplingRate != 48000)
            {
                throw new ArgumentOutOfRangeException(nameof(samplingRate));
            }
            if (channels != 1 && channels != 2)
            {
                throw new ArgumentOutOfRangeException(nameof(channels));
            }
            if (bitrate != null && (bitrate < 1 || bitrate > 512))
            {
                throw new ArgumentOutOfRangeException(nameof(bitrate));
            }

            InputSamplingRate = samplingRate;
            InputChannels     = channels;
            Application       = application;
            FrameLength       = frameLength;
            SampleSize        = (BitsPerSample / 8) * channels;
            SamplesPerFrame   = samplingRate / 1000 * FrameLength;
            FrameSize         = SamplesPerFrame * SampleSize;
            BitRate           = bitrate;

            OpusError error;

            _ptr = UnsafeNativeMethods.CreateEncoder(samplingRate, channels, (int)application, out error);
            if (error != OpusError.OK)
            {
                throw new InvalidOperationException($"Error occured while creating encoder: {error}");
            }

            SetForwardErrorCorrection(true);
            if (bitrate != null)
            {
                SetBitrate(bitrate.Value);
            }
        }
示例#14
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 OpusEncoder(int inputSamplingRate, int inputChannels, OpusApplication 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;
            this._encoder = Wrapper.opus_encoder_create(inputSamplingRate, inputChannels, (int)application, out error);

            if ((Errors)error != Errors.OK)
                throw new Exception("Exception occured while creating encoder");

            this.InputSamplingRate = inputSamplingRate;
            this.InputChannels = inputChannels;
            this.Application = application;
            this.MaxDataBytes = 4000;
        }
示例#15
0
 public static extern IntPtr OpusEncoderCreate(
     SamplingFrequency samplingFrequency,
     NumChannels channels,
     OpusApplication application,
     out ErrorCode error);
示例#16
0
 public OpusEncodeStream CreatePCMStream(int samplesPerFrame, int?bitrate = null,
                                         OpusApplication application      = OpusApplication.MusicOrMixed, int bufferSize = 4000)
 {
     return(new OpusEncodeStream(this, _secretKey, samplesPerFrame, _ssrc, bitrate, application, bufferSize));
 }
示例#17
0
        public static void Main(string[] args)
        {
            LoadTestFile(8, false);
            LoadTestFile(12, false);
            LoadTestFile(16, false);
            LoadTestFile(24, false);
            LoadTestFile(48, false);
            LoadTestFile(8, true);
            LoadTestFile(12, true);
            LoadTestFile(16, true);
            LoadTestFile(24, true);
            LoadTestFile(48, true);

            OpusApplication[] Applications = new OpusApplication[] { OpusApplication.OPUS_APPLICATION_AUDIO, OpusApplication.OPUS_APPLICATION_VOIP, OpusApplication.OPUS_APPLICATION_RESTRICTED_LOWDELAY };
            int[]             Bitrates     = new int[] { -1, 6, 16, 20, 32, 64, 500 };
            int[]             Channels     = new int[] { 1, 2 };
            int[]             Complexities = new int[] { 0, 2, 4, 6, 8, 10 };
            int[]             SampleRates  = new int[] { 8000, 12000, 16000, 24000, 48000 };
            double[]          FrameSizes   = new double[] { 2.5, 5, 10, 20, 40, 60 };
            int[]             PacketLosses = new int[] { 0, 20 };
            OpusMode[]        ForceModes   = new OpusMode[] { OpusMode.MODE_AUTO, OpusMode.MODE_CELT_ONLY, OpusMode.MODE_SILK_ONLY };
            bool[]            DTXModes     = new bool[] { false, true };
            int[]             VBRModes     = new int[] { 0, 1, 2 };

            IList <TestParameters> allTests = new List <TestParameters>();

            for (int app_idx = 0; app_idx < Applications.Length; app_idx++)
            {
                for (int plc_idx = 0; plc_idx < PacketLosses.Length; plc_idx++)
                {
                    for (int chan_idx = 0; chan_idx < Channels.Length; chan_idx++)
                    {
                        for (int sr_idx = 0; sr_idx < SampleRates.Length; sr_idx++)
                        {
                            for (int fs_idx = 0; fs_idx < FrameSizes.Length; fs_idx++)
                            {
                                for (int cpx_idx = 0; cpx_idx < Complexities.Length; cpx_idx++)
                                {
                                    for (int bit_idx = 0; bit_idx < Bitrates.Length; bit_idx++)
                                    {
                                        for (int fm_idx = 0; fm_idx < ForceModes.Length; fm_idx++)
                                        {
                                            for (int dtx_idx = 0; dtx_idx < DTXModes.Length; dtx_idx++)
                                            {
                                                for (int vbr_idx = 0; vbr_idx < VBRModes.Length; vbr_idx++)
                                                {
                                                    TestParameters newParams = new TestParameters()
                                                    {
                                                        Application       = Applications[app_idx],
                                                        Bitrate           = Bitrates[bit_idx],
                                                        Channels          = Channels[chan_idx],
                                                        Complexity        = Complexities[cpx_idx],
                                                        PacketLossPercent = PacketLosses[plc_idx],
                                                        SampleRate        = SampleRates[sr_idx],
                                                        FrameSize         = FrameSizes[fs_idx],
                                                        ForceMode         = ForceModes[fm_idx],
                                                        UseDTX            = DTXModes[dtx_idx]
                                                    };
                                                    if (VBRModes[vbr_idx] == 0)
                                                    {
                                                        newParams.UseVBR         = false;
                                                        newParams.ConstrainedVBR = false;
                                                    }
                                                    else if (VBRModes[vbr_idx] == 1)
                                                    {
                                                        newParams.UseVBR         = true;
                                                        newParams.ConstrainedVBR = false;
                                                    }
                                                    else if (VBRModes[vbr_idx] == 2)
                                                    {
                                                        newParams.UseVBR         = true;
                                                        newParams.ConstrainedVBR = true;
                                                    }

                                                    // Validate params
                                                    if (newParams.Bitrate > 40 || newParams.FrameSize < 10)
                                                    {
                                                        // No FEC outside of SILK mode
                                                        if (newParams.PacketLossPercent > 0)
                                                        {
                                                            continue;
                                                        }
                                                        // No DTX outside of SILK mode
                                                        if (newParams.UseDTX)
                                                        {
                                                            continue;
                                                        }
                                                        if (newParams.ForceMode == OpusMode.MODE_SILK_ONLY)
                                                        {
                                                            continue;
                                                        }
                                                    }
                                                    // Constrained VBR only applies to CELT
                                                    if (newParams.ForceMode == OpusMode.MODE_SILK_ONLY && newParams.ConstrainedVBR)
                                                    {
                                                        continue;
                                                    }
                                                    // 12Khz + 2.5ms triggers an opus bug for now
                                                    if (newParams.SampleRate == 12000 && newParams.FrameSize < 5)
                                                    {
                                                        continue;
                                                    }

                                                    allTests.Add(newParams);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            TestParameters[] allTestsRandom = allTests.ToArray();
            int numTestCases = allTests.Count;

            Console.WriteLine("Preparing " + numTestCases + " test cases");

            // Shuffle the test list
            if (true)
            {
                TestParameters temp;
                int            a;
                int            b;
                Random         rand = new Random();
                for (int c = 0; c < numTestCases; c++)
                {
                    a    = rand.Next(numTestCases);
                    b    = rand.Next(numTestCases);
                    temp = allTestsRandom[a];
                    allTestsRandom[a] = allTestsRandom[b];
                    allTestsRandom[b] = temp;
                }
            }

            double concentusTime = 0.001;
            double opusTime      = 0;
            int    passedTests   = 0;
            int    testsRun      = 0;

            foreach (TestParameters p in allTestsRandom)
            {
                testsRun++;
                Console.Write("{0,5} {1} {2} Cpx={3,2} {4}Kbps {5,2}Khz {6,3} Ms PLC {7,2}% {8} {9} {10}... ",
                              testsRun,
                              PrintApplication(p.Application),
                              p.Channels == 1 ? "Mono  " : "Stereo",
                              p.Complexity,
                              p.Bitrate > 0 ? string.Format("{0,3}", p.Bitrate) : "VAR",
                              p.SampleRate / 1000,
                              p.FrameSize,
                              p.PacketLossPercent,
                              PrintVBRMode(p),
                              p.UseDTX ? "DTX" : "   ",
                              PrintForceMode(p.ForceMode));

                TestResults response = TestDriver.RunTest(p, GetTestSample(p));

                if (response.Passed)
                {
                    passedTests++;
                    if (passedTests > 7)
                    {
                        concentusTime *= 0.999;
                        opusTime      *= 0.999;
                        concentusTime += response.ConcentusTimeMs;
                        opusTime      += response.OpusTimeMs;
                    }
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.WriteLine("{0} (Speed {1:F2}% Pass {2:F2}%)", response.Message, (opusTime * 100 / concentusTime), ((double)passedTests * 100 / testsRun));
                    Console.ForegroundColor = ConsoleColor.Gray;
                }
                else
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("FAIL: " + response.Message);
                    Console.ForegroundColor = ConsoleColor.Gray;

                    //if (response.FrameCount == 0)
                    //{
                    //    PrintShortArray(response.FailureFrame);
                    //    Console.WriteLine(response.FrameLength);
                    //    Console.ReadLine();
                    //}
                }
            }

            Console.ForegroundColor = ConsoleColor.Cyan;
            Console.WriteLine("All tests FINISHED");
            Console.WriteLine("{0} out of {1} tests passed ({2:F2}%)", passedTests, numTestCases, ((double)passedTests * 100 / numTestCases));
            Console.WriteLine("Speed benchmark was {0:F2}%", (opusTime * 100 / concentusTime));
        }
示例#18
0
 public static extern IntPtr opus_encoder_create(int fs, int channels, OpusApplication application, ref ErrorCodes error);
示例#19
0
        public void ShotgunTest()
        {
            OpusApplication[] Applications = new OpusApplication[] { OpusApplication.OPUS_APPLICATION_AUDIO, OpusApplication.OPUS_APPLICATION_VOIP, OpusApplication.OPUS_APPLICATION_RESTRICTED_LOWDELAY };
            int[]             Bitrates     = new int[] { -1, 6, 16, 20, 32, 64, 500 };
            int[]             Channels     = new int[] { 1, 2 };
            int[]             Complexities = new int[] { 0, 2, 4, 6, 8, 10 };
            int[]             SampleRates  = new int[] { 8000, 12000, 16000, 24000, 48000 };
            double[]          FrameSizes   = new double[] { 2.5, 5, 10, 20, 40, 60 };
            int[]             PacketLosses = new int[] { 0, 20 };
            OpusMode[]        ForceModes   = new OpusMode[] { OpusMode.MODE_AUTO, OpusMode.MODE_CELT_ONLY, OpusMode.MODE_SILK_ONLY };
            bool[]            DTXModes     = new bool[] { false, true };
            int[]             VBRModes     = new int[] { 0, 1, 2 };

            IList <TestParameters> allTests = new List <TestParameters>();

            for (int app_idx = 0; app_idx < Applications.Length; app_idx++)
            {
                for (int plc_idx = 0; plc_idx < PacketLosses.Length; plc_idx++)
                {
                    for (int chan_idx = 0; chan_idx < Channels.Length; chan_idx++)
                    {
                        for (int sr_idx = 0; sr_idx < SampleRates.Length; sr_idx++)
                        {
                            for (int fs_idx = 0; fs_idx < FrameSizes.Length; fs_idx++)
                            {
                                for (int cpx_idx = 0; cpx_idx < Complexities.Length; cpx_idx++)
                                {
                                    for (int bit_idx = 0; bit_idx < Bitrates.Length; bit_idx++)
                                    {
                                        for (int fm_idx = 0; fm_idx < ForceModes.Length; fm_idx++)
                                        {
                                            for (int dtx_idx = 0; dtx_idx < DTXModes.Length; dtx_idx++)
                                            {
                                                for (int vbr_idx = 0; vbr_idx < VBRModes.Length; vbr_idx++)
                                                {
                                                    TestParameters newParams = new TestParameters()
                                                    {
                                                        Application       = Applications[app_idx],
                                                        Bitrate           = Bitrates[bit_idx],
                                                        Channels          = Channels[chan_idx],
                                                        Complexity        = Complexities[cpx_idx],
                                                        PacketLossPercent = PacketLosses[plc_idx],
                                                        SampleRate        = SampleRates[sr_idx],
                                                        FrameSize         = FrameSizes[fs_idx],
                                                        ForceMode         = ForceModes[fm_idx],
                                                        UseDTX            = DTXModes[dtx_idx]
                                                    };
                                                    if (VBRModes[vbr_idx] == 0)
                                                    {
                                                        newParams.UseVBR         = false;
                                                        newParams.ConstrainedVBR = false;
                                                    }
                                                    else if (VBRModes[vbr_idx] == 1)
                                                    {
                                                        newParams.UseVBR         = true;
                                                        newParams.ConstrainedVBR = false;
                                                    }
                                                    else if (VBRModes[vbr_idx] == 2)
                                                    {
                                                        newParams.UseVBR         = true;
                                                        newParams.ConstrainedVBR = true;
                                                    }

                                                    // Validate params
                                                    if (newParams.Bitrate > 40 || newParams.FrameSize < 10)
                                                    {
                                                        // No FEC outside of SILK mode
                                                        if (newParams.PacketLossPercent > 0)
                                                        {
                                                            continue;
                                                        }
                                                        // No DTX outside of SILK mode
                                                        if (newParams.UseDTX)
                                                        {
                                                            continue;
                                                        }
                                                        if (newParams.ForceMode == OpusMode.MODE_SILK_ONLY)
                                                        {
                                                            continue;
                                                        }
                                                    }
                                                    // Constrained VBR only applies to CELT
                                                    if (newParams.ForceMode == OpusMode.MODE_SILK_ONLY && newParams.ConstrainedVBR)
                                                    {
                                                        continue;
                                                    }
                                                    // 12Khz + 2.5ms triggers an opus bug for now
                                                    if (newParams.SampleRate == 12000 && newParams.FrameSize < 5)
                                                    {
                                                        continue;
                                                    }

                                                    allTests.Add(newParams);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            TestParameters[] allTestsRandom = allTests.ToArray();
            int numTestCases = allTests.Count;

            // Shuffle the test list
            TestParameters temp;
            int            a;
            int            b;
            Random         rand = new Random();

            for (int c = 0; c < numTestCases; c++)
            {
                a    = rand.Next(numTestCases);
                b    = rand.Next(numTestCases);
                temp = allTestsRandom[a];
                allTestsRandom[a] = allTestsRandom[b];
                allTestsRandom[b] = temp;
            }

            int testsRun = 0;

            foreach (TestParameters p in allTestsRandom)
            {
                testsRun++;
                TestResults response = TestDriver.RunTest(p, GetTestSample(p));
                Assert.IsTrue(response.Passed);
                if (testsRun > 50)
                {
                    break;
                }
            }
        }
示例#20
0
 public void SetApplication(OpusApplication application)
 {
     _application = application;
     _encoder.Application = _application;
 }
示例#21
0
 public void SetApplication(OpusApplication application)
 {
     _application = (int)application;
     opus_encoder_ctl(_encoder, (int)OpusControl.OPUS_SET_APPLICATION_REQUEST, _application);
 }
示例#22
0
 internal int opus_multistream_surround_encoder_init(
     int Fs,
     int channels,
     int mapping_family,
     out int streams,
     out int coupled_streams,
     byte[] mapping,
     OpusApplication application
     )
 {
     streams         = 0;
     coupled_streams = 0;
     if ((channels > 255) || (channels < 1))
     {
         return(OpusError.OPUS_BAD_ARG);
     }
     this.lfe_stream = -1;
     if (mapping_family == 0)
     {
         if (channels == 1)
         {
             streams         = 1;
             coupled_streams = 0;
             mapping[0]      = 0;
         }
         else if (channels == 2)
         {
             streams         = 1;
             coupled_streams = 1;
             mapping[0]      = 0;
             mapping[1]      = 1;
         }
         else
         {
             return(OpusError.OPUS_UNIMPLEMENTED);
         }
     }
     else if (mapping_family == 1 && channels <= 8 && channels >= 1)
     {
         int i;
         streams         = VorbisLayout.vorbis_mappings[channels - 1].nb_streams;
         coupled_streams = VorbisLayout.vorbis_mappings[channels - 1].nb_coupled_streams;
         for (i = 0; i < channels; i++)
         {
             mapping[i] = VorbisLayout.vorbis_mappings[channels - 1].mapping[i];
         }
         if (channels >= 6)
         {
             this.lfe_stream = streams - 1;
         }
     }
     else if (mapping_family == 255)
     {
         byte i;
         streams         = channels;
         coupled_streams = 0;
         for (i = 0; i < channels; i++)
         {
             mapping[i] = i;
         }
     }
     else
     {
         return(OpusError.OPUS_UNIMPLEMENTED);
     }
     return(opus_multistream_encoder_init(Fs, channels, streams, coupled_streams,
                                          mapping, application, (channels > 2 && mapping_family == 1) ? 1 : 0));
 }
示例#23
0
        internal int opus_multistream_encoder_init(
            int Fs,
            int channels,
            int streams,
            int coupled_streams,
            byte[] mapping,
            OpusApplication application,
            int surround
            )
        {
            int i, ret;
            int encoder_ptr;

            if ((channels > 255) || (channels < 1) || (coupled_streams > streams) ||
                (streams < 1) || (coupled_streams < 0) || (streams > 255 - coupled_streams))
            {
                return(OpusError.OPUS_BAD_ARG);
            }

            this.layout.nb_channels        = channels;
            this.layout.nb_streams         = streams;
            this.layout.nb_coupled_streams = coupled_streams;
            this.subframe_mem[0]           = this.subframe_mem[1] = this.subframe_mem[2] = 0;
            if (surround == 0)
            {
                this.lfe_stream = -1;
            }
            this.bitrate_bps       = OpusConstants.OPUS_AUTO;
            this.application       = application;
            this.variable_duration = OpusFramesize.OPUS_FRAMESIZE_ARG;
            for (i = 0; i < this.layout.nb_channels; i++)
            {
                this.layout.mapping[i] = mapping[i];
            }
            if (OpusMultistream.validate_layout(this.layout) == 0 || validate_encoder_layout(this.layout) == 0)
            {
                return(OpusError.OPUS_BAD_ARG);
            }

            encoder_ptr = 0;

            for (i = 0; i < this.layout.nb_coupled_streams; i++)
            {
                ret = this.encoders[encoder_ptr].opus_init_encoder(Fs, 2, application);
                if (ret != OpusError.OPUS_OK)
                {
                    return(ret);
                }
                if (i == this.lfe_stream)
                {
                    this.encoders[encoder_ptr].IsLFE = true;
                }
                encoder_ptr += 1;
            }
            for (; i < this.layout.nb_streams; i++)
            {
                ret = this.encoders[encoder_ptr].opus_init_encoder(Fs, 1, application);
                if (i == this.lfe_stream)
                {
                    this.encoders[encoder_ptr].IsLFE = true;
                }
                if (ret != OpusError.OPUS_OK)
                {
                    return(ret);
                }
                encoder_ptr += 1;
            }
            if (surround != 0)
            {
                Arrays.MemSetInt(this.preemph_mem, 0, channels);
                Arrays.MemSetInt(this.window_mem, 0, channels * 120);
            }
            this.surround = surround;
            return(OpusError.OPUS_OK);
        }