Brifing

Programming Music Theory from First Principles

Learn music theory through code and physics

1The Problem with Traditional Music Theory

Traditional music theory often teaches conventions without explaining the underlying physics and arithmetic. This approach leaves learners memorizing patterns like the major scale without understanding why those notes were chosen. The article argues that music theory should be taught from first principles, starting with the physics of sound and building up to musical concepts through logical derivation.

2Sound as Air Pressure Waves

Sound is fundamentally air pressure wobbling over time. A speaker produces sound by moving its cone in and out, controlled by a list of numbers representing the cone's position 44,000 times per second. This digital representation forms the basis of all computer audio, whether stored in files or generated in real-time by synthesizers.

3Creating Basic Audio with Web API

The Web Audio API allows generating sound directly in browsers. A simple sine wave oscillator can be created with just a few lines of JavaScript, setting a frequency like 440Hz (concert pitch A). This demonstrates how pitch directly corresponds to frequency - higher numbers produce higher notes, forming the most basic building block of music.

4The Importance of Envelopes

Raw oscillators produce harsh, clicking sounds due to instant pressure changes. Envelopes solve this by controlling volume over time through attack (fade in), decay (fade out), sustain (held volume), and release (final fade). These parameters transform mathematical tones into musical notes, with different envelope shapes mimicking various playing techniques like bowing versus plucking.

5Understanding Timbre

Real instruments sound richer than pure sine waves because they produce harmonic overtones - integer multiples of the fundamental frequency. These harmonics form specific patterns that define an instrument's timbre. The browser offers basic waveform types (sine, triangle, square, sawtooth) that approximate different harmonic content.

6From Physics to Musical Notes

The article explains how the twelve-tone system emerges naturally from physics. When frequencies are doubled (octaves) and divided using simple ratios, the familiar notes of Western music appear. This mathematical foundation explains why certain intervals sound consonant while others are dissonant.

7Building Scales Programmatically

Musical scales can be constructed by selecting specific frequency ratios from the harmonic series. The major scale's particular pattern of whole and half steps corresponds to simple mathematical relationships between frequencies. This approach reveals why scales have their characteristic sounds and emotional qualities.

8Creating Chords with Arrays

Chords are combinations of notes that sound good together because their frequencies form simple integer ratios. By representing notes as frequencies in arrays, programmers can algorithmically generate consonant chords and progressions. This demonstrates how harmony emerges from mathematical relationships between tones.

9From Code to Musical Notation

Traditional musical notation is introduced only after establishing the underlying physics and mathematics. The staff, clefs, and note symbols are shown to be visual representations of concepts already understood through code. This reverses the typical learning process, making notation meaningful rather than arbitrary.

10Practical Applications for Developers

Understanding music through programming has practical benefits. Developers can create custom audio tools, generate procedural music, or build better music education software. The approach also demonstrates how complex systems can emerge from simple rules - a concept applicable across many programming domains.

11The Value of First Principles Thinking

Learning music theory through programming exemplifies first-principles thinking - breaking down complex topics to their fundamental truths. This method provides deeper understanding than memorization, allowing for creative applications beyond traditional boundaries. It's particularly valuable for programmers accustomed to logical systems.

12Democratizing Music Creation

By making music theory accessible through code, this approach lowers barriers to music creation. Programmers without traditional musical training can now understand and generate music algorithmically. This fusion of disciplines opens new possibilities for digital art and interactive audio experiences on the web.

Original article: Hacker News (news.ycombinator.com) — Programming Music Theory from First Principles