Ejemplo n.º 1
0
 def _set_new_measure(self, measure):
     if measure is None:
         new_measure = TreeMeasure()
         measures = self.get_children_by_type(TreeMeasure)
         if measures:
             new_measure.time = measures[-1].time.__copy__()
         else:
             new_measure.time.show = True
     else:
         new_measure = measure
     return new_measure
Ejemplo n.º 2
0
    def test_split_chord(self):
        b = TreeBeat()
        p = TreePart('one')
        chord = TreeChord((60, 62), quarter_duration=1)
        m = TreeMeasure()
        m.add_child(p)
        p.add_chord(chord)
        b.add_chord(chord)
        split = chord.split([1, 0.5, 3])

        result = [Fraction(2, 9), Fraction(1, 9), Fraction(2, 3)]
        self.assertEqual([chord.quarter_duration for chord in split], result)
Ejemplo n.º 3
0
    def test_split_beats_2(self):
        m = TreeMeasure(time=(3, 4))
        p = TreePart(id='one')
        p.max_division = 8
        p.forbidden_divisions = []
        m.add_child(p)
        # p.set_beats()

        p.add_chord(TreeChord(60, quarter_duration=1.4))
        p.add_chord(TreeChord(60, quarter_duration=1.6))
        # p._add_chords_to_beats()
        # p._split_chords_beatwise()
        p.finish()
        result = [Fraction(1, 1), Fraction(2, 5), Fraction(3, 5), Fraction(1, 1)]
        self.assertEqual([chord.quarter_duration for chord in p.chords], result)
Ejemplo n.º 4
0
    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)
Ejemplo n.º 5
0
    def test_duration(self):
        m = TreeMeasure(time=(3, 4))
        self.assertEqual(m.quarter_duration, 3)

        p = TreePart(id='one')
        m.add_child(p)

        tree_part_voice = p.get_staff(1).get_voice(1)
        tree_part_voice.set_beats([
            TreeBeat(duration=2),
            TreeBeat(duration=0.5),
            TreeBeat(duration=0.5)
        ])
        result = [0, 2, 2.5]
        self.assertEqual([beat.offset for beat in tree_part_voice.beats],
                         result)
Ejemplo n.º 6
0
    def test_quantize(self):
        m = TreeMeasure(time=(4, 4))
        p = TreePart(id='one')
        p.max_division = 8
        p.forbidden_divisions = []
        m.add_child(p)
        p.add_chord(TreeChord(60, quarter_duration=1))
        p.add_chord(TreeChord(60, quarter_duration=1.2))
        p.add_chord(TreeChord(60, quarter_duration=0.3))
        p.add_chord(TreeChord(60, quarter_duration=0.2))
        p.add_chord(TreeChord(60, quarter_duration=1.3))
        p.finish()

        result = [Fraction(1, 1), Fraction(1, 1), Fraction(1, 6), Fraction(1, 3), Fraction(1, 6), Fraction(1, 3),
                  Fraction(1, 1)]
        self.assertEqual([chord.quarter_duration for chord in p.chords], result)
Ejemplo n.º 7
0
class Test(TestCase):
    def setUp(self) -> None:
        self.measure = TreeMeasure()

    def test_number(self):
        self.assertTrue(isinstance(self.measure.number, str))
        with self.assertRaises(TypeError):
            self.measure.number = 3

        copied = self.measure.__copy__()
        self.assertTrue(isinstance(copied.number, str))
        with self.assertRaises(TypeError):
            copied.number = 3

    def test_duration(self):
        m = TreeMeasure(time=(3, 4))
        self.assertEqual(m.quarter_duration, 3)

        p = TreePart(id='one')
        m.add_child(p)

        tree_part_voice = p.get_staff(1).get_voice(1)
        tree_part_voice.set_beats([
            TreeBeat(duration=2),
            TreeBeat(duration=0.5),
            TreeBeat(duration=0.5)
        ])
        result = [0, 2, 2.5]
        self.assertEqual([beat.offset for beat in tree_part_voice.beats],
                         result)
Ejemplo n.º 8
0
        def set_times(current_measure_number, duration):
            global current_time
            duration = float(duration)
            remaining_duration = duration

            while True:
                if current_measure_number in times.keys():
                    current_time = times[current_measure_number]

                measure = TreeMeasure(time=current_time)

                measure_duration = measure.quarter_duration
                if measure_duration > remaining_duration:
                    measure = make_measure(duration=remaining_duration)
                    self.add_measure(measure)
                    break
                else:
                    self.add_measure(measure)
                    current_measure_number += 1
                    remaining_duration -= measure.quarter_duration
                    if remaining_duration == 0:
                        break
            if barline_style:
                self.get_children_by_type(TreeMeasure)[-1].set_barline_style(
                    barline_style)
            return current_measure_number
