def test_note_with_velocity(): n1 = Note('A4', (3, 8), velocity=0.43) n2 = n1.with_velocity(0.86) assert n1.tone == n2.tone assert n1.duration.nominator == n2.duration.nominator assert n1.duration.denominator == n2.duration.denominator assert n1.velocity == 0.43 assert n2.velocity == 0.86
def test_note_with_duration(): n1 = Note('A4', (3, 8)) n2 = n1.with_duration((7, 4)) assert n1.tone == n2.tone assert n1.duration == Signature(3, 8) assert n2.duration == Signature(7, 4) assert n1.velocity == n2.velocity
def test_note_transposed(): n1 = Note('A4') n2 = n1.transposed(+3) assert n1.tone == Tone.from_notation('A4') assert n2.tone == Tone.from_notation('C5') assert n1.duration.nominator == n2.duration.nominator assert n1.duration.denominator == n2.duration.denominator assert n1.velocity == n2.velocity
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 test_note_properties(): n = Note('B#6', (3, 8), 0.1415) assert n.tone == Tone.from_notation('B#6') assert n.duration.nominator == 3 assert n.duration.denominator == 8 assert n.velocity == 0.1415
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_note_comp(): assert Note('C#4') >= Note('C4') assert Note('C#4') > Note('C4') assert Note('C#4') <= Note('D4') assert Note('C#4') < Note('D4')
def test_note_eq(): assert Note('C4') == Note('C4') assert Note('C4') != Note('C5') assert Note('C4', (1, 2), 0.3) == Note('C4', (2, 4), 0.3) assert Note('C4', (1, 2), 0.3) != Note('C4', (2, 4), 0.4)
def test_note_basics(): assert Note(Tone(60)) == Note(60) assert Note(Tone.from_notation('A4')) == Note('A4') assert Note(Tone(60), Signature(3, 4)) == Note(60, (3, 4)) Note('A4', velocity=0.5) with pytest.raises(ValueError): Note('re') with pytest.raises(TypeError): Note('A4', '1/4') with pytest.raises(ValueError): Note('A4', velocity=-1.0) with pytest.raises(ValueError): Note('A4', velocity=1.1)
def test_midi_format_note_on(): midi_c4 = MIDIWriter(middle_c='C4', channel=3) with pytest.raises(ValueError): midi_c4._format_note_on(Note('C-2')) with pytest.raises(ValueError): midi_c4._format_note_on(Note('C10')) with pytest.raises(ValueError): midi_c4._format_note_on(Note('D11')) midi_c3 = MIDIWriter(middle_c='C3', channel=3) midi_c3._format_note_on(Note('C-2')) with pytest.raises(ValueError): midi_c3._format_note_on(Note('C-3')) assert midi_c3._format_note_on(Note('C3', velocity=1.0)) == b'\x93\x3c\x7f' assert midi_c3._format_note_on(Note('C4', velocity=0.0)) == b'\x93\x48\x00' assert midi_c4._format_note_on(Note('C4', velocity=0.0)) == b'\x93\x3c\x00' n = Note('C4') n._velocity = 2.0 assert midi_c4._format_note_on(n) == b'\x93\x3c\x7f' n._velocity = -10.0 assert midi_c4._format_note_on(n) == b'\x93\x3c\x00'