change Tone samplers to players

This commit is contained in:
Eric Rosenbaum 2016-10-24 13:57:47 -04:00
parent f0b316ca56
commit 73dc7a3d40

View file

@ -1,3 +1,4 @@
var log = require('./log');
var Tone = require('tone'); var Tone = require('tone');
var Soundfont = require('soundfont-player'); var Soundfont = require('soundfont-player');
@ -15,23 +16,18 @@ function AudioEngine (sounds) {
this.reverb = new Tone.Freeverb(); this.reverb = new Tone.Freeverb();
this.pitchShiftRatio; this.pitchShiftRatio;
// reset effects to their default parameters
this.clearEffects();
// the effects are chained to an effects node for this clone, then to the master output // the effects are chained to an effects node for this clone, then to the master output
// so audio is sent from each sampler or instrument, through the effects in order, then out // so audio is sent from each player or instrument, through the effects in order, then out
// note that the pitch effect works differently - it sets the playback rate for each sampler // note that the pitch effect works differently - it sets the playback rate for each player
this.effectsNode = new Tone.Gain(); this.effectsNode = new Tone.Gain();
this.effectsNode.chain(this.delay, this.panner, this.reverb, Tone.Master); this.effectsNode.chain(this.delay, this.panner, this.reverb, Tone.Master);
// drum sounds // reset effects to their default parameters
// this.clearEffects();
// var drumFileNames = ['high_conga', 'small_cowbell', // load sounds
// 'snare_drum', 'splash cymbal'];
// this.drumSamplers = this._loadSoundFiles(drumFileNames);
// sound urls - map each url to its tone.sampler this.soundPlayers = [];
this.soundSamplers = [];
this.loadSounds(sounds); this.loadSounds(sounds);
// soundfont setup // soundfont setup
@ -58,26 +54,31 @@ function AudioEngine (sounds) {
AudioEngine.prototype.loadSounds = function (sounds) { AudioEngine.prototype.loadSounds = function (sounds) {
for (var i=0; i<sounds.length; i++) { for (var i=0; i<sounds.length; i++) {
var url = sounds[i].fileUrl;
// skip adpcm form sounds since we can't load them yet // skip adpcm form sounds since we can't load them yet
if (sounds[i].format == 'adpcm') { if (sounds[i].format == 'adpcm') {
log.warn('cannot load sound in adpcm format');
continue; continue;
} }
var sampler = new Tone.Sampler(url); this.soundPlayers[i] = new Tone.Player(sounds[i].fileUrl);
sampler.connect(this.effectsNode); this.soundPlayers[i].connect(this.effectsNode);
// this.soundSamplers.push(sampler);
this.soundSamplers[i] = sampler;
} }
}; };
AudioEngine.prototype.playSound = function (index) { AudioEngine.prototype.playSound = function (index) {
this.soundSamplers[index].triggerAttack(); var player = this.soundPlayers[index];
this.soundSamplers[index].player.playbackRate = 1 + this.pitchShiftRatio; if (player.buffer.loaded) {
player.start();
} else {
// if the sound has not yet loaded, wait and try again
log.warn('sound ' + index + ' not loaded yet');
setTimeout(function () {
this.playSound(index);
}, 100);
}
}; };
AudioEngine.prototype.getSoundDuration = function (index) { AudioEngine.prototype.getSoundDuration = function (index) {
return this.soundSamplers[index].player.buffer.duration; return this.soundPlayers[index].buffer.duration;
}; };
AudioEngine.prototype.playNoteForBeats = function (note, beats) { AudioEngine.prototype.playNoteForBeats = function (note, beats) {
@ -93,7 +94,7 @@ AudioEngine.prototype.playThereminForBeats = function (note, beats) {
// trigger attack // trigger attack
// create a timeout for slightly longer than the duration of the block // create a timeout for slightly longer than the duration of the block
// that releases the theremin - so we can slide continuously between // that releases the theremin - so we can slide continuously between
// successive notes without releasing and attacking // successive notes without releasing and re-attacking
var freq = this._midiToFreq(note); var freq = this._midiToFreq(note);
@ -124,9 +125,9 @@ AudioEngine.prototype.stopAllSounds = function () {
// for (var i = 0; i<this.drumSamplers.length; i++) { // for (var i = 0; i<this.drumSamplers.length; i++) {
// this.drumSamplers[i].triggerRelease(); // this.drumSamplers[i].triggerRelease();
// } // }
// stop sounds triggered with playSound (indexed by their urls) // stop sounds triggered with playSound
for (var key in this.soundSamplers) { for (var i=0; i<this.soundPlayers.length; i++) {
this.soundSamplers[key].triggerRelease(); this.soundPlayers[i].stop();
} }
// stop soundfont notes // stop soundfont notes
this.instrument.stop(); this.instrument.stop();
@ -171,8 +172,11 @@ AudioEngine.prototype.changeEffect = function (effect, value) {
AudioEngine.prototype._setPitchShift = function (value) { AudioEngine.prototype._setPitchShift = function (value) {
this.pitchShiftRatio = value; this.pitchShiftRatio = value;
for (var i in this.soundSamplers) { for (var i=0; i<this.soundPlayers.length; i++) {
this.soundSamplers[i].player.playbackRate = 1 + this.pitchShiftRatio; var s = this.soundPlayers[i];
if (s) {
s.playbackRate = 1 + this.pitchShiftRatio;
}
} }
}; };