Exemple #1
1
        private void StartStopSineWave()
        {
            if (waveOut == null)
            {
                button1.Content = "Stop Sound";
                //string str = channelSelector.Text;
                //selectedChannels = Int32.Parse(str);
                Console.WriteLine("User Selected Channels: " + selectedChannels);
                WaveOutCapabilities outdeviceInfo = WaveOut.GetCapabilities(0);
                waveOutChannels = outdeviceInfo.Channels;
                waveOut = new WaveOut();

                int waveOutDevices = WaveOut.DeviceCount;
                for(int i = 0; i< waveOutDevices; i++)
                {
                    outdeviceInfo = WaveOut.GetCapabilities(i);
                    Console.WriteLine("Device {0}: {1}, {2} channels",
                            i, outdeviceInfo.ProductName, outdeviceInfo.Channels);
                }

                List<IWaveProvider> inputs = new List<IWaveProvider>();
                frequencies = new List<int>();
                centerbins = new List<int>();
                //Foutstream = new List<StreamWriter>();
                for (int c = 0; c < selectedChannels; c++)
                {
                    inputs.Add(new SineWaveProvider32(18000+c*700, 0.25f, 44100, 1));
                    frequencies.Add(18000+c*700);
                    centerbins.Add((int)Math.Round((18000 + c * 700) / 10.768));

                    Console.WriteLine(centerbins.ElementAt(c));
                }

                var splitter = new MultiplexingWaveProvider(inputs, selectedChannels);
                try
                {
                    waveOut.Init(splitter);
                    waveOut.Play();
                }
                catch(System.ArgumentException)
                {
                    Console.WriteLine("Invalid audio channel count. Please select a lower number of audio channels");
                }
            }
            else
            {
                waveOut.Stop();
                waveOut.Dispose();
                waveOut = null;
                button1.Content = "Generate Sound";

                frequencies.Clear();
                centerbins.Clear();
                //Foutstream.Clear();
            }
        }
 public void InputChannelCountIsCorrect()
 {
     var input1 = new TestWaveProvider(new WaveFormat(32000, 16, 2));
     var input2 = new TestWaveProvider(new WaveFormat(32000, 16, 1));
     var mp = new MultiplexingWaveProvider(new IWaveProvider[] { input1, input2 }, 1);
     Assert.AreEqual(3, mp.InputChannelCount);
 }
 public void CorrectOutputFormatIsSetForIeeeFloat()
 {
     var input1 = new TestWaveProvider(WaveFormat.CreateIeeeFloatWaveFormat(32000, 1));
     byte[] expected = new byte[] { 0, 1, 2, 3, 0, 1, 2, 3, 4, 5, 6, 7, 4, 5, 6, 7, 8, 9, 10, 11, 8, 9, 10, 11, };
     var mp = new MultiplexingWaveProvider(new IWaveProvider[] { input1 }, 2);
     Assert.AreEqual(WaveFormatEncoding.IeeeFloat, mp.WaveFormat.Encoding);
 }
 public void OneInOneOutShouldCopyWaveFormat()
 {
     var input1 = new Mock<IWaveProvider>();
     var inputWaveFormat = new WaveFormat(32000, 16, 1);
     input1.Setup(x => x.WaveFormat).Returns(inputWaveFormat);
     var mp = new MultiplexingWaveProvider(new IWaveProvider[] { input1.Object }, 1);
     Assert.AreEqual(inputWaveFormat, mp.WaveFormat);
 }
 public void OneInOneOutShouldCopyInReadMethod()
 {
     var input1 = new TestWaveProvider(new WaveFormat(32000, 16, 1));
     byte[] expected = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
     var mp = new MultiplexingWaveProvider(new IWaveProvider[] { input1 }, 1);
     byte[] buffer = new byte[10];
     var read = mp.Read(buffer, 0, 10);
     Assert.AreEqual(10, read);
     Assert.AreEqual(expected, buffer);
 }
 public void CorrectlyHandlesIeeeFloat()
 {
     var input1 = new TestWaveProvider(WaveFormat.CreateIeeeFloatWaveFormat(32000, 1));
     byte[] expected = new byte[] { 0, 1, 2, 3, 0, 1, 2, 3, 4, 5, 6, 7, 4, 5, 6, 7, 8, 9, 10, 11, 8, 9, 10, 11, };
     var mp = new MultiplexingWaveProvider(new IWaveProvider[] { input1 }, 2);
     byte[] buffer = new byte[expected.Length];
     var read = mp.Read(buffer, 0, expected.Length);
     Assert.AreEqual(expected.Length, read);
     Assert.AreEqual(expected, buffer);
 }
 public void OneInTwoOutShouldConvertMonoToStereo()
 {
     var input1 = new TestWaveProvider(new WaveFormat(32000, 16, 1));
     // 16 bit so left right pairs
     byte[] expected = new byte[] { 0, 1, 0, 1, 2, 3, 2, 3, 4, 5, 4, 5, 6, 7, 6, 7, 8, 9, 8, 9 };
     var mp = new MultiplexingWaveProvider(new IWaveProvider[] { input1 }, 2);
     byte[] buffer = new byte[20];
     var read = mp.Read(buffer, 0, 20);
     Assert.AreEqual(20, read);
     Assert.AreEqual(expected, buffer);
 }
 public void HasConnectInputToOutputMethod()
 {
     var input1 = new TestWaveProvider(new WaveFormat(32000, 16, 2));
     var mp = new MultiplexingWaveProvider(new IWaveProvider[] { input1 }, 1);
     mp.ConnectInputToOutput(1, 0);
 }
 public void StereoInTwoOutShouldCopyStereo()
 {
     var input1 = new TestWaveProvider(new WaveFormat(32000, 16, 2));
     // 4 bytes per pair of samples
     byte[] expected = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
     var mp = new MultiplexingWaveProvider(new IWaveProvider[] { input1 }, 2);
     byte[] buffer = new byte[12];
     var read = mp.Read(buffer, 0, 12);
     Assert.AreEqual(12, read);
     Assert.AreEqual(expected, buffer);
 }
 public void TwoInOneOutShouldCanBeConfiguredToSelectRightChannel()
 {
     var input1 = new TestWaveProvider(new WaveFormat(32000, 16, 2));
     // 16 bit so left right pairs
     byte[] expected = new byte[] { 2, 3, 6, 7, 10, 11, 14, 15, 18, 19 };
     var mp = new MultiplexingWaveProvider(new IWaveProvider[] { input1 }, 1);
     mp.ConnectInputToOutput(1, 0);
     byte[] buffer = new byte[10];
     var read = mp.Read(buffer, 0, 10);
     Assert.AreEqual(10, read);
     Assert.AreEqual(expected, buffer);
 }
 public void ShouldZeroOutBufferIfInputStopsShort()
 {
     var input1 = new TestWaveProvider(new WaveFormat(32000, 16, 1), 6);
     byte[] expected = new byte[] { 0, 1, 2, 3, 4, 5, 0, 0, 0, 0 };
     var mp = new MultiplexingWaveProvider(new IWaveProvider[] { input1 }, 1);
     byte[] buffer = new byte[10];
     for (int n = 0; n < buffer.Length; n++)
     {
         buffer[n] = 0xFF;
     }
     var read = mp.Read(buffer, 0, buffer.Length);
     Assert.AreEqual(6, read);
     Assert.AreEqual(expected, buffer);
 }
        public void PerformanceTest()
        {
            var waveFormat = new WaveFormat(32000, 16, 1);
            var input1 = new TestWaveProvider(waveFormat);
            var input2 = new TestWaveProvider(waveFormat);
            var input3 = new TestWaveProvider(waveFormat);
            var input4 = new TestWaveProvider(waveFormat);
            var mp = new MultiplexingWaveProvider(new IWaveProvider[] { input1, input2, input3, input4 }, 4);
            mp.ConnectInputToOutput(0, 3);
            mp.ConnectInputToOutput(1, 2);
            mp.ConnectInputToOutput(2, 1);
            mp.ConnectInputToOutput(3, 0);

            byte[] buffer = new byte[waveFormat.AverageBytesPerSecond];
            Stopwatch s = new Stopwatch();
            var duration = s.Time(() =>
            {
                // read one hour worth of audio
                for (int n = 0; n < 60 * 60; n++)
                {
                    mp.Read(buffer, 0, buffer.Length);
                }
            });
            Console.WriteLine("Performance test took {0}ms", duration);
        }
 public void ReadReturnsCountIfOneInputHasEndedButTheOtherHasnt()
 {
     var input1 = new TestWaveProvider(new WaveFormat(32000, 16, 1), 0);
     var input2 = new TestWaveProvider(new WaveFormat(32000, 16, 1));
     byte[] expected = new byte[] { };
     var mp = new MultiplexingWaveProvider(new IWaveProvider[] { input1, input2 }, 1);
     byte[] buffer = new byte[10];
     var read = mp.Read(buffer, 0, 10);
     Assert.AreEqual(10, read);
 }
 public void TwoMonoInTwoOutShouldCreateStereo()
 {
     var input1 = new TestWaveProvider(new WaveFormat(32000, 16, 1));
     var input2 = new TestWaveProvider(new WaveFormat(32000, 16, 1)) { Position = 100 };
     // 4 bytes per pair of samples
     byte[] expected = new byte[] { 0, 1, 100, 101, 2, 3, 102, 103, 4, 5, 104, 105, };
     var mp = new MultiplexingWaveProvider(new IWaveProvider[] { input1, input2 }, 2);
     byte[] buffer = new byte[expected.Length];
     var read = mp.Read(buffer, 0, expected.Length);
     Assert.AreEqual(expected.Length, read);
     Assert.AreEqual(expected, buffer);
 }
Exemple #15
0
        public void SetSound(byte[] data, WaveFormat sourceFormat)
        {
            MemoryStream dataStream = new MemoryStream(data);
            RawSourceWaveStream wavStream = new RawSourceWaveStream(dataStream, sourceFormat);
            WaveStream wavConvertStream = null;

            try
            {
                wavConvertStream = WaveFormatConversionStream.CreatePcmStream(wavStream);

                // using a mux, we force all sounds to be 2 channels
                MultiplexingWaveProvider sourceProvider = new MultiplexingWaveProvider(new IWaveProvider[] { wavConvertStream }, 2);
                int bytesToRead = (int)((wavConvertStream.Length * 2) / wavConvertStream.WaveFormat.Channels);
                byte[] rawWaveData = new byte[bytesToRead];
                int bytesRead = sourceProvider.Read(rawWaveData, 0, bytesToRead);

                Data = rawWaveData;
                Format = sourceProvider.WaveFormat;

                // clean up
                sourceProvider = null;
            }
            catch
            {
                Data = data;
                Format = sourceFormat;
            }
            finally
            {
                if (wavConvertStream != null)
                    wavConvertStream.Dispose();
                wavConvertStream = null;
                wavStream.Dispose();
                wavStream = null;
                dataStream.Dispose();
                dataStream = null;
            }
        }
 public void TwoInOneOutShouldSelectLeftChannel()
 {
     var input1 = new TestWaveProvider(new WaveFormat(32000, 16, 2));
     // 16 bit so left right pairs
     byte[] expected = new byte[] { 0, 1, 4, 5, 8, 9, 12, 13, 16, 17 };
     var mp = new MultiplexingWaveProvider(new IWaveProvider[] { input1 }, 1);
     byte[] buffer = new byte[10];
     var read = mp.Read(buffer, 0, 10);
     Assert.AreEqual(10, read);
     Assert.AreEqual(expected, buffer);
 }
 public void StereoInTwoOutCanBeConfiguredToSwapLeftAndRight()
 {
     var input1 = new TestWaveProvider(new WaveFormat(32000, 16, 2));
     // 4 bytes per pair of samples
     byte[] expected = new byte[] { 2, 3, 0, 1, 6, 7, 4, 5, 10, 11, 8, 9, };
     var mp = new MultiplexingWaveProvider(new IWaveProvider[] { input1 }, 2);
     mp.ConnectInputToOutput(0, 1);
     mp.ConnectInputToOutput(1, 0);
     byte[] buffer = new byte[12];
     var read = mp.Read(buffer, 0, 12);
     Assert.AreEqual(12, read);
     Assert.AreEqual(expected, buffer);
 }
 public void ConnectInputToOutputThrowsExceptionForInvalidOutput()
 {
     var input1 = new TestWaveProvider(new WaveFormat(32000, 16, 2));
     var mp = new MultiplexingWaveProvider(new IWaveProvider[] { input1 }, 1);
     Assert.Throws<ArgumentException>(() => mp.ConnectInputToOutput(1, 1));
 }
 public void ReadReturnsZeroIfSingleInputHasReachedEnd()
 {
     var input1 = new TestWaveProvider(new WaveFormat(32000, 16, 1), 0);
     byte[] expected = new byte[] { };
     var mp = new MultiplexingWaveProvider(new IWaveProvider[] { input1 }, 1);
     byte[] buffer = new byte[10];
     var read = mp.Read(buffer, 0, 10);
     Assert.AreEqual(0, read);
 }
