-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathplayer.js
133 lines (130 loc) · 4.31 KB
/
player.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
var instrumentURL = "./soundfont/basic/";
var instrumentMap = new Int32Array(128);
var instrumentDownload = new Int32Array(128);
function downloadSoundFont(id){
if(instrumentDownload[id]!=1){
var stabox = document.getElementById('soundfont-status');
var sta = document.createElement("div");
var staNum = document.createElement("span");
sta.appendChild(document.createTextNode("加载音源:"));
staNum.innerText="0";
sta.appendChild(staNum);
sta.appendChild(document.createTextNode("%"));
stabox.appendChild(sta);
console.log("download soundfont:" + id);
instrumentDownload[id] = 1;
MIDI.loadPlugin({
soundfontUrl: instrumentURL,
instruments: [id],
onprogress: function(state, progress) {
staNum.innerText = parseInt(progress * 100);
},
onsuccess: function() {
instrumentMap[id] = id;
setTimeout(function(){
sta.remove();
},1000);
}
});
}
}
window.onload = function() {
//console.log("load soundfont");
if (self != top) return;
document.getElementById('soundfont-status').innerHTML = "";
downloadSoundFont(0);
};
window.mgnr = {
"noteOn": function(info,channel, tone, v) {
if (window.playOutput) {
if (window.mgnr.engine == null) {
window.mgnr.engine = document.getElementById("synth-engine");
}
window.mgnr.engine.contentWindow.postMessage({
"mode": "noteOn",
"note": tone,
"vol": v,
"info": info
}, '*');
} else {
MIDI.noteOn(channel, tone, v, 0);
}
},
"noteOff": function(info,channel, tone) {
if (window.playOutput) {
if (window.mgnr.engine == null) {
window.mgnr.engine = document.getElementById("synth-engine");
}
window.mgnr.engine.contentWindow.postMessage({
"mode": "noteOff",
"note": tone,
"info": info
}, '*');
} else {
MIDI.noteOff(channel, tone, 0);
}
},
"setChannelInstrument":function(c,i){
try{
MIDI.channels[c].instrument = window.instrumentMap[i];
}catch(e){
console.log(e);
}
},
"requireInstrument": function(id) {
if (self != top) return;
try{
downloadSoundFont(id);
}catch(e){}
},
"loadName": function(info) {
//console.log("use:" + info);
},
"synth": {
//mGenNer歌声合成协议
//工程通过postMessage发送至目标引擎
//由于使用的postMessage,引擎允许跨域加载
//一个工程由多个音轨构成,每个音轨由按顺序排列的音符(词汇)或休止符构成,单位是秒
"init": function(num) {
//初始化,并且告诉引擎有多少个音轨
window.mgnr.engine = document.getElementById("synth-engine");
window.mgnr.engine.contentWindow.postMessage({
"mode": "init",
"channels": num
}, '*');
},
"addWord": function(id, vname, tone, vol, sec) {
//往一个音轨追加一个词(单位是秒)
window.mgnr.engine.contentWindow.postMessage({
"mode": "addWord",
"name": vname,
"tone": tone,
"volume": vol,
"length": sec
}, '*');
},
"addPause": function(id, len) {
//往一个音轨追加一个休止符(单位是秒)
window.mgnr.engine.contentWindow.postMessage({
"mode": "addPause",
"id": id,
"length": len
}, '*');
},
"start": function() {
//开始合成
window.mgnr.engine.contentWindow.postMessage({
"mode": "start"
}, '*');
}
},
"engine": null
};
window.onmessage = function(data) {
if (data.data) {
if (data.data.mode == "playOutput") {
window.playOutput = data.data.val;
console.log("set playOutput:", window.playOutput);
}
}
};