5
votes

I use this function to join two wav files

 public static void Concatenate(string outputFile, ArrayList sourceFiles)
    {
        byte[] buffer = new byte[1024];
        WaveFileWriter waveFileWriter = null;
        try
        {
            foreach (string sourceFile in sourceFiles)
            {
                using (WaveFileReader reader = new WaveFileReader(sourceFile))
                {
                    if (waveFileWriter == null)
                    {
                        // first time in create new Writer
                        waveFileWriter = new WaveFileWriter(outputFile, reader.WaveFormat);
                    }
                    else
                    {
                        if (!reader.WaveFormat.Equals(waveFileWriter.WaveFormat))
                        {
                            //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.WriteData(buffer, 0, read);
                    }
                }
            }
        }
        catch (Exception ex)
        { 
        }
        finally
        {
            if (waveFileWriter != null)
            {
                waveFileWriter.Dispose();
            }
        }
    }

But this function join the second wav file after the first wav's end.

enter image description here

What I really want is to join the second wav file 2 milliseconds before the wav's end .

enter image description here

Is there any way to do it using NAudio , or using some other library ?

1
How are you wanting to join the tracks? Are you looking to mix them or cut 2ms off the second one and paste it to the end?kidmosey
I just want to mix it @kidmosey !zey
Hi @zey, i am using the same code as you posted, for me it throws one exception like failed to resolve "System.Runtime.InteropServices.StandardOleMarshalObject". Did you overcome this issue?Riyas

1 Answers

1
votes

What you are trying to do, is not a simple join of waves, is a merge. In a merge the waves will be combined to form one "mixed" wave.

To merge files you can follow this documentation of NAudio.