This project has moved and is read-only. For the latest updates, please go here.

Save modificated audio on hard drive - NAudio

Mar 26, 2014 at 12:41 PM
Edited Mar 26, 2014 at 12:44 PM
I have a problem with saving my audio file on hard drive. I'm using a WaveChannel32 class fron Naudio to make a Pan Effect on my played sound. I want to save an audio on disk with changed Pan Values. But when i want save, it a stream is endless and file grows up to big data Wav file. Here is my code where i used it:

Making a stream:
 if (dialog.FileName.EndsWith(".mp3"))
                pcm = NAudio.Wave.WaveFormatConversionStream.CreatePcmStream(new NAudio.Wave.Mp3FileReader(dialog.FileName));

            } else if (dialog.FileName.EndsWith(".wav")) {
                pcm = new NAudio.Wave.WaveChannel32(new NAudio.Wave.WaveFileReader(dialog.FileName));               

            stream = new NAudio.Wave.BlockAlignReductionStream(pcm); 

            if (stream == null) return;

            /* Wave Channel new instance (Pan) */
            wave_channel = new WaveChannel32(stream);
            wave_channel.Pan = panSlider1.Pan;

            /* Volume Meter */
            sample_channel = new SampleChannel(wave_channel, true);
            sample_channel.PreVolumeMeter += OnPostVolumeMeter;

            /* Volume Slider */
            sample_channel.Volume = volumeSlider1.Volume;

            realtime_stream = new MeteringSampleProvider(sample_channel);
            realtime_stream.StreamVolume += OnPostVolumeMeter;
            /* Wave out initialize */
            wavo = new WaveOut();
Saving a wav file code:
  WaveStream ch = wave_channel; 
            long outputLenght = 0; 

            using (var writer = new WaveFileWriter(save_file_dialog.FileName + ".wav", ch.WaveFormat)) { 
                byte[] buffer = new byte[ch.WaveFormat.AverageBytesPerSecond * 4]; 
                while (true) { 
                    int bytesread = ch.Read(buffer, 0, buffer.Length); 
                    if (bytesread == 0) { 
                    outputLenght += bytesread; 
                    if (outputLenght > Int32.MaxValue) { 
                        throw new InvalidOperationException("Plik nie może być większy niż 2GB"); 
                    writer.Write(buffer, 0, bytesread); 
How can i save it on hard drive with the same time as original?
Apr 29, 2014 at 8:26 PM
See this post on saving and playing audio at the same time