예제 #1
0
 def test_4(self):
     fm = FractalMusic(duration=30)
     fm.add_layer()
     fm.get_children()[1].tempo = 72
     fm.set_none_tempi(80)
     fm.quantize_leaves(0.5)
     fm.add_layer()
     score = fm.get_root_score(layer_number=1, show_fractal_orders=True)
     xml_path = path + '_test_4.xml'
     score.write(path=xml_path)
     with self.assertRaises(SetTempoFirstException):
         fm.get_score()
예제 #2
0
class Test(TestCase):
    def setUp(self) -> None:
        self.fm = FractalMusic(proportions=[1, 2, 3],
                               tree_permutation_order=[3, 1, 2],
                               tempo=60,
                               quarter_duration=20)

    def test_1(self):
        self.fm.midi_generator.midi_range = [60, 72]
        self.fm.add_layer()
        self.fm.get_children()[0].midi_value = 80
        self.fm.add_layer()
        for node in self.fm.traverse():
            if node.fractal_order is not None:
                node.chord.add_lyric(node.fractal_order)
        score = self.fm.get_score()
        score.max_division = 7

        text_path = path + '_test_1.txt'
        self.fm.write_infos(text_path)
        self.assertCompareFiles(actual_file_path=text_path)

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

    def test_2(self):
        self.fm.midi_generator.midi_range = [60, 72]
        self.fm.midi_generator.set_directions(1, -1, 1)
        self.fm.add_layer()
        self.fm.get_children()[0].midi_value = 80

        split_nodes = self.fm.get_children()[0].split(1, 1)
        split_nodes[1].midi_value = 0

        self.fm.add_layer()

        for node in self.fm.traverse():
            if node.fractal_order is not None:
                node.chord.add_lyric(node.fractal_order)
        score = self.fm.get_score()
        score.max_division = 7

        text_path = path + '_test_2.txt'
        self.fm.write_infos(text_path)
        self.assertCompareFiles(actual_file_path=text_path)

        xml_path = path + '_test_2.xml'
        score.write(path=xml_path)
        self.assertCompareFiles(actual_file_path=xml_path)
예제 #3
0
    def test_10(self):
        fm = FractalMusic(proportions=[1, 2, 3],
                          tree_permutation_order=[3, 1, 2])
        fm.tempo = 60
        fm.duration = 10
        fm.midi_generator.midi_range = (60, 72)
        fm.permute_directions = True
        fm.midi_generator.set_directions(-1, 1, -1)

        fm.add_layer()

        for node in fm.traverse():
            node.chord.add_lyric(node.midi_generator.directions)
            node.chord.add_words(node.children_generated_midis, relative_y=30)
            node.chord.add_words(node.midi_generator.midi_range, relative_y=60)

        fm.add_layer()

        score = TreeScoreTimewise()
        score.accidental_mode = 'modern'
        score = fm.get_score(score)
        score.page_style.staff_distance = 150
        xml_path = path + '_test_10.xml'
        score.write(xml_path)
        self.assertCompareFiles(xml_path)
예제 #4
0
    def test_9(self):
        fm = FractalMusic(proportions=[1, 2, 3, 4, 5, 6, 7],
                          tree_permutation_order=[3, 6, 2, 5, 1, 7, 4])

        fm.multi = (7, 4)
        fm.tempo = 60
        fm.quarter_duration = 70

        fm.midi_generator.microtone = 4
        # fm.quantize_leaves(0.5)
        fm.midi_generator.directions = [1, -1, 1, -1, 1, -1, 1]

        fm.midi_generator.midi_range = [36, 56]
        fm.add_layer()

        fm.reduce_children(lambda child: child.fractal_order > 4)
        fm.quantize_leaves(0.5)
        fm.get_children()[3].add_layer()
        fm.get_children()[3].reduce_children(
            lambda child: child.fractal_order > 4)
        for node in fm.traverse():
            node.chord.add_words(node.midi_value)
        text_path = path + '_test_9.txt'
        fm.write_infos(text_path)
        self.assertCompareFiles(actual_file_path=text_path)

        score = fm.get_score()
        xml_path = path + '_test_9.xml'
        score.write(xml_path)
        self.assertCompareFiles(actual_file_path=xml_path)
예제 #5
0
 def test_1(self):
     fm = FractalMusic(tempo=60, quarter_duration=10)
     fm.midi_generator.midi_range = [60, 72]
     fm.add_layer()
     fm.add_info('fractal_order')
     score = fm.get_score(layer_number=fm.number_of_layers)
     xml_path = path + '_test_1.xml'
     score.write(path=xml_path)
     self.assertCompareFiles(xml_path)
예제 #6
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)
예제 #7
0
class Test(TestCase):
    def setUp(self) -> None:
        self.fm = FractalMusic(tempo=60, quarter_duration=4)

    def test_1(self):
        self.fm.split(1, 2)
        score = TreeScoreTimewise()
        self.fm.get_score(score)
        xml_path = path + '_test_1.xml'
        score.write(xml_path)
        self.assertCompareFiles(xml_path)

    def test_2(self):
        self.fm.split(1, 2)[1].chord.to_rest()
        score = TreeScoreTimewise()
        self.fm.get_score(score)
        xml_path = path + '_test_2.xml'
        score.write(xml_path)
        self.assertCompareFiles(xml_path)
예제 #8
0
 def test_3(self):
     fm = FractalMusic(tempo=60, quarter_duration=10)
     fm.midi_generator.midi_range = [60, 72]
     fm.add_layer()
     fm.add_layer()
     fm.add_info('midi_value')
     score = fm.get_score()
     xml_path = path + '_test_3.xml'
     score.write(path=xml_path)
     self.assertCompareFiles(xml_path)
예제 #9
0
 def test_4(self):
     fm = FractalMusic(tempo=60, quarter_duration=10)
     fm.midi_generator.midi_range = [60, 72]
     fm.add_layer()
     fm.add_layer()
     fm.add_info((lambda node: int(node.midi_value) if int(node.midi_value) == node.midi_value else node.midi_values,
                  'above'))
     score = fm.get_score()
     xml_path = path + '_test_4.xml'
     score.write(path=xml_path)
     self.assertCompareFiles(xml_path)
예제 #10
0
 def test_1(self):
     fm = FractalMusic(tempo=60, quarter_duration=10)
     fm.midi_generator.midi_range = [60, 72]
     fm.add_layer()
     for child in fm.get_children():
         child.add_gliss(grid=0.5, show_heads=True)
         child.chord_field.chords[0].add_articulation('accent')
     score = fm.get_score(show_fractal_orders=True,
                          layer_number=fm.number_of_layers)
     xml_path = path + '_test_1.xml'
     score.write(path=xml_path)
     self.assertCompareFiles(xml_path)
예제 #11
0
 def test_1(self):
     fm = FractalMusic(tempo=60,
                       quarter_duration=10,
                       proportions=(1, 2, 3),
                       tree_permutation_order=(3, 1, 2))
     fm.add_layer()
     midis = [B(4), A(4, 'b'), C(4, '#'), A(3), F(4)]
     fm.get_children()[0].add_layer()
     for leaf, midi in zip(fm.traverse_leaves(), midis):
         leaf.set_chord(TreeChord(midi))
     score = fm.get_score(show_fractal_orders=True)
     xml_path = path + '_test_.xml'
     score.write(path=xml_path)
예제 #12
0
 def test_2(self):
     fm = FractalMusic(tempo=60, quarter_duration=10, multi=(1, 2))
     fm.midi_generator.midi_range = [60, 72]
     fm.add_layer()
     for child in fm.get_children():
         child.chord.add_articulation('accent')
         child.chord.add_dynamics('f')
     for child in fm.get_children():
         child.add_gliss()
     score = fm.get_score(show_fractal_orders=True,
                          layer_number=fm.number_of_layers)
     xml_path = path + '_test_2.xml'
     score.write(path=xml_path)
     self.assertCompareFiles(xml_path)
