Ejemplo n.º 1
0
def test_close_inside_iteration():
    # This type of port can close when it runs out of messages.
    # (And example of this is socket ports.)
    #
    # Iteration should then stop after all messages in the
    # internal queue have been received.
    message = Message('note_on')

    class Port(BaseIOPort):
        def __init__(self, messages):
            BaseIOPort.__init__(self)
            # Simulate some messages that arrived
            # earlier.
            self._messages.extend(messages)
            self.closed = False

        def _receive(self, block=True):
            # Oops, the other end hung up.
            if self._messages:
                return self._messages.popleft()
            else:
                self.close()
                return None

    message = Message('note_on')
    with Port([message, message]) as port:
        assert len(list(port)) == 2
Ejemplo n.º 2
0
def test_encode_and_parse():
    """Encode a message and then parse it.

    Should return the same message.
    """
    note_on = Message('note_on')
    assert note_on == parse(note_on.bytes())
Ejemplo n.º 3
0
def test_track_repr():
    track = MidiTrack([
        Message('note_on', channel=1, note=2, time=3),
        Message('note_off', channel=1, note=2, time=3),
    ])
    track_eval = eval(repr(track))
    for m1, m2 in zip(track, track_eval):
        assert m1 == m2
Ejemplo n.º 4
0
def test_handle_any_whitespace(tmpdir):
    path = tmpdir.join("test.syx").strpath

    with open(path, 'wt') as outfile:
        outfile.write('F0 01 02 \t F7\n   F0 03 04 F7\n')
    assert read_syx_file(path) == [
        Message('sysex', data=[1, 2]),
        Message('sysex', data=[3, 4])
    ]
Ejemplo n.º 5
0
def test_write(tmpdir):
    # p = tmpdir.mkdir("sub").join("hello.txt")
    path = tmpdir.join("test.syx").strpath
    msg = Message('sysex', data=(1, 2, 3))

    write_syx_file(path, [msg])
    with open(path, 'rb') as infile:
        assert infile.read() == msg.bin()

    write_syx_file(path, [msg], plaintext=True)
    with open(path, 'rt') as infile:
        assert infile.read().strip() == msg.hex()
Ejemplo n.º 6
0
def test_write(tmpdir):
    # p = tmpdir.mkdir("sub").join("hello.txt")
    path = tmpdir.join("test.syx").strpath
    msg = Message('sysex', data=(1, 2, 3))

    write_syx_file(path, [msg])
    with open(path, 'rb') as infile:
        assert infile.read() == msg.bin()

    write_syx_file(path, [msg], plaintext=True)
    with open(path, 'rt') as infile:
        assert infile.read().strip() == msg.hex()
Ejemplo n.º 7
0
def test_encode_and_parse_all():
    """Encode and then parse all message types.

    This checks mostly for errors in the parser.
    """

    parser = Parser()
    for type_ in sorted(specs.SPEC_BY_TYPE.keys()):
        msg = Message(type_)
        parser.feed(msg.bytes())
        parser.get_message() == msg

    assert parser.get_message() is None
Ejemplo n.º 8
0
def test_midifile_repr():
    midifile = MidiFile(type=1, ticks_per_beat=123, tracks=[
        MidiTrack([
            Message('note_on', channel=1, note=2, time=3),
            Message('note_off', channel=1, note=2, time=3)]),
        MidiTrack([
            MetaMessage('sequence_number', number=5),
            Message('note_on', channel=2, note=6, time=9),
            Message('note_off', channel=2, note=6, time=9)]),
    ])
    midifile_eval = eval(repr(midifile))
    for track, track_eval in zip(midifile.tracks, midifile_eval.tracks):
        for m1, m2 in zip(track, track_eval):
            assert m1 == m2
Ejemplo n.º 9
0
    def play(self, notes, velocity=64, lapse=0, duration=0):
        """ Play an array of notes. Support sharp and bemol notation

            Parameters:
                notes (List):       ['C', 'A', ...]
                velocity (int):     Volume (see `mido` specification)
                lapse (int):        Sequential: Time between each note in array to create sequential sound
                duration (int):    Chords: Time at the end of sounding all notes in the array to create chord like sound

            Returns:
                Sound (Midi Output)

            Example:
                ```python
                p = PlayNotes()
                p.play(p.create_mode('lidio', 'A', 3), lapse=0.5)
                ```
        """
        for note in notes:
            midinote = self.mapping_bemol[
                note] if 'b' in note else self.mapping_sharp[note]
            print(note, midinote)
            self.port.send(
                Message('note_on', note=midinote, time=lapse, velocity=64))
            if lapse:
                time.sleep(lapse)

        time.sleep(duration)
Ejemplo n.º 10
0
def test_read(tmpdir):
    path = tmpdir.join("test.syx").strpath
    msg = Message('sysex', data=(1, 2, 3))

    with open(path, 'wb') as outfile:
        outfile.write(msg.bin())

    assert read_syx_file(path) == [msg]

    with open(path, 'wt') as outfile:
        outfile.write(msg.hex())
    assert read_syx_file(path) == [msg]

    with open(path, 'wt') as outfile:
        outfile.write('NOT HEX')

    with raises(ValueError):
        read_syx_file(path)
