def test_3(self):
        # fields: both with midi_generators, one duration_generator
        # group: duration_generator with __next__ (Random)
        cfg = ChordField(
            duration_generator=ValueGenerator(Random(pool=[0.2, 0.4, 0.8, 1.6], seed=10))
        )

        cf_1 = ChordField(quarter_duration=3,
                          midi_generator=ValueGenerator(cycle([60, 61, 64, 66])),
                          duration_generator=ValueGenerator(cycle([1]))
                          )
        cf_2 = ChordField(quarter_duration=6,
                          midi_generator=ValueGenerator(cycle([72, 73, 74, 73, 72])),
                          long_ending_mode='cut'
                          )

        cfg.add_child(cf_1)
        cfg.add_child(cf_2)
        sf_1 = cf_1.simple_format
        sf_2 = cf_2.simple_format
        sf = SimpleFormat()
        sf.extend(sf_1)
        sf.extend(sf_2)
        xml_path = path + 'test_3.xml'
        # cfg.simple_format.to_stream_voice().add_to_score(self.score)
        sf.to_stream_voice().add_to_score(self.score)
        self.score.write(xml_path)
        self.assertCompareFiles(xml_path)
示例#2
0
class RandomInterpolation(Interpolation):
    def __init__(self, periodicity=None, seed=None, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self._random = Random(periodicity=periodicity, seed=seed)

    @Interpolation.start.setter
    def start(self, val):
        if not hasattr(val, '__iter__'):
            val = [val]

        self._start = val
        self._check_pool_length()

    @Interpolation.end.setter
    def end(self, val):
        if not hasattr(val, '__iter__'):
            val = [val]
        self._end = val
        self._check_pool_length()

    def _check_pool_length(self):
        if self.start and self.end:
            if len(self.start) != len(self.end):
                return ValueError('start and end must be of the same length.')

    def _get_pool(self, x):
        pools = self.start + self.end
        pool_size = len(self.start)
        first_index = int(round(Interpolation(start=0, end=pool_size, duration=self.duration)(x)))
        pool = pools[first_index:first_index + pool_size]

        return pool

    def __call__(self, x):
        self._check_x(x)
        self._random.pool = self._get_pool(x)
        output = self._random.__next__()
        if self.key:
            return self.key(output)
        return output

    def __deepcopy__(self, memodict={}):
        copied = self.__class__(start=self.start, end=self.end, duration=self.duration, key=self.key)
        copied._random = self._random.__deepcopy__()
        return copied
示例#3
0
    def __init__(self,
                 pool=None,
                 periodicity=None,
                 seed=None,
                 *args,
                 **kwargs):
        super().__init__(*args, **kwargs)
        self._my_random = Random()

        self.pool = pool
        self.periodicity = periodicity
        self.seed = seed
示例#4
0
 def test_13(self):
     field = ChordField(quarter_duration=10,
                        duration_generator=ValueGenerator(
                            Random(pool=[0.2, 0.4, 0.8, 1.2, 1.6, 2],
                                   periodicity=3,
                                   seed=20)),
                        midi_generator=ValueGenerator(cycle([71])),
                        long_ending_mode='self_extend')
     sf = field.simple_format
     self.score.set_time_signatures(
         quarter_durations=ceil(field.quarter_duration))
     sf.to_stream_voice().add_to_score(self.score)
     xml_path = path + '_test_13.xml'
     self.score.write(xml_path)
     self.assertCompareFiles(xml_path)
    def test_2(self):
        # fields: midi_generators
        # group: duration_generator with __next__
        cfg = ChordField(
            duration_generator=ValueGenerator(Random(pool=[0.2, 0.4, 0.8, 1.6], seed=10))
        )
        cf_1 = ChordField(quarter_duration=3,
                          midi_generator=ValueGenerator(cycle([60, 61, 64, 66])),
                          long_ending_mode='self_extend'
                          )
        cf_2 = ChordField(quarter_duration=6,
                          midi_generator=ValueGenerator(cycle([72, 73, 74, 73, 72])),
                          long_ending_mode='self_extend')

        # cfg = ChordFieldGroup(duration_generator=Random(pool=[0.2, 0.4, 0.8, 1.6], seed=10))
        cfg.add_child(cf_1)
        cfg.add_child(cf_2)
        simple_format = cfg.simple_format
        self.score.set_time_signatures(quarter_durations=ceil(cfg.quarter_duration))
        xml_path = path + 'test_2.xml'
        simple_format.to_stream_voice().add_to_score(self.score)
        self.score.write(xml_path)
        self.assertCompareFiles(xml_path)
示例#6
0
from musurgia.random import Random

ar_1 = Random(pool=[5, 2, 3, 1, 2, 1])
print(ar_1.pool)
ar_2 = Random(pool=[5.3, 2.2, 3, 1, 2, 1])
print(ar_2.pool)
ar_3 = Random(pool=['1', '2', '3', '4'])
print(ar_3.pool)
ar_4 = Random(pool=[4, 3, 5.3, 2.2, 3, 1, 2, 1])
print(ar_4.pool)
#
# pool = ['1', '2', '3', '4']
# print(sorted(list(set(pool))))

示例#7
0
 def random(self):
     if self._random is None:
         self._random = Random()
     return self._random
示例#8
0
 def __init__(self, periodicity=None, seed=None, *args, **kwargs):
     super().__init__(*args, **kwargs)
     self._random = Random(periodicity=periodicity, seed=seed)