from sklearn.cluster import KMeans from mingus.containers.Bar import Bar from mingus.midi import fluidsynth import numpy as np import pandas as pd import re fluidsynth.init('/usr/share/sounds/sf2/FluidR3_GM.sf2',"alsa") # Import and order the original notes. oscar2 = pd.read_csv('oscar2notes.txt', skiprows=2)[:].sort("Offset") oscar2.index = xrange(1, len(oscar2) + 1) oscar2 = oscar2[oscar2.Octave >= 4] # threshold >= octave 4 for melodies with open('oscar2notes.txt', 'rb') as f: metmark = float(f.readline()) tsig_num, tsig_den = [i for i in f.readline().replace(' /', '').split()] print "# of notes: %s" % len(oscar2) oscar2.head() # Convert music21 note to mingus note. # This version (different from that in 3. Play Notes) # doesn't return a Note object: returns a string. def mingifytext(note): accidental = re.compile("[A-Z](-|#)[0-9]") if accidental.match(note): if '-' not in note: note = "%s%s-%s" % (note[0], note[1], note[2]) else: note = note.replace('-', 'b-') else: note = "%s-%s" % (note[0], note[1]) return note allnotes = [mingifytext("%s%s" % (n, o)) for n, o in zip(oscar2["Note/Rest"], oscar2["Octave"])] # Given a duration (say 2 for 2 quarter notes), get # the duration in mingus form. This means that you # can feed in the duration into mingus like # fluidsynth.place_note(note, duration) # Stands for toMingusBeat. def toMB(duration): return (1. / duration) # Test your to-mingus-beat function. note1 = "C-5" note2 = "D-5" note3 = "E-5" dur1 = 0.25 dur2 = 0.5 dur3 = 0.25 b = Bar() b.set_meter((4, 4)) b.place_notes(note1, toMB(dur1)) b.place_notes(note2, toMB(dur2)) b.place_notes(note3, toMB(dur3)) fluidsynth.play_Bar(b) # test with oscar's playing b = Bar() b.set_meter((sum([toMB(length) for length in oscar2["Len"]]), 4)) for note, length in zip(allnotes, oscar2["Len"]): b.place_notes(note, toMB(length)) fluidsynth.play_Bar(b, 1, 1000) # need to work more on mingus note values. b