Ejemplo n.º 11
0
def test_read(tmpdir):
    path = tmpdir.join("test.syx").strpath
    msg = Message('sysex', data=(1, 2, 3))

    with open(path, 'wb') as outfile:
        outfile.write(msg.bin())

    assert read_syx_file(path) == [msg]

    with open(path, 'wt') as outfile:
        outfile.write(msg.hex())
    assert read_syx_file(path) == [msg]

    with open(path, 'wt') as outfile:
        outfile.write('NOT HEX')

    with raises(ValueError):
        read_syx_file(path)
Ejemplo n.º 12
0
    def new_callback_wrapper(self, msg_data, data):
        from mido.messages import Message
        # I copied this from the body of the old callback in the `mido` source,
        # rather than calling it, because I needed access to the `msg`
        # variable, and in the case where `self._callback` is defined, I may not
        # be able to rely on `msg` being at the end of `self._queue`.
        try:
            msg = Message.from_bytes(msg_data[0])
        except ValueError:
            # Ignore invalid message.
            return

        # (Actually storing the DELTA time here, because otherwise I'd just need
        # to conver to this anyway, and that's all I'd use `msg.time` for.)
        msg.time = msg_data[1]

        # (also copied from `mido` source)
        (self._callback or self._queue.put)(msg)
Ejemplo n.º 13
0
def test_freeze_and_thaw():
    """Test that messages are hashable."""
    assert not is_frozen(thaw_message(freeze_message(Message('note_on'))))
Ejemplo n.º 14
0
    def test_recv_non_blocking(self, port):
        message = Message('note_on')

        port.send(message)
        port.poll()
        assert port.poll() is None
Ejemplo n.º 15
0
    track.append(Message('note_on', note=value_code, velocity=80, time=32))
    track.append(Message('note_on', note=value_code, velocity=0, time=32))
"""

if __name__ == '__main__':
    out_path = "midi_files/Test1.mid"

    mid = MidiFile()
    track = MidiTrack()
    mid.tracks.append(track)

    # Tempo
    track.append(MetaMessage('set_tempo', tempo=800000, time=0))

    # Start A
    track.append(Message('note_on', note=23, velocity=80, time=0))
    # End A
    track.append(Message('note_on', note=23, velocity=0, time=560))

    # Start B
    track.append(Message('note_on', note=56, velocity=80, time=40))
    # End B
    track.append(Message('note_on', note=56, velocity=0, time=100))

    # Tempo
    track.append(MetaMessage('set_tempo', tempo=500000, time=0))

    # Start C
    track.append(Message('note_on', note=96, velocity=80, time=320))
    # End C
    track.append(Message('note_on', note=96, velocity=0, time=400))
Ejemplo n.º 16
0
def test_parse():
    """Parse a note_on msg and compare it to one created with Message()."""
    parsed = parse(b'\x90\x4c\x20')
    other = Message('note_on', channel=0, note=0x4c, velocity=0x20)
    assert parsed == other
Ejemplo n.º 17
0
    def test_send_message(self, port):
        message = Message('note_on')

        port.send(message)
        port.send(message)
Ejemplo n.º 18
0
 def message(self):
     return Message('note_on', channel=self.channel.value)
Ejemplo n.º 19
0
def test_decode_invalid_sysex_with_spaces():
    with raises(ValueError):
        Message.from_str('sysex data=(1, 2, 3)')
Ejemplo n.º 20
0
def test_decode_sysex():
    assert Message.from_str('sysex data=(1,2,3)').data == (1, 2, 3)
Ejemplo n.º 21
0
 def message(self, value):
     return Message('control_change', channel=self.channel, control=self.control.value, value=value)
Ejemplo n.º 22
0
def test_thawed_message_is_copy():
    frozen_msg = FrozenMessage('note_on')
    thawed_msg = Message('note_on')
    assert thaw_message(frozen_msg) == thawed_msg
Ejemplo n.º 23
0
def test_is_frozen():
    assert is_frozen(FrozenMessage('note_on'))
    assert not is_frozen(Message('note_on'))
Ejemplo n.º 24
0
def test_encode_sysex():
    assert str(Message('sysex', data=())) == 'sysex data=() time=0'
    # This should not have an extra comma.
    assert str(Message('sysex', data=(1,))) == 'sysex data=(1) time=0'
    assert str(Message('sysex', data=(1, 2, 3))) == 'sysex data=(1,2,3) time=0'
Ejemplo n.º 25
0
def test_single_message():
    assert read_file(HEADER_ONE_TRACK + """
    4d 54 72 6b  # MTrk
    00 00 00 04
    20 90 40 40  # note_on
    """).tracks[0] == [Message('note_on', note=64, velocity=64, time=32)]
Ejemplo n.º 26
0
def test_encode_long_int():
    # Make sure Python 2 doesn't stick an 'L' at the end of a long
    # integer.
    assert 'L' not in str(Message('clock', time=1231421984983298432948))