Wednesday, March 2, 2016

16. MIDI of Major and Minor Scales

The midi of a scale in a random root note is generated. We use the scales of major and different minors, which are stored in the SCALE dictionary.


All notes are quarter notes, no each takes 2 measures or 8 quarter lengths, assuming time signature of 4/4.


The total duration is 8 measures for the 4 scales.


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.


# mus16.py
# scales

import music21 as m21
from writeMIDI import writeMIDI
from random import choice

SCALE = {}
SCALE['major'] = [2,2,1,2,2,2,1]
SCALE['natural minor'] = [2,1,2,2,1,2,2]
SCALE['melodic minor'] = [2,1,2,2,2,2,1]
SCALE['harmonic minor'] = [2,1,2,2,1,3,1]

start = 0
notes = []

def scale(root, n, kind):
    global start
    start += 8
    print('\nroot = {} \tkind = {}'.format(root,kind))
    p = m21.pitch.Pitch(root)
    midi = p.midi
    for i in range(8):
        if i<7: print('{},'.format(midi),end=' ')
        else: print('{}'.format(midi))
        n.append((midi,start+i,1,120))
        midi += SCALE[kind][i%7]
        
keys = ['C','C#','D','D#',
        'E','F','F#','G',
        'G#','A','A#','B']

for kind in SCALE:
    scale(choice(keys),notes,kind)

writeMIDI('C','piano',130,notes,'mus16')

This is one possible output:


root = A  kind = harmonic minor
69, 71, 72, 74, 76, 77, 80, 81

root = D#  kind = major
63, 65, 67, 68, 70, 72, 74, 75

root = G  kind = melodic minor
67, 69, 70, 72, 74, 76, 78, 79

root = A  kind = natural minor
69, 71, 72, 74, 76, 77, 79, 81

The output is:

No comments:

Post a Comment