Exemplo n.º 1
0
        public void PcmToFloatAndBackTest()
        {
            for (int i = 0; i < 200; i++)
            {
                short sample      = RandomPCMSample();
                float floatResult = InputResampler.PCMtoFloat(sample);
                int   shortResult = InputResampler.FloatToPCM(floatResult);

                int delta = Math.Abs(sample - shortResult);
                Assert.IsTrue(delta < 2, "Conversion to float and back should be within one unit. Delta was {0}", delta);
            }
        }
Exemplo n.º 2
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);
                        }
                    }
                }
            }
        }