Python 无法写入';休息';midi文件中的对象

Python 无法写入';休息';midi文件中的对象,python,deep-learning,music21,Python,Deep Learning,Music21,我有一个列表,其中包含几个“音符””、“和弦””和“rest”对象,并试图将它们写入midi文件。我正在尝试以下代码 midi_stream = stream.Stream(output_notes) midi_stream.write('midi', fp='test_output.mid') 在阅读创建的midi文件时,我发现midi文件中没有附加“rest”对象 输出注释变量的内容为: <music21.note.Rest rest> <music21.chord.C

我有一个列表,其中包含几个“
音符”
”、“
和弦”
”和“
rest
”对象,并试图将它们写入midi文件。我正在尝试以下代码

midi_stream = stream.Stream(output_notes)
midi_stream.write('midi', fp='test_output.mid')
在阅读创建的midi文件时,我发现midi文件中没有附加“
rest
”对象

输出注释变量的内容为:

 <music21.note.Rest rest> <music21.chord.Chord A1 E2>
 <music21.chord.Chord A1 E2> <music21.chord.Chord A1 E2>
 <music21.note.Rest rest> <music21.chord.Chord A1 E2>
 <music21.note.Rest rest> <music21.note.Rest rest>
 <music21.chord.Chord A1 E2> <music21.chord.Chord A1 E2>
 <music21.note.Rest rest> <music21.note.Rest rest>
 <music21.note.Rest rest> <music21.note.Note A> <music21.note.Rest rest>
 <music21.note.Rest rest> <music21.note.Note A> <music21.note.Rest rest>
 <music21.note.Rest rest> <music21.note.Rest rest> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Rest rest> <music21.note.Rest rest>
 <music21.note.Rest rest> <music21.chord.Chord A4 C#5 F5>
 <music21.note.Rest rest> <music21.note.Rest rest>
 <music21.chord.Chord A4 C#5 F5> <music21.note.Note B>
 <music21.note.Rest rest> <music21.note.Rest rest>
 <music21.note.Rest rest> <music21.note.Rest rest>
 <music21.chord.Chord A4 C#5 F5> <music21.note.Rest rest>
 <music21.chord.Chord A4 C#5 F5> <music21.note.Rest rest>
 <music21.chord.Chord A4 C#5 F5> <music21.note.Rest rest>
 <music21.note.Rest rest> <music21.chord.Chord G4 B4 D5>
 <music21.chord.Chord F#4 B4 D5> <music21.chord.Chord F#4 B4 D5>
 <music21.chord.Chord F#4 B4 D5> <music21.chord.Chord F#4 B4 D5>
 <music21.note.Note C#> <music21.note.Rest rest> <music21.note.Rest rest>
 <music21.chord.Chord F#4 B4 D5> <music21.note.Rest rest>
 <music21.note.Rest rest> <music21.chord.Chord E3 A3>
 <music21.note.Rest rest> <music21.note.Note C> <music21.note.Rest rest>
 <music21.note.Rest rest> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.chord.Chord C3 G3>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.chord.Chord C3 G3> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Rest rest> <music21.note.Note G>
 <music21.chord.Chord C3 G3> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.chord.Chord C3 G3> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.chord.Chord C3 G3>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.chord.Chord C3 G3> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.chord.Chord C3 G3>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.chord.Chord C3 G3> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.chord.Chord C3 G3>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.chord.Chord C3 G3>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G>]

]
创建的midi文件中的内容是:

