0

我编写了一个接收 MIDI 文件的程序,然后使用 Mido,它通过删除特定类型的元数据、重复消息等来清理数据。它还计算了累积时间(因为每个 midi 消息中的时间都是 delta时间)。然后使用它创建一个新的 mido 文件(从头开始),我将所有这些消息附加到一个轨道中(因此基本上轨道被合并)并按累积时间对它们进行排序。然后相应地调整增量时间(请记住,每个新的 midi 轨道从累积时间 0 开始)。我意识到这可能看起来毫无意义(因为我试图构建更干净的同一首歌曲),但目的是拥有更好的数据然后做其他事情。

我已将我的代码分成两部分。第一个执行所有过滤并构造一个大列表,其中每个子列表中的第一项是消息本身,每个子列表中的第二项是累积时间(如上所述,这是按累积时间排序的)。代码的第二部分调整此列表中每个项目的增量时间,然后将列表中的所有消息按顺序(具有更正的增量时间)附加到从头开始创建的轨道上。然后它使用 pygame 播放该曲目。

我似乎遇到的主要问题是时间/节奏。重建的曲目似乎播放得太快或太慢。在某些文件(例如 Bohemian Rhapsody 文件)的情况下,乐器部分也似乎是分离和混乱的。

这是解构和列表构建代码:


import mido
import pygame

all_mid = ['major-scale.mid']


# check is midi file is type 2 (and removes if so) - this is unlikely but can happen on old sites
def remove_type_2(midi):
    return True if midi.type == 2 else False


# removes unnecessary meta data types
def filter_meta_type(msg):
    accept = ["set_tempo", "time_signature", "key_signature"]
    return True if msg.type in accept else False


# removes tempo duplicates and only keeps the last tempo stated for a particular cumulative time
def remove_extra_tempo(msg, msgwithtempos, current_time):
    if not msgwithtempos:  # if the list is empty
        msgwithtempos.append([msg, current_time])
    else:
        for i in range(len(msgwithtempos)):
            msgwithtempo = msgwithtempos[i]
            if msgwithtempo[1] == current_time:  # this checks duplicates
                msgwithtempos.remove(msgwithtempo)
        msgwithtempos.append([msg, current_time])
    return msgwithtempos


def do_shit(mid, all_messages):  # for each track (then message) do the following
    msgwithtempos = []
    for i, track in enumerate(mid.tracks):
        current_time = 0
        print(f"Track {i}: {track.name}")
        for msg in track:
            current_time += msg.time
            if msg.type == "sysex data":
                pass
            elif msg.is_meta:
                if filter_meta_type(msg):
                    if msg.type == "set_tempo":
                        msgwithtempos = remove_extra_tempo(msg, msgwithtempos, current_time)
                    else:
                        all_messages.append([msg, current_time])
            else:
                all_messages.append([msg, current_time])
    return all_messages, msgwithtempos


def main():  # for each midi file do the following
    all_lists = []
    for i in range(0, len(all_mid)):
        all_messages = []
        mid = mido.MidiFile(all_mid[i])
        if not remove_type_2(mid):
            all_messages, msgwithtempos = do_shit(mid, all_messages)
            final_messages = all_messages + msgwithtempos
            final_messages = sorted(final_messages, key=lambda x: x[1])
            all_lists.append(final_messages)
    print(all_lists)
    return all_lists


if __name__ == '__main__':
    main()

这是重构代码:

import mido
import pygame
import regulate_tracks


def play_with_pygame(song):
    pygame.init()
    pygame.mixer.music.load(song)
    length = pygame.time.get_ticks()
    pygame.mixer.music.play()
    while pygame.mixer.music.get_busy():
        pygame.time.wait(length)


def printmessages(mid):
    for i, track in enumerate(mid.tracks):
        print(f"Track {i}: {track.name}")
        for msg in track:
            print(msg)


def main():
    # get the list of midi files from regulate_tracks
    output = regulate_tracks.main()
    list1 = output[0]

    # create a blank midi file and add a track to it
    mid = mido.MidiFile()
    track = mido.MidiTrack()
    mid.tracks.append(track)

    for i in range(len(list1)):
        message = list1[i][0]
        print(message.type)
        if i == 0:
            message.time = 0
        else:
            message.time = list1[i][1] - list1[i - 1][1]
        print(message)
        track.append(message)

    mid.save('new_song.mid')

    printmessages(mid)

    play_with_pygame('new_song.mid')


if __name__ == '__main__':
    main()

示例文件:

波西米亚狂想曲:https ://bitmidi.com/queen-bohemian-rhapsody-mid

河流在你心中流淌:http ://midicollection.net/songs/index.php?id=13

谢谢

编辑:

这似乎是空白 MIDI 创建和附加的问题,因为我创建了此代码,该代码手动复制了短 MIDI 文件中的消息,并且在播放时它们仍然听起来错误(较慢)。


import mido
import pygame


def play_with_pygame(song):
    pygame.init()
    pygame.mixer.music.load(song)
    length = pygame.time.get_ticks()
    pygame.mixer.music.play()
    while pygame.mixer.music.get_busy():
        pygame.time.wait(length)


def main():
    mid = mido.MidiFile()
    track = mido.MidiTrack()
    mid.tracks.append(track)
    track.append(mido.MetaMessage('set_tempo', tempo=500000, time=3840))
    track.append(mido.MetaMessage('end_of_track', time=0))
    track = mido.MidiTrack()
    mid.tracks.append(track)
    track.append(mido.Message('note_on', channel=0, note=60, velocity=100, time=0))
    track.append(mido.Message('note_on', channel=0, note=62, velocity=100, time=960))
    track.append(mido.Message('note_on', channel=0, note=64, velocity=100, time=960))
    track.append(mido.Message('note_on', channel=0, note=65, velocity=100, time=960))
    track.append(mido.Message('program_change', channel=0, program=123, time=960))
    track.append(mido.Message('note_on', channel=0, note=67, velocity=100, time=0))
    track.append(mido.Message('note_on', channel=0, note=69, velocity=100, time=960))
    track.append(mido.Message('note_on', channel=0, note=71, velocity=100, time=960))
    track.append(mido.Message('note_on', channel=0, note=72, velocity=100, time=960))
    track.append(mido.Message('note_off', channel=0, note=60, velocity=100, time=2880))
    track.append(mido.Message('note_off', channel=0, note=62, velocity=100, time=960))
    track.append(mido.Message('note_off', channel=0, note=64, velocity=100, time=960))
    track.append(mido.Message('note_off', channel=0, note=65, velocity=100, time=960))
    track.append(mido.Message('note_off', channel=0, note=67, velocity=100, time=960))
    track.append(mido.Message('note_off', channel=0, note=69, velocity=100, time=960))
    track.append(mido.Message('note_off', channel=0, note=71, velocity=100, time=960))
    track.append(mido.Message('note_off', channel=0, note=72, velocity=100, time=960))
    track.append(mido.MetaMessage('end_of_track', time=0))

    mid.save('new_song.mid')

    play_with_pygame('new_song.mid')


if __name__ == '__main__':
    main()
4

1 回答 1

2

您需要保存ticks_per_beat. 只需使用 获取ticks_per_beat原始文件的ticksperbeat = mid.ticks_per_beat,然后将新文件设置为mid.ticks_per_beat = ticksperbeat

于 2020-01-03T01:30:49.560 回答