def test_track_basics(): Track() Track((1, 4)) Track(Signature(1, 4)) with pytest.raises(ValueError): Track((1, 6))
def test_track_eq(): assert Track(signature=(17, 16), content=[]) == Track(signature=(17, 16), content=[]) assert Track(signature=(15, 16), content=[]) != Track(signature=(17, 16), content=[]) for _ in range(10): track = random_track() assert track == track
def random_track(): track = Track(signature=(random.randint(0, 100), 16)) for _ in range(random.randint(100, 300)): note = Note(random.randint(-100, 100), (random.randint(0, 100), 16)) position = (random.randint(0, 10000), random.choice([1, 2, 4, 8, 16, 32, 64, 128, 256, 512])) track.add(note, position) return track
def random_track_without_overlaps(): track = Track(signature=(random.randint(0, 100), 16)) p = 0 for _ in range(random.randint(100, 300)): note = Note(random.randint(30, 90), (random.randint(0, 100), 16), velocity=1.0) p += 200 track.add(note, (p, 16)) return track
def test_midi_serialize(): empty_track = Track(signature=(17, 16)) assert empty_track == midi.loads(midi.dumps(empty_track)) one_note_track = Track(signature=(17, 16), content=[(Note(0, (3, 8), 1.0), (7, 16))]) assert one_note_track == midi.loads(midi.dumps(one_note_track)) for _ in range(10): track = random_track_without_overlaps() assert track == midi.loads(midi.dumps(track))
def test_track_add(): t = Track() assert list(t) == [] t.add(Note(0, (1, 4))) assert list(t) == [(Signature(0, 1), Note(0, (1, 4)))] t.add([Note(0, (1, 4)), Note(1, (1, 4)), Note(-1, (1, 4))]) assert list(t) == [ (Signature(0, 1), Note(0, (1, 4))), (Signature(1, 4), Note(-1, (1, 4))), (Signature(1, 4), Note(0, (1, 4))), (Signature(1, 4), Note(1, (1, 4))), ] t.add(Note(42, (8, 1)), (13, 1)) assert list(t) == [(Signature(0, 1), Note(0, (1, 4))), (Signature(1, 4), Note(-1, (1, 4))), (Signature(1, 4), Note(0, (1, 4))), (Signature(1, 4), Note(1, (1, 4))), (Signature(13, 1), Note(42, (8, 1)))] assert list(t) == [(Signature(0, 1), Note(0, (1, 4))), (Signature(1, 4), Note(-1, (1, 4))), (Signature(1, 4), Note(0, (1, 4))), (Signature(1, 4), Note(1, (1, 4))), (Signature(13, 1), Note(42, (8, 1)))] assert list(t) == [(Signature(0, 1), Note(0, (1, 4))), (Signature(1, 4), Note(-1, (1, 4))), (Signature(1, 4), Note(0, (1, 4))), (Signature(1, 4), Note(1, (1, 4))), (Signature(13, 1), Note(42, (8, 1)))]
def test_track_length(): assert Track().length == Signature(0, 1) t = Track() t.add(Note(0, (10, 4))) assert t.length == Signature(10, 4) t.add(Note(0, (30, 8)), (40, 2)) assert t.length == Signature(190, 8) t.add(Note(0, (1, 4))) assert t.length == Signature(192, 8)
def randomize_velocities(track: Track, minimum: float = 0.5, maximum: float = 1.0, mean: float = 0.75, variance: float = 0.01) -> Track: """ Randomizes velocities of the notes in the track. Returns copy of the provided track with randomized velocities. :param track: input track :param minimum: minimum velocity (default: 0.5) :param maximum: maximum velocity (default: 1.0) :param mean: mean velocity (default: 0.75) :param variance: velocity variance (default: 0.01) :return: new track with randomized velocities """ if not 0 <= minimum <= 1.0: raise ValueError('minimum velocity must be in range [0.0, 1.0]') if not 0 <= maximum <= 1.0: raise ValueError('maximum velocity must be in range [0.0, 1.0]') if not 0 <= mean <= 1.0: raise ValueError('mean velocity must be in range [0.0, 1.0]') if not variance >= 0: raise ValueError('variance must be positive') result = Track(signature=track.signature) std = math.sqrt(variance) for position, note in track: new_velocity = max(minimum, min(maximum, random.gauss(mean, std))) result.add(note.with_velocity(new_velocity), position) return result
def test_track_signature(): t = Track((3, 8)) assert t.signature.nominator == 3 assert t.signature.denominator == 8