Exemple #20
0
        public byte[] Render(float masterVolume)
        {
            // due to the way NAudio works, the source files must be provided twice.
            // this is because all channels are kept in sync by the mux, and the unused
            // channel data is discarded. If we tried to use the same source for both
            // muxes, it would try to read 2x the data present in the buffer!
            // If only we had a way to create separate WaveProviders from within the
            // MultiplexingWaveProvider..

            try
            {
                using (MemoryStream sourceLeft = new MemoryStream(Data), sourceRight = new MemoryStream(Data))
                {
                    using (RawSourceWaveStream waveLeft = new RawSourceWaveStream(new IgnoreDisposeStream(sourceLeft), Format), waveRight = new RawSourceWaveStream(new IgnoreDisposeStream(sourceRight), Format))
                    {
                        // step 1: separate the stereo stream
                        MultiplexingWaveProvider demuxLeft = new MultiplexingWaveProvider(new IWaveProvider[] { waveLeft }, 1);
                        MultiplexingWaveProvider demuxRight = new MultiplexingWaveProvider(new IWaveProvider[] { waveRight }, 1);
                        demuxLeft.ConnectInputToOutput(0, 0);
                        demuxRight.ConnectInputToOutput(1, 0);

                        // step 2: adjust the volume of a stereo stream
                        VolumeWaveProvider16 volLeft = new VolumeWaveProvider16(demuxLeft);
                        VolumeWaveProvider16 volRight = new VolumeWaveProvider16(demuxRight);

                        // note: use logarithmic scale
            #if (true)
                        // log scale is applied to each operation
                        float volumeValueLeft = (float)Math.Pow(1.0f - Panning, 0.5f);
                        float volumeValueRight = (float)Math.Pow(Panning, 0.5f);
                        // ensure 1:1 conversion
                        volumeValueLeft /= (float)Math.Sqrt(0.5);
                        volumeValueRight /= (float)Math.Sqrt(0.5);
                        // apply volume
                        volumeValueLeft *= (float)Math.Pow(Volume, 0.5f);
                        volumeValueRight *= (float)Math.Pow(Volume, 0.5f);
                        // clamp
                        volumeValueLeft = Math.Min(Math.Max(volumeValueLeft, 0.0f), 1.0f);
                        volumeValueRight = Math.Min(Math.Max(volumeValueRight, 0.0f), 1.0f);
            #else
                        // log scale is applied to the result of the operations
                        float volumeValueLeft = (float)Math.Pow(1.0f - Panning, 0.5f);
                        float volumeValueRight = (float)Math.Pow(Panning, 0.5f);
                        // ensure 1:1 conversion
                        volumeValueLeft /= (float)Math.Sqrt(0.5);
                        volumeValueRight /= (float)Math.Sqrt(0.5);
                        // apply volume
                        volumeValueLeft *= Volume;
                        volumeValueRight *= Volume;
                        // apply log scale
                        volumeValueLeft = (float)Math.Pow(volumeValueLeft, 0.5f);
                        volumeValueRight = (float)Math.Pow(volumeValueRight, 0.5f);
                        // clamp
                        volumeValueLeft = Math.Min(Math.Max(volumeValueLeft, 0.0f), 1.0f);
                        volumeValueRight = Math.Min(Math.Max(volumeValueRight, 0.0f), 1.0f);
            #endif
                        // use linear scale for master volume
                        volLeft.Volume = volumeValueLeft * masterVolume;
                        volRight.Volume = volumeValueRight * masterVolume;

                        // step 3: combine them again
                        IWaveProvider[] tracks = new IWaveProvider[] { volLeft, volRight };
                        MultiplexingWaveProvider mux = new MultiplexingWaveProvider(tracks, 2);

                        // step 4: export them to a byte array
                        byte[] finalData = new byte[Data.Length];
                        mux.Read(finalData, 0, finalData.Length);

                        // cleanup
                        demuxLeft = null;
                        demuxRight = null;
                        volLeft = null;
                        volRight = null;
                        mux = null;

                        return finalData;
                    }
                }
            }
            catch
            {
                return Data;
            }
        }
