Reading a Wav Header
-
Hello, I'm on the beg again :) I'm trying to read in the header of a wav file. My code so far is..
struct { WORD wFormatTag; // Format category WORD wChannels; // Number of channels DWORD dwSamplesPerSec; // Sampling rate DWORD dwAvgBytesPerSec; // For buffer estimation WORD wBlockAlign; // Data block size } FormatChunk; char ch[1]; CFile waveFile; if(waveFile.Open(wavName,CFile::modeRead|CFile::typeBinary)==FALSE) { TRACE("Error opening file"); return NULL; } //read till we get to the start of above chunk header while(1) { waveFile.Read(ch,1); TRACE("%c\n",ch[0]); if(ch[0] == 'E') break; } waveFile.Read(&FormatChunk,sizeof(FormatChunk)); DWORD temp = FormatChunk.dwAvgBytesPerSec; //FormatChunk.dwAvgBytesPerSec holds the sampling rate TRACE("Sampling rate is : %ld\n",temp); TRACE("wChannels is : %0.2d\n",FormatChunk.wChannels); // TRACE("format tag is : %s\n",FormatChunk.wFormatTag); waveFile.Close(); return temp;
Ignore the ugliness of it please :) My problem is reading in the wChannels value from byte offset 16, its a 2byte value. It should either be 0x10 or 0x20 in the file but I'm having problems reading it right. I've been playing with different variables to get it read right but all I get is either 16 for all of them or something like 8016 :( Or have I totally missed the point and theres an API function out there that'll do it for me? :) Thanks :) -
Hello, I'm on the beg again :) I'm trying to read in the header of a wav file. My code so far is..
struct { WORD wFormatTag; // Format category WORD wChannels; // Number of channels DWORD dwSamplesPerSec; // Sampling rate DWORD dwAvgBytesPerSec; // For buffer estimation WORD wBlockAlign; // Data block size } FormatChunk; char ch[1]; CFile waveFile; if(waveFile.Open(wavName,CFile::modeRead|CFile::typeBinary)==FALSE) { TRACE("Error opening file"); return NULL; } //read till we get to the start of above chunk header while(1) { waveFile.Read(ch,1); TRACE("%c\n",ch[0]); if(ch[0] == 'E') break; } waveFile.Read(&FormatChunk,sizeof(FormatChunk)); DWORD temp = FormatChunk.dwAvgBytesPerSec; //FormatChunk.dwAvgBytesPerSec holds the sampling rate TRACE("Sampling rate is : %ld\n",temp); TRACE("wChannels is : %0.2d\n",FormatChunk.wChannels); // TRACE("format tag is : %s\n",FormatChunk.wFormatTag); waveFile.Close(); return temp;
Ignore the ugliness of it please :) My problem is reading in the wChannels value from byte offset 16, its a 2byte value. It should either be 0x10 or 0x20 in the file but I'm having problems reading it right. I've been playing with different variables to get it read right but all I get is either 16 for all of them or something like 8016 :( Or have I totally missed the point and theres an API function out there that'll do it for me? :) Thanks :)Accoring to this[^] your FormatChunk structure is incorrect. It should be: struct { DWORD wFormatTag; // Format category DWORD dwFormatLen; // Length of format chunk - always 0x10 WORD wReserved; // Always 0x01 WORD wChannels; // Number of channels DWORD dwSamplesPerSec; // Sampling rate DWORD dwAvgBytesPerSec; // For buffer estimation WORD wBytesPerSample; WORD wBitsPerSample; } FormatChunk; Hope this helps, Phil
-
Accoring to this[^] your FormatChunk structure is incorrect. It should be: struct { DWORD wFormatTag; // Format category DWORD dwFormatLen; // Length of format chunk - always 0x10 WORD wReserved; // Always 0x01 WORD wChannels; // Number of channels DWORD dwSamplesPerSec; // Sampling rate DWORD dwAvgBytesPerSec; // For buffer estimation WORD wBytesPerSample; WORD wBitsPerSample; } FormatChunk; Hope this helps, Phil