Skip to content

Commit a03ad48

Browse files
authored
Replacement of es5 functions to es6 classes feat p5.onsetDetect (#525)
1 parent 57b8e88 commit a03ad48

File tree

2 files changed

+43
-35
lines changed

2 files changed

+43
-35
lines changed

src/app.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,6 @@ import './soundRecorder';
6868
import peakDetect from './peakDetect';
6969
p5.peakDetect = peakDetect;
7070

71-
72-
7371
import Distortion from './distortion';
7472
p5.Distortion = Distortion;
7573

@@ -82,5 +80,11 @@ p5.AudioVoice = AudioVoice;
8280
import MonoSynth from './monosynth';
8381
p5.MonoSynth = MonoSynth;
8482

83+
84+
85+
import OnsetDetect from './onsetDetect';
86+
p5.OnsetDetect = OnsetDetect;
87+
8588
import PolySynth from './polysynth';
8689
p5.PolySynth = PolySynth;
90+

src/onsetDetect.js

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -9,40 +9,44 @@
99
* @param {Number} threshold Amplitude threshold between 0 (no energy) and 1 (maximum)
1010
* @param {Function} callback Function to call when an onset is detected
1111
*/
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;
3930

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+
}
4446
}
47+
48+
this.penergy = this.energy;
4549
}
50+
}
4651

47-
this.penergy = this.energy;
48-
};
52+
export default OnsetDetect;

0 commit comments

Comments
 (0)