Ejemplo n.º 9
0
 def test_1(self):
     self.score.add_measure(TreeMeasure(time=(1, 4)))
     sf = SimpleFormat(
         quarter_durations=[Fraction(4, 6),
                            Fraction(1, 6),
                            Fraction(1, 6)])
     v = sf.to_stream_voice(1)
     v.add_to_score(self.score)
     result_path = path + '_test_1'
     self.score.write(path=result_path)
     TestScore().assert_template(result_path=result_path)
Ejemplo n.º 10
0
 def test_beats(self):
     m = TreeMeasure(time=(3, 8, 2, 4))
     p = TreePart(id='one')
     m.add_child(p)
     tree_part_voice = p.get_staff(1).get_voice(1)
     tree_part_voice.set_beats()
     result = [0.5, 0.5, 0.5, 1.0, 1.0]
     self.assertEqual([beat.duration for beat in tree_part_voice.beats], result)
     result = [0, 0.5, 1.0, 1.5, 2.5]
     self.assertEqual([beat.offset for beat in tree_part_voice.beats], result)
     # result = [4, 4, 4, 8, 8]
     # self.assertEqual([beat.max_division for beat in tree_part_voice.beats], result)
     # tree_part_voice.beats[3].max_division = 5
     # result = [4, 4, 4, 5, 8]
     # self.assertEqual([beat.max_division for beat in tree_part_voice.beats], result)
     with self.assertRaises(ValueError):
         tree_part_voice.set_beats([TreeBeat(duration=0.5), TreeBeat(duration=0.5), TreeBeat(duration=0.5)])
     tree_part_voice.set_beats([TreeBeat(duration=1), TreeBeat(duration=0.5), TreeBeat(duration=2)])
     result = [0, 1, 1.5]
     self.assertEqual([beat.offset for beat in tree_part_voice.beats], result)
Ejemplo n.º 11
0
    def test_1(self):
        self.score.add_measure(TreeMeasure(time=(5, 8)))
        # sf = SimpleFormat(durations=[1.8, 0.2, 0.5])
        sf = SimpleFormat(quarter_durations=[2.5])
        v = sf.to_stream_voice(1)
        v.add_to_score(self.score)

        result_path = path + '_test_1'
        self.score.finish()
        self.score.write(path=result_path)
        TestScore().assert_template(result_path=result_path)
Ejemplo n.º 12
0
    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)
Ejemplo n.º 13
0
        def make_measure(duration=None):
            def get_time():
                if duration % 1 == 0:
                    time = (int(duration), 4)
                    return time
                elif (duration * 2) % 1 == 0:
                    time = (int(duration * 2), 8)
                    return time
                else:
                    raise ValueError(
                        'duration {} is not dividable'.format(duration))

            if not duration:
                time = (4, 4)
            else:
                time = get_time()

            return TreeMeasure(time)
Ejemplo n.º 14
0
    def test_split_quantize(self):
        s = TreeScoreTimewise()
        m = TreeMeasure(time=(3, 4))
        s.add_measure(m)
        s.add_part()
        # m.add_child(p)

        chord1 = s.add_chord(1, 1, TreeChord((71, 72), quarter_duration=1.3))
        l1 = Lyric()
        l1.add_child(Text('bla'))
        chord1.add_child(l1)
        s.add_chord(1, 1, TreeChord((60, 63, 65), quarter_duration=0.6))
        s.add_chord(1, 1, TreeChord(60, quarter_duration=1.1))
        s.finish()

        # print(s.to_string())
        s.write(path=path)

        result = '''<part id="one">
Ejemplo n.º 15
0
class TestCopyMeasure(TestCase):
    def setUp(self):
        self.measure = TreeMeasure()

    def test_copy_measure(self):
        new_measure = self.measure.__copy__()
        new_measure.time.get_children_by_type(Beats)[0].value = 8

        result = '''<time>
  <beats>8</beats>
  <beat-type>4</beat-type>
</time>
'''
        self.assertEqual(new_measure.time.to_string(), result)

        result = '''<time>
  <beats>4</beats>
  <beat-type>4</beat-type>
