def test_dottt(self): t = mpd.music_to_tracklist(r"\staff { c4 r4. c4 }") self.assertEqual(t[0].str_repr(), "n48 d1/4 o48 d3/8 n48 d1/4 o48") t = mpd.music_to_tracklist(r"\staff { c4 r4.. c4 }") self.assertEqual(t[0].str_repr(), "n48 d1/4 o48 d7/16 n48 d1/4 o48") t = mpd.music_to_tracklist(r"\staff { c4.. c16 }") self.assertEqual(t[0].str_repr(), "n48 d7/16 o48 n48 d1/16 o48")
def test_dottt(self): t = mpd.music_to_tracklist(r"\staff { c4 r4. c4 }") self.assertEquals(t[0].str_repr(), "n48 d1/4 o48 d3/8 n48 d1/4 o48") t = mpd.music_to_tracklist(r"\staff { c4 r4.. c4 }") self.assertEquals(t[0].str_repr(), "n48 d1/4 o48 d7/16 n48 d1/4 o48") t = mpd.music_to_tracklist(r"\staff { c4.. c16 }") self.assertEquals(t[0].str_repr(), "n48 d7/16 o48 n48 d1/16 o48")
def test_parse_times_do_not_nest(self): s = "\\staff { c4 \\times 3/2 { \\times 3/2 { c4 c4 } } }\n" try: mpd.music_to_tracklist(s) except ParseError as e: self._check(e, s, '\\times 3/2 {') self.assertEqual((e.m_linepos1, e.m_linepos2), (25, 37)) else: self.assertFalse("No exception raised!")
def test_parse_addvoice_before_staff(self): s = " \\addvoice { \n" \ " c4 d }" try: mpd.music_to_tracklist(s) except ParseError as e: self._check(e, s, '\\addvoice') self.assertEqual((e.m_linepos1, e.m_linepos2), (3, 12)) else: self.assertFalse("No exception raised!")
def test_parse_err1(self): s = "\\staff{ c4 < d < e \n" \ "}" try: mpd.music_to_tracklist(s) except ParseError as e: self._check(e, s, '<') self.assertEqual((e.m_linepos1, e.m_linepos2), (15, 16)) else: self.assertFalse("No exception raised!")
def test_track2(self): self.do_file(""" header { random_transpose = no } question { music = music("\staff{ c'' }" + "\\addvoice{ e' }" + "\staff{ c }") } """) self.p._idx = 0 mpd.music_to_tracklist(self.p.get_question()['music'].get_mpd_music_string(self.p))
def test_parse_err3(self): s = "\\staff{ \n" \ " c4 d > \times 3/2" try: mpd.music_to_tracklist(s) except ParseError as e: self._check(e, s, '>') self.assertEqual((e.m_linepos1, e.m_linepos2), (7, 8)) else: self.assertFalse("No exception raised!")
def test_parse_brace_not_allowed(self): s = "\\staff{ c4 { e \n" \ "}" try: mpd.music_to_tracklist(s) except ParseError as e: self._check(e, s, '{') self.assertEqual((e.m_linepos1, e.m_linepos2), (11, 12)) else: self.assertFalse("No exception raised!")
def test_track2(self): self.do_file(""" header { random_transpose = no } question { music = music("\staff{ c'' }" + "\\addvoice{ e' }" + "\staff{ c }") } """) self.p._idx = 0 mpd.music_to_tracklist( self.p.get_question()['music'].get_mpd_music_string(self.p))
def test_track3_2(self): self.p = lessonfile.QuestionsLessonfile() self.p.parse_string(r""" header { random_transpose = no } question { music = music("\staff{ c''1 c''1 }" + "\staff{ r4 r g2 g1 }" + "\staff{ r4 r r c c1}") } """) self.p._idx = 0 tracklist = mpd.music_to_tracklist(self.p.get_question()['music'].get_mpd_music_string(self.p)) track_fasit = ["n72 d1/1 o72 n72 d1/1 o72", "d1/2 n55 d1/2 o55 n55 d1/1 o55", "d3/4 n48 d1/4 o48 n48 d1/1 o48"] for idx, correct in enumerate(track_fasit): self.assertEquals(tracklist[idx].str_repr(), correct) for idx in range(3): tracklist[idx].prepend_patch(idx + 1) track_fasit[idx] = "p%i " % (idx + 1) + track_fasit[idx] for idx, correct in enumerate(track_fasit): self.assertEquals(tracklist[idx].str_repr(), correct) self.assertEquals(MidiEventStream(*tracklist).str_repr(details=1), "p0:1 v0:100 n0:72 d1/2 " "p1:2 v1:100 n1:55 d1/4 " "p2:3 v2:100 n2:48 d1/4 " "o72 o55 o48 " "n0:72 n1:55 n2:48 d1/1 " "o72 o55 o48")
def test_track3(self): self.do_file(""" header { random_transpose = no } question { music = music("\staff{ c''1 c''1 }" + "\\addvoice{ r4 e'2. e'1 }" + "\staff{ r4 r g2 g1 }" + "\\addvoice{ r4 r r c c1}") } """) self.p._idx = 0 tracklist = mpd.music_to_tracklist(self.p.get_question()['music'].get_mpd_music_string(self.p)) track_fasit = ["n72 d1/1 o72 n72 d1/1 o72", "d1/4 n64 d3/4 o64 n64 d1/1 o64", "d1/2 n55 d1/2 o55 n55 d1/1 o55", "d3/4 n48 d1/4 o48 n48 d1/1 o48"] for idx, correct in enumerate(track_fasit): self.assertEqual(tracklist[idx].str_repr(), correct) for idx in range(4): tracklist[idx].prepend_patch(idx + 1) track_fasit[idx] = "p%i " % (idx + 1) + track_fasit[idx] for idx, correct in enumerate(track_fasit): self.assertEqual(tracklist[idx].str_repr(), correct) self.assertEqual( MidiEventStream(*tracklist).str_repr(details=1), "p0:1 p1:2 p2:3 p3:4 v0:100 n0:72 d1/4 " "v1:100 n1:64 d1/4 " "v2:100 n2:55 d1/4 " "v3:100 n3:48 d1/4 " "o72 o64 o55 o48 " "n0:72 n1:64 n2:55 n3:48 d1/1 " "o72 o64 o55 o48")
def test_parse_times_do_not_nest(self): s = "\\staff { c4 \\times 3/2 { \\times 3/2 { c4 c4 } } }\n" try: t = mpd.music_to_tracklist(s) except ParseError, e: self._check(e, s, '\\times 3/2 {') self.assertEquals((e.m_linepos1, e.m_linepos2), (25, 37))
def test_track3_2(self): self.do_file(r""" header { random_transpose = no } question { music = music("\staff{ c''1 c''1 }" + "\staff{ r4 r g2 g1 }" + "\staff{ r4 r r c c1}") } """) self.p._idx = 0 tracklist = mpd.music_to_tracklist( self.p.get_question()['music'].get_mpd_music_string(self.p)) track_fasit = [ "n72 d1/1 o72 n72 d1/1 o72", "d1/2 n55 d1/2 o55 n55 d1/1 o55", "d3/4 n48 d1/4 o48 n48 d1/1 o48" ] for idx, correct in enumerate(track_fasit): self.assertEquals(tracklist[idx].str_repr(), correct) for idx in range(3): tracklist[idx].prepend_patch(idx + 1) track_fasit[idx] = "p%i " % (idx + 1) + track_fasit[idx] for idx, correct in enumerate(track_fasit): self.assertEquals(tracklist[idx].str_repr(), correct) self.assertEquals( MidiEventStream(*tracklist).str_repr(details=1), "p0:1 p1:2 p2:3 v0:100 n0:72 d1/2 " "v1:100 n1:55 d1/4 " "v2:100 n2:48 d1/4 " "o72 o55 o48 " "n0:72 n1:55 n2:48 d1/1 " "o72 o55 o48")
def test_parse_times_do_not_nest(self): s = "\\staff { c4 \\times 3/2 { \\times 3/2 { c4 c4 } } }\n" try: t = mpd.music_to_tracklist(s) except ParseError, e: self._check(e, s, '\\times 3/2 {') self.assertEquals((e.m_linepos1, e.m_linepos2), (25, 37))
def test_parse_addvoice_before_staff(self): s = " \\addvoice { \n" \ " c4 d }" try: t = mpd.music_to_tracklist(s) except ParseError, e: self._check(e, s, '\\addvoice') self.assertEquals((e.m_linepos1, e.m_linepos2), (3, 12))
def test_parse_err1(self): s = "\\staff{ c4 < d < e \n" \ "}" try: t = mpd.music_to_tracklist(s) except ParseError, e: self._check(e, s, '<') self.assertEquals((e.m_linepos1, e.m_linepos2), (15, 16))
def test_parse_err1(self): s = "\\staff{ c4 < d < e \n" \ "}" try: t = mpd.music_to_tracklist(s) except ParseError, e: self._check(e, s, '<') self.assertEquals((e.m_linepos1, e.m_linepos2), (15, 16))
def test_parse_err3(self): s = "\\staff{ \n" \ " c4 d > \times 3/2" try: t = mpd.music_to_tracklist(s) except ParseError, e: self._check(e, s, '>') self.assertEquals((e.m_linepos1, e.m_linepos2), (7, 8))
def test_parse_brace_not_allowed(self): s = "\\staff{ c4 { e \n" \ "}" try: t = mpd.music_to_tracklist(s) except ParseError, e: self._check(e, s, '{') self.assertEquals((e.m_linepos1, e.m_linepos2), (11, 12))
def test_parse_addvoice_before_staff(self): s = " \\addvoice { \n" \ " c4 d }" try: t = mpd.music_to_tracklist(s) except ParseError, e: self._check(e, s, '\\addvoice') self.assertEquals((e.m_linepos1, e.m_linepos2), (3, 12))
def test_parse_brace_not_allowed(self): s = "\\staff{ c4 { e \n" \ "}" try: t = mpd.music_to_tracklist(s) except ParseError, e: self._check(e, s, '{') self.assertEquals((e.m_linepos1, e.m_linepos2), (11, 12))
def test_parse_err3(self): s = "\\staff{ \n" \ " c4 d > \times 3/2" try: t = mpd.music_to_tracklist(s) except ParseError, e: self._check(e, s, '>') self.assertEquals((e.m_linepos1, e.m_linepos2), (7, 8))
def play_music3(music, tempo, instrument, start=None, end=None): """ Either a tuple (patch, velocity) or a tuple of 6: (patch1, velocity1, p2, v2, p3, v3) """ if len(instrument) == 2: instrument = instrument * 3 tracklist = mpd.music_to_tracklist(music, start, end) tracklist[0].prepend_patch(instrument[4]) tracklist[0].prepend_volume(instrument[5]) a, b = tempo # FIXME the bpm api is wrong. We set the tempo to one track, and it works for all. tracklist[0].prepend_bpm(a, b) for track in tracklist[:-1][1:]: track.prepend_patch(instrument[2]) track.prepend_volume(instrument[3]) tracklist[-1].prepend_patch(instrument[0]) tracklist[-1].prepend_volume(instrument[1]) soundcard.synth.play_track(*tracklist)
def test_track3_1(self): self.do_file(""" header { random_transpose = no } question { music = music("\staff{ c''1 c''1 }" + "\staff{ r4 r g2 g1 }") } """) self.p._idx = 0 tracklist = mpd.music_to_tracklist(self.p.get_question()['music'].get_mpd_music_string(self.p)) track_fasit = ["n72 d1/1 o72 n72 d1/1 o72", "d1/2 n55 d1/2 o55 n55 d1/1 o55"] for idx, correct in enumerate(track_fasit): self.assertEquals(tracklist[idx].str_repr(), correct) for idx in range(2): tracklist[idx].prepend_patch(idx + 1) track_fasit[idx] = "p%i " % (idx + 1) + track_fasit[idx] for idx, correct in enumerate(track_fasit): self.assertEquals(tracklist[idx].str_repr(), correct) self.assertEquals(MidiEventStream(*tracklist).str_repr(details=1), "p0:1 p1:2 v0:100 n0:72 d1/2 v1:100 n1:55 d1/2 o72 o55 n0:72 n1:55 d1/1 o72 o55")
def test_times(self): t = mpd.music_to_tracklist(r"\staff{ \times 2/3{c8 d e} }") self.assertEqual( t[0].str_repr(), "n48 d1/12 o48 n50 d1/12 o50 n52 d1/12 o52") parse_to_score_object(r"\staff{ \times 2/3{c8 d e} }")
def test_whitespace_at_end(self): s = " \\staff { \n" \ " c4 d } \n" \ "\t \n" mpd.music_to_tracklist(s)
def on_play_first(self, _o): t = self.m_buf.get_text(self.m_buf.get_start_iter(), self.m_buf.get_end_iter(), True) tr = mpd.music_to_tracklist(t, mpd.Rat(0, 1), mpd.Rat(1, 8)) soundcard.synth.play_track(*tr)
def test_rest(self): # cmp to 1 because this is one track self.assertEquals(len(mpd.music_to_tracklist(r"\staff {r}")), 1)
def test_dotted_rest(self): self.assertEquals(len(mpd.music_to_tracklist(r"\staff {r4.}")), 1) self.assertEquals(len(mpd.music_to_tracklist(r"\staff {r4..}")), 1)
def test_times(self): t = mpd.music_to_tracklist(r"\staff{ \times 2/3{c8 d e} }") self.assertEquals(t[0].str_repr(), "n48 d1/12 o48 n50 d1/12 o50 n52 d1/12 o52") score = parse_to_score_object(r"\staff{ \times 2/3{c8 d e} }")
def test_whitespace_at_end(self): s = " \\staff { \n" \ " c4 d } \n" \ "\t \n" t = mpd.music_to_tracklist(s)
def test_dotted_rest(self): self.assertEqual(len(mpd.music_to_tracklist(r"\staff {r4.}")), 1) self.assertEqual(len(mpd.music_to_tracklist(r"\staff {r4..}")), 1)
def test_rest(self): # cmp to 1 because this is one track self.assertEqual(len(mpd.music_to_tracklist(r"\staff {r}")), 1)
def on_play_first(self, _o): t = self.m_buf.get_text(self.m_buf.get_start_iter(), self.m_buf.get_end_iter(), True) tr = mpd.music_to_tracklist(t, mpd.Rat(0, 1), mpd.Rat(1, 8)) soundcard.synth.play_track(*tr)