Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
anzz1 authored Mar 31, 2024
1 parent 148326b commit 893c5a5
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 25 deletions.
38 changes: 20 additions & 18 deletions example/nes-embed.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ var SCREEN_WIDTH = 256;
var SCREEN_HEIGHT = 240;
var FRAMEBUFFER_SIZE = SCREEN_WIDTH*SCREEN_HEIGHT;

var nes;
var canvas_ctx, image;
var framebuffer_u8, framebuffer_u32;

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

var nes = new jsnes.NES({
onFrame: function(framebuffer_24){
for(var i = 0; i < FRAMEBUFFER_SIZE; i++) framebuffer_u32[i] = 0xFF000000 | framebuffer_24[i];
},
onAudioSample: function(l, r){
audio_samples_L[audio_write_cursor] = l;
audio_samples_R[audio_write_cursor] = r;
audio_write_cursor = (audio_write_cursor + 1) & SAMPLE_MASK;
},
});

function onAnimationFrame(){
window.requestAnimationFrame(onAnimationFrame);

Expand Down Expand Up @@ -76,6 +66,22 @@ function keyboard(callback, event){
}

function nes_init(canvas_id){
var audio_ctx = new window.AudioContext({
latencyHint: "interactive",
});

nes = new jsnes.NES({
onFrame: function(framebuffer_24){
for(var i = 0; i < FRAMEBUFFER_SIZE; i++) framebuffer_u32[i] = 0xFF000000 | framebuffer_24[i];
},
onAudioSample: function(l, r){
audio_samples_L[audio_write_cursor] = l;
audio_samples_R[audio_write_cursor] = r;
audio_write_cursor = (audio_write_cursor + 1) & SAMPLE_MASK;
},
sampleRate: audio_ctx.sampleRate,
});

var canvas = document.getElementById(canvas_id);
canvas_ctx = canvas.getContext("2d");
image = canvas_ctx.getImageData(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
Expand All @@ -89,13 +95,12 @@ function nes_init(canvas_id){
framebuffer_u32 = new Uint32Array(buffer);

// Setup audio.
var audio_ctx = new window.AudioContext({
latencyHint: "interactive",
sampleRate: 48000,
});
var script_processor = audio_ctx.createScriptProcessor(AUDIO_BUFFERING, 0, 2);
script_processor.onaudioprocess = audio_callback;
script_processor.connect(audio_ctx.destination);

document.addEventListener('keydown', (event) => {keyboard(nes.buttonDown, event)});
document.addEventListener('keyup', (event) => {keyboard(nes.buttonUp, event)});
}

function nes_boot(rom_data){
Expand Down Expand Up @@ -152,6 +157,3 @@ function nes_volume(value){
nes.papu.setMasterVolume(value);
}
}

document.addEventListener('keydown', (event) => {keyboard(nes.buttonDown, event)});
document.addEventListener('keyup', (event) => {keyboard(nes.buttonUp, event)});
3 changes: 2 additions & 1 deletion src/nes.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var NES = function (opts) {
onBatteryRamWrite: function () {},

emulateSound: true,
sampleRate: 48000,
};
if (typeof opts !== "undefined") {
var key;
Expand Down Expand Up @@ -159,7 +160,7 @@ NES.prototype = {

getFPS: function () {
var now = +new Date();
var fps = null;
var fps = 0;
if (this.lastFpsTime) {
fps = this.fpsFrameCount / ((now - this.lastFpsTime) / 1000);
}
Expand Down
11 changes: 5 additions & 6 deletions src/papu.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ var utils = require("./utils");

var CPU_FREQ_NTSC = 1789772.5; //1789772.72727272d;
// var CPU_FREQ_PAL = 1773447.4;
var APU_TO_CPU_CYCLE_NTSC = 14915;
// var APU_TO_CPU_CYCLE_PAL = 16627;
var APU_TO_CPU_CYCLE = 14915;

var PAPU = function (nes) {
this.nes = nes;
Expand Down Expand Up @@ -60,7 +59,7 @@ var PAPU = function (nes) {
this.dcValue = 0;

// Master volume:
this.masterVolume = 256;
this.masterVolume = 0;

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

PAPU.prototype = {
reset: function () {

this.sampleRate = this.nes.opts.sampleRate;
this.sampleTimerMax = Math.floor(
(1024.0 * CPU_FREQ_NTSC) / this.sampleRate
);

this.frameTime = APU_TO_CPU_CYCLE_NTSC;
this.frameTime = APU_TO_CPU_CYCLE;

this.sampleTimer = 0;

Expand Down Expand Up @@ -382,7 +381,7 @@ PAPU.prototype = {
// Clock frame counter at double CPU speed:
this.masterFrameCounter += nCycles << 1;
if (this.masterFrameCounter >= this.frameTime) {
// 240Hz tick (NTSC):
// 240Hz tick:
this.masterFrameCounter -= this.frameTime;
this.frameCounterTick();
}
Expand Down

0 comments on commit 893c5a5

Please sign in to comment.