</time>
'''
        self.assertEqual(self.measure.time.to_string(), result)
Ejemplo n.º 16
0
    def test_2(self):
        self.score.add_measure(TreeMeasure(time=(5, 8)))
        # sf = SimpleFormat(durations=[1.8, 0.2, 0.5])
        sf = SimpleFormat(quarter_durations=[
            Fraction(1, 7),
            Fraction(6, 7),
            Fraction(1, 14),
            Fraction(3, 14),
            Fraction(10, 14),
            Fraction(1, 2)
        ])
        v = sf.to_stream_voice(1)
        v.add_to_score(self.score, part_number=1, first_measure=1)
        v = sf.to_stream_voice(1)
        v.add_to_score(self.score, part_number=2, first_measure=1)
        self.score.get_score_parts()[1].max_division = 7

        result_path = path + '_test_2'
        self.score.finish()
        self.score.write(path=result_path)
        TestScore().assert_template(result_path=result_path)
Ejemplo n.º 17
0
 def setUp(self) -> None:
     self.score = TreeScoreTimewise()
     self.score.add_measure(TreeMeasure(time=(1, 4)))
Ejemplo n.º 18
0
 def setUp(self):
     m = TreeMeasure(time=(4, 4))
     self.part = TreePart(id='one')
     self.part.max_division = 8
     self.part.forbidden_divisions = []
     m.add_child(self.part)
Ejemplo n.º 19
0
 def setUp(self) -> None:
     self.measure = TreeMeasure()
Ejemplo n.º 20
0
    def set_time_signatures(self,
                            quarter_durations=None,
                            times=None,
                            barline_style=None):
        global current_time
        if self.get_children_by_type(TreeMeasure):
            raise Exception(
                'for setting time signatures score should be empty')

        def make_measure(duration=None):
            def get_time():
                if duration % 1 == 0:
                    time = (int(duration), 4)
                    return time
                elif (duration * 2) % 1 == 0:
                    time = (int(duration * 2), 8)
                    return time
                else:
                    raise ValueError(
                        'duration {} is not dividable'.format(duration))

            if not duration:
                time = (4, 4)
            else:
                time = get_time()

            return TreeMeasure(time)

        if not times:
            times = {}

        current_time = (4, 4)

        def set_times(current_measure_number, duration):
            global current_time
            duration = float(duration)
            remaining_duration = duration

            while True:
                if current_measure_number in times.keys():
                    current_time = times[current_measure_number]

                measure = TreeMeasure(time=current_time)

                measure_duration = measure.quarter_duration
                if measure_duration > remaining_duration:
                    measure = make_measure(duration=remaining_duration)
                    self.add_measure(measure)
                    break
                else:
                    self.add_measure(measure)
                    current_measure_number += 1
                    remaining_duration -= measure.quarter_duration
                    if remaining_duration == 0:
                        break
            if barline_style:
                self.get_children_by_type(TreeMeasure)[-1].set_barline_style(
                    barline_style)
            return current_measure_number

        if quarter_durations:
            if not hasattr(quarter_durations, '__iter__'):
                quarter_durations = [quarter_durations]
            elif isinstance(quarter_durations, str):
                raise TypeError()
            else:
                quarter_durations = quarter_durations

            current_measure_number = 1
            for duration in quarter_durations:
                current_measure_number = set_times(current_measure_number,
                                                   duration)

            if list(times.keys()) != [] and current_measure_number < list(
                    times.keys())[-1]:
                current_time = (4, 4)
                for key in times.keys():
                    if key <= current_measure_number:
                        current_time = times[key]

                for measure_number in range(current_measure_number,
                                            list(times.keys())[-1] + 1):
                    try:
                        current_time = times[measure_number]
                    except KeyError:
                        pass
                    self.add_measure(TreeMeasure(time=current_time))

        else:
            try:
                number_of_measures = list(times.keys())[-1]
                current_time = (4, 4)
                for measure_number in range(1, number_of_measures + 1):
                    try:
                        current_time = times[measure_number]
                    except KeyError:
                        pass
                    self.add_measure(TreeMeasure(time=current_time))
            except IndexError:
                pass
Ejemplo n.º 21
0
 def setUp(self):
     self.measure = TreeMeasure()
Ejemplo n.º 22
0
 def make_measure(self, number, time_signature=(4, 4)):
     self.score.add_measure(TreeMeasure(time=time_signature))
     for i in range(time_signature[0] * 8 // time_signature[1]):
         self.score.add_chord(number, 1, TreeChord(60,
                                                   quarter_duration=0.5))