/* Date :
* Author:
* Course: [AISE-M/ITS-M/ITSB-M] DSP[1/2]
* Code based on following references:
* [1]quot;Tutorial: Decoding Audio (Windows)quot;, msdn.microsoft.com, 2018. [Online]. Available: https://msdn.microsoft.com/en-us/library/windows/desktop/dd757929(v=vs.85).aspx. [Accessed: 23-03-2018].
*/
#include
#include
typedef struct
{
char RiffSignature[4]; // Should be quot;RIFFquot;
uint32_t FileLengthMinus8Byte; // Should be WAV file length minus 8 Byte
char WaveSignature[4]; // Should be quot;WAVEquot;
} RiffHeader;
typedef struct
{
char fmtSignature[4]; // Should be quot;fmt quot; (including space)
uint32_t RemainingHeaderLength; // Should be 16 Byte
uint16_t fmtTag; // e.g. 0x01 for PCM
uint16_t NumChannels; // e.g. 2 for Stereo
uint32_t SamplingRate; // e.g. 44100 Hz
uint32_t BytesPerSecond; // e.g. 176400 (= 44100 * 4) Bytes per Second for Stereo 16-bit PCM???
uint16_t BlockAlign; // e.g. 4 Bytes for Stereo 16-bit PCM
uint16_t BitsPerSample; // e.g. 16 for a 16-bit PCM channel
} FmtHeader;
typedef struct
{
char DataSignature[4]; // Should be quot;dataquot;
uint32_t DataLength; // Maximum: File length - 44
} DataHeader;
typedef struct
{
RiffHeader riffHeader;
FmtHeader fmtHeader;
DataHeader dataHeader;
} WaveHeader;
int main(int argc, char* argv[])
{
if(argc < 3)
{
printf(quot;CLI call: wav input.wav output.wav\r\nquot;);
return 0;
}
FILE* inputFile = fopen(argv[1],quot;rbquot;);
FILE* utputFile = fopen(argv[2], quot;wbquot;);
WaveHeader waveHeader;
// Skip wave header till we know how much bytes are written
fseek(outputFile, sizeof(WaveHeader), 0);
fread(waveHeader, sizeof(WaveHeader), 1, inputFile);
int16_t buffer[2048], leftSample[1024], rightSample[1024],leftFiltered[1024], rightFiltered[1024];
for (uint32_t blockCount = 0; blockCount < waveHeader.dataHeader.DataLength; blockCount += sizeof(int16_t)*2048)
{
fread(buffer, sizeof(int16_t), 2048, inputFile);
// Deinterleave
for (uint16_t i = 0; i < 1024; i++)
{
leftSample[i] = buffer[2 * i];
rightSample[i] = buffer[2 * i + 1];
}
// TODO: Processing
for(uint16_t i=0; i<1024;i++)
{
leftFiltered[i] = leftSample[i];
rightFiltered[i] = rightSample[i];
}
// Interleave
for (uint16_t i = 0; i < 1024; i++)
{
buffer[2 * i] = leftFiltered[i];
buffer[2 * i + 1] = rightFiltered[i];
}
fwrite(buffer, sizeof(int16_t), 2048, outputFile);
}
// Go to beginning of output file and write updated wave header
fseek(outputFile, 0, 0);
// Write updated wave header
// TODO: Modify waveHeader accordingly
fwrite(waveHeader, sizeof(WaveHeader), 1, outputFile);
fclose(outputFile);
fclose(inputFile);
return 0;
}