Skip to content

Commit 279345b

Browse files
author
Kevin J Walters
committed
Setting the package names correctly in the MIDI messages class files.
Adding some warnings on SystemExclusive needing to fit into the input buffer to parse. Changing the reST docs for default class methods in MIDIMessage as those are inherited by documentation too and were not appropropriate for all. Documenting the MIDI constructor. #3
1 parent 5b813a3 commit 279345b

13 files changed

Lines changed: 74 additions & 149 deletions

adafruit_midi/__init__.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,32 @@
5151

5252

5353
class MIDI:
54-
"""MIDI helper class."""
54+
"""MIDI helper class.
55+
56+
:param midi_in: an object which implements ``read(length)``,
57+
defaults to ``usb_midi.ports[0]``.
58+
:param midi_out: an object which implements ``write(buffer, length)``,
59+
defaults to ``usb_midi.ports[1]``.
60+
:param in_channel: The input channel(s).
61+
This is used by ``receive`` to filter data.
62+
This can either be an ``int`` for the wire protocol channel number (0-15)
63+
a tuple of ``int`` to listen for multiple channels or ``"ALL"``.
64+
Defaults to None.
65+
:param int out_channel: The wire protocol output channel number (0-15)
66+
used by ``send`` if no channel is specified,
67+
defaults to 0 (MIDI Channel 1).
68+
:param int in_buf_size: Size of input buffer in bytes, default 30.
69+
:param bool debug: Debug mode, default False.
70+
71+
"""
5572

5673
NOTE_ON = 0x90
5774
NOTE_OFF = 0x80
5875
PITCH_BEND = 0xE0
5976
CONTROL_CHANGE = 0xB0
6077

6178
def __init__(self, midi_in=usb_midi.ports[0], midi_out=usb_midi.ports[1], *,
62-
in_channel=None, out_channel=0, debug=False, in_buf_size=30):
79+
in_channel=None, out_channel=0, in_buf_size=30, debug=False):
6380
self._midi_in = midi_in
6481
self._midi_out = midi_out
6582
self.in_channel = in_channel
@@ -108,8 +125,10 @@ def out_channel(self, channel):
108125
def receive(self):
109126
"""Read messages from MIDI port, store them in internal read buffer, then parse that data
110127
and return the first MIDI message (event).
128+
This maintains the blocking characteristics of the midi_in port.
111129
112-
Returns (MIDIMessage object, channel) or (None, None) for nothing.
130+
:returns (MIDIMessage object, int channel): Returns object and channel
131+
or (None, None) for nothing.
113132
"""
114133
### could check _midi_in is an object OR correct object OR correct interface here?
115134
# If the buffer here is not full then read as much as we can fit from

adafruit_midi/channel_pressure.py

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,17 @@
2020
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2121
# THE SOFTWARE.
2222
"""
23-
`adafruit_midi`
23+
`adafruit_midi.channel_pressure`
2424
================================================================================
2525
26-
A CircuitPython helper for encoding/decoding MIDI packets over a MIDI or UART connection.
26+
Channel Pressure MIDI message.
2727
2828
2929
* Author(s): Kevin J. Walters
3030
3131
Implementation Notes
3232
--------------------
3333
34-
**Hardware:**
35-
36-
37-
38-
**Software and Dependencies:**
39-
40-
* Adafruit CircuitPython firmware for the supported boards:
41-
https://github.com/adafruit/circuitpython/releases
42-
4334
"""
4435

4536
from .midi_message import MIDIMessage

adafruit_midi/control_change.py

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,17 @@
2020
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2121
# THE SOFTWARE.
2222
"""
23-
`adafruit_midi`
23+
`adafruit_midi.control_change`
2424
================================================================================
2525
26-
A CircuitPython helper for encoding/decoding MIDI packets over a MIDI or UART connection.
26+
Control Change MIDI message.
2727
2828
2929
* Author(s): Kevin J. Walters
3030
3131
Implementation Notes
3232
--------------------
3333
34-
**Hardware:**
35-
36-
37-
38-
**Software and Dependencies:**
39-
40-
* Adafruit CircuitPython firmware for the supported boards:
41-
https://github.com/adafruit/circuitpython/releases
42-
4334
"""
4435

