Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Reading a Wav Header

Reading a Wav Header

Scheduled Pinned Locked Moved C / C++ / MFC
helpdebuggingjsonquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    carrie
    wrote on last edited by
    #1

    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 :)

    P 1 Reply Last reply
    0
    • C carrie

      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 :)

      P Offline
      P Offline
      Phil Speller
      wrote on last edited by
      #2

      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

      C 1 Reply Last reply
      0
      • P Phil Speller

        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

        C Offline
        C Offline
        carrie
        wrote on last edited by
        #3

        Phil, thou art a god :) Knew it had to be something stupid that I was doing, thanks for that link too :) /me gets out my winsocks books to see about your question :)

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups