Exemplo n.º 1
0
class TestOffset(XMLTestCase):
    def setUp(self) -> None:
        self.score = TreeScoreTimewise()

    def test_1(self):
        sf = SimpleFormat(quarter_durations=[1, 0.25, 2.25, 0.5, 1, 2])
        sf.to_stream_voice(1).add_to_score(self.score)
        xml_path = path + '_test_1.xml'
        self.score.write(xml_path)
        chords = []
        for measure in self.score.get_children_by_type(TreeMeasure):
            for part in measure.get_children_by_type(TreePart):
                for staff in part.tree_part_staves.values():
                    chords.extend(staff.chords)
        expected = [0, Fraction(1, 1), Fraction(5, 4), Fraction(2, 1), Fraction(7, 2), 0, Fraction(1, 1),
                    Fraction(3, 1)]
        actual = [tree_chord.offset for tree_chord in chords]
        self.assertEqual(expected, actual)

    def test_2(self):
        sf_1 = SimpleFormat(quarter_durations=[1, 0.25, 2.25])
        sf_2 = SimpleFormat(quarter_durations=[0.25, 2.25, 1])
        sf_3 = SimpleFormat(quarter_durations=[2.25, 1, 0.25])
        sf_1.to_stream_voice().add_to_score(self.score, part_number=1, staff_number=1)
        sf_2.to_stream_voice().add_to_score(self.score, part_number=1, staff_number=2)
        sf_3.to_stream_voice().add_to_score(self.score, part_number=1, staff_number=3)
        xml_path = path + '_test_2.xml'
        self.score.write(xml_path)
        chord_offsets = {}
        for measure in self.score.get_children_by_type(TreeMeasure):
            for part in measure.get_children_by_type(TreePart):
                for key in part.tree_part_staves.keys():
                    staff = part.tree_part_staves[key]
                    chord_offsets[key] = [tree_chord.offset for tree_chord in staff.chords]
        expected = {1: [0, Fraction(1, 1), Fraction(5, 4), Fraction(2, 1), Fraction(7, 2)],
                    2: [0, Fraction(1, 4), Fraction(1, 1), Fraction(5, 2), Fraction(7, 2)],
                    3: [0, Fraction(2, 1), Fraction(9, 4), Fraction(3, 1), Fraction(13, 4), Fraction(7, 2)]}
        actual = chord_offsets
        self.assertEqual(expected, actual)

    def test_3(self):
        sf_1 = SimpleFormat(quarter_durations=[1, 0.25, 2.25])
        sf_2 = SimpleFormat(quarter_durations=[0.25, 2.25, 1], midis=[60, 60, 60])
        sf_1.to_stream_voice(1).add_to_score(self.score, part_number=1, staff_number=1)
        sf_2.to_stream_voice(2).add_to_score(self.score, part_number=1, staff_number=1)
        xml_path = path + '_test_3.xml'
        self.score.write(xml_path)
        chord_offsets = {}
        for measure in self.score.get_children_by_type(TreeMeasure):
            for part in measure.get_children_by_type(TreePart):
                for staff in part.tree_part_staves.values():
                    for key in staff.tree_part_voices.keys():
                        voice = staff.tree_part_voices[key]
                        chord_offsets[key] = [tree_chord.offset for tree_chord in voice.chords]
        expected = {1: [0, Fraction(1, 1), Fraction(5, 4), Fraction(2, 1), Fraction(7, 2)],
                    2: [Fraction(0, 1), Fraction(1, 4), Fraction(1, 1), Fraction(5, 2), Fraction(7, 2)]}

        actual = chord_offsets
        self.assertEqual(actual, expected)
