Topic : How to Load a Wave File
Author : Nathan Davidson
Page : << Previous 2  
Go to page :


  { //we had 'RIFF' let's continue
            fread(size, sizeof(DWORD), 1, fp); //read in 32bit size value
            fread(id, sizeof(BYTE), 4, fp); //read in 4 byte string now
            if (!strcmp(id,"WAVE"))
            { //this is probably a wave file since it contained "WAVE"
                fread(id, sizeof(BYTE), 4, fp); //read in 4 bytes "fmt ";
                fread(&format_length, sizeof(DWORD),1,fp);
                fread(&format_tag, sizeof(short), 1, fp); //check mmreg.h (i think?) for other
                                                              // possible format tags like ADPCM
                fread(&channels, sizeof(short),1,fp); //1 mono, 2 stereo
                fread(&sample_rate, sizeof(DWORD), 1, fp); //like 44100, 22050, etc...
                fread(&avg_bytes_sec, sizeof(short), 1, fp); //probably won't need this
                fread(&block_align, sizeof(short), 1, fp); //probably won't need this
                fread(&bits_per_sample, sizeof(short), 1, fp); //8 bit or 16 bit file?
                fread(id, sizeof(BYTE), 4, fp); //read in 'data'
                fread(&data_size, sizeof(DWORD), 1, fp); //how many bytes of sound data we have
                sound_buffer = (BYTE *) malloc (sizeof(BYTE) * data_size); //set aside sound buffer space
                fread(sound_buffer, sizeof(BYTE), data_size, fp); //read in our whole sound data chunk
            }
            else
                printf("Error: RIFF file but not a wave file\n");
        }
        else
            printf("Error: not a RIFF file\n");
    }
}


There you go, it's as easy as that, there's probably a few errors in this code as I didn't have time to compile and test it, and you should probably do a bit more file error checking and other fancy things to make sure the data isn't corrupted and it looks ok. Once you have it loaded it's very simple to work with, you can manipulate your sound_buffer any way you need to or put it into a structure that windows applications like (look up WAVEFORMATEX in your compilers reference section). If your format_tag is something other than a 1 (1 means your data is stored in PCM or Pulse Code Modulation form) then you have some sort of compressed file, and in order to handle it you're going to have to know exactly how that data is compressed, and with so many formats out there (see mmreg.h I think? for a list of all the format tags) it's not practical that you're going to be able to handle all of them. Good luck...


Disclaimer and Distribution Info


This file is Copyright 1998, Nathan Davidson
You may NOT distribute this tutorial or put it up on your website without getting my permission first.
All information is provided "as is" and the author assumes no responsibility for any damage caused by use of information in this tutorial. Use at your own risk.



If you want to see some other sound tutorials then head over to my web site, currently located at:
http://www.aros.net/~npawn/

Reprinted with permission


Page : << Previous 2