def setUp(self): # LilyPond output files are created in current working directory self.tempdir = tempfile.mkdtemp() self.oldcwd = os.getcwd() os.chdir(self.tempdir) self.commonbar = Bar() self.ebar = Bar("E", (4, 4)) self.fbar = Bar("F", (6, 8)) self.tbar = Bar("C", (4, 4)) self.mbar = Bar("C", (4, 4)) self.a_minor_bar = Bar("a", (4, 4)) self.b_flat_minor_bar = Bar("bb", (4, 4)) self.f_sharp_minor_bar = Bar("f#", (4, 4)) for y in [self.commonbar, self.ebar, self.fbar]: list(map(lambda x: y + x, ["C", "E", "G", "B"])) for x in ["C", "E", "G", "B", "C", "E"]: self.tbar.place_notes(NoteContainer(x), 6) for x in ["C", "E"]: self.mbar.place_notes(NoteContainer(x), 4) for x in ["G", "B", "C"]: self.mbar.place_notes(NoteContainer(x), 6) self.track1 = Track() self.track1 + self.commonbar self.track2 = Track() self.track2 + self.commonbar self.track2 + self.ebar self.composition1 = Composition() self.composition1.add_track(self.track1) self.composition2 = Composition() self.composition2.add_track(self.track1) self.composition2.add_track(self.track2)
def setUp(self): self.commonbar = Bar() self.ebar = Bar('E', (4, 4)) self.fbar = Bar('F', (6, 8)) self.tbar = Bar('C', (4, 4)) self.mbar = Bar('C', (4, 4)) self.a_minor_bar = Bar('a', (4, 4)) self.b_flat_minor_bar = Bar('bb', (4, 4)) self.f_sharp_minor_bar = Bar('f#', (4, 4)) for y in [self.commonbar, self.ebar, self.fbar]: map(lambda x: y + x, ['C', 'E', 'G', 'B']) map(lambda x: self.tbar.place_notes(NoteContainer(x), 6), [ 'C', 'E', 'G', 'B', 'C', 'E', ]) map(lambda x: self.mbar.place_notes(NoteContainer(x), 4), ['C', 'E']) map(lambda x: self.mbar.place_notes(NoteContainer(x), 6), ['G', 'B', 'C' ]) self.track1 = Track() self.track1 + self.commonbar self.track2 = Track() self.track2 + self.commonbar self.track2 + self.ebar self.composition1 = Composition() self.composition1.add_track(self.track1) self.composition2 = Composition() self.composition2.add_track(self.track1) self.composition2.add_track(self.track2)
def composificate(tracks1, tracks2): """Extends tracks1 (presumed to be some sort of percussion) to the length of track2. Returns a Composition.""" compo = Composition() #Extend each track1 to the length of the corresponding track2 ntracks = [] if len(tracks1) < len(tracks2): for track in tracks1: idx = tracks1.index(track) track = extend_track(track, len(tracks2[idx])) ntracks.append(track) else: ntracks = tracks1 percus = sum_tracks(ntracks) melody = sum_tracks(tracks2) piano = MidiInstrument() piano.name = 'Acoustic Grand Piano' percussion = MidiInstrument() percussion.name = 'Woodblock' percus.instrument = percussion melody.instrument = piano compo.add_track(percus) compo.add_track(melody) return compo
def setUp(self): self.commonbar = Bar() self.ebar = Bar('E', (4, 4)) self.fbar = Bar('F', (6, 8)) self.tbar = Bar('C', (4, 4)) self.mbar = Bar('C', (4, 4)) for y in [self.commonbar, self.ebar, self.fbar]: map(lambda x: y + x, ['C', 'E', 'G', 'B']) map(lambda x: self.tbar.place_notes(NoteContainer(x), 6), [ 'C', 'E', 'G', 'B', 'C', 'E', ]) map(lambda x: self.mbar.place_notes(NoteContainer(x), 4), ['C', 'E']) map(lambda x: self.mbar.place_notes(NoteContainer(x), 6), ['G', 'B', 'C' ]) self.track1 = Track() self.track1 + self.commonbar self.track2 = Track() self.track2 + self.commonbar self.track2 + self.ebar self.composition1 = Composition() self.composition1.add_track(self.track1) self.composition2 = Composition() self.composition2.add_track(self.track1) self.composition2.add_track(self.track2)
def load_entry(db, doc_id=0): ''' Re-instantiates entry as a JazzLick object given TinyDB doc_id ''' x = Track_from_list(ast.literal_eval(db.get(doc_id=doc_id)['passage'])[1]) c = Composition() x.bars = Bars_from_list(x.bars) c.add_track(x) j = JazzLick(c, passage_track=0, chords=db.get(doc_id=doc_id)['chords']) return j
def main(): comp = Composition() with open('out.txt', 'r') as f: lines = f.readlines() for line in lines: tb = TrackBuilder() symbols = line.rstrip().split(' ')[1:] print(symbols) for symbol in symbols: tb.add_symbol(symbol) track = tb.flush() comp.add_track(track) fluidsynth.init('../sfs/soundfont.sf2', 'alsa') fluidsynth.play_Composition(comp)
def setup_composition(nbars=64, ntracks=2, quant=16): comp = Composition() tks =[Track() for i in xrange(ntracks)] for i in xrange(ntracks): bars = [Bar() for j in xrange(nbars)] for j in xrange(nbars): init_bar(bars[j], quant) tks[i] + bars[j] comp + tks[i] return comp
def rebuild_composition(old, xylo, cutoff): lowest = Note.__int__(Note('C', 8)) highest = Note.__int__(Note('C', 0)) new = Composition() for i in old.selected_tracks: t = Track() t.instrument = xylo for bar in old[i]: b = Bar() b.key.name = bar.key.name b.set_meter(bar.meter) # note value per beat == the denominator in time signature dem = b.meter[1] for lst in bar: value = lst[1] if (is_garbage(value, dem, cutoff[old.selected_tracks.index(i)])): continue # do not include lists without notes if (not lst[2]): continue nc = NoteContainer() for note in lst[2]: if (Note.__int__(note) < lowest): lowest = Note.__int__(note) if (Note.__int__(note) > highest): highest = Note.__int__(note) nc + note b.place_notes(nc, value) t.add_bar(b) new.add_track(t) # can't do the transposing until all notes in all tracks have been # compared against lowest and highest, which is why it is done here n1 = Note() n2 = Note() low = n1.from_int(lowest) high = n2.from_int(highest) # print("lowest and highest notes:", low, high) if (not xylo.notes_in_range([low, high])): new = transposer(new, lowest, highest, xylo) return new
def setUp(self): self.commonbar = Bar() self.ebar = Bar("E", (4, 4)) self.fbar = Bar("F", (6, 8)) self.tbar = Bar("C", (4, 4)) self.mbar = Bar("C", (4, 4)) self.a_minor_bar = Bar("a", (4, 4)) self.b_flat_minor_bar = Bar("bb", (4, 4)) self.f_sharp_minor_bar = Bar("f#", (4, 4)) for y in [self.commonbar, self.ebar, self.fbar]: map(lambda x: y + x, ["C", "E", "G", "B"]) map(lambda x: self.tbar.place_notes(NoteContainer(x), 6), ["C", "E", "G", "B", "C", "E"]) map(lambda x: self.mbar.place_notes(NoteContainer(x), 4), ["C", "E"]) map(lambda x: self.mbar.place_notes(NoteContainer(x), 6), ["G", "B", "C"]) self.track1 = Track() self.track1 + self.commonbar self.track2 = Track() self.track2 + self.commonbar self.track2 + self.ebar self.composition1 = Composition() self.composition1.add_track(self.track1) self.composition2 = Composition() self.composition2.add_track(self.track1) self.composition2.add_track(self.track2)
def gen(self, midifilename): dummy = Note('C-0') dummy.velocity = 0 dummy.channel = 9 dummy.duration = self.dur for nb in xrange( self.n_bars ): b = Bar('C', (4,4)) # note "dummy" posizionate su una "griglia di quantizzazione" for i in xrange(self.nsteps): b.place_notes(dummy, self.dur) # print repr(b) # debug # note "vere" for x in self.STRMAP.keys(): ys = self.STRMAP[x] y = self.DRMAP[x] if len(ys) > 0: if len(self.VARMAP[x]) > 0: var = self.VARMAP[x][nb] else: var = 0 if self.vartype == self._OR_VAR_: # or = variazione aggiunge !? xor = toggle tmps = or_str( ys, gen_bin_str(self.nsteps, var) ) elif self.vartype == self._XOR_VAR_: tmps = xor_str( ys, gen_bin_str(self.nsteps, var) ) else: tmps = ys # no var. bin_to_bar(b, y, tmps, self.dur) # cleanup - tolgo le note "dummy" for x in b.bar : x[2].remove_note('C', 0) # print repr(b) # debug self.tk + b # "dummy bar" db = Bar('C', (4,4)) db.place_notes(dummy,1) self.tk + db comp = Composition() comp + self.tk write_Composition(midifilename, comp, self.tempo)
def MIDI_to_Composition(self, file): (header, track_data) = self.parse_midi_file(file) c = Composition() bpm = 120 if header[2]['fps']: print "Don't know how to parse this yet" return c ticks_per_beat = header[2]['ticks_per_beat'] for track in track_data: t = Track() b = Bar() metronome = 1 # Tick once every quarter note thirtyseconds = 8 # 8 thirtyseconds in a quarter note meter = (4, 4) key = 'C' for e in track: (deltatime, event) = e duration = float(deltatime) / (ticks_per_beat * 4.0) if duration != 0.0: duration = 1.0 / duration if len(b.bar) > 0: current_length = b.bar[-1][1] b.bar[-1][1] = duration if current_length - duration != 0: b.current_beat -= 1.0 / current_length b.current_beat += 1.0 / duration if not b.place_notes(NoteContainer(), duration): t + b b = Bar(key, meter) b.place_notes(NoteContainer(), duration) if event['event'] == 8: if deltatime == 0: pass elif event['event'] == 9: # note on n = Note(notes.int_to_note(event['param1'] % 12), event['param1'] / 12 - 1) n.channel = event['channel'] n.velocity = event['param2'] if len(b.bar) > 0: b.bar[-1][2] + n else: b + n elif event['event'] == 10: # note aftertouch pass elif event['event'] == 11: # controller select pass elif event['event'] == 12: # program change i = MidiInstrument() i.instrument_nr = event['param1'] t.instrument = i elif event['event'] == 0x0f: # meta event Text if event['meta_event'] == 1: pass elif event['meta_event'] == 3: # Track name t.name = event['data'] elif event['meta_event'] == 6: # Marker pass elif event['meta_event'] == 7: # Cue Point pass elif event['meta_event'] == 47: # End of Track pass elif event['meta_event'] == 81: # Set tempo warning Only the last change in bpm will get # saved currently mpqn = self.bytes_to_int(event['data']) bpm = 60000000 / mpqn elif event['meta_event'] == 88: # Time Signature d = event['data'] thirtyseconds = self.bytes_to_int(d[3]) metronome = self.bytes_to_int(d[2]) / 24.0 denom = 2**self.bytes_to_int(d[1]) numer = self.bytes_to_int(d[0]) meter = (numer, denom) b.set_meter(meter) elif event['meta_event'] == 89: # Key Signature d = event['data'] sharps = self.bytes_to_int(d[0]) minor = self.bytes_to_int(d[0]) if minor: key = 'A' else: key = 'C' for i in xrange(abs(sharps)): if sharps < 0: key = intervals.major_fourth(key) else: key = intervals.major_fifth(key) b.key = Note(key) else: print 'Unsupported META event', event['meta_event'] else: print 'Unsupported MIDI event', event t + b c.tracks.append(t) return (c, bpm)
def from_Note(note): c = Composition() c.add_note(note) return _composition2musicxml(c).toprettyxml()
class test_LilyPond(unittest.TestCase): def setUp(self): self.commonbar = Bar() self.ebar = Bar('E', (4, 4)) self.fbar = Bar('F', (6, 8)) self.tbar = Bar('C', (4, 4)) self.mbar = Bar('C', (4, 4)) self.a_minor_bar = Bar('a', (4, 4)) self.b_flat_minor_bar = Bar('bb', (4, 4)) self.f_sharp_minor_bar = Bar('f#', (4, 4)) for y in [self.commonbar, self.ebar, self.fbar]: map(lambda x: y + x, ['C', 'E', 'G', 'B']) map(lambda x: self.tbar.place_notes(NoteContainer(x), 6), [ 'C', 'E', 'G', 'B', 'C', 'E', ]) map(lambda x: self.mbar.place_notes(NoteContainer(x), 4), ['C', 'E']) map(lambda x: self.mbar.place_notes(NoteContainer(x), 6), ['G', 'B', 'C' ]) self.track1 = Track() self.track1 + self.commonbar self.track2 = Track() self.track2 + self.commonbar self.track2 + self.ebar self.composition1 = Composition() self.composition1.add_track(self.track1) self.composition2 = Composition() self.composition2.add_track(self.track1) self.composition2.add_track(self.track2) def test_from_Note(self): self.assertEqual(LilyPond.from_Note(Note('C'), standalone=False), "c'") self.assertEqual(LilyPond.from_Note(Note('C#'), standalone=False), "cis'") self.assertEqual(LilyPond.from_Note(Note('C##'), standalone=False), "cisis'") self.assertEqual(LilyPond.from_Note(Note('Cb'), standalone=False), "ces'") self.assertEqual(LilyPond.from_Note(Note('Cbb'), standalone=False), "ceses'") self.assertEqual(LilyPond.from_Note(Note('C', 0), standalone=False), 'c,,,') self.assertEqual(LilyPond.from_Note(Note('C', 1), standalone=False), 'c,,') self.assertEqual(LilyPond.from_Note(Note('C', 2), standalone=False), 'c,') self.assertEqual(LilyPond.from_Note(Note('C', 3), standalone=False), 'c' ) self.assertEqual(LilyPond.from_Note(Note('C', 4), standalone=False), "c'") self.assertEqual(LilyPond.from_Note(Note('C', 5), standalone=False), "c''") self.assertEqual(LilyPond.from_Note(Note('C', 6), standalone=False), "c'''") self.assertEqual(LilyPond.from_Note(Note('C', 7), standalone=False), "c''''") def test_from_NoteContainer(self): self.assertEqual(LilyPond.from_NoteContainer(NoteContainer('C'), standalone=False), "c'") self.assertEqual(LilyPond.from_NoteContainer(NoteContainer('C'), 4, standalone=False), "c'4") self.assertEqual(LilyPond.from_NoteContainer(NoteContainer(['C', 'E']), standalone=False), "<c' e'>") self.assertEqual(LilyPond.from_NoteContainer(NoteContainer(['C', 'E']), 4, standalone=False), "<c' e'>4") # issue #37 self.assertEqual(LilyPond.from_NoteContainer(NoteContainer('C'), 16, standalone=False), "c'16") self.assertEqual(LilyPond.from_NoteContainer(NoteContainer('C'), 16.0, standalone=False), "c'16") self.assertEqual(LilyPond.from_NoteContainer(NoteContainer('C'), value.dots(16), standalone=False), "c'16.") self.assertEqual(LilyPond.from_NoteContainer(NoteContainer('C'), 0.25, standalone=False), "c'\\longa") self.assertEqual(LilyPond.from_NoteContainer(NoteContainer('C'), 0.5, standalone=False), "c'\\breve") def test_from_Bar(self): self.assertEqual(LilyPond.from_Bar(self.commonbar), "{ \\time 4/4 \\key c \\major c'4 e'4 g'4 b'4 }") self.assertEqual(LilyPond.from_Bar(self.ebar), "{ \\time 4/4 \\key e \\major c'4 e'4 g'4 b'4 }") self.assertEqual(LilyPond.from_Bar(self.fbar), "{ \\time 6/8 \\key f \\major c'8 e'8 g'8 b'8 }") self.assertEqual(LilyPond.from_Bar(self.a_minor_bar), "{ \\time 4/4 \\key a \\minor }") self.assertEqual(LilyPond.from_Bar(self.b_flat_minor_bar), "{ \\time 4/4 \\key bes \\minor }") self.assertEqual(LilyPond.from_Bar(self.f_sharp_minor_bar), "{ \\time 4/4 \\key fis \\minor }") def test_from_Track(self): self.assertEqual(LilyPond.from_Track(self.track1), "{ { c'4 e'4 g'4 b'4 } }") self.assertEqual(LilyPond.from_Track(self.track2), "{ { c'4 e'4 g'4 b'4 } { \\key e \\major c'4 e'4 g'4 b'4 } }" ) def test_from_Composition(self): self.assertEqual(LilyPond.from_Composition(self.composition1), '\\header { title = "Untitled" composer = "" opus = "" } { { c\'4 e\'4 g\'4 b\'4 } }' ) self.assertEqual(LilyPond.from_Composition(self.composition2), '\\header { title = "Untitled" composer = "" opus = "" } { { c\'4 e\'4 g\'4 b\'4 } } { { c\'4 e\'4 g\'4 b\'4 } { \\key e \\major c\'4 e\'4 g\'4 b\'4 } }' ) def test_from_Suite(self): LilyPond.from_Suite(None) def test_dotted_notes(self): self.assertEqual(LilyPond.from_NoteContainer(NoteContainer('C'), value.dots(8), standalone=False), "c'8.") self.assertEqual(LilyPond.from_NoteContainer(NoteContainer('C'), value.dots(4, 2), standalone=False), "c'4..") def test_to_pdf(self): self.assert_(LilyPond.to_pdf('{ %s }' % LilyPond.from_NoteContainer(NoteContainer('C'), value.dots(8)), 'pdftest first test')) self.assert_(LilyPond.to_pdf(LilyPond.from_Bar(self.tbar), 'pdftest2')) self.assert_(LilyPond.to_pdf(LilyPond.from_Bar(self.mbar), 'pdftest3')) def test_to_png(self): self.assert_(LilyPond.to_png('{ %s }' % LilyPond.from_NoteContainer(NoteContainer('C'), value.dots(8)), 'pn1')) self.assert_(LilyPond.to_png(LilyPond.from_Bar(self.tbar), 'pn2')) self.assert_(LilyPond.to_png(LilyPond.from_Bar(self.mbar), 'pn3'))
def from_Bar(bar): c = Composition() t = Track() t.add_bar(bar) c.add_track(t) return _composition2musicxml(c).toprettyxml()
def from_Track(track): c = Composition() c.add_track(track) return _composition2musicxml(c).toprettyxml()
class test_LilyPond(unittest.TestCase): def setUp(self): self.commonbar = Bar() self.ebar = Bar("E", (4, 4)) self.fbar = Bar("F", (6, 8)) self.tbar = Bar("C", (4, 4)) self.mbar = Bar("C", (4, 4)) self.a_minor_bar = Bar("a", (4, 4)) self.b_flat_minor_bar = Bar("bb", (4, 4)) self.f_sharp_minor_bar = Bar("f#", (4, 4)) for y in [self.commonbar, self.ebar, self.fbar]: map(lambda x: y + x, ["C", "E", "G", "B"]) map(lambda x: self.tbar.place_notes(NoteContainer(x), 6), ["C", "E", "G", "B", "C", "E"]) map(lambda x: self.mbar.place_notes(NoteContainer(x), 4), ["C", "E"]) map(lambda x: self.mbar.place_notes(NoteContainer(x), 6), ["G", "B", "C"]) self.track1 = Track() self.track1 + self.commonbar self.track2 = Track() self.track2 + self.commonbar self.track2 + self.ebar self.composition1 = Composition() self.composition1.add_track(self.track1) self.composition2 = Composition() self.composition2.add_track(self.track1) self.composition2.add_track(self.track2) def test_from_Note(self): self.assertEqual(LilyPond.from_Note(Note("C"), standalone=False), "c'") self.assertEqual(LilyPond.from_Note(Note("C#"), standalone=False), "cis'") self.assertEqual(LilyPond.from_Note(Note("C##"), standalone=False), "cisis'") self.assertEqual(LilyPond.from_Note(Note("Cb"), standalone=False), "ces'") self.assertEqual(LilyPond.from_Note(Note("Cbb"), standalone=False), "ceses'") self.assertEqual(LilyPond.from_Note(Note("C", 0), standalone=False), "c,,,") self.assertEqual(LilyPond.from_Note(Note("C", 1), standalone=False), "c,,") self.assertEqual(LilyPond.from_Note(Note("C", 2), standalone=False), "c,") self.assertEqual(LilyPond.from_Note(Note("C", 3), standalone=False), "c") self.assertEqual(LilyPond.from_Note(Note("C", 4), standalone=False), "c'") self.assertEqual(LilyPond.from_Note(Note("C", 5), standalone=False), "c''") self.assertEqual(LilyPond.from_Note(Note("C", 6), standalone=False), "c'''") self.assertEqual(LilyPond.from_Note(Note("C", 7), standalone=False), "c''''") def test_from_NoteContainer(self): self.assertEqual(LilyPond.from_NoteContainer(NoteContainer("C"), standalone=False), "c'") self.assertEqual(LilyPond.from_NoteContainer(NoteContainer("C"), 4, standalone=False), "c'4") self.assertEqual(LilyPond.from_NoteContainer(NoteContainer(["C", "E"]), standalone=False), "<c' e'>") self.assertEqual(LilyPond.from_NoteContainer(NoteContainer(["C", "E"]), 4, standalone=False), "<c' e'>4") # issue #37 self.assertEqual(LilyPond.from_NoteContainer(NoteContainer("C"), 16, standalone=False), "c'16") self.assertEqual(LilyPond.from_NoteContainer(NoteContainer("C"), 16.0, standalone=False), "c'16") self.assertEqual(LilyPond.from_NoteContainer(NoteContainer("C"), value.dots(16), standalone=False), "c'16.") self.assertEqual(LilyPond.from_NoteContainer(NoteContainer("C"), 0.25, standalone=False), "c'\\longa") self.assertEqual(LilyPond.from_NoteContainer(NoteContainer("C"), 0.5, standalone=False), "c'\\breve") def test_from_Bar(self): self.assertEqual(LilyPond.from_Bar(self.commonbar), "{ \\time 4/4 \\key c \\major c'4 e'4 g'4 b'4 }") self.assertEqual(LilyPond.from_Bar(self.ebar), "{ \\time 4/4 \\key e \\major c'4 e'4 g'4 b'4 }") self.assertEqual(LilyPond.from_Bar(self.fbar), "{ \\time 6/8 \\key f \\major c'8 e'8 g'8 b'8 }") self.assertEqual(LilyPond.from_Bar(self.a_minor_bar), "{ \\time 4/4 \\key a \\minor }") self.assertEqual(LilyPond.from_Bar(self.b_flat_minor_bar), "{ \\time 4/4 \\key bes \\minor }") self.assertEqual(LilyPond.from_Bar(self.f_sharp_minor_bar), "{ \\time 4/4 \\key fis \\minor }") def test_from_Track(self): self.assertEqual(LilyPond.from_Track(self.track1), "{ { c'4 e'4 g'4 b'4 } }") self.assertEqual( LilyPond.from_Track(self.track2), "{ { c'4 e'4 g'4 b'4 } { \\key e \\major c'4 e'4 g'4 b'4 } }" ) def test_from_Composition(self): self.assertEqual( LilyPond.from_Composition(self.composition1), '\\header { title = "Untitled" composer = "" opus = "" } { { c\'4 e\'4 g\'4 b\'4 } }', ) self.assertEqual( LilyPond.from_Composition(self.composition2), "\\header { title = \"Untitled\" composer = \"\" opus = \"\" } { { c'4 e'4 g'4 b'4 } } { { c'4 e'4 g'4 b'4 } { \\key e \\major c'4 e'4 g'4 b'4 } }", ) def test_from_Suite(self): LilyPond.from_Suite(None) def test_dotted_notes(self): self.assertEqual(LilyPond.from_NoteContainer(NoteContainer("C"), value.dots(8), standalone=False), "c'8.") self.assertEqual(LilyPond.from_NoteContainer(NoteContainer("C"), value.dots(4, 2), standalone=False), "c'4..") def test_to_pdf(self): self.assert_( LilyPond.to_pdf( "{ %s }" % LilyPond.from_NoteContainer(NoteContainer("C"), value.dots(8)), "pdftest first test" ) ) self.assert_(LilyPond.to_pdf(LilyPond.from_Bar(self.tbar), "pdftest2")) self.assert_(LilyPond.to_pdf(LilyPond.from_Bar(self.mbar), "pdftest3")) def test_to_png(self): self.assert_(LilyPond.to_png("{ %s }" % LilyPond.from_NoteContainer(NoteContainer("C"), value.dots(8)), "pn1")) self.assert_(LilyPond.to_png(LilyPond.from_Bar(self.tbar), "pn2")) self.assert_(LilyPond.to_png(LilyPond.from_Bar(self.mbar), "pn3"))
class test_LilyPond(unittest.TestCase): def setUp(self): # LilyPond output files are created in current working directory self.tempdir = tempfile.mkdtemp() self.oldcwd = os.getcwd() os.chdir(self.tempdir) self.commonbar = Bar() self.ebar = Bar("E", (4, 4)) self.fbar = Bar("F", (6, 8)) self.tbar = Bar("C", (4, 4)) self.mbar = Bar("C", (4, 4)) self.a_minor_bar = Bar("a", (4, 4)) self.b_flat_minor_bar = Bar("bb", (4, 4)) self.f_sharp_minor_bar = Bar("f#", (4, 4)) for y in [self.commonbar, self.ebar, self.fbar]: list(map(lambda x: y + x, ["C", "E", "G", "B"])) for x in ["C", "E", "G", "B", "C", "E"]: self.tbar.place_notes(NoteContainer(x), 6) for x in ["C", "E"]: self.mbar.place_notes(NoteContainer(x), 4) for x in ["G", "B", "C"]: self.mbar.place_notes(NoteContainer(x), 6) self.track1 = Track() self.track1 + self.commonbar self.track2 = Track() self.track2 + self.commonbar self.track2 + self.ebar self.composition1 = Composition() self.composition1.add_track(self.track1) self.composition2 = Composition() self.composition2.add_track(self.track1) self.composition2.add_track(self.track2) def tearDown(self): os.chdir(self.oldcwd) shutil.rmtree(self.tempdir) def test_from_Note(self): self.assertEqual(LilyPond.from_Note(Note("C"), standalone=False), "c'") self.assertEqual(LilyPond.from_Note(Note("C#"), standalone=False), "cis'") self.assertEqual(LilyPond.from_Note(Note("C##"), standalone=False), "cisis'") self.assertEqual(LilyPond.from_Note(Note("Cb"), standalone=False), "ces'") self.assertEqual(LilyPond.from_Note(Note("Cbb"), standalone=False), "ceses'") self.assertEqual(LilyPond.from_Note(Note("C", 0), standalone=False), "c,,,") self.assertEqual(LilyPond.from_Note(Note("C", 1), standalone=False), "c,,") self.assertEqual(LilyPond.from_Note(Note("C", 2), standalone=False), "c,") self.assertEqual(LilyPond.from_Note(Note("C", 3), standalone=False), "c") self.assertEqual(LilyPond.from_Note(Note("C", 4), standalone=False), "c'") self.assertEqual(LilyPond.from_Note(Note("C", 5), standalone=False), "c''") self.assertEqual(LilyPond.from_Note(Note("C", 6), standalone=False), "c'''") self.assertEqual(LilyPond.from_Note(Note("C", 7), standalone=False), "c''''") def test_from_NoteContainer(self): self.assertEqual( LilyPond.from_NoteContainer(NoteContainer("C"), standalone=False), "c'") self.assertEqual( LilyPond.from_NoteContainer(NoteContainer("C"), 4, standalone=False), "c'4") self.assertEqual( LilyPond.from_NoteContainer(NoteContainer(["C", "E"]), standalone=False), "<c' e'>", ) self.assertEqual( LilyPond.from_NoteContainer(NoteContainer(["C", "E"]), 4, standalone=False), "<c' e'>4", ) # issue #37 self.assertEqual( LilyPond.from_NoteContainer(NoteContainer("C"), 16, standalone=False), "c'16", ) self.assertEqual( LilyPond.from_NoteContainer(NoteContainer("C"), 16.0, standalone=False), "c'16", ) self.assertEqual( LilyPond.from_NoteContainer(NoteContainer("C"), value.dots(16), standalone=False), "c'16.", ) self.assertEqual( LilyPond.from_NoteContainer(NoteContainer("C"), 0.25, standalone=False), "c'\\longa", ) self.assertEqual( LilyPond.from_NoteContainer(NoteContainer("C"), 0.5, standalone=False), "c'\\breve", ) def test_from_Bar(self): self.assertEqual( LilyPond.from_Bar(self.commonbar), "{ \\time 4/4 \\key c \\major c'4 e'4 g'4 b'4 }", ) self.assertEqual( LilyPond.from_Bar(self.ebar), "{ \\time 4/4 \\key e \\major c'4 e'4 g'4 b'4 }", ) self.assertEqual( LilyPond.from_Bar(self.fbar), "{ \\time 6/8 \\key f \\major c'8 e'8 g'8 b'8 }", ) self.assertEqual(LilyPond.from_Bar(self.a_minor_bar), "{ \\time 4/4 \\key a \\minor }") self.assertEqual(LilyPond.from_Bar(self.b_flat_minor_bar), "{ \\time 4/4 \\key bes \\minor }") self.assertEqual( LilyPond.from_Bar(self.f_sharp_minor_bar), "{ \\time 4/4 \\key fis \\minor }", ) def test_from_Track(self): self.assertEqual(LilyPond.from_Track(self.track1), "{ { c'4 e'4 g'4 b'4 } }") self.assertEqual( LilyPond.from_Track(self.track2), "{ { c'4 e'4 g'4 b'4 } { \\key e \\major c'4 e'4 g'4 b'4 } }", ) def test_from_Composition(self): self.assertEqual( LilyPond.from_Composition(self.composition1), '\\header { title = "Untitled" composer = "" opus = "" } { { c\'4 e\'4 g\'4 b\'4 } }', ) self.assertEqual( LilyPond.from_Composition(self.composition2), "\\header { title = \"Untitled\" composer = \"\" opus = \"\" } { { c'4 e'4 g'4 b'4 } } { { c'4 e'4 g'4 b'4 } { \\key e \\major c'4 e'4 g'4 b'4 } }", ) def test_from_Suite(self): LilyPond.from_Suite(None) def test_dotted_notes(self): self.assertEqual( LilyPond.from_NoteContainer(NoteContainer("C"), value.dots(8), standalone=False), "c'8.", ) self.assertEqual( LilyPond.from_NoteContainer(NoteContainer("C"), value.dots(4, 2), standalone=False), "c'4..", ) def test_to_pdf(self): self.assertTrue( LilyPond.to_pdf( "{ %s }" % LilyPond.from_NoteContainer(NoteContainer("C"), value.dots(8)), "pdftest first test", )) self.assertTrue( LilyPond.to_pdf(LilyPond.from_Bar(self.tbar), "pdftest2")) self.assertTrue( LilyPond.to_pdf(LilyPond.from_Bar(self.mbar), "pdftest3")) def test_to_png(self): self.assertTrue( LilyPond.to_png( "{ %s }" % LilyPond.from_NoteContainer(NoteContainer("C"), value.dots(8)), "pn1", )) self.assertTrue(LilyPond.to_png(LilyPond.from_Bar(self.tbar), "pn2")) self.assertTrue(LilyPond.to_png(LilyPond.from_Bar(self.mbar), "pn3"))
def MIDI_to_Composition(self, file): (header, track_data) = self.parse_midi_file(file) # print('MIDI_to_Composition:', track_data[1]) # print('===') # print(track_data[2]) # for j in track_data[2]: # if j[1]['event'] == 9 or j[1]['event'] == 8: # print('MIDI_to_Composition:', j[0], j[1]['event'], Note(notes.int_to_note(j[1]['param1'] % 12))) c = Composition() if header[2]['fps']: print("Don't know how to parse this yet") return c ticks_per_beat = header[2]['ticks_per_beat'] for track_idx, track in enumerate(track_data): t = Track() b = Bar() metronome = 1 # Tick once every quarter note thirtyseconds = 8 # 8 thirtyseconds in a quarter note meter = (4, 4) key = 'C' NOTE_ON_counter = 0 for event_idx, e in enumerate(track): (deltatime, event) = e duration = float(deltatime) / (ticks_per_beat * 4.0) if NOTE_ON_counter == 0 and duration != 0 and event['event'] == 9: # then we need to add a rest before playing b.place_rest(1.0/duration) NOTE_ON_counter += 1 # this logic is nice and clear... if duration != 0.0: duration = 1.0 / duration if len(b.bar) > 0: current_length = b.bar[-1][1] b.bar[-1][1] = duration if current_length - duration != 0: b.current_beat -= 1.0 / current_length b.current_beat += 1.0 / duration if not b.place_notes(NoteContainer(), duration): t + b b = Bar(key, meter) b.place_notes(NoteContainer(), duration) if event['event'] == 8: if deltatime == 0: pass elif event['event'] == 9: # note on n = Note(notes.int_to_note(event['param1'] % 12), np.floor(event['param1'] / 12)) # this was event['param1']/12 - 1 but that gives skewed, incorrect octaves n.channel = event['channel'] n.velocity = event['param2'] if len(b.bar) > 0: b.bar[-1][2] + n else: b + n elif event['event'] == 10: # note aftertouch pass elif event['event'] == 11: # controller select pass elif event['event'] == 12: # program change i = MidiInstrument() i.instrument_nr = event['param1'] t.instrument = i elif event['event'] == 0x0f: # meta event Text if event['meta_event'] == 1: pass elif event['meta_event'] == 3: # Track name t.name = event['data'] elif event['meta_event'] == 6: # Marker pass elif event['meta_event'] == 7: # Cue Point pass elif event['meta_event'] == 47: # End of Track pass elif event['meta_event'] == 81: # Set tempo warning Only the last change in bpm will get # saved currently mpqn = self.bytes_to_int(event['data']) bpm = 60000000 / mpqn elif event['meta_event'] == 88: # Time Signature d = event['data'] thirtyseconds = d[3] #self.bytes_to_int(d[3]) metronome = d[2]//24.0 #self.bytes_to_int(d[2]) / 24.0 denom = 2**d[1] #2 ** self.bytes_to_int(d[1]) numer = d[0]#self.bytes_to_int(d[0]) meter = (numer, denom) b.set_meter(meter) elif event['meta_event'] == 89: # Key Signature d = event['data'] sharps = d[0] #self.bytes_to_int(d[0]) minor = d[0] #self.bytes_to_int(d[0]) if minor: key = 'A' else: key = 'C' for i in range(abs(sharps)): if sharps < 0: key = intervals.major_fourth(key) else: key = intervals.major_fifth(key) b.key = Note(key) else: print('Unsupported META event', event['meta_event']) else: print('Unsupported MIDI event', event) t + b c.tracks.append(t) return (c, bpm)
class test_LilyPond(unittest.TestCase): def setUp(self): self.commonbar = Bar() self.ebar = Bar('E', (4, 4)) self.fbar = Bar('F', (6, 8)) self.tbar = Bar('C', (4, 4)) self.mbar = Bar('C', (4, 4)) for y in [self.commonbar, self.ebar, self.fbar]: map(lambda x: y + x, ['C', 'E', 'G', 'B']) map(lambda x: self.tbar.place_notes(NoteContainer(x), 6), [ 'C', 'E', 'G', 'B', 'C', 'E', ]) map(lambda x: self.mbar.place_notes(NoteContainer(x), 4), ['C', 'E']) map(lambda x: self.mbar.place_notes(NoteContainer(x), 6), ['G', 'B', 'C' ]) self.track1 = Track() self.track1 + self.commonbar self.track2 = Track() self.track2 + self.commonbar self.track2 + self.ebar self.composition1 = Composition() self.composition1.add_track(self.track1) self.composition2 = Composition() self.composition2.add_track(self.track1) self.composition2.add_track(self.track2) def test_from_Note(self): self.assertEqual(LilyPond.from_Note(Note('C'), standalone=False), "c'") self.assertEqual(LilyPond.from_Note(Note('C#'), standalone=False), "cis'") self.assertEqual(LilyPond.from_Note(Note('C##'), standalone=False), "cisis'") self.assertEqual(LilyPond.from_Note(Note('Cb'), standalone=False), "ces'") self.assertEqual(LilyPond.from_Note(Note('Cbb'), standalone=False), "ceses'") self.assertEqual(LilyPond.from_Note(Note('C', 0), standalone=False), 'c,,,') self.assertEqual(LilyPond.from_Note(Note('C', 1), standalone=False), 'c,,') self.assertEqual(LilyPond.from_Note(Note('C', 2), standalone=False), 'c,') self.assertEqual(LilyPond.from_Note(Note('C', 3), standalone=False), 'c' ) self.assertEqual(LilyPond.from_Note(Note('C', 4), standalone=False), "c'") self.assertEqual(LilyPond.from_Note(Note('C', 5), standalone=False), "c''") self.assertEqual(LilyPond.from_Note(Note('C', 6), standalone=False), "c'''") self.assertEqual(LilyPond.from_Note(Note('C', 7), standalone=False), "c''''") def test_from_NoteContainer(self): self.assertEqual(LilyPond.from_NoteContainer(NoteContainer('C'), standalone=False), "c'") self.assertEqual(LilyPond.from_NoteContainer(NoteContainer('C'), 4, standalone=False), "c'4") self.assertEqual(LilyPond.from_NoteContainer(NoteContainer(['C', 'E']), standalone=False), "<c' e'>") self.assertEqual(LilyPond.from_NoteContainer(NoteContainer(['C', 'E']), 4, standalone=False), "<c' e'>4") # issue #37 self.assertEqual(LilyPond.from_NoteContainer(NoteContainer('C'), 16, standalone=False), "c'16") self.assertEqual(LilyPond.from_NoteContainer(NoteContainer('C'), 16.0, standalone=False), "c'16") self.assertEqual(LilyPond.from_NoteContainer(NoteContainer('C'), value.dots(16), standalone=False), "c'16.") self.assertEqual(LilyPond.from_NoteContainer(NoteContainer('C'), 0.25, standalone=False), "c'\\longa") self.assertEqual(LilyPond.from_NoteContainer(NoteContainer('C'), 0.5, standalone=False), "c'\\breve") def test_from_Bar(self): self.assertEqual(LilyPond.from_Bar(self.commonbar), "{ \\time 4/4 \\key c \\major c'4 e'4 g'4 b'4 }") self.assertEqual(LilyPond.from_Bar(self.ebar), "{ \\time 4/4 \\key e \\major c'4 e'4 g'4 b'4 }") self.assertEqual(LilyPond.from_Bar(self.fbar), "{ \\time 6/8 \\key f \\major c'8 e'8 g'8 b'8 }") def test_from_Track(self): self.assertEqual(LilyPond.from_Track(self.track1), "{ { c'4 e'4 g'4 b'4 } }") self.assertEqual(LilyPond.from_Track(self.track2), "{ { c'4 e'4 g'4 b'4 } { \\key e \\major c'4 e'4 g'4 b'4 } }" ) def test_from_Composition(self): self.assertEqual(LilyPond.from_Composition(self.composition1), '\\header { title = "Untitled" composer = "" opus = "" } { { c\'4 e\'4 g\'4 b\'4 } }' ) self.assertEqual(LilyPond.from_Composition(self.composition2), '\\header { title = "Untitled" composer = "" opus = "" } { { c\'4 e\'4 g\'4 b\'4 } } { { c\'4 e\'4 g\'4 b\'4 } { \\key e \\major c\'4 e\'4 g\'4 b\'4 } }' ) def test_from_Suite(self): LilyPond.from_Suite(None) def test_dotted_notes(self): self.assertEqual(LilyPond.from_NoteContainer(NoteContainer('C'), value.dots(8), standalone=False), "c'8.") self.assertEqual(LilyPond.from_NoteContainer(NoteContainer('C'), value.dots(4, 2), standalone=False), "c'4..") def test_to_pdf(self): self.assert_(LilyPond.to_pdf('{ %s }' % LilyPond.from_NoteContainer(NoteContainer('C'), value.dots(8)), 'pdftest first test')) self.assert_(LilyPond.to_pdf(LilyPond.from_Bar(self.tbar), 'pdftest2')) self.assert_(LilyPond.to_pdf(LilyPond.from_Bar(self.mbar), 'pdftest3')) def test_to_png(self): self.assert_(LilyPond.to_png('{ %s }' % LilyPond.from_NoteContainer(NoteContainer('C'), value.dots(8)), 'pn1')) self.assert_(LilyPond.to_png(LilyPond.from_Bar(self.tbar), 'pn2')) self.assert_(LilyPond.to_png(LilyPond.from_Bar(self.mbar), 'pn3'))