16  The Convolver Node

In this chapter, you will learn how to use the convolver node. The convolver allows you to apply reverberation to node graph input sources by referencing a special kind of audio file called an impulse response.

Images  Convolution Reverb

When an acoustic sound is created, its characteristics are shaped by its immediate environment. This is due to sound waves bouncing off and around various obstacles. These obstacles can be made of different materials that affect the sound in different ways. The result of sound emanating from a small room has different characteristics than sound emanating from a large room. Because the human ear can hear these differences, when this information is transmitted to the brain, we perceive these characteristics as room ambience. Modern advancements in digital audio technology allow us to record the ambience of any real-world environment and apply it to any digital audio signal directly. These recorded ambiences are stored as a special file called an impulse response. An impulse response file is made by recording a single sound burst in an environment, which could be white noise, a sine wave sweep, or even a balloon pop. This recording is then run through a special digital algorithm to create a single file called an impulse response. This impulse response file is combined or convolved with another input source to give the targeted sound the spacial characteristics of the room that the impulse is modeled from.

The format of impulse response files can be any audio file type including WAV, MP3, AIFF, or OGG. However, to use them with the Web Audio API, impulse response files must be in a browser-compatible audio format. For this chapter, we use WAV files because they are of higher quality than MP3 files. And because impulse response files are small, load time is not a concern.

Images  Where to Get Pre-Recorded Impulse Response Files

There are many online resources where you can download impulse response files for free, such as: http://www.openairlib.net/.

Images  Using Impulse Response Files

To use impulse response files, you must first load them, decode them, and store them in a buffer.

var audioContext = new AudioContext();
var impulseResponseBuffer;
var getSound = new XMLHttpRequest();
getSound.open("get", "sounds/impulse.js", true); // impulse file
getSound.responseType = "arraybuffer";

getSound.onload = function() {
  audioContext.decodeAudioData(getSound.response, function(buffer) {
    impulseResponseBuffer = buffer;
  });
};
getSound.send();

After the file is stored in a buffer, the next step is to wire up the necessary nodes to apply the effect to an input source. To integrate the impulse response into the node graph configuration, you must first create a convolver node using audioContext.createConvolver() and store the returned object in a variable.

var convolver = audioContext.createConvolver();

You then assign the loaded impulse response buffer to the buffer property of the object.

convolver.buffer = impulseResponseBuffer;

Next, you connect any input source you want to the convolver node. Here is an example of connecting an oscillator.

var osc = audioContext.createOscillator();
var convolver = audioContext.createConvolver();
osc.type = "sawtooth";
convolver.buffer = impulseResponseBuffer;
osc.connect(convolver);
convolver.connect(audioContext.destination);
osc.start(audioContext.currentTime);

The following HTML and JavaScript code combines the impulse file loader, node graph connections, and JQuery DOM selectors to allow you to play the oscillator by clicking an HTML button and holding it. This allows you to hear the reverberation effect more explicitly because the reverb tail is audible after removing your finger from the mouse button and stopping the oscillator.

HTML

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title></title>
    <script type="text/javascript" src="https://ajax.googleapis.
      com/ajax/libs/jquery/2.1.0/jquery.js"></script>
    <script src="js/app.js"></script>
  </head>
  <body>
    <button>Oscillation</button>
  </body>
</html>

JavaScript

"use strict";
var audioContext = new AudioContext();
var impulseResponseBuffer;
var getSound = new XMLHttpRequest();
getSound.open("get", "sounds/impulse.wav", true);
getSound.responseType = "arraybuffer";

getSound.onload = function() {
  audioContext.decodeAudioData(getSound.response, function(buffer) {
    impulseResponseBuffer = buffer;
  });
};

getSound.send();

/*___________________________________________BEGIN playback 
  functionality*/

var osc = audioContext.createOscillator();

function playback() {
  var convolver = audioContext.createConvolver();
  osc = audioContext.createOscillator();
  osc.type = "sawtooth";
  convolver.buffer = impulseResponseBuffer;
  osc.connect(convolver);
  convolver.connect(audioContext.destination);
  osc.start(audioContext.currentTime);
}

$(function() {

  $("button").on("mousedown", function() {
    playback();
  });

  $("button").on("mouseup", function() {
    osc.stop();
  });
});

Images  Controlling the Amount of Reverberation

In the previous code example, the amount of reverb applied to the oscillator is fixed at 100 percent. If you want to make the effect variable, which allows you to control how much of the effect is applied to the input source, you can do so by splitting the input source with a gain node and routing one split to the convolver node prior to connecting it to the destination. You then connect the other split directly to the destination. You use gain.value to blend the amount of the effect you want to hear.

The following diagram and node graph configuration code demonstrate the splitting operation.

Images

var gain = audioContext.createGain();
var convolver = audioContext.createConvolver();

osc = audioContext.createOscillator();
osc.type = "sawtooth";
convolver.buffer = impulseResponseBuffer;
osc.connect(convolver);
convolver.connect(gain);
gain.gain.value = 0.2;
gain.connect(audioContext.destination);
osc.connect(audioContext.destination);
osc.start(audioContext.currentTime);

Images  Summary

In this chapter, you learned how to use the convolver node to apply an impulse response file to an input source. You also learned how to use gain nodes to control the amount of the effect you want to hear. In the next chapter, you will learn how to modify the panning of stereo input sources and how to create sophisticated routing schemes using the channel and merger nodes.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.139.233.245