2

This is my first time working with wav files and fft alike. Given the following code:

    char* loadWAV(const char* fn, int& chan, int& samplerate, int& bps, int& size){
        char buffer[4];
        ifstream in(fn, ios::binary);
        in.read(buffer, 4);                                                             //ChunkID "RIFF"
        if(strncmp(buffer, "RIFF", 4) != 0){ 
                cerr << "this is not a valid wave file";
                return NULL;
        }   
        in.read(buffer,4);                                                              //ChunkSize 
        in.read(buffer,4);                                                              //Format "WAVE"
        in.read(buffer,4);                                                              // "fmt "
        in.read(buffer,4);                                                              // 16
        in.read(buffer,2);                                                              // 1
        in.read(buffer,2);                                                              // NUMBER OF CHANNELS
        chan = convertToInt(buffer,2);
        in.read(buffer,4);                                                              // SAMPLE RATE
        samplerate = convertToInt(buffer,4);
        in.read(buffer,4);                                                              // ByteRate
        in.read(buffer,2);                                                              // BlockAlign
        in.read(buffer,2);                                                              // bits per sample
        bps = convertToInt(buffer,2);
        in.read(buffer,4);                                                              // "data"
        in.read(buffer,4);
        size = convertToInt(buffer,4);
        char * data = new char[size];
        in.read(data,size);
        return data;
}

I'm assuming data pointer contains the information I need, but I don't know how to discern this information. I'm using this as a reference but I don't know what to make of the "right channel left channel" aspect and how to prepare this data to be used in an FFT. If you have any references to good documentation about this I appreciate it, My search efforts have so far resulted in NILL.

edit: also If anyone could point me to a good guide to operating with wav format files on this level i would greatly appreciate it. Thank you in advance.

ritual_code
  • 137
  • 2
  • 12
  • Do you know what format you want the data in? What is your FFT function expecting? – RJFalconer Feb 18 '14 at 17:16
  • Regarding left/right: the site is saying that a stereo source will have double the sample size (see how ByteRate is multiplied by numChannels), and that left/right will be streamed interleaved (a given chunk "Subchunk2Size" contains both sides). – RJFalconer Feb 18 '14 at 17:18
  • I see, I got to look over the wavread.m file in the gnu octave source and It seems to take that logic into account when reading it in. I'm going to try to copy it over into c++. I'm trying to do pulse detection, and from the looks of the vector produced from octave's wavread function I might not need FFT. plotting it against time gives me the frequencies over time. Now I need to figure out how use the data while playing the audio to sync and do pulse detection. – ritual_code Feb 19 '14 at 00:55

1 Answers1

1

The data you have is in PCM packets.

Try these questions as a starting point:

For the FFT part of your question, you may want to consider https://dsp.stackexchange.com/.

Community
  • 1
  • 1
RJFalconer
  • 8,488
  • 3
  • 44
  • 58
  • I was thinking of using the code in the gnu octave source (wavread.m) It conveniently reads the data into a vector and plotting it give me the proper frequencies. is translating it into a c++ function the way to go? This would mean that I have to process the data before hand and come up with a way to sync a stream of the data with the audio. should I multithread if I'm using openal to play the wav stream from a separate samplebuffer? – ritual_code Feb 19 '14 at 00:48
  • I'm trying to do pulse detection, the end goal is to make a robot dance to a wav file. Thanks. – ritual_code Feb 19 '14 at 00:49
  • 1
    That's a lot of questions. RE: C++, use whichever language you feel comfortable with. C++ is a good way to go. RE: syncing streams, this is usually quite difficult. RE: multithread, most likely, but you should focus on getting it working on one large buffer before switching to a producer-consumer pattern. Hope this helps. – RJFalconer Feb 19 '14 at 09:26