Urgent.News

What's breaking now, across thousands of outlets.

Culture

Music theory for programmers

I have never been able to play a musical instrument. Despite trying multiple times, I struggled to produce the right sounds without understanding the underlying principles. My issue was not with practice but with the explanations of music theory that seemed to omit fundamental reasons behind the arrangement and patterns of notes.

It struck me that music, at its core, is deeply rooted in physics and mathematics. There are twelve notes for a reason, and the major scale's shape has significance. Chords that sound pleasing do so for specific reasons, which can be computed. This inspired me to start learning music from the ground up, and I began by writing code to explore the underlying concepts.

This article is the culmination of that journey. It begins with a single number changing over time and gradually leads to the derivation of the twelve notes, building scales and chords, and eventually crafting a chord progression that resembles actual music. No instrument is required, and no assumptions are made. Written notation is introduced at the end, after we have established a foundation.

Sound is essentially air pressure variations, and a speaker reproduces sound by moving its cone in and out rapidly. Audio is represented by a list of numbers describing the position of the cone 44,000 times per second. This list is generated and manipulated by the Web Audio API. The fundamental frequency, 440 Hz, is arbitrary—it is the pitch we have agreed to call "A."

Changing this value alters the pitch without affecting the overall structure. This is the only musical meaning of the number 440. The simplest waveform is a sine wave, which repeats at a frequency of 440 times per second. For example, by changing the frequency to 300 Hz, you obtain a different pitch without any issues. However, a click is heard at the end of the sound, not due to a browser bug but due to physics.

The oscillator was at the midpoint of its wave when it stopped, resulting in an abrupt pressure change—characteristic of a click. Mitigating this involves introducing a second number that changes over time, controlling the volume rather than the pitch. Musicians refer to this shape—as the sound moves from silence to silence—as an envelope.

The attack represents the rise, while the decay signifies the subsequent fall. A ten-millisecond fade-in followed by a slow decay results in a more pleasant listening experience compared to a sharp test tone. Modifying the attack duration from a few milliseconds to half a second transforms the note from a struck sound to a bowed one, without altering the pitch.

The provided envelope is simplified; the complete version includes ADSR (attack, decay, sustain, and release), where sustain represents the note's level while the key is pressed, and release describes the fading when the key is released. A helper function is employed throughout the subsequent examples. A single frequency generates a simple waveform, resulting in a hearing test-like sound distinct from any musical instrument.

However, when you pluck a guitar string tuned to 440 Hz, the string vibrates not only at its fundamental frequency but also at harmonics (880 Hz, 1320 Hz, 1760 Hz, and so on). These additional frequencies, known as the harmonic series, are superimposed on the fundamental frequency. The fundamental frequency is called the "fundamental," and the harmonic series consists of multiples of this frequency (1, 2, 3, 4, 5, etc.).

The above harmonic series of 220 Hz is illustrated, where clicking a bar reveals the harmonic played alone, and clicking multiple bars simultaneously reveals all harmonics together. Collectively, they form a complex sound, with each harmonic's relative loudness contributing to the distinctive timbre or tone of an instrument. For instance, a square wave contains only odd harmonics, yielding a hollow and slightly electronic sound.

In contrast, a sawtooth wave incorporates all harmonics, producing a harsh and buzzy tone. The harmonic series of 220 Hz is visualized, with each bar representing a click on individual harmonics, resulting in different timbres. These harmonics are inherent and unavoidable, arising from the physics of vibrating strings and columns of air.

They are consistent across instruments built upon these principles. The following five notes are presented, each with a frequency double that of the preceding note. Despite being different pitches, they are perceived as the same note—a phenomenon rooted in arithmetic and fixed by the physics of vibrating strings and air columns.

Across cultures with no contact, people have independently identified this doubling relationship, naming it an octave. The octave's significance extends beyond mere naming; it arises from the harmonic series. Every harmonic of 440 Hz is already present in the harmonic series of 220 Hz because the series of 220 Hz includes all multiples of 220 (220, 440, 660, 880, etc.).

This arithmetic relationship underpins the concept of the octave, a fundamental building block in music theory.

Written by urgent.news from Lobsters's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at runjs.app →

More in Culture

More from Friday 21 August →