/*
* Add code to sample code 02
*/
// ....
var t2 = decay;
var t2Value = sustain;
// for legacy browsers
eg.gain.setTargetAtTime = eg.gain.setTargetAtTime ||
eg.gain.setTargetValueAtTime;
// Decay -> Sustain :
// gain gradually decreases to value of sustain
// during decay time (t2) from assigned time (t1)
eg.gain.setTargetAtTime(t2Value, t1, t2);
isStop = false;
// ....
/*
* Add code to sample code 05
*/
// ....
document.body.addEventListener('mouseup', function() {
if (isStop) {
return;
}
// oscillator.stop(0); // Unnecessary !!
var t3 = context.currentTime;
var t4 = release;
// for legacy browsers
eg.gain.setTargetAtTime = eg.gain.setTargetAtTime ||
eg.gain.setTargetValueAtTime;
// in the case of mouse up on the way of Attack or Decay
eg.gain.cancelScheduledValues(t3);
eg.gain.setValueAtTime(eg.gain.value, t3);
// Release :
// gain gradually decreases to 0
// during release time (t4) from assigned time (t3)
eg.gain.setTargetAtTime(0, t3, t4); // Release
intervalid = window.setInterval(function() {
var VALUE_OF_STOP = 1e-3;
if (eg.gain.value < VALUE_OF_STOP) {
// Stop sound
oscillator.stop(0);
if (intervalid !== null) {
window.clearInterval(intervalid);
intervalid = null;
}
isStop = true;
}
}, 0);
}, false);
window.AudioContext = window.AudioContext || window.webkitAudioContext;
// Create the instance of AudioContext
var context = new AudioContext();
// for the instance of OscillatorNode
var oscillator = null;
// for legacy browsers
context.createGain = context.createGain || context.createGainNode;
// Create the instance of GainNode (for Envelope Generator)
var eg = context.createGain();
// for legacy browsers
eg.gain.setTargetAtTime = eg.gain.setTargetAtTime ||
eg.gain.setTargetValueAtTime;
// Flag for starting or stopping sound
var isStop = true;
var attack = 0.5;
var decay = 0.3;
var sustain = 0.5;
var release = 1.0;
document.body.addEventListener('mousedown', function() {
if (!isStop) {
oscillator.stop(0);
}
// Create the instance of OscillatorNode
oscillator = context.createOscillator();
// for legacy browsers
oscillator.start = oscillator.start || oscillator.noteOn;
oscillator.stop = oscillator.stop || oscillator.noteOff;
// OscillatorNode (Input) -> GainNode (Envelope Generator) -> AudioDestinationNode (Output)
oscillator.connect(eg);
eg.connect(context.destination);
var t0 = context.currentTime;
// Start sound at t0
oscillator.start(t0);
// Start from gain = 0
eg.gain.setValueAtTime(0, t0);
var t1 = t0 + attack; // (at start) + (attack time)
var t2 = decay;
var t2Value = sustain;
// Attack : gain increases linearly until assigned time (t1)
eg.gain.linearRampToValueAtTime(1, t1);
// Decay -> Sustain :
// gain gradually decreases to value of sustain
// during decay time (t2) from assigned time (t1)
eg.gain.setTargetAtTime(t2Value, t1, t2);
isStop = false;
}, false);
document.body.addEventListener('mouseup', function() {
if (isStop) {
return;
}
// oscillator.stop(0); // Unnecessary !!
var t3 = context.currentTime;
var t4 = release;
// in the case of mouse up on the way of Attack or Decay
eg.gain.cancelScheduledValues(t3);
eg.gain.setValueAtTime(eg.gain.value, t3);
// Release :
// gain gradually decreases to 0
// during release time (t4) from assigned time (t3)
eg.gain.setTargetAtTime(0, t3, t4); // Release
intervalid = window.setInterval(function() {
var VALUE_OF_STOP = 1e-3;
if (eg.gain.value < VALUE_OF_STOP) {
// Stop sound
oscillator.stop(0);
if (intervalid !== null) {
window.clearInterval(intervalid);
intervalid = null;
}
isStop = true;
}
}, 0);
}, false);