Skip to content

Commit bb44772

Browse files
author
Kevin J Walters
committed
Giving NoteOn and NoteOff some default velocity values as this is useful from REPL. #3
1 parent e3dfd0e commit bb44772

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

adafruit_midi/note_off.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,15 @@ class NoteOff(MIDIMessage):
4444
4545
:param note: The note (key) number either as an ``int`` (0-127) or a
4646
``str`` which is parsed, e.g. "C4" (middle C) is 60, "A4" is 69.
47-
:param int velocity: The release velocity, 0-127.
47+
:param int velocity: The release velocity, 0-127, defaults to 0.
4848
4949
"""
5050

5151
_STATUS = 0x80
5252
_STATUSMASK = 0xf0
5353
LENGTH = 3
5454

55-
def __init__(self, note, velocity, *, channel=None):
55+
def __init__(self, note, velocity=0, *, channel=None):
5656
self.note = note_parser(note)
5757
self.velocity = velocity
5858
super().__init__(channel=channel)

adafruit_midi/note_on.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,14 @@ class NoteOn(MIDIMessage):
4545
:param note: The note (key) number either as an ``int`` (0-127) or a
4646
``str`` which is parsed, e.g. "C4" (middle C) is 60, "A4" is 69.
4747
:param int velocity: The strike velocity, 0-127, 0 is equivalent
48-
to a Note Off.
48+
to a Note Off, defaults to 127.
4949
"""
5050

5151
_STATUS = 0x90
5252
_STATUSMASK = 0xf0
5353
LENGTH = 3
5454

55-
def __init__(self, note, velocity, *, channel=None):
55+
def __init__(self, note, velocity=127, *, channel=None):
5656
self.note = note_parser(note)
5757
self.velocity = velocity
5858
super().__init__(channel=channel)

tests/test_MIDIMessage_unittests.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,12 @@ def test_NoteOn_constructor_int(self):
223223
self.assertEqual(object3.velocity, 0x50)
224224
self.assertEqual(object3.channel, 7)
225225

226+
object4 = NoteOn(60) # velocity defaults to 127
227+
228+
self.assertEqual(object4.note, 60)
229+
self.assertEqual(object4.velocity, 127)
230+
self.assertIsNone(object4.channel)
231+
226232
def test_SystemExclusive_NoteOn(self):
227233
data = bytes([0xf0, 0x42, 0x01, 0x02, 0x03, 0x04, 0xf7, 0x90 | 14, 0x30, 0x60])
228234
ichannel = 14
@@ -335,6 +341,10 @@ def test_NoteOff_constructor_string(self):
335341
self.assertEqual(object3.note, 61)
336342
self.assertEqual(object3.velocity, 0)
337343

344+
object4 = NoteOff("C#4") # velocity defaults to 0
345+
self.assertEqual(object4.note, 61)
346+
self.assertEqual(object4.velocity, 0)
347+
338348
def test_NoteOff_constructor_valueerror1(self):
339349
with self.assertRaises(ValueError):
340350
NoteOff(60, 0x80)

0 commit comments

Comments
 (0)