forked from wothke/websid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtinyrsid_adapter.js
409 lines (364 loc) · 13.3 KB
/
tinyrsid_adapter.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/*
tinyrsid_adapter.js: Adapts Tiny'R'Sid backend to generic WebAudio/ScriptProcessor player.
version 1.02
Copyright (C) 2020 Juergen Wothke
LICENSE
This software is licensed under a CC BY-NC-SA
(http://creativecommons.org/licenses/by-nc-sa/4.0/).
*/
SIDBackendAdapter = (function(){ var $this = function (basicROM, charROM, kernalROM, nextFrameCB) {
$this.base.call(this, backend_SID.Module, 2); // use stereo (for the benefit of multi-SID songs)
this.playerSampleRate;
this._scopeEnabled= false;
this._chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
this._ROM_SIZE= 0x2000;
this._CHAR_ROM_SIZE= 0x1000;
this._nextFrameCB= (typeof nextFrameCB == 'undefined') ? this.nopCB : nextFrameCB;
this._basicROM= this.base64DecodeROM(basicROM, this._ROM_SIZE);
this._charROM= this.base64DecodeROM(charROM, this._CHAR_ROM_SIZE);
this._kernalROM= this.base64DecodeROM(kernalROM, this._ROM_SIZE);
this._digiShownLabel= "";
this._digiShownRate= 0;
this.resetDigiMeta();
};
// TinyRSid's sample buffer contains 2-byte (signed short) sample data
// for 1 channel
extend(EmsHEAP16BackendAdapter, $this, {
nopCB: function() {
},
resetDigiMeta: function() {
this._digiTypes= {};
this._digiRate= 0;
this._digiBatches= 0;
this._digiEmuCalls= 0;
},
enableScope: function(enable) {
this._scopeEnabled= enable;
},
getAudioBuffer: function() {
var ptr= this.Module.ccall('getSoundBuffer', 'number');
return ptr>>1; // 16 bit samples
},
getAudioBufferLength: function() {
var len= this.Module.ccall('getSoundBufferLen', 'number');
return len;
},
printMemDump: function(name, startAddr, endAddr) { // util for debugging
var text= "const unsigned char "+name+"[] =\n{\n";
var line= "";
var j= 0;
for (var i= 0; i<(endAddr-startAddr+1); i++) {
var d= this.Module.ccall('getRAM', 'number', ['number'], [startAddr+i]);
line += "0x"+(("00" + d.toString(16)).substr(-2).toUpperCase())+", ";
if (j == 11) {
text+= (line + "\n");
line= "";
j= 0;
}else {
j++;
}
}
text+= (j?(line+"\n"):"")+"}\n";
console.log(text);
},
updateDigiMeta: function() {
// get a "not so jumpy" status describing digi output
var dTypeStr= this.getExtAsciiString(this.Module.ccall('getDigiTypeDesc', 'number'));
var dRate= this.Module.ccall('getDigiRate', 'number');
// "computeAudioSamples" correspond to 50/60Hz, i.e. to show some
// status for at least half a second, labels should only be updated every
// 25/30 calls..
if (!isNaN(dRate) && dRate) {
this._digiBatches++;
this._digiRate+= dRate;
this._digiTypes[dTypeStr]= 1; // collect labels
}
this._digiEmuCalls++;
if (this._digiEmuCalls == 20) {
this._digiShownLabel= "";
if (!this._digiBatches) {
this._digiShownRate= 0;
} else {
this._digiShownRate= Math.round(this._digiRate/this._digiBatches);
const arr = Object.keys(this._digiTypes).sort();
for (const key of arr) {
if (key.length && (key != "NONE"))
this._digiShownLabel+= (this._digiShownLabel.length ? "&"+key : key);
}
}
this.resetDigiMeta();
}
},
computeAudioSamples: function() {
if (typeof window.sid_measure_runs == 'undefined' || !window.sid_measure_runs) {
window.sid_measure_sum= 0;
window.sid_measure_runs= 0;
}
this._nextFrameCB(this); // used for "interactive mode"
var t = performance.now();
// console.profile(); // if compiled using "emcc.bat --profiling"
var len= this.Module.ccall('computeAudioSamples', 'number');
if (len <= 0) {
this.resetDigiMeta();
return 1; // >0 means "end song"
}
// console.profileEnd();
window.sid_measure_sum+= performance.now() - t;
if (window.sid_measure_runs++ == 100) {
window.sid_measure = window.sid_measure_sum/window.sid_measure_runs;
// console.log("time; " + window.sid_measure_sum/window.sid_measure_runs);
window.sid_measure_sum = window.sid_measure_runs = 0;
if (typeof window.sid_measure_avg_runs == 'undefined' || !window.sid_measure_avg_runs) {
window.sid_measure_avg_sum= window.sid_measure;
window.sid_measure_avg_runs= 1;
} else {
window.sid_measure_avg_sum+= window.sid_measure;
window.sid_measure_avg_runs+= 1;
}
window.sid_measure_avg= window.sid_measure_avg_sum/window.sid_measure_avg_runs;
}
this.updateDigiMeta();
return 0;
},
getPathAndFilename: function(filename) {
return ['/', filename];
},
registerFileData: function(pathFilenameArray, data) {
return 0; // FS not used in Tiny'R'Sid
},
loadMusicData: function(sampleRate, path, filename, data, options) {
var buf = this.Module._malloc(data.length);
this.Module.HEAPU8.set(data, buf);
var basicBuf= 0;
if (this._basicROM) { basicBuf = this.Module._malloc(this._ROM_SIZE); this.Module.HEAPU8.set(this._basicROM, basicBuf);}
var charBuf= 0;
if (this._charROM) { charBuf = this.Module._malloc(this._CHAR_ROM_SIZE); this.Module.HEAPU8.set(this._charROM, charBuf);}
var kernalBuf= 0;
if (this._kernalROM) { kernalBuf = this.Module._malloc(this._ROM_SIZE); this.Module.HEAPU8.set(this._kernalROM, kernalBuf);}
// try to use native sample rate to avoid resampling
this.playerSampleRate= (typeof window._gPlayerAudioCtx == 'undefined') ? 0 : window._gPlayerAudioCtx.sampleRate;
var isMus= filename.endsWith(".mus") || filename.endsWith(".str"); // Compute! Sidplayer file (stereo files not supported)
var ret = this.Module.ccall('loadSidFile', 'number', ['number', 'number', 'number', 'number', 'string', 'number', 'number', 'number'],
[isMus, buf, data.length, this.playerSampleRate, filename, basicBuf, charBuf, kernalBuf]);
if (kernalBuf) this.Module._free(kernalBuf);
if (charBuf) this.Module._free(charBuf);
if (basicBuf) this.Module._free(basicBuf);
this.Module._free(buf);
if (ret == 0) {
this.playerSampleRate = this.Module.ccall('getSampleRate', 'number');
this.resetSampleRate(sampleRate, this.playerSampleRate);
}
return ret;
},
evalTrackOptions: function(options) {
if (typeof options.timeout != 'undefined') {
ScriptNodePlayer.getInstance().setPlaybackTimeout(options.timeout*1000);
}
var traceSID= this._scopeEnabled;
if (typeof options.traceSID != 'undefined') {
traceSID= options.traceSID;
}
if (typeof options.track == 'undefined') {
options.track= -1;
}
this.resetDigiMeta();
return this.Module.ccall('playTune', 'number', ['number', 'number'], [options.track, traceSID]);
},
teardown: function() {
// nothing to do
},
getSongInfoMeta: function() {
return {
loadAddr: Number,
playSpeed: Number,
maxSubsong: Number,
actualSubsong: Number,
songName: String,
songAuthor: String,
songReleased: String
};
},
getExtAsciiString: function(heapPtr) {
// Pointer_stringify cannot be used here since UTF-8 parsing
// messes up original extASCII content
var text="";
for (var j= 0; j<32; j++) {
var b= this.Module.HEAP8[heapPtr+j] & 0xff;
if(b ==0) break;
if(b < 128){
text = text + String.fromCharCode(b);
} else {
text = text + "&#" + b + ";";
}
}
return text;
},
updateSongInfo: function(filename, result) {
// get song infos (so far only use some top level module infos)
var numAttr= 7;
var ret = this.Module.ccall('getMusicInfo', 'number');
var array = this.Module.HEAP32.subarray(ret>>2, (ret>>2)+7);
result.loadAddr= this.Module.HEAP32[((array[0])>>2)]; // i32
result.playSpeed= this.Module.HEAP32[((array[1])>>2)]; // i32
result.maxSubsong= this.Module.HEAP8[(array[2])]; // i8
result.actualSubsong= this.Module.HEAP8[(array[3])]; // i8
result.songName= this.getExtAsciiString(array[4]);
result.songAuthor= this.getExtAsciiString(array[5]);
result.songReleased= this.getExtAsciiString(array[6]);
},
// C64 emu specific accessors (that might be useful in GUI)
isSID6581: function() {
return this.Module.ccall('envIsSID6581', 'number');
},
setSID6581: function(is6581) {
this.Module.ccall('envSetSID6581', 'number', ['number'], [is6581]);
},
isNTSC: function() {
return this.Module.ccall('envIsNTSC', 'number');
},
setNTSC: function(ntsc) {
this.Module.ccall('envSetNTSC', 'number', ['number'], [ntsc]);
},
// To activate the below output a song must be started with the "traceSID" option set to 1:
// At any given moment the below getters will then correspond to the output of getAudioBuffer
// and what has last been generated by computeAudioSamples. They expose some of the respective
// underlying internal SID state (the "filter" is NOT reflected in this data).
getNumberTraceStreams: function() {
return this.Module.ccall('getNumberTraceStreams', 'number');
},
getTraceStreams: function() {
var result= [];
var n= this.getNumberTraceStreams();
var ret = this.Module.ccall('getTraceStreams', 'number');
var array = this.Module.HEAP32.subarray(ret>>2, (ret>>2)+n);
for (var i= 0; i<n; i++) {
result.push(array[i] >> 1); // pointer to int16 array
}
return result;
},
// XXX FIXME UNUSED?
readFloatTrace: function(buffer, idx) {
return (this.Module.HEAP16[buffer+idx])/0x8000;
},
// perf optimization: XXX FIXME UNUSED?
getCopiedScopeStream: function(input, len, output) {
for(var i= 0; i<len; i++){
output[i]= this.Module.HEAP16[input+i]; // will be scaled later anyway.. avoid the extra division here /0x8000;
}
return len;
},
/**
* This just queries the *current* state of the emulator. It
* is less precisely correlated to the music that is currently playing (than the above
* buffers), i.e. it represents the state *after* the last emulator call (respective data
* may not yet have been fed to WebAudio or if it has already been fed then
* WebAudio may not yet be playing it yet). The lag should normally not be very large
* (<0.2s) and when using it for display purposes it would be hard to see a difference anyway.
*/
getRegisterSID: function(offset) {
return this.Module.ccall('getRegisterSID', 'number', ['number'], [offset]);
},
getRAM: function(offset) {
return this.Module.ccall('getRAM', 'number', ['number'], [offset]);
},
setRAM: function(offset, value) {
this.Module.ccall('setRAM', 'number', ['number', 'number'], [offset, value]);
},
setRegisterSID: function(offset, value) {
this.Module.ccall('setRegisterSID', 'number', ['number', 'number'], [offset, value]);
},
/**
* Diagnostics digi-samples (if any).
*/
getDigiType: function() {
return this.Module.ccall('getDigiType', 'number');
},
getDigiTypeDesc: function() {
return this._digiShownLabel;
},
getDigiRate: function() {
return this._digiShownRate;
},
enableVoice: function(sidIdx, voice, on) {
this.Module.ccall('enableVoice', 'number', ['number', 'number', 'number'], [sidIdx, voice, on]);
},
/*
* @deprecated APIs below - use getTraceStreams/getNumberTraceStreams instead
*/
// disable voices (0-3) by clearing respective bit
enableVoices: function(mask) {
this.Module.ccall('enableVoices', 'number', ['number'], [mask]);
},
getBufferVoice1: function() {
var ptr= this.Module.ccall('getBufferVoice1', 'number');
return ptr>>1; // 16 bit samples
},
getBufferVoice2: function() {
var ptr= this.Module.ccall('getBufferVoice2', 'number');
return ptr>>1; // 16 bit samples
},
getBufferVoice3: function() {
var ptr= this.Module.ccall('getBufferVoice3', 'number');
return ptr>>1; // 16 bit samples
},
getBufferVoice4: function() {
var ptr= this.Module.ccall('getBufferVoice4', 'number');
return ptr>>1; // 16 bit samples
},
// base64 decoding util
findChar: function(str, c) {
for (var i= 0; i<str.length; i++) {
if (str.charAt(i) == c) {
return i;
}
}
return -1;
},
alphanumeric: function(inputtxt) {
var letterNumber = /^[0-9a-zA-Z]+$/;
return inputtxt.match(letterNumber);
},
is_base64: function(c) {
return (this.alphanumeric(""+c) || (c == '+') || (c == '/'));
},
base64DecodeROM: function(encoded, romSize) {
if (typeof encoded == 'undefined') return 0;
var in_len= encoded.length;
var i= j= in_= 0;
var arr4= new Array(4);
var arr3= new Array(3);
var ret= new Uint8Array(romSize);
var ri= 0;
while (in_len-- && ( encoded.charAt(in_) != '=') && this.is_base64(encoded.charAt(in_))) {
arr4[i++]= encoded.charAt(in_); in_++;
if (i ==4) {
for (i = 0; i <4; i++) {
arr4[i] = this.findChar(this._chars, arr4[i]);
}
arr3[0] = ( arr4[0] << 2 ) + ((arr4[1] & 0x30) >> 4);
arr3[1] = ((arr4[1] & 0xf) << 4) + ((arr4[2] & 0x3c) >> 2);
arr3[2] = ((arr4[2] & 0x3) << 6) + arr4[3];
for (i = 0; (i < 3); i++) {
var val= arr3[i];
ret[ri++]= val;
}
i = 0;
}
}
if (i) {
for (j = 0; j < i; j++) {
arr4[j] = this.findChar(this._chars, arr4[j]);
}
arr3[0] = (arr4[0] << 2) + ((arr4[1] & 0x30) >> 4);
arr3[1] = ((arr4[1] & 0xf) << 4) + ((arr4[2] & 0x3c) >> 2);
for (j = 0; (j < i - 1); j++) {
var val= arr3[j];
ret[ri++]= val;
}
}
if (ri == romSize) {
return ret;
}
return 0;
},
}); return $this; })();