Пример #1
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;
            }
        }
        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 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 HasConnectInputToOutputMethod()
 {
     var input1 = new TestWaveProvider(new WaveFormat(32000, 16, 2));
     var mp = new MultiplexingWaveProvider(new IWaveProvider[] { input1 }, 1);
     mp.ConnectInputToOutput(1, 0);
 }
 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 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);
 }