-
Notifications
You must be signed in to change notification settings - Fork 3
/
sound.js
263 lines (221 loc) · 5.08 KB
/
sound.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
import File from './file.js'
import Sound from '../boot/Sound.js'
import Tool from './tool.js'
import * as Adpcm from '../boot/imaadpcm.js'
const SOUND = 'COMMON/Sound/'
// 背景音乐, sub01 里昂第一个场景音乐
const BGM = SOUND +'BGM/';
// fs 是地板声音, room 是机关/水/电梯等房间中的音效
const ROOM = SOUND +'room/';
const FLOOR = ROOM;
// 开枪的声音
const ARMS = SOUND +'arms/';
// 开门的声音, 开门是上楼梯等
const DOOR = SOUND +'door/';
// 敌人发出的声音
const ENEMY = SOUND +'enemy/';
// 各种声音音效, core16 是标题音乐.
const CORE = SOUND +'core/';
let core;
let main;
let sub0;
let sub1;
export default {
init,
bgm,
vab,
getBgm,
playSE,
playVoice,
floorSE,
enemySE,
mapSE,
};
// 暂时不用
function vab(buf, rate, ch) {
let wavbuf = new Int16Array(Adpcm.decode(buf, 16));
console.log(buf.length, wavbuf.length);
let wav = new Sound.Wav(core);
wav.rawBuffer(wavbuf, rate || 44100, ch || 1);
wav.play();
wav.loop(true);
return wav;
}
//
// 允许内部 wav 对象被释放而不会崩溃.
//
class AnimSound {
constructor(fname, _wav) {
this.state = 0;
try {
if (fname) {
const file = File.open(fname);
this.wav = sap(file.buf, false, false);
this.state = 1;
} else if (_wav) {
this.wav = _wav;
this.state = 1;
} else {
console.warn("not se");
}
} catch(e) {
console.error("cannot init AnimSound", fname || e);
this.state = -1;
}
}
free() {
if (this.state == -1) return;
this.state = -1;
if (this.wav) {
this.wav.free();
this.wav = null;
}
}
// flag: 0x8 左脚? 0xC 右脚?
play(flag) {
if (this.state == 1) {
let cp = this.wav.clone();
cp.play();
}
}
length() {
if (this.state == 1) {
return this.wav.length();
}
return 0;
}
}
function enemySE(sound_bank) {
const fname = ENEMY +'enemy'+ Tool.d2(sound_bank) +'.sap';
return new AnimSound(fname);
}
function floorSE(floor, vab) {
// TODO: 整对他
let prgIdx = ((floor.se_no & 0xF0) >>4) -1;
let tonIdx = floor.se_no & 0x7;
let raw = vab.raw[ vab.prog[prgIdx].tone[tonIdx].vag ];
const se = mapSE(raw);
se.height = floor.height;
se.range = Tool.xywd2range(floor);
return se;
}
function mapSE(raw) {
let wav = null;
if (raw) {
wav = new Sound.Wav(core);
wav.rawBuffer(raw, 44100/2, 1, audio.RAW_TYPE_16BIT);
}
return new AnimSound(null, wav);
}
function playSE(stage, se) {
let name = ROOM +'room'+ stage + Tool.b2(se) +'.sap';
// let name = ROOM +'room'+ se +'.sap';
try {
let f = File.open(name);
return sap(f.buf, true, false);
} catch(e) {
console.error("Play SE", name, "fail:", e);
}
}
function playVoice(playerid, stage, id) {
let name = 'Pl'+ playerid +'/VOICE/stage'+ stage +'/v' + Tool.d3(id) +'.sap';
try {
let f = File.open(name);
return sap(f.buf, true, false);
} catch(e) {
console.error("Play voice", name, 'fail:', e);
}
}
function getBgm(id) {
switch (id) {
case 0:
return main;
case 1:
return sub0;
case 2:
return sub1;
default:
throw new Error("bad id "+ id);
}
}
function bgm(mainid, sub0id, sub1id) {
if (mainid) {
if (!main) {
loadMain();
} else if (main.id != mainid) {
main.free();
loadMain();
}
} else {
main.free();
}
if (sub0) sub0.free();
if (sub1) sub1.free();
if (sub0id) sub0 = loadSub(sub0id);
if (sub1id) sub1 = loadSub(sub1id);
function loadMain() {
try {
let f = File.open(BGM +'main'+ mainid +'.sap');
main = sap(f.buf, false, true);
main.id = mainid;
} catch(e) {
console.error("Load Main sound", e.stack);
}
}
function loadSub(subid) {
try {
let f = File.open(BGM +'sub'+ subid +'.sap');
return sap(f.buf, false, true);
} catch(e) {
console.error('Load Sub sound', e.stack);
}
}
}
function sap(arraybuf, play, loop) {
let wav = new Sound.Wav(core);
wav.fromBuffer(new Uint8Array(arraybuf, 8));
if (play) wav.play();
wav.loop(loop);
return wav;
}
function init(win) {
core = new Sound.Core();
// test(ROOM, win, true);
}
function test(dir, win, loop) {
let bg = File.read_dir(dir);
let arr = [];
let wav = new Sound.Wav(core);
let i = 0; //61; 初始场景音乐
bg.forEach(function(f) {
let n = f.toLowerCase();
if (n.endsWith('.sap')) {
arr.push(f);
}
});
// play();
let it = win.input();
it.pressOnce(gl.GLFW_KEY_U, function() {
if (++i >= arr.length) i = 0;
play();
});
it.pressOnce(gl.GLFW_KEY_I, function() {
if (--i < 0) i = arr.length -1;
play();
});
it.pressOnce(gl.GLFW_KEY_O, function() {
wav && wav.pause();
});
console.log("press U/I switch music");
function play() {
if (wav) wav.free();
console.log('Read', i, arr[i]);
let f = File.open(arr[i], true);
if (f.size <= 8) {
console.log(" bad", f.size);
return;
}
wav = sap(f.buf, true, loop);
console.log(' length:', wav.length(), 's');
}
}