window.AudioContext = window.AudioContext || window.webkitAudioContext;
// Create the instance of AudioContext
var context = new AudioContext();
// Create the instance of OscillatorNode
var oscillator = context.createOscillator(); // for Input
// for legacy browsers
oscillator.start = oscillator.start || oscillator.noteOn;
oscillator.stop = oscillator.stop || oscillator.noteOff;
// Create the instance of BiquadFilterNode
var filter = context.createBiquadFilter();
window.AudioContext = window.AudioContext || window.webkitAudioContext;
// Create the instance of AudioContext
var context = new AudioContext();
// Create the instance of OscillatorNode
var oscillator = context.createOscillator(); // for input
// for legacy browsers
oscillator.start = oscillator.start || oscillator.noteOn;
oscillator.stop = oscillator.stop || oscillator.noteOff;
var MAXIMUM_STAGES = 24; // The maximun number of All-Pass Filters
var filters = new Array(MAXIMUM_STAGES); /** @type {Array.<BiquadFilterNode>} */
var numberOfStages = 4; // The number of using All-Pass Filters
// Create the instance of BiquadFilterNode
for (var i = 0; i < MAXIMUM_STAGES; i++) {
// Create the instance of BiquadFilterNode
filters[i] = context.createBiquadFilter();
// Use All-Pass Filter
filters[i].type = (typeof filters[i].type === 'string') ? 'allpass' : 7;
}
/*
* Add code to sample code 04
*/
// ....
// Create the instance of OscillatorNode
var lfo = context.createOscillator(); // for LFO
// for legacy browsers
lfo.start = lfo.start || lfo.noteOn;
lfo.stop = lfo.stop || lfo.noteOff;
// Create the instance of GainNode
var depth = context.createGain(); // for LFO
// Connect nodes for LFO that changes frequency periodically
// OscillatorNode (LFO) -> GainNode (Depth) -> frequency (AudioParam) x N
lfo.connect(depth);
for (var i = 0; i < MAXIMUM_STAGES; i++) {
depth.connect(filters[i].frequency);
}
window.AudioContext = window.AudioContext || window.webkitAudioContext;
// Create the instance of AudioContext
var context = new AudioContext();
// 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 (Phaser) sound
var depth = context.createGain(); // for LFO
var MAXIMUM_STAGES = 24; // The maximun number of All-Pass Filters
var filters = new Array(MAXIMUM_STAGES); /** @type {Array.<BiquadFilterNode>} */
var numberOfStages = 4; // The number of using All-Pass Filters
// Create the instance of BiquadFilterNode
for (var i = 0; i < MAXIMUM_STAGES; i++) {
// Create the instance of BiquadFilterNode
filters[i] = context.createBiquadFilter();
// Use All-Pass Filter
filters[i].type = (typeof filters[i].type === 'string') ? 'allpass' : 7;
}
// Clear connection
oscillator.disconnect(0);
for (var i = 0; i < MAXIMUM_STAGES; i++) {
filters[i].disconnect(0);
}
mix.disconnect(0);
// Connect nodes for original sound
// OscillatorNode (Input) -> AudioDestinationNode (Output)
oscillator.connect(context.destination);
if (numberOfStages > 0) {
// Phaser ON
// Connect nodes for effect (Phaser) sound
// OscillatorNode (Input) -> BiquadFilterNode (All-Pass Filter) x N -> GainNode (Mix) -> AudioDestinationNode (Output)
oscillator.connect(filters[0]);
for (var i = 0; i < numberOfStages; i++) {
if (i < (numberOfStages - 1)) {
filters[0].connect(filters[i + 1]);
} else {
filters[i].connect(mix);
// Connect nodes for Feedback
// (OscillatorNode (Input) ->) BiquadFilterNode (All-Pass Filter) x N -> GainNode (Feedback) -> BiquadFilterNode (All-Pass Filter) x N -> ...
filters[i].connect(feedback);
feedback.connect(filters[0]);
}
}
mix.connect(context.destination);
} else {
// Phaser OFF
}
// Connect nodes for LFO that changes frequency periodically
// OscillatorNode (LFO) -> GainNode (Depth) -> frequency (AudioParam) x N
lfo.connect(depth);
for (var i = 0; i < MAXIMUM_STAGES; i++) {
depth.connect(filters[i].frequency);
}
// Set parameters for Phaser
for (var i = 0; i < MAXIMUM_STAGES; i++) {
// Set Base Value
filters[i].frequency.value = 350; // 350 Hz
}
// This value must be less than or equal to 1.0 (100%)
var depthRate = 0.5; // 50 %
// Set Depth
depth.gain.value = filters[i].frequency.value * depthRate; // 350 Hz +- 175 (350 * 0.5) Hz
// Set Rate
lfo.frequency.value = 1; // 1 Hz
// Set Mix
mix.gain.value = 0.5;
// Set Feedback
feedback.gain.value = 0.5;
// Start sound
oscillator.start(0);
// Effector (Phaser) ON
lfo.start(0);