Advertisement
AudioByte

RIFF Reader

May 5th, 2011
346
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.23 KB | None | 0 0
  1.  
  2. /* RIFF Reader by James Branston */
  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      */
  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("/Users/jtjbranston/Documents/Programming/ReadRIFFHeader/build/Debug/test.wav", "r");
  43.     if(!fp)
  44.     {
  45.         perror("open");
  46.         return errormessage("Error: Cannot Open File");
  47.     }
  48.    
  49.     WavHeader header;
  50.     fread(&header, sizeof(WavHeader), 1, fp);
  51.    
  52.     if(!strcmp(header.RIFF, "RIFF"))
  53.     {
  54.         errormessage("Error: Not RIFF Format!");
  55.     }
  56.    
  57.     if(!strcmp(header.WAVE, "WAVE"))
  58.     {
  59.         return errormessage("Error: File Is Not .Wav");
  60.     }
  61.    
  62.     if(!strcmp(header.fmt, "fmt "))
  63.     {
  64.         return errormessage("Error: 'fmt ' Error");
  65.     }
  66.    
  67.     cout << "File Size: " << header.fileSize << endl;
  68.     cout << "Chunk Size: " << header.chunkSize << endl;
  69.     cout << "Format Type: " << header.audioFormat << endl;
  70.     cout << "Channels: " << header.numOfChan << endl;
  71.     cout << "Sample Rate: " << header.samplesPerSec << endl;
  72.     cout << "Bytes Per Sec: " << header.bytesPerSec << endl;
  73.     cout << "Press Enter To End";
  74.    
  75.     while(cin.get() != 10);
  76.    
  77.     return 0;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement