From 2ca88c880ea0820f38368fe4f3f906ed4ab22cb6 Mon Sep 17 00:00:00 2001 From: Eric Rosenbaum Date: Sun, 8 Jan 2017 18:57:51 -0500 Subject: [PATCH] play note and play drum return a promise to wait some number of beats --- src/index.js | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/index.js b/src/index.js index 696e625..e10cd5f 100644 --- a/src/index.js +++ b/src/index.js @@ -126,15 +126,21 @@ AudioPlayer.prototype.playNoteForBeats = function (note, beats) { this.instrument.play( note, Tone.context.currentTime, {duration : Number(beats)} ); + return this.waitForBeats(beats); }; -AudioPlayer.prototype._midiToFreq = function (midiNote) { - var freq = this.tone.intervalToFrequencyRatio(midiNote - 60) * 261.63; // 60 is C4 - return freq; -}; - -AudioPlayer.prototype.playDrumForBeats = function () { +AudioPlayer.prototype.playDrumForBeats = function (beats) { // this.drumSamplers[drumNum].triggerAttack(); + return this.waitForBeats(beats); +}; + +AudioPlayer.prototype.waitForBeats = function (beats) { + var storedContext = this; + return new Promise(function (resolve) { + setTimeout(function () { + resolve(); + }, ((60 / storedContext.currentTempo) * 1000 * beats)); + }); }; AudioPlayer.prototype.stopAllSounds = function () {