Saturday, March 12, 2016

18. Motifs

Here motif1 and motif2 are series of notes, with timing and volume information. The offset time will correspond to first note, which is at time 0. Here they are strings, but they could also be separate text files with minor changes in program.


notes1 and notes2 are lists which will be populated by getNotes().


In addNotes(), we add either notes1 or notes2 to the main list, notes. We have to adjust the offset time. Tuples can not be modified, and thus we create a temporary list, modify it, and then convert it back to a tuple to append to the notes list.


Finally, we call addNotes() 4 times, adding notes1 or notes2 at appropriate times.


It is also possible to randomly changes notes, etc. to add variation.


# mus18.py
# Motifs

from writeMIDI import writeMIDI

motif1 = '''
C#5 0.0 0.375 106
B4 0.5 0.375 67
G#4 1.0 0.375 80
F#4 1.5 0.375 99
E4 2.0 0.375 84
E4 2.5 0.625 91
E4 3.5 0.375 93
E4 4.0 0.375 78
E4 4.5 0.375 74
E4 5.0 0.375 80
E-4 5.5 0.375 80
E-4 6.0 0.375 80
E4 6.5 0.375 70
E4 7.0 0.625 80
'''

motif2 = '''
C#5 0.0 0.375 81
B4 0.5 0.375 97
G#4 1.0 0.375 77
F#4 1.5 0.375 81
E4 2.0 0.375 97
E4 2.5 0.75 84
E4 3.5 0.375 97
E4 4.0 0.375 91
E4 4.5 0.375 84
E4 5.0 0.375 91
C#4 5.5 0.375 84
C#4 6.0 0.375 80
B3 6.5 0.375 87
B3 7.0 0.667 95
'''

notes = []

notes1 = []
notes2 = []

def getNotes(n,s):
    mot = s.split('\n')
    for m in mot:
        if m == '': continue
        t = m.split()
        n.append((t[0],float(t[1]),
                  float(t[2]),int(t[3])))

def addNotes(n,t):
    for note in n:
        tmp = list(note)
        tmp[1] += t
        notes.append(tuple(tmp))
    
getNotes(notes1,motif1)
getNotes(notes2,motif2)

addNotes(notes1,0)
addNotes(notes2,10)
addNotes(notes2,20)
addNotes(notes1,30)
writeMIDI('C','piano',80,notes,'mus18')

This will generate this:


No comments:

Post a Comment