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 d38e549..0186154 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 index c5c3282..d85d0f7 100644 --- a/examples/midi_simpletest.py +++ b/examples/midi_simpletest.py @@ -1,7 +1,12 @@ +# simple_test import time import random import usb_midi import adafruit_midi +from adafruit_midi.control_change import ControlChange +from adafruit_midi.note_off import NoteOff +from adafruit_midi.note_on import NoteOn +from adafruit_midi.pitch_bend import PitchBend midi = adafruit_midi.MIDI(midi_out=usb_midi.ports[1], out_channel=0) @@ -13,8 +18,12 @@ 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) + 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) + # note how a list of messages can be used + midi.send([NoteOff("G#2", 120), + ControlChange(3, 44)]) + time.sleep(0.5) diff --git a/examples/midi_simpletest2.py b/examples/midi_simpletest2.py deleted file mode 100644 index a0425d5..0000000 --- a/examples/midi_simpletest2.py +++ /dev/null @@ -1,37 +0,0 @@ -# simple_test demonstrating both interfaces -import time -import random -import usb_midi -import adafruit_midi -from adafruit_midi.control_change import ControlChange -from adafruit_midi.note_off import NoteOff -from adafruit_midi.note_on import NoteOn -from adafruit_midi.pitch_bend import PitchBend - -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: - # method per message interface - midi.note_on(44, 120) - 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))) - time.sleep(0.25) - midi.send([NoteOff("G#2", 120), - ControlChange(3, 44)]) - time.sleep(0.5)