Code:
#include <iostream.h>
#include <fstream.h>
#include <windows.h>
typedef struct tagWAVEHEADER
{
char RIFF[4]; // 'R','I','F','F'
DWORD size; // size of wave file from here on
char WAVEfmt[8]; // 'W','A','V','E','f','m','t',' '
DWORD wFormatLength; // The length of the TAG format
WORD wFormatTag; // should be 1 for PCM type data
WORD nChannels; // should be 1 for MONO type data
DWORD nSamplesPerSec; // should be 11025, 22050, 44100
DWORD nAvgBytesPerSec; // Average Data Rate
WORD nBlockAlign; // 1 for 8 bit data, 2 for 16 bit
WORD wBitsPerSample; // 8 for 8 bit data, 16 for 16 bit
char data[4]; // 'd','a','t','a'
DWORD datasize; // size of data from here on
} WAVEHEADER;
int wavKaraoke(char *InputFileName,char *OutputFileName);
double ModifyVolume(double chunk, int value);
int ReadWaveHeader(char *FileName, WAVEHEADER &wave);
//chunk- a temporary data chunk which has the sample data.
//value- [0;100]. 0->min volume, 100->max volume
double ModifyVolume(double chunk,int value)
{
return (chunk*value)/100;
}
int ReadWaveHeader(char *FileName, WAVEHEADER &wave)
{
//First, open the wave file in binary mode
ifstream OpenFile(FileName,ios::binary | ios::in);
//Read the first 4 bytes, which will hold 'RIFF'
OpenFile.read(wave.RIFF,sizeof(wave.RIFF));
//Now, read the size
OpenFile.read((char*)&wave.size,sizeof(wave.size));
//Now, read 'WAVE' and 'fmt' in a single procedure
OpenFile.read(wave.WAVEfmt,sizeof(wave.WAVEfmt));
//Now, read wFormatLength
OpenFile.read((char*)&wave.wFormatLength,sizeof(wave.wFormatLength));
//Now, read the wFormatTag (which tells whther it is PCM wave file)
OpenFile.read((char*)&wave.wFormatTag,sizeof(wave.wFormatTag));
//Now, read the number of channels (1-mono, 2-stereo)
OpenFile.read((char*)&wave.nChannels,sizeof(wave.nChannels));
//Read the sample rate - 11025,22050 or 44100
OpenFile.read((char*)&wave.nSamplesPerSec,sizeof(wave.nSamplesPerSec));
//The average number of bytes per second
OpenFile.read((char*)&wave.nAvgBytesPerSec,sizeof(wave.nAvgBytesPerSec));
//1 is for 8bit data, 2 is for 16bit data
OpenFile.read((char*)&wave.nBlockAlign,sizeof(wave.nBlockAlign));
//The same as above
OpenFile.read((char*)&wave.wBitsPerSample,sizeof(wave.wBitsPerSample));
//Now, read 'data'
OpenFile.read(wave.data,sizeof(wave.data));
//The number of bytes of the sound itself
OpenFile.read((char*)&wave.datasize,sizeof(wave.datasize));
//Close the file
OpenFile.close();
return 0;
}
int wavKaraoke(char *InputFileName, char *OutputFileName)
{
ifstream OpenFile(InputFileName,ios::binary);
ofstream SaveFile(OutputFileName, ios::binary);
//Read the wave header
WAVEHEADER waveHeader;
ReadWaveHeader(InputFileName,waveHeader);
//Now, put it into the new file
SaveFile.write((char *)&waveHeader,sizeof(waveHeader));
int oldLeft,oldRight,newLeft,newRight;
while(!OpenFile.eof())
{
//assume that it is stereo file
//Read one left one right channel
OpenFile.read((char *)&oldLeft, sizeof(oldLeft));
OpenFile.read((char *)&oldRight,sizeof(oldRight));
newLeft = oldLeft - oldRight;
newRight = oldRight - oldLeft;
SaveFile.write((char *)&newLeft, sizeof(newLeft));
SaveFile.write((char *)&newRight,sizeof(newRight));
}
OpenFile.close();
SaveFile.close();
return 0;
}
int main()
{
wavKaraoke("e:\\Shadows.wav","e:\\zShadowsKaraoke.wav");
cout << "Done!" << endl;
return 0;
}
It does the new file, but the new file sounds very bad... nothing to do with Karaoke.