Lecture 13 - Audio Reactivity

Published

October 27, 2026

For the past few classes, we have been looking at different ways to add complexity to our work. We have looked at randomness and noise as a way to approximate complex systems, we have looked at simulation to mimic them, now we are just going to the source and getting some real complex, messy data. Specifically, we are going to make some audio reactive visualizations.

Sound

What we perceive as sound is really just changes in air pressure. Our ears have a membrane that is moved by the change in pressure and those vibrations are transformed into signal that our brain perceives as sound. A speaker is the inverse of this. A basic speaker’s main component is a cone, this is a membrane with a magnetic rod at the center. We use an electric signal to induce a magnetic field that moves the rod back and forth. This causes the membrane to move, pushing air.

It is rapid changes in the air pressure that we perceive, not the pressure itself. The important pieces are the amplitude and the frequency of those changes. The amplitude contributes to our perception of loudness and the the frequency provides the pitch. Most sounds that we are hear are a combination of a lot of different frequencies at different amplitudes.

amplitude

Our sensitivity to loudness is logarithmic, so we use something called decibels (dB) to measure it. It is not a unit of measure, it is a ratio that tells us something about how much larger or smaller the amplitude is compared to a reference point. We use it is a collection of different ways.

dB SPL (Sound Pressure Level) is an actual measurement of sound pressure, which we get by having an absolute floor that we use as a reference point.

  • 0 dB SPL - lowest limit of hearing (not zero sound)
  • 20-30 dB SPL - quiet library
  • 60 db SPL - conversational level
  • 85 db SPL - occupational safety limit (heavy traffic, blenders) can lead to hearing loss
  • 110-120 db SPL - rock concert, chain saws, threshold of discomfort
  • 120 - 130 dB SPL - jet engine, gunshot, risk of immediate permanent damage

We can also talk about dB just as a pure ratio. “Turn that up by 10 dB”. This is 10 times the energy, but we will perceive it as about twice as loud.

For digital audio, our reference level is the maximum value that the system can represent. Since the ratio is then \(\frac{current-value}{max-value}\), the result will be \(\le 1\), so the log will be negative.

frequency

The range of human hearing is typically given as a range from 20Hz to 20kHz.

As with loudness, we perceive pitch logarithmically as well, because we are responding to the ratio between frequencies.

An octave represents a doubling of the frequency. We perceive each octave as spanning the same number of pitches. - lowest octave: 20Hz -> 40Hz - middle octave: 330 Hz -> 660 Hz - Highest octave: 10K -> 20K

So, the lowest octave is about .1% of the spectrum, while the highest one is 50%.

representing music

Sound files are really just a long list of amplitude values. We get this through the process of sampling the changes in air pressure which is a continuous phenomenon.

The sampling rate determines which frequencies we can capture. In general, we need to sample twice as often as the highest frequency we want to capture, so CDs are sampled at 44.1kHz, which can capture frequencies up to 22.05 kHz.

Audio reactivity

In general, we will have access to both the amplitude and the frequency information. Amplitude is very straightforward, because this is just the sample we are on. Frequency is harder because we can’t tell the frequency just by looking at a single sample. We need to look at a window of samples. Fortunately, we have a mathematic tool called the Fast Fourier Transform (FFT) that is capable of moving our sound into frequency space, which breaks out the amplitudes of all of the audible frequencies.

We have two basic approaches to reactivity.

  • map one of our measures into visual representations
  • use events in the sound to trigger effects

Tone.js

We are using a library called Tone.js. The library is a powerful tool for music synthesis, but we are going to ignore all of its synth capabilities and focus on its analysis tools.

Tone.js, like many audio tools, is really modeled on physical sound systems, so the mental model you have should be of physical boxes that we are wiring together.

We will look at five “tools” now and then in the tutorial you will meet a couple more.

  • Tone.Player- this reads a music file and outputs the sound
  • Tone.Meter - this takes sound input and returns the amplitude of the sound at this moment
  • Tone.FFT - this takes a sound input and returns an array of amplitude values in frequency bins
  • Tone.Waveform - this takes a sound input and returns a snapshot of the samples leading up to this point
  • Tone.MultibandSplit - this takes a sound input and breaks it into three frequency bins, low, mid and high

Player

let player;

function setup() {
    createCanvas(400, 400);

    player = new Tone.Player(
        "assets/late-night.mp3"
    ).toDestination();

}

function draw() {
    if (player.state === "started") {
        background("#00ff00");
    } else {

        background(0);
    }
}

function mousePressed() {
    if (player.state === "started") {
        player.stop();
    } else {
        player.start();
    }
}

With the player, we can load in a file and we can start and stop it.

notes:

  • toDestination is what routes the signal to the output so we can hear it
  • the state are strings, which is easy to read, but not great software engineering – they should have named constants in the library for that
  • we need to have some kind of user interaction to start the player. This is enforced by the browser to make sure we don’t click on a link and suddenly get assaulted by audio

Meter

let player;
let meter;

function setup() {
    createCanvas(400, 400);
    player = new Tone.Player(
        "assets/Die Interimsliebenden.m4a"
    ).toDestination();
    meter = new Tone.Meter();
    player.connect(meter);
}

