Skip to content

Commit

Permalink
Replacement of es5 functions to es6 classes feat p5.metro (#527)
Browse files Browse the repository at this point in the history
  • Loading branch information
endurance21 authored Aug 17, 2020
1 parent a03ad48 commit 3163e18
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 77 deletions.
7 changes: 5 additions & 2 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@ p5.Delay = Delay;


import './reverb';
import './metro';

import Metro from './metro';
p5.Metro = Metro;

import './looper';
import './soundLoop';

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

import './soundRecorder';


import peakDetect from './peakDetect';
p5.peakDetect = peakDetect;


import Distortion from './distortion';
p5.Distortion = Distortion;

Expand Down
153 changes: 78 additions & 75 deletions src/metro.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,93 +3,96 @@ import p5sound from './master';
// https://github.com/TONEnoTONE/Tone.js/
import Clock from 'Tone/core/Clock';

p5.Metro = function () {
this.clock = new Clock({
callback: this.ontick.bind(this),
});
this.syncedParts = [];
this.bpm = 120; // gets overridden by p5.Part
this._init();
class Metro {
constructor() {
this.clock = new Clock({
callback: this.ontick.bind(this),
});
this.syncedParts = [];
this.bpm = 120; // gets overridden by p5.Part
this._init();

this.prevTick = 0;
this.tatumTime = 0;
this.prevTick = 0;
this.tatumTime = 0;

this.tickCallback = function () {};
};
this.tickCallback = function () {};
}

p5.Metro.prototype.ontick = function (tickTime) {
var elapsedTime = tickTime - this.prevTick;
var secondsFromNow = tickTime - p5sound.audiocontext.currentTime;
if (elapsedTime - this.tatumTime <= -0.02) {
return;
} else {
// console.log('ok', this.syncedParts[0].phrases[0].name);
this.prevTick = tickTime;
ontick(tickTime) {
var elapsedTime = tickTime - this.prevTick;
var secondsFromNow = tickTime - p5sound.audiocontext.currentTime;
if (elapsedTime - this.tatumTime <= -0.02) {
return;
} else {
// console.log('ok', this.syncedParts[0].phrases[0].name);
this.prevTick = tickTime;

// for all of the active things on the metro:
var self = this;
this.syncedParts.forEach(function (thisPart) {
if (!thisPart.isPlaying) return;
thisPart.incrementStep(secondsFromNow);
// each synced source keeps track of its own beat number
thisPart.phrases.forEach(function (thisPhrase) {
var phraseArray = thisPhrase.sequence;
var bNum = self.metroTicks % phraseArray.length;
if (
phraseArray[bNum] !== 0 &&
(self.metroTicks < phraseArray.length || !thisPhrase.looping)
) {
thisPhrase.callback(secondsFromNow, phraseArray[bNum]);
}
// for all of the active things on the metro:
var self = this;
this.syncedParts.forEach(function (thisPart) {
if (!thisPart.isPlaying) return;
thisPart.incrementStep(secondsFromNow);
// each synced source keeps track of its own beat number
thisPart.phrases.forEach(function (thisPhrase) {
var phraseArray = thisPhrase.sequence;
var bNum = self.metroTicks % phraseArray.length;
if (
phraseArray[bNum] !== 0 &&
(self.metroTicks < phraseArray.length || !thisPhrase.looping)
) {
thisPhrase.callback(secondsFromNow, phraseArray[bNum]);
}
});
});
});
this.metroTicks += 1;
this.tickCallback(secondsFromNow);
this.metroTicks += 1;
this.tickCallback(secondsFromNow);
}
}
};

p5.Metro.prototype.setBPM = function (bpm, rampTime = 0) {
var beatTime = 60 / (bpm * this.tatums);
var now = p5sound.audiocontext.currentTime;
this.tatumTime = beatTime;
setBPM(bpm, rampTime = 0) {
var beatTime = 60 / (bpm * this.tatums);
var now = p5sound.audiocontext.currentTime;
this.tatumTime = beatTime;

this.clock.frequency.setValueAtTime(this.clock.frequency.value, now);
this.clock.frequency.linearRampToValueAtTime(bpm, now + rampTime);
this.bpm = bpm;
};
this.clock.frequency.setValueAtTime(this.clock.frequency.value, now);
this.clock.frequency.linearRampToValueAtTime(bpm, now + rampTime);
this.bpm = bpm;
}

p5.Metro.prototype.getBPM = function () {
return (this.clock.getRate() / this.tatums) * 60;
};
getBPM() {
return (this.clock.getRate() / this.tatums) * 60;
}

p5.Metro.prototype._init = function () {
this.metroTicks = 0;
// this.setBPM(120);
};
_init() {
this.metroTicks = 0;
// this.setBPM(120);
}

// clear existing synced parts, add only this one
p5.Metro.prototype.resetSync = function (part) {
this.syncedParts = [part];
};
// clear existing synced parts, add only this one
resetSync(part) {
this.syncedParts = [part];
}

// push a new synced part to the array
p5.Metro.prototype.pushSync = function (part) {
this.syncedParts.push(part);
};
// push a new synced part to the array
pushSync(part) {
this.syncedParts.push(part);
}

p5.Metro.prototype.start = function (timeFromNow) {
var t = timeFromNow || 0;
var now = p5sound.audiocontext.currentTime;
this.clock.start(now + t);
this.setBPM(this.bpm);
};
start(timeFromNow) {
var t = timeFromNow || 0;
var now = p5sound.audiocontext.currentTime;
this.clock.start(now + t);
this.setBPM(this.bpm);
}

p5.Metro.prototype.stop = function (timeFromNow) {
var t = timeFromNow || 0;
var now = p5sound.audiocontext.currentTime;
this.clock.stop(now + t);
};
stop(timeFromNow) {
var t = timeFromNow || 0;
var now = p5sound.audiocontext.currentTime;
this.clock.stop(now + t);
}

p5.Metro.prototype.beatLength = function (tatums) {
this.tatums = 1 / tatums / 4; // lowest possible division of a beat
};
beatLength(tatums) {
this.tatums = 1 / tatums / 4; // lowest possible division of a beat
}
}
export default Metro;

0 comments on commit 3163e18

Please sign in to comment.