Skip to content

Commit 754dad7

Browse files
committed
Allow velocity and channel directly as kwargs in Note constructor
1 parent 803870a commit 754dad7

2 files changed

Lines changed: 18 additions & 4 deletions

File tree

mingus/containers/note.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,15 @@ class Note(object):
5151
channel = _DEFAULT_CHANNEL
5252
velocity = _DEFAULT_VELOCITY
5353

54-
def __init__(self, name="C", octave=4, dynamics=None):
54+
def __init__(self, name="C", octave=4, dynamics=None, velocity=None, channel=None):
5555
if dynamics is None:
5656
dynamics = {}
57+
58+
if velocity is not None:
59+
dynamics["velocity"] = velocity
60+
if channel is not None:
61+
dynamics["channel"] = channel
62+
5763
if isinstance(name, six.string_types):
5864
self.set_note(name, octave, dynamics)
5965
elif hasattr(name, "name"):
@@ -83,18 +89,24 @@ def set_velocity(self, velocity):
8389
raise ValueError("MIDI velocity must be 0-127")
8490
self.velocity = velocity
8591

86-
def set_note(self, name="C", octave=4, dynamics=None):
92+
def set_note(self, name="C", octave=4, dynamics=None, velocity=None, channel=None):
8793
"""Set the note to name in octave with dynamics.
8894
8995
Return the objects if it succeeded, raise an NoteFormatError
9096
otherwise.
9197
"""
9298
if dynamics is None:
9399
dynamics = {}
100+
101+
if velocity is not None:
102+
self.set_velocity(velocity)
103+
elif "velocity" in dynamics:
104+
self.set_velocity(dynamics["velocity"])
105+
106+
if channel is not None:
107+
self.set_channel(channel)
94108
if "channel" in dynamics:
95109
self.set_channel(dynamics["channel"])
96-
if "velocity" in dynamics:
97-
self.set_velocity(dynamics["velocity"])
98110

99111
dash_index = name.split("-")
100112
if len(dash_index) == 1:

tests/unit/containers/test_note.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,13 @@ def test_velocity(self):
117117
self.assertEqual(42, Note("A", 4, {"velocity": 42}).velocity)
118118
self.assertEqual(64, Note("A", 4, {"channel": 2}).velocity) # default velocity
119119
self.assertEqual(42, Note(Note("A", 4, {"velocity": 42})).velocity)
120+
self.assertEqual(42, Note("A", 4, velocity=42).velocity)
120121

121122
def test_channel(self):
122123
self.assertEqual(1, Note("A", 4, {"velocity": 100}).channel) # default channel
123124
self.assertEqual(4, Note("A", 4, {"channel": 4}).channel)
124125
self.assertEqual(4, Note(Note("A", 4, {"channel": 4})).channel)
126+
self.assertEqual(8, Note("A", 4, channel=8).channel)
125127

126128
def test_invalid_channel(self):
127129
with self.assertRaises(ValueError):

0 commit comments

Comments
 (0)