4536
from .midi_message import MIDIMessage

adafruit_midi/midi_message.py

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,23 @@
2020
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2121
# THE SOFTWARE.
2222
"""
23-
`adafruit_midi`
23+
`adafruit_midi.midi_message.MIDIMessage`
2424
================================================================================
2525
26-
A CircuitPython helper for encoding/decoding MIDI packets over a MIDI or UART connection.
26+
An abstract class for objects which represent MIDI messages (events).
27+
When individual messages are imported they register themselves with
28+
:func:register_message_type which makes them recognised
29+
by the parser, :func:from_message_bytes.
30+
31+
Large messages like :class:SystemExclusive can only be parsed if they fit
32+
within the input buffer in :class:MIDI.
2733
2834
2935
* Author(s): Kevin J. Walters
3036
3137
Implementation Notes
3238
--------------------
3339
34-
**Hardware:**
35-
36-
37-
38-
**Software and Dependencies:**
39-
40-
* Adafruit CircuitPython firmware for the supported boards:
41-
https://github.com/adafruit/circuitpython/releases
42-
4340
"""
4441

4542
__version__ = "0.0.0-auto.0"
@@ -97,16 +94,19 @@ def note_parser(note):
9794

9895
class MIDIMessage:
9996
"""
100-
A MIDI message:
101-
- _STATUS - extracted from Status byte with channel replaced by 0s
102-
(high bit always set).
103-
- _STATUSMASK - mask used to compared a status byte with _STATUS value
104-
- LENGTH - length for a fixed size message including status
105-
or -1 for variable length.
106-
- CHANNELMASK - mask use to apply a (wire protocol) channel number.
107-
- ENDSTATUS - the EOM status byte, only set for variable length.
108-
109-
This is an abstract class.
97+
The parent class for MIDI messages.
98+
99+
Class variables:
100+
101+
* ``_STATUS`` - extracted from status byte with channel replaced by 0s
102+
(high bit is always set by convention).
103+
* ``_STATUSMASK`` - mask used to compared a status byte with ``_STATUS`` value.
104+
* ``LENGTH`` - length for a fixed size message *including* status
105+
or -1 for variable length.
106+
* ``CHANNELMASK`` - mask used to apply a (wire protocol) channel number.
107+
* ``ENDSTATUS`` - the end of message status byte, only set for variable length.
108+
109+
This is an *abstract* class.
110110
"""
111111
_STATUS = None
112112
_STATUSMASK = None
@@ -277,18 +277,21 @@ def from_message_bytes(cls, midibytes, channel_in):
277277
return (msg, msgendidxplusone, skipped, channel)
278278

279279
# channel value present to keep interface uniform but unused
280+
# A default method for constructing wire messages with no data.
281+
# Returns a (mutable) bytearray with just the status code in.
280282
# pylint: disable=unused-argument
281283
def as_bytes(self, channel=None):
282-
"""A default method for constructing wire messages with no data.
283-
Returns a (mutable) bytearray with just status code in."""
284+
"""Return the ``bytearray`` wire protocol representation of the object."""
284285
return bytearray([self._STATUS])
285286

286287
# databytes value present to keep interface uniform but unused
288+
# A default method for constructing message objects with no data.
289+
# Returns the new object.
287290
# pylint: disable=unused-argument
288291
@classmethod
289292
def from_bytes(cls, databytes):
290-
"""A default method for constructing message objects with no data.
291-
Returns the new object."""
293+
"""Creates an object from the byte stream of the wire protocol
294+
(not including the first status byte)."""
292295
return cls()
293296

294297

adafruit_midi/note_off.py

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,17 @@
2020
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2121
# THE SOFTWARE.
2222
"""
23-
`adafruit_midi`
23+
`adafruit_midi.note_off`
2424
================================================================================
2525
26-
A CircuitPython helper for encoding/decoding MIDI packets over a MIDI or UART connection.
26+
Note Off Change MIDI message.
2727
2828
2929
* Author(s): Kevin J. Walters
3030
3131
Implementation Notes
3232
--------------------
3333
34-
**Hardware:**
35-
36-
37-
38-
**Software and Dependencies:**
39-
40-
* Adafruit CircuitPython firmware for the supported boards:
41-
https://github.com/adafruit/circuitpython/releases
42-
4334
"""
4435

