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 test_diminish(self): b = Bar() c = Bar() b + 'A' c + 'Ab' b.diminish() self.assertEqual(b, c)
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 add_notes(self, note, duration=None): """Add a Note, note as string or NoteContainer to the last Bar. If the Bar is full, a new one will automatically be created. If the Bar is not full but the note can't fit in, this method will return False. True otherwise. An InstrumentRangeError exception will be raised if an Instrument is attached to the Track, but the note turns out not to be within the range of the Instrument. """ if self.instrument != None: if not self.instrument.can_play_notes(note): raise InstrumentRangeError( "Note '%s' is not in range of the instrument (%s)" % (note, self.instrument)) if duration == None: duration = 4 # Check whether the last bar is full, if so create a new bar and add the # note there if len(self.bars) == 0: self.bars.append(Bar()) last_bar = self.bars[-1] if last_bar.is_full(): self.bars.append(Bar(last_bar.key, last_bar.meter)) # warning should hold note if it doesn't fit return self.bars[-1].place_notes(note, duration)
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_diminish(self): b = Bar() c = Bar() b + "A" c + "Ab" b.diminish() self.assertEqual(b, c)
def test_transpose(self): b = Bar() c = Bar() b + ['C', 'E', 'G'] c + ['E', 'G#', 'B'] b + ['F', 'A', 'C'] c + ['A', 'C#', 'E'] b.transpose('3', True) self.assertEqual(b, c) b.transpose('3', False) b.transpose('3') self.assertEqual(b, c)
def __init__(self): self._notes = [] self._bar = None self._durs = [] self._index = 0 self._key = "" self._bpm = 120 self._dynamics = {} self.paused = False self._bar = Bar() fs.init("sound.sf2", "oss") mixer.init()
def test_set_item(self): b = Bar() b + ['A', 'C', 'E'] c = Bar() c + ['A', 'C', 'E'] self.assertEqual(b, c) c[0] = NoteContainer(['A', 'C', 'E']) self.assertEqual(b, c) c[0] = ['A', 'C', 'E'] self.assertEqual(b, c) c[0] = Note('A') c[0] = c[0][2] + NoteContainer(['C', 'E']) self.assertEqual(b, c) c[0] = Note('A') c[0] = c[0][2] + 'C' c[0] = c[0][2] + 'E' self.assertEqual(b, c)
def test_set_item(self): b = Bar() b + ["A", "C", "E"] c = Bar() c + ["A", "C", "E"] self.assertEqual(b, c) c[0] = NoteContainer(["A", "C", "E"]) self.assertEqual(b, c) c[0] = ["A", "C", "E"] self.assertEqual(b, c) c[0] = Note("A") c[0] = c[0][2] + NoteContainer(["C", "E"]) self.assertEqual(b, c) c[0] = Note("A") c[0] = c[0][2] + "C" c[0] = c[0][2] + "E" self.assertEqual(b, c)
def loadSong(self, fn, isTwoHand): """Loads in a song from the specified file""" header = dict() length = 0.0 self._notes = [] self._bar = None self._durs = [] with open(fn) as file: i = 0 for line in file: if line[0] == "#" or line[0].strip( ) == "": # Skips blank lines and comment lines continue if i < 3: # The first three lines are loaded into the header dictionary vals = line.split('=') header[vals[0]] = vals[1].strip() i += 1 else: vals = line.split(',') if line[0] == 'r': # Indicates a rest value self._notes.append(None) dur = vals[1].strip() else: if isTwoHand: # If isTwoHand is true, then a two note chord is looked for self._notes.append( [vals[0].strip(), vals[1].strip()]) dur = vals[2].strip() else: self._notes.append(vals[0].strip()) dur = vals[1].strip() if dur[len(dur) - 1] == '.': # Looks for dotted notes dur = value.dots(int(dur[:-1])) else: dur = int(dur) self._durs.append(dur) length += 1.0 / dur self._key = header["key"] self._bpm = int(header["bpm"]) self._beat = int(header["beat"]) length *= self._bpm self._bar = Bar() self._bar.set_meter((length, int(header["beat"])))
def playNextNoteLong(self): """Plays the next note in the song and advances the index""" tempBar = Bar() tempBar.set_meter((1, 1)) tempBar.key = self._key tempBar.place_notes(self._notes[self._index], 1) fs.play_Bar(tempBar, 120)
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 test_augment(self): b = Bar() c = Bar() d = Bar() b + 'A' c + 'A#' d + 'A##' b.augment() self.assertEqual(b, c) b.augment() self.assertEqual(b, d) c.augment() self.assertEqual(c, d)
def playManyNotes(self, notes): """Plays the next note in the song and advances the index""" tempBar = Bar() tempBar.set_meter((len(notes) * 4, 4)) for n in notes: tempBar.place_notes(n, 1) fs.play_Bar(tempBar, 1, 200)
def playNextNote(self): """Plays the next note in the song and advances the index""" tempBar = Bar() beats = 1.0 / (1.0 / self._beat) / (1.0 / self._durs[self._index]) tempBar.set_meter((beats, self._beat)) tempBar.key = self._key tempBar.place_notes(self._notes[self._index], self._durs[self._index]) print tempBar fs.play_Bar(tempBar, self._bpm) return self._index
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 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
class test_Bar(unittest.TestCase): def setUp(self): self.b = Bar("C", (4, 4)) self.c = Bar("E", (2, 2)) self.meterless = Bar("C", (0, 0)) def test_equality(self): self.assertEqual(self.b, self.c) self.assertEqual(self.b, self.meterless) self.assertEqual(self.c, self.meterless) b1 = Bar("C", (4, 4)) b1 + ["A", "C"] b1 + ["D"] b2 = Bar("C", (4, 4)) b2 + ["A", "C"] b2 + ["D"] self.assertEqual(b1, b2) self.assertNotEqual(b1, self.b) def test_place_notes_types(self): self.assertEqual(True, self.meterless + NoteContainer(["A", "C"])) self.assertEqual(True, self.meterless + "A") self.assertEqual(True, self.meterless + Note("A")) self.assertEqual(True, self.meterless + ["A", "B"]) self.assertEqual(True, self.meterless + [Note("A"), Note("B")]) def test_get_range(self): self.b + NoteContainer(["C", "E"]) self.assertEqual((Note("C"), Note("E")), self.b.get_range()) def test_set_item(self): b = Bar() b + ["A", "C", "E"] # A4 C5 E5 c = Bar() c + ["A", "C", "E"] self.assertEqual(b, c) c[0] = NoteContainer(["A", "C", "E"]) self.assertEqual(b, c) c[0] = ["A", "C", "E"] self.assertEqual(b, c) c[0] = Note("A") c[0] = c[0][2] + NoteContainer(["C", "E"]) # C4 E4 self.assertNotEqual(b, c) c[0] = Note("A") c[0] = c[0][2] + NoteContainer(["C-5", "E"]) # C5 E5 self.assertEqual(b, c) c[0] = Note("A") c[0] = c[0][2] + "C" c[0] = c[0][2] + "E" self.assertEqual(b, c) def test_key(self): self.assertEqual(self.b.key, Key("C")) self.assertEqual(self.c.key, Key("E")) def test_transpose(self): b = Bar() c = Bar() b + ["C", "E", "G"] c + ["E", "G#", "B"] b + ["F", "A", "C"] c + ["A", "C#", "E"] b.transpose("3", True) self.assertEqual(b, c) b.transpose("3", False) b.transpose("3") self.assertEqual(b, c) def test_transpose_rest(self): b = Bar() b.place_notes("C-4", 4) b.place_rest(4) c = Bar() c.place_notes("E-4", 4) c.place_rest(4) b.transpose("3", True) self.assertEqual(b, c) def test_augment(self): b = Bar() c = Bar() d = Bar() b + "A" c + "A#" d + "A##" b.augment() self.assertEqual(b, c) b.augment() self.assertEqual(b, d) c.augment() self.assertEqual(c, d) def test_augment_rest(self): b = Bar() b.place_notes("C-4", 4) b.place_rest(4) c = Bar() c.place_notes("C#-4", 4) c.place_rest(4) b.augment() self.assertEqual(b, c) def test_diminish(self): b = Bar() c = Bar() b + "A" c + "Ab" b.diminish() self.assertEqual(b, c) def test_diminish_rest(self): b = Bar() b.place_notes("C#-4", 4) b.place_rest(4) c = Bar() c.place_notes("C-4", 4) c.place_rest(4) b.diminish() self.assertEqual(b, c) # def test_to_minor(self): # b = Bar() # c = Bar() # b + 'C' # c + 'A' # b.to_minor() # self.assertEqual(b, c) # # def test_to_major(self): # b = Bar() # c = Bar() # b + 'C' # c + 'A' # c.to_major() # self.assertEqual(b, c) def test_get_note_names(self): b = Bar() b + "C" b + "A" self.assertEqual(["C", "A"], b.get_note_names()) def test_determine_chords(self): b = Bar() b + ["C", "E", "G"] b + ["F", "A", "C"] self.assertEqual([[0.0, ["C major triad"]], [0.25, ["F major triad"]]], b.determine_chords()) def test_determine_progression(self): b = Bar() b + ["C", "E", "G"] b + ["F", "A", "C"] self.assertEqual([[0.0, ["I"]], [0.25, ["IV"]]], b.determine_progression(True))
def test_augment_rest(self): b = Bar() b.place_notes("C-4", 4) b.place_rest(4) c = Bar() c.place_notes("C#-4", 4) c.place_rest(4) b.augment() self.assertEqual(b, c)
def test_get_note_names(self): b = Bar() b + 'C' b + 'A' self.assertEqual(['C', 'A'], b.get_note_names())
def test_determine_chords(self): b = Bar() b + ['C', 'E', 'G'] b + ['F', 'A', 'C'] self.assertEqual([[0.0, ['C major triad']], [0.25, ['F major triad']]], b.determine_chords())
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 test_determine_progression(self): b = Bar() b + ['C', 'E', 'G'] b + ['F', 'A', 'C'] self.assertEqual([[0.0, ['I']], [0.25, ['IV']]], b.determine_progression(True))
class test_Bar(unittest.TestCase): def setUp(self): self.b = Bar('C', (4, 4)) self.c = Bar('E', (2, 2)) self.meterless = Bar('C', (0, 0)) def test_place_notes_types(self): self.assertEqual(True, self.meterless + NoteContainer(['A', 'C'])) self.assertEqual(True, self.meterless + 'A') self.assertEqual(True, self.meterless + Note('A')) self.assertEqual(True, self.meterless + ['A', 'B']) self.assertEqual(True, self.meterless + [Note('A'), Note('B')]) def test_get_range(self): self.b + NoteContainer(['C', 'E']) self.assertEqual((Note('C'), Note('E')), self.b.get_range()) def test_set_item(self): b = Bar() b + ['A', 'C', 'E'] c = Bar() c + ['A', 'C', 'E'] self.assertEqual(b, c) c[0] = NoteContainer(['A', 'C', 'E']) self.assertEqual(b, c) c[0] = ['A', 'C', 'E'] self.assertEqual(b, c) c[0] = Note('A') c[0] = c[0][2] + NoteContainer(['C', 'E']) self.assertEqual(b, c) c[0] = Note('A') c[0] = c[0][2] + 'C' c[0] = c[0][2] + 'E' self.assertEqual(b, c) def test_key(self): self.assertEqual(self.b.key, Key('C')) self.assertEqual(self.c.key, Key('E')) def test_transpose(self): b = Bar() c = Bar() b + ['C', 'E', 'G'] c + ['E', 'G#', 'B'] b + ['F', 'A', 'C'] c + ['A', 'C#', 'E'] b.transpose('3', True) self.assertEqual(b, c) b.transpose('3', False) b.transpose('3') self.assertEqual(b, c) def test_augment(self): b = Bar() c = Bar() d = Bar() b + 'A' c + 'A#' d + 'A##' b.augment() self.assertEqual(b, c) b.augment() self.assertEqual(b, d) c.augment() self.assertEqual(c, d) def test_diminish(self): b = Bar() c = Bar() b + 'A' c + 'Ab' b.diminish() self.assertEqual(b, c) # def test_to_minor(self): # b = Bar() # c = Bar() # b + 'C' # c + 'A' # b.to_minor() # self.assertEqual(b, c) # # def test_to_major(self): # b = Bar() # c = Bar() # b + 'C' # c + 'A' # c.to_major() # self.assertEqual(b, c) def test_get_note_names(self): b = Bar() b + 'C' b + 'A' self.assertEqual(['C', 'A'], b.get_note_names()) def test_determine_chords(self): b = Bar() b + ['C', 'E', 'G'] b + ['F', 'A', 'C'] self.assertEqual([[0.0, ['C major triad']], [0.25, ['F major triad']]], b.determine_chords()) def test_determine_progression(self): b = Bar() b + ['C', 'E', 'G'] b + ['F', 'A', 'C'] self.assertEqual([[0.0, ['I']], [0.25, ['IV']]], b.determine_progression(True))
def setUp(self): self.b = Bar('C', (4, 4)) self.c = Bar('E', (2, 2)) self.meterless = Bar('C', (0, 0))
for chord in args.chords: chord_notes = chords.from_shorthand(chord) new_note = reduce_accidentals(tritone(chord_notes[0])) new_chord = new_note + '7' ii_chord_type_short, chord = reharmonize_v_to_ii_v(new_chord) print("{} {}".format(ii_chord_type_short, new_chord)) elif args.action == 'chord_modes': chords = [chords.determine_seventh(mode_func(args.key), shorthand=True)[0] for mode_func in MODE_CHORD_FUNCTIONS] print(', '.join(chords)) elif args.action == '12_bar_blues': for four_bars in twelve_bar_blues(args.key): print('\t'.join(four_bars)) elif args.action == 'blues_scale': print("{} blues scale: {}".format(args.key, ', '.join(blues_scale(args.key)))) elif args.action == '12_bar_blues_printout': from mingus.containers.note import QuarterNoteFactory as Q from mingus.core.chords import WholNoteChordFactory as WNC blues_scale = Blues(args.key) bar = Bar() bar.extend(Q(list(blues_scale.generate(4*12, undulating=True, starting_octave=4)))) bar.set_chord_notes([WNC(chord) for chord in twelve_bar_blues_chord_progression(args.key)]) result = from_Bar(bar) print(result) to_png(result, args.filename)
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'))
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 MIDI_to_Composition(self, file): (header, track_data) = self.parse_midi_file(file) 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 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 deltatime != 0: 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)