Skip to content

Commit

Permalink
Removing the original method per MIDI message (event) API in favour o…
Browse files Browse the repository at this point in the history
…f new send() method for class based messages.

Updating examples in README.rst and midi_simpletest2.py, removing midi_simpletest.py. #12
  • Loading branch information
Kevin J Walters committed Apr 26, 2019
1 parent 5a7dc9d commit 6eed59c
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 94 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -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
Expand Down
13 changes: 8 additions & 5 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
54 changes: 0 additions & 54 deletions adafruit_midi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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]])
Expand Down
2 changes: 1 addition & 1 deletion examples/midi_intest1.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 0 additions & 20 deletions examples/midi_simpletest.py

This file was deleted.

18 changes: 5 additions & 13 deletions examples/midi_simpletest2.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# simple_test demonstrating both interfaces
# simple_test
import time
import random
import usb_midi
Expand All @@ -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)

0 comments on commit 6eed59c

Please sign in to comment.