scratch-audioengine
Advanced tools
Comparing version 0.1.0-prerelease.1477936704 to 0.1.0-prerelease.1478036823
{ | ||
"name": "scratch-audioengine", | ||
"version": "0.1.0-prerelease.1477936704", | ||
"version": "0.1.0-prerelease.1478036823", | ||
"description": "audio engine for scratch 3.0", | ||
@@ -5,0 +5,0 @@ "main": "dist.js", |
@@ -31,4 +31,4 @@ var log = require('./log'); | ||
this.soundPlayers = []; | ||
this.loadSounds(sounds); | ||
this.soundPlayers = this.loadSounds(sounds); | ||
// Tone.Buffer.on('load', this._soundsLoaded.bind(this)); | ||
@@ -63,2 +63,4 @@ // soundfont setup | ||
AudioEngine.prototype.loadSounds = function (sounds) { | ||
var soundPlayers = []; | ||
for (var i=0; i<sounds.length; i++) { | ||
@@ -70,36 +72,37 @@ // skip adpcm form sounds since we can't load them yet | ||
} | ||
this.soundPlayers[i] = new Tone.Player(sounds[i].fileUrl); | ||
this.soundPlayers[i].connect(this.effectsNode); | ||
var player = {}; | ||
player.buffer = new Tone.Buffer(sounds[i].fileUrl); | ||
player.bufferSource = null; | ||
soundPlayers[i] = player; | ||
} | ||
return soundPlayers; | ||
}; | ||
// AudioEngine.prototype._soundsLoaded = function() { | ||
// console.log('all sounds loaded'); | ||
// } | ||
AudioEngine.prototype.playSound = function (index) { | ||
var player = this.soundPlayers[index]; | ||
if (player && player.buffer.loaded) { | ||
player.start(); | ||
// if the soundplayer exists and its buffer has loaded | ||
if (this.soundPlayers[index] && this.soundPlayers[index].buffer.loaded) { | ||
// stop the sound if it's already playing | ||
var b = this.soundPlayers[index].bufferSource; | ||
if (b) { | ||
b.stop(); | ||
} | ||
// create a new buffer source to play the sound | ||
var bufferSource = new Tone.BufferSource(this.soundPlayers[index].buffer.get()); | ||
bufferSource.connect(this.effectsNode); | ||
bufferSource.start(); | ||
bufferSource.playbackRate.value = this._getPitchRatio(); | ||
this.soundPlayers[index].bufferSource = bufferSource; | ||
return new Promise(function (resolve) { | ||
setTimeout(function () { | ||
resolve(); | ||
}, (player.buffer.duration * 1000) / player.playbackRate); | ||
bufferSource.onended = function (){resolve();}; | ||
}); | ||
} else { | ||
// if the sound has not yet loaded, wait and try again | ||
log.warn('sound ' + index + ' not loaded yet'); | ||
if (player) { | ||
setTimeout(function () { | ||
this.playSound(index); | ||
}.bind(this), 500); | ||
} | ||
} | ||
}; | ||
AudioEngine.prototype.getSoundDuration = function (index) { | ||
var player = this.soundPlayers[index]; | ||
if (player && player.buffer.loaded) { | ||
return player.buffer.duration; | ||
} else { | ||
return 0; | ||
} | ||
}; | ||
AudioEngine.prototype.playNoteForBeats = function (note, beats) { | ||
@@ -152,3 +155,6 @@ this.instrument.play( | ||
for (var i=0; i<this.soundPlayers.length; i++) { | ||
this.soundPlayers[i].stop(); | ||
var bufferSource = this.soundPlayers[i].bufferSource; | ||
if (bufferSource) { | ||
bufferSource.stop(); | ||
} | ||
} | ||
@@ -218,7 +224,8 @@ } | ||
} | ||
var ratio = this.tone.intervalToFrequencyRatio(this.pitchEffectValue / 10); | ||
var ratio = this._getPitchRatio(); | ||
for (var i=0; i<this.soundPlayers.length; i++) { | ||
var s = this.soundPlayers[i]; | ||
if (s) { | ||
s.playbackRate = ratio; | ||
var s = this.soundPlayers[i].bufferSource; | ||
if (s && s.playbackRate) { | ||
s.playbackRate.value = ratio; | ||
} | ||
@@ -228,2 +235,6 @@ } | ||
AudioEngine.prototype._getPitchRatio = function () { | ||
return this.tone.intervalToFrequencyRatio(this.pitchEffectValue / 10); | ||
}; | ||
AudioEngine.prototype.setInstrument = function (instrumentNum) { | ||
@@ -230,0 +241,0 @@ return Soundfont.instrument(Tone.context, this.instrumentNames[instrumentNum]).then( |
Sorry, the diff of this file is too big to display
827771
16
24024