At the risk of being one of those annoying "well, actually" people on the internet, I'm going to try and explain some confusing things about music, aimed at people of a programming sort. This is also going to feature convenient javascript examples! These are done via MIDI.js, a fantastic library that lets me directly play songs from JS. Hopefully this works in your browser!

Let's take a melody in a major key; say, Green Greens!

That was in the key of C major, which we can guess by the fact that it resolves to a C chord at the end and has no flats or sharps (and also because I chose a song in C major for this example). Here's the first part of the song, omitting rests:

G        A        B   C        D        E   F   G        A        B        C
G                                                                           
G                                                                           
                      C                                                     
                      C                                                     
                                        E                                   
                                                G                           
                                                                           C
                                                                  B         
                                                         A                  
                                                G                           

Here's the thing, though. While technically the notes are all in the right place, that doesn't tell you very much about how transposition and other musical concepts work. In reality, musicians tend to think of the notes as the indices of the scale. Think of a scale as an array of pitches: for C major, the first note would be C, the second D, the third E, and so on. Here, I've drawn them out (note that it's not zero-indexed, because musical notation isn't either):

G        A        B   C        D        E   F   G        A        B        C
s[4]    s[5]     s[6] s[1]     s[2]   s[3] s[4] s[5]   s[6]     s[7]    s[1]
4                                                                           
4                                                                           
                      1                                                     
                      1                                                     
                                        3                                   
                                                5                           
                                                                           1
                                                                  7         
                                                         6                  
                                                5                           


Instead of this:

for note in song:
    playNote(note)

A song is more like this:

for noteNumber in song:
    playNote(scale[note])

To transpose Green Greens to any key, all we need to do is figure out what indices of s[] each note corresponds to, then swap out s[] while keeping those indices the same. Every major chord follows a 2-2-1-2-2-2-1 interval; for example, in C major, the starting note C is two notes away from D, two notes after that is E, and one note after is F. For A major, set s[1] to A and follow the normal 2-2-1-2-2-2-1 interval to find the notes:

E   F   F#   G   G#   A   A#   B   C   C#   D   D#   E   F   F#   G   G#   A
s[5]    s[6]   s[7] s[1]     s[2]    s[3]  s[4]      s[5]    s[6]  s[7] s[1]
4                                                                           
4                                                                           
                      1                                                     
                      1                                                     
                                       3                                    
                                                     5                      
                                                                           1
                                                                      7     
                                                              6             
                                                     5                      

To your ear, it's VERY IMPORTANT what note you start from. If you kept the same notes but rearranged them:

s = [A B C# D E F# G# A ]->
s = [B C# D E F# G# A B ]

Then that wouldn't be A major anymore!

Notice how different it sounds, even though it's using the same pitches as A major! Here's why: Remember that a major scale uses this series of jumps:

2-2-1-2-2-2-1

per note, respectively. We've just made a new series of jumps, though; this time starting from B:

2-1-2-2-2-1-2

And that's not a major scale anymore. Different types of interval sequences are called "modes", and this type of scale is called a Dorian Mode. But all that we've really done is just changed s[] while keeping the indices intact, since the indices are what the song is. If we started from C again and kept the same dorian intervals, we could play Green Greens in a dorian scale. Notice how C Dorian sounds more lika A Dorian than C Major. The note you start on really doesn't matter too much to your brain - it's the ratios that you're hearing, after all, and the ratios are staying the same.

Note: There are a 6 more modes; two of which, Ionian and Aeolean, are identical to major and natural minor keys. They all sound different; Ephrygian mode, for example, sounds egyptian.


You may be wondering how your ear can tell the difference between B Dorian and A Major if they're the same notes. Is it possible to write a melody in A major that sounds like it's in B dorian, since they're made of the same notes? Absolutely! But the chords that you use to accompany the melody will be different. Going from an s[7] chord to an s[1] chord, for example, resolves itself well. It gets rid of tension.

(In these examples, I'm playing the respective scale first, then an E major chord, then an A major chord. The last two chords are playing the exact same notes yet don't sound as relaxing)

But if you're playing a melody with chords in B Dorian, your brain is primed to hear whatever comes next in B Dorian. If you hear a G chord in a B Dorian, it's going to sound like s[6] (again, your brain cares more about relative ratio than the exact note). You're going to hear that s[5] -> s[1] chord progression as an s[4]->s[7], which still resolves, but not as well.

Note: Musicians usually write chords with roman numerals, so what I'm referring to as an s[5] chord is usually written as V. To play that chord, just find the 5th note of the key you're in and make a major chord starting from that note. Capital letters = major chord, lowercase letters = minor chord. V -> I is also known as an authentic cadence.

Finally, let's tackle why C major and A minor don't sound the same. Even though they have the same pitches, they have completely different intervals: C major has the standard major
2-2-1-2-2-2-1
intervals. As for A minor, starting with A and going to B there's an interval of two, then from B to C an interval of one, and so on to get a different set of intervals:

2-1-2-2-2-1-2

These are the intervals for a minor scale; every minor scale has these intervals. Let's play Green Greens using the notes in the A minor scale:

E   F   F#   G   G#   A   A#   B   C   C#   D   D#   E   F   F#   G   G#   A
s[5]s[6]   s[7]     s[1]     s[2] s[3]     s[4]    s[5] s[6]    s[7]    s[1]
4                                                                           
4                                                                           
                      1                                                     
                      1                                                     
                                    3                                       
                                                     5                      
                                                                           1
                                                                  7         
                                                         6                  
                                                     5                      

By the way, here's some boss music featuring green greens in a minor key at 0:05 to 0:12!

Notice how ominous it sounds in A minor! Because the intervals of the scale are all different, and the intervals start from the root note of the scale (A and C, respectively), A minor and C major don't sound the same. Start from C? C major. Start from A? A minor.

You can use this technique to transpose any song into any key. To transpose a major-key song into a natural minor, for example, first figure out what key (or keys) it belongs to. Then, for all notes, swap out the intervals in your scale s[i] for some minor intervals, and play the appropriate notes from that new scale. Lowering the s[2], s[6], and s[7] notes, for example, will transpose from a major key to the same key, but in minor - and then all you need to do is play.

By the way, every single major scale has a "relative minor" scale that uses the exact same pitches - just count down 3 notes from s[1] to find the next s[1]. F major has a relative minor of D minor.

In the end, the difference between A Minor and C Major is that one is a major scale when you start on C, and one is a minor scale when you start on A - and they're not the same thing because the intervals are relative to those starting notes. Think about a melody as a list of indices of a scale, and hopefully music will seem a bit less insane.


@hillexed