This notebook takes a txt file where each line is of the form ("%s,%s" % (G5,1.25)) and plays it to stdout.
NOTE: You need to write a function to convert a music21 length to a mingus length, given the time signature.
import sys, time, re
import music21
import mingus.core.notes as notes
import mingus.core.value as value
sys.path.append('/usr/local/lib/python2.7/dist-packages/fluidsynth/')
from mingus.midi import fluidsynth
from mingus.containers.Note import Note
from mingus.containers.Bar import Bar
fluidsynth.init('/usr/share/sounds/sf2/FluidR3_GM.sf2',"alsa")
True
# Convert music21 note to mingus note.
def mingify(note):
accidental = re.compile("[A-Z](-|#)[0-9]")
if accidental.match(note):
if '-' not in note: note = Note("%s%s-%s" % (note[0], note[1], note[2]))
else: note = Note(note.replace('-', 'b-'))
else: note = Note("%s-%s" % (note[0], note[1]))
return note
# Get the notes. Note that you need to convert "E-" to "Eb" since you're converting music21 to mingus.
with open('oscar2trigrams.txt', 'rb') as f:
allnotes = [] # list of Note objects
alldurations = []
for line in f:
items = line.split(',')
note = items[0].rstrip()
allnotes.append(mingify(note))
alldurations.append(float(items[1]))
# Get the durations.
# print allnotes
# print alldurations
print len(allnotes)
print sum(alldurations)
489 258.75
# Put notes into one huge bar.
b = Bar()
b.set_meter((len(allnotes) * 4, 4))
for note in allnotes:
b.place_notes(note, value.eighth)
# Assert # of notes in bar == total # of notes.
print len(b)
489
# For now, just adjust tempo to do faster/slower.
fluidsynth.play_Bar(b, 1, 375)
{'bpm': 375}