Saturday, February 27, 2016

12. Major Scales

There are 12 notes in each octave. They are in the Python list called keys. It can be written also all on one long line. However, here, it is broken into 3 lines.


We use the semitone intervals of a major scale, inside the major list.


Inside the main loop, iterated over all keys, we use i%7 (i modulo 7) as index into major so we only access indexes 0 through 6.


Also we print a new line only if i is 7. The default end, for a print statement, is new line, unless we override it.


# mus12.py
# major scales

import music21 as m21

major = [2,2,1,2,2,2,1]

keys = ['C','C#','D','D#',
        'E','F','F#','G',
        'G#','A','A#','B']

print('The 12 major scales:')
for key in keys:
    n = m21.note.Note(key)
    midi = n.pitch.midi
    for i in range(8):
        n = m21.note.Note(midi = midi)
        if i<7: print(n.name, end= ' - ')
        else: print(n.name)
        midi = n.pitch.midi
        midi += major[i%7]

This will generate this output:


The 12 major scales:
C - D - E - F - G - A - B - C
C# - E- - F - F# - G# - B- - C - C#
D - E - F# - G - A - B - C# - D
E- - F - G - G# - B- - C - D - E-
E - F# - G# - A - B - C# - E- - E
F - G - A - B- - C - D - E - F
F# - G# - B- - B - C# - E- - F - F#
G - A - B - C - D - E - F# - G
G# - B- - C - C# - E- - F - G - G#
A - B - C# - D - E - F# - G# - A
B- - C - D - E- - F - G - A - B-
B - C# - E- - E - F# - G# - B- - B

No comments:

Post a Comment