Detect BPM rate of inFile and adjust tempo setting accordingly if necessary
| 228 | |
| 229 | // Detect BPM rate of inFile and adjust tempo setting accordingly if necessary |
| 230 | static void detectBPM(WavInFile *inFile, RunParameters *params) |
| 231 | { |
| 232 | float bpmValue; |
| 233 | int nChannels; |
| 234 | BPMDetect bpm(inFile->getNumChannels(), inFile->getSampleRate()); |
| 235 | SAMPLETYPE sampleBuffer[BUFF_SIZE]; |
| 236 | |
| 237 | // detect bpm rate |
| 238 | fprintf(stderr, "Detecting BPM rate..."); |
| 239 | fflush(stderr); |
| 240 | |
| 241 | nChannels = (int)inFile->getNumChannels(); |
| 242 | assert(BUFF_SIZE % nChannels == 0); |
| 243 | |
| 244 | // Process the 'inFile' in small blocks, repeat until whole file has |
| 245 | // been processed |
| 246 | while (inFile->eof() == 0) |
| 247 | { |
| 248 | int num, samples; |
| 249 | |
| 250 | // Read sample data from input file |
| 251 | num = inFile->read(sampleBuffer, BUFF_SIZE); |
| 252 | |
| 253 | // Enter the new samples to the bpm analyzer class |
| 254 | samples = num / nChannels; |
| 255 | bpm.inputSamples(sampleBuffer, samples); |
| 256 | } |
| 257 | |
| 258 | // Now the whole song data has been analyzed. Read the resulting bpm. |
| 259 | bpmValue = bpm.getBpm(); |
| 260 | fprintf(stderr, "Done!\n"); |
| 261 | |
| 262 | // rewind the file after bpm detection |
| 263 | inFile->rewind(); |
| 264 | |
| 265 | if (bpmValue > 0) |
| 266 | { |
| 267 | fprintf(stderr, "Detected BPM rate %.1f\n\n", bpmValue); |
| 268 | } |
| 269 | else |
| 270 | { |
| 271 | fprintf(stderr, "Couldn't detect BPM rate.\n\n"); |
| 272 | return; |
| 273 | } |
| 274 | |
| 275 | if (params->goalBPM > 0) |
| 276 | { |
| 277 | // adjust tempo to given bpm |
| 278 | params->tempoDelta = (params->goalBPM / bpmValue - 1.0f) * 100.0f; |
| 279 | fprintf(stderr, "The file will be converted to %.1f BPM\n\n", params->goalBPM); |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | |
| 284 |
no test coverage detected