This project has moved. For the latest updates, please go here.

Processing and creating proper .wav file

Nov 30, 2014 at 10:27 PM
Edited Nov 30, 2014 at 10:37 PM

I have a problem concerning processing two files, mixing them together and saving the output in readable wave format. The Processing is just adjusting volume of the second file accordingly to the volume of the first one (i.e. envelope controlling). I load both of them to the AudioFileReader. I pick where to save 'writer' (which is a WaveFileWriter) using SaveFileDialog and i adjust its WaveFormat accordingly to what WaveFormat first input file has:
writer = new WaveFileWriter(saveDialog.FileName, firstReader.WaveFormat);
Then this method gets triggered:
 * In one loop: filling both buffers, adjusting volume, mixing and saving the output 

firstBuffer = new float[firstReader.WaveFormat.SampleRate * 2];
secondBuffer = new float[secondReader.WaveFormat.SampleRate * 2];

int firstBytesRead = 0;
int secondBytesRead = 0;
    firstBytesRead = firstReader.Read(firstBuffer, 0, firstBuffer.Length);
    secondBytesRead = secondReader.Read(secondBuffer, 0, secondBuffer.Length);

    for (int i = 0; i < firstBuffer.Length; i++)
        firstBuffer[i] /= 2;  //volume rescaling

        // if secondBufer isn't empty - adjust the volume and mix it with firstBuffer
        if (secondBytesRead > 0)
            secondBuffer[i] *= firstBuffer[i]*VOLUME_RATIO;
            firstBuffer[i] += secondBuffer[i];

    writer.WriteSamples(firstBuffer, 0, firstBytesRead);
} while (firstBytesRead > 0);

MessageBox.Show("Completed succesfully", "Done!");
The output is created and i can play it in Winamp (but not in a different player). The problem is that I would like to use my output as an input for another processing cycle, i.e. simply loading output and chaining (adjusting volume and mixing) it with another file. Unfortunately i get FormatException while loading this output. It says:

An unhandled exception of type 'System.FormatException' occurred in NAudio.dll
Additional information: Invalid WAV file - No fmt chunk found

Why it's invalid .wav file if i used WaveFormat from existing, proper .wav file? How can i provide this fmt chunk to my output?

Thanks in advance for any help

EDIT: I didn't dispose after writing. Sorry for the mess, stupid mistake and i don't know how to delete this post...
Marked as answer by stereoid on 11/30/2014 at 3:37 PM
Jan 10, 2015 at 8:30 AM
no problem, glad you found a solution