midi 文件读取与写入(python mido)

2023-12-01 04:04
文章标签 python 读取 写入 midi mido

本文主要是介绍midi 文件读取与写入(python mido),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

midi 文件读取与写入

  • 地址
  • 第一种方法
  • 第二种方法
  • 读取法

地址

https://codeleading.com/article/90425033665/

第一种方法

from mido import Message, MidiFile, MidiTrack# 把音名、唱名等换成 MIDO相对应的数字
# 已完成
def num(yin):if yin == "A0" or yin == "a0":  # 最低音 lareturn 21if yin == "B0" or yin == "b0":  # 最低的sireturn 23if yin == "C1" or yin == "c1":  # 低三个八度 doreturn 24if yin == "D1" or yin == "d1":  # rereturn 26if yin == "E1" or yin == "e1":  # mireturn 28if yin == "F1" or yin == "f1":  # fareturn 29if yin == "G1" or yin == "g1":  # so 或者说 solreturn 31if yin == "A1" or yin == "a1":  # lareturn 33if yin == "B1" or yin == "b1":  # sireturn 35if yin == "C2" or yin == "c2":  # 低两个八度 doreturn 36if yin == "D2" or yin == "d2":  # rereturn 38if yin == "E2" or yin == "e2":  # mireturn 40if yin == "F2" or yin == "f2":  # fareturn 41if yin == "G2" or yin == "g2":  # so 或者说 solreturn 43if yin == "A2" or yin == "a2":  # lareturn 45if yin == "B2" or yin == "b2":  # sireturn 47if yin == "C3" or yin == "c3" or yin == ".do" or yin == ".1" or yin == ".Do" or yin == ".DO":  # 低八度   doreturn 48if yin == "D3" or yin == "d3" or yin == ".re" or yin == ".2" or yin == ".Re" or yin == ".RE":  # rereturn 50if yin == "E3" or yin == "e3" or yin == ".mi" or yin == ".3" or yin == ".Mi" or yin == ".MI":  # mireturn 52if yin == "F3" or yin == "f3" or yin == ".fa" or yin == ".4" or yin == ".Fa" or yin == ".FA":  # fareturn 53if yin == "G3" or yin == "g3" or yin == ".so" or yin == ".sol" or yin == ".5" or yin == ".So" or yin == ".Sol" or yin == ".SO" or yin == ".SOL":  # so 或者说 solreturn 55if yin == "A3" or yin == "a3" or yin == ".la" or yin == ".6" or yin == ".La" or yin == ".LA":  # lareturn 57if yin == "B3" or yin == "b3" or yin == ".si" or yin == ".7" or yin == ".Si" or yin == ".SI":  # sireturn 59# 中音区if yin == "C4" or yin == "c4" or yin == "C" or yin == "c" or yin == "1" or yin == 1 or yin == "do" or yin == "Do" or yin == "DO":  # 中音doreturn 60if yin == "D4" or yin == "d4" or yin == "D" or yin == "d" or yin == "2" or yin == 2 or yin == "re" or yin == "Re" or yin == "RE":  # 中音rereturn 62if yin == "E4" or yin == "e4" or yin == "E" or yin == "e" or yin == "3" or yin == 3 or yin == "mi" or yin == "Mi" or yin == "MI":  # 中音mireturn 64if yin == "F4" or yin == "f4" or yin == "F" or yin == "f" or yin == "4" or yin == 4 or yin == "fa" or yin == "Fa" or yin == "FA":  # 中音fareturn 65if yin == "G4" or yin == "g4" or yin == "G" or yin == "g" or yin == "5" or yin == 5 or yin == "so" or yin == "sol" or yin == "So" or yin == "Sol" or yin == "SO" or yin == "SOL":  # 中音so  或者说 solreturn 67if yin == "A4" or yin == "a4" or yin == "A" or yin == "a" or yin == "6" or yin == 6 or yin == "la" or yin == "La" or yin == "LA":  # 中音lareturn 69if yin == "B4" or yin == "b4" or yin == "B" or yin == "b" or yin == "7" or yin == 7 or yin == "si" or yin == "Si" or yin == "SI":  # 中音sireturn 71# 高音区if yin == "C5" or yin == "c5" or yin == "1." or yin == "do." or yin == "Do." or yin == "DO.":  # doreturn 72if yin == "D5" or yin == "d5" or yin == "2." or yin == "re." or yin == "Re." or yin == "RE.":  # rereturn 74if yin == "E5" or yin == "e5" or yin == "3." or yin == "mi." or yin == "Mi." or yin == "MI.":  # mireturn 76if yin == "F5" or yin == "f5" or yin == "4." or yin == "fa." or yin == "Fa." or yin == "FA.":  # fareturn 77if yin == "G5" or yin == "g5" or yin == "5." or yin == "so." or yin == "So." or yin == "SO." or yin == "sol." or yin == "Sol." or yin == "SOL.":  # so 或者说是 solreturn 79if yin == "A5" or yin == "a5" or yin == "6." or yin == "la." or yin == "La." or yin == "LA.":  # lareturn 81if yin == "B5" or yin == "b5" or yin == "7." or yin == "si." or yin == "Si." or yin == "SI.":  # sireturn 83# 从此,退出常用区# 高两个八度if yin == "C6" or yin == "c6":  # doreturn 84if yin == "D6" or yin == "d6":  # rereturn 86if yin == "E6" or yin == "e6":  # mireturn 88if yin == "F6" or yin == "f6":  # fareturn 89if yin == "G6" or yin == "g6":  # soreturn 91if yin == "A6" or yin == "a6":  # lareturn 93if yin == "B6" or yin == "b6":  # sireturn 95# 高三个八度if yin == "C7" or yin == "c7":  # doreturn 96if yin == "D7" or yin == "d7":  # rereturn 98if yin == "E7" or yin == "e7":  # mireturn 100if yin == "F7" or yin == "f7":  # fareturn 101if yin == "G7" or yin == "g7":  # soreturn 103if yin == "A7" or yin == "a7":  # lareturn 105if yin == "B7" or yin == "b7":  # sireturn 107# 高四个八度# 最高音doif yin == "C8" or yin == "c8" or yin == "highestdo" or yin == "hdo" or yin == "h1" or yin == "highest1":  # doreturn 108if yin == '0':return 1return Nonedef make_track(_score, program=0):track = MidiTrack()no = programmeta_time = 60 * 60 * 10 / 75track.append(Message('program_change', channel=0, program=no, time=0))track.append(Message('note_on', note=0, velocity=64, time=32, channel=0))track.append(Message('note_off', note=0, velocity=127, time=32, channel=0))last = 0for i in range(0, len(_score)):ch = _score[i]if ch[0] == '$':no = int(ch[1:])continueif ch == '_':track.append(Message('note_off', note=last, velocity=127, time=round(1 * meta_time), channel=0))continueif ch == '__':track.append(Message('note_off', note=last, velocity=127, time=round(2 * meta_time), channel=0))continueif num(ch):track.append(Message('program_change', channel=0, program=no, time=0))track.append(Message('note_on', note=num(ch), velocity=64, time=0, channel=0))last = num(ch)try:if num(score[i + 1]):track.append(Message('note_off', note=num(ch), velocity=127, time=round(0.5 * meta_time), channel=0))except Exception:passreturn trackscore = [# 乐谱'$3', '1.', '_', '2.', '_', '3.', '2.', '1.', '7', '_', '6', '5', '6', '__', '0','$8', '1.', '2.', '3.', '2.', '1.', '7', '_', '5', '4.', '2.', '3.', '__', '0''$11', '1.', '2.', '3.', '2.', '1.', '7','_', '5', '2.', '2.', '_', '1.', '7', '6', '__', '0','$12', '6', '7', '1.', '7', '1.', '2.', '_', '1.', '7', '1.', '__', '0','$44', '3.', '_', '3.', '3.', '_', '2.', '_', '5.', '_', '2.', '2.', '__', '0','$25', '3.', '3.', '4.', '3.', '2', '_', '7', '1.', '2.', '3.', '_', '3.', '3.', '_', '2.', '_', '5.', '_', '2.','2.', '__', '0','$66', '2.', '1.', '7', '5', '__', '0','$78', '3', '3', '1.', '_', '1.', '1.', '7', '6', '5', '__','$53', '3', '4', '5', '4', '__', '6', '_', '5', '4', '3', '__','$18', '3', '2', '1', '__', '1', '_', '6', '_', '5', '_', '4', '_', '3', '_', '5', '_', '2', '__','$16', '3', '3', '1.', '_', '1.', '1.', '7', '6', '5', '__','$22', '3', '4', '5', '4', '__', '6', '_', '5', '4', '3', '__','$88', '3', '2', '1', '__', '1', '_', '6', '_', '5', '__','$72', '3', '4', '5', '4', '_', '5', '_', '3', '3', '6', '__','$23', '6', '_', '6', '6', '7', '1.', '5', '_', '5', '3', '4', '5', '_', '0', '5', '4', '3', '1', '__','$19', '1', '_', '4', '3', '4', '_', '0', '4', '5', '4', '5', '__','$1', '5', '6', '_', '0', '6', '7', '1.', '7', '__','$74', '1.', '_', '2.', '_', '3.', '2.', '1.', '7', '_', '6', '5', '6', '__', '0','$114', '1.', '2.', '3.', '2.', '1.', '7', '_', '5', '4.', '2.', '3.', '__', '0''$2', '1.', '2.', '3.', '2.', '1.','7', '_', '5', '2.', '2.', '_', '1.', '7', '6', '__', '0','$5', '6', '7', '1.', '7', '1.', '2.', '_', '1.', '7', '1.', '_','$60', '1.', '_', '2.', '_', '3.', '2.', '1.', '7', '_', '6', '5', '6', '__', '0','$61', '1.', '2.', '3.', '2.', '1.', '7', '_', '5', '4.', '2.', '3.', '__', '0''$77', '1.', '2.', '3.', '2.', '1.','7', '_', '5', '2.', '2.', '_', '1.', '7', '6', '__', '0','$69', '6', '7', '1.', '7', '1.', '2.', '_', '1.', '7', '1.', '__', '0',]
score2 = [# 鼓声'$116', '.1', '.1', '_', '$118', '.2', '_', '$116', '.1', '.1', '_', '$118', '.2', '_', '$116', '.1', '.1', '_','$118', '.2', '_','$116', '.1', '.1', '_', '$118', '.2', '_', '$116', '.1', '.1', '_', '$118', '.2', '_', '$116', '.1', '.1', '_','$118', '.2', '_','$116', '.1', '.1', '_', '$118', '.2', '_', '$116', '.1', '.1', '_', '$118', '.2', '_', '$116', '.1', '.1', '_','$118', '.2', '_','$116', '.1', '.1', '_', '$118', '.2', '_', '$116', '.1', '.1', '_', '$118', '.2', '_', '$116', '.1', '.1', '_','$118', '.2', '_','$116', '.1', '.1', '_', '$118', '.2', '_', '$116', '.1', '.1', '_', '$118', '.2', '_', '$116', '.1', '.1', '_','$118', '.2', '_','$116', '.1', '.1', '_', '$118', '.2', '_', '$116', '.1', '.1', '_', '$118', '.2', '_', '$116', '.1', '.1', '_','$118', '.2', '_','$116', '.1', '.1', '_', '$118', '.2', '_', '$116', '.1', '.1', '_', '$118', '.2', '_', '$116', '.1', '.1', '_','$118', '.2', '_','$116', '.1', '.1', '_', '$118', '.2', '_', '$116', '.1', '.1', '_', '$118', '.2', '_', '$116', '.1', '.1', '_','$118', '.2', '_','$116', '.1', '.1', '_', '$118', '.2', '_', '$116', '.1', '.1', '_', '$118', '.2', '_', '$116', '.1', '.1', '_','$118', '.2', '_','$116', '.1', '.1', '_', '$118', '.2', '_', '$116', '.1', '.1', '_', '$118', '.2', '_', '$116', '.1', '.1', '_','$118', '.2', '_','$116', '.1', '.1', '_', '$118', '.2', '_', '$116', '.1', '.1', '_', '$118', '.2', '_', '$116', '.1', '.1', '_','$118', '.2', '_','$116', '.1', '.1', '_', '$118', '.2', '_', '$116', '.1', '.1', '_', '$118', '.2', '_', '$116', '.1', '.1', '_','$118', '.2', '_','$116', '.1', '.1', '_', '$118', '.2', '_', '$116', '.1', '.1', '_', '$118', '.2', '_', '$116', '.1', '.1', '_','$118', '.2', '_','$116', '.1', '.1', '_', '$118', '.2', '_', '$116', '.1', '.1', '_', '$118', '.2', '_', '$116', '.1', '.1', '_','$118', '.2', '_','$116', '.1', '.1', '_', '$118', '.2', '_', '$116', '.1', '.1', '_', '$118', '.2', '_', '$116', '.1', '.1', '_','$118', '.2', '_','$116', '.1', '.1', '_', '$118', '.2', '_', '$116', '.1', '.1', '_', '$118', '.2', '_', '$116', '.1', '.1', '_','$118', '.2', '_','$116', '.1', '.1', '_', '$118', '.2', '_', '$116', '.1', '.1', '_', '$118', '.2', '_', '$116', '.1', '.1', '_','$118', '.2', '_','$116', '.1', '.1', '_', '$118', '.2', '_', '$116', '.1', '.1', '_', '$118', '.2', '_', '$116', '.1', '.1', '_','$118', '.2', '_','$116', '.1', '.1', '_', '$118', '.2', '_', '$116', '.1', '.1', '_', '$118', '.2', '_', '$116', '.1', '.1', '_','$118', '.2', '_','$116', '.1', '.1', '_', '$118', '.2', '_', '$116', '.1', '.1', '_', '$118', '.2', '_', '$116', '.1', '.1', '_','$118', '.2', '_','$116', '.1', '.1', '_', '$118', '.2', '_', '$116', '.1', '.1', '_', '$118', '.2', '_', '$116', '.1', '.1', '_','$118', '.2', '_','$116', '.1', '.1', '_', '$118', '.2', '_', '$116', '.1', '.1', '_', '$118', '.2', '_', '$116', '.1', '.1', '_','$118', '.2', '_','$116', '.1', '.1', '_', '$118', '.2', '_', '$116', '.1', '.1', '_', '$118', '.2', '_', '$116', '.1', '.1', '_','$118', '.2', '_','$116', '.1', '.1', '_', '$118', '.2', '_', '$116', '.1', '.1', '_', '$118', '.2', '_', '$116', '.1', '.1', '_','$118', '.2', '_',
]
score3 = [# 其他旁白音效"$102", ".1", '__', '__', ".2", '__', '__', ".1", '__', '__', "$101", ".1", '__', '__', ".2", '__', '__',"$102", ".4", '__', '__', ".5", '__', '__', ".1", '__', '__', "$101", ".1", '__', '__', ".2", '__', '__',"$102", ".2", '__', '__', ".6", '__', '__', ".1", '__', '__', "$101", ".1", '__', '__', ".2", '__', '__',"$102", ".3", '__', '__', ".3", '__', '__', ".1", '__', '__', "$101", ".1", '__', '__', ".2", '__', '__',"$102", ".2", '__', '__', ".4", '__', '__', ".1", '__', '__', "$101", ".1", '__', '__', ".2", '__', '__',"$102", ".5", '__', '__', ".1", '__', '__', ".1", '__', '__', "$101", ".1", '__', '__', ".2", '__', '__',"$102", ".7", '__', '__', ".3", '__', '__', ".1", '__', '__', "$101", ".1", '__', '__', ".2", '__', '__',"$102", ".6", '__', '__', ".3", '__', '__', ".1", '__', '__', "$101", ".1", '__', '__', ".2", '__', '__',"$102", ".2", '__', '__', ".2", '__', '__', ".1", '__', '__', "$101", ".1", '__', '__', ".2", '__', '__',
]
meta_time = 60 * 60 * 10 / 75
mid = MidiFile()
track1 = make_track(score)
track2 = make_track(score2)
track3 = make_track(score3)
mid.tracks.append(track1)
mid.tracks.append(track2)
mid.tracks.append(track3)
mid.save("music.mid")  # 写入midi文件
# os.system("music.mid")  # 直接打开文件

