Skip to content

Commit 6eed59c

Browse files
author
Kevin J Walters
committed
Removing the original method per MIDI message (event) API in favour of new send() method for class based messages.
Updating examples in README.rst and midi_simpletest2.py, removing midi_simpletest.py. adafruit#12
1 parent 5a7dc9d commit 6eed59c

File tree

6 files changed

+15
-94
lines changed

6 files changed

+15
-94
lines changed

LICENSE

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The MIT License (MIT)
22

3-
Copyright (c) 2019 Ladyada for Adafruit Industries
3+
Copyright (c) 2019 Ladyada for Adafruit Industries, Kevin J. Walters
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.rst

+8-5
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,14 @@ Usage Example
6868
print("Default output MIDI channel:", midi.out_channel + 1)
6969
7070
while True:
71-
midi.note_on(44, 120)
72-
midi.note_off(44, 120)
73-
midi.control_change(3, 44)
74-
midi.pitch_bend(random.randint(0,16383))
75-
time.sleep(1)
71+
midi.send(NoteOn(44, 120)) # G sharp 2nd octave
72+
time.sleep(0.25)
73+
a_pitch_bend = PitchBend(random.randint(0, 16383))
74+
midi.send(a_pitch_bend)
75+
time.sleep(0.25)
76+
midi.send([NoteOff("G#2", 120),
77+
ControlChange(3, 44)])
78+
time.sleep(0.5)
7679
7780
7881
Contributing

adafruit_midi/__init__.py

-54
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,6 @@ class MIDI:
6868
6969
"""
7070

71-
NOTE_ON = 0x90
72-
NOTE_OFF = 0x80
73-
PITCH_BEND = 0xE0
74-
CONTROL_CHANGE = 0xB0
75-
7671
def __init__(self, midi_in=None, midi_out=None, *,
7772
in_channel=None, out_channel=0, in_buf_size=30, debug=False):
7873
if midi_in is None and midi_out is None:
@@ -173,55 +168,6 @@ def send(self, msg, channel=None):
173168

174169
self._send(data, len(data))
175170

176-
def note_on(self, note, vel, channel=None):
177-
"""Sends a MIDI Note On message.
178-
179-
:param int note: The note number. Must be 0-127.
180-
:param int vel: The note velocity. Must be 0-127.
181-
182-
"""
183-
self._generic_3(self.NOTE_ON, note, vel, channel)
184-
185-
def note_off(self, note, vel, channel=None):
186-
"""Sends a MIDI Note Off message.
187-
188-
:param int note: The note number. Must be 0-127.
189-
:param int vel: The note velocity. Must be 0-127.
190-
191-
"""
192-
self._generic_3(self.NOTE_OFF, note, vel, channel)
193-
194-
def pitch_bend(self, value, channel=None):
195-
"""Send a MIDI Pitch Wheel message.
196-
197-
:param int value: Range is 0-16383. A ``value`` of 8192 equates to no pitch bend, a value
198-
of less than 8192 equates to a negative pitch bend, and a value of more
199-
than 8192 equates to a positive pitch bend.
200-
201-
"""
202-
self._generic_3(self.PITCH_BEND, value & 0x7F, value >> 7, channel)
203-
204-
def control_change(self, control, value, channel=None):
205-
"""Sends a MIDI CC message.
206-
207-
:param int control: The controller number. Must be 0-127.
208-
:param int value: The control value. Must be 0-127.
209-
210-
"""
211-
self._generic_3(self.CONTROL_CHANGE, control, value, channel)
212-
213-
def _generic_3(self, cmd, arg1, arg2, channel=None):
214-
if not 0 <= arg1 <= 0x7F:
215-
raise RuntimeError("Argument 1 value %d invalid" % arg1)
216-
if not 0 <= arg2 <= 0x7F:
217-
raise RuntimeError("Argument 2 value %d invalid" % arg2)
218-
if channel is None:
219-
channel = self._out_channel
220-
self._outbuf[0] = (cmd & 0xF0) | (channel & 0x0f)
221-
self._outbuf[1] = arg1
222-
self._outbuf[2] = arg2
223-
self._send(self._outbuf, 3)
224-
225171
def _send(self, packet, num):
226172
if self._debug:
227173
print("Sending: ", [hex(i) for i in packet[:num]])

examples/midi_intest1.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
print("Midi input test with pauses")
1919

2020
# Convert channel numbers at the presentation layer to the ones musicians use
21-
print("Input channel:", midi.in_channel + 1 )
21+
print("Input channel:", midi.in_channel + 1)
2222

2323
# play with the pause to simulate code doing other stuff
2424
# in the loop

examples/midi_simpletest.py

-20
This file was deleted.

examples/midi_simpletest2.py

+5-13
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# simple_test demonstrating both interfaces
1+
# simple_test
22
import time
33
import random
44
import usb_midi
@@ -18,20 +18,12 @@
1818
midi.in_channel + 1 if midi.in_channel is not None else None)
1919

2020
while True:
21-
# method per message interface
22-
midi.note_on(44, 120)
21+
midi.send(NoteOn(44, 120)) # G sharp 2nd octave
2322
time.sleep(0.25)
24-
midi.pitch_bend(random.randint(0, 16383))
25-
time.sleep(0.25)
26-
midi.note_off(44, 120)
27-
midi.control_change(3, 44)
28-
time.sleep(0.5)
29-
30-
# send message(s) interface
31-
midi.send(NoteOn(44, 120))
32-
time.sleep(0.25)
33-
midi.send(PitchBend(random.randint(0, 16383)))
23+
a_pitch_bend = PitchBend(random.randint(0, 16383))
24+
midi.send(a_pitch_bend)
3425
time.sleep(0.25)
26+
# note how a list of messages can be used
3527
midi.send([NoteOff("G#2", 120),
3628
ControlChange(3, 44)])
3729
time.sleep(0.5)

0 commit comments

Comments
 (0)