Wednesday, February 24, 2016

9. Natural Minor Scale

The Python list, minor, contains the semitone intervals for the natural minor scale.


Each time, the scale function is called, it will increase the global variable start by 8, or two measures. If we did not indicate it is global inside function definition, it will try to look for a local variable and complain.


Now the scale function can be called with kind equal to 'major' or 'minor'.


The scale function is called twice with the two different kinds of scale. If we did not have a start which changes, it will write to same 2 measures.


# mus9.py
# D natural minor scale
# D major scale

import music21 as m21
from writeMIDI import writeMIDI

# beats per minute
bpm = 130

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

notes = []

start = 0

def scale(key, n, kind):
    global start
    start += 8
    print('\nkey = {}'.format(key))
    p = m21.pitch.Pitch(key)
    midi = p.midi
    for i in range(8):
        print('midi of {} added'.format(midi))
        n.append((midi,start+i,1,120))
        if kind == 'major': midi += major[i%7]
        elif kind == 'minor': midi += minor[i%7]

scale('D',notes,'minor')
scale('D',notes,'major')
writeMIDI('C',"piano",bpm,notes,'mus9')

The output is:

key = D
midi of 62 added
midi of 64 added
midi of 65 added
midi of 67 added
midi of 69 added
midi of 70 added
midi of 72 added
midi of 74 added

key = D
midi of 62 added
midi of 64 added
midi of 66 added
midi of 67 added
midi of 69 added
midi of 71 added
midi of 73 added
midi of 74 added

This will generate this:


No comments:

Post a Comment