NorseByte
NorseByte

Reputation: 312

How to concatenate WAV files in C#

I have 2 wav files that I want to concatenate into one file with the two sound tracks.

Is there any api for that task or some built in commands in .NET that can I can use in some genius way to make this task possible?

Many thanks for the help. :)

Upvotes: 4

Views: 8791

Answers (6)

D'Artagnan222
D'Artagnan222

Reputation: 11

You can use the ConcatenatingSampleProvider of NAudio package.

var fs = new FileStream(@"C:\son1.wav", FileMode.Open);
var fs2 = new FileStream(@"C:\son2.wav", FileMode.Open);
      
var reader = new WaveFileReader(fs);
var reader2 = new WaveFileReader(fs2);
var sample1 = reader.ToSampleProvider();
var sample2 = reader2.ToSampleProvider();
var sample3 = new ISampleProvider[] { sample1, sample2 };
var conc = new ConcatenatingSampleProvider(sample3);
WaveFileWriter.CreateWaveFile16(@"C:\son3.wav", conc);
var sound = new SoundPlayer(@"C:\son3.wav");
sound.Play();
reader.Close();
reader2.Close();
fs.Close();
fs2.Close();

Upvotes: 0

Emperor Eto
Emperor Eto

Reputation: 3520

You cannot merely append one file (sans the 44 byte header) to the other as others suggested. The header defines not just the size of the data but the sample rate, channel count, and bit depth. If the audio format is precisely the same between the two files, however, you could append if you also update the headers in the two places that depend on the size.

This site provides a good low-level descrpition of the format from which you can identify what parts of the header you'd need to change. Again assuming the audio formats are identical you'd need to update the chunk size at byte offset 4, and the Subchunk2Size at byte offset 40. Basically just add to each of the existing values the size of the 2nd file (less 44). Then append the data from the 2nd file (at offset 44) to the first.

Now, if the audio formats are not identical (remember, even the sample rates must match) you will need to resample the second file to match the format of the first, and output a totally new WAV file with the combined data. To do this you will likely want to use an A/V library such as FFMpegCore for C#. While the math for resampling simple PCM audio data is not that complicated, it's tedious and will be error prone if you're not experienced. The upside is that once you master a library like FFMpeg you will be able to do literally anything imaginable with multimedia, so it's worth the investment.

Upvotes: 0

dedouze
dedouze

Reputation: 1

Yes you can just append the data of the second wav ( excluding its headers) as sais Jon B, but you must also modify the header of the first file to indicate the new length, otherwise some audio players will stop at the end of the first part.

For dynamic concatenation, as you don't know the size of the total wav at the beginning, you can put a fake length on the header of the first file, like put a duration of 10hours for example.

Then the audio player should just wait till you append each data. It works for ffmpeg at least

Upvotes: -1

RobS
RobS

Reputation: 3857

If you only need to do this once then your best bet is to use some software specifically for the task. Praat or audacity will do this (join or merge two tracks).

Praat can also be scripted so it is possible to script Praat and then call Praat and the script from within a .Net application.

Rob

Upvotes: 3

Han
Han

Reputation: 2037

The straightforward way would be to interpret the wav file headers, extract the samples and interleave the samples from both files into a new sample stream which you write to a new wav file. You could also add the sample values of both source wav files sample by sample to 'mix' both files into a single track. The wav file format is not that complicated so it should not be too difficult to write this code. Also, there are lots of open source projects containing code to read and write wav files. Note that things can get somewhat trickier of both source wav files have different sample rates. Check this for a description of the wav format.

Upvotes: -1

user27414
user27414

Reputation:

If I'm not mistaken, you can just append the bytes from the second file to the end of the first. If there is any header data, you'll want to exclude that (see here)

Be advised that you may hear a click or pop between the two clips, as there will be no blending. You could probably get around this by fading (gradually reducing the values for the last few ms to the center value).

Upvotes: 1

Related Questions