第二种方法

from mido import Message, MidiFile, MidiTrack
# from play import play_mididef play_note(note, length, track, base_num=0, delay=0, velocity=1.0, channel=0):meta_time = 60 * 60 * 10 / bpmmajor_notes = [0, 2, 2, 1, 2, 2, 2, 1]base_note = 60track.append(Message('note_on', note=base_note + base_num * 12 + sum(major_notes[0:note]), velocity=round(64 * velocity),time=round(delay * meta_time), channel=channel))track.append(Message('note_off', note=base_note + base_num * 12 + sum(major_notes[0:note]), velocity=round(64 * velocity),time=round(meta_time * length), channel=channel))def verse(track):play_note(1, 0.5, track)  # 小play_note(2, 0.5, track)  # 时play_note(1, 1.5, track)  # 候play_note(7, 0.25, track, -1)  # 妈play_note(6, 0.25, track, -1)  # 妈play_note(5, 0.5, track, -1, channel=1)  # 对play_note(3, 0.5, track, channel=1)  # 我play_note(3, 2, track, channel=1)  # 讲play_note(3, 0.5, track)  # 大play_note(4, 0.5, track)play_note(3, 1.5, track)  # 海play_note(2, 0.25, track)  # 就play_note(1, 0.25, track)  # 是play_note(6, 0.5, track, -1, channel=1)  # 我play_note(2, 0.5, track, channel=1)  # 故play_note(2, 2, track, channel=1)  # 乡play_note(7, 0.5, track, -1)  # 海play_note(1, 0.5, track)play_note(7, 1.5, track, -1)  # 边play_note(6, 0.25, track, -1)play_note(5, 0.25, track, -1)play_note(5, 0.5, track, -1, channel=1)  # 出play_note(2, 0.5, track, channel=1)play_note(2, 2, track, channel=1)  # 生play_note(4, 1.5, track)  # 海play_note(3, 0.5, track)  # 里play_note(1, 0.5, track)  # 成play_note(6, 0.5, track, -1)play_note(1, 3, track)  # 长def chorus(track, num):play_note(5, 0.5, track)  # 大play_note(6, 0.5, track)play_note(5, 1.5, track)  # 海play_note(3, 0.5, track)  # 啊play_note(5, 0.5, track, channel=1)  # 大play_note(6, 0.5, track, channel=1)play_note(5, 2, track, channel=1)  # 海play_note(6, 0.5, track)  # 是(就)play_note(5, 0.5, track)  # 我(像)play_note(4, 0.5, track)  # 生(妈)if num == 1:play_note(1, 0.25, track, channel=1)  # 活play_note(1, 0.25, track, channel=1)  # 的if num == 2:play_note(1, 0.5, track, channel=1)  # (妈)play_note(6, 0.5, track, channel=1)  # 地(一)play_note(5, 0.5, track, channel=1)play_note(5, 3, track, channel=1)  # 方(样)play_note(3, 0.5, track)  # 海(走)play_note(4, 0.5, track)  # 风(遍)play_note(3, 1.5, track)  # 吹(天)play_note(2, 0.25, track)  # (涯)play_note(1, 0.25, track)play_note(6, 0.5, track, -1, channel=1)  # 海(海)play_note(2, 0.5, track, channel=1)  # 浪play_note(2, 2, track, channel=1)  # 涌(角)play_note(4, 0.5, track)  # 随(总)play_note(5, 0.5, track)  # 我(在)play_note(4, 0.5, track)  # 漂(我)play_note(3, 0.5, track)  # 流(的)play_note(1, 0.5, track, channel=1)  # 四(身)play_note(6, 0.5, track, -1, channel=1)play_note(1, 3, track, channel=1)  # 方(旁)mid = MidiFile()  # 创建MidiFile对象
track = MidiTrack()  # 创建音轨
mid.tracks.append(track)  # 把音轨加到MidiFile对象中bpm = 75# 向音轨添加
#    Message对象(包括program_change、note_on、note_off等)
#    MetaMessage对象(用以表示MIDI文件的节拍、速度、调式等属性)
track.append(Message('program_change', program=12, time=0))
track.append(Message('note_on', note=64, velocity=64, time=32))
track.append(Message('note_off', note=64, velocity=127, time=32))verse(track)  # 主歌
chorus(track, num=1)  # 副歌1
chorus(track, num=2)  # 副歌2mid.save('new_song.mid')
# play_midi('new_song.mid')

