Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

`music21.midi.MidiEvent.getBytes()` confuses `SEQUENCER_SPECIFIC_META_EVENT` (0x7F) with `POLY_MODE_ON` (also 0x7F)

未关闭
#1,772 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
55/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
停滞
技术栈
python

调研方向

The bug is in getBytes() in music21/midi/init.py, which dispatches on the raw enum value so SEQUENCER_SPECIFIC_META_EVENT (0x7F) matches ChannelModeMessages.POLY_MODE_ON and crashes when channel is None. Reproduce with the snippet in the issue, then make the branch checks also verify the enum class of self.type. Done when the snippet no longer raises and a MidiFile read-then-write round trip succeeds.

由索引模型根据 Issue 内容生成。

描述

music21 version
9.5.0

Operating System(s) checked
OS agnostic
(I am on Ubuntu 24.04, though, but that should be irrelevant.)

Problem summary
music21.midi.MidiEvent.getBytes() confuses SEQUENCER_SPECIFIC_META_EVENT (0x7F) with POLY_MODE_ON (also 0x7F) and will therefore cause a TypeError if the former does not care about channels (i.e self.channel == None).

Steps to reproduce

from music21 import midi

ev = midi.MidiEvent(type=midi.MetaEvents.SEQUENCER_SPECIFIC_META_EVENT, channel=None)
print(ev)
b = ev.getBytes()

FYI: Actually I encountered this issue not by instantiating a MidiEvent manually but by writing a MidiFile which comes with some SEQUENCER_SPECIFIC_META_EVENT's.

mf = midi.MidiFile()
mf.open(inputfile)
mf.read()
mf.close()

# write the contents back immediately without touching anything

mf.open(outputfile, 'wb')
mf.write()
mf.close()

Expected vs. actual behavior
EXPECTED BEHAVIOUR:
music21.midi.MidiEvent.getBytes() should process all MIDI events correctly without triggering any exception.

ACTUAL BEHAVIOUR:

File "/foo/bar/lib/python3.12/site-packages/music21/midi/__init__.py", line 982, in getBytes
channelMode = bytes([0xB0 + self.channel - 1])
                        ~~~~~^~~~~~~~~~~~~~
TypeError: unsupported operand type(s) for +: 'int' and 'NoneType'

More information
The current code of music21.midi.MidiEvent.getBytes() checks the event type essentially by looking at its value only:

def getBytes(self):
    if self.type in ChannelVoiceMessages:
        ...
    elif self.type in ChannelModeMessages:
        channelMode = bytes([0xB0 + self.channel - 1])
        ...
    elif self.type in SysExEvents:
        ...
    elif self.type in MetaEvents:
        ...
    else:
        ...

However:

class ChannelModeMessages(_ContainsEnum):
    ...
    POLY_MODE_ON = 0x7F
    ...
class MetaEvents(_ContainsEnum):
    ...
    SEQUENCER_SPECIFIC_META_EVENT = 0x7F
    ...

Apparently, getBytes() will mistake a SEQUENCER_SPECIFIC_META_EVENT as a POLY_MODE_ON, and will then fail immediately if self.channel is null.

Therefore, it is probably more appropriate to also check the event class, in addition to the event type value:

if isinstance(self.type, ChannelVoiceMessages) and self.type in ChannelVoiceMessages:
    ...
elif isinstance(self.type, ChannelModeMessages) and self.type in ChannelModeMessages:
    ...
elif isinstance(self.type, SysExEvents) and self.type in SysExEvents:
    ...
elif isinstance(self.type, MetaEvents) and self.type in MetaEvents:
    ...
主要语言
Python
星标
2.6k
派生
459
平均合并
1 天 7 小时
30 天内合并 PR
40

环境准备

在 Codespaces 中打开

在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

cuthbertLab/music21 的其他 Issue

查看 cuthbertLab/music21 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。