chapter 4
scales
When particles in the air vibrate and those vibrations reach our ears, our ear drums vibrate in tandem sending electrical signals to our brain which we hallucinate as "sound". Our brain only registers vibrations between 20 Hz to 20,000 Hz, the audible human range, in the example below I've limited that range from 220 Hz 1760 by default. This imposed limitation is our first step from "sound" to "music." We've discussed sound at great length, but we've yet to really talk about music. Simply put, music is created by limiting the sounds we hear to a finite list of mathematically related frequencies and then playing those frequencies at mathematically related points in time. The latter we'll discuss in the next section. In this section we'll talk about how we algorithmically generate a set of "musical" frequencies. In the interactive example below there are a number of drop-down lists you can adjust, each will limit the playable frequencies to a smaller set of possible values. Below that, I'll explain each of these, as well as the theory and algorithms behind them.
tuning systems
The first of the drop-down lists above let you limit the frequencies to a set number of octaves (either 3, 2 or 1), for context a piano usually contains ~7 octaves. In music an octave is what we call a 2:1 or 2/1
relationship between frequencies, for example 440 Hz and 880 Hz. The first step on our journey to creating a set of frequencies for our musical palette is deciding on a tuning system, an algorithm we'll use to divide up the potentially infinite number of frequencies in an octave. In the vast majority of popular music the tuning system used is called "12 tone equal temperament", as the name implies, the formula for this one is to divide the octave in to 12 equally spaced parts. Despite it's ubiquity today, it's not the only tuning system out there, nor was it the first.

Tone.js has 12 tone equal temperament built into it. If that's the tuning system we want to use then we don't ever need to write our own algorithms, instead we can simply specify the note we want as a string synth.triggerAttack('A4')
is no different from synth.triggerAttack(440)
. That said, if we want to use an alternative sytem, or even create our own, then we'll need to roll out our own algorithms like the examples below.