读取法

import mido
mid = mido.MidiFile("music.mid")
for i, track in enumerate(mid.tracks):#enumerate():创建索引序列,索引初始为0print('Track {}: {}'.format(i, track.name))for msg in track:#每个音轨的消息遍历print(msg)

这篇关于midi 文件读取与写入(python mido)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/439716

相关文章

使用Python绘制蛇年春节祝福艺术图

《使用Python绘制蛇年春节祝福艺术图》:本文主要介绍如何使用Python的Matplotlib库绘制一幅富有创意的“蛇年有福”艺术图,这幅图结合了数字,蛇形,花朵等装饰,需要的可以参考下... 目录1. 绘图的基本概念2. 准备工作3. 实现代码解析3.1 设置绘图画布3.2 绘制数字“2025”3.3

python使用watchdog实现文件资源监控

《python使用watchdog实现文件资源监控》watchdog支持跨平台文件资源监控,可以检测指定文件夹下文件及文件夹变动,下面我们来看看Python如何使用watchdog实现文件资源监控吧... python文件监控库watchdogs简介随着Python在各种应用领域中的广泛使用,其生态环境也

Python中构建终端应用界面利器Blessed模块的使用

《Python中构建终端应用界面利器Blessed模块的使用》Blessed库作为一个轻量级且功能强大的解决方案,开始在开发者中赢得口碑,今天,我们就一起来探索一下它是如何让终端UI开发变得轻松而高... 目录一、安装与配置:简单、快速、无障碍二、基本功能:从彩色文本到动态交互1. 显示基本内容2. 创建链

Java调用Python代码的几种方法小结

《Java调用Python代码的几种方法小结》Python语言有丰富的系统管理、数据处理、统计类软件包,因此从java应用中调用Python代码的需求很常见、实用,本文介绍几种方法从java调用Pyt... 目录引言Java core使用ProcessBuilder使用Java脚本引擎总结引言python

python 字典d[k]中key不存在的解决方案

《python字典d[k]中key不存在的解决方案》本文主要介绍了在Python中处理字典键不存在时获取默认值的两种方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,... 目录defaultdict:处理找不到的键的一个选择特殊方法__missing__有时候为了方便起见,

使用Python绘制可爱的招财猫

《使用Python绘制可爱的招财猫》招财猫,也被称为“幸运猫”,是一种象征财富和好运的吉祥物,经常出现在亚洲文化的商店、餐厅和家庭中,今天,我将带你用Python和matplotlib库从零开始绘制一... 目录1. 为什么选择用 python 绘制?2. 绘图的基本概念3. 实现代码解析3.1 设置绘图画

Python pyinstaller实现图形化打包工具

《Pythonpyinstaller实现图形化打包工具》:本文主要介绍一个使用PythonPYQT5制作的关于pyinstaller打包工具,代替传统的cmd黑窗口模式打包页面,实现更快捷方便的... 目录1.简介2.运行效果3.相关源码1.简介一个使用python PYQT5制作的关于pyinstall

使用Python实现大文件切片上传及断点续传的方法

《使用Python实现大文件切片上传及断点续传的方法》本文介绍了使用Python实现大文件切片上传及断点续传的方法,包括功能模块划分(获取上传文件接口状态、临时文件夹状态信息、切片上传、切片合并)、整... 目录概要整体架构流程技术细节获取上传文件状态接口获取临时文件夹状态信息接口切片上传功能文件合并功能小

python实现自动登录12306自动抢票功能

《python实现自动登录12306自动抢票功能》随着互联网技术的发展,越来越多的人选择通过网络平台购票,特别是在中国,12306作为官方火车票预订平台,承担了巨大的访问量,对于热门线路或者节假日出行... 目录一、遇到的问题?二、改进三、进阶–展望总结一、遇到的问题?1.url-正确的表头:就是首先ur

基于Python实现PDF动画翻页效果的阅读器

《基于Python实现PDF动画翻页效果的阅读器》在这篇博客中,我们将深入分析一个基于wxPython实现的PDF阅读器程序,该程序支持加载PDF文件并显示页面内容,同时支持页面切换动画效果,文中有详... 目录全部代码代码结构初始化 UI 界面加载 PDF 文件显示 PDF 页面页面切换动画运行效果总结主