def test_m21_2_timeline1(): score = m21.converter.parse( str(Path("tests/test_musicxml/test_score1.musicxml"))) measure = score.parts[0].getElementsByClass("Measure")[4] gns = measure.getElementsByClass("GeneralNote") tim = m21_2_timeline(gns) assert list(tim.events) == [ (0.0, [62, 65, 69]), (1.0, [62, 65, 69]), (2.0, [62, 65, 72]), (3.0, [60, 64, 72]), ] measure = score.parts[0].getElementsByClass("Measure")[6] gns = measure.getElementsByClass("GeneralNote") tim = m21_2_timeline(gns) assert len(tim) == 6
def test_m21_2_rhythmtree(): score = m21.converter.parse( str(Path("tests/test_musicxml/test_score1.musicxml"))) measures = score.parts[0].getElementsByClass("Measure") for m in measures: gns = m.getElementsByClass("GeneralNote") rt = m21_2_rhythmtree(gns, div_preferences=[2, 2, 2, 2, 2, 2, 2]) assert rt.get_timeline(0, 4) == m21_2_timeline(gns)
def test_m21_2_timeline3(): score = m21.converter.parse( str(Path("tests/test_musicxml/test_score3.musicxml"))) measures = score.parts[0].getElementsByClass("Measure") for m in measures: gns = m.getElementsByClass("GeneralNote") tim = m21_2_timeline(gns) assert tim.end == 4
def get_voices(self): voices = [] for ip, p in enumerate(self.m21_score.parts): voices.append([]) for im, m in enumerate(p.getElementsByClass(m21.stream.Measure)): # consider only the first voice (TO UPDATE) voice = m.getElementsByClass(m21.stream.Voice)[0] notes = voice.getElementsByClass("GeneralNote") voices[ip].extend(notes) return [m21u.m21_2_timeline(v) for v in voices]
def get_timelines(self): # return one timeline for each voice. # TODO: Consider all voices, for now works with only the first of each part # TODO: merge if there are continuations at the beginning of the measures timelines = [] for ip, p in enumerate(self.m21_score.parts): voice_tim = None # empty timeline for the voice for im, m in enumerate(p.getElementsByClass(m21.stream.Measure)): # consider only the first voice (TO UPDATE) voice = m.getElementsByClass(m21.stream.Voice)[0] gn_list = voice.getElementsByClass(m21.note.GeneralNote) m_tim = m21u.m21_2_timeline(gn_list).shift_and_rescale( 0, 1) # force each measure to be in the interval 0-1 voice_tim = m_tim if voice_tim is None else voice_tim + m_tim timelines.append(voice_tim) return timelines