def test_simplest(self): l = mpd.lexer.Lexer("c4 r4") g = l.next() self.assertEquals(l.NOTE, g[0]) self.assertEquals(g[1].m_duration.get_rat_value(), mpd.Rat(1, 4)) g = l.next() self.assertEquals(l.REST, g[0]) self.assertEquals(g[1].m_duration.get_rat_value(), mpd.Rat(1, 4))
def test_whitespace(self): l = mpd.lexer.Lexer("\t\nc4 \n r4 \n") g = l.next() self.assertEquals(l.NOTE, g[0]) self.assertEquals(g[1].m_duration.get_rat_value(), mpd.Rat(1, 4)) g = l.next() self.assertEquals(l.REST, g[0]) self.assertEquals(g[1].m_duration.get_rat_value(), mpd.Rat(1, 4))
def test_m_notelen(self): lexer = mpd.lexer.Lexer("c8 d s16 r32") toc, toc_data = lexer.next() self.assertEquals(toc_data.m_duration.get_rat_value(), mpd.Rat(1, 8)) self.assertEquals(lexer.m_notelen.get_rat_value(), mpd.Rat(1, 8)) toc, toc_data = lexer.next() self.assertEquals(lexer.m_notelen.get_rat_value(), mpd.Rat(1, 8)) toc, toc_data = lexer.next() self.assertEquals(toc_data.m_duration.get_rat_value(), mpd.Rat(1, 16)) toc, toc_data = lexer.next() self.assertEquals(toc_data.m_duration.get_rat_value(), mpd.Rat(1, 32))
def delay(n, tempo): """ tempo is a dict of two integers """ track = mpd.Track() track.set_bpm(*tempo) #self.get_int('config/default_bpm')) track.note(mpd.Rat(n, 4), 80, 0) soundcard.synth.play_track(track)
def new_question(self, widget=None): def exception_cleanup(): soundcard.synth.stop() self.std_buttons_exception_cleanup() self.g_entry.set_text("") self.g_music_displayer.clear(2) # pop just in case there is something in the stack. self.g_flashbar.pop() try: g = self.m_t.new_question() if g == self.m_t.OK: self.m_t.play_question() self.g_music_displayer.display( self.m_t.m_P.get_music(), self.get_int('config/feta_font_size=20'), mpd.Rat(0, 1)) self.std_buttons_new_question() self.g_entry.set_text("") self.g_entry.grab_focus() except Exception, e: if not self.standard_exception_handler(e, exception_cleanup): raise
def play_question(self): cadence = self.m_cadence['music'][:] p = mpd.MusicalPitch.new_from_notename("c'") + self.m_tone if self.get_bool('random_tonic'): cadence = cadence.replace( "\\staff", "\\staff\\transpose %s" % self.m_transpose.get_octave_notename()) p.transpose_by_musicalpitch(self.m_transpose) m = mpd.parse_to_score_object(cadence) # Here we assume that we can check the first voice of the first # staff when finding the timepos and the duration of the last # tone in the cadence. But it is ok to have more staffs or voices # in the cadence, as long as the first assumption is true. staff = m.add_staff() voice = staff.add_voice() if 'tone_instrument' in self.m_cadence: try: instr = soundcard.find_midi_instrument_number( self.m_cadence.get("tone_instrument")) except KeyError: logging.warning("WARNING: Bad MIDI instrument name in «%s»" % self.m_P.m_filename) instr = cfg.get_int("config/preferred_instrument") else: instr = cfg.get_int("config/preferred_instrument") if self.get_bool('tone_in_cadence'): timepos = m.m_staffs[0].get_timeposes()[-1] last_len = m.m_staffs[0].m_voices[0].m_length - timepos else: timepos = m.m_staffs[0].m_voices[0].m_length last_len = mpd.Rat(1, 4) voice.set_elem([elems.Note(p, elems.Duration.new_from_rat(last_len))], timepos) tr = mpd.score_to_tracks(m) t = self.m_cadence.get('tempo', (60, 4)) tr[0].prepend_bpm(t[0], t[1]) tr[-1].prepend_patch(instr) soundcard.synth.play_track(*tr)
def test_skip(self): score = parse_to_score_object(r"\staff{ c s }") d = score.m_staffs[0].m_voices[0].m_tdict self.assert_(isinstance(d[mpd.Rat(0, 1)]['elem'], Stem)) self.assert_(isinstance(d[mpd.Rat(1, 4)]['elem'][0], Skip))
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 on_display_first_notes(self, _o): t = self.m_buf.get_text(self.m_buf.get_start_iter(), self.m_buf.get_end_iter(), True) self.save(t) self.g_displayer.display(t, 20, mpd.Rat(0, 1))