-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayerweb.htm
239 lines (224 loc) · 7.39 KB
/
playerweb.htm
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
<!DOCTYPE html>
<script>
// Check for wasm support.
if (!('WebAssembly' in window)) {
alert('you need a browser with wasm support enabled :(');
}
var tempRet0;
var env = {
i64Add__asm: true,
i64Add__sig: 'iiiii',
_i64Add: function(a, b, c, d) {
a = a|0; b = b|0; c = c|0; d = d|0;
var l = 0, h = 0;
l = (a + c)>>>0;
h = (b + d + (((l>>>0) < (a>>>0))|0))>>>0; // Add carry from low word to high word on overflow.
return tempRet0 = h, l|0;
},
getTempRet0__asm: true,
getTempRet0__sig: 'i',
getTempRet0: function() {
return tempRet0;
},
bitshift64Shl__asm: true,
bitshift64Shl__sig: 'iiii',
_bitshift64Shl: function(low, high, bits) {
low = low|0; high = high|0; bits = bits|0;
var ander = 0;
if ((bits|0) < 32) {
ander = ((1 << bits) - 1)|0;
tempRet0 = (high << bits) | ((low&(ander << (32 - bits))) >>> (32 - bits));
return low << bits;
}
tempRet0 = low << (bits - 32);
return 0;
},
_log_int:console.log,
memory:new WebAssembly.Memory({ initial: 256 })
}
// Loads a WebAssembly dynamic library, returns a promise.
// imports is an optional imports object
function loadWebAssembly(filename, imports) {
// Fetch the file and compile it
return fetch(filename)
.then(response => response.arrayBuffer())
.then(buffer => WebAssembly.compile(buffer))
.then(module => {
// Create the imports for the module, including the
// standard dynamic library imports
imports = imports || {};
imports.env = imports.env || env;
imports.env.__memory_base = imports.env.__memory_base || 0;
imports.env.__table_base = imports.env.__table_base || 0;
imports.env.abort = imports.env.abort || alert;
//imports.env.segfault = imports.env.segfault || alert;
//imports.env.alignfault = imports.env.alignfault || alert;
//imports.env.abortStackOverflow = imports.env.abortStackOverflow || alert;
if (!imports.env.memory) {
imports.env.memory = new WebAssembly.Memory({ initial: 256 });
}
if (!imports.env.table) {
imports.env.table = new WebAssembly.Table({ initial: 256, element: 'anyfunc' });
}
// Create the instance.
return WebAssembly.instantiate(module, imports);
});
}
// One-liner to resume playback when user interacted with the page.
document.addEventListener('click', function() {
console.log('click');
if (!audioContext || audioContext.state != 'suspended')
next();
else
audioContext.resume().then(() => {
console.log('Playback resumed successfully');
next();
});
});
var xhr = new XMLHttpRequest();
xhr.open('GET', 'descent.hog', true);
xhr.responseType = 'arraybuffer';
xhr.onload = function(ev) {
var ret = this.response;
if (!ret) {
console.log('no response');
return;
}
var data = new Uint8Array(ret);
if (data[0] != 0x44 || data[1] != 0x48 || data[2] != 0x46) {
console.log('not hog: ' + data[0]);
return;
}
var alen = data.length;
var i = 3;
var files = {};
while (i < alen) {
var name = '';
for (var j = 0; j < 13 && data[i + j]; j++)
name += String.fromCharCode(data[i + j]);
var flen = data[i + 13] | (data[i + 14] << 8) | (data[i + 15] << 16) | (data[i + 16] << 24);
//console.log(name, flen);
i += 17;
files[name] = data.subarray(i, i + flen);
i += flen;
}
window.hog = files;
//haveHog(files); //{data:data, files:files, get:function(name){var f=this.files[name];if (!f) throw new Exception('Not found: ' + name);return this.data.subarray(f[0], f[0] + f[1]);}});
next();
}
xhr.send();
// Main part of this example, loads the module and uses it.
loadWebAssembly('playerweb.wasm')
.then(instance => {
var exports = instance.exports; // the exports of that instance
exports.__post_instantiate();
window.playerweb = exports;
window.playerwebMem = new Uint8Array(env.memory.buffer);
window.playerwebMem16s = new Int16Array(env.memory.buffer);
next();
}
);
var songs = ['descent.hmp', 'briefing.hmp', 'game0.hmp',
'game1.hmp', 'game2.hmp', 'game3.hmp', 'game4.hmp'];
var songIdx = 0;
function next() {
if (!window.hog || !window.playerweb)
return;
if (!play(songs[songIdx]))
return;
if (++songIdx == songs.length)
songIdx = 0;
}
function getStr(arr, ofs) {
var i = ofs;
while (arr[i])
i++;
return new TextDecoder().decode(arr.subarray(ofs, i));
}
function getBankFiles(fn) {
var sng = new TextDecoder().decode(hog['descent.sng']);
var m = sng.match(new RegExp('(?:^|\n)' + fn.replace(/[.]/g, '[.]') + '\\s+(\\S+)\\s+(\\S+)'));
return m && [m[1], m[2]];
}
function play(fn) {
audioInit();
if (!audioContext) {
console.log('no audio.');
return;
}
var song = hog[fn.slice(0, -1) + 'q'] || hog[fn];
if (!song)
throw new Error('song not found');
var bnks = getBankFiles(fn) || ['melodic.bnk', 'drum.bnk'];
var melo, drum;
if (!bnks || !(melo = hog[bnks[0]]) || !(drum = hog[bnks[1]]))
throw new Error('banks not found');
console.log('playing ' + fn + '...');
//playerweb._playerweb_stop();
playerwebMem.set(song, playerweb._song_data.value);
playerwebMem.set(melo, playerweb._melobnk_data.value);
playerwebMem.set(drum, playerweb._drumbnk_data.value);
//console.log(song.length, melo.length, drum.length);
var ret = playerweb._playerweb_play(song.length, melo.length, drum.length, 1);
if (ret) {
console.log('play failed', ret, getStr(playerwebMem, ret));
audioContext && audioContext.close();
audioContext = null;
return;
}
nextBuf();
nextBuf();
console.log('play started!');
return true;
}
var audioContext, rate, samps, time;
function audioInit() {
if (audioContext)
audioContext.close();
audioContext = new AudioContext();
if (audioContext.state == 'suspended') {
audioContext.close();
audioContext = null;
return;
}
rate = audioContext.sampleRate;
samps = 48000 >> 3;
time = 0;
playerweb._playerweb_init(rate);
console.log('playerweb init done');
}
function fillBuf(myBuffer) {
let ch0 = myBuffer.getChannelData(0);
let ch1 = myBuffer.getChannelData(1);
let ofs = playerweb._playerweb_gen(samps * 2) >> 1;
if (!ofs)
throw new Error("playerweb: " + playerweb._playerweb_get_last_rc());
let sndBuf = playerwebMem16s;
//let sndBuf = playerwebMem16s.subarray(ofs, ofs + samps * 2);
//console.log(sndBuf);
for (var i = 0, p = ofs; i < samps; i++, p += 2) {
ch0[i] = sndBuf[p] / 32768;
ch1[i] = sndBuf[p + 1] / 32768;
}
//onsole.log(ofs);
/*
for (let sampleNumber = 0 ; sampleNumber < samps ; sampleNumber++) {
ch0[sampleNumber] = generateSample(sampleNumber);
ch1[sampleNumber] = generateSample(sampleNumber);
}
*/
}
let freeBufs = [];
function nextBuf() {
let myBuffer = freeBufs.length ? freeBufs.pop() : audioContext.createBuffer(2, samps, rate);
fillBuf(myBuffer);
let src = audioContext.createBufferSource();
src.buffer = myBuffer;
src.connect(audioContext.destination);
src.onended = function() { freeBufs.push(this.buffer); nextBuf(); }
src.start(time);
time += samps / rate;
}
//next();
//next();
</script>