-
Notifications
You must be signed in to change notification settings - Fork 1
/
gbemu-part1.js
1351 lines (1305 loc) · 50.1 KB
/
gbemu-part1.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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Portions copyright 2013 Google, Inc
// Copyright (C) 2010 - 2012 Grant Galitz
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 2 as
// published by the Free Software Foundation.
// The full license is available at http://www.gnu.org/licenses/gpl.html
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU General Public License for more details.
// The code has been adapted for use as a benchmark by Google.
var GameboyBenchmark = new BenchmarkSuite('Gameboy', [26288412],
[new Benchmark('Gameboy',
false,
false,
20,
runGameboy,
setupGameboy,
tearDownGameboy,
null,
4)]);
var decoded_gameboy_rom = null;
function setupGameboy() {
// Check if all the types required by the code are supported.
// If not, throw exception and quit.
if (!(typeof Uint8Array != "undefined" &&
typeof Int8Array != "undefined" &&
typeof Float32Array != "undefined" &&
typeof Int32Array != "undefined") ) {
throw "TypedArrayUnsupported";
}
decoded_gameboy_rom = base64_decode(gameboy_rom);
rom = null;
}
function runGameboy() {
start(new GameBoyCanvas(), decoded_gameboy_rom);
gameboy.instructions = 0;
gameboy.totalInstructions = 250000;
while (gameboy.instructions <= gameboy.totalInstructions) {
gameboy.run();
GameBoyAudioNode.run();
}
resetGlobalVariables();
}
function tearDownGameboy() {
decoded_gameboy_rom = null;
expectedGameboyStateStr = null;
}
var expectedGameboyStateStr =
'{"registerA":160,"registerB":255,"registerC":255,"registerE":11,' +
'"registersHL":51600,"programCounter":24309,"stackPointer":49706,' +
'"sumROM":10171578,"sumMemory":3435856,"sumMBCRam":234598,"sumVRam":0}';
// Start of browser emulation.
var GameBoyWindow = { };
function GameBoyContext() {
this.createBuffer = function() {
return new Buffer();
}
this.createImageData = function (w, h) {
var result = {};
// The following line was updated since Octane 1.0 to avoid OOB access.
result.data = new Uint8Array(w * h * 4);
return result;
}
this.putImageData = function (buffer, x, y) {
var sum = 0;
for (var i = 0; i < buffer.data.length; i++) {
sum += i * buffer.data[i];
sum = sum % 1000;
}
}
this.drawImage = function () { }
};
function GameBoyCanvas() {
this.getContext = function() {
return new GameBoyContext();
}
this.width = 160;
this.height = 144;
this.style = { visibility: "visibile" };
}
function cout(message, colorIndex) {
}
function clear_terminal() {
}
var GameBoyAudioNode = {
bufferSize : 0,
onaudioprocess : null ,
connect : function () {},
run: function() {
var event = {outputBuffer : this.outputBuffer};
this.onaudioprocess(event);
}
};
function GameBoyAudioContext () {
this.createBufferSource = function() {
return { noteOn : function () {}, connect : function() {}};
}
this.sampleRate = 48000;
this.destination = {}
this.createBuffer = function (channels, len, sampleRate) {
return { gain : 1,
numberOfChannels : 1,
length : 1,
duration : 0.000020833333110203966,
sampleRate : 48000}
}
this.createJavaScriptNode = function (bufferSize, inputChannels, outputChannels) {
GameBoyAudioNode.bufferSize = bufferSize;
GameBoyAudioNode.outputBuffer = {
getChannelData : function (i) {return this.channelData[i];},
channelData : []
};
for (var i = 0; i < outputChannels; i++) {
GameBoyAudioNode.outputBuffer.channelData[i] = new Float32Array(bufferSize);
}
return GameBoyAudioNode;
}
}
var mock_date_time_counter = 0;
function new_Date() {
return {
getTime: function() {
mock_date_time_counter += 16;
return mock_date_time_counter;
}
};
}
// End of browser emulation.
// Start of helper functions.
function checkFinalState() {
function sum(a) {
var result = 0;
for (var i = 0; i < a.length; i++) {
result += a[i];
}
return result;
}
var state = {
registerA: gameboy.registerA,
registerB: gameboy.registerB,
registerC: gameboy.registerC,
registerE: gameboy.registerE,
registerF: gameboy.registerF,
registersHL: gameboy.registersHL,
programCounter: gameboy.programCounter,
stackPointer: gameboy.stackPointer,
sumROM : sum(gameboy.fromTypedArray(gameboy.ROM)),
sumMemory: sum(gameboy.fromTypedArray(gameboy.memory)),
sumMBCRam: sum(gameboy.fromTypedArray(gameboy.MBCRam)),
sumVRam: sum(gameboy.fromTypedArray(gameboy.VRam))
}
var stateStr = JSON.stringify(state);
if (typeof expectedGameboyStateStr != "undefined") {
if (stateStr != expectedGameboyStateStr) {
alert("Incorrect final state of processor:\n" +
" actual " + stateStr + "\n" +
" expected " + expectedGameboyStateStr);
}
} else {
alert(stateStr);
}
}
function resetGlobalVariables () {
//Audio API Event Handler:
audioContextHandle = null;
audioNode = null;
audioSource = null;
launchedContext = false;
audioContextSampleBuffer = [];
resampled = [];
webAudioMinBufferSize = 15000;
webAudioMaxBufferSize = 25000;
webAudioActualSampleRate = 44100;
XAudioJSSampleRate = 0;
webAudioMono = false;
XAudioJSVolume = 1;
resampleControl = null;
audioBufferSize = 0;
resampleBufferStart = 0;
resampleBufferEnd = 0;
resampleBufferSize = 2;
gameboy = null; //GameBoyCore object.
gbRunInterval = null; //GameBoyCore Timer
}
// End of helper functions.
// Original code from Grant Galitz follows.
// Modifications by Google are marked in comments.
// Start of js/other/base64.js file.
var toBase64 = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z",
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z",
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "+" , "/", "="];
var fromBase64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
function base64(data) {
try {
// The following line was modified for benchmarking:
var base64 = GameBoyWindow.btoa(data); //Use this native function when it's available, as it's a magnitude faster than the non-native code below.
}
catch (error) {
//Defaulting to non-native base64 encoding...
var base64 = "";
var dataLength = data.length;
if (dataLength > 0) {
var bytes = [0, 0, 0];
var index = 0;
var remainder = dataLength % 3;
while (data.length % 3 > 0) {
//Make sure we don't do fuzzy math in the next loop...
data[data.length] = " ";
}
while (index < dataLength) {
//Keep this loop small for speed.
bytes = [data.charCodeAt(index++) & 0xFF, data.charCodeAt(index++) & 0xFF, data.charCodeAt(index++) & 0xFF];
base64 += toBase64[bytes[0] >> 2] + toBase64[((bytes[0] & 0x3) << 4) | (bytes[1] >> 4)] + toBase64[((bytes[1] & 0xF) << 2) | (bytes[2] >> 6)] + toBase64[bytes[2] & 0x3F];
}
if (remainder > 0) {
//Fill in the padding and recalulate the trailing six-bit group...
base64[base64.length - 1] = "=";
if (remainder == 2) {
base64[base64.length - 2] = "=";
base64[base64.length - 3] = toBase64[(bytes[0] & 0x3) << 4];
}
else {
base64[base64.length - 2] = toBase64[(bytes[1] & 0xF) << 2];
}
}
}
}
return base64;
}
function base64_decode(data) {
try {
// The following line was modified for benchmarking:
var decode64 = GameBoyWindow.atob(data); //Use this native function when it's available, as it's a magnitude faster than the non-native code below.
}
catch (error) {
//Defaulting to non-native base64 decoding...
var decode64 = "";
var dataLength = data.length;
if (dataLength > 3 && dataLength % 4 == 0) {
var sixbits = [0, 0, 0, 0]; //Declare this out of the loop, to speed up the ops.
var index = 0;
while (index < dataLength) {
//Keep this loop small for speed.
sixbits = [fromBase64.indexOf(data.charAt(index++)), fromBase64.indexOf(data.charAt(index++)), fromBase64.indexOf(data.charAt(index++)), fromBase64.indexOf(data.charAt(index++))];
decode64 += String.fromCharCode((sixbits[0] << 2) | (sixbits[1] >> 4)) + String.fromCharCode(((sixbits[1] & 0x0F) << 4) | (sixbits[2] >> 2)) + String.fromCharCode(((sixbits[2] & 0x03) << 6) | sixbits[3]);
}
//Check for the '=' character after the loop, so we don't hose it up.
if (sixbits[3] >= 0x40) {
decode64.length -= 1;
if (sixbits[2] >= 0x40) {
decode64.length -= 1;
}
}
}
}
return decode64;
}
function to_little_endian_dword(str) {
return to_little_endian_word(str) + String.fromCharCode((str >> 16) & 0xFF, (str >> 24) & 0xFF);
}
function to_little_endian_word(str) {
return to_byte(str) + String.fromCharCode((str >> 8) & 0xFF);
}
function to_byte(str) {
return String.fromCharCode(str & 0xFF);
}
function arrayToBase64(arrayIn) {
var binString = "";
var length = arrayIn.length;
for (var index = 0; index < length; ++index) {
if (typeof arrayIn[index] == "number") {
binString += String.fromCharCode(arrayIn[index]);
}
}
return base64(binString);
}
function base64ToArray(b64String) {
var binString = base64_decode(b64String);
var outArray = [];
var length = binString.length;
for (var index = 0; index < length;) {
outArray.push(binString.charCodeAt(index++) & 0xFF);
}
return outArray;
}
// End of js/other/base64.js file.
// Start of js/other/resampler.js file.
//JavaScript Audio Resampler (c) 2011 - Grant Galitz
function Resampler(fromSampleRate, toSampleRate, channels, outputBufferSize, noReturn) {
this.fromSampleRate = fromSampleRate;
this.toSampleRate = toSampleRate;
this.channels = channels | 0;
this.outputBufferSize = outputBufferSize;
this.noReturn = !!noReturn;
this.initialize();
}
Resampler.prototype.initialize = function () {
//Perform some checks:
if (this.fromSampleRate > 0 && this.toSampleRate > 0 && this.channels > 0) {
if (this.fromSampleRate == this.toSampleRate) {
//Setup a resampler bypass:
this.resampler = this.bypassResampler; //Resampler just returns what was passed through.
this.ratioWeight = 1;
}
else {
//Setup the interpolation resampler:
this.compileInterpolationFunction();
this.resampler = this.interpolate; //Resampler is a custom quality interpolation algorithm.
this.ratioWeight = this.fromSampleRate / this.toSampleRate;
this.tailExists = false;
this.lastWeight = 0;
this.initializeBuffers();
}
}
else {
throw(new Error("Invalid settings specified for the resampler."));
}
}
Resampler.prototype.compileInterpolationFunction = function () {
var toCompile = "var bufferLength = Math.min(buffer.length, this.outputBufferSize);\
if ((bufferLength % " + this.channels + ") == 0) {\
if (bufferLength > 0) {\
var ratioWeight = this.ratioWeight;\
var weight = 0;";
for (var channel = 0; channel < this.channels; ++channel) {
toCompile += "var output" + channel + " = 0;"
}
toCompile += "var actualPosition = 0;\
var amountToNext = 0;\
var alreadyProcessedTail = !this.tailExists;\
this.tailExists = false;\
var outputBuffer = this.outputBuffer;\
var outputOffset = 0;\
var currentPosition = 0;\
do {\
if (alreadyProcessedTail) {\
weight = ratioWeight;";
for (channel = 0; channel < this.channels; ++channel) {
toCompile += "output" + channel + " = 0;"
}
toCompile += "}\
else {\
weight = this.lastWeight;";
for (channel = 0; channel < this.channels; ++channel) {
toCompile += "output" + channel + " = this.lastOutput[" + channel + "];"
}
toCompile += "alreadyProcessedTail = true;\
}\
while (weight > 0 && actualPosition < bufferLength) {\
amountToNext = 1 + actualPosition - currentPosition;\
if (weight >= amountToNext) {";
for (channel = 0; channel < this.channels; ++channel) {
toCompile += "output" + channel + " += buffer[actualPosition++] * amountToNext;"
}
toCompile += "currentPosition = actualPosition;\
weight -= amountToNext;\
}\
else {";
for (channel = 0; channel < this.channels; ++channel) {
toCompile += "output" + channel + " += buffer[actualPosition" + ((channel > 0) ? (" + " + channel) : "") + "] * weight;"
}
toCompile += "currentPosition += weight;\
weight = 0;\
break;\
}\
}\
if (weight == 0) {";
for (channel = 0; channel < this.channels; ++channel) {
toCompile += "outputBuffer[outputOffset++] = output" + channel + " / ratioWeight;"
}
toCompile += "}\
else {\
this.lastWeight = weight;";
for (channel = 0; channel < this.channels; ++channel) {
toCompile += "this.lastOutput[" + channel + "] = output" + channel + ";"
}
toCompile += "this.tailExists = true;\
break;\
}\
} while (actualPosition < bufferLength);\
return this.bufferSlice(outputOffset);\
}\
else {\
return (this.noReturn) ? 0 : [];\
}\
}\
else {\
throw(new Error(\"Buffer was of incorrect sample length.\"));\
}";
this.interpolate = Function("buffer", toCompile);
}
Resampler.prototype.bypassResampler = function (buffer) {
if (this.noReturn) {
//Set the buffer passed as our own, as we don't need to resample it:
this.outputBuffer = buffer;
return buffer.length;
}
else {
//Just return the buffer passsed:
return buffer;
}
}
Resampler.prototype.bufferSlice = function (sliceAmount) {
if (this.noReturn) {
//If we're going to access the properties directly from this object:
return sliceAmount;
}
else {
//Typed array and normal array buffer section referencing:
try {
return this.outputBuffer.subarray(0, sliceAmount);
}
catch (error) {
try {
//Regular array pass:
this.outputBuffer.length = sliceAmount;
return this.outputBuffer;
}
catch (error) {
//Nightly Firefox 4 used to have the subarray function named as slice:
return this.outputBuffer.slice(0, sliceAmount);
}
}
}
}
Resampler.prototype.initializeBuffers = function () {
//Initialize the internal buffer:
try {
this.outputBuffer = new Float32Array(this.outputBufferSize);
this.lastOutput = new Float32Array(this.channels);
}
catch (error) {
this.outputBuffer = [];
this.lastOutput = [];
}
}
// End of js/other/resampler.js file.
// Start of js/other/XAudioServer.js file.
/*Initialize here first:
Example:
Stereo audio with a sample rate of 70 khz, a minimum buffer of 15000 samples total, a maximum buffer of 25000 samples total and a starting volume level of 1.
var parentObj = this;
this.audioHandle = new XAudioServer(2, 70000, 15000, 25000, function (sampleCount) {
return parentObj.audioUnderRun(sampleCount);
}, 1);
The callback is passed the number of samples requested, while it can return any number of samples it wants back.
*/
function XAudioServer(channels, sampleRate, minBufferSize, maxBufferSize, underRunCallback, volume) {
this.audioChannels = (channels == 2) ? 2 : 1;
webAudioMono = (this.audioChannels == 1);
XAudioJSSampleRate = (sampleRate > 0 && sampleRate <= 0xFFFFFF) ? sampleRate : 44100;
webAudioMinBufferSize = (minBufferSize >= (samplesPerCallback << 1) && minBufferSize < maxBufferSize) ? (minBufferSize & ((webAudioMono) ? 0xFFFFFFFF : 0xFFFFFFFE)) : (samplesPerCallback << 1);
webAudioMaxBufferSize = (Math.floor(maxBufferSize) > webAudioMinBufferSize + this.audioChannels) ? (maxBufferSize & ((webAudioMono) ? 0xFFFFFFFF : 0xFFFFFFFE)) : (minBufferSize << 1);
this.underRunCallback = (typeof underRunCallback == "function") ? underRunCallback : function () {};
XAudioJSVolume = (volume >= 0 && volume <= 1) ? volume : 1;
this.audioType = -1;
this.mozAudioTail = [];
this.audioHandleMoz = null;
this.audioHandleFlash = null;
this.flashInitialized = false;
this.mozAudioFound = false;
this.initializeAudio();
}
XAudioServer.prototype.MOZWriteAudio = function (buffer) {
//mozAudio:
this.MOZWriteAudioNoCallback(buffer);
this.MOZExecuteCallback();
}
XAudioServer.prototype.MOZWriteAudioNoCallback = function (buffer) {
//mozAudio:
this.writeMozAudio(buffer);
}
XAudioServer.prototype.callbackBasedWriteAudio = function (buffer) {
//Callback-centered audio APIs:
this.callbackBasedWriteAudioNoCallback(buffer);
this.callbackBasedExecuteCallback();
}
XAudioServer.prototype.callbackBasedWriteAudioNoCallback = function (buffer) {
//Callback-centered audio APIs:
var length = buffer.length;
for (var bufferCounter = 0; bufferCounter < length && audioBufferSize < webAudioMaxBufferSize;) {
audioContextSampleBuffer[audioBufferSize++] = buffer[bufferCounter++];
}
}
/*Pass your samples into here!
Pack your samples as a one-dimenional array
With the channel samplea packed uniformly.
examples:
mono - [left, left, left, left]
stereo - [left, right, left, right, left, right, left, right]
*/
XAudioServer.prototype.writeAudio = function (buffer) {
if (this.audioType == 0) {
this.MOZWriteAudio(buffer);
}
else if (this.audioType == 1) {
this.callbackBasedWriteAudio(buffer);
}
else if (this.audioType == 2) {
if (this.checkFlashInit() || launchedContext) {
this.callbackBasedWriteAudio(buffer);
}
else if (this.mozAudioFound) {
this.MOZWriteAudio(buffer);
}
}
}
/*Pass your samples into here if you don't want automatic callback calling:
Pack your samples as a one-dimenional array
With the channel samplea packed uniformly.
examples:
mono - [left, left, left, left]
stereo - [left, right, left, right, left, right, left, right]
Useful in preventing infinite recursion issues with calling writeAudio inside your callback.
*/
XAudioServer.prototype.writeAudioNoCallback = function (buffer) {
if (this.audioType == 0) {
this.MOZWriteAudioNoCallback(buffer);
}
else if (this.audioType == 1) {
this.callbackBasedWriteAudioNoCallback(buffer);
}
else if (this.audioType == 2) {
if (this.checkFlashInit() || launchedContext) {
this.callbackBasedWriteAudioNoCallback(buffer);
}
else if (this.mozAudioFound) {
this.MOZWriteAudioNoCallback(buffer);
}
}
}
//Developer can use this to see how many samples to write (example: minimum buffer allotment minus remaining samples left returned from this function to make sure maximum buffering is done...)
//If -1 is returned, then that means metric could not be done.
XAudioServer.prototype.remainingBuffer = function () {
if (this.audioType == 0) {
//mozAudio:
return this.samplesAlreadyWritten - this.audioHandleMoz.mozCurrentSampleOffset();
}
else if (this.audioType == 1) {
//WebKit Audio:
return (((resampledSamplesLeft() * resampleControl.ratioWeight) >> (this.audioChannels - 1)) << (this.audioChannels - 1)) + audioBufferSize;
}
else if (this.audioType == 2) {
if (this.checkFlashInit() || launchedContext) {
//Webkit Audio / Flash Plugin Audio:
return (((resampledSamplesLeft() * resampleControl.ratioWeight) >> (this.audioChannels - 1)) << (this.audioChannels - 1)) + audioBufferSize;
}
else if (this.mozAudioFound) {
//mozAudio:
return this.samplesAlreadyWritten - this.audioHandleMoz.mozCurrentSampleOffset();
}
}
//Default return:
return 0;
}
XAudioServer.prototype.MOZExecuteCallback = function () {
//mozAudio:
var samplesRequested = webAudioMinBufferSize - this.remainingBuffer();
if (samplesRequested > 0) {
this.writeMozAudio(this.underRunCallback(samplesRequested));
}
}
XAudioServer.prototype.callbackBasedExecuteCallback = function () {
//WebKit /Flash Audio:
var samplesRequested = webAudioMinBufferSize - this.remainingBuffer();
if (samplesRequested > 0) {
this.callbackBasedWriteAudioNoCallback(this.underRunCallback(samplesRequested));
}
}
//If you just want your callback called for any possible refill (Execution of callback is still conditional):
XAudioServer.prototype.executeCallback = function () {
if (this.audioType == 0) {
this.MOZExecuteCallback();
}
else if (this.audioType == 1) {
this.callbackBasedExecuteCallback();
}
else if (this.audioType == 2) {
if (this.checkFlashInit() || launchedContext) {
this.callbackBasedExecuteCallback();
}
else if (this.mozAudioFound) {
this.MOZExecuteCallback();
}
}
}
//DO NOT CALL THIS, the lib calls this internally!
XAudioServer.prototype.initializeAudio = function () {
try {
throw (new Error("Select initializeWebAudio case")); // Line added for benchmarking.
this.preInitializeMozAudio();
if (navigator.platform == "Linux i686") {
//Block out mozaudio usage for Linux Firefox due to moz bugs:
throw(new Error(""));
}
this.initializeMozAudio();
}
catch (error) {
try {
this.initializeWebAudio();
}
catch (error) {
try {
this.initializeFlashAudio();
}
catch (error) {
throw(new Error("Browser does not support real time audio output."));
}
}
}
}
XAudioServer.prototype.preInitializeMozAudio = function () {
//mozAudio - Synchronous Audio API
this.audioHandleMoz = new Audio();
this.audioHandleMoz.mozSetup(this.audioChannels, XAudioJSSampleRate);
this.samplesAlreadyWritten = 0;
var emptySampleFrame = (this.audioChannels == 2) ? [0, 0] : [0];
var prebufferAmount = 0;
if (navigator.platform != "MacIntel" && navigator.platform != "MacPPC") { //Mac OS X doesn't experience this moz-bug!
while (this.audioHandleMoz.mozCurrentSampleOffset() == 0) {
//Mozilla Audio Bugginess Workaround (Firefox freaks out if we don't give it a prebuffer under certain OSes):
prebufferAmount += this.audioHandleMoz.mozWriteAudio(emptySampleFrame);
}
var samplesToDoubleBuffer = prebufferAmount / this.audioChannels;
//Double the prebuffering for windows:
for (var index = 0; index < samplesToDoubleBuffer; index++) {
this.samplesAlreadyWritten += this.audioHandleMoz.mozWriteAudio(emptySampleFrame);
}
}
this.samplesAlreadyWritten += prebufferAmount;
webAudioMinBufferSize += this.samplesAlreadyWritten;
this.mozAudioFound = true;
}
XAudioServer.prototype.initializeMozAudio = function () {
//Fill in our own buffering up to the minimum specified:
this.writeMozAudio(getFloat32(webAudioMinBufferSize));
this.audioType = 0;
}
XAudioServer.prototype.initializeWebAudio = function () {
if (launchedContext) {
resetCallbackAPIAudioBuffer(webAudioActualSampleRate, samplesPerCallback);
this.audioType = 1;
}
else {
throw(new Error(""));
}
}
XAudioServer.prototype.initializeFlashAudio = function () {
var existingFlashload = document.getElementById("XAudioJS");
if (existingFlashload == null) {
var thisObj = this;
var mainContainerNode = document.createElement("div");
mainContainerNode.setAttribute("style", "position: fixed; bottom: 0px; right: 0px; margin: 0px; padding: 0px; border: none; width: 8px; height: 8px; overflow: hidden; z-index: -1000; ");
var containerNode = document.createElement("div");
containerNode.setAttribute("style", "position: static; border: none; width: 0px; height: 0px; visibility: hidden; margin: 8px; padding: 0px;");
containerNode.setAttribute("id", "XAudioJS");
mainContainerNode.appendChild(containerNode);
document.getElementsByTagName("body")[0].appendChild(mainContainerNode);
swfobject.embedSWF(
"XAudioJS.swf",
"XAudioJS",
"8",
"8",
"9.0.0",
"",
{},
{"allowscriptaccess":"always"},
{"style":"position: static; visibility: hidden; margin: 8px; padding: 0px; border: none"},
function (event) {
if (event.success) {
thisObj.audioHandleFlash = event.ref;
}
else {
thisObj.audioType = 1;
}
}
);
}
else {
this.audioHandleFlash = existingFlashload;
}
this.audioType = 2;
}
XAudioServer.prototype.changeVolume = function (newVolume) {
if (newVolume >= 0 && newVolume <= 1) {
XAudioJSVolume = newVolume;
if (this.checkFlashInit()) {
this.audioHandleFlash.changeVolume(XAudioJSVolume);
}
if (this.mozAudioFound) {
this.audioHandleMoz.volume = XAudioJSVolume;
}
}
}
//Moz Audio Buffer Writing Handler:
XAudioServer.prototype.writeMozAudio = function (buffer) {
var length = this.mozAudioTail.length;
if (length > 0) {
var samplesAccepted = this.audioHandleMoz.mozWriteAudio(this.mozAudioTail);
this.samplesAlreadyWritten += samplesAccepted;
this.mozAudioTail.splice(0, samplesAccepted);
}
length = Math.min(buffer.length, webAudioMaxBufferSize - this.samplesAlreadyWritten + this.audioHandleMoz.mozCurrentSampleOffset());
var samplesAccepted = this.audioHandleMoz.mozWriteAudio(buffer);
this.samplesAlreadyWritten += samplesAccepted;
for (var index = 0; length > samplesAccepted; --length) {
//Moz Audio wants us saving the tail:
this.mozAudioTail.push(buffer[index++]);
}
}
//Checks to see if the NPAPI Adobe Flash bridge is ready yet:
XAudioServer.prototype.checkFlashInit = function () {
if (!this.flashInitialized && this.audioHandleFlash && this.audioHandleFlash.initialize) {
this.flashInitialized = true;
this.audioHandleFlash.initialize(this.audioChannels, XAudioJSVolume);
resetCallbackAPIAudioBuffer(44100, samplesPerCallback);
}
return this.flashInitialized;
}
/////////END LIB
function getFloat32(size) {
try {
return new Float32Array(size);
}
catch (error) {
return new Array(size);
}
}
function getFloat32Flat(size) {
try {
var newBuffer = new Float32Array(size);
}
catch (error) {
var newBuffer = new Array(size);
var audioSampleIndice = 0;
do {
newBuffer[audioSampleIndice] = 0;
} while (++audioSampleIndice < size);
}
return newBuffer;
}
//Flash NPAPI Event Handler:
var samplesPerCallback = 2048; //Has to be between 2048 and 4096 (If over, then samples are ignored, if under then silence is added).
var outputConvert = null;
function audioOutputFlashEvent() { //The callback that flash calls...
resampleRefill();
return outputConvert();
}
function generateFlashStereoString() { //Convert the arrays to one long string for speed.
var copyBinaryStringLeft = "";
var copyBinaryStringRight = "";
for (var index = 0; index < samplesPerCallback && resampleBufferStart != resampleBufferEnd; ++index) {
//Sanitize the buffer:
copyBinaryStringLeft += String.fromCharCode(((Math.min(Math.max(resampled[resampleBufferStart++] + 1, 0), 2) * 0x3FFF) | 0) + 0x3000);
copyBinaryStringRight += String.fromCharCode(((Math.min(Math.max(resampled[resampleBufferStart++] + 1, 0), 2) * 0x3FFF) | 0) + 0x3000);
if (resampleBufferStart == resampleBufferSize) {
resampleBufferStart = 0;
}
}
return copyBinaryStringLeft + copyBinaryStringRight;
}
function generateFlashMonoString() { //Convert the array to one long string for speed.
var copyBinaryString = "";
for (var index = 0; index < samplesPerCallback && resampleBufferStart != resampleBufferEnd; ++index) {
//Sanitize the buffer:
copyBinaryString += String.fromCharCode(((Math.min(Math.max(resampled[resampleBufferStart++] + 1, 0), 2) * 0x3FFF) | 0) + 0x3000);
if (resampleBufferStart == resampleBufferSize) {
resampleBufferStart = 0;
}
}
return copyBinaryString;
}
//Audio API Event Handler:
var audioContextHandle = null;
var audioNode = null;
var audioSource = null;
var launchedContext = false;
var audioContextSampleBuffer = [];
var resampled = [];
var webAudioMinBufferSize = 15000;
var webAudioMaxBufferSize = 25000;
var webAudioActualSampleRate = 44100;
var XAudioJSSampleRate = 0;
var webAudioMono = false;
var XAudioJSVolume = 1;
var resampleControl = null;
var audioBufferSize = 0;
var resampleBufferStart = 0;
var resampleBufferEnd = 0;
var resampleBufferSize = 2;
function audioOutputEvent(event) { //Web Audio API callback...
var index = 0;
var buffer1 = event.outputBuffer.getChannelData(0);
var buffer2 = event.outputBuffer.getChannelData(1);
resampleRefill();
if (!webAudioMono) {
//STEREO:
while (index < samplesPerCallback && resampleBufferStart != resampleBufferEnd) {
buffer1[index] = resampled[resampleBufferStart++] * XAudioJSVolume;
buffer2[index++] = resampled[resampleBufferStart++] * XAudioJSVolume;
if (resampleBufferStart == resampleBufferSize) {
resampleBufferStart = 0;
}
}
}
else {
//MONO:
while (index < samplesPerCallback && resampleBufferStart != resampleBufferEnd) {
buffer2[index] = buffer1[index] = resampled[resampleBufferStart++] * XAudioJSVolume;
++index;
if (resampleBufferStart == resampleBufferSize) {
resampleBufferStart = 0;
}
}
}
//Pad with silence if we're underrunning:
while (index < samplesPerCallback) {
buffer2[index] = buffer1[index] = 0;
++index;
}
}
function resampleRefill() {
if (audioBufferSize > 0) {
//Resample a chunk of audio:
var resampleLength = resampleControl.resampler(getBufferSamples());
var resampledResult = resampleControl.outputBuffer;
for (var index2 = 0; index2 < resampleLength; ++index2) {
resampled[resampleBufferEnd++] = resampledResult[index2];
if (resampleBufferEnd == resampleBufferSize) {
resampleBufferEnd = 0;
}
if (resampleBufferStart == resampleBufferEnd) {
++resampleBufferStart;
if (resampleBufferStart == resampleBufferSize) {
resampleBufferStart = 0;
}
}
}
audioBufferSize = 0;
}
}
function resampledSamplesLeft() {
return ((resampleBufferStart <= resampleBufferEnd) ? 0 : resampleBufferSize) + resampleBufferEnd - resampleBufferStart;
}
function getBufferSamples() {
//Typed array and normal array buffer section referencing:
try {
return audioContextSampleBuffer.subarray(0, audioBufferSize);
}
catch (error) {
try {
//Regular array pass:
audioContextSampleBuffer.length = audioBufferSize;
return audioContextSampleBuffer;
}
catch (error) {
//Nightly Firefox 4 used to have the subarray function named as slice:
return audioContextSampleBuffer.slice(0, audioBufferSize);
}
}
}
//Initialize WebKit Audio /Flash Audio Buffer:
function resetCallbackAPIAudioBuffer(APISampleRate, bufferAlloc) {
audioContextSampleBuffer = getFloat32(webAudioMaxBufferSize);
audioBufferSize = webAudioMaxBufferSize;
resampleBufferStart = 0;
resampleBufferEnd = 0;
resampleBufferSize = Math.max(webAudioMaxBufferSize * Math.ceil(XAudioJSSampleRate / APISampleRate), samplesPerCallback) << 1;
if (webAudioMono) {
//MONO Handling:
resampled = getFloat32Flat(resampleBufferSize);
resampleControl = new Resampler(XAudioJSSampleRate, APISampleRate, 1, resampleBufferSize, true);
outputConvert = generateFlashMonoString;
}
else {
//STEREO Handling:
resampleBufferSize <<= 1;
resampled = getFloat32Flat(resampleBufferSize);
resampleControl = new Resampler(XAudioJSSampleRate, APISampleRate, 2, resampleBufferSize, true);
outputConvert = generateFlashStereoString;
}
}
//Initialize WebKit Audio:
(function () {
if (!launchedContext) {
try {
// The following line was modified for benchmarking:
audioContextHandle = new GameBoyAudioContext(); //Create a system audio context.
}
catch (error) {
try {
audioContextHandle = new AudioContext(); //Create a system audio context.
}
catch (error) {
return;
}
}
try {
audioSource = audioContextHandle.createBufferSource(); //We need to create a false input to get the chain started.
audioSource.loop = false; //Keep this alive forever (Event handler will know when to ouput.)
XAudioJSSampleRate = webAudioActualSampleRate = audioContextHandle.sampleRate;
audioSource.buffer = audioContextHandle.createBuffer(1, 1, webAudioActualSampleRate); //Create a zero'd input buffer for the input to be valid.
audioNode = audioContextHandle.createJavaScriptNode(samplesPerCallback, 1, 2); //Create 2 outputs and ignore the input buffer (Just copy buffer 1 over if mono)
audioNode.onaudioprocess = audioOutputEvent; //Connect the audio processing event to a handling function so we can manipulate output
audioSource.connect(audioNode); //Send and chain the input to the audio manipulation.
audioNode.connect(audioContextHandle.destination); //Send and chain the output of the audio manipulation to the system audio output.
audioSource.noteOn(0); //Start the loop!
}
catch (error) {
return;
}
launchedContext = true;
}
})();
// End of js/other/XAudioServer.js file.
// Start of js/other/resize.js file.
//JavaScript Image Resizer (c) 2012 - Grant Galitz
function Resize(widthOriginal, heightOriginal, targetWidth, targetHeight, blendAlpha, interpolationPass) {
this.widthOriginal = Math.abs(parseInt(widthOriginal) || 0);
this.heightOriginal = Math.abs(parseInt(heightOriginal) || 0);
this.targetWidth = Math.abs(parseInt(targetWidth) || 0);
this.targetHeight = Math.abs(parseInt(targetHeight) || 0);
this.colorChannels = (!!blendAlpha) ? 4 : 3;
this.interpolationPass = !!interpolationPass;
this.targetWidthMultipliedByChannels = this.targetWidth * this.colorChannels;
this.originalWidthMultipliedByChannels = this.widthOriginal * this.colorChannels;
this.originalHeightMultipliedByChannels = this.heightOriginal * this.colorChannels;
this.widthPassResultSize = this.targetWidthMultipliedByChannels * this.heightOriginal;
this.finalResultSize = this.targetWidthMultipliedByChannels * this.targetHeight;
this.initialize();
}
Resize.prototype.initialize = function () {
//Perform some checks:
if (this.widthOriginal > 0 && this.heightOriginal > 0 && this.targetWidth > 0 && this.targetHeight > 0) {
if (this.widthOriginal == this.targetWidth) {
//Bypass the width resizer pass:
this.resizeWidth = this.bypassResizer;
}
else {
//Setup the width resizer pass:
this.ratioWeightWidthPass = this.widthOriginal / this.targetWidth;
if (this.ratioWeightWidthPass < 1 && this.interpolationPass) {
this.initializeFirstPassBuffers(true);
this.resizeWidth = (this.colorChannels == 4) ? this.resizeWidthInterpolatedRGBA : this.resizeWidthInterpolatedRGB;
}
else {
this.initializeFirstPassBuffers(false);
this.resizeWidth = (this.colorChannels == 4) ? this.resizeWidthRGBA : this.resizeWidthRGB;
}
}
if (this.heightOriginal == this.targetHeight) {
//Bypass the height resizer pass:
this.resizeHeight = this.bypassResizer;
}
else {
//Setup the height resizer pass: