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);
}