Skip to content

Commit a510002

Browse files
Merge pull request #469 from CPJKU/consistent_channel
Consistent MIDI channel default value of 0
2 parents ef64300 + 322c0df commit a510002

File tree

5 files changed

+28
-21
lines changed

5 files changed

+28
-21
lines changed

partitura/io/exportmidi.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,34 +36,34 @@ def map_to_track_channel(note_keys, mode):
3636
if mode == 0:
3737
trk = tr_helper.setdefault(p, len(tr_helper))
3838
ch1 = ch_helper.setdefault(p, {})
39-
ch2 = ch1.setdefault(v, len(ch1) + 1)
39+
ch2 = ch1.setdefault(v, len(ch1))
4040
track[(pg, p, v)] = trk
4141
channel[(pg, p, v)] = ch2
4242
elif mode == 1:
4343
trk = tr_helper.setdefault(pg, len(tr_helper))
4444
ch1 = ch_helper.setdefault(pg, {})
45-
ch2 = ch1.setdefault(p, len(ch1) + 1)
45+
ch2 = ch1.setdefault(p, len(ch1))
4646
track[(pg, p, v)] = trk
4747
channel[(pg, p, v)] = ch2
4848
elif mode == 2:
4949
track[(pg, p, v)] = 0
50-
ch = ch_helper.setdefault(p, len(ch_helper) + 1)
50+
ch = ch_helper.setdefault(p, len(ch_helper))
5151
channel[(pg, p, v)] = ch
5252
elif mode == 3:
5353
trk = tr_helper.setdefault(p, len(tr_helper))
5454
track[(pg, p, v)] = trk
55-
channel[(pg, p, v)] = 1
55+
channel[(pg, p, v)] = 0
5656
elif mode == 4:
5757
track[(pg, p, v)] = 0
58-
channel[(pg, p, v)] = 1
58+
channel[(pg, p, v)] = 0
5959
elif mode == 5:
6060
trk = tr_helper.setdefault((p, v), len(tr_helper))
6161
track[(pg, p, v)] = trk
62-
channel[(pg, p, v)] = 1
62+
channel[(pg, p, v)] = 0
6363
else:
6464
raise Exception("unsupported part/voice assign mode {}".format(mode))
6565

