我正在制作一个程序,该程序应该能够从某个 midi 文件中提取音符、休止符和和弦,并将音符和和弦的相应音高(以 midi 音调数字 - 它们从 0-127 变为 csv 文件)供以后使用。
对于这个项目,我使用的是 Python 库“Music21”。
from music21 import *
import pandas as pd
#SETUP
path = r"Pirates_TheCarib_midi\1225766-Pirates_of_The_Caribbean_Medley.mid"
#create a function for taking parsing and extracting the notes
def extract_notes(path):
stm = converter.parse(path)
treble = stm[0] #access the first part (if there is only one part)
bass = stm[1]
#note extraction
notes_treble = []
notes_bass = []
for thisNote in treble.getElementsByClass("Note"):
indiv_note = [thisNote.name, thisNote.pitch.midi, thisNote.offset]
notes_treble.append(indiv_note) # print's the note and the note's
offset
for thisNote in bass.getElementsByClass("Note"):
indiv_note = [thisNote.name, thisNote.pitch.midi, thisNote.offset]
notes_bass.append(indiv_note) #add the notes to the bass
return notes_treble, notes_bass
#write to csv
def to_csv(notes_array):
df = pd.DataFrame(notes_array, index=None, columns=None)
df.to_csv("attempt1_v1.csv")
#using the functions
notes_array = extract_notes(path)
#to_csv(notes_array)
#DEBUGGING
stm = converter.parse(path)
print(stm.parts)
这是我用作测试的分数的链接。 https://musescore.com/user/1699036/scores/1225766
当我运行 extract_notes 函数时,它返回两个空数组和行:
print(stm.parts)
它返回
<music21.stream.iterator.StreamIterator for Score:0x1b25dead550 @:0>
我对它为什么这样做感到困惑。这首曲子应该有两个部分,高音和低音。如何将每个音符、和弦和休止符放入一个数组中,以便将其放入 csv 文件中?