RC RANDOM CHAOS

Deriving Music Theory From Scratch With a Web Audio Oscillator

· via Hacker News

Original source

Music Theory for Programmers

Hacker News →

A programmer who never grasped music theory through conventional lessons rebuilds it from physics and arithmetic instead of convention, using JavaScript and the browser’s Web Audio API. The premise is that a sound is just a number describing speaker-cone position roughly 44,000 times a second, and pitch is nothing more than how fast that number oscillates. A bare 440Hz sine wave demonstrates this — and the audible click when it stops abruptly motivates the next concept: an envelope, a second value controlling volume over time. Stretching the attack alone turns a struck note into a bowed one without changing pitch, showing that much of an instrument’s perceived character lives in its amplitude curve rather than its frequency.

From there the piece explains timbre through the harmonic series: a real string vibrates not just at its fundamental but simultaneously in halves, thirds, and quarters, stacking integer-multiple frequencies on top. The relative loudness of those overtones is what distinguishes a violin from a trumpet, and the browser’s four built-in waveforms (sine, triangle, square, sawtooth) are effectively four fixed harmonic recipes. The square wave’s hollow tone, for instance, comes from containing only odd harmonics.

The key theoretical payoff is the octave. Doubling a frequency produces a note so similar that cultures worldwide independently gave it the same name, because every harmonic of the higher note already exists in the lower note’s series. This yields two rules the author builds on: pitch is multiplicative rather than additive (up an octave means times two, so musical intervals are ratios and frequency space is logarithmic), and the entire problem of which pitches should exist collapses to how you divide a single octave. It’s a hands-on, code-first path toward deriving the twelve notes, scales, and chords without taking anything on faith.

Read the full article

Continue reading at Hacker News →

This is an AI-generated summary. Read the original for the full story.