Saturday, February 20, 2016

3. C# and D major scales

Version 2 of writeMIDI.py, included in the right side link, has as one of the arguments, the key signature.


We are using the lists notes1 and notes2 to populate the notes of the two scales. In Python, lists are passed as reference, thus in the first call the local n points to note1 and then in second call, n is the same as note2.


Since we are only changing lists, the function scale does not return anything.


The printouts will show which notes are being added.


# mus3.py
# C# major 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]

notes1 = []
notes2 = []

def scale(key, n):
    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,i,1,120))
        midi += major[i%7]

key = 'C#'
scale(key,notes1)
writeMIDI(key,"piano",bpm,notes1,'mus3a')
key = 'D'
scale(key,notes2)
writeMIDI(key,"flute",bpm,notes2,'mus3b')


PrintOut:


key = C#
midi of 61 added
midi of 63 added
midi of 65 added
midi of 66 added
midi of 68 added
midi of 70 added
midi of 72 added
midi of 73 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 for mus3a.mid:



This will generate this for mus3b.mid:


No comments:

Post a Comment