@psweb/midi
v2023.4.28
Published
midiPlayer support vue3 nodejs
Downloads
1
Readme
midi audio
版本
版本V2023.4.28全新版本
开发者已放弃V2023.3.5的维护。
物理硬件说明
- 最低配置 双核4线程
- 硬件不同,差异可能较大,此程序在10核20线程的Windows10 x64 pc机上编译通过。
支持 nodejs 和 Vue3
支持同步和异步处理
任务、代码说明
- 安装 npm i @psweb/midi
- 配置参数
//主旋律参数设置
midi.setVolume(0x7f); //音量设置
midi.setChannel(10); //通道设置
midi.setSleep(460); //节拍设置
midi.setTonesID(35); //音色设备设置
//主旋律参数设置
midi.setVoice("Volume", 0x4f); //音量设置
midi.setVoice("Channel", 5); //通道设置 0-1是钢琴专用通道
midi.setVoice("Sleep", 280); //节拍设置
midi.setVoice("TonesID", 50); //音色设备设置 0-1 channel时 Tones无效
midi.setVoice("Print", 2);//屏幕打印 0-5打印各类运行代码于终端
midi.setVoice("LiftThe", 0); //升降调[...,-3,-2,-1,0,3,2,1,...]
- 两种主旋律参数设置 ,都可以使用,采用其中一种。
- 工程规定
import * as midi from '@psweb/midi/midi'
midi.midiOpen()
{
...
//所有的任务都在{}中设置、配置和运行
};
midi.midiClose();
- 播放
//1.播放池
data.forEach(notes=>{
//退出,要求歌谱结尾 NULL
if (notes == NULL) midi.midiClose()
midi.midiPlayer(notes);//播放
})
//2.播放池
for(int i=0; i<data.length;i++)
{
midi.midiPlayer(data[i]);//播放
}
//退出
midi.midiClose()
- 设备音色配置
const Tones = {
//小于2 channel的通道,系统保留给钢琴音色。
MainMuisc: [
{ tones: midi.midiTones(10, 2, M2, Vol, HIGH_SPEED) },
{ tones: midi.midiTones(15, 2, M2, Vol, HIGH_SPEED) }
]
}
//1.参数说明
//midi.midiTones(音符设备号, 通道, 音符, 音量, 节拍)
//2.对象配置
//midi.configMidi(Tones);暂时未使用
//3.对象项目只认函数,标题随意。
- 测试音符 <a id="index11/>
midi.midiAudition(60);//音色设备试音
//测试音色
for(let i=0;i<128;i++)
{
midi.midiAudition(i);
}
//单音符测试
midi.Audio(M5);
//线程测试
midi.ThreadTest(C6);
源码(歌谱:校音和北风吹)
- index.ts
import * as midi from '@psweb/midi/midi'
let //声调
X1 = 36, X2 = 38, X3 = 40, X4 = 41, X5 = 43, X6 = 45, X7 = 47,
L1 = 48, L2 = 50, L3 = 52, L4 = 53, L5 = 55, L6 = 57, L7 = 59,
M1 = 60, M2 = 62, M3 = 64, M4 = 65, M5 = 67, M6 = 69, M7 = 71,
H1 = 72, H2 = 74, H3 = 76, H4 = 77, H5 = 79, H6 = 81, H7 = 83,
A1 = 84, A2 = 86, A3 = 88, A4 = 89, A5 = 91, A6 = 93, A7 = 95;
let //控制符
LOW_SPEED = 600, //低速,前缀符,管理他后面的音符
MIDDLE_SPEED = 300, //中速,前缀符
HIGH_SPEED = 175, //高速,前缀符
_ = 0XFFA, //拍数
ξ = 0XFFE, //休止符,0
Vol = 0x7F, //音量0x0F-0x7F
NULL = 0XFFFF, //终止
ʃ = 0XFFB, //颤音后缀符,管理他前面的音符
β = 0XFFD; //下滑音后缀符,管理他前面的音符
//1,2,3,4,5,6,7,8,9 //后缀符,管理他前面的音符
//谱子
let data = [
//校音
HIGH_SPEED,
L6, _, _, M1, _, _, L7, _, _, L6, _, _, L5, _,
A1, 1, A2, 2, A3, 3, A4, 4, A5, 5, A6, 6, A7, 7, H1, 1,
L1, L2, L3, L4, L5, L6, L7, X1,ξ,
MIDDLE_SPEED,
M1, M2, M3, M4, M5, M6, M7, L1,
H1, H2, H3, β, H4, β, H5, H6, H7, M1,
LOW_SPEED,
X1, X2, X3, X4, X5, X6, X7, M1,
0, 0, 0, 0,
M1, M2, M3, M4, M5, M6, L6, ʃ, ʃ, ʃ,
MIDDLE_SPEED,
L6, _, _, M1, _, _, L7, _, _, L6, _, _, L5, _,
0, 0, 0, 0,
L6, ʃ, L5, ʃ, M5, ʃ, M6, ʃ, M5, ʃ,
0, 0,
//北风吹
HIGH_SPEED,
M6, _, M5, M5, M2, M3, M2, M3,
M5, M4, M3, M2, M2, M6, M1,
M3, M3, M5, M2, M1, M1, M5,
M6, M6, M2, M2, M7, M6, M5,
M3, M2, M3, M6, M1, M2, M5,
M3, M5, M3, M2, M1, M2, M1,
M2, M3, M5, M3, M2, L6, L6,
M1, L7, L6, L5, M5, M6, M5,
L6, 1, M1, 1, L7, 1,
L6, ʃ, L5, ʃ, M5, ʃ, M6, ʃ, M5, ʃ,
0, 0, 0, 0,
MIDDLE_SPEED,
M6, _, M5, M5, M2, M3, M2, M3,
M5, M4, M3, M2, M2, M6, M1,
M3, M3, M5, M2, M1, M1, M5,
M6, M6, M2, M2, M7, M6, M5,
M3, M2, M3, M6, M1, M2, M5,
M3, M5, M3, M2, M1, M2, M1,
M2, M3, M5, M3, M2, L6, L6,
M1, L7, L6, L5, M5, M6, M5,
L6, _, _, M1, _, _, L7, _, _, L6, _, _, L5, _,
0, 0, 0, 0,
LOW_SPEED,
M6, _, M5, M5, M2, M3, M2, M3,
M5, M4, M3, M2, M2, M6, M1,
M3, M3, M5, M2, M1, M1, M5,
M6, M6, M2, M2, M7, M6, M5,
M3, M2, M3, M6, M1, M2, M5,
M3, M5, M3, M2, M1, M2, M1,
M2, M3, M5, M3, M2, L6, L6,
M1, L7, L6, L5, M5, M6, M5,
L6, _, M1, _, L7, _, L6, _, L5, _,
0, 0, 0, 0,
HIGH_SPEED,
M6, _, M5, M5, M2, M3, M2, M3,
M5, M4, M3, M2, M2, M6, M1,
M3, M3, M5, M2, M1, M1, M5,
M6, M6, M2, M2, M7, M6, M5,
M3, M2, M3, M6, M1, M2, M5,
M3, M5, M3, M2, M1, M2, M1,
M2, M3, M5, M3, M2, L6, _, _,
M1, _, _, L7, _, _, L6, _, _,
L5, _, _, _, _, _, _, _, _, _, _,
0, 0, 0, 0,
NULL
];
midi.midiOpen()
{
//配置音色
const Tones = {
//小于2 channel的通道,系统保留给钢琴音色。
Muisc: [
{ tones: midi.midiTones(22, 10, M2, Vol, HIGH_SPEED) },
{ tones: midi.midiTones(23, 10, M2, Vol, HIGH_SPEED) },
{ tones: midi.midiTones(24, 10, M2, Vol, HIGH_SPEED) },
{ tones: midi.midiTones(25, 10, M2, Vol, HIGH_SPEED) },
{ tones: midi.midiTones(26, 10, M2, Vol, HIGH_SPEED) }
],
percussion: [
{ tones: midi.midiTones(112, 9, M3, Vol, HIGH_SPEED) },
{ tones: midi.midiTones(113, 9, M3, Vol, HIGH_SPEED) },
{ tones: midi.midiTones(114, 9, M3, Vol, HIGH_SPEED) },
{ tones: midi.midiTones(116, 10, M3, Vol, HIGH_SPEED) },
{ tones: midi.midiTones(117, 10, M3, Vol, HIGH_SPEED) },
{ tones: midi.midiTones(118, 10, M3, Vol, HIGH_SPEED) },
{ tones: midi.midiTones(119, 9, M3, Vol, HIGH_SPEED) }
],
FolkMusical: [
{ tones: midi.midiTones(104, 3, M3, Vol, HIGH_SPEED) },
{ tones: midi.midiTones(105, 3, M3, Vol, HIGH_SPEED) },
{ tones: midi.midiTones(115, 10, M3, Vol, HIGH_SPEED) },
{ tones: midi.midiTones(106, 7, M3, Vol, HIGH_SPEED) },
{ tones: midi.midiTones(107, 7, M3, Vol, HIGH_SPEED) },
{ tones: midi.midiTones(108, 7, M3, Vol, HIGH_SPEED) }
]
}
//主旋律参数设置
midi.setVoice("Volume", 0x7f);//音量设置
midi.setVoice("Channel", 10); //0-1是钢琴专用通道
midi.setVoice("Sleep", 280); //旋律速度
midi.setVoice("TonesID", 73); //0-1 channel时 Tones无效
midi.setVoice("Print", 5); //屏幕打印 0-6打印各类运行代码于终端
midi.setVoice("LiftThe", 0); //升降调[...,-3,-2,-1,0,3,2,1,...]
let res = 0;
while (res <= 3) {
//不同音色,重复演奏
// if (res == 1) { midi.setVoice("TonesID", 74); }
// if (res == 2) { midi.setVoice("TonesID", 10); }
// if (res == 3) { midi.setVoice("TonesID", 104); }
// res++;
//播放池
data.forEach(i => {
//节拍
if (i == HIGH_SPEED) {
midi.setVoice("Sleep", HIGH_SPEED);
// console.log("高速");
return;
}
if (i == MIDDLE_SPEED) {
midi.setVoice("Sleep", MIDDLE_SPEED);
// console.log("中速");
return;
}
if (i == LOW_SPEED) {
midi.setVoice("Sleep", LOW_SPEED);
// console.log("低速");
return;
}
midi.midiPlayer(i);//播放
})
}
}
//结束时关闭
midi.midiClose();
音色
//音色 tones:
钢琴: 0-7,
色彩打击乐器:8-15,
风琴: 16-23,
吉它: 24-31,
贝司: 32-39,
弦乐,提琴: 40-47,
合奏: 48-55,
铜管,号: 56-63,
簧管,萨克斯: 64-71,
笛: 72-79,
合成主音:80-87,
合成音色:88-95,
合成效果:96-103,
民间乐器:104-111,
打击乐器:112-119,
声音效果:120-127
示例 example
- example\index.ts 北风吹
- example\Trivia.ts 平凡之路
- example\wind.ts 起飞了
- example\Anotes.ts 音符试音