4536
from .midi_message import MIDIMessage, note_parser

adafruit_midi/note_on.py

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,17 @@
2020
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2121
# THE SOFTWARE.
2222
"""
23-
`adafruit_midi`
23+
`adafruit_midi.note_on`
2424
================================================================================
2525
26-
A CircuitPython helper for encoding/decoding MIDI packets over a MIDI or UART connection.
26+
Note On Change MIDI message.
2727
2828
2929
* Author(s): Kevin J. Walters
3030
3131
Implementation Notes
3232
--------------------
3333
34-
**Hardware:**
35-
36-
37-
38-
**Software and Dependencies:**
39-
40-
* Adafruit CircuitPython firmware for the supported boards:
41-
https://github.com/adafruit/circuitpython/releases
42-
4334
"""
4435

4536
from .midi_message import MIDIMessage, note_parser

adafruit_midi/pitch_bend_change.py

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,17 @@
2020
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2121
# THE SOFTWARE.
2222
"""
23-
`adafruit_midi`
23+
`adafruit_midi.pitch_bend_change`
2424
================================================================================
2525
26-
A CircuitPython helper for encoding/decoding MIDI packets over a MIDI or UART connection.
26+
Pitch Bend Change MIDI message.
2727
2828
2929
* Author(s): Kevin J. Walters
3030
3131
Implementation Notes
3232
--------------------
3333
34-
**Hardware:**
35-
36-
37-
38-
**Software and Dependencies:**
39-
40-
* Adafruit CircuitPython firmware for the supported boards:
41-
https://github.com/adafruit/circuitpython/releases
42-
4334
"""
4435

4536
from .midi_message import MIDIMessage

adafruit_midi/polyphonic_key_pressure.py

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,17 @@
2020
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2121
# THE SOFTWARE.
2222
"""
23-
`adafruit_midi`
23+
`adafruit_midi.polyphonic_key_pressure`
2424
================================================================================
2525
26-
A CircuitPython helper for encoding/decoding MIDI packets over a MIDI or UART connection.
26+
Polyphonic Key Pressure MIDI message.
2727
2828
2929
* Author(s): Kevin J. Walters
3030
3131
Implementation Notes
3232
--------------------
3333
34-
**Hardware:**
35-
36-
37-
38-
**Software and Dependencies:**
39-
40-
* Adafruit CircuitPython firmware for the supported boards:
41-
https://github.com/adafruit/circuitpython/releases
42-
4334
"""
4435

4536
from .midi_message import MIDIMessage, note_parser

adafruit_midi/program_change.py

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,17 @@
2020
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2121
# THE SOFTWARE.
2222
"""
23-
`adafruit_midi`
23+
`adafruit_midi.program_change`
2424
================================================================================
2525
26-
A CircuitPython helper for encoding/decoding MIDI packets over a MIDI or UART connection.
26+
Program Change MIDI message.
2727
2828
2929
* Author(s): Kevin J. Walters
3030
3131
Implementation Notes
3232
--------------------
3333
34-
**Hardware:**
35-
36-
37-
38-
**Software and Dependencies:**
39-
40-
* Adafruit CircuitPython firmware for the supported boards:
41-
https://github.com/adafruit/circuitpython/releases
42-
4334
"""
4435

4536
from .midi_message import MIDIMessage

adafruit_midi/start.py

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,17 @@
2020
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2121
# THE SOFTWARE.
2222
"""
23-
`adafruit_midi`
23+
`adafruit_midi.start`
2424
================================================================================
2525
26-
A CircuitPython helper for encoding/decoding MIDI packets over a MIDI or UART connection.
26+
Start MIDI message.
2727
2828
2929
* Author(s): Kevin J. Walters
3030
3131
Implementation Notes
3232
--------------------
3333
34-
**Hardware:**
35-
36-
37-
38-
**Software and Dependencies:**
39-
40-
* Adafruit CircuitPython firmware for the supported boards:
41-
https://github.com/adafruit/circuitpython/releases
42-
4334
"""
4435

4536
from .midi_message import MIDIMessage

0 commit comments

Comments
 (0)