1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
|
# SPDX-FileCopyrightText: 2013 Ole Martin Bjorndalen <ombdalen@gmail.com>
#
# SPDX-License-Identifier: MIT
"""
MIDI Parser
There is no need to use this module directly. All you need is
available in the top level module.
"""
from collections import deque
from .messages import Message
from .tokenizer import Tokenizer
class Parser:
"""
MIDI byte stream parser
Parses a stream of MIDI bytes and produces messages.
Data can be put into the parser in the form of
integers, byte arrays or byte strings.
"""
def __init__(self, data=None):
# For historical reasons self.messages is public and must be a
# deque(). (It is referenced directly inside ports.)
self.messages = deque()
self._tok = Tokenizer()
if data:
self.feed(data)
def _decode(self):
for midi_bytes in self._tok:
self.messages.append(Message.from_bytes(midi_bytes))
def feed(self, data):
"""Feed MIDI data to the parser.
Accepts any object that produces a sequence of integers in
range 0..255, such as:
[0, 1, 2]
(0, 1, 2)
[for i in range(256)]
(for i in range(256)]
bytearray()
"""
self._tok.feed(data)
self._decode()
def feed_byte(self, byte):
"""Feed one MIDI byte into the parser.
The byte must be an integer in range 0..255.
"""
self._tok.feed_byte(byte)
self._decode()
def get_message(self):
"""Get the first parsed message.
Returns None if there is no message yet. If you don't want to
deal with None, you can use pending() to see how many messages
you can get before you get None, or just iterate over the
parser.
"""
for msg in self:
return msg
else:
return None
def pending(self):
"""Return the number of pending messages."""
return len(self.messages)
__len__ = pending
def __iter__(self):
"""Yield messages that have been parsed so far."""
while len(self.messages) > 0:
yield self.messages.popleft()
def parse_all(data):
"""Parse MIDI data and return a list of all messages found.
This is typically used to parse a little bit of data with a few
messages in it. It's best to use a Parser object for larger
amounts of data. Also, tt's often easier to use parse() if you
know there is only one message in the data.
"""
return list(Parser(data))
def parse(data):
""" Parse MIDI data and return the first message found.
Data after the first message is ignored. Use parse_all()
to parse more than one message.
"""
return Parser(data).get_message()
|