|
9 | 9 | * @param {Number} threshold Amplitude threshold between 0 (no energy) and 1 (maximum)
|
10 | 10 | * @param {Function} callback Function to call when an onset is detected
|
11 | 11 | */
|
12 |
| -p5.OnsetDetect = function (freqLow, freqHigh, threshold, callback) { |
13 |
| - this.isDetected = false; |
14 |
| - this.freqLow = freqLow; |
15 |
| - this.freqHigh = freqHigh; |
16 |
| - this.treshold = threshold; |
17 |
| - this.energy = 0; |
18 |
| - this.penergy = 0; |
19 |
| - |
20 |
| - // speed of decay |
21 |
| - this.sensitivity = 500; |
22 |
| - |
23 |
| - this.callback = callback; |
24 |
| -}; |
25 |
| - |
26 |
| -// callback here too? |
27 |
| -p5.OnsetDetect.prototype.update = function (fftObject, callback) { |
28 |
| - this.energy = fftObject.getEnergy(this.freqLow, this.freqHigh) / 255; |
29 |
| - |
30 |
| - if (this.isDetected === false) { |
31 |
| - if (this.energy - this.penergy > this.treshold) { |
32 |
| - this.isDetected = true; |
33 |
| - |
34 |
| - if (this.callback) { |
35 |
| - this.callback(this.energy); |
36 |
| - } else if (callback) { |
37 |
| - callback(this.energy); |
38 |
| - } |
| 12 | +class OnsetDetect { |
| 13 | + constructor(freqLow, freqHigh, threshold, callback) { |
| 14 | + this.isDetected = false; |
| 15 | + this.freqLow = freqLow; |
| 16 | + this.freqHigh = freqHigh; |
| 17 | + this.treshold = threshold; |
| 18 | + this.energy = 0; |
| 19 | + this.penergy = 0; |
| 20 | + |
| 21 | + // speed of decay |
| 22 | + this.sensitivity = 500; |
| 23 | + |
| 24 | + this.callback = callback; |
| 25 | + } |
| 26 | + |
| 27 | + // callback here too? |
| 28 | + update(fftObject, callback) { |
| 29 | + this.energy = fftObject.getEnergy(this.freqLow, this.freqHigh) / 255; |
39 | 30 |
|
40 |
| - var self = this; |
41 |
| - setTimeout(function () { |
42 |
| - self.isDetected = false; |
43 |
| - }, this.sensitivity); |
| 31 | + if (this.isDetected === false) { |
| 32 | + if (this.energy - this.penergy > this.treshold) { |
| 33 | + this.isDetected = true; |
| 34 | + |
| 35 | + if (this.callback) { |
| 36 | + this.callback(this.energy); |
| 37 | + } else if (callback) { |
| 38 | + callback(this.energy); |
| 39 | + } |
| 40 | + |
| 41 | + var self = this; |
| 42 | + setTimeout(function () { |
| 43 | + self.isDetected = false; |
| 44 | + }, this.sensitivity); |
| 45 | + } |
44 | 46 | }
|
| 47 | + |
| 48 | + this.penergy = this.energy; |
45 | 49 | }
|
| 50 | +} |
46 | 51 |
|
47 |
| - this.penergy = this.energy; |
48 |
| -}; |
| 52 | +export default OnsetDetect; |
0 commit comments