示例#1
0
    def test_6(self):
        def get_chord(node):
            output = fm.get_multi_chord_midis_with_range_factor(range_factor=1, microtone=4)
            transposition = node.midi_value - output[3]
            output = [midi + transposition for midi in output]
            return output

        fm = FractalMusic(proportions=(1, 2, 3, 4, 5, 6, 7), tree_permutation_order=(2, 6, 4, 1, 3, 7, 5),
                          quarter_duration=30, tempo=72)
        fm.midi_generator.midi_range = (62, 62 + 11)
        fm.midi_generator.microtone = 4
        fm.add_layer()
        fm.quantize_children(grid_size=1)

        def make_breathe(nodes, proportions, breakpoints):
            breath_quarter_duration = sum([node.quarter_duration for node in nodes])
            breathe = Breathe(proportions=proportions, breakpoints=breakpoints,
                              quarter_duration=breath_quarter_duration,
                              quantize=1)

            parent_chord_field = ChordField(duration_generator=breathe.duration_generator.__deepcopy__())
            for i in range(len(nodes)):
                node = selected_nodes[i]
                start_chord = get_chord(node)
                next_node = node.next_sibling
                if next_node:
                    end_chord = get_chord(next_node)
                else:
                    end_chord = start_chord

                chord_field = ChordField(
                    quarter_duration=node.quarter_duration,
                    midi_generator=ValueGenerator(RandomInterpolation(start=start_chord, end=end_chord, seed=10)),
                    long_ending_mode='self_extend',
                    short_ending_mode='self_shrink')
                parent_chord_field.add_child(chord_field)

            return parent_chord_field

        selected_nodes = fm.get_children()[2:5]
        # print(sum([node.quarter_duration for node in selected_nodes]))
        proportions = (1, 10, 1, 7, 1)
        breakpoints = (1, Fraction(1, 7), 1)
        breathe = make_breathe(nodes=selected_nodes, proportions=proportions,
                               breakpoints=breakpoints)
        # print(breathe.quarter_duration)
        fm.merge_children(2, 3, 2)
        # print(fm.get_children()[1].quarter_duration)
        fm.get_children()[1].simple_format = breathe.simple_format
        score = fm.get_score(show_fractal_orders=True, layer_number=fm.number_of_layers)
        score.max_division = 7
        score.finish()
        partwise = score.to_partwise()
        xml_path = path + '_test_6.xml'
        partwise.write(xml_path)
        self.assertCompareFiles(xml_path)
示例#2
0
class Test(TestCase):
    def setUp(self) -> None:
        self.fm = FractalMusic(proportions=(1, 2, 3, 4, 5),
                               tree_permutation_order=(3, 5, 1, 2, 4))
        self.fm.duration = 10
        self.fm.midi_generator.midi_range = [60, 72]

    def test_1(self):
        fm = self.fm
        fm.add_layer()
        fm.tempo = 60
        for ch in fm.get_children():
            ch.chord.add_words(ch.fractal_order)

        score = TreeScoreTimewise()
        fm.get_simple_format().to_stream_voice().add_to_score(score)

        fm.merge_children(2, 1, 2)
        # print(fm.get_leaves(key=lambda leaf: leaf.index))
        # print(fm.get_leaves(key=lambda leaf: leaf.fractal_order))
        # print(fm.get_leaves(key=lambda leaf: round(float(leaf.value), 2)))
        # print(fm.get_leaves(key=lambda leaf: round(float(leaf.duration), 2)))
        # print(fm.get_leaves(key=lambda leaf: round(float(leaf.chord.quarter_duration), 2)))
        fm.get_simple_format().to_stream_voice().add_to_score(score,
                                                              part_number=2)
        xml_path = path + '_test_1.xml'
        score.write(xml_path)
        self.assertCompareFiles(xml_path)
        # # ft.add_layer()
        # # print(ft.get_leaves(key=lambda leaf: leaf.index))
        # # print(ft.get_leaves(key=lambda leaf: leaf.fractal_order))
        # # print(ft.get_leaves(key=lambda leaf: round(float(leaf.value), 2)))
        # ft.merge_children(1, 2, 2)
        # # print(ft.get_leaves(key=lambda leaf: leaf.index))
        # self.assertEqual(ft.get_leaves(key=lambda leaf: leaf.fractal_order), [3, 5, 2])
        # self.assertEqual(ft.get_leaves(key=lambda leaf: round(float(leaf.value), 2)), [2.0, 4.0, 4.0])

    def test_2(self):
        self.fm.duration = 20
        self.fm.add_layer()
        self.fm.get_children()[1].tempo = 72
        self.fm.set_none_tempi(60)
        with self.assertRaises(MergeTempoException):
            self.fm.merge_children(2, 1, 2)