Skip to content

Commit 893c5a5

Browse files
authored
Add files via upload
1 parent 148326b commit 893c5a5

File tree

3 files changed

+27
-25
lines changed

3 files changed

+27
-25
lines changed

example/nes-embed.js

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ var SCREEN_WIDTH = 256;
22
var SCREEN_HEIGHT = 240;
33
var FRAMEBUFFER_SIZE = SCREEN_WIDTH*SCREEN_HEIGHT;
44

5+
var nes;
56
var canvas_ctx, image;
67
var framebuffer_u8, framebuffer_u32;
78

@@ -12,17 +13,6 @@ var audio_samples_L = new Float32Array(SAMPLE_COUNT);
1213
var audio_samples_R = new Float32Array(SAMPLE_COUNT);
1314
var audio_write_cursor = 0, audio_read_cursor = 0;
1415

15-
var nes = new jsnes.NES({
16-
onFrame: function(framebuffer_24){
17-
for(var i = 0; i < FRAMEBUFFER_SIZE; i++) framebuffer_u32[i] = 0xFF000000 | framebuffer_24[i];
18-
},
19-
onAudioSample: function(l, r){
20-
audio_samples_L[audio_write_cursor] = l;
21-
audio_samples_R[audio_write_cursor] = r;
22-
audio_write_cursor = (audio_write_cursor + 1) & SAMPLE_MASK;
23-
},
24-
});
25-
2616
function onAnimationFrame(){
2717
window.requestAnimationFrame(onAnimationFrame);
2818

@@ -76,6 +66,22 @@ function keyboard(callback, event){
7666
}
7767

7868
function nes_init(canvas_id){
69+
var audio_ctx = new window.AudioContext({
70+
latencyHint: "interactive",
71+
});
72+
73+
nes = new jsnes.NES({
74+
onFrame: function(framebuffer_24){
75+
for(var i = 0; i < FRAMEBUFFER_SIZE; i++) framebuffer_u32[i] = 0xFF000000 | framebuffer_24[i];
76+
},
77+
onAudioSample: function(l, r){
78+
audio_samples_L[audio_write_cursor] = l;
79+
audio_samples_R[audio_write_cursor] = r;
80+
audio_write_cursor = (audio_write_cursor + 1) & SAMPLE_MASK;
81+
},
82+
sampleRate: audio_ctx.sampleRate,
83+
});
84+
7985
var canvas = document.getElementById(canvas_id);
8086
canvas_ctx = canvas.getContext("2d");
8187
image = canvas_ctx.getImageData(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
@@ -89,13 +95,12 @@ function nes_init(canvas_id){
8995
framebuffer_u32 = new Uint32Array(buffer);
9096

9197
// Setup audio.
92-
var audio_ctx = new window.AudioContext({
93-
latencyHint: "interactive",
94-
sampleRate: 48000,
95-
});
9698
var script_processor = audio_ctx.createScriptProcessor(AUDIO_BUFFERING, 0, 2);
9799
script_processor.onaudioprocess = audio_callback;
98100
script_processor.connect(audio_ctx.destination);
101+
102+
document.addEventListener('keydown', (event) => {keyboard(nes.buttonDown, event)});
103+
document.addEventListener('keyup', (event) => {keyboard(nes.buttonUp, event)});
99104
}
100105

101106
function nes_boot(rom_data){
@@ -152,6 +157,3 @@ function nes_volume(value){
152157
nes.papu.setMasterVolume(value);
153158
}
154159
}
155-
156-
document.addEventListener('keydown', (event) => {keyboard(nes.buttonDown, event)});
157-
document.addEventListener('keyup', (event) => {keyboard(nes.buttonUp, event)});

src/nes.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ var NES = function (opts) {
1212
onBatteryRamWrite: function () {},
1313

1414
emulateSound: true,
15+
sampleRate: 48000,
1516
};
1617
if (typeof opts !== "undefined") {
1718
var key;
@@ -159,7 +160,7 @@ NES.prototype = {
159160

160161
getFPS: function () {
161162
var now = +new Date();
162-
var fps = null;
163+
var fps = 0;
163164
if (this.lastFpsTime) {
164165
fps = this.fpsFrameCount / ((now - this.lastFpsTime) / 1000);
165166
}

src/papu.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ var utils = require("./utils");
22

33
var CPU_FREQ_NTSC = 1789772.5; //1789772.72727272d;
44
// var CPU_FREQ_PAL = 1773447.4;
5-
var APU_TO_CPU_CYCLE_NTSC = 14915;
6-
// var APU_TO_CPU_CYCLE_PAL = 16627;
5+
var APU_TO_CPU_CYCLE = 14915;
76

87
var PAPU = function (nes) {
98
this.nes = nes;
@@ -60,7 +59,7 @@ var PAPU = function (nes) {
6059
this.dcValue = 0;
6160

6261
// Master volume:
63-
this.masterVolume = 256;
62+
this.masterVolume = 0;
6463

6564
// Stereo positioning:
6665
this.stereoPosLSquare1 = null;
@@ -103,12 +102,12 @@ var PAPU = function (nes) {
103102

104103
PAPU.prototype = {
105104
reset: function () {
106-
105+
this.sampleRate = this.nes.opts.sampleRate;
107106
this.sampleTimerMax = Math.floor(
108107
(1024.0 * CPU_FREQ_NTSC) / this.sampleRate
109108
);
110109

111-
this.frameTime = APU_TO_CPU_CYCLE_NTSC;
110+
this.frameTime = APU_TO_CPU_CYCLE;
112111

113112
this.sampleTimer = 0;
114113

@@ -382,7 +381,7 @@ PAPU.prototype = {
382381
// Clock frame counter at double CPU speed:
383382
this.masterFrameCounter += nCycles << 1;
384383
if (this.masterFrameCounter >= this.frameTime) {
385-
// 240Hz tick (NTSC):
384+
// 240Hz tick:
386385
this.masterFrameCounter -= this.frameTime;
387386
this.frameCounterTick();
388387
}

0 commit comments

Comments
 (0)