Pregunta

I'm working on a project that will involve having to process PCM audio data through fft as its being played, preferably in sync. I'm using a linux g++ compiler and currently reading and playing audio data using OpenAL.

My question is this: is there a better way to process PCM audio data with an fft live as the audio is playing then using threads? If not, then what threading library would be best to use for these purposes.

this is my function that loads the wave data into an array of bytes, these can later be cast to ints for processing and all I use to play the data is OpenAL.

    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;
}

thank you for any and all help.

edit: to anyone who might be interested I wrote the function using this as a reference to know how a WAV file is formated

¿Fue útil?

Solución

Are you hoping to perform the FFT using OpenAL? I don't know if that's possible. Your code will likely be performing the FFT.

You don't need to explicitly set up any threads. However, your audio output library will probably do so on your behalf. I'm not familiar with OpenAL, but the way that a lot of audio libraries operate is by letting you specify a callback that will feed more audio into the output. Thus, your main program will load audio from the audio file, stuff it into a buffer (likely protected by a mutex) for the audio callback to read, compute an FFT over the audio window, and perhaps visualize the data for the user.

Again, the audio library will probably be managing the threading so you don't need to worry about the exact threading implementation or library. But be sure to manage shared data correctly with a mutex.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top