def test_close_polyphonic_context(self): context = midi2lily.PolyphonicContext() # voice 1 melody_expression = midi2lily.CompoundExpression() melody_expression.add( midi2lily.Note(midi2lily.Pitch(72), midi2lily.Duration(Fraction(4, 4)))) context.add(melody_expression) # voice 2 (unbalanced) rythm_expression = midi2lily.CompoundExpression() rythm_expression.add( midi2lily.Note(midi2lily.Pitch(64), midi2lily.Duration(Fraction(2, 4)))) context.add(rythm_expression) # before closing the context is unbalanced self.assertTrue(not context.is_balanced()) context.close() # after closing, the context is balanced by adding a rest self.assertTrue(context.is_balanced()) self.assertEqual(str(context), "<<\n{\nc''1 }\n\\\\\n{\ne'2 r2 }\n>>")
def test_polyphonic_context_is_balanced(self): context = midi2lily.PolyphonicContext() # voice 1 melody_expression = midi2lily.CompoundExpression() melody_expression.add( midi2lily.Note(midi2lily.Pitch(72), midi2lily.Duration(Fraction(4, 4)))) context.add(melody_expression) # voice 2 rythm_expression = midi2lily.CompoundExpression() rythm_expression.add( midi2lily.Note(midi2lily.Pitch(64), midi2lily.Duration(Fraction(2, 4)))) rythm_expression.add( midi2lily.Note(midi2lily.Pitch(67), midi2lily.Duration(Fraction(2, 4)))) context.add(rythm_expression) self.assertTrue(context.is_balanced())
def test_polyphonic_context(self): context = midi2lily.PolyphonicContext() # voice 1 melody_expression = midi2lily.CompoundExpression() melody_expression.add( midi2lily.Note(midi2lily.Pitch(72), midi2lily.Duration.get_duration(2, 1, 4))) context.add(melody_expression) # voice 2 rythm_expression = midi2lily.CompoundExpression() rythm_expression.add( midi2lily.Note(midi2lily.Pitch(64), midi2lily.Duration.get_duration(1, 1, 4))) rythm_expression.add( midi2lily.Note(midi2lily.Pitch(67), midi2lily.Duration.get_duration(1, 1, 4))) context.add(rythm_expression) self.assertEqual(str(context), "<<\n{\nc''2 }\n\\\\\n{\ne'4 g'4 }\n>>")
def test_bass_clef(self): expression = midi2lily.CompoundExpression() expression.add( midi2lily.Note(midi2lily.Pitch(54), midi2lily.Duration.get_duration(1, 1, 4))) expression.add( midi2lily.Note(midi2lily.Pitch(60), midi2lily.Duration.get_duration(1, 1, 4))) self.assertEqual(len(expression.pitches()), 2) self.assertEqual(expression.lowest_pitch(), 54) self.assertEqual(expression.highest_pitch(), 60) self.assertEqual(expression.get_clef(), 'bass')
def test_duplicate_pitches(self): expression = midi2lily.CompoundExpression() expression.add( midi2lily.Note(midi2lily.Pitch(60), midi2lily.Duration.get_duration(1, 1, 4))) expression.add( midi2lily.Note(midi2lily.Pitch(60), midi2lily.Duration.get_duration(1, 1, 4))) self.assertEqual(len(expression.pitches()), 1) self.assertEqual(expression.lowest_pitch(), 60) self.assertEqual(expression.highest_pitch(), 60) self.assertEqual(expression.get_clef(), None)
def test_divisable_expression(self): expression = midi2lily.CompoundExpression() expression.add( midi2lily.Note(midi2lily.Pitch(79), midi2lily.Duration.get_duration(1, 1, 1))) expression.add( midi2lily.Note(midi2lily.Pitch(79), midi2lily.Duration.get_duration(1, 1, 1))) self.assertEqual(str(expression), "{\ng''1 g''1 }") new_expression = expression.split_at(1) self.assertEqual(str(expression), "{\ng''1 }") self.assertEqual(str(new_expression), "{\ng''1 }")
def test_expression_with_notes(self): expression = midi2lily.CompoundExpression() note = midi2lily.Note(midi2lily.Pitch(60), midi2lily.Duration.get_duration(1, 1, 4)) expression.add(note) self.assertEqual(str(expression), "{\nc'4 }")
def testOneNoteExpression(self): expression = midi2lily.CompoundExpression() note = midi2lily.Note(midi2lily.Pitch(60), midi2lily.Duration.get_duration(1, 1, 4)) expression.add(note) self.assertEqual(expression.length(), Fraction(1, 4))
def testMultipleMeasuresExpression(self): expression = midi2lily.CompoundExpression() for _ in range(12): note = midi2lily.Note(midi2lily.Pitch(60), midi2lily.Duration.get_duration(1, 1, 4)) expression.add(note) self.assertEqual(expression.length(), Fraction(12, 4))
def test_file_with_staff_with_notes(self): file = midi2lily.File("1") staff = midi2lily.Staff("trumpet") # TODO: hide children property staff.add( midi2lily.Note(midi2lily.Pitch(79), midi2lily.Duration.get_duration(4, 4, 4))) staff.add( midi2lily.Note(midi2lily.Pitch(79), midi2lily.Duration.get_duration(4, 4, 4))) staff.add( midi2lily.Note(midi2lily.Pitch(79), midi2lily.Duration.get_duration(6, 4, 4))) staff.add( midi2lily.Note(midi2lily.Pitch(77), midi2lily.Duration.get_duration(2, 4, 4))) staff.add( midi2lily.Note(midi2lily.Pitch(76), midi2lily.Duration.get_duration(3, 4, 4))) staff.add( midi2lily.Note(midi2lily.Pitch(74), midi2lily.Duration.get_duration(1, 4, 4))) staff.add( midi2lily.Note(midi2lily.Pitch(72), midi2lily.Duration.get_duration(3, 4, 4))) staff.add( midi2lily.Note(midi2lily.Pitch(76), midi2lily.Duration.get_duration(1, 4, 4))) staff.add( midi2lily.Note(midi2lily.Pitch(74), midi2lily.Duration.get_duration(4, 4, 4))) staff.add( midi2lily.Note(midi2lily.Pitch(67), midi2lily.Duration.get_duration(4, 4, 4))) file.add(staff) self.assertEqual( str(file), "\\version \"1\"\n\n\\new Staff = \"trumpet\" \\relative c' {\ng'4 g g4. f8 |\ne8. d16 c8. e16 d4 g, |\n}" )
def test_non_divisable_expression2(self): expression = midi2lily.CompoundExpression() expression.add( midi2lily.Note(midi2lily.Pitch(79), midi2lily.Duration.get_duration(4, 4, 4))) self.assertEqual(len(expression._children), 1) new_expression = expression.split_at(0) self.assertEqual(len(expression._children), 0) self.assertEqual(str(new_expression), "{\ng''4 }")
def test_two_measures(self): expression = midi2lily.CompoundExpression() expression.add( midi2lily.Note(midi2lily.Pitch(79), midi2lily.Duration.get_duration(4, 4, 4))) expression.add( midi2lily.Note(midi2lily.Pitch(79), midi2lily.Duration.get_duration(4, 4, 4))) expression.add( midi2lily.Note(midi2lily.Pitch(79), midi2lily.Duration.get_duration(4, 4, 4))) expression.add( midi2lily.Note(midi2lily.Pitch(79), midi2lily.Duration.get_duration(4, 4, 4))) expression.add( midi2lily.Note(midi2lily.Pitch(79), midi2lily.Duration.get_duration(4, 2, 4))) expression.add( midi2lily.Note(midi2lily.Pitch(79), midi2lily.Duration.get_duration(4, 2, 4))) self.assertEqual(str(expression), "{\ng''4 g''4 g''4 g''4 g''2 g''2 }") new_expression = expression.split_at(1) self.assertEqual(str(expression), "{\ng''4 g''4 g''4 g''4 }") self.assertEqual(str(new_expression), "{\ng''2 g''2 }")
def test_duration_crossing_measure(self): # 1/2 note duration... note = midi2lily.Note(midi2lily.Pitch(60), midi2lily.Duration(Fraction(1, 2))) # ...rendered in 4/4 time at beat 3 of first measure... context = midi2lily.RenderContext() context.time_signature = midi2lily.TimeSignature(4, 4) context.position = Fraction(3, 4) # should be rendered as tied 1/4 note durations when crossing the measure self.assertEqual(note.__str__(context), "c4~ | 4")
def testNote(self): pitch = midi2lily.Pitch(60) duration = midi2lily.Duration.get_duration(1, 1, 4) note = midi2lily.Note(pitch, duration) self.assertEqual(str(note), "c'4")
def test_staff_with_notes(self): staff = midi2lily.Staff("trumpet") note = midi2lily.Note(midi2lily.Pitch(60), midi2lily.Duration.get_duration(1, 1, 4)) staff.add(note) self.assertEqual(str(staff), "\\new Staff = \"trumpet\" {\nc\'4 }")