def test_union(self) -> None: r0 = binr.Compound([2, 2, 1]) # 0 2 4 (5) r1 = binr.Compound([2, 1, 2]) # 0 2 3 (5) r2 = binr.Compound([2, 1, 1, 1]) r3 = binr.Compound([1, 2, 2]) # 0 1 3 (5) r4 = binr.Compound([1, 1, 1, 1, 1]) self.assertEqual(r0.union(r1), r2) self.assertEqual(r0.union(r3), r4)
def test_modulate(self) -> None: r0 = binr.Compound([3, 2, 3, 2, 3, 2]) r1 = binr.Compound([2, 1, 1, 2]) r2 = binr.Compound([3, 1, 2]) expectation0 = binr.Compound([5, 3, 2, 5]) expectation1 = binr.Compound([8, 2, 5]) self.assertEqual(r0.modulate(r1), expectation0) self.assertEqual(r0.modulate(r2), expectation1)
def test_real_stretch(self) -> None: r0 = binr.Compound([2, 2, 1]) r1 = binr.Compound([4, 4, 2]) # showing difference between stretch and # real_stretch method: self.assertNotEqual(hash(r0.stretch(2)), hash(r1)) self.assertNotEqual(hash(r1.stretch(0.5)), hash(r0)) self.assertEqual(hash(r0.real_stretch(2)), hash(r1)) self.assertEqual(hash(r1.real_stretch(0.5)), hash(r0))
def test_set_item(self) -> None: basic = [2, 1, 1, 3, 2] c = binr.Compound(basic) new_value = 0.5 c[1] = new_value self.assertEqual(c.multiply, new_value) self.assertEqual(c[1], new_value)
def detect_metricity_per_tone_per_voice( voices: tuple, metricity_per_beat: tuple) -> tuple: metricity_per_tone_per_voice = [] for vox in voices: metricity_per_tone_per_voice.append( tuple(metricity_per_beat[int(position)] for position in binr.Compound(vox.delay).convert2absolute())) return tuple(metricity_per_tone_per_voice)
def test_move_int(self) -> None: basic = [2, 3] c = binr.Compound(basic) expected0 = [2, 2, 1] expected1 = [2, 1, 2] expected2 = [2, 1, 1, 1] expected3 = [3, 1, 1] expected4 = [3, 2] self.assertEqual(c, c.move_int(0)) self.assertEqual(expected0, c.move_int(1)) self.assertEqual(expected1, c.move_int(2)) self.assertEqual(expected2, c.move_int(3)) self.assertEqual(expected3, c.move_int(-1)) self.assertEqual(expected4, c.move_int(-2))
def test_move_gc(self) -> None: basic = [2, 3] c = binr.Compound(basic) expected0 = [2, 1, 2] expected1 = [1, 1, 1, 2] expected2 = [1, 2, 2] expected3 = [1, 1, 3] expected4 = [1, 4] self.assertEqual(c, c.move_gc(0)) self.assertEqual(expected0, c.move_gc(1)) self.assertEqual(expected1, c.move_gc(2)) self.assertEqual(expected2, c.move_gc(3)) self.assertEqual(expected3, c.move_gc(-1)) self.assertEqual(expected4, c.move_gc(-2))
def make_counterpoint_result(self) -> tuple: allowed_pitches_per_voice = self.find_allowed_pitches_per_voice() self._organism = organisms.Organism( self._action_per_voice, self._sound_per_voice, allowed_pitches_per_voice, self._rhythms, tools.scale(self._weight_per_beat, *self._weight_range), predefined_voices=self._predefined_voices, allow_unisono=self._allow_unisono, allow_melodic_octaves=self._allow_melodic_octaves, random_seed=self._random_seed, harmonic_weight=self._harmonic_weight, melodic_weight=self._melodic_weight, ) converted_voices0 = [] converted_voices1 = [] for vox in tuple(self._organism): converted = (tuple(vox.pitch), binr.Compound(vox.delay)) converted_voices0.append(converted + (tuple(True for i in vox.delay), )) converted_voices1.append(converted) return tuple(converted_voices0), tuple(converted_voices1)
def rhythm_maker(self) -> tuple: n_bars = self._n_bars # for first attack every voice should have a theoretical attack # (rests are explicitly controlled through the start_harmony argument) distribution_for_first_attack_per_bar = ((0, 1, 2), ) distribution_for_first_attack_per_bar += tuple( next(distribution_function_for_first_attack_per_bar) for i in range(n_bars - 1)) rhythm_per_voice = [] max_attacks_per_bar = min(self._metrical_numbers) for voice_idx, metrical_prime, density, curve in zip( range(3), self._metrical_numbers, density_per_voice, curve_per_voice): rhythm = [] # if density between 0 and 1 understand as percentage # for density 1 only understand it as percentage if type is # float if (density >= 0 and density < 1) or (density == 1 and type(density) is float): n_attacks_per_bar = int(density * max_attacks_per_bar) # else use it as an absolute number else: assert density <= max_attacks_per_bar n_attacks_per_bar = int(density) for (distribution_for_first_attack ) in distribution_for_first_attack_per_bar: has_first_attack = voice_idx in distribution_for_first_attack n_attacks = n_attacks_per_bar + has_first_attack if rhythmic_function == "euclid": current_rhythm = tools.euclid(metrical_prime, n_attacks) elif rhythmic_function == "barlow": if metrical_prime == 10: divided = (2, 5) else: divided = tuple( prime_factors.factorise(metrical_prime)) ranking = indispensability.bar_indispensability2indices( indispensability.indispensability_for_bar(divided)) choosen_attacks = sorted(ranking[:n_attacks]) current_rhythm = tuple(b - a for a, b in zip( choosen_attacks, choosen_attacks[1:] + [metrical_prime])) else: msg = "Unknown rhythmic function {}.".format( rhythmic_function) raise NotImplementedError(msg) if not has_first_attack: rhythm[-1] += current_rhythm[0] current_rhythm = current_rhythm[1:] rhythm.extend(current_rhythm) rhythm_per_voice.append(binr.Compound(rhythm)) return tuple(rhythm_per_voice)
def test_init_functions(self) -> None: basic = [8, 4, 7, 12, 8] factor = fractions.Fraction(1, 4) rhythm = [i * factor for i in basic] c0 = binr.Compound(rhythm) self.assertEqual(list(c0), rhythm) c1 = binr.Compound.from_binary(bin(276019282048), multiply=factor) self.assertEqual(list(c0), list(c1)) self.assertEqual(c0.multiply, c1.multiply) c2 = binr.Compound.from_int(276019282048, factor) self.assertEqual(list(c0), list(c2)) self.assertEqual(c0.multiply, c2.multiply) c3 = binr.Compound.from_binary_rhythm( ( 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, ), factor, ) self.assertEqual(list(c0), list(c3)) self.assertEqual(c0.multiply, c3.multiply) # equality method tester self.assertEqual(c0, c1) self.assertEqual(c0, c2) self.assertEqual(c0, c3) self.assertEqual(c0, list(c1)) c4 = binr.Compound((1, 1, 1, 1)) self.assertNotEqual(c0, c4) c5 = binr.Compound(tuple(c0.intr)) self.assertNotEqual(c0, c5)
def test_intersection(self) -> None: r0 = binr.Compound([2, 2, 1]) # 0 2 4 (5) r1 = binr.Compound([2, 1, 2]) # 0 2 3 (5) r2 = binr.Compound([2, 3]) self.assertEqual(r0.intersection(r1), r2)
def test_get_item(self) -> None: basic = [2, 1, 1, 3, 2] c = binr.Compound(basic) for idx, n in enumerate(basic): self.assertEqual(c[idx], n)