class Test(TestCase):
    def setUp(self) -> None:
        random.seed(1)
        durations = [random.random() + random.random() for i in range(10)]
        self.sf = SimpleFormat(quarter_durations=durations)
        self.score = TreeScoreTimewise()

    def test_1(self):
        xml_path = path + '_test_1.xml'
        self.sf.to_stream_voice().add_to_score(self.score, part_number=1)
        self.sf.to_stream_voice().add_to_score(self.score, part_number=2)
        for measure in self.score.get_children_by_type(TreeMeasure):
            measure.get_children_by_type(TreePart)[-1].forbidden_divisions = [
                8
            ]
        self.score.write(xml_path)
        TestScore().assert_template(xml_path)

    def test_2(self):
        xml_path = path + '_test_2.xml'
        self.score.max_division = 6
        self.sf.to_stream_voice().add_to_score(self.score, part_number=1)
        self.sf.to_stream_voice().add_to_score(self.score, part_number=2)
        for measure in self.score.get_children_by_type(TreeMeasure):
            measure.get_children_by_type(TreePart)[-1].forbidden_divisions = [
                5
            ]
        self.score.write(xml_path)
        TestScore().assert_template(xml_path)

    def test_3(self):
        xml_path = path + '_test_3.xml'
        self.score.max_division = 6
        self.score.forbidden_divisions = [8]
        self.sf.to_stream_voice().add_to_score(self.score, part_number=1)
        self.score.write(xml_path)
        TestScore().assert_template(xml_path)
Exemplo n.º 3
0
class Test(TestCase):
    def setUp(self) -> None:
        self.score = TreeScoreTimewise()

    def test_1(self):
        self.score.add_measure(TreeMeasure(time=(5, 4)))
        sf = SimpleFormat(quarter_durations=[5])
        v = sf.to_stream_voice(1)
        v.add_to_score(self.score)

        self.score.finish()
        output = []
        for measure in self.score.get_children_by_type(TreeMeasure):
            for part in measure.get_children_by_type(TreePart):
                for beat in part.get_beats():
                    output.append([ch.quarter_duration for ch in beat.chords])

        result = [[Fraction(3, 1)], [], [], [Fraction(2, 1)], []]
        self.assertEqual(output, result)
Exemplo n.º 4
0
class Test(TestCase):
    def setUp(self) -> None:
        self.score = TreeScoreTimewise()
        sf = SimpleFormat(quarter_durations=30 * [4])
        v = sf.to_stream_voice(1)
        v.add_to_score(self.score)

    def test_1(self):
        self.score.page_style.orientation = 'landscape'

        for index, measure in enumerate(
                self.score.get_children_by_type(TreeMeasure)):
            if index % 4 == 0:
                measure.add_system_break()

        result_path = path + '_test_1'
        self.score.finish()
        partwise = self.score.to_partwise()
        partwise.write(path=result_path)
        # self.score.write(path=result_path)
        TestScore().assert_template(result_path=result_path)
Exemplo n.º 5
0
class Test(TestCase):
    def setUp(self) -> None:
        self.score = TreeScoreTimewise()
        sf = SimpleFormat(
            quarter_durations=[4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4])
        v = sf.to_stream_voice(1)
        v.add_to_score(self.score)

    def test_orientation(self):
        self.score.page_style.orientation = 'landscape'
        result_path = path + '_test_1'
        self.score.write(path=result_path)
        TestScore().assert_template(result_path=result_path)

    def test_2(self):
        for index, measure in enumerate(
                self.score.get_children_by_type(TreeMeasure)):
            if index % 4 == 0:
                measure.add_system_break()
        self.score.page_style.orientation = 'landscape'
        self.score.page_style.system_distance = 150
        result_path = path + '_test_2'
        self.score.write(path=result_path)
        TestScore().assert_template(result_path=result_path)

    def test_3(self):
        # self.score.page_style.format = 'landscape'
        self.score.get_measure(5).add_system_break()
        # p = self.score.get_measure(5).get_part(1).get_children_by_type(Print)[0]
        # s = p.add_child(SystemLayout())
        # s.add_child(SystemDistance(300))
        self.score.get_measure(10).add_system_break()
        self.score.get_measure(10).add_system_distance(200)
        self.score.get_measure(12).add_system_distance(200)
        # self.score.page_style.system_distance = 20

        result_path = path + '_test_3'
        self.score.write(path=result_path)
        TestScore().assert_template(result_path=result_path)
