From 3163e18a556e4003c859b729f1eda2bb34505801 Mon Sep 17 00:00:00 2001 From: DIVYANSHU RAJ <43696525+endurance21@users.noreply.github.com> Date: Mon, 17 Aug 2020 21:20:06 +0530 Subject: [PATCH] Replacement of es5 functions to es6 classes feat p5.metro (#527) --- src/app.js | 7 ++- src/metro.js | 153 ++++++++++++++++++++++++++------------------------- 2 files changed, 83 insertions(+), 77 deletions(-) diff --git a/src/app.js b/src/app.js index 7e102f30..eaae71e2 100644 --- a/src/app.js +++ b/src/app.js @@ -55,7 +55,10 @@ p5.Delay = Delay; import './reverb'; -import './metro'; + +import Metro from './metro'; +p5.Metro = Metro; + import './looper'; import './soundLoop'; @@ -64,10 +67,10 @@ p5.Compressor = Compressor; import './soundRecorder'; - import peakDetect from './peakDetect'; p5.peakDetect = peakDetect; + import Distortion from './distortion'; p5.Distortion = Distortion; diff --git a/src/metro.js b/src/metro.js index f2301d90..74382a96 100644 --- a/src/metro.js +++ b/src/metro.js @@ -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;