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

Mixdown multiple WAVs to a single file - solved

Jul 9, 2012 at 4:26 PM

Hi,

similar to this(http://naudio.codeplex.com/discussions/356641) problem, I'm looking for a clever solution to mixdown n files (mono) to a single file (stereo or mono doesn't matter). 

I run into WaveMixerStream32 but I don't know how to handle it correctly.

Also I'm looking for a way to amplify/normalize the mixdown. Do some of you have any ideas how to do that in NAudio?

Thanks!

Jul 12, 2012 at 5:57 PM

 private WaveFileWriter writer;
 private List<WaveFileReader> inputs = new List<WaveFileReader>();
...
...

    private void button1_Click(object sender, RoutedEventArgs e)
        {
        
            foreach (string s in blah.audiofilenames)
                inputs.Add(new WaveFileReader(s));

            var waveProvider = new MultiplexingWaveProvider(inputs, inputs.Count);
            writer = new WaveFileWriter(@"D:\temp\mixdown.wav", waveProvider.WaveFormat);

            int bytesRead = 0;
            int buffer_size = 4096;

            while (true)
            {
                byte[] bytes = new byte[buffer_size];
                try
                {
                    bytesRead = waveProvider.Read(bytes, 0, buffer_size);
                }
                catch (Exception ec)
                {
                    //  throw ec;
                }

                if (bytesRead == 0)
                {
                    Console.WriteLine("end");

                    if (writer != null)
                    {
                        writer.Flush();
                        writer.Close();
                        writer.Dispose();
                    }

                    break;
                }
                writer.Write(bytes, 0, buffer_size);
            }
        }

 

This is the code that solved my first problem. It is generating a multitrack wave file (8+ tracks in one file in my case). Of course you could put this in its own thread.

Apr 18, 2013 at 8:37 PM
Edited Apr 18, 2013 at 8:50 PM
Hi i used this code works fine but after mixdown two wav files i cant delete them i have IO error file is in use. I mean source files.
Apr 18, 2013 at 9:05 PM
Solved :)
            foreach (WaveFileReader wave in inputs)
            {
                wave.Close();
            }