Copyright 2014 Alan deLespinasse
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
15import { releaseAudioNode } from './helpers.mjs';
17var reverbGen = {};
Generates a reverb impulse response.
@tags internals @param {!Object} params TODO: Document the properties. @param {!function(!AudioBuffer)} callback Function to call when the impulse response has been generated. The impulse response is passed to this function as its parameter. May be called immediately within the current execution context, or later.
27reverbGen.generateReverb = function (params, callback) { 28 var audioContext = params.audioContext || new AudioContext(); 29 var sampleRate = audioContext.sampleRate; 30 var numChannels = params.numChannels || 2; 31 // params.decayTime is the -60dB fade time. We let it go 50% longer to get to -90dB. 32 var totalTime = params.decayTime * 1.5; 33 var decaySampleFrames = Math.round(params.decayTime * sampleRate); 34 var numSampleFrames = Math.round(totalTime * sampleRate); 35 var fadeInSampleFrames = Math.round((params.fadeInTime || 0) * sampleRate); 36 // 60dB is a factor of 1 million in power, or 1000 in amplitude. 37 var decayBase = Math.pow(1 / 1000, 1 / decaySampleFrames); 38 var reverbIR = audioContext.createBuffer(numChannels, numSampleFrames, sampleRate); 39 for (var i = 0; i < numChannels; i++) { 40 var chan = reverbIR.getChannelData(i); 41 for (var j = 0; j < numSampleFrames; j++) { 42 chan[j] = randomSample() * Math.pow(decayBase, j); 43 } 44 for (var j = 0; j < fadeInSampleFrames; j++) { 45 chan[j] *= j / fadeInSampleFrames; 46 } 47 } 48 49 applyGradualLowpass(reverbIR, params.lpFreqStart || 0, params.lpFreqEnd || 0, params.decayTime, callback); 50};
Creates a canvas element showing a graph of the given data.
@tags internals @param {!Float32Array} data An array of numbers, or a Float32Array. @param {number} width Width in pixels of the canvas. @param {number} height Height in pixels of the canvas. @param {number} min Minimum value of data for the graph (lower edge). @param {number} max Maximum value of data in the graph (upper edge). @return {!CanvasElement} The generated canvas element.
61reverbGen.generateGraph = function (data, width, height, min, max) { 62 var canvas = document.createElement('canvas'); 63 canvas.width = width; 64 canvas.height = height; 65 var gc = canvas.getContext('2d'); 66 gc.fillStyle = '#000'; 67 gc.fillRect(0, 0, canvas.width, canvas.height); 68 gc.fillStyle = '#fff'; 69 var xscale = width / data.length; 70 var yscale = height / (max - min); 71 for (var i = 0; i < data.length; i++) { 72 gc.fillRect(i * xscale, height - (data[i] - min) * yscale, 1, 1); 73 } 74 return canvas; 75};
Applies a constantly changing lowpass filter to the given sound.
@private @param {!AudioBuffer} input @param {number} lpFreqStart @param {number} lpFreqEnd @param {number} lpFreqEndAt @param {!function(!AudioBuffer)} callback May be called immediately within the current execution context, or later. @tags internals
88var applyGradualLowpass = function (input, lpFreqStart, lpFreqEnd, lpFreqEndAt, callback) { 89 if (lpFreqStart == 0) { 90 callback(input); 91 return; 92 } 93 var channelData = getAllChannelData(input); 94 var context = new OfflineAudioContext(input.numberOfChannels, channelData[0].length, input.sampleRate); 95 var player = context.createBufferSource(); 96 player.buffer = input; 97 var filter = context.createBiquadFilter(); 98 99 lpFreqStart = Math.min(lpFreqStart, input.sampleRate / 2); 100 lpFreqEnd = Math.min(lpFreqEnd, input.sampleRate / 2); 101 102 filter.type = 'lowpass'; 103 filter.Q.value = 0.0001; 104 filter.frequency.setValueAtTime(lpFreqStart, 0); 105 filter.frequency.linearRampToValueAtTime(lpFreqEnd, lpFreqEndAt); 106 107 player.connect(filter); 108 filter.connect(context.destination); 109 player.start(); 110 context.oncomplete = function (event) { 111 callback(event.renderedBuffer); 112 releaseAudioNode(filter); 113 releaseAudioNode(player); 114 }; 115 context.startRendering(); 116 117 window.filterNode = filter; 118};
@private @param {!AudioBuffer} buffer @return {!Array.<!Float32Array>} An array containing the Float32Array of each channel's samples.
@private @return {number} A random number from -1 to 1.
137export default reverbGen;