Exemple #21
0
        private void StartStopSineWave()
        {
            if (waveOut == null)
            {
                button1.Content = "Stop Sound";
                Console.WriteLine("User Selected Channels: " + selectedChannels);
                WaveOutCapabilities outdeviceInfo = WaveOut.GetCapabilities(0);
                waveOutChannels = outdeviceInfo.Channels;
                waveOut = new WaveOut();

                int waveOutDevices = WaveOut.DeviceCount;
                for (int i = 0; i < waveOutDevices; i++)
                {
                    outdeviceInfo = WaveOut.GetCapabilities(i);
                    Console.WriteLine("Device {0}: {1}, {2} channels",
                            i, outdeviceInfo.ProductName, outdeviceInfo.Channels);
                }

                List<IWaveProvider> inputs = new List<IWaveProvider>();
                frequencies = new List<int>();
                centerbins = new List<int>();

                for (int c = 0; c < selectedChannels; c++)
                {
                        //Original Sine Wave generation
                        inputs.Add(new SineWaveProvider32(minFrequency + c * frequencyStep, 0.5f, 44100, 1));
                        frequencies.Add(minFrequency + c * frequencyStep);
                        centerbins.Add((int)Math.Round((minFrequency + c * frequencyStep) / 10.768));
                }

                var splitter = new MultiplexingWaveProvider(inputs, selectedChannels);
                try
                {
                    waveOut.Init(splitter);
                    waveOut.Play();
                }
                catch (System.ArgumentException)
                {
                    Console.WriteLine("Invalid audio channel count. Please select a lower number of audio channels");
                }

                //Console.WriteLine("Number of Channels: " + wOut.NumberOfBuffers);
                Console.WriteLine("Number of Channels: " + waveOut.OutputWaveFormat.Channels);
            }
            else
            {
                waveOut.Stop();
                waveOut.Dispose();
                waveOut = null;
                button1.Content = "Start Sound";

                frequencies.Clear();
                centerbins.Clear();
            }
        }