/*
* Add code to sample code 01
*/
// This value must be less than or equal to 1.0 (100%)
var depthRate = 0.5; // 50 %
// Set Base Value
delay.delayTime.value = 0.020; // 20 msec (Base Value)
// Set Depth
depth.gain.value = delay.delayTime.value * depthRate; // 20 msec +- 10 (20 * 0.5) msec
// ....
最後に, 整理したコードを記載しておきます.
サンプルコード 05
window.AudioContext = window.AudioContext || window.webkitAudioContext;
// Create the instance of AudioContext
var context = new AudioContext();
// for legacy browsers
context.createDelay = context.createDelay || context.createDelayNode;
// Create the instance of DelayNode
var delay = context.createDelay();
// Create the instance of OscillatorNode
var oscillator = context.createOscillator(); // for Input
var lfo = context.createOscillator(); // for LFO
// for legacy browsers
oscillator.start = oscillator.start || oscillator.noteOn;
oscillator.stop = oscillator.stop || oscillator.noteOff;
lfo.start = lfo.start || lfo.noteOn;
lfo.stop = lfo.stop || lfo.noteOff;
// for legacy browsers
context.createGain = context.createGain || context.createGainNode;
// Create the instance of GainNode
var mix = context.createGain(); // for effect (Chorus) sound
var depth = context.createGain(); // for LFO
// Connect nodes for original sound
// OscillatorNode (Input) -> AudioDestinationNode (Output)
oscillator.connect(context.destination);
// Connect nodes for effect (Chorus) sound
// OscillatorNode (Input) -> DelayNode (Delay) -> GainNode (Mix) -> AudioDestinationNode (Output)
oscillator.connect(delay);
delay.connect(mix);
mix.connect(context.destination);
// Connect nodes for LFO that changes Delay Time periodically
// OscillatorNode (LFO) -> GainNode (Depth) -> delayTime (AudioParam)
lfo.connect(depth);
depth.connect(delay.delayTime);
// This value must be less than or equal to 1.0 (100%)
var depthRate = 0.5; // 50 %
// Set Base Value
delay.delayTime.value = 0.020; // 20 msec (Base Value)
// Set Depth
depth.gain.value = delay.delayTime.value * depthRate; // 20 msec +- 10 (20 * 0.5) msec
// Set Rate
lfo.frequency.value = 0.05; // 0.05 Hz
// Set Mix
mix.gain.value = 0.5;
// Start sound
oscillator.start(0);
// Effector (Chorus) ON
lfo.start(0);