From 6eed59c82d4e8ca5d70985f9b06aba714dd2d33c Mon Sep 17 00:00:00 2001 From: Kevin J Walters Date: Fri, 26 Apr 2019 19:57:09 +0100 Subject: [PATCH 1/4] 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. #12 --- LICENSE | 2 +- README.rst | 13 +++++---- adafruit_midi/__init__.py | 54 ------------------------------------ examples/midi_intest1.py | 2 +- examples/midi_simpletest.py | 20 ------------- examples/midi_simpletest2.py | 18 ++++-------- 6 files changed, 15 insertions(+), 94 deletions(-) delete mode 100644 examples/midi_simpletest.py diff --git a/LICENSE b/LICENSE index 3d3de28..e622f85 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ The MIT License (MIT) -Copyright (c) 2019 Ladyada for Adafruit Industries +Copyright (c) 2019 Ladyada for Adafruit Industries, Kevin J. Walters Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.rst b/README.rst index 0ce3d20..3fccae9 100644 --- a/README.rst +++ b/README.rst @@ -68,11 +68,14 @@ Usage Example print("Default output MIDI channel:", midi.out_channel + 1) while True: - midi.note_on(44, 120) - midi.note_off(44, 120) - midi.control_change(3, 44) - midi.pitch_bend(random.randint(0,16383)) - time.sleep(1) + midi.send(NoteOn(44, 120)) # G sharp 2nd octave + time.sleep(0.25) + a_pitch_bend = PitchBend(random.randint(0, 16383)) + midi.send(a_pitch_bend) + time.sleep(0.25) + midi.send([NoteOff("G#2", 120), + ControlChange(3, 44)]) + time.sleep(0.5) Contributing diff --git a/adafruit_midi/__init__.py b/adafruit_midi/__init__.py index cd421c5..e6aa1d1 100644 --- a/adafruit_midi/__init__.py +++ b/adafruit_midi/__init__.py @@ -68,11 +68,6 @@ class MIDI: """ - NOTE_ON = 0x90 - NOTE_OFF = 0x80 - PITCH_BEND = 0xE0 - CONTROL_CHANGE = 0xB0 - def __init__(self, midi_in=None, midi_out=None, *, in_channel=None, out_channel=0, in_buf_size=30, debug=False): if midi_in is None and midi_out is None: @@ -173,55 +168,6 @@ def send(self, msg, channel=None): self._send(data, len(data)) - def note_on(self, note, vel, channel=None): - """Sends a MIDI Note On message. - - :param int note: The note number. Must be 0-127. - :param int vel: The note velocity. Must be 0-127. - - """ - self._generic_3(self.NOTE_ON, note, vel, channel) - - def note_off(self, note, vel, channel=None): - """Sends a MIDI Note Off message. - - :param int note: The note number. Must be 0-127. - :param int vel: The note velocity. Must be 0-127. - - """ - self._generic_3(self.NOTE_OFF, note, vel, channel) - - def pitch_bend(self, value, channel=None): - """Send a MIDI Pitch Wheel message. - - :param int value: Range is 0-16383. A ``value`` of 8192 equates to no pitch bend, a value - of less than 8192 equates to a negative pitch bend, and a value of more - than 8192 equates to a positive pitch bend. - - """ - self._generic_3(self.PITCH_BEND, value & 0x7F, value >> 7, channel) - - def control_change(self, control, value, channel=None): - """Sends a MIDI CC message. - - :param int control: The controller number. Must be 0-127. - :param int value: The control value. Must be 0-127. - - """ - self._generic_3(self.CONTROL_CHANGE, control, value, channel) - - def _generic_3(self, cmd, arg1, arg2, channel=None): - if not 0 <= arg1 <= 0x7F: - raise RuntimeError("Argument 1 value %d invalid" % arg1) - if not 0 <= arg2 <= 0x7F: - raise RuntimeError("Argument 2 value %d invalid" % arg2) - if channel is None: - channel = self._out_channel - self._outbuf[0] = (cmd & 0xF0) | (channel & 0x0f) - self._outbuf[1] = arg1 - self._outbuf[2] = arg2 - self._send(self._outbuf, 3) - def _send(self, packet, num): if self._debug: print("Sending: ", [hex(i) for i in packet[:num]]) diff --git a/examples/midi_intest1.py b/examples/midi_intest1.py index 86bd971..559550e 100644 --- a/examples/midi_intest1.py +++ b/examples/midi_intest1.py @@ -18,7 +18,7 @@ print("Midi input test with pauses") # Convert channel numbers at the presentation layer to the ones musicians use -print("Input channel:", midi.in_channel + 1 ) +print("Input channel:", midi.in_channel + 1) # play with the pause to simulate code doing other stuff # in the loop diff --git a/examples/midi_simpletest.py b/examples/midi_simpletest.py deleted file mode 100644 index c5c3282..0000000 --- a/examples/midi_simpletest.py +++ /dev/null @@ -1,20 +0,0 @@ -import time -import random -import usb_midi -import adafruit_midi - -midi = adafruit_midi.MIDI(midi_out=usb_midi.ports[1], out_channel=0) - -print("Midi test") - -# Convert channel numbers at the presentation layer to the ones musicians use -print("Default output channel:", midi.out_channel + 1) -print("Listening on input channel:", - midi.in_channel + 1 if midi.in_channel is not None else None) - -while True: - midi.note_on(44, 120) - midi.note_off(44, 120) - midi.control_change(3, 44) - midi.pitch_bend(random.randint(0, 16383)) - time.sleep(1) diff --git a/examples/midi_simpletest2.py b/examples/midi_simpletest2.py index a0425d5..d85d0f7 100644 --- a/examples/midi_simpletest2.py +++ b/examples/midi_simpletest2.py @@ -1,4 +1,4 @@ -# simple_test demonstrating both interfaces +# simple_test import time import random import usb_midi @@ -18,20 +18,12 @@ midi.in_channel + 1 if midi.in_channel is not None else None) while True: - # method per message interface - midi.note_on(44, 120) + midi.send(NoteOn(44, 120)) # G sharp 2nd octave time.sleep(0.25) - midi.pitch_bend(random.randint(0, 16383)) - time.sleep(0.25) - midi.note_off(44, 120) - midi.control_change(3, 44) - time.sleep(0.5) - - # send message(s) interface - midi.send(NoteOn(44, 120)) - time.sleep(0.25) - midi.send(PitchBend(random.randint(0, 16383))) + a_pitch_bend = PitchBend(random.randint(0, 16383)) + midi.send(a_pitch_bend) time.sleep(0.25) + # note how a list of messages can be used midi.send([NoteOff("G#2", 120), ControlChange(3, 44)]) time.sleep(0.5) From 0731bed88d5056d55ca0c2e19a5a53e89845509e Mon Sep 17 00:00:00 2001 From: Kevin J Walters Date: Fri, 26 Apr 2019 20:24:34 +0100 Subject: [PATCH 2/4] examples.rst needs updating to use midi_simpletest2.py for new API example. #12 --- docs/examples.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/examples.rst b/docs/examples.rst index 26f7a40..f7ae16d 100644 --- a/docs/examples.rst +++ b/docs/examples.rst @@ -3,6 +3,6 @@ Simple test Ensure your device works with this simple test. -.. literalinclude:: ../examples/midi_simpletest.py - :caption: examples/midi_simpletest.py +.. literalinclude:: ../examples/midi_simpletest2.py + :caption: examples/midi_simpletest2.py :linenos: From 96b68ee7cac7d3410eb4ea734f07ed7c227a7326 Mon Sep 17 00:00:00 2001 From: Kevin J Walters Date: Sat, 26 Oct 2019 00:56:07 +0100 Subject: [PATCH 3/4] Renaming midi_simpletest2.py to midi_simpletest.py per @tannewt recommendation. #12 #13 --- examples/{midi_simpletest2.py => midi_simpletest.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename examples/{midi_simpletest2.py => midi_simpletest.py} (100%) diff --git a/examples/midi_simpletest2.py b/examples/midi_simpletest.py similarity index 100% rename from examples/midi_simpletest2.py rename to examples/midi_simpletest.py From d933879a5138b4c75e5f9302ea46abe0f8fbe1bb Mon Sep 17 00:00:00 2001 From: Kevin J Walters Date: Thu, 31 Oct 2019 00:09:06 +0000 Subject: [PATCH 4/4] Aligning example filename with the renamed file. #12 #13 --- docs/examples.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/examples.rst b/docs/examples.rst index f7ae16d..26f7a40 100644 --- a/docs/examples.rst +++ b/docs/examples.rst @@ -3,6 +3,6 @@ Simple test Ensure your device works with this simple test. -.. literalinclude:: ../examples/midi_simpletest2.py - :caption: examples/midi_simpletest2.py +.. literalinclude:: ../examples/midi_simpletest.py + :caption: examples/midi_simpletest.py :linenos: