用的是Python的mido库,先来测试几个音。
#导入mido库 from mido import Message, MidiFile, MidiTrack #创建.mid文件 mid = MidiFile() #创建一个音轨 track = MidiTrack() #设置音色,1表示明亮的钢琴 track.append(Message('program_change',program=1, time=0)) #播放C4 track.append(Message('note_on', note=60,velocity=64, time=480)) track.append(Message('note_off', note=60,velocity=127, time=480)) #播放D4 track.append(Message('note_on', note=62,velocity=64, time=480)) track.append(Message('note_off', note=62,velocity=127, time=480)) midi_file="test.mid" #保存.mid文件 mid.save(midi_file)
运行后生成mid文件,Mac电脑可以用库乐队播放,Windows电脑可以用MidiEditor播放。
测试没问题,咱们正式开始写歌~
第一步:
找到《本草纲目》简谱
第二步:
简谱翻译成mido库能看懂的数据
def play_note(note, length, track, base_num=1, delay=0, velocity=1.0, channel=0): meta_time = 60 * 60 * 10 / bpm major_notes = [0, 2, 2, 1, 2, 2, 2, 1] base_note = 60 track.append(Message("program_change", program=2, time=0)) track.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))
第三步:
创建音轨,生成mid文件
fileName = "bcgm.mid" mid = MidiFile() # 创建midi文件 pg.mixer.init() # 创建音轨 track1 = play_note(2,mids1,0) track2 = play_note(44,mids2,4) mid.tracks.append(track1) mid.tracks.append(track2) mid.save(fileName)
最后播放音乐,听听最终效果吧~
原视频点击网址
https://blog.csdn.net/shine_a/article/details/124407036?spm=1001.2014.3001.5502
用Python写首《本草纲目》
完整代码:
# 本草纲目 from mido import Message, MidiFile, MidiTrack import pygame as pg ''' 播放一个音符 note 简谱的音符,21到108,以半音作为最小单位 length 音符长度 Program 确定乐器 2钢琴 velocity 音量 time 间隔时间 ''' def play_note(program,mids,channel): track = MidiTrack() track.append(Message("program_change", program=program, time=0)) for arrs in mids: if arrs[0] == 0: track.append(Message('note_on', note=arrs[1], velocity=arrs[2], time=arrs[3], channel=channel)) else: track.append(Message('note_off', note=arrs[1], velocity=arrs[2], time=arrs[3], channel=channel)) return track # 定义音符 arr[0] 0代表note_on,1代表note_off arr[1]=note arr[2]=velocity arr[3]=time mids1 = [[0, 75, 80, 48], [1, 75, 64, 216], [0, 70, 80, 24], [1, 70, 64, 108], [0, 73, 80, 12], [1, 73, 64, 108], [0, 75, 80, 12], [1, 75, 64, 216], [0, 75, 80, 24], [1, 75, 64, 216], [0, 70, 80, 24], [1, 70, 64, 108], [0, 73, 80, 12], [1, 73, 64, 108], [0, 75, 80, 12], [1, 75, 64, 216], [0, 75, 80, 24], [1, 75, 64, 432], [0, 75, 80, 48], [1, 75, 64, 216], [0, 70, 80, 24], [1, 70, 64, 108], [0, 73, 80, 12], [1, 73, 64, 108], [0, 75, 80, 12], [1, 75, 64, 216], [0, 75, 80, 24], [1, 75, 64, 216], [0, 70, 80, 24], [1, 70, 64, 108], [0, 73, 80, 12], [1, 73, 64, 108], [0, 70, 80, 12], [1, 70, 64, 216], [0, 63, 80, 24], [1, 63, 64, 432], [0, 75, 80, 48], [1, 75, 64, 216], [0, 70, 80, 24], [1, 70, 64, 108], [0, 73, 80, 12], [1, 73, 64, 108], [0, 75, 80, 12], [1, 75, 64, 216], [0, 75, 80, 24], [1, 75, 64, 216], [0, 70, 80, 24], [1, 70, 64, 108], [0, 73, 80, 12], [1, 73, 64, 108], [0, 75, 80, 12], [1, 75, 64, 216], [0, 75, 80, 24], [1, 75, 64, 432], [0, 75, 80, 48], [1, 75, 64, 216], [0, 70, 80, 24], [1, 70, 64, 108], [0, 73, 80, 12], [1, 73, 64, 108], [0, 75, 80, 12], [1, 75, 64, 216], [0, 75, 80, 24], [1, 75, 64, 216], [0, 70, 80, 24], [1, 70, 64, 108], [0, 73, 80, 12], [1, 73, 64, 108], [0, 75, 80, 12], [1, 75, 64, 216], [0, 75, 80, 24], [1, 75, 64, 432], [0, 76, 80, 0], [1, 76, 64, 216], [0, 71, 80, 24], [1, 71, 64, 108], [0, 74, 80, 12], [1, 74, 64, 108], [0, 76, 80, 12], [1, 76, 64, 216], [0, 76, 80, 24], [1, 76, 64, 216], [0, 71, 80, 24], [1, 71, 64, 108], [0, 74, 80, 12], [1, 74, 64, 108], [0, 76, 80, 12], [1, 76, 64, 216], [0, 76, 80, 24], [1, 76, 64, 432], [0, 76, 80, 48], [1, 76, 64, 216], [0, 71, 80, 24], [1, 71, 64, 108], [0, 74, 80, 12], [1, 74, 64, 108], [0, 76, 80, 12], [1, 76, 64, 216], [0, 76, 80, 24], [1, 76, 64, 216], [0, 71, 80, 24], [1, 71, 64, 108], [0, 74, 80, 12], [1, 74, 64, 108], [0, 71, 80, 12], [1, 71, 64, 216], [0, 64, 80, 24], [1, 64, 64, 432], [0, 76, 80, 48], [1, 76, 64, 216], [0, 71, 80, 24], [1, 71, 64, 108], [0, 74, 80, 12], [1, 74, 64, 108], [0, 76, 80, 12], [1, 76, 64, 216], [0, 76, 80, 24], [1, 76, 64, 216], [0, 71, 80, 24], [1, 71, 64, 108], [0, 74, 80, 12], [1, 74, 64, 108], [0, 76, 80, 12], [1, 76, 64, 216], [0, 76, 80, 24], [1, 76, 64, 432], [0, 76, 80, 48], [1, 76, 64, 216], [0, 71, 80, 24], [1, 71, 64, 108], [0, 74, 80, 12], [1, 74, 64, 108], [0, 76, 80, 12], [1, 76, 64, 216], [0, 76, 80, 24], [1, 76, 64, 216], [0, 71, 80, 24], [1, 71, 64, 108], [0, 74, 80, 12], [1, 74, 64, 108], [0, 76, 80, 12], [1, 76, 64, 216], [0, 76, 80, 24], [1, 76, 64, 432]] mids2 = [[0, 37, 80, 0], [1, 37, 64, 108], [0, 39, 80, 12], [1, 39, 64, 108], [0, 39, 80, 612], [1, 39, 64, 108], [0, 37, 80, 852], [1, 37, 64, 108], [0, 39, 80, 12], [1, 39, 64, 216], [0, 39, 80, 624], [1, 39, 64, 108], [0, 39, 80, 132], [1, 39, 64, 216], [0, 39, 80, 24], [1, 39, 64, 108], [0, 39, 80, 12], [1, 39, 64, 108], [0, 39, 80, 12], [1, 39, 64, 108], [0, 37, 80, 252], [1, 37, 64, 108], [0, 39, 80, 12], [1, 39, 64, 108], [0, 39, 80, 612], [1, 39, 64, 108], [0, 37, 80, 852], [1, 37, 64, 108], [0, 39, 80, 12], [1, 39, 64, 216], [0, 39, 80, 624], [1, 39, 64, 108], [0, 39, 80, 132], [1, 39, 64, 216], [0, 39, 80, 24], [1, 39, 64, 108], [0, 39, 80, 12], [1, 39, 64, 108], [0, 39, 80, 12], [1, 39, 64, 108], [0, 37, 80, 732], [1, 37, 64, 108],[0, 37, 80, 0], [1, 37, 64, 108], [0, 39, 80, 12], [1, 39, 64, 108], [0, 39, 80, 612],[1, 39, 64, 108], [0, 37, 80, 852], [1, 37, 64, 108], [0, 39, 80, 12], [1, 39, 64, 216],[0, 39, 80, 624], [1, 39, 64, 108], [0, 39, 80, 132], [1, 39, 64, 216], [0, 39, 80, 24], [1, 39, 64, 108], [0, 39, 80, 12], [1, 39, 64, 108], [0, 39, 80, 12], [1, 39, 64, 108],[0, 37, 80, 252], [1, 37, 64, 108], [0, 39, 80, 12], [1, 39, 64, 108], [0, 39, 80, 612],[1, 39, 64, 108], [0, 37, 80, 852], [1, 37, 64, 108], [0, 39, 80, 12], [1, 39, 64, 216]] # 播放MIDI def play_music(music_file): clock = pg.time.Clock() pg.mixer.music.load(music_file) pg.mixer.music.play() while pg.mixer.music.get_busy(): clock.tick(30) # 主程序 fileName = "bcgm.mid" mid = MidiFile() # 创建midi文件 pg.mixer.init() # 创建音轨 track1 = play_note(2,mids1,0) track2 = play_note(44,mids2,4) mid.tracks.append(track1) mid.tracks.append(track2) # 保存mid文件 mid.save(fileName) play_music(fileName)
复制粘贴代码保存到Python文件中,运行Python文件,就能听到歌声啦~