Python中的MIDI消息令人困惑吗?

问题描述

我目前正在处理MIDI数据集maestro-v2.0.0(see)。

我使用from mido import MidiFile在python中加载文件
然后,我处理数据集中的文件(例如,在2009/MIDI-Unprocessed_07_R1_2009_04-05_ORIG_MID--AUDIO_07_R1_2009_07_R1_2009_04_WAV.midi上)。
如果我按时间对事件(速度不是零)消息进行排序,那么我会得到(对于前10条消息):

message note_on channel=0 note=62 velocity=53 time=0,message note_on channel=0 note=66 velocity=58 time=0,message note_on channel=0 note=62 velocity=72 time=0,message note_on channel=0 note=71 velocity=60 time=0,message note_on channel=0 note=78 velocity=61 time=0,message note_on channel=0 note=54 velocity=55 time=0,message note_on channel=0 note=72 velocity=53 time=0,message note_on channel=0 note=71 velocity=61 time=0,message note_on channel=0 note=73 velocity=67 time=0,message note_on channel=0 note=66 velocity=13 time=0
...

如您所见,一开始有多个音符。
同时,我发现这些note_on事件与velocity=0的音高相同。我在某处读到note_onvelocity=0对应于事件关闭令牌,即便笺已释放。
我只是想知道发生了什么,为什么我的数据看起来如此“丑陋”? 当我说丑时,我的意思是为什么要包括未播放的midi文件音符。如果在时间note_ont发生velocity>0事件,但同时在同一时间有note_on具有相同音高的情况下,则不会播放音符velocity=0。这些不必要的消息可以简单地省略,我们不必说没有演奏音符。这就是Midi文件中发生的事情(就我的代码而言)。我希望我的意思很清楚。

我的代码可以在这里查看

import os
import pandas as pd
import json
import numpy as  np
from mido import MidiFile

def openJSON():
    path = "C:\\Users\\Bunlong\\PycharmProjects\\midiPreprocessing\\midi\\maestro-v2.0.0"
    path = os.path.realpath(path)
    os.chdir(path)
    filename = 'maestro-v2.0.0.json'
    with open(filename,'r') as json_file:
        data = json.load(json_file)
    return data


def openMidi(folderNr,fileNr):
    data = openJSON()
    folder = data[folderNr]['year']
    name = data[fileNr]['midi_filename']
    mid = MidiFile(name)
    return mid

def getRepresentation(mid):
    MAX_NOTE = 127
    noteOn_sequence = []
    noteOff_sequence = []
    time = []
    for i,track in enumerate(mid.tracks):
        if i == 0:
            continue
        print('Track {}: {}'.format(i,track.name))
        for msg in track:
            a = msg
            print(msg)
            if(a.type != 'control_change'):
                if (a.type == 'note_on'):
                    if (a.velocity == 0):
                        noteOff_sequence.append(a)
                    else:
                        noteOn_sequence.append(a)
                    time.append(a.time)

    #Sorting noteOff and noteOn sequence with respect to the timestamp
    indexPermutatationOn = sorted(range(len(noteOn_sequence)),key=lambda k: noteOn_sequence[k].time)
    indexPermutatationOff = sorted(range(len(noteOff_sequence)),key=lambda k: noteOff_sequence[k].time)

    sortedOn = []
    for index in indexPermutatationOn:
        sortedOn.append(noteOn_sequence[index])

    sortedOff = []
    for index in indexPermutatationOff:
        sortedOff.append(noteOff_sequence[index])


#call of methods
midiFile = openMidi(0,5)
getRepresentation(midiFile)

我的目标是按照以下方式表示数据。如果对于给定的时间戳 t ,音高 p_1,...,p_k 的演奏速度为 v_1,...,v_k ,那么我定义通过说第 p_i 项是音高播放的持续时间来表示矢量,而第 2p_i 项的速度是速度,最后一个项保留该时间 t

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...