Advertisement
AudioByte

C++ RIFF HEADER READER

May 5th, 2011
665
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.17 KB | None | 0 0
  1.  
  2. // LoadWavHeader.cpp : main project file.
  3.  
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <iostream>
  7. #include <stdint.h>
  8.  
  9. using namespace std;
  10.  
  11.  
  12. class WavHeader
  13. {
  14. public:
  15.     char                RIFF[4];        /* RIFF Header      */ //Magic header
  16.     uint32_t            fileSize;      /* RIFF Chunk Size  */
  17.     char                WAVE[4];        /* WAVE Header      */
  18.     char                fmt[4];        /* FMT header      */
  19.     uint32_t            chunkSize;      /* Size of the fmt chunk                                */
  20.     uint16_t            audioFormat;    /* Audio format 1=PCM,6=mulaw,7=alaw, 257=IBM Mu-Law, 258=IBM A-Law, 259=ADPCM */
  21.     uint16_t            numOfChan;      /* Number of channels 1=Mono 2=Sterio                  */
  22.     uint32_t            samplesPerSec;  /* Sampling Frequency in Hz                            */
  23.     uint32_t            bytesPerSec;    /* bytes per second */
  24.     uint16_t            blockAlign;    /* 2=16-bit mono, 4=16-bit stereo */
  25.     uint16_t            bitsPerSample;  /* Number of bits per sample      */
  26.     char                subchunk2ID[4]; /* "data"  string  */
  27.     uint32_t            subchunk2Size;  /* Sampled data length    */
  28. };
  29.  
  30.  
  31. int errormessage(const char* msg, int error = 0)
  32. {
  33.     cout << msg << endl;
  34.     while(cin.get() != 10);
  35.    
  36.     return error;
  37. }
  38.  
  39. int main()
  40. {
  41.     FILE *fp = NULL;
  42.     fp = fopen("test.wav", "r");
  43.     if(!fp)
  44.     {
  45.         return errormessage("Error: Cannot Open File");
  46.     }
  47.    
  48.     WavHeader header;
  49.     fread(&header, sizeof(WavHeader), 1, fp);
  50.    
  51.     if(!strcmp(header.RIFF, "RIFF"))
  52.     {
  53.         errormessage("Error: Not RIFF Format!");
  54.     }
  55.    
  56.     if(!strcmp(header.WAVE, "WAVE"))
  57.     {
  58.         return errormessage("Error: File Is Not .Wav");
  59.     }
  60.    
  61.     if(!strcmp(header.fmt, "fmt "))
  62.     {
  63.         return errormessage("Error: 'fmt ' Error");
  64.     }
  65.    
  66.     cout << "File Size: " << header.fileSize << endl;
  67.     cout << "Chunk Size: " << header.chunkSize << endl;
  68.     cout << "Format Type: " << header.audioFormat << endl;
  69.     cout << "Channels: " << header.numOfChan << endl;
  70.     cout << "Sample Rate: " << header.samplesPerSec << endl;
  71.     cout << "Bytes Per Sec: " << header.bytesPerSec << endl;
  72.     cout << "Press Enter To End";
  73.    
  74.     while(cin.get() != 10);
  75.    
  76.     return 0;
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement