首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么干净的Midi文件播放方式不同?(与米多)

为什么干净的Midi文件播放方式不同?(与米多)
EN

Stack Overflow用户
提问于 2020-01-03 00:38:16
回答 1查看 932关注 0票数 1

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

我把我的代码分成两部分。第一项进行所有筛选并构造一个大的列表列表,其中每个子列表中的第一项是消息本身,而每个子列表中的第二项是累积时间(这是按累积时间排序的)。代码的第二部分调整此列表中每个项的增量时间,然后将列表中的所有消息按顺序(带有更正的增量时间)追加到从头创建的轨道上。然后,它使用玩偶游戏来播放这个曲目。

我似乎遇到的主要问题是时机/节奏。重建的曲目要么弹得太快要么太慢。在某些文件(例如波希米亚Rhapsody文件)中,工具部分似乎也是分离的和混乱的。

这是解构和列出建筑代码:

代码语言:javascript
复制
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()

这是重构代码:

代码语言:javascript
复制
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文件,它们听起来仍然错误(较慢)时播放。

代码语言:javascript
复制
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()
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-01-03 01:30:49

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

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59571840

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档