Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

join two wav files in NAudio before first wav end

Tags:

c#

wav

naudio

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 ?

like image 613
zey Avatar asked Nov 09 '22 01:11

zey


1 Answers

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.

like image 58
Jonny Piazzi Avatar answered Nov 14 '22 21:11

Jonny Piazzi