Hacktoberfest 2026:メンテナが10月に向けて印を付けた、オープンで初心者向けの issue。 Hacktoberfest の issue を見る

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

オープン
#1,772 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
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時間
マージ済み PR(30日)
40

環境構築

Codespaces で開く

このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

cuthbertLab/music21 のほかの issue

cuthbertLab/music21 の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。