Exemplo n.º 6
0
class Test(TestCase):
    def setUp(self) -> None:
        self.score = TreeScoreTimewise()

    def test_1(self):
        sf = SimpleFormat(quarter_durations=[0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1])
        for index, chord in enumerate(sf.chords):
            chord.add_lyric(index + 1)
        v = sf.to_stream_voice(1)
        v.add_to_score(self.score, part_number=2)

        sf = SimpleFormat(quarter_durations=[0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1])
        v = sf.to_stream_voice(1)
        v.add_to_score(self.score)

        self.score.fill_with_rest()
        self.score.preliminary_adjoin_rests()
        self.score.add_beats()

        for measure in self.score.get_children_by_type(TreeMeasure):
            part = measure.get_part(2)
            for beat in part.get_beats():
                beat.max_division = 7

        result_path = path + '_test_1'
        # with self.assertWarns(UserWarning):
        #     self.score.write(path=result_path)
        self.score.write(path=result_path)
        TestScore().assert_template(result_path=result_path)

    def test_2(self):
        self.score.add_measure(TreeMeasure(time=(3, 4)))
        sf = SimpleFormat(quarter_durations=[0.5, 0.6, 0.7, 0.8])
        for index, chord in enumerate(sf.chords):
            chord.add_lyric(index + 1)
        v = sf.to_stream_voice(1)
        v.add_to_score(self.score)

        sf = SimpleFormat(quarter_durations=[0.5, 0.6, 0.7, 0.8])
        for index, chord in enumerate(sf.chords):
            chord.add_lyric(index + 1)
        v = sf.to_stream_voice(1)
        v.add_to_score(self.score, part_number=2)

        self.score.fill_with_rest()
        self.score.preliminary_adjoin_rests()
        self.score.add_beats()

        for measure in self.score.get_children_by_type(TreeMeasure):
            part = measure.get_part(2)
            for beat in part.get_beats():
                beat.max_division = 7

        self.score.quantize()

        result_path = path + '_test_2'
        # with self.assertWarns(UserWarning):
        #     self.score.write(path=result_path)
        self.score.write(path=result_path)
        TestScore().assert_template(result_path=result_path)

    def test_3(self):
        random.seed(3)
        durations = []
        while sum(durations) <= 16:
            duration = random.randrange(0, 2) + (random.random() / 2.)
            durations.append(Fraction(duration).limit_denominator(100))

        def add_to_score(part=1):
            sf = SimpleFormat(quarter_durations=durations)
            dynamics = itertools.cycle(['pppp', 'ppp', 'pp', 'p', 'mp', 'mf', 'f', 'ff', 'fff'])

            for index, chord in enumerate(sf.chords):
                chord.add_lyric(index + 1)
                d = chord.add_dynamics(dynamics.__next__())[0]
                d.relative_y = -20
                d.halign = 'center'
            v = sf.to_stream_voice(1)
            v.add_to_score(self.score, part)

        add_to_score(1)
        add_to_score(2)
        add_to_score(3)
        add_to_score(4)
        add_to_score(5)
        add_to_score(6)
        add_to_score(7)
        add_to_score(8)

        self.score.get_score_parts()[0].max_division = 8
        self.score.get_score_parts()[1].max_division = 7
        self.score.get_score_parts()[2].max_division = 6
        self.score.get_score_parts()[3].max_division = 5
        self.score.get_score_parts()[4].max_division = 4
        self.score.get_score_parts()[5].max_division = 3
        self.score.get_score_parts()[6].max_division = 2
        self.score.get_score_parts()[7].max_division = 1
        result_path = path + '_test_3'

        self.score.write(path=result_path)
        TestScore().assert_template(result_path=result_path)

    def test_4(self):
        sf = SimpleFormat(
            quarter_durations=[Fraction(3, 10), Fraction(3, 10), Fraction(3, 10), Fraction(3, 10), Fraction(3, 10),
                               Fraction(3, 2), Fraction(1, 2), Fraction(1, 3)])
        xml_path = path + '_test_4.xml'
        sf.to_stream_voice().add_to_score(self.score)
        self.score.write(xml_path)
        TestScore().assert_template(result_path=xml_path)

    def test_5(self):
        self.score.set_time_signatures(
            [Fraction(3, 2)])
        sf = SimpleFormat(quarter_durations=[0.666, 0.333, 0.5])
        xml_path = path + '_test_5.xml'
        sf.to_stream_voice().add_to_score(self.score)
        self.score.get_score_parts()[0].max_division = 1
        self.score.write(xml_path)
