Skip to content

Commit b42da3c

Browse files
committed
fix program change send bug, add test
1 parent 4afb00e commit b42da3c

File tree

5 files changed

+59
-5
lines changed

5 files changed

+59
-5
lines changed

README.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,12 @@ API documentation for this library can be found on `Read the Docs <https://circu
108108
For information on building library documentation, please check out
109109
`this guide <https://learn.adafruit.com/creating-and-sharing-a-circuitpython-library/sharing-our-docs-on-readthedocs#sphinx-5-1>`_.
110110

111+
Testing
112+
=======
113+
114+
Install `pytest` with `pip3 install pytest --upgrade` and run `pytest -v`
115+
116+
111117
Contributing
112118
============
113119

tests/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2024 Tod Kurt
2+
# SPDX-License-Identifier: MIT

tests/test_tmidi_messages.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2024 Tod Kurt
2+
# SPDX-License-Identifier: MIT
3+
4+
5+
import tmidi
6+
7+
8+
class PortStub:
9+
def __init__(self, data):
10+
self.data = data
11+
self.expected = None
12+
13+
def write(self, buf, nbytes):
14+
assert self.expected == list(buf)
15+
16+
17+
def test_message_note_on():
18+
msg = tmidi.Message(tmidi.NOTE_ON, 64, 123)
19+
assert str(msg) == "Message(NoteOn ch:0 64 123)"
20+
21+
22+
def test_message_program_change():
23+
msg = tmidi.Message(tmidi.PROGRAM_CHANGE, 120, channel=11)
24+
assert str(msg) == "Message(ProgramChange ch:11 120)"
25+
26+
27+
def test_message_note_on_send():
28+
port = PortStub(iter([]))
29+
midi_out = tmidi.MIDI(midi_out=port)
30+
31+
msg = tmidi.Message(tmidi.NOTE_ON, 64, 123)
32+
port.expected = [0x90, 64, 123]
33+
midi_out.send(msg)
34+
35+
msg = tmidi.Message(tmidi.NOTE_ON, 32, 111, channel=3)
36+
port.expected = [0x93, 32, 111]
37+
midi_out.send(msg)
38+
39+
40+
def test_message_program_change_send():
41+
port = PortStub(iter([]))
42+
midi_out = tmidi.MIDI(midi_out=port)
43+
44+
msg = tmidi.Message(tmidi.PROGRAM_CHANGE, 33)
45+
port.expected = [0xC0, 33]
46+
midi_out.send(msg)

tests/test_tmidi_one.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def readinto(self, buf, numbytes=1):
3232

3333
return bytes_read
3434

35-
def write(self, buf):
35+
def write(self, buf, n):
3636
pass
3737

3838

tmidi.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -174,14 +174,14 @@ class Message:
174174
MIDI Message.
175175
176176
:param mtype: The type of message, e.g. tmidi.NOTE_ON.
177-
:param channel: The MIDI channel for this message, if applicable (0-15)
178177
:param data0: The first data byte for this message,
179178
e.g. the note number as an ``int`` (0-127) for NOTE_ON messages.
180179
:param data1: The second data byte for this message,
181180
e.g. the velocity (0-127) for NOTE_ON messages.
181+
:param channel: The MIDI channel for this message, if applicable (0-15)
182182
"""
183183

184-
def __init__(self, mtype=SYSTEM_RESET, channel=None, data0=0, data1=0):
184+
def __init__(self, mtype=SYSTEM_RESET, data0=0, data1=0, channel=0):
185185
"""
186186
Create a MIDI Message.
187187
@@ -204,9 +204,9 @@ def __bytes__(self):
204204
status_byte = self.type
205205
if _is_channel_message(status_byte):
206206
status_byte |= self.channel
207-
if status_byte in _LEN_2_MESSAGES:
207+
if self.type in _LEN_2_MESSAGES:
208208
return bytes([status_byte, self.data0, self.data1])
209-
if status_byte in _LEN_1_MESSAGES:
209+
elif self.type in _LEN_1_MESSAGES:
210210
return bytes([status_byte, self.data0])
211211
return bytes([status_byte])
212212

0 commit comments

Comments
 (0)