[<music21.chord.Chord A1 E2> <music21.chord.Chord A1 E2>
 <music21.chord.Chord A1 E2> <music21.chord.Chord A1 E2>
 <music21.chord.Chord A1 E2> <music21.note.Note A> <music21.note.Note G>
 <music21.note.Note G> <music21.chord.Chord A4 C#5 F5>
 <music21.note.Note B> <music21.chord.Chord A4 C#5 F5>
 <music21.chord.Chord A4 C#5 F5> <music21.note.Note G>
 <music21.chord.Chord B4 D5> <music21.note.Note F#>
 <music21.chord.Chord B4 D5> <music21.note.Note F#>
 <music21.chord.Chord B4 D5> <music21.note.Note C#>
 <music21.chord.Chord F#4 B4 D5> <music21.chord.Chord E3 A3>
 <music21.note.Note C> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.chord.Chord C3 G3> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.chord.Chord C3 G3>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.chord.Chord C3 G3>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.chord.Chord C3 G3> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.chord.Chord C3 G3>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.chord.Chord C3 G3>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.chord.Chord C3 G3> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.chord.Chord C3 G3> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.chord.Chord C3 G3> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.chord.Chord C3 G3> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G>]
[
]

您所做的是获取
音乐21
流,将其转换为MIDI,然后再将其转换回
音乐21
流,您会惊讶于往返过程没有提供与原始输入相同的输出

但是如果你将Excel电子表格转换成
.csv
,然后再转换回来,我想你不会感到惊讶

假设
music21
和MIDI是两种等效的符号。事实并非如此<代码>音乐21面向分数分析。它知道音符、休止符、小节、密钥签名和谱号(除此之外还有很多)

另一方面,MIDI是一种面向合成器的播放指令集。想想你将在一些synth程序中看到的钢琴滚动符号,例如:

黑色水平条纹表示播放的音符。但剩下的在哪里呢?没有任何区别,因为对于整首曲子来说是沉默的音符和酒吧中被沉默打断的音符之间没有区别。这就是为什么我在评论中说MIDI实际上没有任何休息。休息只是为了告诉玩家在酒吧里不可以演奏音符的地方。但MIDI对条一无所知(如果您在MIDI文件中看到条,它们本质上就是注释),因此也不需要休息。当
music21
从MIDI导入时,它的输入效果最好,但是猜测没有音符的情况下应该休息到哪里是非常棘手的,如果
music21
的工作没有你想要的那么好,你不必感到惊讶

MIDI是一种不太丰富的音乐表达媒介,因为它用对目标乐器的直接原始控制来代替对人类有意义的符号。如果您只需在消息中的便笺编号上添加2(或1,取决于上下文),则不需要双尖符号。这就是为什么从
music21
转换到MIDI再转换回来有点像把电子表格写成
.csv
然后再读回。很多信息丢失了

事实上,并没有一个独立于应用程序的音乐记谱标准,当然也不是MIDI。MusicXML试图做到这一点,但它并没有涵盖所有内容,如果您使用它在(比如,
musci21
)Sibelius和Dorico之间进行转换,就会发现这一点。事实上,Dorico会忽略MusicXML中的一些空格符号,因为它认为自己可以做得更好


MIDI文件中没有REST的真正测试是播放MIDI文件,而不是将其转换为其他符号,并将其视为MIDI文件中内容的代理。你能听到文件里没有休息的声音吗?播放的输出声音是否与原始声音不同(忽略乐器、音色、节奏等非音符属性)

如果您写入文件的内容与预期不符,我认为您应该向我们展示您写入文件的内容。您要求我们猜测数据中的内容,然后告诉您问题可能在哪里。我特别想知道您的“rest消息”是什么样子的,因为我认为MIDI中没有rest这样的东西。MIDI表示一种休息,即没有开/关音符消息对。@BoarGules,谢谢您的回复。我已经更新了问题,请看一看。你上面提供的两个区块不匹配,这使得它们非常无用。但无论如何它们都没用。答案就在你写“休息”的地方。您需要显示执行此操作的代码@BoarGules给出了可能的答案,但代码所做的是关键。您是否收到任何错误消息?您是否尝试过在调试器中运行代码,并观察它写出“rest”时的行为?如果你想在这里提出一个对你有帮助的问题,请看一下如何创建一个。