Skip to content

Commit 3163e18

Browse files
authored
Replacement of es5 functions to es6 classes feat p5.metro (#527)
1 parent a03ad48 commit 3163e18

File tree

2 files changed

+83
-77
lines changed

2 files changed

+83
-77
lines changed

src/app.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,10 @@ p5.Delay = Delay;
5555

5656

5757
import './reverb';
58-
import './metro';
58+
59+
import Metro from './metro';
60+
p5.Metro = Metro;
61+
5962
import './looper';
6063
import './soundLoop';
6164

@@ -64,10 +67,10 @@ p5.Compressor = Compressor;
6467

6568
import './soundRecorder';
6669

67-
6870
import peakDetect from './peakDetect';
6971
p5.peakDetect = peakDetect;
7072

73+
7174
import Distortion from './distortion';
7275
p5.Distortion = Distortion;
7376

src/metro.js

Lines changed: 78 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -3,93 +3,96 @@ import p5sound from './master';
33
// https://github.com/TONEnoTONE/Tone.js/
44
import Clock from 'Tone/core/Clock';
55

6-
p5.Metro = function () {
7-
this.clock = new Clock({
8-
callback: this.ontick.bind(this),
9-
});
10-
this.syncedParts = [];
11-
this.bpm = 120; // gets overridden by p5.Part
12-
this._init();
6+
class Metro {
7+
constructor() {
8+
this.clock = new Clock({
9+
callback: this.ontick.bind(this),
10+
});
11+
this.syncedParts = [];
12+
this.bpm = 120; // gets overridden by p5.Part
13+
this._init();
1314

14-
this.prevTick = 0;
15-
this.tatumTime = 0;
15+
this.prevTick = 0;
16+
this.tatumTime = 0;
1617

17-
this.tickCallback = function () {};
18-
};
18+
this.tickCallback = function () {};
19+
}
1920

20-
p5.Metro.prototype.ontick = function (tickTime) {
21-
var elapsedTime = tickTime - this.prevTick;
22-
var secondsFromNow = tickTime - p5sound.audiocontext.currentTime;
23-
if (elapsedTime - this.tatumTime <= -0.02) {
24-
return;
25-
} else {
26-
// console.log('ok', this.syncedParts[0].phrases[0].name);
27-
this.prevTick = tickTime;
21+
ontick(tickTime) {
22+
var elapsedTime = tickTime - this.prevTick;
23+
var secondsFromNow = tickTime - p5sound.audiocontext.currentTime;
24+
if (elapsedTime - this.tatumTime <= -0.02) {
25+
return;
26+
} else {
27+
// console.log('ok', this.syncedParts[0].phrases[0].name);
28+
this.prevTick = tickTime;
2829

29-
// for all of the active things on the metro:
30-
var self = this;
31-
this.syncedParts.forEach(function (thisPart) {
32-
if (!thisPart.isPlaying) return;
33-
thisPart.incrementStep(secondsFromNow);
34-
// each synced source keeps track of its own beat number
35-
thisPart.phrases.forEach(function (thisPhrase) {
36-
var phraseArray = thisPhrase.sequence;
37-
var bNum = self.metroTicks % phraseArray.length;
38-
if (
39-
phraseArray[bNum] !== 0 &&
40-
(self.metroTicks < phraseArray.length || !thisPhrase.looping)
41-
) {
42-
thisPhrase.callback(secondsFromNow, phraseArray[bNum]);
43-
}
30+
// for all of the active things on the metro:
31+
var self = this;
32+
this.syncedParts.forEach(function (thisPart) {
33+
if (!thisPart.isPlaying) return;
34+
thisPart.incrementStep(secondsFromNow);
35+
// each synced source keeps track of its own beat number
36+
thisPart.phrases.forEach(function (thisPhrase) {
37+
var phraseArray = thisPhrase.sequence;
38+
var bNum = self.metroTicks % phraseArray.length;
39+
if (
40+
phraseArray[bNum] !== 0 &&
41+
(self.metroTicks < phraseArray.length || !thisPhrase.looping)
42+
) {
43+
thisPhrase.callback(secondsFromNow, phraseArray[bNum]);
44+
}
45+
});
4446
});
45-
});
46-
this.metroTicks += 1;
47-
this.tickCallback(secondsFromNow);
47+
this.metroTicks += 1;
48+
this.tickCallback(secondsFromNow);
49+
}
4850
}
49-
};
5051

51-
p5.Metro.prototype.setBPM = function (bpm, rampTime = 0) {
52-
var beatTime = 60 / (bpm * this.tatums);
53-
var now = p5sound.audiocontext.currentTime;
54-
this.tatumTime = beatTime;
52+
setBPM(bpm, rampTime = 0) {
53+
var beatTime = 60 / (bpm * this.tatums);
54+
var now = p5sound.audiocontext.currentTime;
55+
this.tatumTime = beatTime;
5556

56-
this.clock.frequency.setValueAtTime(this.clock.frequency.value, now);
57-
this.clock.frequency.linearRampToValueAtTime(bpm, now + rampTime);
58-
this.bpm = bpm;
59-
};
57+
this.clock.frequency.setValueAtTime(this.clock.frequency.value, now);
58+
this.clock.frequency.linearRampToValueAtTime(bpm, now + rampTime);
59+
this.bpm = bpm;
60+
}
6061

61-
p5.Metro.prototype.getBPM = function () {
62-
return (this.clock.getRate() / this.tatums) * 60;
63-
};
62+
getBPM() {
63+
return (this.clock.getRate() / this.tatums) * 60;
64+
}
6465

65-
p5.Metro.prototype._init = function () {
66-
this.metroTicks = 0;
67-
// this.setBPM(120);
68-
};
66+
_init() {
67+
this.metroTicks = 0;
68+
// this.setBPM(120);
69+
}
6970

70-
// clear existing synced parts, add only this one
71-
p5.Metro.prototype.resetSync = function (part) {
72-
this.syncedParts = [part];
73-
};
71+
// clear existing synced parts, add only this one
72+
resetSync(part) {
73+
this.syncedParts = [part];
74+
}
7475

75-
// push a new synced part to the array
76-
p5.Metro.prototype.pushSync = function (part) {
77-
this.syncedParts.push(part);
78-
};
76+
// push a new synced part to the array
77+
pushSync(part) {
78+
this.syncedParts.push(part);
79+
}
7980

80-
p5.Metro.prototype.start = function (timeFromNow) {
81-
var t = timeFromNow || 0;
82-
var now = p5sound.audiocontext.currentTime;
83-
this.clock.start(now + t);
84-
this.setBPM(this.bpm);
85-
};
81+
start(timeFromNow) {
82+
var t = timeFromNow || 0;
83+
var now = p5sound.audiocontext.currentTime;
84+
this.clock.start(now + t);
85+
this.setBPM(this.bpm);
86+
}
8687

87-
p5.Metro.prototype.stop = function (timeFromNow) {
88-
var t = timeFromNow || 0;
89-
var now = p5sound.audiocontext.currentTime;
90-
this.clock.stop(now + t);
91-
};
88+
stop(timeFromNow) {
89+
var t = timeFromNow || 0;
90+
var now = p5sound.audiocontext.currentTime;
91+
this.clock.stop(now + t);
92+
}
9293

93-
p5.Metro.prototype.beatLength = function (tatums) {
94-
this.tatums = 1 / tatums / 4; // lowest possible division of a beat
95-
};
94+
beatLength(tatums) {
95+
this.tatums = 1 / tatums / 4; // lowest possible division of a beat
96+
}
97+
}
98+
export default Metro;

0 commit comments

Comments
 (0)