Quantcast
Channel: NAudio
Viewing all articles
Browse latest Browse all 5831

New Post: Save modificated audio on hard drive - NAudio

$
0
0
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();
            wavo.Init(sample_channel);
            wavo.Stop();
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) { 
                        break; 
                    } 
                    outputLenght += bytesread; 
                    if (outputLenght > Int32.MaxValue) { 
                        throw new InvalidOperationException("Plik nie może być większy niż 2GB"); 
                    }
                    Console.WriteLine(buffer.Length);
                    Console.WriteLine(bytesread);
                    writer.Write(buffer, 0, bytesread); 
                } 
            } 
How can i save it on hard drive with the same time as original?

Viewing all articles
Browse latest Browse all 5831

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>