예제 #13
0
    def test_4(self):
        # self.fm.multi = (self.fm.multi[0], self.fm.multi[1] + 1)
        def get_chord_stamp():
            node = fm.__copy__()
            node.midi_generator.midi_range = fm.midi_generator.midi_range
            node.midi_generator.microtone = 4
            node.add_layer()
            chord_midis = [child.midi_value for child in node.get_children()]
            output = [chord_midi - chord_midis[node.size // 2] for chord_midi in chord_midis]
            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=80)
        fm.midi_generator.midi_range = (60, 72)
        fm.midi_generator.microtone = 4
        fm.add_layer()
        fm.quantize_children(grid_size=1)
        proportions = (1, 10, 1, 7, 1)
        breakpoints = (1, Fraction(1, 7), 1)
        selected_nodes = fm.get_children()[1:3]

        breath_quarter_duration = sum([node.quarter_duration for node in selected_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(selected_nodes)):
            node = selected_nodes[i]
            start_chord = [stamp + node.midi_value for stamp in get_chord_stamp()]
            next_node = node.next_sibling
            if next_node:
                end_chord = [stamp + node.midi_value for stamp in get_chord_stamp()]
            else:
                end_chord = start_chord

            chord_field = ChordField(
                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)

            node.chord_field = chord_field

        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_4.xml'
        partwise.write(xml_path)
        self.assertCompareFiles(xml_path)
예제 #14
0
    def test_3(self):
        fm = FractalMusic(tempo=60, quarter_duration=10, multi=(1, 3))
        fm.midi_generator.midi_range = [60, 72]
        fm.add_layer()
        for child in fm.get_children():
            for midi in child.chord.midis:
                midi.notehead = Notehead('diamond', filled='no')
        for child in fm.get_children():
            child.add_gliss()

        fm.get_children()[-1].chord_field.chords[-1].get_post_grace_chords(
        )[0].midis[0].notehead = Notehead('diamond', filled='no')
        score = fm.get_score(show_fractal_orders=True,
                             layer_number=fm.number_of_layers)
        xml_path = path + '_test_3.xml'
        score.write(path=xml_path)
        self.assertCompareFiles(xml_path)
예제 #15
0
 def test_1(self):
     fm = FractalMusic(quarter_duration=20, tempo=80)
     fm.midi_generator.midi_range = [60, 84]
     fm.add_layer()
     sorted_children = sorted(fm.get_children(), key=lambda child: child.fractal_order)
     chord_field = ChordField(
         quarter_duration=10,
         duration_generator=ValueGenerator(ArithmeticProgression(a1=0.2, an=2)),
         midi_generator=ValueGenerator(Interpolation(start=84, end=60,
                                                     key=lambda midi: round(midi * 2) / 2)),
         short_ending_mode='add_rest'
     )
     sorted_children[-1].chord_field = chord_field
     score = fm.get_score(show_fractal_orders=True)
     xml_path = path + '_test_1.xml'
     score.write(xml_path)
     self.assertCompareFiles(xml_path)
예제 #16
0
    def test_3(self):
        # node.chord_fields are part of a group
        cfg = ChordField(
            duration_generator=ValueGenerator(RandomInterpolation(start=[0.25, 0.25], end=[0.75, 1], seed=20)))
        cf_1 = ChordField(
            midi_generator=ValueGenerator(cycle([60])))
        cf_2 = ChordField(
            midi_generator=ValueGenerator(cycle([72])),
            long_ending_mode='cut')

        cfg.add_child(cf_1)
        cfg.add_child(cf_2)

        fm = FractalMusic(quarter_duration=20, tempo=80)
        fm.add_layer()
        fm.get_children()[0].chord_field = cf_1
        fm.get_children()[1].chord_field = cf_2
        score = fm.get_score(show_fractal_orders=True)
        xml_path = path + 'test_3.xml'
        score.write(xml_path)
        self.assertCompareFiles(xml_path)
예제 #17
0
    def test_2(self):
        def add_chord_field(child):
            child.chord_field = ChordField(duration_generator=ValueGenerator(ArithmeticProgression(a1=0.2, an=2)),
                                           midi_generator=ValueGenerator(
                                                Interpolation(start=child.midi_generator.midi_range[0],
                                                              end=child.midi_generator.midi_range[1],
                                                              duration=None,
                                                              key=lambda
                                                                  midi: round(midi * 2) / 2)),
                                           short_ending_mode='stretch')

        fm = FractalMusic(quarter_duration=20, tempo=80, proportions=[1, 2, 3, 4, 5],
                          tree_permutation_order=[3, 1, 5, 2, 4])
        fm.midi_generator.midi_range = [60, 84]
        fm.add_layer()
        sorted_children = sorted(fm.get_children(), key=lambda child: child.fractal_order)
        add_chord_field(sorted_children[-1])

        score = fm.get_score(show_fractal_orders=True)
        xml_path = path + '_test_2.xml'
        score.write(xml_path)
        self.assertCompareFiles(xml_path)
예제 #18
0
class Test(TestCase):
    def setUp(self) -> None:
        self.fm = FractalMusic(duration=10)

    def test_1(self):
        self.fm.tempo = 70
        self.fm.add_layer()
        self.fm.change_quarter_duration(round(self.fm.quarter_duration))

        xml_path = path + '_test_1.xml'
        score = self.fm.get_score(show_fractal_orders=True)
        score.write(xml_path)
        self.assertCompareFiles(xml_path)

    def test_2(self):
        self.fm.tempo = 70
        self.fm.add_layer()
        self.fm.add_layer()
        self.fm.quantize_children()
        xml_path = path + '_test_2.xml'
        score = self.fm.get_children_score(show_fractal_orders=True)
        score.write(xml_path)
        self.assertCompareFiles(xml_path)
예제 #19
0
    def test_5(self):
        # node.chord_fields are part of a breathe group
        fm = FractalMusic(quarter_duration=20, tempo=80)
        fm.add_layer()
        fm.quantize_children(grid_size=1)

        node_groups = slice_list(fm.get_children(), (2, 1))

        cf_1 = ChordField(
            midi_generator=ValueGenerator(cycle([60, 61, 64, 66])),
            long_ending_mode='self_extend',
            short_ending_mode='self_shrink'
        )
        cf_2 = ChordField(
            midi_generator=ValueGenerator(cycle([72, 73, 74, 73, 72])),
            long_ending_mode='self_extend',
            short_ending_mode='self_shrink'
        )

        breathe_unit = Fraction(1, 5)
        breathe_breakpoints = (5 * breathe_unit, breathe_unit, 5 * breathe_unit)
        breathe_proportions = [2, 4, 1, 7, 2]

        breathe = Breathe(proportions=breathe_proportions,
                          quarter_duration=sum([node.chord.quarter_duration for node in node_groups[0]]),
                          breakpoints=breathe_breakpoints)

        cfg = ChordField(duration_generator=breathe.duration_generator)
        cfg.add_child(cf_1)
        cfg.add_child(cf_2)

        fm.get_children()[0].chord_field = cf_1
        fm.get_children()[1].chord_field = cf_2
        score = fm.get_score(show_fractal_orders=True)
        xml_path = path + 'test_5.xml'
        score.write(xml_path)
        self.assertCompareFiles(xml_path)
예제 #20
0
class TestFMBreathing(TestCase):
    def setUp(self) -> None:
        self.score = TreeScoreTimewise()
        self.fm = FractalMusic(quarter_duration=24, tempo=40)

    def test_1(self):
        proportions = (1, 3, 1, 5, 1)
        breakpoints = (1, Fraction(1, 7), 1)
        quarter_durations = [8, 12]
        breathe = Breathe(proportions=proportions, breakpoints=breakpoints, quarter_duration=sum(quarter_durations),
                          quantize=1)
        breathe.midi_generator = ValueGenerator(cycle([71]))
        test_chord_field = ChordField(duration_generator=breathe.duration_generator.__deepcopy__())
        for i in range(len(quarter_durations)):
            quarter_duration = quarter_durations[i]
            midi = 60 + i
            test_chord_field.add_child(
                ChordField(midi_generator=ValueGenerator(cycle([midi])), long_ending_mode='self_extend',
                           short_ending_mode='self_shrink', quarter_duration=quarter_duration))

        test_chord_field_2 = ChordField(duration_generator=breathe.duration_generator.__deepcopy__())
        for i in range(len(quarter_durations)):
            quarter_duration = quarter_durations[i]
            midi = 72 + i
            test_chord_field_2.add_child(
                ChordField(midi_generator=ValueGenerator(cycle([midi])), long_ending_mode='cut',
                           short_ending_mode='add_rest', quarter_duration=quarter_duration))
        breathe.simple_format.to_stream_voice().add_to_score(score=self.score, part_number=1)
        test_chord_field.simple_format.to_stream_voice().add_to_score(score=self.score, part_number=2)

        simple_format = SimpleFormat()
        for child in test_chord_field_2.children:
            simple_format.extend(child.simple_format)

        simple_format.to_stream_voice().add_to_score(score=self.score, part_number=3)
        xml_path = path + '_test_1.xml'
        self.score.write(xml_path)
        self.assertCompareFiles(xml_path)

    def test_2(self):
        self.fm.add_layer()
        self.fm.quantize_children(grid_size=1)
        proportions = (1, 3, 1, 5, 1)
        breakpoints = (1, Fraction(1, 7), 1)
        selected_nodes = self.fm.get_children()[:2]
        breath_quarter_duration = sum([node.quarter_duration for node in selected_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(selected_nodes)):
            midi = 60 + i
            chord_field = ChordField(midi_generator=ValueGenerator(cycle([midi])), long_ending_mode='self_extend',
                                     short_ending_mode='self_shrink')
            parent_chord_field.add_child(chord_field)
            node = selected_nodes[i]
            node.chord_field = chord_field

        score = self.fm.get_score(show_fractal_orders=True, layer_number=self.fm.number_of_layers)
        xml_path = path + '_test_2.xml'
        score.write(xml_path)
        self.assertCompareFiles(xml_path)

    def test_3(self):
        def get_children_midis(node):
            copy = node.__deepcopy__()
            copy.midi_generator.midi_range = node.midi_generator.midi_range
            copy.add_layer()

            output = [child.chord.midis[0].value for child in copy.get_children()]
            return output

        self.fm.multi = (self.fm.multi[0], self.fm.multi[1] + 1)
        self.fm.midi_generator.midi_range = (60, 72)
        self.fm.add_layer()
        self.fm.quantize_children(grid_size=1)
        proportions = (1, 3, 1, 3, 1)
        breakpoints = (1, Fraction(1, 7), 1)
        selected_nodes = self.fm.get_children()[:2]
        breath_quarter_duration = sum([node.quarter_duration for node in selected_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(selected_nodes)):
            node = selected_nodes[i]
            start_chord = get_children_midis(node)
            next_node = node.next_sibling
            if next_node:
                end_chord = get_children_midis(next_node)
            else:
                end_chord = start_chord

            chord_field = ChordField(
                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)

            node.chord_field = chord_field

        score = self.fm.get_score(show_fractal_orders=True, layer_number=self.fm.number_of_layers)
        score.max_division = 7
        xml_path = path + '_test_3.xml'
        score.write(xml_path)
        self.assertCompareFiles(xml_path)

    def test_4(self):
        # self.fm.multi = (self.fm.multi[0], self.fm.multi[1] + 1)
        def get_chord_stamp():
            node = fm.__copy__()
            node.midi_generator.midi_range = fm.midi_generator.midi_range
            node.midi_generator.microtone = 4
            node.add_layer()
            chord_midis = [child.midi_value for child in node.get_children()]
            output = [chord_midi - chord_midis[node.size // 2] for chord_midi in chord_midis]
            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=80)
        fm.midi_generator.midi_range = (60, 72)
        fm.midi_generator.microtone = 4
        fm.add_layer()
        fm.quantize_children(grid_size=1)
        proportions = (1, 10, 1, 7, 1)
        breakpoints = (1, Fraction(1, 7), 1)
        selected_nodes = fm.get_children()[1:3]

        breath_quarter_duration = sum([node.quarter_duration for node in selected_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(selected_nodes)):
            node = selected_nodes[i]
            start_chord = [stamp + node.midi_value for stamp in get_chord_stamp()]
            next_node = node.next_sibling
            if next_node:
                end_chord = [stamp + node.midi_value for stamp in get_chord_stamp()]
            else:
                end_chord = start_chord

            chord_field = ChordField(
                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)

            node.chord_field = chord_field

        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_4.xml'
        partwise.write(xml_path)
        self.assertCompareFiles(xml_path)

    def test_5(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.multi = (self.fm.multi[0], self.fm.multi[1] + 1)
        fm.midi_generator.midi_range = (62, 62 + 11)
        fm.midi_generator.microtone = 4
        fm.add_layer()
        fm.quantize_children(grid_size=1)
        selected_nodes = fm.get_children()[2:5]

        proportions = (1, 10, 1, 7, 1)
        breakpoints = (1, Fraction(1, 7), 1)
        breath_quarter_duration = sum([node.quarter_duration for node in selected_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(selected_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(
                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)

            node.chord_field = chord_field

        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_5.xml'
        partwise.write(xml_path)
        self.assertCompareFiles(xml_path)

    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)
예제 #21
0
 def test_3(self):
     fm = FractalMusic(duration=30)
     fm.add_layer()
     with self.assertRaises(SetTempoFirstException):
         fm.get_score()
예제 #22
0
import os

from musurgia.fractaltree.fractalmusic import FractalMusic

path = str(os.path.abspath(__file__).split('.')[0])

fm = FractalMusic(tempo=72, quarter_duration=18, tree_permutation_order=(2, 6, 4, 1, 3, 7, 5),
                  proportions=(1, 2, 3, 4, 5, 6, 7), multi=(1, 3), reading_direction='vertical')
fm.midi_generator.set_directions(-1, -1, -1, -1, -1, -1, -1)
fm.midi_generator.midi_range = [60, 72]
fm.generate_children(number_of_children=3)
for child in fm.get_children():
    child.add_gliss()

xml_path = path + '.xml'

score = fm.get_score(layer_number=fm.number_of_layers)
score.max_division = 5
score.write(xml_path)
예제 #23
0
class Test(TestCase):
    def setUp(self) -> None:
        self.fm = FractalMusic(proportions=[1, 2, 3, 4],
                               tree_permutation_order=[3, 1, 4, 2],
                               quarter_duration=20,
                               tempo=70)
        self.fm.midi_generator.midi_range = [36, 60]

    def test_get_multi_chord_midis_bad_number_of_midis(self):
        self.fm.add_layer()
        node = self.fm.get_children()[0]
        with self.assertRaises(ValueError):
            node.get_multi_chord_midis_with_range_factor(number_of_midis=0)

        with self.assertRaises(ValueError):
            node.get_multi_chord_midis_with_range_interval(range_interval=10,
                                                           number_of_midis=0)

    def test_get_multi_chord_midis_number_of_midis_None(self):
        self.fm.add_layer()
        node = self.fm.get_children()[0]
        actual = len(node.get_multi_chord_midis_with_range_factor())
        expected = len(node.children_generated_midis)
        self.assertEqual(actual, expected)
        actual = len(node.get_multi_chord_midis_with_range_interval(10))
        self.assertEqual(expected, actual)

    def test_get_multi_chord_midis_range_interval(self):
        self.fm.add_layer()
        node = self.fm.get_children()[0]
        multi_chord_midis = node.get_multi_chord_midis_with_range_interval(10)
        actual = abs(multi_chord_midis[-1] - multi_chord_midis[0])
        expected = 10
        self.assertEqual(expected, actual)

    def test_get_multi_chord_midis_range_factor(self):
        self.fm.add_layer()
        node = self.fm.get_children()[0]
        multi_chord_midis = node.get_multi_chord_midis_with_range_factor(3)
        actual = abs(multi_chord_midis[-1] - multi_chord_midis[0])
        original_midi_range = node.midi_generator.midi_range

        expected = abs(original_midi_range[1] - original_midi_range[0]) * 3
        self.assertEqual(expected, actual)

    def test_get_multi_chord_midis_with_range_interval_microtone(self):
        self.fm.add_layer()
        node = self.fm.get_children()[0]
        actual = node.get_multi_chord_midis_with_range_interval(10,
                                                                microtone=4)
        expected = [44.0, 45.5, 50.5, 50.5, 54.0]
        self.assertEqual(expected, actual)

    def test_get_multi_chord_midis_with_range_factor_microtone(self):
        self.fm.add_layer()
        node = self.fm.get_children()[0]
        actual = node.get_multi_chord_midis_with_range_factor(
            node.calculate_range_factor(10), microtone=4)
        expected = [44.0, 45.5, 50.5, 50.5, 54.0]
        self.assertEqual(expected, actual)

    def test_get_multi_chord_with_range_factor_microtone(self):
        self.fm.add_layer()
        score = self.fm.get_score(layer_number=1)
        for leaf in self.fm.traverse_leaves():
            multi_chord_midis = leaf.get_multi_chord_midis_with_range_factor(
                range_factor=1.5, microtone=4, no_duplicates=True)
            leaf.chord.midis = multi_chord_midis
        sf = self.fm.get_simple_format()
        sf.auto_clef()
        sf.to_stream_voice().add_to_score(score, 2)

        xml_path = path + '_get_multi_chord_with_range_factor.xml'
        score.write(path=xml_path)

        self.assertCompareFiles(actual_file_path=xml_path)

    def test_get_multi_chord_with_range_factor_original_direction(self):
        self.fm.midi_generator.microtone = 4
        self.fm.add_layer()
        score = self.fm.get_score(layer_number=1)
        for leaf in self.fm.traverse_leaves():
            multi_chord_midis = leaf.get_multi_chord_midis_with_range_factor(
                range_factor=1,
                microtone=4,
                no_duplicates=True,
                original_direction=True)
            leaf.chord.midis = multi_chord_midis
        sf = self.fm.get_simple_format()
        sf.auto_clef()
        sf.to_stream_voice().add_to_score(score, 2)

        self.fm.add_layer()
        sf = self.fm.get_simple_format()
        sf.auto_clef()
        sf.to_stream_voice().add_to_score(score, 3)

        xml_path = path + '_get_multi_chord_with_range_factor_original_direction.xml'
        score.write(path=xml_path)

        self.assertCompareFiles(actual_file_path=xml_path)

    def test_get_multi_chord_with_range_interval(self):
        self.fm.add_layer()
        score = self.fm.get_score(layer_number=1)
        for leaf in self.fm.traverse_leaves():
            range_interval = 6 + ((leaf.fractal_order - 1) * 1.5)
            number_of_midis = leaf.fractal_order + 1
            midis = leaf.get_multi_chord_midis_with_range_interval(
                range_interval=range_interval,
                microtone=4,
                number_of_midis=number_of_midis)
            leaf.chord.add_words(number_of_midis)
            leaf.chord.midis = midis
        sf = self.fm.get_simple_format()
        sf.auto_clef()
        sf.to_stream_voice().add_to_score(score, 2)
        xml_path = path + '_get_multi_chord_with_range_interval.xml'
        score.write(path=xml_path)

        self.assertCompareFiles(actual_file_path=xml_path)

    def test_calculate_range_factor_wrong_number_of_chords_1(self):
        self.fm.add_layer()
        with self.assertRaises(ValueError):
            self.fm.get_children()[0].calculate_range_factor(
                midi_range_interval=0)

    def test_calculate_range_factor_one_node(self):
        self.fm.add_layer()
        actual = self.fm.get_children()[0].calculate_range_factor(
            midi_range_interval=10)
        expected = 0.8333333333333334
        self.assertEqual(expected, actual)

    def test_calculate_range_factor(self):
        fm = FractalMusic(proportions=(1, 2, 3, 4, 5, 6, 7),
                          tree_permutation_order=(2, 6, 4, 1, 3, 7, 5),
                          quarter_duration=30,
                          tempo=60)
        fm.midi_generator.midi_range = [60, 84]
        multi_chord_midi_range_interval = 11
        fm.add_layer()
        actual = [
            leaf.calculate_range_factor(multi_chord_midi_range_interval)
            for leaf in fm.traverse_leaves()
        ]
        expected = [
            -3.6666666666666665, -1.2222222222222223, -1.8333333333333333,
            11.0, 2.2, 1.1, 1.375
        ]
        self.assertEqual(expected, actual)