微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决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 举报,一经查实,本站将立刻删除。