4
votes

I wrote a program that opens wav files and plays them through the console and then modifies the sound and replays it...how do I save a new wav file with the modified sound? so essentially I want to create a wav file and write the modified results to it. I used WAVEFORMATEX for the data of the audio.

I worked on it some more and I got the wav file created but it doesn't play

here is my code:

// writes the modified audio data into a .WAV file
void writeWaveFile(char* filename,WAVEFORMATEX& wfx)
{   
    FILE* file = fopen(filename, "wb");

    if (file)
    {
        // write wave header 
        unsigned short formatType = wfx.wFormatTag;
        unsigned short numChannels = wfx.nChannels;
        unsigned long  sampleRate = wfx.nSamplesPerSec;
        unsigned short bitsPerChannel = wfx.wBitsPerSample;
        unsigned short bytesPerSample = wfx.nAvgBytesPerSec;
        unsigned long  bytesPerSecond = wfx.nAvgBytesPerSec;
        unsigned long  dataLen = wfx.nBlockAlign;

        const int fmtChunkLen = 16;
        const int waveHeaderLen = 4 + 8 + fmtChunkLen + 8;

        unsigned long totalLen = waveHeaderLen + dataLen;

        fwrite("RIFF", 4, 1, file);
        fwrite(&totalLen, 4, 1, file);
        fwrite("WAVE", 4, 1, file);
        fwrite("fmt ", 4, 1, file);
        fwrite(&fmtChunkLen, 4, 1, file);
        fwrite(&formatType, 2, 1, file);
        fwrite(&numChannels, 2, 1, file);
        fwrite(&sampleRate, 4, 1, file);
        fwrite(&bytesPerSecond, 4, 1, file);
        fwrite(&bytesPerSample, 2, 1, file);
        fwrite(&bitsPerChannel, 2, 1, file);

        // write data

        fwrite("data", 4, 1, file);
        fwrite(&dataLen, 4, 1, file);
        //fwrite(data, dataLen, 1, file);

        // finish

        printf("Saved audio as %s\n", filename);
        fclose(file);
    }
    else
        printf("Could not open %s to write audio data\n", filename);
}
2
C++ and I've been trying different things I've found online but i can't get them to work so I keep having to start fresh so i don't have any code yet for the saving portion of the programDan Lombo
okay so I worked on it some more and I got the wav file created but it doesn't playDan Lombo
my project is due tomorrow its my final and its the only thing I have left to do so I would really appreciate help on this I wish I could do more research but I have 2 other finals tomorrow so I don't have time to waste doing research like I did all day yesterday instead of studying...any assistance would be awesomeDan Lombo

2 Answers

0
votes

You can write the file yourself completely, however there is a better idea to use helper API for this. See Save wave file using waveInOpen for code references and details.

-4
votes

I combined some codes and made the source in the following link. It records for 10sec and save the buffer to .wav file. It's as simple as it gets . And sorry i cant post the code here.

http://concon.comule.com/wero/mainsound.cpp