Exemplo n.º 7
0
class Test(XMLTestCase):
    def setUp(self) -> None:
        self.score = TreeScoreTimewise()

    def test_1(self):
        measure = self.score.add_measure()
        self.score.add_part()
        chord = TreeChord(quarter_duration=4, midis=[60])
        measure.get_part(1).add_chord(chord)
        measure.get_part(1).staves = 2
        chord.staff_number = 1
        xml_path = path + '_test_1.xml'
        self.score.write(xml_path)
        self.assertCompareFiles(xml_path)

    def test_2(self):
        measure = self.score.add_measure()
        self.score.add_part()
        chord = TreeChord(quarter_duration=4, midis=[60])
        measure.get_part(1).add_chord(chord)
        chord.staff_number = 2
        measure.get_part(1).staves = 2
        xml_path = path + '_test_2.xml'
        self.score.write(xml_path)
        self.assertCompareFiles(xml_path)

    def test_3(self):
        sf = SimpleFormat(quarter_durations=[0.25, 2.25, 0.5, 1, 2, Fraction(1, 3), Fraction(4, 3), Fraction(1, 3), 1])
        for index, chord in enumerate(sf.chords):
            if index in [1, 6]:
                chord.staff_number = 2
        sf.to_stream_voice(1).add_to_score(self.score, part_number=1)
        all_parts = [part for m in self.score.get_children_by_type(TreeMeasure) for part in
                     m.get_children_by_type(TreePart)]
        for part in all_parts:
            part.staves = 2

        xml_path = path + '_test_3.xml'
        self.score.write(xml_path)
        self.assertCompareFiles(xml_path)

    def test_4(self):
        sf = SimpleFormat(4)
        sf.to_stream_voice(1).add_to_score(self.score, part_number=1)
        score_part = self.score.get_score_parts()[0]
        score_part.number_of_staves = 2
        xml_path = path + '_test_4.xml'
        self.score.write(xml_path)
        self.assertCompareFiles(xml_path)

    def test_5(self):
        sf = SimpleFormat(4)
        sf.to_stream_voice(1).add_to_score(self.score, part_number=1)
        score_part = self.score.get_score_parts()[0]
        score_part.number_of_staves = 2
        clef = BASS_CLEF.__deepcopy__()
        clef.number = 2
        self.score.get_measure(1).get_part(1).add_clef(clef)
        xml_path = path + '_test_5.xml'
        self.score.write(xml_path)
        self.assertCompareFiles(xml_path)

    def test_6(self):
        r_sf = SimpleFormat(quarter_durations=[4])
        l_sf = SimpleFormat(quarter_durations=[4], midis=[60])
        r_sf.to_stream_voice().add_to_score(self.score, part_number=1, staff_number=1)
        l_sf.to_stream_voice().add_to_score(self.score, part_number=1, staff_number=2)
        xml_path = path + '_test_6.xml'
        self.score.write(xml_path)
        self.assertCompareFiles(xml_path)

    def test_7(self):
        r_sf = SimpleFormat(
            quarter_durations=[0.25, 2.25, 0.5, 1, 2, Fraction(1, 3), Fraction(4, 3), Fraction(1, 3), 1])
        l_sf = SimpleFormat(quarter_durations=[4, 5], midis=[72, 75])
        r_sf.to_stream_voice().add_to_score(self.score, part_number=1, staff_number=1)
        l_sf.to_stream_voice().add_to_score(self.score, part_number=1, staff_number=2)
        xml_path = path + '_test_7.xml'
        self.score.write(xml_path)
        self.assertCompareFiles(xml_path)

    def test_8(self):
        r_sf = SimpleFormat(
            quarter_durations=[0.25, 2.25, 0.5, 1, 2, Fraction(1, 3), Fraction(4, 3), Fraction(1, 3), 1])
        l_sf = SimpleFormat(quarter_durations=[4, 5], midis=[50, 55])
        r_sf.to_stream_voice().add_to_score(self.score, part_number=1, staff_number=1)
        l_sf.to_stream_voice().add_to_score(self.score, part_number=1, staff_number=2)
        clef = BASS_CLEF.__deepcopy__()
        clef.number = 2
        self.score.get_measure(1).get_part(1).add_clef(clef)
        xml_path = path + '_test_8.xml'
        self.score.write(xml_path)
        self.assertCompareFiles(xml_path)

    def test_9(self):
        r_sf = SimpleFormat(
            quarter_durations=[0.25, 2.25, 0.5, 1, 2, Fraction(1, 3), Fraction(4, 3), Fraction(1, 3), 1],
            midis=[60, 61, 62, 63])
        l_sf = SimpleFormat(quarter_durations=[4, 5], midis=[50, 55])
        r_sf.chords[1].manual_staff_number = 2
        r_sf.to_stream_voice().add_to_score(self.score, part_number=1, staff_number=1)
        l_sf.to_stream_voice().add_to_score(self.score, part_number=1, staff_number=2)
        clef = BASS_CLEF.__deepcopy__()
        clef.number = 2
        self.score.get_measure(1).get_part(1).add_clef(clef)
        xml_path = path + '_test_9.xml'
        self.score.write(xml_path)
        self.assertCompareFiles(xml_path)

    def test_10(self):
        # chord clef
        r_sf = SimpleFormat(
            quarter_durations=[4, 4],
            midis=[60, 61])
        l_sf = SimpleFormat(quarter_durations=[4, 4], midis=[50, 55])
        l_sf.chords[0].add_clef(BASS_CLEF)
        r_sf.to_stream_voice().add_to_score(self.score, part_number=1, staff_number=1)
        l_sf.to_stream_voice().add_to_score(self.score, part_number=1, staff_number=2)
        xml_path = path + '_test_10.xml'
        self.score.write(xml_path)
        self.assertCompareFiles(xml_path)

    def test_11(self):
        r_sf = SimpleFormat(
            quarter_durations=[0.25, 2.25, 0.5, 1, 2, Fraction(1, 3), Fraction(4, 3), Fraction(1, 3), 1],
            midis=[60, 61, 54, 63])
        l_sf = SimpleFormat(quarter_durations=[4, 5], midis=[72, 53])
        r_sf.chords[1].manual_staff_number = 2
        r_sf.auto_clef()
        l_sf.auto_clef()
        r_sf.to_stream_voice().add_to_score(self.score, part_number=1, staff_number=1)
        l_sf.to_stream_voice().add_to_score(self.score, part_number=1, staff_number=2)

        xml_path = path + '_test_11.xml'
        self.score.write(xml_path)
        self.assertCompareFiles(xml_path)

    def test_12(self):
        r_sf_2 = SimpleFormat(
            quarter_durations=[0.25, 2.25, 0.5, 1, 2, Fraction(1, 3), Fraction(4, 3), Fraction(1, 3), 1],
            midis=[60, 61, 54, 63])
        r_sf_1 = SimpleFormat(
            quarter_durations=[0.25, 2.25, 0.5, 1, 2, Fraction(1, 3), Fraction(4, 3), Fraction(1, 3), 1],
            midis=[70, 71, 64, 73])
        l_sf_1 = SimpleFormat(quarter_durations=[4, 5], midis=[50, 35])
        l_sf_2 = SimpleFormat(quarter_durations=[5, 4], midis=[44, 60])
        r_sf_2.auto_clef()
        r_sf_1.auto_clef()
        l_sf_1.auto_clef()
        l_sf_2.auto_clef()
        r_sf_1.to_stream_voice(2).add_to_score(self.score, part_number=1, staff_number=1)
        r_sf_2.to_stream_voice(1).add_to_score(self.score, part_number=1, staff_number=1)
        l_sf_1.to_stream_voice(2).add_to_score(self.score, part_number=1, staff_number=2)
        l_sf_2.to_stream_voice(1).add_to_score(self.score, part_number=1, staff_number=2)

        xml_path = path + '_test_12.xml'
        self.score.write(xml_path)
        self.assertCompareFiles(xml_path)

    def test_13(self):
        # accidental
        r_sf = SimpleFormat(quarter_durations=[4, 4, 4, 4], midis=[61, 63, 64, 67])
        l_sf = SimpleFormat(quarter_durations=[4, 4, 4, 4], midis=[60, 64, 63, 64])
        r_sf.to_stream_voice().add_to_score(self.score, part_number=1, staff_number=1)
        l_sf.to_stream_voice().add_to_score(self.score, part_number=1, staff_number=2)

        xml_path = path + '_test_13.xml'
        self.score.write(xml_path)
        self.assertCompareFiles(xml_path)