
Music Theory for Programmers: Deriving the Twelve Notes in JavaScript
A RunJS blog post rebuilds music theory from first principles in code: oscillators, harmonic ratios and tuning bugs lead to the twelve notes, scales and chords.
A post on the RunJS blog sets out to teach music theory the way an engineer would learn it: by deriving everything from a single number changing over time, using plain JavaScript and the Web Audio API in the browser.
The author opens with a confession — repeated attempts to learn an instrument that never got past making roughly the right noises — and his diagnosis of why they failed: explanations of theory presented the result and skipped the cause. "Here is a staff. This is a major scale, memorise the pattern. Why those notes? Because that is the convention," he writes. The article instead starts from a sine wave and never asks the reader to take anything on faith.
From a sine wave to the twelve notes
Sound is air pressure over time, so an oscillator at 440Hz is the only musical fact the piece needs to begin: frequency is pitch, and 440 is merely the number the world agreed to call A. The first real complication is physical rather than musical — stopping an oscillator mid-wave produces a click, which the post fixes with a volume envelope, the attack and decay curve that separates a test tone from something worth hearing twice.
Timbre follows from the harmonic series: a string tuned to 440Hz also vibrates in halves, thirds and quarters, adding quiet frequencies at 880, 1320, 1760 and beyond. That recipe of harmonics, not the fundamental, is what makes a violin sound unlike a trumpet. From there the article derives the octave — doubling the frequency gives the same note — and then consonance: the simpler the ratio, the better the pair sounds, because simple ratios share more harmonics and beat less.
The bug in the tuning system
Stacking twelve pure fifths overshoots seven octaves by a factor of 1.0136 — the Pythagorean comma, about 23 cents. It cannot be fixed, and the post gives the reason a programmer will recognise: closing the loop would require 3^n to equal 2^(n+m), and a power of three never equals a power of two. Equal temperament is the compromise — divide the octave into twelve equal steps of the twelfth root of two. A brute-force search over divisions of the octave shows why twelve: it is the first number whose best fifth is accurate to about a tenth of a percent.
Scales, chords and what remains
After that, everything becomes arrays. A scale is a list of gaps — the major scale is [2,2,1,2,2,2,1] — the Greek modes are the same array rotated, and a triad is every other note ([0,2,4]) because notes two scale degrees apart already share harmonics. Major and minor, the article notes, differ by one semitone in a single element. Roman numerals turn out to be relative addressing, which is why one chord chart works in every key.
The author ends by listing what he still does not understand, rhythm and voice leading among them, and points readers to Eevee's "Music Theory for Nerds", the LightNote site and MDN's Web Audio documentation. The whole article is one tradition's answer, he concedes — but it is no longer trivia to memorise.
SiTech — AI-powered web development
We build fast, modern websites and bring AI into real business workflows. Have a project or a question? We'd love to help.