66-
result = dict((k, (track.get(k, 0), channel.get(k, 1))) for k in note_keys)
66+
result = dict((k, (track.get(k, 0), channel.get(k, 0))) for k in note_keys)
6767
# for (pg, p, voice), v in result.items():
6868
# pgn = pg.group_name if hasattr(pg, 'group_name') else pg.id
6969
# print(pgn, p.id, voice)
@@ -177,7 +177,7 @@ def save_performance_midi(
177177

178178
for c in performed_part.controls:
179179
track = c.get("track", 0)
180-
ch = c.get("channel", 1)
180+
ch = c.get("channel", 0)
181181
t = int(np.round(10**6 * ppq * c["time"] / mpq))
182182
track_events[track][t].append(
183183
Message(
@@ -190,7 +190,7 @@ def save_performance_midi(
190190

191191
for n in performed_part.notes:
192192
track = n.get("track", 0)
193-
ch = n.get("channel", 1)
193+
ch = n.get("channel", 0)
194194
t_on = int(np.round(10**6 * ppq * n["note_on"] / mpq))
195195
t_off = int(np.round(10**6 * ppq * n["note_off"] / mpq))
196196
vel = n.get("velocity", default_velocity)
@@ -203,7 +203,7 @@ def save_performance_midi(
203203

204204
for p in performed_part.programs:
205205
track = p.get("track", 0)
206-
ch = p.get("channel", 1)
206+
ch = p.get("channel", 0)
207207
t = int(np.round(10**6 * ppq * p["time"] / mpq))
208208
track_events[track][t].append(
209209
Message("program_change", program=int(p["program"]), channel=ch)
@@ -215,11 +215,11 @@ def save_performance_midi(
215215
list(
216216
set(
217217
[
218-
(c.get("channel", 1), c.get("track", 0))
218+
(c.get("channel", 0), c.get("track", 0))
219219
for c in performed_part.controls
220220
]
221221
+ [
222-
(n.get("channel", 1), n.get("track", 0))
222+
(n.get("channel", 0), n.get("track", 0))
223223
for n in performed_part.notes
224224
]
225225
)

partitura/musicanalysis/performance_codec.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -672,9 +672,8 @@ def to_matched_score(
672672
sn_dur = sn_off - sn_on
673673
# hack for notes with negative durations
674674
n_dur = max(n["duration_sec"], 60 / 200 * 0.25)
675-
pair_info = (sn_on, sn_dur, sn["pitch"], n["onset_sec"], n_dur, n["velocity"])
676-
if include_score_markings:
677-
pair_info += (sn["voice"].item(),)
675+
pair_info = (sn_on, sn_dur, sn["pitch"], n["onset_sec"], n_dur, n["velocity"], sn["voice"].item())
676+
if include_score_markings:
678677
pair_info += tuple(
679678
[sn[field].item() for field in sn.dtype.names if "feature" in field]
680679
)
@@ -688,9 +687,10 @@ def to_matched_score(
688687
("p_onset", "f4"),
689688
("p_duration", "f4"),
690689
("velocity", "i4"),
690+
("voice", "i4")
691691
]
692+
692693
if include_score_markings and not isinstance(score, np.ndarray):
693-
fields += [("voice", "i4")]
694694
fields += [
695695
(field, sn.dtype.fields[field][0])
696696
for field in sn.dtype.fields

partitura/musicanalysis/performance_features.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ def make_performance_features(
4040
alignment: list,
4141
feature_functions: Union[List, str],
4242
add_idx: bool = True,
43+
include_score_markings: bool = True
4344
):
4445
"""
4546
Compute the performance features. This function is defined in the same
@@ -61,13 +62,15 @@ def make_performance_features(
6162
asynchrony_feature, articulation_feature, dynamics_feature, pedal_feature
6263
add_idx: bool
6364
add score note idx column to feature array
65+
include_score_markings (bool): include dynamcis and articulation
66+
markings (Optional)
6467
6568
Returns
6669
-------
6770
performance_features : structured array
6871
"""
6972
m_score, unique_onset_idxs, snote_ids = compute_matched_score(
70-
score, performance, alignment
73+
score, performance, alignment, include_score_markings
7174
)
7275

7376
acc = []
@@ -141,6 +144,7 @@ def compute_matched_score(
141144
score: ScoreLike,
142145
performance: PerformanceLike,
143146
alignment: list,
147+
include_score_markings = True
144148
):
145149
"""
146150
Compute the matched score and add the score features
@@ -153,6 +157,8 @@ def compute_matched_score(
153157
Performance information, can be a ppart, performance
154158
alignment : list
155159
The score--performance alignment, a list of dictionaries
160+
include_score_markings (bool): include dynamcis and articulation
161+
markings (Optional)
156162
157163
Returns
158164
-------
@@ -161,8 +167,9 @@ def compute_matched_score(
161167
"""
162168

163169
m_score, snote_ids = to_matched_score(
164-
score, performance, alignment, include_score_markings=True
170+
score, performance, alignment, include_score_markings=include_score_markings
165171
)
172+
166173
(time_params, unique_onset_idxs) = encode_tempo(
167174
score_onsets=m_score["onset"],
168175
performed_onsets=m_score["p_onset"],

partitura/performance.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ def note_array(self, *args, **kwargs) -> np.ndarray:
209209
n["midi_pitch"],
210210
n["velocity"],
211211
n.get("track", 0),
212-
n.get("channel", 1),
212+
n.get("channel", 0),
213213
n["id"],
214214
)
215215
)
@@ -364,7 +364,7 @@ def __init__(self, pnote_dict):
364364
"sound_off", self["note_off"]
365365
)
366366
self.pnote_dict["track"] = self.pnote_dict.get("track", 0)
367-
self.pnote_dict["channel"] = self.pnote_dict.get("channel", 1)
367+
self.pnote_dict["channel"] = self.pnote_dict.get("channel", 0)
368368
self.pnote_dict["velocity"] = self.pnote_dict.get("velocity", 60)
369369
self._validate_values(pnote_dict)
370370
self._accepted_keys = [

tests/test_performance.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ def test_initialization(self):
158158

159159
# Test initialization to default values
160160
self.assertEqual(pnote["track"], 0)
161-
self.assertEqual(pnote["channel"], 1)
161+
self.assertEqual(pnote["channel"], 0)
162162
self.assertEqual(pnote["velocity"], 60)
163163
self.assertEqual(pnote["sound_off"], 20)
164164

0 commit comments

Comments
 (0)