// HACKER NEWS — CYBERSECURITY
Music Theory for Programmers
I can't play an instrument. I have tried more than once, and each time I got as far as being able to make roughly the right noises without ever understanding why they were the right noises.
The problem was never the practice. It was that every explanation of music theory seemed to miss out the fundamental reasons for how and why things are the way they are. Here is a staff. Here are the notes on it. This is a major scale, memorise the pattern. Why those notes? Why that pattern? Because that is the convention.
Which is a strange way to teach a system that essentially comes out of physics and arithmetic. There are twelve notes for a reason. The major scale has the shape it has for a reason. Chords that sound good sound good for a reason, and you can compute those reasons.
So I wanted to start from scratch and learn music from first principles, and I began that journey by writing code.
This article is the result. It starts with a single number changing over time, and if you follow along, you will derive the twelve notes, build scales and chords out of arrays, and write a chord progression that sounds like actual music. No instrument needed, and nothing you have to take on faith. Written notation does turn up, but not until the very end, once there is something for it to be notation of.
Sound is just air pressure wobbling. A speaker makes sound by pushing its cone in and out, and everything your computer does with audio comes down to producing a list of numbers describing where that cone should be, forty-four thousand times a second.
An audio file is that list written down. A synthesiser makes the list up as it goes, and the browser will do that part for you if you say what shape you want. The simplest shape is a sine wave, so here is one repeating 440 times a second:
ctx and out are mine rather than the browser's. Everything else is the .css-imd1km{color:var(--white);border-bottom:1px solid rgba(255, 255, 255, 0.2);-webkit-text-decoration:none;text-decoration:none;-webkit-transition:color 0.2s ease-in-out,border-color 0.2s ease-in-out;transition:color 0.2s ease-in-out,border-color 0.2s ease-in-out;white-space:nowrap;}.css-imd1km:focus,.css-imd1km:active,.css-imd1km:hover{border-color:rgba(255, 255, 255, 0.5);}.css-imd1km.large{font-size:25px;padding:10px 0;position:relative;}.css-imd1km.large:after{content:"";width:15px;height:15px;border-color:var(--white);border-right-width:1px;border-right-style:solid;border-top-width:1px;border-top-style:solid;-webkit-transform:rotate(45deg);-moz-transform:rotate(45deg);-ms-transform:rotate(45deg);transform:rotate(45deg);display:block;position:absolute;right:-24px;top:19px;}Web Audio API exactly as it ships. To run that snippet anywhere else, start with:
The number 440 is the only thing there that carries any musical meaning, and even that is arbitrary. It is the frequency somebody agreed to call "A", and it is the tuning fork the rest of the system is pinned to. Change it to 300 and run it again. You get a different pitch and nothing breaks, because at this level there are no notes yet, just a number.
Frequency is pitch: higher number, higher note. That is the entire mapping, and it is the last thing about music that will be this simple.