Esempio n. 1
0
        private void HandleAudio(WaveInEventArgs args)
        {
            if (IsRecording && !isDisposed)
            {
                byte[] toEncode = args.Buffer;

                int length = args.BytesRecorded;
                if (length > 0)
                {
                    if (waveIn.WaveFormat != codec.RecordFormat)
                    {
                        if (waveIn.WaveFormat.Encoding == WaveFormatEncoding.IeeeFloat)
                        {
                            var floatSamples = InputResampler.ReadIeeeWav(toEncode, args.BytesRecorded, waveIn.WaveFormat);
                            foreach (var sample in floatSamples)
                            {
                                aggregator.Add(sample);
                            }
                            toEncode = InputResampler.Resample(floatSamples, floatSamples.Length, waveIn.WaveFormat, codec.RecordFormat, out length);
                        }
                        else
                        {
                            for (int i = 0; i < args.BytesRecorded + 1; i += 2)
                            {
                                aggregator.Add(InputResampler.PCMtoFloat(toEncode, i / 2));
                            }

                            toEncode = InputResampler.Resample(toEncode, args.BytesRecorded, waveIn.WaveFormat, codec.RecordFormat, out length);
                        }
                    }
                    if (toEncode == null)
                    {
                        Console.WriteLine("Encode Error: Disabling input. Please choose another record format and report this bug..");
                        StopRecording();
                    }
                    else
                    {
                        if (CoughScalar < 1.0f)
                        {
                            InputResampler.ScalePCM16VolumeDb(ref toEncode, length, CoughScalar);
                        }

                        byte[] encoded = codec.Encode(toEncode, length);

                        if (encoded.Length > 0 && NetworkWPF.Client != null)
                        {
                            NetworkWPF.Client.SendAudio(codec.CodecID, encoded, encoded.Length);
                        }
                    }
                }
            }
        }
Esempio n. 2
0
        public void MonoRateResampleTest()
        {
            int samples = 1000;

            byte[] stream = CreateTestStream(samples, 0);

            WaveFormat sourceFormat = new WaveFormat(44100, 1);
            WaveFormat destFormat   = new WaveFormat(8000, 1);

            int sourceLength      = stream.Length;
            int expectedResultLen = GetExpectedConversionLength(sourceLength, sourceFormat, destFormat);

            int resultLength;

            byte[] resultStream = InputResampler.Resample(stream, sourceLength, sourceFormat, destFormat, out resultLength);

            Assert.AreEqual(expectedResultLen, resultLength);
        }
Esempio n. 3
0
        public void LongerSourceStereoResampleTest()
        {
            int samples = 500;

            byte[] testStream = CreateStereoSampleStream(samples);

            byte[] stream = new byte[testStream.Length + 1000];
            testStream.CopyTo(stream, 0);

            WaveFormat sourceFormat = new WaveFormat(48000, 2);
            WaveFormat destFormat   = new WaveFormat(44100, 1);

            int sourceLength      = testStream.Length;
            int expectedResultLen = GetExpectedConversionLength(sourceLength, sourceFormat, destFormat);

            int resultLength;

            byte[] resultStream = InputResampler.Resample(stream, sourceLength, sourceFormat, destFormat, out resultLength);

            Assert.AreEqual(expectedResultLen, resultLength);
        }
Esempio n. 4
0
        public void ChannelCountChangeTest()
        {
            int samples = 1000;

            byte[] left   = CreateTestStream(samples, 0);
            byte[] right  = CreateTestStream(samples, 1);
            byte[] stream = Multiplex(samples, left, right);

            WaveFormat sourceFormat = new WaveFormat(8000, 2);
            WaveFormat destFormat   = new WaveFormat(8000, 1);

            int sourceLength      = stream.Length;
            int expectedResultLen = sourceLength / 2;

            int resultLength;

            byte[] resultStream = InputResampler.Resample(stream, sourceLength, sourceFormat, destFormat, out resultLength);

            Assert.AreEqual(expectedResultLen, resultLength);

            for (int i = 0; i < samples; i++)
            {
                int leftValue   = left[i * 2 + 1] << 8 | left[i * 2];
                int rightValue  = right[i * 2 + 1] << 8 | right[i * 2];
                int resultValue = resultStream[i * 2 + 1] << 8 | resultStream[i * 2];

                if (leftValue + rightValue < short.MaxValue)
                {
                    Assert.AreEqual(leftValue + rightValue, resultValue);
                }
                else
                {
                    Assert.Inconclusive("Clipping occurred.");
                }
            }
        }