Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to get BPM Value #2

Open
loretoparisi opened this issue Nov 12, 2013 · 2 comments
Open

Unable to get BPM Value #2

loretoparisi opened this issue Nov 12, 2013 · 2 comments

Comments

@loretoparisi
Copy link

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
Copy link
Author

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
Copy link
Owner

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
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants