Music theory derived from a single number, in JavaScript

Music theory derived from a single number, in JavaScript

The author of a blog post on runjs.app describes never having been able to play an instrument, despite repeated attempts, and traces the failure to how music theory is usually taught: rules to memorise (this scale, this pattern) with no explanation of why those particular notes or shapes exist. Since the system is built on physics and arithmetic, the author set out to derive it from first principles by writing code, starting from a single number that changes over time and working, by the article's own account, toward the twelve notes, scales, chords built from arrays, and a full chord progression, with written musical notation deliberately withheld until there is something for it to describe.

The first step treats sound as nothing more than a number: a speaker cone position that must be produced roughly forty-four thousand times a second. A Web Audio API oscillator generates a sine wave repeating 440 times a second, the frequency conventionally named "A" and used as the system's tuning reference; changing it to 300 just produces a different pitch, since at this level there is no concept of a musical note yet, only a number. Frequency is pitch, full stop.

Stopping that oscillator abruptly produces an audible click, caused by the speaker cone snapping back from mid-travel; the fix is a second changing number, a volume envelope, that ramps gain up over about ten milliseconds and then decays exponentially. Stretching that attack out to roughly half a second turns a struck-sounding note into a bowed-sounding one without moving the frequency at all, which the author uses to argue that the envelope, not the frequency, is often doing the real work. The article names the full four-stage version of this shape, attack, decay, sustain and release, as ADSR.

A sine wave is a single frequency and nothing else, which the piece says is why it sounds like a hearing-test tone rather than an instrument. A plucked string at 440Hz also vibrates in halves, thirds and quarters at the same time, adding harmonics at 880, 1320 and 1760Hz and upward, a stack called the harmonic series (the fundamental multiplied by 1, 2, 3, 4 and on). The relative loudness of those harmonics is timbre; the browser's four built-in oscillator shapes, sine, triangle, square and sawtooth, are shown played at the same 220Hz to demonstrate it, with the square wave (odd harmonics only) sounding hollow and the sawtooth (all harmonics) sounding harsh and buzzy.

The piece then shows five notes at 110, 220, 440, 880 and 1760Hz, each double the last, and notes that cultures with no contact with each other independently treat doubled frequencies as the same note, called the octave in Western notation. The harmonic series explains why: every harmonic of 440Hz is already present in 220Hz's own series, so the higher note adds no new frequency. Two consequences follow, per the author: pitch is multiplicative rather than additive (frequency space is logarithmic, and every musical interval is a ratio), and solving how to divide one octave solves the problem for the entire audible range.

On dividing the octave, the article argues that simple frequency ratios sound better than complex ones: the octave (2/1), the fifth (3/2), the fourth (4/3) and the major third (5/4) sound like chords, a semitone ratio of 16/15 sounds like "two notes arguing", and an irrational ratio close to the square root of two sounds like a car alarm. Two physical reasons are given. First, harmonic overlap: 220Hz and 330Hz (a 3:2 ratio) share harmonics exactly at 660 and 1320Hz, while 220Hz and 311Hz (close to a square-root-of-two ratio) share none. Second, beating: two close but non-identical frequencies drift in and out of phase, producing an audible volume pulse at a rate equal to their difference (frequencies six hertz apart beat six times a second). The combined waveform of a simple ratio like 2:1 or 3:2 repeats almost immediately; a 16:15 ratio takes fifteen cycles to repeat; an irrational ratio never repeats at all. The author's summary: consonance is the ear finding a repeating pattern quickly.

The article then tries to build an entire note alphabet using only octaves and the 3/2 fifth, stacking twelve fifths in a row and folding each result back into the octave whenever it overshoots, a method the author describes as roughly what Pythagoras did and says works well for a while. Starting from 220Hz and applying twelve fifths lands on 222.99Hz rather than back on 220Hz exactly, close enough to sound like the same note and far enough off to be unusable. The author states plainly that this mismatch is not a rounding error but something structural, and the retrieved text breaks off mid-example immediately after that line, before the piece reaches the twelve notes, scales, chords or chord progression it set out to build, and without naming any specific tuning system as the resolution.

Key facts

  • Unable to play an instrument despite repeated attempts, the author derives music theory from scratch in JavaScript using the Web Audio API instead of memorising rules.
  • Frequency alone is pitch: a 440Hz oscillator is the conventional tuning reference "A", and a volume envelope (about a 10ms attack, then decay) is what separates a struck-sounding note from a bowed one at the same frequency.
  • Doubling a frequency (110, 220, 440, 880, 1760Hz) sounds like the same note across cultures because every harmonic of the higher note is already present in the lower note's own harmonic series; this is the octave.
  • Simple ratios sound consonant because their harmonics overlap exactly and their combined waveform repeats quickly (a 3:2 fifth almost instantly, a 16:15 semitone after fifteen cycles), while an irrational ratio close to the square root of two never repeats and sounds dissonant.
  • Stacking twelve pure 3/2 fifths from 220Hz and folding into the octave lands on 222.99Hz rather than exactly 220Hz, a mismatch the author calls structural rather than a rounding error; the captured text cuts off there, before the article reaches scales, chords or a chord progression.

Why it matters

It is a first-principles rebuttal of how music theory is usually taught: instead of "here is the scale, memorise it", the piece derives the twelve-note system, octaves and consonant intervals from acoustics and arithmetic that a reader can run and hear at each step.

Who it affects

Programmers and other technically minded people who bounced off conventional music theory or an instrument and want a from-first-principles route in; also useful to anyone curious about how the Web Audio API generates and shapes sound. It is not aimed at people who already have formal music training.

How to use it

The material is a free blog post on runjs.app with runnable inline JavaScript snippets (AudioContext, oscillator, gain envelope) that a reader can execute directly in a browser to hear each concept; no price, account or installation is involved.

How solid is it

The acoustics it relies on, the harmonic series, ratio-based consonance and beating, are standard physics, and the article backs each claim with a short program the reader can run and hear rather than asking for trust. The text retrieved for this summary cuts off mid-example, right after the twelve-fifths mismatch is introduced and before the article reaches the scales, chords and chord progression it promises, so how it resolves that mismatch cannot be confirmed here.

Risks and caveats

The article gives no author name and no publication date in its text. Because the available excerpt ends before the piece's stated destination, this retelling cannot say which tuning system, if any, the article ultimately proposes to fix the twelve-fifths mismatch; readers who want that resolution need to read the source in full.

“Consonance is your ear finding a repeating pattern quickly.”

— the article's author