perl:用 MIDI::Simple 生成midi文件,用 pygame 播放 mid文件

csdn.net 下载 strawberry-perl-5.32.1.1-64bit.zip 解压安装在 D:\Strawberry\

运行 cpan

install MIDI::Simple

D:\Strawberry\c\bin\gmake.exe test -- OK

Running make install for CONKLIN/MIDI-Perl-0.84.tar.gz

Installing D:\Strawberry\perl\site\lib\MIDI.pm

Installing D:\Strawberry\perl\site\lib\MIDI\Event.pm

Installing D:\Strawberry\perl\site\lib\MIDI\Filespec.pod

Installing D:\Strawberry\perl\site\lib\MIDI\Opus.pm

Installing D:\Strawberry\perl\site\lib\MIDI\Score.pm

Installing D:\Strawberry\perl\site\lib\MIDI\Simple.pm

Installing D:\Strawberry\perl\site\lib\MIDI\Track.pm

Appending installation info to D:\Strawberry\perl\lib/perllocal.pod

CONKLIN/MIDI-Perl-0.84.tar.gz

D:\Strawberry\c\bin\gmake.exe install UNINST=1 -- OK


官网文档:https://metacpan.org/pod/MIDI::Simple

编辑 test_midi.pl 如下

perl 复制代码
#!/usr/bin/perl
use warnings;
use MIDI::Simple;
new_score;
text_event 'http://www.ely.anglican.org/parishes/camgsm/bells/chimes.html';
text_event 'Lord through this hour/ be Thou our guide';
text_event 'so, by Thy power/ no foot shall slide';
set_tempo 500000;  # 1 qn => .5 seconds (500,000 microseconds)
patch_change 1, 8;  # Patch 8 = Celesta
 
noop c1, f, o5;  # Setup
# Now play
n 'qn', 'Cs';    n 'F';   n 'Ds';  n 'hn', 'Gs_d1';
n 'qn', 'Cs';    n 'Ds';  n 'F';   n 'hn', 'Cs';
n 'qn', 'F';     n 'Cs';  n 'Ds';  n 'hn', 'Gs_d1';
n 'qn', 'Gs_d1'; n 'Ds';  n 'F';   n 'hn', 'Cs';
 
write_score 'westminster_chimes.mid';

运行 perl test_midi.pl 生成 westminster_chimes.mid 文件

运行 python play_mid.py westminster_chimes.mid

play_mid.py 参见:python:用 mido 生成 midi文件,用 pygame 播放 mid文件


持续时间参数:当前的法定值以"wn"、"hn"、"qn"、"en"、"sn"开头,表示整音符、二分音符、四分音符、八分音符或十六分音符。

字母数字的形式是:一个字符串表示八度音程内的音符(由 确定),然后是一个可选参数"_u[number]"表示"从当前八度音阶上升这么多个八度音阶"或"_d[参数]"表示"从当前八度音阶下降这么多个八度音阶"。例如:"C"、"As"或"Asharp"、"Bflat"或"Bf"、"C_d3"、"As_d1"或"Asharp_d1"、"Bflat_u3"或"Bf_u3"。

patch_change 通道、补丁;

key_after_touch 通道、音符、速度;

channel_after_touch 通道、速度;

control_change 通道,控制器(0-127),值(0-127);

pitch_wheel_change 频道,pitch_wheel;

set_tempo 节奏;(请参阅下面的速度部分。

smpte_offset 小时、明尼苏达州、东南、法国、ff;

time_signature nn、dd、cc、bb;

key_signature SF, MI ;

text_event 文本;

copyright_text_event 文本;

track_name 文本;

instrument_name 文本;

歌词文本;

set_sequence_number 序列;


乐器伴奏《祝你生日快乐》,编写 happy_birthday.pl 如下

perl 复制代码
#!/usr/bin/perl
use warnings;
use MIDI::Simple;
new_score;
text_event 'score: Happy birthday to you';
set_tempo 500000;  # 1 qn => .5 seconds (500,000 microseconds)
#patch_change 1, 8;  # Patch 8 = Celesta
 
noop c1, f, o5;  # Setup
# Now play
n 'qn','G_d1'; n 'qn','G_d1'; n 'hn','A_d1'; n 'hn','G_d1';
n 'hn','C'; n 'wn','B_d1'; 
n 'qn','G_d1'; n 'qn','G_d1'; n 'hn','A_d1'; n 'hn','G_d1';
n 'hn','D'; n 'wn','C';
n 'qn','G_d1'; n 'qn','G_d1'; n 'hn','G'; n 'hn','E';
n 'hn','C'; n 'hn', 'B_d1'; n 'hn', 'A_d1';
n 'qn','F'; n 'qn','F'; n 'hn','E'; n 'hn','C';
n 'hn','D'; n 'wn','C';

write_score 'happy_birthday.mid';

运行 perl happy_birthday.pl 生成 happy_birthday.mid 文件

演奏 python play_mid.py happy_birthday.mid

my 太太说:听音调还是有些不准。请程序员网友指正。

相关推荐
是毛毛吧2 小时前
边打游戏边学Python的5个开源项目
python·开源·github·开源软件·pygame
hid711713668 小时前
基于RISC-V指令集的五级流水线CPU设计、验证及上板实践:详细说明与代码注释完备
perl
沟通qq 87622396512 小时前
基于改进A*算法的单agv路径规划算法仿真 可以更改地图,起始点,目标点 % 1 表示障碍物 ...
perl
Cherry的跨界思维1 天前
10、Python词语排序与查找效率优化终极指南:指定顺序+O(1)查找
开发语言·数据库·python·django·beautifulsoup·pyqt·pygame
牢七1 天前
小迪实验记录
pygame
Cherry的跨界思维3 天前
5、Python长图拼接终极指南:Pillow/OpenCV/ImageMagick三方案
javascript·python·opencv·webpack·django·pillow·pygame
Cherry的跨界思维3 天前
8、Python正则表达式精准搜索实战:从模糊匹配到精准定位
开发语言·python·正则表达式·django·flask·pygame·tornado
是Dream呀3 天前
Python圣诞特辑:打造一棵会唱歌、会下雪的魔法圣诞树
开发语言·python·pygame
添加shujuqudong1如果未回复4 天前
出一个已配置参数的18650圆柱锂电池COMSOL模型用于生热研究
perl
聊天QQ:276998855 天前
横列式双旋翼两轴飞行器倾转旋翼simulink simscape仿真MATLAB 内环
pygame