light/dark mode

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.

How Pythagoras broke music

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.





modes and scales

Once we've established our tuning system, we now have a complete set of frequencies within an octave to play with. However, having a large collection of notes doesn't automatically create musical structure. Some tuning systems, like Just Intonation, naturally produce a major scale (Ionian mode) as their default set of notes. But when using tuning systems that divide the octave into more than 7 or 8 steps—such as 12-tone equal temperament (default in Tone.js and most Western music), 9-TET, or microtonal systems—we need to further filter this set to create something musically cohesive.

One of the oldest ways to organize musical notes is through modes. A mode is a pattern of intervals that defines how notes relate to each other within a tuning system. An interval is the number of steps between two notes, where a step is the distance from one note to the next in a given tuning system. The 7 Greek modes (Ionian, Dorian, Phrygian, Lydian, Mixolydian, Aeolian and Locrian) provided the foundation for much of Western music, and they continue to shape melody and harmony today. Modes establish a tonal center and influence the character of a piece—whether it feels bright and happy (Ionian), somber and melancholic (Aeolian), dark and mysterious (Locrian), etc.

From these modes, we derive scales, which are specific selections of notes within a mode. The major scale is just one example, built from the Ionian mode, while the minor scale comes from the Aeolian mode. Other cultures and traditions use unique scale systems, such as pentatonic scales, maqam scales (Middle East and North Africa), and raga structures (Indian).

Ultimately, modes help shape the scales we use, and scales help us structure our melodies and harmonies. Without this reduction, music can easily sound "out of tune" or incoherent, especially in tuning systems with many possible key choices.





chords

The last algorithmically related subset of frequencies we'll discuss is the chord. Just as a scale is a subset of notes chosen from our tuning system, a chord is a subset of notes selected from a scale. A chord is a set of notes played together, creating harmony. If these same notes are played one at a time instead of simultaneously, it’s called an arpeggio.

Just as scales are built from a tonic note, chords are built around a tonal center, which we call the root of the chord. However, while modes and scales are defined by a pattern of intervals from the tonic (measured in tones and semitones), chords are defined by degrees—specific note positions within the scale. This means that the shape of a chord is determined relative to the scale it’s derived from, making it adaptable to different keys, modes, and tuning systems.








Attribution: Text and code written by Nick Briz. The code editor icons designed by Meko and licensed under Creative Commons Attribution License (CC BY 3.0). All sounds generated using the Web Audio API and/or Tone.js by Yotam Mann and other contributors.