I need to join 2 WAV files which are in a byte array by C#. I'm getting this exception:
Not a WAVE file - no RIFF header
As I got from the net when I was searching about it, the issue is about the header of the byte array that is not in WAV format. But I couldn't find any help about adding header. This is my code:
public static void Concatenate(string outputFile, IEnumerable<byte[]> sourceFiles)
{
byte[] buffer = new byte[1024];
WaveFileWriter waveFileWriter = null;
try
{
foreach (byte[] sourceFile in sourceFiles)
{
Stream streamReader = new MemoryStream(sourceFile);
using (WaveFileReader reader = new WaveFileReader(streamReader))
{
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);
}
}
}
}
finally
{
if (waveFileWriter != null)
{
waveFileWriter.Dispose();
}
}
}
How can I add a WAV file header to byte array? And other question: I have to add the header to the byte arrays one by one or first concat two byte arrays and then add header?