[
  {
    "path": "README",
    "content": "BeatDetktor C++ and Javascript BPM detection library \n\nCopyright (c) 2009 Charles J. Cliffe.\n\nDistributed under the terms of the MIT License: \n\n    http://opensource.org/licenses/MIT\n\n"
  },
  {
    "path": "cpp/BeatDetektor.cpp",
    "content": "\n/*\n *  BeatDetektor.cpp\n *\n *  BeatDetektor - CubicFX Visualizer Beat Detection & Analysis Algorithm\n *\n * Copyright (c) 2009 Charles J. Cliffe.\n *\n * BeatDetektor is distributed under the terms of the MIT License.\n * http://opensource.org/licenses/MIT\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in\n * all copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING\n * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS\n * IN THE SOFTWARE.\n */\n\n\n\n\n\n#include \"BeatDetektor.h\"\n\nvoid BeatDetektor::process(float timer_seconds, std::vector<float> &fft_data)\n{\n\tif (!last_timer) { last_timer = timer_seconds; return; }\t// ignore 0 start time\n\t\n\tif (timer_seconds < last_timer) { reset(); return; }\n\t\n\tfloat timestamp = timer_seconds;\n\t\n\tlast_update = timer_seconds - last_timer;\n\tlast_timer = timer_seconds;\n\t\n\ttotal_time+=last_update;\n\t\n\tunsigned int range_step = (fft_data.size()/BD_DETECTION_RANGES);\n\tunsigned int range = 0;\n\tint i,x;\n\tfloat v;\n\t\n\tfloat bpm_floor = 60.0/BPM_MAX;\n\tfloat bpm_ceil = 60.0/BPM_MIN;\n\t\n\tif (current_bpm != current_bpm) current_bpm = 0;\n\t\n\tfor (x=0; x<fft_data.size(); x+=range_step)\n\t{\n\t\tif (!src)\n\t\t{\n\t\t\ta_freq_range[range] = 0;\n\t\t\t\n\t\t\t// accumulate frequency values for this range\n\t\t\tfor (i = x; i<x+range_step; i++)\n\t\t\t{\n\t\t\t\tv = fabs(fft_data[i]);\n\t\t\t\ta_freq_range[range] += v;\n\t\t\t}\n\t\t\t\n\t\t\t// average for range\n\t\t\ta_freq_range[range] /= range_step;\n\t\t\t\n\t\t\t// two sets of averages chase this one at a \n\t\t\t\n\t\t\t// moving average, increment closer to a_freq_range at a rate of 1.0 / detection_rate seconds\n\t\t\tma_freq_range[range] -= (ma_freq_range[range]-a_freq_range[range])*last_update*detection_rate;\n\t\t\t// moving average of moving average, increment closer to ma_freq_range at a rate of 1.0 / detection_rate seconds\n\t\t\tmaa_freq_range[range] -= (maa_freq_range[range]-ma_freq_range[range])*last_update*detection_rate;\n\t\t}\n\t\telse\n\t\t{\n\t\t\ta_freq_range[range] = src->a_freq_range[range];\n\t\t\tma_freq_range[range] = src->ma_freq_range[range];\n\t\t\tmaa_freq_range[range] = src->maa_freq_range[range];\n\t\t}\n\t\t\n\t\t\n\t\t// if closest moving average peaks above trailing (with a tolerance of BD_DETECTION_FACTOR) then trigger a detection for this range \n\t\tbool det = (ma_freq_range[range]*detection_factor >= maa_freq_range[range]);\n\t\t\n\t\t// compute bpm clamps for comparison to gap lengths\n\t\t\n\t\t// clamp detection averages to input ranges\n\t\tif (ma_bpm_range[range] > bpm_ceil) ma_bpm_range[range] = bpm_ceil;\n\t\tif (ma_bpm_range[range] < bpm_floor) ma_bpm_range[range] = bpm_floor;\n\t\tif (maa_bpm_range[range] > bpm_ceil) maa_bpm_range[range] = bpm_ceil;\n\t\tif (maa_bpm_range[range] < bpm_floor) maa_bpm_range[range] = bpm_floor;\n\t\t\n\t\tbool rewarded = false;\n\t\t\n\t\t// new detection since last, test it's quality\n\t\tif (!detection[range] && det)\n\t\t{\n\t\t\t// calculate length of gap (since start of last trigger)\n\t\t\tfloat trigger_gap = timestamp-last_detection[range];\n\t\t\t\n#define REWARD_VALS 7\n\t\t\tfloat reward_tolerances[REWARD_VALS] = { 0.001, 0.005, 0.01, 0.02, 0.04, 0.08, 0.10  };  \n\t\t\tfloat reward_multipliers[REWARD_VALS] = { 20.0, 10.0, 8.0, 1.0, 1.0/2.0, 1.0/4.0, 1.0/8.0 };\n\t\t\t\n\t\t\t// trigger falls within acceptable range, \n\t\t\tif (trigger_gap < bpm_ceil && trigger_gap > (bpm_floor))\n\t\t\t{\t\t\n\t\t\t\t// compute gap and award quality\n\t\t\t\t\n\t\t\t\tfor (i = 0; i < REWARD_VALS; i++)\n\t\t\t\t{\n\t\t\t\t\tif (fabs(ma_bpm_range[range]-trigger_gap) < ma_bpm_range[range]*reward_tolerances[i])\n\t\t\t\t\t{\n\t\t\t\t\t\tdetection_quality[range] += quality_reward * reward_multipliers[i]; \n\t\t\t\t\t\trewarded = true;\n#if DEVTEST_BUILD\n\t\t\t\t\t\t//\t\t\t\t\t\tprintf(\"1/1\\n\");\n\t\t\t\t\t\tcontribution_counter[1]++;\n#endif\n\t\t\t\t\t\t\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\t\n\t\t\t\t\n\t\t\t\tif (rewarded) \n\t\t\t\t{\n\t\t\t\t\tlast_detection[range] = timestamp;\n\t\t\t\t}\n\t\t\t}\n\t\t\telse if (trigger_gap >= bpm_ceil) // low quality, gap exceeds maximum time\n\t\t\t{\n\t\t\t\t// test for 2* beat\n\t\t\t\ttrigger_gap /= 2.0;\n\t\t\t\t// && fabs((60.0/trigger_gap)-(60.0/ma_bpm_range[range])) < 50.0\n\t\t\t\tif (trigger_gap < bpm_ceil && trigger_gap > (bpm_floor)) for (i = 0; i < REWARD_VALS; i++)\n\t\t\t\t{\n\t\t\t\t\tif (fabs(ma_bpm_range[range]-trigger_gap) < ma_bpm_range[range]*reward_tolerances[i])\n\t\t\t\t\t{\n\t\t\t\t\t\tdetection_quality[range] += quality_reward * reward_multipliers[i]; \n\t\t\t\t\t\trewarded = true;\n#if DEVTEST_BUILD\n\t\t\t\t\t\t//\t\t\t\t\t\tprintf(\"2/1\\n\");\n\t\t\t\t\t\tcontribution_counter[2]++;\n#endif\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\t\n\t\t\t\tif (!rewarded) trigger_gap *= 2.0;\n\t\t\t\t\n\t\t\t\t// start a new gap test, next gap is guaranteed to be longer\n\t\t\t\tlast_detection[range] = timestamp;\t\t\t\t\t\n\t\t\t}\n\t\t\t\n\t\t\t\n\t\t\tfloat qmp = (detection_quality[range]/quality_avg)*BD_QUALITY_STEP;\n\t\t\tif (qmp > 1.0)\n\t\t\t{\n\t\t\t\tqmp = 1.0;\n\t\t\t}\n\t\t\t\n\t\t\tif (rewarded)\n\t\t\t{\n\t\t\t\tma_bpm_range[range] -= (ma_bpm_range[range]-trigger_gap) * qmp;\n\t\t\t\tmaa_bpm_range[range] -= (maa_bpm_range[range]-ma_bpm_range[range]) * qmp;\n\t\t\t}\n\t\t\telse if (trigger_gap >= bpm_floor && trigger_gap <= bpm_ceil)\n\t\t\t{\n\t\t\t\tif (detection_quality[range] < quality_avg*BD_QUALITY_TOLERANCE && current_bpm)\n\t\t\t\t{\n\t\t\t\t\tma_bpm_range[range] -= (ma_bpm_range[range]-trigger_gap) * BD_QUALITY_STEP;\n\t\t\t\t\tmaa_bpm_range[range] -= (maa_bpm_range[range]-ma_bpm_range[range]) * BD_QUALITY_STEP;\n\t\t\t\t}\n\t\t\t\tdetection_quality[range] -= BD_QUALITY_STEP;\n\t\t\t}\n\t\t\telse if (trigger_gap >= bpm_ceil)\n\t\t\t{\n\t\t\t\tif (detection_quality[range] < quality_avg*BD_QUALITY_TOLERANCE && current_bpm)\n\t\t\t\t{\n\t\t\t\t\tma_bpm_range[range] -= (ma_bpm_range[range]-current_bpm) * 0.5;\n\t\t\t\t\tmaa_bpm_range[range] -= (maa_bpm_range[range]-ma_bpm_range[range]) * 0.5;\n\t\t\t\t}\n\t\t\t\tdetection_quality[range] -= quality_reward*BD_QUALITY_STEP;\n\t\t\t}\n\t\t\t\n\t\t}\n\t\t\n\t\tif ((!rewarded && timestamp-last_detection[range] > bpm_ceil) || ((det && fabs(ma_bpm_range[range]-current_bpm) > bpm_offset))) \n\t\t\tdetection_quality[range] -= detection_quality[range]*BD_QUALITY_STEP*quality_decay*last_update;\n\t\t\n\t\t// quality bottomed out, set to 0\n\t\tif (detection_quality[range] <= 0) detection_quality[range]=0.001;\n\t\t\n\t\t\n\t\tdetection[range] = det;\t\t\n\t\t\n\t\trange++;\n\t}\n\t\n\t\n\t// total contribution weight\n\tquality_total = 0;\n\t\n\t// total of bpm values\n\tfloat bpm_total = 0;\n\t// number of bpm ranges that contributed to this test\n\tint bpm_contributions = 0;\n\t\n\t\n\t// accumulate quality weight total\n\tfor (x=0; x<BD_DETECTION_RANGES; x++)\n\t{\n\t\tquality_total += detection_quality[x];\n\t}\n\t\n\t// determine the average weight of each quality range\n\tquality_avg = quality_total / (float)BD_DETECTION_RANGES;\n\t\n\t\n\tma_quality_avg += (quality_avg - ma_quality_avg) * last_update * detection_rate/2.0;\n\tmaa_quality_avg += (ma_quality_avg - maa_quality_avg) * last_update;\n\tma_quality_total += (quality_total - ma_quality_total) * last_update * detection_rate/2.0;\n\t\n\tma_quality_avg -= 0.98*ma_quality_avg*last_update*3.0;\n\t\n\tif (ma_quality_total <= 0) ma_quality_total = 1.0;\n\tif (ma_quality_avg <= 0) ma_quality_avg = 1.0;\n\t\n\tfloat avg_bpm_offset = 0.0;\n\tfloat offset_test_bpm = current_bpm;\n\tstd::map<int,float> draft;\n\tstd::map<int,float> fract_draft;\n\t\n\t{\n\t\tfor (x=0; x<BD_DETECTION_RANGES; x++)\n\t\t{\n\t\t\t// if this detection range weight*tolerance is higher than the average weight then add it's moving average contribution \n\t\t\tif (detection_quality[x]*BD_QUALITY_TOLERANCE >= ma_quality_avg)\n\t\t\t{\n\t\t\t\tif (maa_bpm_range[x] < bpm_ceil && maa_bpm_range[x] > bpm_floor)\n\t\t\t\t{\n\t\t\t\t\tbpm_total += maa_bpm_range[x];\n\t\t\t\t\t\n\t\t\t\t\tfloat draft_float = round((60.0/maa_bpm_range[x])*1000.0);\n\t\t\t\t\t\n\t\t\t\t\tdraft_float = (fabs(ceil(draft_float)-(60.0/current_bpm)*1000.0)<(fabs(floor(draft_float)-(60.0/current_bpm)*1000.0)))?ceil(draft_float/10.0):floor(draft_float/10.0);\n\t\t\t\t\t\n\t\t\t\t\tfloat draft_int = (int)(draft_float/10.0);\n\t\t\t\t\t\n\t\t\t\t\tdraft[draft_int]+= (detection_quality[x]/quality_avg);\n\t\t\t\t\tbpm_contributions++;\n\t\t\t\t\tif (offset_test_bpm == 0.0) offset_test_bpm = maa_bpm_range[x];\n\t\t\t\t\telse \n\t\t\t\t\t{\n\t\t\t\t\t\tavg_bpm_offset += fabs(offset_test_bpm-maa_bpm_range[x]);\n\t\t\t\t\t}\n\t\t\t\t\t\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\t\n\t// if we have one or more contributions that pass criteria then attempt to display a guess\n\tbool has_prediction = (bpm_contributions>=minimum_contributions)?true:false;\n\t\n\t\n\tstd::map<int,float>::iterator draft_i;\n\t\n\tif (has_prediction) \n\t{\n\t\t\n\t\tint draft_winner=0;\n\t\tint win_max = 0;\n\t\t\n\t\tfor (draft_i = draft.begin(); draft_i != draft.end(); draft_i++)\n\t\t{\n\t\t\tif ((*draft_i).second > win_max)\n\t\t\t{\n\t\t\t\twin_max = (*draft_i).second;\n\t\t\t\tdraft_winner = (*draft_i).first;\n\t\t\t}\n\t\t}\n\t\t\n\t\tbpm_predict = (60.0/(float)(draft_winner/10.0));\n\t\t\n\t\tavg_bpm_offset /= (float)bpm_contributions;\n\t\tbpm_offset = avg_bpm_offset;\n\t\t\n\t\tif (!current_bpm)  \n\t\t{\n\t\t\tcurrent_bpm = bpm_predict; \n\t\t}\n\t\t\n\t\t\n\t\tif (current_bpm && bpm_predict) current_bpm -= (current_bpm-bpm_predict)*last_update; //*avg_bpm_offset*200.0;\t\n\t\tif (current_bpm != current_bpm || current_bpm < 0) current_bpm = 0;\n\t\t\n\t\t\n\t\t// hold a contest for bpm to find the current mode\n\t\tstd::map<int,float>::iterator contest_i;\n\t\t\n\t\tfloat contest_max=0;\n\t\t\n\t\tfor (contest_i = bpm_contest.begin(); contest_i != bpm_contest.end(); contest_i++)\n\t\t{\n\t\t\tif (contest_max < (*contest_i).second) contest_max =(*contest_i).second; \n\t\t\tif (((*contest_i).second) > BD_FINISH_LINE/2.0)\n\t\t\t{\n\t\t\t\tbpm_contest_lo[round((float)((*contest_i).first)/10.0)]+= (((*contest_i).second)/10.0)*last_update;\n\t\t\t}\n\t\t}\n\t\t\n\t\t\n\t\t// normalize to a finish line of BD_FINISH_LINE\n\t\tif (contest_max > finish_line) \n\t\t{\n\t\t\tfor (contest_i = bpm_contest.begin(); contest_i != bpm_contest.end(); contest_i++)\n\t\t\t{\n\t\t\t\t(*contest_i).second=((*contest_i).second/contest_max)*finish_line;\n\t\t\t}\n\t\t}\n\t\t\n\t\tcontest_max = 0;\n\t\t\n\t\tfor (contest_i = bpm_contest_lo.begin(); contest_i != bpm_contest_lo.end(); contest_i++)\n\t\t{\n\t\t\tif (contest_max < (*contest_i).second) contest_max =(*contest_i).second; \n\t\t}\n\t\t\n\t\tif (contest_max > finish_line) \n\t\t{\n\t\t\tfor (contest_i = bpm_contest_lo.begin(); contest_i != bpm_contest_lo.end(); contest_i++)\n\t\t\t{\n\t\t\t\t(*contest_i).second=((*contest_i).second/contest_max)*finish_line;\n\t\t\t}\n\t\t}\n\t\t\n\t\t\n\t\t// decay contest values from last loop\n\t\tfor (contest_i = bpm_contest.begin(); contest_i != bpm_contest.end(); contest_i++)\n\t\t{\n\t\t\t(*contest_i).second-=(*contest_i).second*(last_update/detection_rate);\n\t\t}\n\t\t\n\t\t// decay contest values from last loop\n\t\tfor (contest_i = bpm_contest_lo.begin(); contest_i != bpm_contest_lo.end(); contest_i++)\n\t\t{\n\t\t\t(*contest_i).second-=(*contest_i).second*(last_update/detection_rate);\n\t\t}\n\t\t\n\t\t\n\t\tbpm_timer+=last_update;\n\t\t\n\t\tint winner = 0;\n\t\tint winner_lo = 0;\t\t\t\t\n\t\t\n\t\t// attempt to display the beat at the beat interval ;)\n\t\tif (bpm_timer > winning_bpm/4.0 && current_bpm)\n\t\t{\t\t\n\t\t\tif (winning_bpm) while (bpm_timer > winning_bpm/4.0) bpm_timer -= winning_bpm/4.0;\n\t\t\t\n\t\t\t// increment beat counter\n\t\t\t\n\t\t\tquarter_counter++;\t\t\n\t\t\thalf_counter= quarter_counter/2;\n\t\t\tbeat_counter = quarter_counter/4;\n\t\t\t\n\t\t\t// award the winner of this iteration\n\t\t\tbpm_contest[(int)round((60.0/current_bpm)*10.0)]+=quality_reward;\n\t\t\t\n\t\t\twin_val = 0;\n\t\t\t\n\t\t\t// find the overall winner so far\n\t\t\tfor (contest_i = bpm_contest.begin(); contest_i != bpm_contest.end(); contest_i++)\n\t\t\t{\n\t\t\t\tif (win_val < (*contest_i).second)\n\t\t\t\t{\n\t\t\t\t\twinner = (*contest_i).first;\n\t\t\t\t\twin_val = (*contest_i).second;\n\t\t\t\t}\n\t\t\t}\n\t\t\t\n\t\t\tif (winner)\n\t\t\t{\n\t\t\t\twin_bpm_int = winner;\n\t\t\t\twinning_bpm = 60.0/(float)(winner/10.0);\n\t\t\t}\n\t\t\t\n\t\t\t\n\t\t\twin_val_lo = 0;\t\t\n\t\t\t\n\t\t\t// find the overall winner so far\n\t\t\tfor (contest_i = bpm_contest_lo.begin(); contest_i != bpm_contest_lo.end(); contest_i++)\n\t\t\t{\n\t\t\t\tif (win_val_lo < (*contest_i).second)\n\t\t\t\t{\n\t\t\t\t\twinner_lo = (*contest_i).first;\n\t\t\t\t\twin_val_lo = (*contest_i).second;\n\t\t\t\t}\n\t\t\t}\n\t\t\t\n\t\t\tif (winner_lo)\n\t\t\t{\n\t\t\t\twin_bpm_int_lo = winner_lo;\n\t\t\t\twinning_bpm_lo = 60.0/(float)(winner_lo);\n\t\t\t}\n#if DEVTEST_BUILD\n\t\t\tif (debugmode && ((quarter_counter % 4) == 0)) \n\t\t\t{\n\t\t\t\tprintf(\"[%0.0f-%0.0f] quality: %0.2f / %0.2f percent\",BPM_MIN,BPM_MAX,quality_total,(quality_total/(ma_quality_avg*(float)BD_DETECTION_RANGES))*50.0);\n\t\t\t\tprintf(\", current bpm estimate: %d @ %0.2f / %0.5f\",winner,win_val,bpm_offset);\n\t\t\t\tprintf(\"  low res estimate: %d @ %0.2f\\n\",winner_lo,win_val_lo);\n\t\t\t\tstd::map<int, int>::iterator contrib_i;\n\t\t\t\t\n\t\t\t\tprintf(\"contrib: \");\n\t\t\t\tfor (contrib_i = contribution_counter.begin(); contrib_i !=  contribution_counter.end(); contrib_i++)\n\t\t\t\t{\n\t\t\t\t\tprintf(\"%d: %d \\t\",(*contrib_i).first,(*contrib_i).second);\n\t\t\t\t}\n\t\t\t\tprintf(\"\\n\");\n\t\t\t}\n#endif\n\t\t}\n\t}\n}\n"
  },
  {
    "path": "cpp/BeatDetektor.h",
    "content": "#pragma once\n\n/*\n * BeatDetektor.h\n *\n * BeatDetektor - CubicFX Visualizer Beat Detection & Analysis Algorithm\n *\n * Copyright (c) 2009 Charles J. Cliffe.\n *\n * BeatDetektor is distributed under the terms of the MIT License.\n * http://opensource.org/licenses/MIT\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in\n * all copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING\n * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS\n * IN THE SOFTWARE.\n */\n\n\n\n\n/* \n BeatDetektor class\n\n Theory:\n\n Trigger detection is performed using a trail of moving averages, \n \n The FFT input is broken up into 128 ranges and averaged, each range has two moving \n averages that tail each other at a rate of (1.0 / BD_DETECTION_RATE) seconds.  \n\n Each time the moving average for a range exceeds it's own tailing average by:\n\n (moving_average[range] * BD_DETECTION_FACTOR >= moving_average[range])\n\n if this is true there's a rising edge and a detection is flagged for that range. \n Next a trigger gap test is performed between rising edges and timestamp recorded. \n\n If the gap is larger than our BPM window (in seconds) then we can discard it and\n reset the timestamp for a new detection -- but only after checking to see if it's a \n reasonable match for 2* the current detection in case it's only triggered every\n other beat. Gaps that are lower than the BPM window are ignored and the last \n timestamp will not be reset.  \n\n Gaps that are within a reasonable window are run through a quality stage to determine \n how 'close' they are to that channel's current prediction and are incremented or \n decremented by a weighted value depending on accuracy. Repeated hits of low accuracy \n will still move a value towards erroneous detection but it's quality will be lowered \n and will not be eligible for the gap time quality draft.\n \n Once quality has been assigned ranges are reviewed for good match candidates and if \n BD_MINIMUM_CONTRIBUTIONS or more ranges achieve a decent ratio (with a factor of \n BD_QUALITY_TOLERANCE) of contribution to the overall quality we take them into the \n contest round.  Note that the contest round  won't run on a given process() call if \n the total quality achieved does not meet or exceed BD_QUALITY_TOLERANCE.\n  \n Each time through if a select draft of BPM ranges has achieved a reasonable quality \n above others it's awarded a value in the BPM contest.  The BPM contest is a hash \n array indexed by an integer BPM value, each draft winner is awarded BD_QUALITY_REWARD.\n\n Finally the BPM contest is examined to determine a leader and all contest entries \n are normalized to a total value of BD_FINISH_LINE, whichever range is closest to \n BD_FINISH_LINE at any given point is considered to be the best guess however waiting \n until a minimum contest winning value of about 20.0-25.0 will provide more accurate \n results.  Note that the 20-25 rule may vary with lower and higher input ranges. \n A winning value that exceeds 40 or hovers around 60 (the finish line) is pretty much\n a guaranteed match.\n\n\n Configuration Kernel Notes:\n\n The majority of the ratios and values have been reverse-engineered from my own  \n observation and visualization of information from various aspects of the detection \n triggers; so not all parameters have a perfect definition nor perhaps the best value yet.\n However despite this it performs very well; I had expected several more layers \n before a reasonable detection would be achieved. Comments for these parameters will be \n updated as analysis of their direct effect is explored.\n\n\n Input Restrictions:\n\n bpm_maximum must be within the range of (bpm_minimum*2)-1\n i.e. minimum of 50 must have a maximum of 99 because 50*2 = 100\n\n*/\n\n\n#include <map>\n#include <vector>\n#include <math.h>\n\n#define BD_DETECTION_RANGES 128\n#define BD_DETECTION_RATE 12.0\n#define BD_DETECTION_FACTOR 0.925\n\n#define BD_QUALITY_TOLERANCE 0.96\n#define BD_QUALITY_DECAY 0.95\n#define BD_QUALITY_REWARD 7.0\n#define BD_QUALITY_STEP 0.1\n#define BD_FINISH_LINE 60.0\n#define BD_MINIMUM_CONTRIBUTIONS 6\n\nclass BeatDetektor\n{\npublic:\n\tfloat BPM_MIN;\n\tfloat BPM_MAX;\n\t\n\tBeatDetektor *src;\n\t\n\tfloat current_bpm; \n\tfloat winning_bpm; \n\tfloat winning_bpm_lo; \n\tfloat win_val;\n\tint win_bpm_int;\n\tfloat win_val_lo;\n\tint win_bpm_int_lo;\n\t\n\tfloat bpm_predict;\n\t\n\tbool is_erratic;\n\tfloat bpm_offset;\n\tfloat last_timer;\n\tfloat last_update;\n\tfloat total_time;\n\t\n\tfloat bpm_timer;\n\tint beat_counter;\n\tint half_counter;\n\tint quarter_counter;\n\tfloat detection_factor;\n\t//\tfloat quality_minimum,\n\tfloat quality_reward,quality_decay,detection_rate,finish_line;\n\tint minimum_contributions;\n\tfloat quality_total, quality_avg, ma_quality_lo, ma_quality_total, ma_quality_avg, maa_quality_avg;\n\t\n\t// current average (this sample) for range n\n\tfloat a_freq_range[BD_DETECTION_RANGES];\n\t// moving average of frequency range n\n\tfloat ma_freq_range[BD_DETECTION_RANGES];\n\t// moving average of moving average of frequency range n\n\tfloat maa_freq_range[BD_DETECTION_RANGES];\n\t// timestamp of last detection for frequecy range n\n\tfloat last_detection[BD_DETECTION_RANGES];\n\t\n\t// moving average of gap lengths\n\tfloat ma_bpm_range[BD_DETECTION_RANGES];\n\t// moving average of moving average of gap lengths\n\tfloat maa_bpm_range[BD_DETECTION_RANGES];\n\t\n\t// range n quality attribute, good match = quality+, bad match = quality-, min = 0\n\tfloat detection_quality[BD_DETECTION_RANGES];\n\t\n\t// current trigger state for range n\n\tbool detection[BD_DETECTION_RANGES]; \n\t\n\tstd::map<int,float> bpm_contest;\t// 1/10th\n\tstd::map<int,float> bpm_contest_lo; // 1/1\n\t\n#if DEVTEST_BUILD\n\tbool debugmode;\n\tstd::map<int,int> contribution_counter;\n#endif\t\n\t\n\tBeatDetektor(float BPM_MIN_in=100.0,float BPM_MAX_in=200.0, BeatDetektor *link_src = NULL) :\n\t\n\tcurrent_bpm(0.0),\n\twinning_bpm(0.0), \n\twin_val(0.0),\n\twin_bpm_int(0),\n\twin_val_lo(0.0),\n\twin_bpm_int_lo(0),\n\t\n\tbpm_predict(0),\n\t\n\tis_erratic(false),\n\tbpm_offset(0.0),\n\tlast_timer(0.0),\n\tlast_update(0.0),\n\ttotal_time(0.0),\n\t\n\tbpm_timer(0.0),\n\tbeat_counter(0),\n\thalf_counter(0),\n\tquarter_counter(0),\n\tsrc(link_src),\n\t\n\t//\tquality_minimum(BD_QUALITY_MINIMUM),\n\tquality_reward(BD_QUALITY_REWARD),\n\tdetection_rate(BD_DETECTION_RATE),\n\tfinish_line(BD_FINISH_LINE),\n\tminimum_contributions(BD_MINIMUM_CONTRIBUTIONS),\n\tdetection_factor(BD_DETECTION_FACTOR),\n\tquality_total(1.0),\n\tquality_avg(1.0),\n\tquality_decay(BD_QUALITY_DECAY),\n\tma_quality_avg(0.001),\n\tma_quality_lo(1.0),\n\tma_quality_total(1.0)\n#if DEVTEST_BUILD\n\t,debugmode(false)\n#endif\t\n\t\n\t{\n\t\tBPM_MIN = BPM_MIN_in;\n\t\tBPM_MAX = BPM_MAX_in;\n\t\treset();\n\t}\n\t\n\tvoid reset(bool reset_freq=true)\n\t{\n\t\tfor (int i = 0; i < BD_DETECTION_RANGES; i++) \n\t\t{\n\t\t\t//\t\t\tma_bpm_range[i] = maa_bpm_range[i] = 60.0/(float)(BPM_MIN + (1.0+sin(8.0*M_PI*((float)i/(float)BD_DETECTION_RANGES))/2.0)*((BPM_MAX-BPM_MIN)/2));\t\t\t\n\t\t\tma_bpm_range[i] = maa_bpm_range[i] = 60.0/(float)(BPM_MIN+5)+ ((60.0/(float)(BPM_MAX-5)-60.0/(float)(BPM_MIN+5)) * ((float)i/(float)BD_DETECTION_RANGES));\n\t\t\tif (reset_freq) \n\t\t\t{\n\t\t\t\ta_freq_range[i] = ma_freq_range[i] = maa_freq_range[i] = 0;\n\t\t\t}\n\t\t\tlast_detection[i] = 0;\n\t\t\tdetection_quality[i] = 0;\n\t\t\tdetection[i] = false;\n\t\t\t\n\t\t}\n\t\t\n\t\ttotal_time = 0;\n\t\tmaa_quality_avg = 500.0;\n\t\tbpm_offset = bpm_timer = last_update = last_timer = winning_bpm = current_bpm = win_val = win_bpm_int = 0;\n\t\tbpm_contest.clear();\n\t\tbpm_contest_lo.clear();\n#if DEVTEST_BUILD\n\t\tcontribution_counter.clear();\n#endif\n\t}\n\t\n\tvoid process(float timer_seconds, std::vector<float> &fft_data);\n};\n"
  },
  {
    "path": "js/beatdetektor.js",
    "content": "/*\n * BeatDetektor.js\n *\n * BeatDetektor - CubicFX Visualizer Beat Detection & Analysis Algorithm\n * Javascript port by Charles J. Cliffe and Corban Brook\n *  \n * Copyright (c) 2009 Charles J. Cliffe.\n *\n * BeatDetektor is distributed under the terms of the MIT License.\n * http://opensource.org/licenses/MIT\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in\n * all copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING\n * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS\n * IN THE SOFTWARE.\n */\n\n\n\n/* \n BeatDetektor class\n\n\n Theory:\n\n Trigger detection is performed using a trail of moving averages, \n \n The FFT input is broken up into 128 ranges and averaged, each range has two moving \n averages that tail each other at a rate of (1.0 / BD_DETECTION_RATE) seconds.  \n\n Each time the moving average for a range exceeds it's own tailing average by:\n\n (moving_average[range] * BD_DETECTION_FACTOR >= moving_average[range])\n\n if this is true there's a rising edge and a detection is flagged for that range. \n Next a trigger gap test is performed between rising edges and timestamp recorded. \n\n If the gap is larger than our BPM window (in seconds) then we can discard it and\n reset the timestamp for a new detection -- but only after checking to see if it's a \n reasonable match for 2* the current detection in case it's only triggered every\n other beat. Gaps that are lower than the BPM window are ignored and the last \n timestamp will not be reset.  \n\n Gaps that are within a reasonable window are run through a quality stage to determine \n how 'close' they are to that channel's current prediction and are incremented or \n decremented by a weighted value depending on accuracy. Repeated hits of low accuracy \n will still move a value towards erroneous detection but it's quality will be lowered \n and will not be eligible for the gap time quality draft.\n \n Once quality has been assigned ranges are reviewed for good match candidates and if \n BD_MINIMUM_CONTRIBUTIONS or more ranges achieve a decent ratio (with a factor of \n BD_QUALITY_TOLERANCE) of contribution to the overall quality we take them into the \n contest round.  Note that the contest round  won't run on a given process() call if \n the total quality achieved does not meet or exceed BD_QUALITY_TOLERANCE.\n  \n Each time through if a select draft of BPM ranges has achieved a reasonable quality \n above others it's awarded a value in the BPM contest.  The BPM contest is a hash \n array indexed by an integer BPM value, each draft winner is awarded BD_QUALITY_REWARD.\n\n Finally the BPM contest is examined to determine a leader and all contest entries \n are normalized to a total value of BD_FINISH_LINE, whichever range is closest to \n BD_FINISH_LINE at any given point is considered to be the best guess however waiting \n until a minimum contest winning value of about 20.0-25.0 will provide more accurate \n results.  Note that the 20-25 rule may vary with lower and higher input ranges. \n A winning value that exceeds 40 or hovers around 60 (the finish line) is pretty much\n a guaranteed match.\n\n\n Configuration Kernel Notes:\n\n The majority of the ratios and values have been reverse-engineered from my own  \n observation and visualization of information from various aspects of the detection \n triggers; so not all parameters have a perfect definition nor perhaps the best value yet.\n However despite this it performs very well; I had expected several more layers \n before a reasonable detection would be achieved. Comments for these parameters will be \n updated as analysis of their direct effect is explored.\n\n\n Input Restrictions:\n\n bpm_maximum must be within the range of (bpm_minimum*2)-1\n i.e. minimum of 50 must have a maximum of 99 because 50*2 = 100\n\n\n Changelog: \n \n 01/17/2010 - Charles J. Cliffe \n  - Tested and tweaked default kernel values for tighter detection\n  - Added BeatDetektor.config_48_95, BeatDetektor.config_90_179 and BeatDetektor.config_150_280 for more refined detection ranges\n  - Updated unit test to include new range config example\n\n02/21/2010 - Charles J. Cliffe \n - Fixed numerous bugs and divide by 0 on 1% match causing poor accuracy\n - Re-worked the quality calulations, accuracy improved 8-10x\n - Primary value is now a fractional reading (*10, just divide by 10), added win_bpm_int_lo for integral readings\n - Added feedback loop for current_bpm to help back-up low quality channels\n - Unified range configs, now single default should be fine\n - Extended quality reward 'funnel'\n\n*/\nBeatDetektor = function(bpm_minimum, bpm_maximum, alt_config)\n{\n\tif (typeof(bpm_minimum)=='undefined') bpm_minimum = 85.0;\n\tif (typeof(bpm_maximum)=='undefined') bpm_maximum = 169.0\n\t\n\tthis.config = (typeof(alt_config)!='undefined')?alt_config:BeatDetektor.config;\n\t\n\tthis.BPM_MIN = bpm_minimum;\n\tthis.BPM_MAX = bpm_maximum;\n\n\tthis.beat_counter = 0;\n\tthis.half_counter = 0;\n\tthis.quarter_counter = 0;\n\n\t// current average (this sample) for range n\n\tthis.a_freq_range = new Array(this.config.BD_DETECTION_RANGES);\n\t// moving average of frequency range n\n\tthis.ma_freq_range = new Array(this.config.BD_DETECTION_RANGES);\n\t// moving average of moving average of frequency range n\n\tthis.maa_freq_range = new Array(this.config.BD_DETECTION_RANGES);\n\t// timestamp of last detection for frequecy range n\n\tthis.last_detection = new Array(this.config.BD_DETECTION_RANGES);\n\n\t// moving average of gap lengths\n\tthis.ma_bpm_range = new Array(this.config.BD_DETECTION_RANGES);\n\t// moving average of moving average of gap lengths\n\tthis.maa_bpm_range = new Array(this.config.BD_DETECTION_RANGES);\n\n\t// range n quality attribute, good match  = quality+, bad match  = quality-, min  = 0\n\tthis.detection_quality = new Array(this.config.BD_DETECTION_RANGES);\n\n\t// current trigger state for range n\n\tthis.detection = new Array(this.config.BD_DETECTION_RANGES); \n\t\n\tthis.reset();\n\t\n\tif (typeof(console)!='undefined')\n\t{\n\t\tconsole.log(\"BeatDetektor(\"+this.BPM_MIN+\",\"+this.BPM_MAX+\") created.\")\n\t}\n}\n\nBeatDetektor.prototype.reset = function()\n{\n//\tvar bpm_avg = 60.0/((this.BPM_MIN+this.BPM_MAX)/2.0);\n\n\tfor (var i = 0; i < this.config.BD_DETECTION_RANGES; i++)\n\t{\n\t\tthis.a_freq_range[i] = 0.0;\n\t\tthis.ma_freq_range[i] = 0.0;\n\t\tthis.maa_freq_range[i] = 0.0;\n\t\tthis.last_detection[i] = 0.0;\n\t\t\n\t\tthis.ma_bpm_range[i] = \n\t\tthis.maa_bpm_range[i] = 60.0/this.BPM_MIN + ((60.0/this.BPM_MAX-60.0/this.BPM_MIN) * (i/this.config.BD_DETECTION_RANGES));\t\t\n\t\t\n\t\tthis.detection_quality[i] = 0.0;\n\t\tthis.detection[i] = false;\n\t}\n\t\n\tthis.ma_quality_avg = 0;\n\tthis.ma_quality_total = 0;\n\t\n\tthis.bpm_contest = new Array();\n\tthis.bpm_contest_lo = new Array();\n\t\n\tthis.quality_total = 0.0;\n\tthis.quality_avg = 0.0;\n\n\tthis.current_bpm = 0.0; \n\tthis.current_bpm_lo = 0.0; \n\n\tthis.winning_bpm = 0.0; \n\tthis.win_val = 0.0;\n\tthis.winning_bpm_lo = 0.0; \n\tthis.win_val_lo = 0.0;\n\n\tthis.win_bpm_int = 0;\n\tthis.win_bpm_int_lo = 0;\n\n\tthis.bpm_predict = 0;\n\n\tthis.is_erratic = false;\n\tthis.bpm_offset = 0.0;\n\tthis.last_timer = 0.0;\n\tthis.last_update = 0.0;\n\n\tthis.bpm_timer = 0.0;\n\tthis.beat_counter = 0;\n\tthis.half_counter = 0;\n\tthis.quarter_counter = 0;\n}\n\n\nBeatDetektor.config_default = {\n\tBD_DETECTION_RANGES : 128,  // How many ranges to quantize the FFT into\n\tBD_DETECTION_RATE : 12.0,   // Rate in 1.0 / BD_DETECTION_RATE seconds\n\tBD_DETECTION_FACTOR : 0.915, // Trigger ratio\n\tBD_QUALITY_DECAY : 0.6,     // range and contest decay\n\tBD_QUALITY_TOLERANCE : 0.96,// Use the top x % of contest results\n\tBD_QUALITY_REWARD : 10.0,    // Award weight\n\tBD_QUALITY_STEP : 0.1,     // Award step (roaming speed)\n\tBD_MINIMUM_CONTRIBUTIONS : 6,   // At least x ranges must agree to process a result\n\tBD_FINISH_LINE : 60.0,          // Contest values wil be normalized to this finish line\n\t// this is the 'funnel' that pulls ranges in / out of alignment based on trigger detection\n\tBD_REWARD_TOLERANCES : [ 0.001, 0.005, 0.01, 0.02, 0.04, 0.08, 0.10, 0.15, 0.30 ],  // .1%, .5%, 1%, 2%, 4%, 8%, 10%, 15%\n\tBD_REWARD_MULTIPLIERS : [ 20.0, 10.0, 8.0, 1.0, 1.0/2.0, 1.0/4.0, 1.0/8.0, 1/16.0, 1/32.0 ]\n};\n\n\n// Default configuration kernel\nBeatDetektor.config = BeatDetektor.config_default;\n\n\nBeatDetektor.prototype.process = function(timer_seconds, fft_data)\n{\n\tif (!this.last_timer) { this.last_timer = timer_seconds; return; }\t// ignore 0 start time\n\tif (this.last_timer > timer_seconds) { this.reset(); return; }\n\t\n\tvar timestamp = timer_seconds;\n\t\n\tthis.last_update = timer_seconds - this.last_timer;\n\tthis.last_timer = timer_seconds;\n\n\tif (this.last_update > 1.0) { this.reset(); return; }\n\n\tvar i,x;\n\tvar v;\n\t\n\tvar bpm_floor = 60.0/this.BPM_MAX;\n\tvar bpm_ceil = 60.0/this.BPM_MIN;\n\t\n\tvar range_step = (fft_data.length / this.config.BD_DETECTION_RANGES);\n\tvar range = 0;\n\t\n\t\t\n\tfor (x=0; x<fft_data.length; x+=range_step)\n\t{\n\t\tthis.a_freq_range[range] = 0;\n\t\t\n\t\t// accumulate frequency values for this range\n\t\tfor (i = x; i<x+range_step; i++)\n\t\t{\n\t\t\tv = Math.abs(fft_data[i]);\n\t\t\tthis.a_freq_range[range] += v;\n\t\t}\n\t\t\n\t\t// average for range\n\t\tthis.a_freq_range[range] /= range_step;\n\t\t\n\t\t// two sets of averages chase this one at a \n\t\t\n\t\t// moving average, increment closer to a_freq_range at a rate of 1.0 / BD_DETECTION_RATE seconds\n\t\tthis.ma_freq_range[range] -= (this.ma_freq_range[range]-this.a_freq_range[range])*this.last_update*this.config.BD_DETECTION_RATE;\n\t\t// moving average of moving average, increment closer to this.ma_freq_range at a rate of 1.0 / BD_DETECTION_RATE seconds\n\t\tthis.maa_freq_range[range] -= (this.maa_freq_range[range]-this.ma_freq_range[range])*this.last_update*this.config.BD_DETECTION_RATE;\n\t\t\n\t\t// if closest moving average peaks above trailing (with a tolerance of BD_DETECTION_FACTOR) then trigger a detection for this range \n\t\tvar det = (this.ma_freq_range[range]*this.config.BD_DETECTION_FACTOR >= this.maa_freq_range[range]);\n\t\t\n\t\t// compute bpm clamps for comparison to gap lengths\n\t\t\n\t\t// clamp detection averages to input ranges\n\t\tif (this.ma_bpm_range[range] > bpm_ceil) this.ma_bpm_range[range] = bpm_ceil;\n\t\tif (this.ma_bpm_range[range] < bpm_floor) this.ma_bpm_range[range] = bpm_floor;\n\t\tif (this.maa_bpm_range[range] > bpm_ceil) this.maa_bpm_range[range] = bpm_ceil;\n\t\tif (this.maa_bpm_range[range] < bpm_floor) this.maa_bpm_range[range] = bpm_floor;\n\t\t\t\n\t\tvar rewarded = false;\n\t\t\n\t\t// new detection since last, test it's quality\n\t\tif (!this.detection[range] && det)\n\t\t{\n\t\t\t// calculate length of gap (since start of last trigger)\n\t\t\tvar trigger_gap = timestamp-this.last_detection[range];\n\t\t\t\n\t\t\t// trigger falls within acceptable range, \n\t\t\tif (trigger_gap < bpm_ceil && trigger_gap > (bpm_floor))\n\t\t\t{\t\t\n\t\t\t\t// compute gap and award quality\n\t\t\t\t\n\t\t\t\t// use our tolerances as a funnel to edge detection towards the most likely value\n\t\t\t\tfor (i = 0; i < this.config.BD_REWARD_TOLERANCES.length; i++)\n\t\t\t\t{\n\t\t\t\t\tif (Math.abs(this.ma_bpm_range[range]-trigger_gap) < this.ma_bpm_range[range]*this.config.BD_REWARD_TOLERANCES[i])\n\t\t\t\t\t{\n\t\t\t\t\t\tthis.detection_quality[range] += this.config.BD_QUALITY_REWARD * this.config.BD_REWARD_MULTIPLIERS[i]; \n\t\t\t\t\t\trewarded = true;\n\t\t\t\t\t}\n\t\t\t\t}\t\t\t\t\n\t\t\t\t\n\t\t\t\tif (rewarded) \n\t\t\t\t{\n\t\t\t\t\tthis.last_detection[range] = timestamp;\n\t\t\t\t}\n\t\t\t}\n\t\t\telse if (trigger_gap >= bpm_ceil) // low quality, gap exceeds maximum time\n\t\t\t{\n\t\t\t\t// start a new gap test, next gap is guaranteed to be longer\n\t\t\t\t\n\t\t\t\t// test for 1/2 beat\n\t\t\t\ttrigger_gap /= 2.0;\n\n\t\t\t\tif (trigger_gap < bpm_ceil && trigger_gap > (bpm_floor)) for (i = 0; i < this.config.BD_REWARD_TOLERANCES.length; i++)\n\t\t\t\t{\n\t\t\t\t\tif (Math.abs(this.ma_bpm_range[range]-trigger_gap) < this.ma_bpm_range[range]*this.config.BD_REWARD_TOLERANCES[i])\n\t\t\t\t\t{\n\t\t\t\t\t\tthis.detection_quality[range] += this.config.BD_QUALITY_REWARD * this.config.BD_REWARD_MULTIPLIERS[i]; \n\t\t\t\t\t\trewarded = true;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\t\n\t\t\t\t\n\t\t\t\t// decrement quality if no 1/2 beat reward\n\t\t\t\tif (!rewarded) \n\t\t\t\t{\n\t\t\t\t\ttrigger_gap *= 2.0;\n\t\t\t\t}\n\t\t\t\tthis.last_detection[range] = timestamp;\t\n\t\t\t}\n\t\t\t\n\t\t\tif (rewarded)\n\t\t\t{\n\t\t\t\tvar qmp = (this.detection_quality[range]/this.quality_avg)*this.config.BD_QUALITY_STEP;\n\t\t\t\tif (qmp > 1.0)\n\t\t\t\t{\n\t\t\t\t\tqmp = 1.0;\n\t\t\t\t}\n\n\t\t\t\tthis.ma_bpm_range[range] -= (this.ma_bpm_range[range]-trigger_gap) * qmp;\t\t\t\t\n\t\t\t\tthis.maa_bpm_range[range] -= (this.maa_bpm_range[range]-this.ma_bpm_range[range]) * qmp;\n\t\t\t}\n\t\t\telse if (trigger_gap >= bpm_floor && trigger_gap <= bpm_ceil)\n\t\t\t{\n\t\t\t\tif (this.detection_quality[range] < this.quality_avg*this.config.BD_QUALITY_TOLERANCE && this.current_bpm)\n\t\t\t\t{\n\t\t\t\t\tthis.ma_bpm_range[range] -= (this.ma_bpm_range[range]-trigger_gap) * this.config.BD_QUALITY_STEP;\n\t\t\t\t\tthis.maa_bpm_range[range] -= (this.maa_bpm_range[range]-this.ma_bpm_range[range]) * this.config.BD_QUALITY_STEP;\n\t\t\t\t}\n\t\t\t\tthis.detection_quality[range] -= this.config.BD_QUALITY_STEP;\n\t\t\t}\n\t\t\telse if (trigger_gap >= bpm_ceil)\n\t\t\t{\n\t\t\t\tif ((this.detection_quality[range] < this.quality_avg*this.config.BD_QUALITY_TOLERANCE) && this.current_bpm)\n\t\t\t\t{\n\t\t\t\t\tthis.ma_bpm_range[range] -= (this.ma_bpm_range[range]-this.current_bpm) * 0.5;\n\t\t\t\t\tthis.maa_bpm_range[range] -= (this.maa_bpm_range[range]-this.ma_bpm_range[range]) * 0.5 ;\n\t\t\t\t}\n\t\t\t\tthis.detection_quality[range]-= this.config.BD_QUALITY_STEP;\n\t\t\t}\n\t\t\t\n\t\t}\n\t\t\t\t\n\t\tif ((!rewarded && timestamp-this.last_detection[range] > bpm_ceil) || (det && Math.abs(this.ma_bpm_range[range]-this.current_bpm) > this.bpm_offset)) \n\t\t\tthis.detection_quality[range] -= this.detection_quality[range]*this.config.BD_QUALITY_STEP*this.config.BD_QUALITY_DECAY*this.last_update;\n\t\t\n\t\t// quality bottomed out, set to 0\n\t\tif (this.detection_quality[range] < 0.001) this.detection_quality[range]=0.001;\n\t\t\t\t\n\t\tthis.detection[range] = det;\t\t\n\t\t\n\t\trange++;\n\t}\n\t\t\n\t// total contribution weight\n\tthis.quality_total = 0;\n\t\n\t// total of bpm values\n\tvar bpm_total = 0;\n\t// number of bpm ranges that contributed to this test\n\tvar bpm_contributions = 0;\n\t\n\t\n\t// accumulate quality weight total\n\tfor (var x=0; x<this.config.BD_DETECTION_RANGES; x++)\n\t{\n\t\tthis.quality_total += this.detection_quality[x];\n\t}\n\t\n\t\n\tthis.quality_avg = this.quality_total / this.config.BD_DETECTION_RANGES;\n\t\n\t\n\tif (this.quality_total)\n\t{\n\t\t// determine the average weight of each quality range\n\t\tthis.ma_quality_avg += (this.quality_avg - this.ma_quality_avg) * this.last_update * this.config.BD_DETECTION_RATE/2.0;\n\n\t\tthis.maa_quality_avg += (this.ma_quality_avg - this.maa_quality_avg) * this.last_update;\n\t\tthis.ma_quality_total += (this.quality_total - this.ma_quality_total) * this.last_update * this.config.BD_DETECTION_RATE/2.0;\n\n\t\tthis.ma_quality_avg -= 0.98*this.ma_quality_avg*this.last_update*3.0;\n\t}\n\telse\n\t{\n\t\tthis.quality_avg = 0.001;\n\t}\n\n\tif (this.ma_quality_total <= 0) this.ma_quality_total = 0.001;\n\tif (this.ma_quality_avg <= 0) this.ma_quality_avg = 0.001;\n\t\n\tvar avg_bpm_offset = 0.0;\n\tvar offset_test_bpm = this.current_bpm;\n\tvar draft = new Array();\n\t\n\tif (this.quality_avg) for (x=0; x<this.config.BD_DETECTION_RANGES; x++)\n\t{\n\t\t// if this detection range weight*tolerance is higher than the average weight then add it's moving average contribution \n\t\tif (this.detection_quality[x]*this.config.BD_QUALITY_TOLERANCE >= this.ma_quality_avg)\n\t\t{\n\t\t\tif (this.ma_bpm_range[x] < bpm_ceil && this.ma_bpm_range[x] > bpm_floor)\n\t\t\t{\n\t\t\t\tbpm_total += this.maa_bpm_range[x];\n\n\t\t\t\tvar draft_float = Math.round((60.0/this.maa_bpm_range[x])*1000.0);\n\t\t\t\t\n\t\t\t\tdraft_float = (Math.abs(Math.ceil(draft_float)-(60.0/this.current_bpm)*1000.0)<(Math.abs(Math.floor(draft_float)-(60.0/this.current_bpm)*1000.0)))?Math.ceil(draft_float/10.0):Math.floor(draft_float/10.0);\n\t\t\t\tvar draft_int = parseInt(draft_float/10.0);\n\t\t\t//\tif (draft_int) console.log(draft_int);\n\t\t\t\tif (typeof(draft[draft_int]=='undefined')) draft[draft_int] = 0;\n\t\t\t\t\n\t\t\t\tdraft[draft_int]+=this.detection_quality[x]/this.quality_avg;\n\t\t\t\tbpm_contributions++;\n\t\t\t\tif (offset_test_bpm == 0.0) offset_test_bpm = this.maa_bpm_range[x];\n\t\t\t\telse \n\t\t\t\t{\n\t\t\t\t\tavg_bpm_offset += Math.abs(offset_test_bpm-this.maa_bpm_range[x]);\n\t\t\t\t}\n\t\t\t\t\n\t\t\t\t\n\t\t\t}\n\t\t}\n\t}\n\t\t\n\t// if we have one or more contributions that pass criteria then attempt to display a guess\n\tvar has_prediction = (bpm_contributions>=this.config.BD_MINIMUM_CONTRIBUTIONS)?true:false;\n\n\tvar draft_winner=0;\n\tvar win_val = 0;\n\t\n\tif (has_prediction) \n\t{\n\t\tfor (var draft_i in draft)\n\t\t{\n\t\t\tif (draft[draft_i] > win_val)\n\t\t\t{\n\t\t\t\twin_val = draft[draft_i];\n\t\t\t\tdraft_winner = draft_i;\n\t\t\t}\n\t\t}\n\t\t\n\t\tthis.bpm_predict = 60.0/(draft_winner/10.0);\n\t\t\n\t\tavg_bpm_offset /= bpm_contributions;\n\t\tthis.bpm_offset = avg_bpm_offset;\n\t\t\n\t\tif (!this.current_bpm)  \n\t\t{\n\t\t\tthis.current_bpm = this.bpm_predict; \n\t\t}\n\t}\n\t\t\n\tif (this.current_bpm && this.bpm_predict) this.current_bpm -= (this.current_bpm-this.bpm_predict)*this.last_update;\t\n\t\n\t// hold a contest for bpm to find the current mode\n\tvar contest_max=0;\n\t\n\tfor (var contest_i in this.bpm_contest)\n\t{\n\t\tif (contest_max < this.bpm_contest[contest_i]) contest_max = this.bpm_contest[contest_i]; \n\t\tif (this.bpm_contest[contest_i] > this.config.BD_FINISH_LINE/2.0)\n\t\t{\n\t\t\tvar draft_int_lo = parseInt(Math.round((contest_i)/10.0));\n\t\t\tif (this.bpm_contest_lo[draft_int_lo] != this.bpm_contest_lo[draft_int_lo]) this.bpm_contest_lo[draft_int_lo] = 0;\n\t\t\tthis.bpm_contest_lo[draft_int_lo]+= (this.bpm_contest[contest_i]/6.0)*this.last_update;\n\t\t}\n\t}\n\t\t\n\t// normalize to a finish line\n\tif (contest_max > this.config.BD_FINISH_LINE) \n\t{\n\t\tfor (var contest_i in this.bpm_contest)\n\t\t{\n\t\t\tthis.bpm_contest[contest_i]=(this.bpm_contest[contest_i]/contest_max)*this.config.BD_FINISH_LINE;\n\t\t}\n\t}\n\n\tcontest_max = 0;\n\tfor (var contest_i in this.bpm_contest_lo)\n\t{\n\t\tif (contest_max < this.bpm_contest_lo[contest_i]) contest_max = this.bpm_contest_lo[contest_i]; \n\t}\n\n\t// normalize to a finish line\n\tif (contest_max > this.config.BD_FINISH_LINE) \n\t{\n\t\tfor (var contest_i in this.bpm_contest_lo)\n\t\t{\n\t\t\tthis.bpm_contest_lo[contest_i]=(this.bpm_contest_lo[contest_i]/contest_max)*this.config.BD_FINISH_LINE;\n\t\t}\n\t}\n\n\t\n\t// decay contest values from last loop\n\tfor (contest_i in this.bpm_contest)\n\t{\n\t\tthis.bpm_contest[contest_i]-=this.bpm_contest[contest_i]*(this.last_update/this.config.BD_DETECTION_RATE);\n\t}\n\t\n\t// decay contest values from last loop\n\tfor (contest_i in this.bpm_contest_lo)\n\t{\n\t\tthis.bpm_contest_lo[contest_i]-=this.bpm_contest_lo[contest_i]*(this.last_update/this.config.BD_DETECTION_RATE);\n\t}\n\t\n\tthis.bpm_timer+=this.last_update;\n\t\n\tvar winner = 0;\n\tvar winner_lo = 0;\n\t\n\t// attempt to display the beat at the beat interval ;)\n\tif (this.bpm_timer > this.winning_bpm/4.0 && this.current_bpm)\n\t{\t\t\n\t\tthis.win_val = 0;\n\t\tthis.win_val_lo = 0;\n\n\t\tif (this.winning_bpm) while (this.bpm_timer > this.winning_bpm/4.0) this.bpm_timer -= this.winning_bpm/4.0;\n\t\t\n\t\t// increment beat counter\n\t\t\n\t\tthis.quarter_counter++;\t\t\n\t\tthis.half_counter= parseInt(this.quarter_counter/2);\n\t\tthis.beat_counter = parseInt(this.quarter_counter/4);\n\t\t\n\t\t// award the winner of this iteration\n\t\tvar idx = parseInt(Math.round((60.0/this.current_bpm)*10.0));\n\t\tif (typeof(this.bpm_contest[idx])=='undefined') this.bpm_contest[idx] = 0;\n\t\tthis.bpm_contest[idx]+=this.config.BD_QUALITY_REWARD;\n\t\t\n\t\t\n\t\t// find the overall winner so far\n\t\tfor (var contest_i in this.bpm_contest)\n\t\t{\n\t\t\tif (this.win_val < this.bpm_contest[contest_i])\n\t\t\t{\n\t\t\t\twinner = contest_i;\n\t\t\t\tthis.win_val = this.bpm_contest[contest_i];\n\t\t\t}\n\t\t}\n\t\t\n\t\tif (winner)\n\t\t{\n\t\t\tthis.win_bpm_int = parseInt(winner);\n\t\t\tthis.winning_bpm = (60.0/(winner/10.0));\n\t\t}\n\t\t\n\t\t// find the overall winner so far\n\t\tfor (var contest_i in this.bpm_contest_lo)\n\t\t{\n\t\t\tif (this.win_val_lo < this.bpm_contest_lo[contest_i])\n\t\t\t{\n\t\t\t\twinner_lo = contest_i;\n\t\t\t\tthis.win_val_lo = this.bpm_contest_lo[contest_i];\n\t\t\t}\n\t\t}\n\t\t\n\t\tif (winner_lo)\n\t\t{\n\t\t\tthis.win_bpm_int_lo = parseInt(winner_lo);\n\t\t\tthis.winning_bpm_lo = 60.0/winner_lo;\n\t\t}\n\t\t\n\t\t\n\t\tif (typeof(console)!='undefined' && (this.beat_counter % 4) == 0) console.log(\"BeatDetektor(\"+this.BPM_MIN+\",\"+this.BPM_MAX+\"): [ Current Estimate: \"+winner+\" BPM ] [ Time: \"+(parseInt(timer_seconds*1000.0)/1000.0)+\"s, Quality: \"+(parseInt(this.quality_total*1000.0)/1000.0)+\", Rank: \"+(parseInt(this.win_val*1000.0)/1000.0)+\", Jitter: \"+(parseInt(this.bpm_offset*1000000.0)/1000000.0)+\" ]\");\n\t}\n\n}\n\n// Sample Modules\nBeatDetektor.modules = new Object(); \nBeatDetektor.modules.vis = new Object();\n\n// simple bass kick visualizer assistant module\nBeatDetektor.modules.vis.BassKick = function()\n{\n\tthis.is_kick = false;\n}\n\nBeatDetektor.modules.vis.BassKick.prototype.process = function(det)\n{\n\tthis.is_kick = ((det.detection[0] && det.detection[1]) || (det.ma_freq_range[0]/det.maa_freq_range[0])>1.4);\n}\n\nBeatDetektor.modules.vis.BassKick.prototype.isKick = function()\n{\n\treturn this.is_kick;\n}\n\n\n// simple vu spectrum visualizer assistant module\nBeatDetektor.modules.vis.VU = function()\n{\n\tthis.vu_levels = new Array();\t\n}\n\nBeatDetektor.modules.vis.VU.prototype.process = function(det,lus)\n{\n\t\tvar i,det_val,det_max = 0.0;\n\t\tif (typeof(lus)=='undefined') lus = det.last_update;\n\n\t\tfor (i = 0; i < det.config.BD_DETECTION_RANGES; i++)\n\t\t{\n\t\t\tdet_val = (det.ma_freq_range[i]/det.maa_freq_range[i]);\t\n\t\t\tif (det_val > det_max) det_max = det_val;\n\t\t}\t\t\n\n\t\tif (det_max <= 0) det_max = 1.0;\n\n\t\tfor (i = 0; i < det.config.BD_DETECTION_RANGES; i++)\n\t\t{\n\t\t\tdet_val = (det.ma_freq_range[i]/det.maa_freq_range[i]); //fabs(fftData[i*2]/2.0);\n\n\t\t\tif (det_val != det_val) det_val = 0;\n\n\t\t\t//&& (det_val > this.vu_levels[i])\n\t\t\tif (det_val>1.0)\n\t\t\t{\n\t\t\t\tdet_val -= 1.0;\n\t\t\t\tif (det_val>1.0) det_val = 1.0;\n\n\t\t\t\tif (det_val > this.vu_levels[i]) \n\t\t\t\t\tthis.vu_levels[i] = det_val;\n\t\t\t\telse if (det.current_bpm) this.vu_levels[i] -= (this.vu_levels[i]-det_val)*lus*(1.0/det.current_bpm)*3.0;\n\t\t\t}\n\t\t\telse \n\t\t\t{\n\t\t\t\tif (det.current_bpm) this.vu_levels[i] -= (lus/det.current_bpm)*2.0;\n\t\t\t}\n\n\t\t\tif (this.vu_levels[i] < 0 || this.vu_levels[i] != this.vu_levels[i]) this.vu_levels[i] = 0;\n\t\t}\n}\n\n\n// returns vu level for BD_DETECTION_RANGES range[x]\nBeatDetektor.modules.vis.VU.prototype.getLevel = function(x)\n{\n\treturn this.vu_levels[x];\n}\n"
  },
  {
    "path": "js/test/test.html",
    "content": "<html>\n<head>\n<title>BeatDetektor Unit Test</title>\n<script src=\"../beatdetektor.js\" type='text/javascript'></script>\n<script type='text/javascript'>\n\nbd_low = new BeatDetektor(48,95);\nbd_med = new BeatDetektor(85,169);\nbd_high = new BeatDetektor(150,280);\nvu = new BeatDetektor.modules.vis.VU();\nkick_det = new BeatDetektor.modules.vis.BassKick();\n\nvar dummyDataOne = new Array(1024);\nvar dummyDataTwo = new Array(1024);\n\n\n// make two simulated buffers for creating a tick, make some random noise with a higher noise in buffer 2\n// actual FFT will work better because of proper rising/falling edges, some simulated BPM values will cause failure near BPM boundaries\nfor (var i = 0; i < 1024; i++) \n{\n\tif (i < 512)\n\t{\n\t\tdummyDataOne[i] = Math.floor(Math.random()*2.0);\n\t\tdummyDataTwo[i] = Math.floor(Math.random()*10.0);\n\t}\n\telse\n\t{\n\t\tdummyDataOne[i] = Math.floor(Math.random()*2.0);\n\t\tdummyDataTwo[i] = Math.floor(Math.random()*2.0);\n\t}\n}\n\n// simulate bpm\nvar bpm_sim = 145.5;\n\n// simulate 60fps input\nvar bdRate = 16;\t\nvar signal_rate = parseInt(((60.0/(bpm_sim))*1000.0));\nvar signal_counter = 0;\nvar total_calls = 0;\n\nif (typeof(window.console)!='undefined') console.log(\"Starting simulation of \"+bpm_sim+\" BPM.\");\n\n// Simulate 1 minute\nfor (var i = 0; i < 30000; i+=bdRate)\n{\n\twhile (signal_counter>signal_rate) signal_counter-=signal_rate;\n\tbd_low.process((i / 1000.0),(signal_counter < signal_rate*0.1)?dummyDataTwo:dummyDataOne);\n\tbd_med.process((i / 1000.0),(signal_counter < signal_rate*0.2)?dummyDataTwo:dummyDataOne);\n\tbd_high.process((i / 1000.0),(signal_counter < signal_rate*0.5)?dummyDataTwo:dummyDataOne);\n\t\n\t// module test\n\tvu.process(bd_med);\n\tkick_det.process(bd_med);\n\n\t// if (kick_det.isKick()) if (typeof(window.console)!='undefined') console.log(\"Kick @\"+(i/1000.0));\n\t// if (typeof(window.console)!='undefined') console.log(\"VU @0\"+(i/1000.0)+\" = \"+vu.getLevel(0));\n\t\n\tsignal_counter+=bdRate;\n\ttotal_calls++;\n}\n\nif (typeof(window.console)!='undefined') console.log(\"Total BeatDetektor.process() calls: \"+total_calls);\n\nfor (var i in bd_med.bpm_contest)\n{\n\tconsole.log(i+\": \"+bd_med.bpm_contest[i]);\n\t\n}\n\nconsole.log(bd_med.ma_bpm_range);\n\n</script>\n</head>\n<body><br/>\n<h1>BeatDetektor Result: \nLow:\n<script type='text/javascript'>\n\tdocument.write((bd_low.win_bpm_int/10.0)+\" BPM / \"+(bd_low.win_bpm_int_lo)+\" BPM\");\n</script>\n</h1><br><br>\n<h1>Med:\n<script type='text/javascript'>\n\tdocument.write((bd_med.win_bpm_int/10.0)+\" BPM / \"+(bd_med.win_bpm_int_lo)+\" BPM\");\n</script>\n</h1><br><br>\n<h1>High:\n<script type='text/javascript'>\n\tdocument.write((bd_high.win_bpm_int/10.0)+\" BPM / \"+(bd_high.win_bpm_int_lo)+\" BPM\");\n</script>\n</h1><br><br>\nView console for details.\n</body>\n</html>\n"
  }
]