-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdetect-bpm.js
245 lines (191 loc) · 5.8 KB
/
detect-bpm.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
const OfflineContext = (window.OfflineAudioContext || window.webkitOfflineAudioContext);
/**
* Detect BPM of a sound source
* @param {AudioBuffer} buffer Sound to process
* @return {Number} Detected BPM
*/
function detect(buffer) {
const source = getLowPassSource(buffer);
/**
* Schedule the sound to start playing at time:0
*/
source.start(0);
/**
* Pipe the source through the program
*/
return [
findPeaks,
identifyIntervals,
groupByTempo(buffer.sampleRate),
getTopCandidate
].reduce(
(state, fn) => fn(state),
source.buffer.getChannelData(0)
);
}
/**
* Sort results by count and return top candidate
* @param {Object} Candidate
* @return {Number}
*/
function getTopCandidate(candidates) {
return candidates
.sort((a, b) => (b.count - a.count))
.splice(0, 5)[0].tempo;
}
/**
* Apply a low pass filter to an AudioBuffer
* @param {AudioBuffer} buffer Source AudioBuffer
* @return {AudioBufferSourceNode}
*/
function getLowPassSource(buffer) {
const {length, numberOfChannels, sampleRate} = buffer;
const context = new OfflineContext(numberOfChannels, length, sampleRate);
/**
* Create buffer source
*/
const source = context.createBufferSource();
source.buffer = buffer;
/**
* Create filter
*/
const filter = context.createBiquadFilter();
filter.type = 'lowpass';
/**
* Pipe the song into the filter, and the filter into the offline context
*/
source.connect(filter);
filter.connect(context.destination);
return source;
}
/**
* Find peaks in sampleRate
* @param {Array} data Bugger channel data
* @return {Array} Peaks found that are greater than the threshold
*/
function findPeaks(data) {
let peaks = [];
let threshold = 0.9;
const minThresold = 0.3;
const minPeaks = 15;
/**
* Keep looking for peaks lowering the threshold until
* we have at least 15 peaks (10 seconds @ 90bpm)
*/
while (peaks.length < minPeaks && threshold >= minThresold) {
peaks = findPeaksAtThreshold(data, threshold);
threshold -= 0.05;
}
/**
* Too fiew samples are unreliable
*/
if (peaks.length < minPeaks) {
throw (
new Error('Could not find enough samples for a reliable detection.')
);
}
return peaks;
}
/**
* Function to identify peaks
* @param {Array} data Buffer channel data
* @param {Number} threshold Threshold for qualifying as a peak
* @return {Array} Peaks found that are grater than the threshold
*/
function findPeaksAtThreshold(data, threshold) {
const peaks = [];
/**
* Identify peaks that pass the threshold, adding them to the collection
*/
for (var i = 0, l = data.length; i < l; i += 1) {
if (data[i] > threshold) {
peaks.push(i);
/**
* Skip forward ~ 1/4s to get past this peak
*/
i += 10000;
}
}
return peaks;
}
/**
* Identify intervals between peaks
* @param {Array} peaks Array of qualified peaks
* @return {Array} Identifies intervals between peaks
*/
function identifyIntervals(peaks) {
const intervals = [];
peaks.forEach((peak, index) => {
for (let i = 0; i < 10; i+= 1) {
let interval = peaks[index + i] - peak;
/**
* Try and find a matching interval and increase it's count
*/
let foundInterval = intervals.some(intervalCount => {
if (intervalCount.interval === interval) {
return intervalCount.count += 1;
}
});
/**
* Add the interval to the collection if it's unique
*/
if (!foundInterval) {
intervals.push({
interval: interval,
count: 1
});
}
}
});
return intervals;
}
/**
* Factory for group reducer
* @param {Number} sampleRate Audio sample rate
* @return {Function}
*/
function groupByTempo(sampleRate) {
/**
* Figure out best possible tempo candidates
* @param {Array} intervalCounts List of identified intervals
* @return {Array} Intervals grouped with similar values
*/
return (intervalCounts) => {
const tempoCounts = [];
intervalCounts.forEach(intervalCount => {
if (intervalCount.interval !== 0) {
/**
* Convert an interval to tempo
*/
let theoreticalTempo = (60 / (intervalCount.interval / sampleRate));
/**
* Adjust the tempo to fit within the 90-180 BPM range
*/
while (theoreticalTempo < 90) theoreticalTempo *= 2;
while (theoreticalTempo > 180) theoreticalTempo /= 2;
/**
* Round to legible integer
*/
theoreticalTempo = Math.round(theoreticalTempo);
/**
* See if another interval resolved to the same tempo
*/
let foundTempo = tempoCounts.some(tempoCount => {
if (tempoCount.tempo === theoreticalTempo) {
return tempoCount.count += intervalCount.count;
}
});
/**
* Add a unique tempo to the collection
*/
if (!foundTempo) {
tempoCounts.push({
tempo: theoreticalTempo,
count: intervalCount.count
});
}
}
});
return tempoCounts;
}
}