Example #1
0
def test_signature_normalized():
    s1 = Signature(6, 8)
    s2 = s1.normalized()

    assert s1.nominator == 6
    assert s1.denominator == 8
    assert s2.nominator == 3
    assert s2.denominator == 4
Example #2
0
def test_signature_to():
    s1 = Signature(1, 2)
    s2 = s1.to(8)

    assert s1.nominator == 1
    assert s1.denominator == 2
    assert s2.nominator == 4
    assert s2.denominator == 8
Example #3
0
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
Example #4
0
def test_midi_format_signature():
    midi = MIDIWriter()

    assert midi._format_signature(Signature(0, 1), 16,
                                  32) == b'\xff\x58\x04\x00\x00\x10\x20'
    assert midi._format_signature(Signature(3, 4), 16,
                                  32) == b'\xff\x58\x04\x03\x02\x10\x20'
    assert midi._format_signature(Signature(127, 170141183460469231731687303715884105728), 16, 32) == \
           b'\xff\x58\x04\x7f\x7f\x10\x20'

    assert midi._format_signature(Signature(255, 170141183460469231731687303715884105728), 16, 32) == \
           b'\xff\x58\x04\xff\x7f\x10\x20'

    with pytest.raises(OverflowError):
        midi._format_signature(Signature(512, 512), 16, 32)

    with pytest.raises(OverflowError):
        midi._format_signature(Signature(1, 4), -16, 32)

    with pytest.raises(OverflowError):
        midi._format_signature(Signature(1, 4), 16, -32)

    with pytest.raises(OverflowError):
        midi._format_signature(Signature(1, 4), 300, 32)

    with pytest.raises(OverflowError):
        midi._format_signature(Signature(1, 4), 16, 300)
Example #5
0
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)
Example #6
0
def test_track_basics():
    Track()
    Track((1, 4))
    Track(Signature(1, 4))

    with pytest.raises(ValueError):
        Track((1, 6))
Example #7
0
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)
Example #8
0
def test_midi_time_to_signature():
    time_to_signature = MIDIReader._time_to_signature

    with pytest.raises(AssertionError):
        time_to_signature(0, 0)

    with pytest.raises(AssertionError):
        time_to_signature(0, -10)

    assert time_to_signature(1, 1) == Signature(1, 4)
    assert time_to_signature(2, 1) == Signature(2, 4)
    assert time_to_signature(3, 1) == Signature(3, 4)
    assert time_to_signature(4, 1) == Signature(4, 4)
    assert time_to_signature(5, 1) == Signature(5, 4)
    assert time_to_signature(6, 1) == Signature(6, 4)
    assert time_to_signature(7, 1) == Signature(7, 4)
    assert time_to_signature(8, 1) == Signature(8, 4)

    assert time_to_signature(5, 10) == Signature(1, 8)
    assert time_to_signature(10, 10) == Signature(1, 4)
    assert time_to_signature(100, 10) == Signature(10, 4)
    assert time_to_signature(1000, 10) == Signature(100, 4)
    assert time_to_signature(500, 10) == Signature(50, 4)

    assert time_to_signature(3, 96) == Signature(1, 128)
Example #9
0
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)))]
Example #10
0
def test_signature_abs():
    assert abs(Signature(3, 8)) == Signature(3, 8)
Example #11
0
def test_signature_mul():
    assert Signature(1, 4) * Signature(2, 2) == Signature(2, 8)
    assert Signature(3, 4) * Signature(5, 4) == Signature(15, 16)
    assert Signature(1, 1) * Signature(2, 1) == Signature(2, 1)
Example #12
0
def test_signature_sum():
    assert Signature(0, 1) + Signature(0, 1) == Signature(0, 1)
    assert Signature(1, 2) + Signature(1, 2) == Signature(1, 1)
    assert Signature(1, 2) + Signature(1, 4) == Signature(3, 4)
Example #13
0
def test_signature_comp():
    assert Signature(2, 4) >= Signature(1, 2)
    assert Signature(2, 4) <= Signature(1, 2)
    assert Signature(2, 4) > Signature(1, 4)
    assert Signature(2, 4) >= Signature(1, 4)
    assert Signature(2, 4) < Signature(3, 4)
    assert Signature(2, 4) <= Signature(3, 4)
