123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573 |
- #define _USE_MATH_DEFINES
- #include <math.h>
- #include <assert.h>
- #include <string.h>
- #include <stdio.h>
- #include <cfloat>
- #include "FIFOSampleBuffer.h"
- #include "PeakFinder.h"
- #include "BPMDetect.h"
- using namespace soundtouch;
- static const int INPUT_BLOCK_SIZE = 2048;
- static const int DECIMATED_BLOCK_SIZE = 256;
- static const int TARGET_SRATE = 1000;
- static const int XCORR_UPDATE_SEQUENCE = (int)(TARGET_SRATE / 5);
- static const int MOVING_AVERAGE_N = 15;
- static const double XCORR_DECAY_TIME_CONSTANT = 30.0;
- static const int OVERLAP_FACTOR = 4;
- static const double TWOPI = (2 * M_PI);
- #ifdef _CREATE_BPM_DEBUG_FILE
- static void _SaveDebugData(const char *name, const float *data, int minpos, int maxpos, double coeff)
- {
- FILE *fptr = fopen(name, "wt");
- int i;
- if (fptr)
- {
- printf("\nWriting BPM debug data into file %s\n", name);
- for (i = minpos; i < maxpos; i ++)
- {
- fprintf(fptr, "%d\t%.1lf\t%f\n", i, coeff / (double)i, data[i]);
- }
- fclose(fptr);
- }
- }
- void _SaveDebugBeatPos(const char *name, const std::vector<BEAT> &beats)
- {
- printf("\nWriting beat detections data into file %s\n", name);
- FILE *fptr = fopen(name, "wt");
- if (fptr)
- {
- for (uint i = 0; i < beats.size(); i++)
- {
- BEAT b = beats[i];
- fprintf(fptr, "%lf\t%lf\n", b.pos, b.strength);
- }
- fclose(fptr);
- }
- }
- #else
- #define _SaveDebugData(name, a,b,c,d)
- #define _SaveDebugBeatPos(name, b)
- #endif
- void hamming(float *w, int N)
- {
- for (int i = 0; i < N; i++)
- {
- w[i] = (float)(0.54 - 0.46 * cos(TWOPI * i / (N - 1)));
- }
- }
- IIR2_filter::IIR2_filter(const double *lpf_coeffs)
- {
- memcpy(coeffs, lpf_coeffs, 5 * sizeof(double));
- memset(prev, 0, sizeof(prev));
- }
- float IIR2_filter::update(float x)
- {
- prev[0] = x;
- double y = x * coeffs[0];
- for (int i = 4; i >= 1; i--)
- {
- y += coeffs[i] * prev[i];
- prev[i] = prev[i - 1];
- }
- prev[3] = y;
- return (float)y;
- }
- const double _LPF_coeffs[5] = { 0.00996655391939, -0.01944529148401, 0.00996655391939, 1.96867605796247, -0.96916387431724 };
- BPMDetect::BPMDetect(int numChannels, int aSampleRate) :
- beat_lpf(_LPF_coeffs)
- {
- beats.reserve(250);
- this->sampleRate = aSampleRate;
- this->channels = numChannels;
- decimateSum = 0;
- decimateCount = 0;
-
- decimateBy = sampleRate / TARGET_SRATE;
- if ((decimateBy <= 0) || (decimateBy * DECIMATED_BLOCK_SIZE < INPUT_BLOCK_SIZE))
- {
- ST_THROW_RT_ERROR("Too small samplerate");
- }
-
- windowLen = (60 * sampleRate) / (decimateBy * MIN_BPM);
- windowStart = (60 * sampleRate) / (decimateBy * MAX_BPM_RANGE);
- assert(windowLen > windowStart);
-
- xcorr = new float[windowLen];
- memset(xcorr, 0, windowLen * sizeof(float));
- pos = 0;
- peakPos = 0;
- peakVal = 0;
- init_scaler = 1;
- beatcorr_ringbuffpos = 0;
- beatcorr_ringbuff = new float[windowLen];
- memset(beatcorr_ringbuff, 0, windowLen * sizeof(float));
-
- buffer = new FIFOSampleBuffer();
-
- buffer->setChannels(1);
- buffer->clear();
-
- hamw = new float[XCORR_UPDATE_SEQUENCE];
- hamming(hamw, XCORR_UPDATE_SEQUENCE);
- hamw2 = new float[XCORR_UPDATE_SEQUENCE / 2];
- hamming(hamw2, XCORR_UPDATE_SEQUENCE / 2);
- }
- BPMDetect::~BPMDetect()
- {
- delete[] xcorr;
- delete[] beatcorr_ringbuff;
- delete[] hamw;
- delete[] hamw2;
- delete buffer;
- }
- int BPMDetect::decimate(SAMPLETYPE *dest, const SAMPLETYPE *src, int numsamples)
- {
- int count, outcount;
- LONG_SAMPLETYPE out;
- assert(channels > 0);
- assert(decimateBy > 0);
- outcount = 0;
- for (count = 0; count < numsamples; count ++)
- {
- int j;
-
- for (j = 0; j < channels; j ++)
- {
- decimateSum += src[j];
- }
- src += j;
- decimateCount ++;
- if (decimateCount >= decimateBy)
- {
-
- out = (LONG_SAMPLETYPE)(decimateSum / (decimateBy * channels));
- decimateSum = 0;
- decimateCount = 0;
- #ifdef SOUNDTOUCH_INTEGER_SAMPLES
-
- if (out > 32767)
- {
- out = 32767;
- }
- else if (out < -32768)
- {
- out = -32768;
- }
- #endif
- dest[outcount] = (SAMPLETYPE)out;
- outcount ++;
- }
- }
- return outcount;
- }
- void BPMDetect::updateXCorr(int process_samples)
- {
- int offs;
- SAMPLETYPE *pBuffer;
-
- assert(buffer->numSamples() >= (uint)(process_samples + windowLen));
- assert(process_samples == XCORR_UPDATE_SEQUENCE);
- pBuffer = buffer->ptrBegin();
-
- float xcorr_decay = (float)pow(0.5, 1.0 / (XCORR_DECAY_TIME_CONSTANT * TARGET_SRATE / process_samples));
-
- float tmp[XCORR_UPDATE_SEQUENCE];
- for (int i = 0; i < process_samples; i++)
- {
- tmp[i] = hamw[i] * hamw[i] * pBuffer[i];
- }
- #pragma omp parallel for
- for (offs = windowStart; offs < windowLen; offs ++)
- {
- float sum;
- int i;
- sum = 0;
- for (i = 0; i < process_samples; i ++)
- {
- sum += tmp[i] * pBuffer[i + offs];
- }
- xcorr[offs] *= xcorr_decay;
- xcorr[offs] += (float)fabs(sum);
- }
- }
- void BPMDetect::updateBeatPos(int process_samples)
- {
- SAMPLETYPE *pBuffer;
- assert(buffer->numSamples() >= (uint)(process_samples + windowLen));
- pBuffer = buffer->ptrBegin();
- assert(process_samples == XCORR_UPDATE_SEQUENCE / 2);
-
- double posScale = (double)this->decimateBy / (double)this->sampleRate;
- int resetDur = (int)(0.12 / posScale + 0.5);
-
- float tmp[XCORR_UPDATE_SEQUENCE / 2];
- for (int i = 0; i < process_samples; i++)
- {
- tmp[i] = hamw2[i] * hamw2[i] * pBuffer[i];
- }
- #pragma omp parallel for
- for (int offs = windowStart; offs < windowLen; offs++)
- {
- float sum = 0;
- for (int i = 0; i < process_samples; i++)
- {
- sum += tmp[i] * pBuffer[offs + i];
- }
- beatcorr_ringbuff[(beatcorr_ringbuffpos + offs) % windowLen] += (float)((sum > 0) ? sum : 0);
- }
- int skipstep = XCORR_UPDATE_SEQUENCE / OVERLAP_FACTOR;
-
- float scale = (float)windowLen / (float)(skipstep * init_scaler);
- if (scale > 1.0f)
- {
- init_scaler++;
- }
- else
- {
- scale = 1.0f;
- }
-
- for (int i = 0; i < skipstep; i++)
- {
- LONG_SAMPLETYPE max = 0;
- float sum = beatcorr_ringbuff[beatcorr_ringbuffpos];
- sum -= beat_lpf.update(sum);
- if (sum > peakVal)
- {
-
- peakVal = sum;
- peakPos = pos;
- }
- if (pos > peakPos + resetDur)
- {
-
- peakPos += skipstep;
- if (peakVal > 0)
- {
-
- BEAT temp = { (float)(peakPos * posScale), (float)(peakVal * scale) };
- beats.push_back(temp);
- }
- peakVal = 0;
- peakPos = pos;
- }
- beatcorr_ringbuff[beatcorr_ringbuffpos] = 0;
- pos++;
- beatcorr_ringbuffpos = (beatcorr_ringbuffpos + 1) % windowLen;
- }
- }
- #define max(x,y) ((x) > (y) ? (x) : (y))
- void BPMDetect::inputSamples(const SAMPLETYPE *samples, int numSamples)
- {
- SAMPLETYPE decimated[DECIMATED_BLOCK_SIZE];
-
- while (numSamples > 0)
- {
- int block;
- int decSamples;
- block = (numSamples > INPUT_BLOCK_SIZE) ? INPUT_BLOCK_SIZE : numSamples;
-
- decSamples = decimate(decimated, samples, block);
- samples += block * channels;
- numSamples -= block;
- buffer->putSamples(decimated, decSamples);
- }
-
- int req = max(windowLen + XCORR_UPDATE_SEQUENCE, 2 * XCORR_UPDATE_SEQUENCE);
- while ((int)buffer->numSamples() >= req)
- {
-
- updateXCorr(XCORR_UPDATE_SEQUENCE);
-
- updateBeatPos(XCORR_UPDATE_SEQUENCE / 2);
-
- int n = XCORR_UPDATE_SEQUENCE / OVERLAP_FACTOR;
- buffer->receiveSamples(n);
- }
- }
- void BPMDetect::removeBias()
- {
- int i;
-
-
- double mean_i = 0;
- double mean_x = 0;
- for (i = windowStart; i < windowLen; i++)
- {
- mean_x += xcorr[i];
- }
- mean_x /= (windowLen - windowStart);
- mean_i = 0.5 * (windowLen - 1 + windowStart);
-
- double b = 0;
- double div = 0;
- for (i = windowStart; i < windowLen; i++)
- {
- double xt = xcorr[i] - mean_x;
- double xi = i - mean_i;
- b += xt * xi;
- div += xi * xi;
- }
- b /= div;
-
- float minval = FLT_MAX;
- for (i = windowStart; i < windowLen; i ++)
- {
- xcorr[i] -= (float)(b * i);
- if (xcorr[i] < minval)
- {
- minval = xcorr[i];
- }
- }
-
- for (i = windowStart; i < windowLen; i ++)
- {
- xcorr[i] -= minval;
- }
- }
- void MAFilter(float *dest, const float *source, int start, int end, int N)
- {
- for (int i = start; i < end; i++)
- {
- int i1 = i - N / 2;
- int i2 = i + N / 2 + 1;
- if (i1 < start) i1 = start;
- if (i2 > end) i2 = end;
- double sum = 0;
- for (int j = i1; j < i2; j ++)
- {
- sum += source[j];
- }
- dest[i] = (float)(sum / (i2 - i1));
- }
- }
- float BPMDetect::getBpm()
- {
- double peakPos;
- double coeff;
- PeakFinder peakFinder;
-
- removeBias();
- coeff = 60.0 * ((double)sampleRate / (double)decimateBy);
-
- _SaveDebugData("soundtouch-bpm-xcorr.txt", xcorr, windowStart, windowLen, coeff);
-
- float *data = new float[windowLen];
- memset(data, 0, sizeof(float) * windowLen);
- MAFilter(data, xcorr, windowStart, windowLen, MOVING_AVERAGE_N);
-
- peakPos = peakFinder.detectPeak(data, windowStart, windowLen);
-
- _SaveDebugData("soundtouch-bpm-smoothed.txt", data, windowStart, windowLen, coeff);
- delete[] data;
- assert(decimateBy != 0);
- if (peakPos < 1e-9) return 0.0;
- _SaveDebugBeatPos("soundtouch-detected-beats.txt", beats);
-
- float bpm = (float)(coeff / peakPos);
- return (bpm >= MIN_BPM && bpm <= MAX_BPM_VALID) ? bpm : 0;
- }
- int BPMDetect::getBeats(float *pos, float *values, int max_num)
- {
- int num = (int)beats.size();
- if ((!pos) || (!values)) return num;
- for (int i = 0; (i < num) && (i < max_num); i++)
- {
- pos[i] = beats[i].pos;
- values[i] = beats[i].strength;
- }
- return num;
- }
|