scratch-audio/src/effects/PitchEffect.js
Eric Rosenbaum e1d478244d audioengine loads sounds indexed by md5
audioplayers store list of their own active sound players indexed by md5 of the sound. sound players are created when the sound is played, deleted when it ends, and removed from the list of active sound players. the pitch effect uses this list of active sound players to set their playback ratios.
2017-01-30 11:09:45 -05:00

45 lines
825 B
JavaScript

/*
A Pitch effect
*/
var Tone = require('tone');
function PitchEffect () {
this.value = 0;
this.ratio = 1;
this.tone = new Tone();
}
PitchEffect.prototype.set = function (val, players) {
this.value = val;
this.ratio = this.getRatio(this.value);
this.updatePlayers(players);
};
PitchEffect.prototype.changeBy = function (val, players) {
this.set(this.value + val, players);
};
PitchEffect.prototype.getRatio = function (val) {
return this.tone.intervalToFrequencyRatio(val / 10);
};
PitchEffect.prototype.updatePlayer = function (player) {
player.setPlaybackRate(this.ratio);
};
PitchEffect.prototype.updatePlayers = function (players) {
if (!players) return;
for (var md5 in players) {
this.updatePlayer(players[md5]);
}
};
module.exports = PitchEffect;