Merging Two Or More WAV Files

Oct 7, 2014 at 2:25 PM
How would you go about merging two or more WAV format files together? Each WAV file is of the same format.

I have looked through the documentation and discussions and there is nothing that I can find that will help me with this.
Coordinator
Oct 7, 2014 at 4:25 PM
CSCore itself does not contain any mixer class since finding a good mixing algorithm takes some time (which I actually did not had). But there is a Sample called "SimpleMixerSample" which shows how to mix two multiple wave sources together. Take a look at the sample: https://cscore.codeplex.com/SourceControl/latest#Samples/SimpleMixerSample/Program.cs first. If there are any questions feel free to ask me :).
Oct 8, 2014 at 4:08 PM
Thanks for your reply. I managed to finally work out a routine that merged two or more files:
        public static void ConcatenateWAV(string outputFile, IEnumerable<string> sourceFiles)
        {
            byte[] buffer = new byte[1024];

            WaveWriter waveFileWriter = null;
            bool writingWaveOut = false;

            foreach (string sourceFile in sourceFiles)
            {
                FileInfo fileInfo = new FileInfo(sourceFile);
                if (!fileInfo.Exists)
                {
                    throw new Exception("Unable to locate file - " + sourceFile);
                }
            }

            try
            {
                FileInfo fileInfo = new FileInfo(outputFile);
                if (fileInfo.Exists)
                {
                    fileInfo.Delete();
                }

                WaveFormat currentWaveFormat = new WaveFormat();

                foreach (string sourceFile in sourceFiles)
                {
                    using (IWaveSource reader = CodecFactory.Instance.GetCodec(sourceFile))
                    {
                        if (!writingWaveOut)
                        {
                            // first time in create new Writer
                            writingWaveOut = true;
                            waveFileWriter = new WaveWriter(outputFile, reader.WaveFormat);
                            currentWaveFormat = reader.WaveFormat;
                        }
                        else
                        {
                            if (!reader.WaveFormat.ToString().Equals(currentWaveFormat.ToString()))
                            {
                                throw new InvalidOperationException("Can't concatenate WAV Files that don't share the same format");
                            }
                        }

                        int read;
                        while ((read = reader.Read(buffer, 0, buffer.Length)) > 0)
                        {
                            waveFileWriter.Write(buffer, 0, read);
                        }

                        reader.Dispose();
                    }
                }
            }
            catch (Exception ex)
            {
                if (waveFileWriter != null)
                {
                    waveFileWriter.Dispose();
                    waveFileWriter = null;
                }
            }
            finally
            {
                if (waveFileWriter != null)
                {
                    waveFileWriter.Dispose();
                    waveFileWriter = null;
                }
            }
        }