cjcliffe / beatdetektor

BeatDetektor BPM detection / visualization library
http://www.beatdetektor.com
324 stars 54 forks source link

Unable to get BPM Value #2

Open loretoparisi opened 10 years ago

loretoparisi commented 10 years ago

I have a wave file. I go through 1025 bytes samples and read the wav

std::vector<float> floatVect[ 1024 ];

while (inFile->eof() == 0) {

     // Read a chunk of samples from the input file
    num = inFile->read(shortBuffer, BUFF_SIZE);
    samples = num / nChannels;

     //// here we calculate FFT with AccelerateToolbox in output vector
     ////// short int shortBuffer go to FFT samples in output
     ///// now convert to float

   for(int i=0;i<samples;i++) {
        float fSample = output[ i ] / 32768.0f;
        floatVect->push_back(fSample);   //// <---- push to the float vector
    }

}

 /// calculate BPM
 bpmDetect->process( inFile->getLengthMS()/1000.0f, *floatVect);
  bpm = bpmDetect->winning_bpm;
 bpmDetect->reset();

 inFile->rewind();

The resulting bpm is always bpm = 0.

Is that right to accumulate the float samples from the FFT and then pass the vector to the process() method with the wav duration expressed in seconds ?

loretoparisi commented 10 years ago

I realized that the calculation should be done for each buffer:

while (inFile->eof() == 0)
{

    // ....

   for(int i=0;i<samples;i++) {
        float fSample = output[ i ] / 32768.0f;
        floatVect->push_back(fSample);
    }
    bpmDetect->process((*inFile).getElapsedMS(), *floatVect);

    // ....

 }

 bpm = bpmDetect->winning_bpm;
bpmDetect->reset();

The problem is the same in this case too, bpm = 0

Could it be a buffer problem with the

std::vector<float> floatVect[BUFF_SIZE];
cjcliffe commented 10 years ago

I can't tell from your code what the overall process before the detection is and whether you've cleared the floatVect, what the FFT output is, etc.. but I'll outline the basic idea as reference for everyone.

The input is the current FFT frame, and you'll want to keep the symmetry (don't just take half of the fft result array -- explanation for that is enough for another discussion though) and the time at which it was sampled from.

If you're processing an audio file you'll need to choose your frame rate so that you get enough quality FFT frames per second for your sample rate. For this example we'll assume a function exists that does that called getSamplesBySeconds, which at 29.4 fps for 44100hz would give us 1500 samples per frame.

Some basic pseudo-code for that process is:

timer = 0;
frames_per_second = 29.4;
while (samplesAvailable()) {

   // get a second worth of samples
   samples = getSamplesBySeconds(1 / frames_per_second);

   // get full FFT frame for this sample
   fft = getFFT(samples);

   // run BeatDetektor
   bpmDetect->process(timer, fft); 

   timer += 1 / frames_per_second;  // increment timer
}