Example #14
0
def test_signature_eq():
    assert Signature(0, 1) == Signature(0, 4)
    assert Signature(1, 4) == Signature(8, 32)
    assert Signature(3, 2) == Signature(6, 4)
Example #15
0
def test_signature_basics():
    Signature(0, 1)
    Signature(1, 2)
    Signature(2, 4)
    Signature(3, 4)
    Signature(4, 4)
    Signature(1000, 4)

    with pytest.raises(ValueError):
        Signature(-1, 4)

    with pytest.raises(ValueError):
        Signature(0, 0)

    with pytest.raises(ValueError):
        Signature(0, -1)

    with pytest.raises(ValueError):
        Signature(1, 5)

    with pytest.raises(ValueError):
        Signature(1, 333)

    s = Signature(43, 4)

    assert s.nominator == 43
    assert s._denominator == 4
Example #16
0
def test_midi_signature_to_pulses():
    assert MIDIWriter(pulses_per_quarter=1)._signature_to_pulses(
        Signature(0, 1)) == 0
    assert MIDIWriter(pulses_per_quarter=1)._signature_to_pulses(
        Signature(1, 1)) == 4
    assert MIDIWriter(pulses_per_quarter=1)._signature_to_pulses(
        Signature(1, 2)) == 2
    assert MIDIWriter(pulses_per_quarter=1)._signature_to_pulses(
        Signature(1, 4)) == 1
    assert MIDIWriter(pulses_per_quarter=1)._signature_to_pulses(
        Signature(1, 8)) == 0
    assert MIDIWriter(pulses_per_quarter=1)._signature_to_pulses(
        Signature(1, 16)) == 0

    assert MIDIWriter(pulses_per_quarter=1)._signature_to_pulses(
        Signature(0, 16)) == 0
    assert MIDIWriter(pulses_per_quarter=1)._signature_to_pulses(
        Signature(1, 16)) == 0
    assert MIDIWriter(pulses_per_quarter=1)._signature_to_pulses(
        Signature(2, 16)) == 0
    assert MIDIWriter(pulses_per_quarter=1)._signature_to_pulses(
        Signature(3, 16)) == 0
    assert MIDIWriter(pulses_per_quarter=1)._signature_to_pulses(
        Signature(4, 16)) == 1
    assert MIDIWriter(pulses_per_quarter=1)._signature_to_pulses(
        Signature(5, 16)) == 1
    assert MIDIWriter(pulses_per_quarter=1)._signature_to_pulses(
        Signature(6, 16)) == 1
    assert MIDIWriter(pulses_per_quarter=1)._signature_to_pulses(
        Signature(7, 16)) == 1
    assert MIDIWriter(pulses_per_quarter=1)._signature_to_pulses(
        Signature(8, 16)) == 2
    assert MIDIWriter(pulses_per_quarter=1)._signature_to_pulses(
        Signature(9, 16)) == 2
    assert MIDIWriter(pulses_per_quarter=1)._signature_to_pulses(
        Signature(10, 16)) == 2
    assert MIDIWriter(pulses_per_quarter=1)._signature_to_pulses(
        Signature(11, 16)) == 2
    assert MIDIWriter(pulses_per_quarter=1)._signature_to_pulses(
        Signature(12, 16)) == 3
    assert MIDIWriter(pulses_per_quarter=1)._signature_to_pulses(
        Signature(13, 16)) == 3
    assert MIDIWriter(pulses_per_quarter=1)._signature_to_pulses(
        Signature(14, 16)) == 3
    assert MIDIWriter(pulses_per_quarter=1)._signature_to_pulses(
        Signature(15, 16)) == 3
    assert MIDIWriter(pulses_per_quarter=1)._signature_to_pulses(
        Signature(16, 16)) == 4

    assert MIDIWriter(pulses_per_quarter=32)._signature_to_pulses(
        Signature(1, 8)) == 16

    assert MIDIWriter(pulses_per_quarter=1000)._signature_to_pulses(
        Signature(1, 2)) == 2000