Function bodies 123 total
_waitForWasm method · javascript · L151-L160 (10 LOC)public/js/snes-adapter.js
_waitForWasm() {
return new Promise((resolve, reject) => {
if (window.snineX.isReady()) { resolve(); return; }
let tries = 0;
const id = setInterval(() => {
if (window.snineX.isReady()) { clearInterval(id); resolve(); return; }
if (++tries > 200) { clearInterval(id); reject(new Error('snes9x WASM init timed out')); }
}, 50);
});
}setAudioMuted method · javascript · L165-L171 (7 LOC)public/js/snes-adapter.js
setAudioMuted(muted) {
this._audioMuted = muted;
// Resume the AudioContext on unmute — browsers suspend it until a user gesture.
if (!muted && this._audioCtx?.state === 'suspended') {
this._audioCtx.resume();
}
}stopAudio method · javascript · L173-L182 (10 LOC)public/js/snes-adapter.js
stopAudio() {
if (this._workletNode) {
this._workletNode.disconnect();
this._workletNode = null;
}
if (this._audioCtx && this._audioCtx.state !== 'closed') {
this._audioCtx.close();
this._audioCtx = null;
}
}step method · javascript · L190-L205 (16 LOC)public/js/snes-adapter.js
step(inputMap) {
if (!this._romLoaded) return;
const j1 = this._toJoypad(inputMap[this.playerIds[0]] ?? 0);
const j2 = this._toJoypad(inputMap[this.playerIds[1]] ?? 0);
window.snineX.step(j1, j2);
// Feed audio to the worklet thread, skipping during rollback re-simulation.
// Transfer the buffer (zero-copy) rather than cloning it.
if (!this._audioMuted && this._workletNode) {
if (this._audioCtx?.state === 'suspended') this._audioCtx.resume();
const samples = window.snineX.getAudioSamples();
if (samples) this._workletNode.port.postMessage({ samples }, [samples.buffer]);
}
}saveState method · javascript · L211-L214 (4 LOC)public/js/snes-adapter.js
saveState() {
if (!this._romLoaded) return null;
return window.snineX.saveState();
}loadState method · javascript · L220-L223 (4 LOC)public/js/snes-adapter.js
loadState(snap) {
if (!snap || !this._romLoaded) return;
window.snineX.loadState(snap);
}render method · javascript · L230-L233 (4 LOC)public/js/snes-adapter.js
render() {
if (!this._romLoaded) return;
window.snineX.render(this.ctx, this._imageData);
}Open data scored by Repobility · https://repobility.com
_toJoypad method · javascript · L245-L260 (16 LOC)public/js/snes-adapter.js
_toJoypad(input) {
let j = 0;
if (input & InputBits.UP) j |= 0x0800;
if (input & InputBits.DOWN) j |= 0x0400;
if (input & InputBits.LEFT) j |= 0x0200;
if (input & InputBits.RIGHT) j |= 0x0100;
if (input & InputBits.A) j |= 0x0080;
if (input & InputBits.B) j |= 0x8000;
if (input & InputBits.START) j |= 0x1000;
if (input & InputBits.SELECT) j |= 0x2000;
if (input & InputBits.X) j |= 0x0040;
if (input & InputBits.Y) j |= 0x4000;
if (input & InputBits.L) j |= 0x0020;
if (input & InputBits.R) j |= 0x0010;
return j;
}SNESAudioProcessor class · javascript · L24-L127 (104 LOC)public/js/snes-audio-worklet.js
class SNESAudioProcessor extends AudioWorkletProcessor {
static get parameterDescriptors() { return []; }
constructor() {
super();
// Ring buffer: 8192 stereo sample-pairs ≈ 186 ms at 44 100 Hz.
// Large enough to absorb scheduling jitter and rollback re-simulation
// gaps without audible latency.
this._size = 8192;
this._mask = this._size - 1;
this._bufL = new Float32Array(this._size);
this._bufR = new Float32Array(this._size);
this._wr = 0; // write head (integer)
this._rd = 0; // read head (integer)
this._rdF = 0.0; // fractional part of read position [0, 1)
// DRC target: keep the buffer 25 % full (≈ 46 ms at 44 100 Hz).
// Proportional gain of 0.1 / size yields ≤ 1.25 % rate correction per
// quantum at the maximum expected fill deviation, converging in ≈ 1 s.
this._target = this._size >> 2; // 2048 sample-pairs
this.port.onmessage = ({ data }) => {
if (!data?.samples) return;
const s constructor method · javascript · L27-L67 (41 LOC)public/js/snes-audio-worklet.js
constructor() {
super();
// Ring buffer: 8192 stereo sample-pairs ≈ 186 ms at 44 100 Hz.
// Large enough to absorb scheduling jitter and rollback re-simulation
// gaps without audible latency.
this._size = 8192;
this._mask = this._size - 1;
this._bufL = new Float32Array(this._size);
this._bufR = new Float32Array(this._size);
this._wr = 0; // write head (integer)
this._rd = 0; // read head (integer)
this._rdF = 0.0; // fractional part of read position [0, 1)
// DRC target: keep the buffer 25 % full (≈ 46 ms at 44 100 Hz).
// Proportional gain of 0.1 / size yields ≤ 1.25 % rate correction per
// quantum at the maximum expected fill deviation, converging in ≈ 1 s.
this._target = this._size >> 2; // 2048 sample-pairs
this.port.onmessage = ({ data }) => {
if (!data?.samples) return;
const s = data.samples; // interleaved Float32: L0,R0,L1,R1,…
const n = s.length >> 1; // stereo sample-pair coprocess method · javascript · L69-L126 (58 LOC)public/js/snes-audio-worklet.js
process(_inputs, outputs) {
const out = outputs[0];
const chL = out[0];
const chR = out[1]; // may be undefined if output is mono
if (!chL) return true;
const n = chL.length; // typically 128
const avail = (this._wr - this._rd) & this._mask;
// ── DRC: proportional controller ────────────────────────────────────────
// rate > 1 → consume input faster (buffer above target, draining it)
// rate < 1 → consume input slower (buffer below target, letting it fill)
// Clamped to ±5 % — pitch deviation is imperceptible at this range.
const rate = Math.max(0.95, Math.min(1.05,
1.0 + 0.1 * (avail - this._target) / this._size
));
if (avail === 0) {
// Buffer completely empty — output silence.
chL.fill(0);
if (chR) chR.fill(0);
return true;
}
// ── Linear-interpolation resampling ─────────────────────────────────────
// Read at `rate` input samples per output sample. TromCacheGet function · javascript · L92-L97 (6 LOC)server.js
function romCacheGet(url) {
const entry = romCache.get(url);
if (!entry) return null;
entry.ts = Date.now(); // refresh for LRU ordering
return entry.buf;
}romCacheSet function · javascript · L99-L113 (15 LOC)server.js
function romCacheSet(url, buf) {
if (buf.length > ROM_CACHE_MAX_BYTES) return; // single entry too large to cache
// Evict oldest entries until there is room
while (romCacheTotalBytes + buf.length > ROM_CACHE_MAX_BYTES && romCache.size > 0) {
let oldestKey, oldestTs = Infinity;
for (const [k, v] of romCache) {
if (v.ts < oldestTs) { oldestTs = v.ts; oldestKey = k; }
}
const evicted = romCache.get(oldestKey);
romCache.delete(oldestKey);
romCacheTotalBytes -= evicted.size;
}
romCache.set(url, { buf, size: buf.length, ts: Date.now() });
romCacheTotalBytes += buf.length;
}genId function · javascript · L173-L175 (3 LOC)server.js
function genId(bytes) {
return crypto.randomBytes(bytes).toString('hex').toUpperCase();
}Room class · javascript · L177-L227 (51 LOC)server.js
class Room {
constructor(id, hostId) {
this.id = id;
this.hostId = hostId;
/** @type {Map<string, {ws: WebSocket, name: string}>} */
this.players = new Map();
this.gameStarted = false;
/** Ordered player IDs used for the current/last game */
this.playerOrder = [];
}
addPlayer(playerId, name, ws) {
this.players.set(playerId, { ws, name });
wsToPlayer.set(ws, { playerId, roomId: this.id });
}
removePlayer(playerId) {
const entry = this.players.get(playerId);
if (entry) wsToPlayer.delete(entry.ws);
this.players.delete(playerId);
this.playerOrder = this.playerOrder.filter(id => id !== playerId);
}
send(ws, msg) {
if (ws && ws.readyState === WebSocket.OPEN) {
ws.send(JSON.stringify(msg));
}
}
broadcast(msg, excludeId = null) {
for (const [id, player] of this.players) {
if (id !== excludeId) this.send(player.ws, msg);
}
}
broadcastAll(msg) {
this.broadcast(msg, null);
}
getPlayMethodology: Repobility · https://repobility.com/research/state-of-ai-code-2026/
constructor method · javascript · L178-L186 (9 LOC)server.js
constructor(id, hostId) {
this.id = id;
this.hostId = hostId;
/** @type {Map<string, {ws: WebSocket, name: string}>} */
this.players = new Map();
this.gameStarted = false;
/** Ordered player IDs used for the current/last game */
this.playerOrder = [];
}addPlayer method · javascript · L188-L191 (4 LOC)server.js
addPlayer(playerId, name, ws) {
this.players.set(playerId, { ws, name });
wsToPlayer.set(ws, { playerId, roomId: this.id });
}removePlayer method · javascript · L193-L198 (6 LOC)server.js
removePlayer(playerId) {
const entry = this.players.get(playerId);
if (entry) wsToPlayer.delete(entry.ws);
this.players.delete(playerId);
this.playerOrder = this.playerOrder.filter(id => id !== playerId);
}send method · javascript · L200-L204 (5 LOC)server.js
send(ws, msg) {
if (ws && ws.readyState === WebSocket.OPEN) {
ws.send(JSON.stringify(msg));
}
}broadcast method · javascript · L206-L210 (5 LOC)server.js
broadcast(msg, excludeId = null) {
for (const [id, player] of this.players) {
if (id !== excludeId) this.send(player.ws, msg);
}
}broadcastAll method · javascript · L212-L214 (3 LOC)server.js
broadcastAll(msg) {
this.broadcast(msg, null);
}getPlayerList method · javascript · L216-L222 (7 LOC)server.js
getPlayerList() {
return [...this.players.entries()].map(([id, p]) => ({
id,
name: p.name,
isHost: id === this.hostId,
}));
}sanitizeName function · javascript · L410-L412 (3 LOC)server.js
function sanitizeName(raw) {
return String(raw ?? 'Player').trim().slice(0, 24) || 'Player';
}‹ prevpage 3 / 3