mirror of
https://github.com/scratchfoundation/scratch-audio.git
synced 2024-12-22 22:12:48 -05:00
audio engine manages play note and beat timing
This commit is contained in:
parent
e1d478244d
commit
8557c4a7ab
1 changed files with 21 additions and 23 deletions
44
src/index.js
44
src/index.js
|
@ -38,8 +38,6 @@ function AudioEngine () {
|
||||||
|
|
||||||
// global tempo in bpm (beats per minute)
|
// global tempo in bpm (beats per minute)
|
||||||
this.currentTempo = 60;
|
this.currentTempo = 60;
|
||||||
this.minTempo = 10;
|
|
||||||
this.maxTempo = 1000;
|
|
||||||
|
|
||||||
// instrument player for play note blocks
|
// instrument player for play note blocks
|
||||||
this.instrumentPlayer = new InstrumentPlayer(this.input);
|
this.instrumentPlayer = new InstrumentPlayer(this.input);
|
||||||
|
@ -82,8 +80,27 @@ AudioEngine.prototype.loadSounds = function (sounds) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
AudioEngine.prototype.playNoteForBeatsWithInst = function (note, beats, inst) {
|
||||||
|
var sec = this.beatsToSec(beats);
|
||||||
|
this.instrumentPlayer.playNoteForSecWithInst(note, sec, inst);
|
||||||
|
return this.waitForBeats(beats);
|
||||||
|
};
|
||||||
|
|
||||||
|
AudioEngine.prototype.beatsToSec = function (beats) {
|
||||||
|
return (60 / this.currentTempo) * beats;
|
||||||
|
};
|
||||||
|
|
||||||
|
AudioEngine.prototype.waitForBeats = function (beats) {
|
||||||
|
var storedContext = this;
|
||||||
|
return new Promise(function (resolve) {
|
||||||
|
setTimeout(function () {
|
||||||
|
resolve();
|
||||||
|
}, storedContext.beatsToSec(beats) * 1000);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
AudioEngine.prototype.setTempo = function (value) {
|
AudioEngine.prototype.setTempo = function (value) {
|
||||||
// var newTempo = this._clamp(value, this.minTempo, this.maxTempo);
|
|
||||||
this.currentTempo = value;
|
this.currentTempo = value;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -153,28 +170,9 @@ AudioPlayer.prototype.playSound = function (md5) {
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
AudioPlayer.prototype.playNoteForBeats = function (note, beats) {
|
|
||||||
var sec = this.beatsToSec(beats);
|
|
||||||
this.audioEngine.instrumentPlayer.playNoteForSecWithInst(note, sec, this.currentInstrument);
|
|
||||||
return this.waitForBeats(beats);
|
|
||||||
};
|
|
||||||
|
|
||||||
AudioPlayer.prototype.playDrumForBeats = function (drum, beats) {
|
AudioPlayer.prototype.playDrumForBeats = function (drum, beats) {
|
||||||
this.audioEngine.drumPlayer.play(drum, this.effectsNode);
|
this.audioEngine.drumPlayer.play(drum, this.effectsNode);
|
||||||
return this.waitForBeats(beats);
|
return this.audioEngine.waitForBeats(beats);
|
||||||
};
|
|
||||||
|
|
||||||
AudioPlayer.prototype.waitForBeats = function (beats) {
|
|
||||||
var storedContext = this;
|
|
||||||
return new Promise(function (resolve) {
|
|
||||||
setTimeout(function () {
|
|
||||||
resolve();
|
|
||||||
}, storedContext.beatsToSec(beats) * 1000);
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
AudioPlayer.prototype.beatsToSec = function (beats) {
|
|
||||||
return (60 / this.audioEngine.currentTempo) * beats;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
AudioPlayer.prototype.stopAllSounds = function () {
|
AudioPlayer.prototype.stopAllSounds = function () {
|
||||||
|
|
Loading…
Reference in a new issue