Skip to content

Instantly share code, notes, and snippets.

@rjbultitude
Created June 5, 2017 19:59
Show Gist options
  • Save rjbultitude/279fc4758ee6059a2159d9f63c0af758 to your computer and use it in GitHub Desktop.
Save rjbultitude/279fc4758ee6059a2159d9f63c0af758 to your computer and use it in GitHub Desktop.
p5.SoundFile.prototype.setVolume = function (vol, rampTime, tFromNow, volFrom, volTo) {
if (typeof vol === 'number') {
var rampTime = rampTime || 0;
var tFromNow = tFromNow || 0;
var now = p5sound.audiocontext.currentTime;
var currentVol = this.output.gain.value;
this.output.gain.cancelScheduledValues(now + tFromNow);
// Attack
if (typeof volFrom === 'number' && volFrom >= 0) {
this.output.gain.linearRampToValueAtTime(volFrom, now + tFromNow);
} else {
this.output.gain.linearRampToValueAtTime(currentVol, now + tFromNow);
}
// Decay
if (typeof volTo === 'number' && volTo >= 0) {
this.output.gain.linearRampToValueAtTime(volTo, now + tFromNow + rampTime);
} else {
this.output.gain.linearRampToValueAtTime(vol, now + tFromNow + rampTime);
}
} else if (vol) {
vol.connect(this.output.gain);
} else {
// return the Gain Node
return this.output.gain;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment