The module writeMIDI.py requires music21 music module.
Each note is a tuple with 4 different values. The first is a string corresponding to note number, such as 'C4'.
Next is the start time in quarter lengths. A note starting in the second measure will have start of 4.
The third number is the length in quarter notes. A whole note would correspond to 4. Here we only have quarter notes (duration is 1).
The last number is volume and is between 0 and 127, 0 no sound and 127 is max sound.
# mus1.py
# C major scale
from writeMIDI import writeMIDI
# beats per minute
bpm = 130
notes = []
# C4, quarter note at time 0, at volume 120
notes.append(('C4',0,1,120))
# D4, quarter note at time 1, at volume 120
notes.append(('D4',1,1,120))
# E4, quarter note at time 2, at volume 120
notes.append(('E4',2,1,120))
# F4, quarter note at time 3, at volume 120
notes.append(('F4',3,1,120))
# G4, quarter note at time 4, at volume 120
notes.append(('G4',4,1,120))
# A4, quarter note at time 5, at volume 120
notes.append(('A4',5,1,120))
# B4, quarter note at time 6, at volume 120
notes.append(('B4',6,1,120))
# C5, eighth note at time 7, at volume 120
notes.append(('C5',7,1,120))
writeMIDI("piano",bpm,notes,'mus1')
This will generate this:
No comments:
Post a Comment