function draw() {
    background(0);
    if (player.state === "started") {
        const amplitude = meter.getValue();

        const h = map(constrain(amplitude, -60, 0), -60, 0, 0, 400);

        fill("#00ff00");
        rect(width/2 - 50, height - h, 100, h);
    }
}

function mousePressed() {
    if (player.state === "started") {
        player.stop();
    } else {
        player.start();
    }
}

There are a couple of things happening in here

We are using connect to connect the meter to the output of the player.

Connection path

We use meter.getValue() to get the current amplitude

The value that we get out of this is in decibels, so it will range from -Infinity to 0. We are setting the floor at -60, and using constrain to just cap it off. We use map to convert that into a useful value.

The amplitude is very noisy. Tone.js has a built in smoother that will average a collection of input together. we can set it to a value between 0 (no smoothing) to 1 (average everything)

meter.smoothing = .95

Some other tips:

  • We can use lerp to do our own smoothing
const target = meter.getValue();
current = lerp(current, target, .2);

This will move us towards the new value, but won’t commit fully, which will smooth out some of the strong bursts that can happen.

  • Instead of mapping to size (like I did here), we can map to velocity or force so as things have high energy our shapes will move fast and low energy will let them coast down to lower speeds

Waveform

let player;
let waveformMeter;

function setup() {
    createCanvas(1024, 400);
    player = new Tone.Player(
        "assets/Die Interimsliebenden.m4a"
    ).toDestination();
     waveformMeter = new Tone.Waveform(1024);

    player.connect(waveformMeter);

}

function draw() {
    background(0);
    noFill();
    stroke("#00ff00");
    if (player.state === "started") {
        const waveform = waveformMeter.getValue();

        push();
        translate(0, height/2);
        beginShape();

        for (let i = 0; i < waveform.length-1; i++){
            const h = map(waveform[i], -1, 1, -200, 200);

            vertex(i, h);
        }
      
        endShape();
        pop();

    }
}

function mousePressed() {
    if (player.state === "started") {
        player.stop();
    } else {
        player.start();
    }
}

The key difference here is that we are getting an array of values. The number is controlled by the value we pass into the constructor (which must be a power of 2).

The samples themselves will range between -1 and 1.

FFT

let player;
let fft;

function setup() {
    createCanvas(1024, 400);
    player = new Tone.Player(
        "assets/Sequence.mp3"
    ).toDestination();
    fft = new Tone.FFT(1024);

    player.connect(fft);

}

function draw() {
    background(0);
    noFill();
    stroke("#00ff00");
    if (player.state === "started") {
        const freqs = fft.getValue();

        for (let i = 0; i < freqs.length - 1; i++) {
            if (isFinite(freqs[i])) {
                const top = map(constrain(freqs[i], -60, 0), -60, 0, height, 0);

                line(i, height, i, top);
            }
        }
    }
}

function mousePressed() {
    if (player.state === "started") {
        player.stop();
    } else {
        player.start();
    }
}

This allows us to look at the amount of energy in each frequency. I switched songs so we can see the frequency sweeps.

Here we can really see how concentrated the frequencies we listen to are in the low end of the spectrum.

The number we pass in again control the length of the output (again, it must be a power of 2). The larger the number, the more buckets we get, and the more processing is required.

MultibandSplit

The MultibandSplit will allow us to filter the data into three streams

let player;
let lowMeter, midMeter, highMeter;

function setup() {
    createCanvas(500, 400);
    player = new Tone.Player(
        "assets/Die Interimsliebenden.m4a"
    );

    // create the meters
    lowMeter = new Tone.Meter({ smoothing: 0.94 });
    midMeter = new Tone.Meter({ smoothing: 0.94 });
    highMeter = new Tone.Meter({ smoothing: 0.94 });

    // create the splitter
    splitter = new Tone.MultibandSplit({
        lowFrequency: 250,
        highFrequency: 2500
    });

    // connect up the components
    splitter.low.connect(lowMeter);
    splitter.mid.connect(midMeter);
    splitter.high.connect(highMeter);

    splitter.low.toDestination();
    splitter.mid.toDestination();
    splitter.high.toDestination();


    player.connect(splitter);

}

function draw() {
    background(0);
    noFill();
    stroke("#00ff00");
    if (player.state === "started") {
        push();
        translate(100, 0);
        drawMeter(lowMeter);

        translate(150, 0);
        drawMeter(midMeter);

        translate(150, 0);
        drawMeter(highMeter);

        pop();
       
    }
}

function drawMeter(meter){
    const amplitude = meter.getValue();

        const h = map(constrain(amplitude, -60, 0), -60, 0, 0, 400);

        fill("#00ff00");
        rect(-50, height - h, 100, h);

}

function mousePressed() {
    if (player.state === "started") {
        player.stop();
    } else {
        player.start();
    }
}

The options that we pass the the MultibandSplit at the low and high cut offs

  • everything under lowThreshold will be in the low output
  • everything over highThreshold will be in the high output
  • everything else will be in mid

The numbers were chosen to roughly capture the ranges that popular music falls into

  • < 250 Hz - bass and kicks
  • 250 Hz - 2500 Hz - guitars, voice
  • 2500 Hz - hi-hats

We can plug the waveform or FFT into the splitter as well if we like.

Note that I have also put toDestination on the three outputs instead of the player, so we can hear what each path is capturing.