c64-ready 2.1.1 → 2.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/admin.mjs +0 -0
- package/bin/headless.mjs +1 -0
- package/dist/assets/index-BAsZcXuS.js +178 -0
- package/dist/index.html +1 -1
- package/dist-ts/emulator/c64-emulator.d.ts +3 -3
- package/dist-ts/emulator/c64-emulator.d.ts.map +1 -1
- package/dist-ts/emulator/c64-emulator.js +3 -2
- package/dist-ts/emulator/c64-emulator.js.map +1 -1
- package/dist-ts/emulator/c64-wasm.d.ts +5 -5
- package/dist-ts/emulator/c64-wasm.d.ts.map +1 -1
- package/dist-ts/emulator/c64-wasm.js.map +1 -1
- package/dist-ts/emulator/input.d.ts +11 -4
- package/dist-ts/emulator/input.d.ts.map +1 -1
- package/dist-ts/emulator/input.js +41 -11
- package/dist-ts/emulator/input.js.map +1 -1
- package/dist-ts/headless/audio-capture.d.ts +1 -1
- package/dist-ts/headless/c64-headless.d.ts +2 -2
- package/dist-ts/headless/c64-headless.js +4 -4
- package/dist-ts/headless/frame-capture.d.ts +1 -1
- package/dist-ts/headless/input-bridge.d.ts +1 -1
- package/dist-ts/player/audio-engine.d.ts.map +1 -1
- package/dist-ts/player/audio-engine.js +4 -0
- package/dist-ts/player/audio-engine.js.map +1 -1
- package/dist-ts/player/c64-player.d.ts +21 -5
- package/dist-ts/player/c64-player.d.ts.map +1 -1
- package/dist-ts/player/c64-player.js +126 -10
- package/dist-ts/player/c64-player.js.map +1 -1
- package/dist-ts/player/canvas-renderer.d.ts +2 -2
- package/dist-ts/player/index.d.ts +7 -7
- package/dist-ts/player/index.d.ts.map +1 -1
- package/dist-ts/player/index.js +4 -4
- package/dist-ts/player/input-handler.d.ts +14 -4
- package/dist-ts/player/input-handler.d.ts.map +1 -1
- package/dist-ts/player/input-handler.js +33 -12
- package/dist-ts/player/input-handler.js.map +1 -1
- package/dist-ts/player/load-formats.d.ts +1 -1
- package/dist-ts/player/load-formats.js +1 -1
- package/dist-ts/player/load-formats.js.map +1 -1
- package/dist-ts/player/ui-controller.d.ts +1 -1
- package/dist-ts/player/ui-controller.d.ts.map +1 -1
- package/dist-ts/player/ui-controller.js +10 -2
- package/dist-ts/player/ui-controller.js.map +1 -1
- package/package.json +1 -1
- package/src/emulator/c64-emulator.ts +2 -1
- package/src/emulator/c64-wasm.ts +4 -4
- package/src/emulator/input.ts +50 -16
- package/src/headless/c64-key-map.mjs +357 -165
- package/src/headless/c64-wasm.mjs +2 -2
- package/src/headless/ffmpeg-runner.mjs +214 -75
- package/src/headless/headless-cli.d.ts +1 -1
- package/src/headless/headless-cli.mjs +137 -19
- package/src/headless/headless-cli.mjs.d.ts +1 -1
- package/src/headless/input-server.mjs +511 -67
- package/src/headless/webrtc-encoder.mjs +49 -25
- package/src/headless/webrtc-server.mjs +21 -20
- package/src/main.ts +3 -2
- package/src/player/audio-engine.ts +4 -0
- package/src/player/c64-player.ts +130 -8
- package/src/player/index.ts +8 -1
- package/src/player/input-handler.ts +44 -13
- package/src/player/load-formats.ts +1 -1
- package/src/player/styles/canvas-renderer.css +10 -5
- package/src/player/ui-controller.ts +26 -14
- package/dist/assets/index-Bk-mEPYS.js +0 -169
- package/dist-ts/emulator/memory.d.ts +0 -2
- package/dist-ts/emulator/memory.d.ts.map +0 -1
- package/dist-ts/emulator/memory.js +0 -9
- package/dist-ts/emulator/memory.js.map +0 -1
- package/src/emulator/memory.ts +0 -9
|
@@ -18,7 +18,10 @@ import wrtc from '@roamhq/wrtc';
|
|
|
18
18
|
const { RTCVideoSource, RTCAudioSource, rgbaToI420 } = wrtc.nonstandard;
|
|
19
19
|
|
|
20
20
|
const nowMonoMs = () => {
|
|
21
|
-
if (
|
|
21
|
+
if (
|
|
22
|
+
typeof globalThis.performance !== 'undefined' &&
|
|
23
|
+
typeof globalThis.performance.now === 'function'
|
|
24
|
+
) {
|
|
22
25
|
return globalThis.performance.now();
|
|
23
26
|
}
|
|
24
27
|
return Date.now();
|
|
@@ -50,29 +53,29 @@ export class WebRTCEncoder {
|
|
|
50
53
|
// ~1300ms *behind* the audio clock after every cart load. The browser's
|
|
51
54
|
// AV sync logic then delayed video rendering until the audio clock "caught
|
|
52
55
|
// up", creating the observed ~1–30s of perceived input lag.
|
|
53
|
-
_videoOriginMs = 0;
|
|
56
|
+
_videoOriginMs = 0; // set in init() to nowMonoMs()
|
|
54
57
|
_videoFrameDurationUs = 0; // microseconds per frame, set in setFps() (kept for compatibility)
|
|
55
58
|
|
|
56
59
|
// RTCAudioSource.onData() requires exactly (sampleRate / 100) samples per call —
|
|
57
60
|
// that is one 10 ms WebRTC audio processing frame. At 44100 Hz that is 441 samples.
|
|
58
61
|
// The SID buffer is 4096 samples, so we queue incoming audio and drain it in
|
|
59
62
|
// 441-sample chunks.
|
|
60
|
-
_audioChunkSize = 441;
|
|
63
|
+
_audioChunkSize = 441; // recalculated in init()
|
|
61
64
|
// Pre-allocated audio ring buffer — large enough to hold several SID pulls
|
|
62
65
|
// without allocation. Using a ring avoids per-frame Float32Array allocs
|
|
63
66
|
// (which caused GC pauses and audio stuttering at 50fps).
|
|
64
|
-
_audioRing = null;
|
|
67
|
+
_audioRing = null; // Float32Array, allocated in init()
|
|
65
68
|
_audioRingSize = 0;
|
|
66
69
|
_audioRingWrite = 0;
|
|
67
70
|
_audioRingRead = 0;
|
|
68
71
|
_audioRingCount = 0;
|
|
69
|
-
_audioInt16 = null;
|
|
72
|
+
_audioInt16 = null; // Int16Array(chunkSize), allocated in init()
|
|
70
73
|
// Pre-allocated staging buffers — reused every frame to avoid GC pressure.
|
|
71
74
|
// rgbaToI420() validates data.byteLength === width*height*4, so we cannot
|
|
72
75
|
// pass a subarray of the WASM heap (whose byteLength = full heap size).
|
|
73
76
|
// Copying into _rgbaBuf each frame is one memcpy (~417 KB) — negligible.
|
|
74
|
-
_rgbaBuf = null;
|
|
75
|
-
_i420Buf = null;
|
|
77
|
+
_rgbaBuf = null; // Uint8ClampedArray(width*height*4)
|
|
78
|
+
_i420Buf = null; // Uint8ClampedArray(width*height * 3/2)
|
|
76
79
|
|
|
77
80
|
/**
|
|
78
81
|
* @param {{ width?: number, height?: number, sampleRate?: number }} opts
|
|
@@ -84,12 +87,12 @@ export class WebRTCEncoder {
|
|
|
84
87
|
this._audioChunkSize = Math.floor(sampleRate / 100); // 441 @ 44100, 480 @ 48000
|
|
85
88
|
// Audio ring: large enough for 8× the SID pull size (8×4096 = 32768 samples)
|
|
86
89
|
// so even bursts after a cart load never overflow.
|
|
87
|
-
this._audioRingSize
|
|
88
|
-
this._audioRing
|
|
90
|
+
this._audioRingSize = 4096 * 8;
|
|
91
|
+
this._audioRing = new Float32Array(this._audioRingSize);
|
|
89
92
|
this._audioRingWrite = 0;
|
|
90
|
-
this._audioRingRead
|
|
93
|
+
this._audioRingRead = 0;
|
|
91
94
|
this._audioRingCount = 0;
|
|
92
|
-
this._audioInt16
|
|
95
|
+
this._audioInt16 = new Int16Array(this._audioChunkSize);
|
|
93
96
|
this._videoOriginMs = nowMonoMs();
|
|
94
97
|
|
|
95
98
|
// Staging buffers with exact byte sizes that rgbaToI420 expects
|
|
@@ -141,7 +144,7 @@ export class WebRTCEncoder {
|
|
|
141
144
|
*/
|
|
142
145
|
pushSilenceForGap(gapMs) {
|
|
143
146
|
if (!this.audioSource || gapMs <= 0) return;
|
|
144
|
-
const chunkSize
|
|
147
|
+
const chunkSize = this._audioChunkSize; // 441 @ 44100 Hz
|
|
145
148
|
const sampleRate = this._sampleRate;
|
|
146
149
|
// Number of silence samples needed to cover the gap
|
|
147
150
|
const totalSamples = Math.round((gapMs / 1000) * sampleRate);
|
|
@@ -152,16 +155,36 @@ export class WebRTCEncoder {
|
|
|
152
155
|
let pushed = 0;
|
|
153
156
|
while (pushed + chunkSize <= totalSamples) {
|
|
154
157
|
this.audioSource.onData({
|
|
155
|
-
samples:
|
|
156
|
-
sampleRate:
|
|
157
|
-
bitsPerSample:
|
|
158
|
-
channelCount:
|
|
158
|
+
samples: int16,
|
|
159
|
+
sampleRate: sampleRate,
|
|
160
|
+
bitsPerSample: 16,
|
|
161
|
+
channelCount: 1,
|
|
159
162
|
numberOfFrames: chunkSize,
|
|
160
163
|
});
|
|
161
164
|
pushed += chunkSize;
|
|
162
165
|
}
|
|
163
166
|
}
|
|
164
167
|
|
|
168
|
+
/**
|
|
169
|
+
* Push a small amount of silence so newly-created audio tracks have data
|
|
170
|
+
* before the main emulation loop delivers the first SID frame.
|
|
171
|
+
* @param {number} chunks Number of 10 ms audio chunks to push.
|
|
172
|
+
*/
|
|
173
|
+
primeAudio(chunks = 1) {
|
|
174
|
+
if (!this.audioSource || chunks <= 0) return;
|
|
175
|
+
const chunkSize = this._audioChunkSize;
|
|
176
|
+
this._audioInt16.fill(0);
|
|
177
|
+
for (let i = 0; i < chunks; i++) {
|
|
178
|
+
this.audioSource.onData({
|
|
179
|
+
samples: this._audioInt16,
|
|
180
|
+
sampleRate: this._sampleRate,
|
|
181
|
+
bitsPerSample: 16,
|
|
182
|
+
channelCount: 1,
|
|
183
|
+
numberOfFrames: chunkSize,
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
165
188
|
/**
|
|
166
189
|
* Set the frame rate used to compute video timestamps.
|
|
167
190
|
* Must be called after init() if the fps differs from the default 50.
|
|
@@ -185,10 +208,7 @@ export class WebRTCEncoder {
|
|
|
185
208
|
// This is necessary because rgbaToI420 checks data.byteLength strictly.
|
|
186
209
|
this._rgbaBuf.set(rgbaData.subarray(0, width * height * 4));
|
|
187
210
|
|
|
188
|
-
rgbaToI420(
|
|
189
|
-
{ width, height, data: this._rgbaBuf },
|
|
190
|
-
{ width, height, data: this._i420Buf },
|
|
191
|
-
);
|
|
211
|
+
rgbaToI420({ width, height, data: this._rgbaBuf }, { width, height, data: this._i420Buf });
|
|
192
212
|
|
|
193
213
|
// Drive timestamp from wall clock (microseconds elapsed since init()).
|
|
194
214
|
// After any gap (e.g. the ~1300ms c64_loadCartridge blockage) the timestamp
|
|
@@ -212,9 +232,9 @@ export class WebRTCEncoder {
|
|
|
212
232
|
* @param {Float32Array} f32samples - SID output, sampleRate Hz mono
|
|
213
233
|
*/
|
|
214
234
|
pushAudioFrame(f32samples) {
|
|
215
|
-
const chunkSize
|
|
216
|
-
const ringSize
|
|
217
|
-
const ring
|
|
235
|
+
const chunkSize = this._audioChunkSize;
|
|
236
|
+
const ringSize = this._audioRingSize;
|
|
237
|
+
const ring = this._audioRing;
|
|
218
238
|
|
|
219
239
|
// Write incoming samples into the ring (wrap-around, drop on overflow)
|
|
220
240
|
for (let i = 0; i < f32samples.length; i++) {
|
|
@@ -246,6 +266,10 @@ export class WebRTCEncoder {
|
|
|
246
266
|
}
|
|
247
267
|
}
|
|
248
268
|
|
|
249
|
-
get width() {
|
|
250
|
-
|
|
269
|
+
get width() {
|
|
270
|
+
return this._width;
|
|
271
|
+
}
|
|
272
|
+
get height() {
|
|
273
|
+
return this._height;
|
|
274
|
+
}
|
|
251
275
|
}
|
|
@@ -51,10 +51,7 @@ function buildTimedTurnCredentials({
|
|
|
51
51
|
const safeLabel = String(usernameLabel ?? '').trim() || 'c64live';
|
|
52
52
|
const expiresAt = Math.floor(nowSeconds) + normalizeTurnTtlSeconds(ttlSeconds);
|
|
53
53
|
const username = `${expiresAt}:${safeLabel}`;
|
|
54
|
-
const credential = crypto
|
|
55
|
-
.createHmac('sha1', safeSecret)
|
|
56
|
-
.update(username)
|
|
57
|
-
.digest('base64');
|
|
54
|
+
const credential = crypto.createHmac('sha1', safeSecret).update(username).digest('base64');
|
|
58
55
|
|
|
59
56
|
return { username, credential };
|
|
60
57
|
}
|
|
@@ -116,11 +113,12 @@ function buildIceServers({
|
|
|
116
113
|
if (verbose) {
|
|
117
114
|
const stunCount = stunUrls.length;
|
|
118
115
|
const turnCount = turnUrls.length;
|
|
119
|
-
const turnMode =
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
116
|
+
const turnMode =
|
|
117
|
+
turnUrls.length > 0 && turnSecret
|
|
118
|
+
? 'timed'
|
|
119
|
+
: turnUrls.length > 0 && turnUsername && turnPassword
|
|
120
|
+
? 'static'
|
|
121
|
+
: 'disabled';
|
|
124
122
|
console.error(
|
|
125
123
|
`[webrtc] ICE config stunUrls=${stunCount} turnUrls=${turnCount} turnMode=${turnMode}`,
|
|
126
124
|
);
|
|
@@ -177,16 +175,17 @@ export function createWebRTCServer({
|
|
|
177
175
|
onPeerConnected,
|
|
178
176
|
onDataChannelInput,
|
|
179
177
|
} = {}) {
|
|
180
|
-
const buildRuntimeIceServers = () =>
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
178
|
+
const buildRuntimeIceServers = () =>
|
|
179
|
+
buildIceServers({
|
|
180
|
+
iceStunUrls,
|
|
181
|
+
iceTurnUrls,
|
|
182
|
+
iceTurnSecret,
|
|
183
|
+
iceTurnTtlSeconds,
|
|
184
|
+
iceTurnUsernameLabel,
|
|
185
|
+
iceTurnUsername,
|
|
186
|
+
iceTurnPassword,
|
|
187
|
+
verbose,
|
|
188
|
+
});
|
|
190
189
|
|
|
191
190
|
const iceServers = buildRuntimeIceServers();
|
|
192
191
|
|
|
@@ -533,7 +532,9 @@ export function createWebRTCServer({
|
|
|
533
532
|
if (req.method === 'GET' && (req.url === '/' || req.url === '/index.html')) {
|
|
534
533
|
const requestIceServers = buildRuntimeIceServers();
|
|
535
534
|
res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' });
|
|
536
|
-
res.end(
|
|
535
|
+
res.end(
|
|
536
|
+
buildBrowserHtml(inputPort, minBitrateKbpsSafe, maxBitrateKbpsSafe, requestIceServers),
|
|
537
|
+
);
|
|
537
538
|
} else if (req.method === 'GET' && req.url === '/ice-config') {
|
|
538
539
|
const requestIceServers = buildRuntimeIceServers();
|
|
539
540
|
res.writeHead(200, { 'Content-Type': 'application/json; charset=utf-8' });
|
package/src/main.ts
CHANGED
|
@@ -27,11 +27,12 @@ const player = new C64Player({
|
|
|
27
27
|
gameUrl,
|
|
28
28
|
gameType,
|
|
29
29
|
renderer,
|
|
30
|
+
audio: { assetBaseUrl: base },
|
|
30
31
|
onProgress: (pct, label) => renderer.setProgress(pct, label),
|
|
31
32
|
});
|
|
32
33
|
|
|
33
34
|
// Initialise UI with reference to the player (for audio controls)
|
|
34
|
-
new UIController().init(player);
|
|
35
|
+
new UIController({ assetBaseUrl: base }).init(player);
|
|
35
36
|
|
|
36
37
|
player
|
|
37
38
|
.start()
|
|
@@ -99,7 +100,7 @@ window.addEventListener('c64-controller-disconnected', (e: Event) => {
|
|
|
99
100
|
const detail = (e as CustomEvent).detail;
|
|
100
101
|
status.textContent = `Controller Disconnected : ${detail.index} - ${detail.name}`;
|
|
101
102
|
status.style.color = '#9ecbff';
|
|
102
|
-
})
|
|
103
|
+
});
|
|
103
104
|
|
|
104
105
|
window.addEventListener('c64-load-tool', async (e: Event) => {
|
|
105
106
|
const detail = (e as CustomEvent).detail as
|
|
@@ -101,6 +101,7 @@ export class AudioEngine {
|
|
|
101
101
|
this._suspended = this.ctx?.state === 'suspended';
|
|
102
102
|
if (wasSuspended !== this._suspended) {
|
|
103
103
|
this.fireStateChange();
|
|
104
|
+
this.feedWorklet();
|
|
104
105
|
}
|
|
105
106
|
});
|
|
106
107
|
|
|
@@ -140,6 +141,7 @@ export class AudioEngine {
|
|
|
140
141
|
};
|
|
141
142
|
|
|
142
143
|
this._ready = true;
|
|
144
|
+
this.feedWorklet();
|
|
143
145
|
}
|
|
144
146
|
|
|
145
147
|
// ── SID buffer reader ─────────────────────────────────────────────────────
|
|
@@ -150,6 +152,7 @@ export class AudioEngine {
|
|
|
150
152
|
*/
|
|
151
153
|
setSidBufferReader(reader: SidBufferReader): void {
|
|
152
154
|
this._readSidBuffer = reader;
|
|
155
|
+
this.feedWorklet();
|
|
153
156
|
}
|
|
154
157
|
|
|
155
158
|
// ── Sample feeding ────────────────────────────────────────────────────────
|
|
@@ -171,6 +174,7 @@ export class AudioEngine {
|
|
|
171
174
|
await this.ctx.resume();
|
|
172
175
|
this._suspended = this.ctx.state === 'suspended';
|
|
173
176
|
this.fireStateChange();
|
|
177
|
+
this.feedWorklet();
|
|
174
178
|
} catch (err) {
|
|
175
179
|
console.error('AudioEngine: resume failed', err);
|
|
176
180
|
}
|
package/src/player/c64-player.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { C64Emulator } from '../emulator/c64-emulator';
|
|
|
2
2
|
import { getUnsupportedCrtReason, parseCrtInfo } from '../emulator/crt-info';
|
|
3
3
|
import type { GameLoadOptions } from '../types';
|
|
4
4
|
import type { JoystickPort } from '../emulator/constants';
|
|
5
|
-
import type { InputMode } from '../emulator/input';
|
|
5
|
+
import type { InputMode, KeyboardJoystickMap } from '../emulator/input';
|
|
6
6
|
import type CanvasRenderer from './canvas-renderer';
|
|
7
7
|
import { AudioEngine, type AudioEngineOptions } from './audio-engine';
|
|
8
8
|
import InputHandler from './input-handler';
|
|
@@ -14,7 +14,12 @@ const C64_KEYBOARD_BUFFER_LENGTH_ADDR = 0x00c6;
|
|
|
14
14
|
const C64_KEYBOARD_BUFFER_ADDR = 0x0277;
|
|
15
15
|
const C64_KEYBOARD_BUFFER_MAX_LENGTH = 8;
|
|
16
16
|
const PRG_AUTORUN_DELAY_MS = 650;
|
|
17
|
+
const DISK_AUTOLOAD_DELAY_MS = 1500;
|
|
18
|
+
const DISK_AUTOLOAD_RETURN_DELAY_MS = 250;
|
|
17
19
|
const PRG_AUTORUN_BUFFER_TIMEOUT_MS = 2000;
|
|
20
|
+
const NORMAL_DEBUG_SPEED = 100;
|
|
21
|
+
const FAST_FORWARD_STEP = 100;
|
|
22
|
+
const MAX_DEBUG_SPEED = 300;
|
|
18
23
|
|
|
19
24
|
export interface C64PlayerOptions {
|
|
20
25
|
wasmUrl: string;
|
|
@@ -32,6 +37,10 @@ export class C64Player {
|
|
|
32
37
|
private inputHandler: InputHandler | null = null;
|
|
33
38
|
private disableCrtPreloadChecks: boolean = false;
|
|
34
39
|
private destroyed: boolean = false;
|
|
40
|
+
private diskSessionActive: boolean = false;
|
|
41
|
+
private startupLoadInProgress: boolean = false;
|
|
42
|
+
private startupDiskAutoloadPending: boolean = false;
|
|
43
|
+
private debugSpeed: number = NORMAL_DEBUG_SPEED;
|
|
35
44
|
readonly audio: AudioEngine;
|
|
36
45
|
private readonly options: Required<Pick<C64PlayerOptions, 'wasmUrl' | 'gameUrl' | 'gameType'>> &
|
|
37
46
|
C64PlayerOptions;
|
|
@@ -48,14 +57,25 @@ export class C64Player {
|
|
|
48
57
|
onProgress?.(10, 'INITIALISING WASM...');
|
|
49
58
|
this.emulator = await C64Emulator.load(wasmUrl);
|
|
50
59
|
this.emulator.setCrtPreloadChecksEnabled(!this.disableCrtPreloadChecks);
|
|
60
|
+
this.applyDebugSpeed();
|
|
51
61
|
this.attachInputAndRenderer(this.emulator, renderer);
|
|
52
62
|
let shouldAutoRunPrgAfterStart = false;
|
|
53
63
|
|
|
54
64
|
if (gameData !== undefined && gameData !== null) {
|
|
55
|
-
|
|
65
|
+
this.startupLoadInProgress = true;
|
|
66
|
+
try {
|
|
67
|
+
await this.loadGameData(gameData, gameType, gameSource ?? 'inline game data', onProgress);
|
|
68
|
+
} finally {
|
|
69
|
+
this.startupLoadInProgress = false;
|
|
70
|
+
}
|
|
56
71
|
shouldAutoRunPrgAfterStart = gameType === 'prg';
|
|
57
72
|
} else if (gameUrl && gameUrl !== 'null') {
|
|
58
|
-
|
|
73
|
+
this.startupLoadInProgress = true;
|
|
74
|
+
try {
|
|
75
|
+
await this.loadGame(gameUrl, gameType, onProgress);
|
|
76
|
+
} finally {
|
|
77
|
+
this.startupLoadInProgress = false;
|
|
78
|
+
}
|
|
59
79
|
shouldAutoRunPrgAfterStart = gameType === 'prg';
|
|
60
80
|
}
|
|
61
81
|
|
|
@@ -63,6 +83,10 @@ export class C64Player {
|
|
|
63
83
|
if (shouldAutoRunPrgAfterStart) {
|
|
64
84
|
await this.autoRunPrgIfRunning();
|
|
65
85
|
}
|
|
86
|
+
if (this.startupDiskAutoloadPending) {
|
|
87
|
+
this.startupDiskAutoloadPending = false;
|
|
88
|
+
await this.autoLoadDiskIfRunning();
|
|
89
|
+
}
|
|
66
90
|
|
|
67
91
|
// Initialise audio in the background — never blocks game startup
|
|
68
92
|
this.initAudio();
|
|
@@ -84,15 +108,14 @@ export class C64Player {
|
|
|
84
108
|
window.dispatchEvent(new CustomEvent('c64-audio-state', { detail: state }));
|
|
85
109
|
};
|
|
86
110
|
|
|
111
|
+
this.audio.setSidBufferReader(() => this.emulator?.getSidBuffer() ?? null);
|
|
112
|
+
|
|
87
113
|
// Fire-and-forget init
|
|
88
114
|
this.audio.init().then((autoplayOk) => {
|
|
89
115
|
if (!this.emulator) return;
|
|
90
116
|
// Tell the SID what sample rate we're using (matches AudioContext)
|
|
91
117
|
this.emulator.setSampleRate(this.audio.sampleRate);
|
|
92
118
|
|
|
93
|
-
// Give the engine a reader that snapshots the SID circular buffer
|
|
94
|
-
this.audio.setSidBufferReader(() => this.emulator?.getSidBuffer() ?? null);
|
|
95
|
-
|
|
96
119
|
if (!autoplayOk) {
|
|
97
120
|
window.dispatchEvent(new CustomEvent('c64-audio-suspended'));
|
|
98
121
|
}
|
|
@@ -201,6 +224,8 @@ export class C64Player {
|
|
|
201
224
|
if (!this.emulator) return;
|
|
202
225
|
try {
|
|
203
226
|
this.emulator.removeCartridge();
|
|
227
|
+
this.diskSessionActive = false;
|
|
228
|
+
this.startupDiskAutoloadPending = false;
|
|
204
229
|
window.dispatchEvent(new CustomEvent('c64-detach', { detail: {} }));
|
|
205
230
|
window.dispatchEvent(new CustomEvent('c64-close-menu'));
|
|
206
231
|
} catch (e) {
|
|
@@ -227,7 +252,10 @@ export class C64Player {
|
|
|
227
252
|
await this.emulator.reboot();
|
|
228
253
|
this.attachInputAndRenderer(this.emulator, this.options.renderer);
|
|
229
254
|
this.emulator.start();
|
|
255
|
+
this.diskSessionActive = false;
|
|
256
|
+
this.startupDiskAutoloadPending = false;
|
|
230
257
|
this.emulator.setSampleRate(this.audio.sampleRate);
|
|
258
|
+
this.applyDebugSpeed();
|
|
231
259
|
this.audio.setSidBufferReader(() => this.emulator?.getSidBuffer() ?? null);
|
|
232
260
|
window.dispatchEvent(new CustomEvent('c64-reboot', { detail: {} }));
|
|
233
261
|
window.dispatchEvent(new CustomEvent('c64-close-menu'));
|
|
@@ -262,6 +290,11 @@ export class C64Player {
|
|
|
262
290
|
this.inputHandler?.setKeyboardJoystickPort(port);
|
|
263
291
|
}
|
|
264
292
|
|
|
293
|
+
/** Change which keyboard codes map to joystick controls. */
|
|
294
|
+
setKeyboardJoystickMap(map: KeyboardJoystickMap): void {
|
|
295
|
+
this.inputHandler?.setKeyboardJoystickMap(map);
|
|
296
|
+
}
|
|
297
|
+
|
|
265
298
|
/**
|
|
266
299
|
* Change the input mode ('joystick' | 'keyboard' | 'mixed').
|
|
267
300
|
* - 'joystick' — arrows + ControlLeft control the joystick only (default)
|
|
@@ -273,6 +306,33 @@ export class C64Player {
|
|
|
273
306
|
this.inputHandler?.setInputMode(mode);
|
|
274
307
|
}
|
|
275
308
|
|
|
309
|
+
/** Set emulator speed in 100% increments, clamped from normal speed through 10x. */
|
|
310
|
+
setFastForwardSpeed(speed: number): number {
|
|
311
|
+
const nextSpeed = clampDebugSpeed(speed);
|
|
312
|
+
if (nextSpeed === this.debugSpeed) return this.debugSpeed;
|
|
313
|
+
|
|
314
|
+
this.debugSpeed = nextSpeed;
|
|
315
|
+
this.applyDebugSpeed();
|
|
316
|
+
this.notifyDebugSpeedChanged();
|
|
317
|
+
return this.debugSpeed;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
incrementFastForwardSpeed(): number {
|
|
321
|
+
return this.setFastForwardSpeed(this.debugSpeed + FAST_FORWARD_STEP);
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
decrementFastForwardSpeed(): number {
|
|
325
|
+
return this.setFastForwardSpeed(this.debugSpeed - FAST_FORWARD_STEP);
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
resetFastForwardSpeed(): number {
|
|
329
|
+
return this.setFastForwardSpeed(NORMAL_DEBUG_SPEED);
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
getFastForwardSpeed(): number {
|
|
333
|
+
return this.debugSpeed;
|
|
334
|
+
}
|
|
335
|
+
|
|
276
336
|
setActiveGamepadIndex(index: number): void {
|
|
277
337
|
this.inputHandler?.setActiveGamepadIndex(index);
|
|
278
338
|
}
|
|
@@ -315,6 +375,27 @@ export class C64Player {
|
|
|
315
375
|
await this.insertTextIntoKeyboardBuffer('run\n');
|
|
316
376
|
}
|
|
317
377
|
|
|
378
|
+
private async autoLoadDiskIfRunning(): Promise<void> {
|
|
379
|
+
if (!this.emulator || !this.emulator.isRunning()) return;
|
|
380
|
+
await waitMs(DISK_AUTOLOAD_DELAY_MS);
|
|
381
|
+
await this.insertTextIntoKeyboardBuffer('LOAD"*",8,1');
|
|
382
|
+
await waitMs(DISK_AUTOLOAD_RETURN_DELAY_MS);
|
|
383
|
+
await this.insertTextIntoKeyboardBuffer('\n');
|
|
384
|
+
await this.waitForKeyboardBufferEmpty();
|
|
385
|
+
await this.insertTextIntoKeyboardBuffer('run\n');
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
private async prepareFirstDiskLoad(): Promise<void> {
|
|
389
|
+
if (!this.emulator || this.startupLoadInProgress) return;
|
|
390
|
+
try {
|
|
391
|
+
this.emulator.removeCartridge();
|
|
392
|
+
} catch {
|
|
393
|
+
this.emulator.reset();
|
|
394
|
+
}
|
|
395
|
+
this.emulator.start();
|
|
396
|
+
await waitMs(120);
|
|
397
|
+
}
|
|
398
|
+
|
|
318
399
|
private async insertTextIntoKeyboardBuffer(text: string): Promise<void> {
|
|
319
400
|
if (!this.emulator) return;
|
|
320
401
|
|
|
@@ -355,12 +436,27 @@ export class C64Player {
|
|
|
355
436
|
if (type === 'crt') {
|
|
356
437
|
this.handleCrtPreloadChecks(data, source, onProgress);
|
|
357
438
|
}
|
|
439
|
+
const shouldAutoLoadDisk = type === 'd64' && !this.diskSessionActive;
|
|
440
|
+
if (shouldAutoLoadDisk) {
|
|
441
|
+
await this.prepareFirstDiskLoad();
|
|
442
|
+
}
|
|
358
443
|
this.emulator.loadGame({ type, data });
|
|
359
444
|
if (type === 'prg') {
|
|
445
|
+
this.diskSessionActive = false;
|
|
360
446
|
await this.autoRunPrgIfRunning();
|
|
447
|
+
} else if (type === 'd64') {
|
|
448
|
+
this.diskSessionActive = true;
|
|
449
|
+
if (shouldAutoLoadDisk) {
|
|
450
|
+
if (this.startupLoadInProgress) this.startupDiskAutoloadPending = true;
|
|
451
|
+
else await this.autoLoadDiskIfRunning();
|
|
452
|
+
}
|
|
453
|
+
} else {
|
|
454
|
+
this.diskSessionActive = false;
|
|
361
455
|
}
|
|
362
456
|
onProgress?.(100, 'READY!');
|
|
363
|
-
window.dispatchEvent(
|
|
457
|
+
window.dispatchEvent(
|
|
458
|
+
new CustomEvent('c64-load-success', { detail: { ...eventDetail, type } }),
|
|
459
|
+
);
|
|
364
460
|
} catch (err) {
|
|
365
461
|
onProgress?.(0, 'FAILED');
|
|
366
462
|
console.error('Failed to insert cartridge or load game:', err);
|
|
@@ -409,11 +505,31 @@ export class C64Player {
|
|
|
409
505
|
|
|
410
506
|
private attachInputAndRenderer(emulator: C64Emulator, renderer: CanvasRenderer): void {
|
|
411
507
|
this.inputHandler?.detach();
|
|
412
|
-
this.inputHandler = new InputHandler(emulator
|
|
508
|
+
this.inputHandler = new InputHandler(emulator, window, {
|
|
509
|
+
onFastForwardIncrease: () => this.incrementFastForwardSpeed(),
|
|
510
|
+
onFastForwardDecrease: () => this.decrementFastForwardSpeed(),
|
|
511
|
+
onFastForwardReset: () => this.resetFastForwardSpeed(),
|
|
512
|
+
});
|
|
413
513
|
this.inputHandler.attach();
|
|
414
514
|
renderer.attachTo(emulator);
|
|
415
515
|
}
|
|
416
516
|
|
|
517
|
+
private applyDebugSpeed(): void {
|
|
518
|
+
this.emulator?.setDebugSpeed(this.debugSpeed);
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
private notifyDebugSpeedChanged(): void {
|
|
522
|
+
const message =
|
|
523
|
+
this.debugSpeed === NORMAL_DEBUG_SPEED
|
|
524
|
+
? 'Fast-forward: normal speed'
|
|
525
|
+
: `Fast-forward: ${this.debugSpeed / NORMAL_DEBUG_SPEED}x`;
|
|
526
|
+
window.dispatchEvent(
|
|
527
|
+
new CustomEvent('c64-load-info', {
|
|
528
|
+
detail: { mode: 'info', source: 'keyboard', message },
|
|
529
|
+
}),
|
|
530
|
+
);
|
|
531
|
+
}
|
|
532
|
+
|
|
417
533
|
private handleCrtPreloadChecks(
|
|
418
534
|
data: Uint8Array<ArrayBufferLike>,
|
|
419
535
|
source: string,
|
|
@@ -480,6 +596,12 @@ function waitMs(ms: number): Promise<void> {
|
|
|
480
596
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
481
597
|
}
|
|
482
598
|
|
|
599
|
+
function clampDebugSpeed(speed: number): number {
|
|
600
|
+
if (!Number.isFinite(speed)) return NORMAL_DEBUG_SPEED;
|
|
601
|
+
const steppedSpeed = Math.round(speed / FAST_FORWARD_STEP) * FAST_FORWARD_STEP;
|
|
602
|
+
return Math.max(NORMAL_DEBUG_SPEED, Math.min(MAX_DEBUG_SPEED, steppedSpeed));
|
|
603
|
+
}
|
|
604
|
+
|
|
483
605
|
function normaliseKeyboardBufferText(text: string): number[] {
|
|
484
606
|
const bytes: number[] = [];
|
|
485
607
|
const normalised = text.toUpperCase().replace(/\r\n/g, '\n');
|
package/src/player/index.ts
CHANGED
|
@@ -4,4 +4,11 @@ export type { C64PlayerOptions, ProgressCallback } from './c64-player';
|
|
|
4
4
|
export { AudioEngine } from './audio-engine';
|
|
5
5
|
export type { AudioEngineOptions, AudioStateChangeCallback, SidBufferReader } from './audio-engine';
|
|
6
6
|
export { default as InputHandler } from './input-handler';
|
|
7
|
-
export type {
|
|
7
|
+
export type {
|
|
8
|
+
C64Config,
|
|
9
|
+
C64State,
|
|
10
|
+
FrameBuffer,
|
|
11
|
+
AudioBuffer,
|
|
12
|
+
InputEvent,
|
|
13
|
+
GameLoadOptions,
|
|
14
|
+
} from '../types';
|
|
@@ -1,16 +1,28 @@
|
|
|
1
|
-
import { EmulatorInput
|
|
2
|
-
import type { InputMode } from '../emulator/input';
|
|
1
|
+
import { EmulatorInput } from '../emulator/input';
|
|
2
|
+
import type { InputMode, KeyboardJoystickMap } from '../emulator/input';
|
|
3
3
|
import type { JoystickPort } from '../emulator/constants';
|
|
4
4
|
import type { C64Emulator } from '../emulator/c64-emulator';
|
|
5
5
|
|
|
6
|
+
export interface InputHandlerOptions {
|
|
7
|
+
onFastForwardIncrease?: () => void;
|
|
8
|
+
onFastForwardDecrease?: () => void;
|
|
9
|
+
onFastForwardReset?: () => void;
|
|
10
|
+
}
|
|
11
|
+
|
|
6
12
|
export default class InputHandler {
|
|
7
13
|
private readonly emulatorInput: EmulatorInput;
|
|
14
|
+
private readonly options: InputHandlerOptions;
|
|
8
15
|
// capture-phase blockers
|
|
9
16
|
private readonly captureKeyDown = (e: KeyboardEvent) => this.onCaptureKeyDown(e);
|
|
10
17
|
private readonly captureKeyUp = (e: KeyboardEvent) => this.onCaptureKeyUp(e);
|
|
11
18
|
|
|
12
|
-
constructor(
|
|
19
|
+
constructor(
|
|
20
|
+
emulator: C64Emulator,
|
|
21
|
+
target: EventTarget = window,
|
|
22
|
+
options: InputHandlerOptions = {},
|
|
23
|
+
) {
|
|
13
24
|
this.emulatorInput = new EmulatorInput(emulator, target);
|
|
25
|
+
this.options = options;
|
|
14
26
|
}
|
|
15
27
|
|
|
16
28
|
attach(): void {
|
|
@@ -31,6 +43,11 @@ export default class InputHandler {
|
|
|
31
43
|
this.emulatorInput.setKeyboardPort(port);
|
|
32
44
|
}
|
|
33
45
|
|
|
46
|
+
/** Change which keyboard codes map to joystick controls. */
|
|
47
|
+
setKeyboardJoystickMap(map: KeyboardJoystickMap): void {
|
|
48
|
+
this.emulatorInput.setKeyboardJoystickMap(map);
|
|
49
|
+
}
|
|
50
|
+
|
|
34
51
|
/** Change the input mode ('joystick' | 'keyboard' | 'mixed') */
|
|
35
52
|
setInputMode(mode: InputMode): void {
|
|
36
53
|
this.emulatorInput.setInputMode(mode);
|
|
@@ -49,13 +66,11 @@ export default class InputHandler {
|
|
|
49
66
|
}
|
|
50
67
|
|
|
51
68
|
private onCaptureKeyDown(e: KeyboardEvent): void {
|
|
69
|
+
if (this.handleFastForwardShortcut(e)) return;
|
|
70
|
+
|
|
52
71
|
// Block joystick-mapped keys (standard + mixed mode) when overlays are
|
|
53
72
|
// visible or the document is unfocused. Use the union of both key sets.
|
|
54
|
-
|
|
55
|
-
...(JOYSTICK_KEY_CODES as string[]),
|
|
56
|
-
...Object.keys(MIXED_JOYSTICK_KEYS),
|
|
57
|
-
]);
|
|
58
|
-
if (!JOY_KEYS.has(e.code)) return;
|
|
73
|
+
if (!this.isJoystickKey(e)) return;
|
|
59
74
|
|
|
60
75
|
// Block if document is unfocused. Prefer the Visibility API (document.hidden)
|
|
61
76
|
// which is stable under JSDOM; fall back to hasFocus() only if hidden is unavailable.
|
|
@@ -102,11 +117,7 @@ export default class InputHandler {
|
|
|
102
117
|
|
|
103
118
|
private onCaptureKeyUp(e: KeyboardEvent): void {
|
|
104
119
|
// Only block joystick-mapped keys (standard + mixed mode)
|
|
105
|
-
|
|
106
|
-
...(JOYSTICK_KEY_CODES as string[]),
|
|
107
|
-
...Object.keys(MIXED_JOYSTICK_KEYS),
|
|
108
|
-
]);
|
|
109
|
-
if (!JOY_KEYS.has(e.code)) return;
|
|
120
|
+
if (!this.isJoystickKey(e)) return;
|
|
110
121
|
|
|
111
122
|
// Mirror the same blocking logic for keyup events
|
|
112
123
|
try {
|
|
@@ -137,4 +148,24 @@ export default class InputHandler {
|
|
|
137
148
|
// ignore DOM errors
|
|
138
149
|
}
|
|
139
150
|
}
|
|
151
|
+
|
|
152
|
+
private isJoystickKey(e: KeyboardEvent): boolean {
|
|
153
|
+
return this.emulatorInput.isJoystickKeyCode(e.code);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
private handleFastForwardShortcut(e: KeyboardEvent): boolean {
|
|
157
|
+
if (!e.altKey || e.ctrlKey || e.metaKey || e.repeat) return false;
|
|
158
|
+
|
|
159
|
+
const increase = e.key === '+' || e.key === '=' || e.code === 'Equal';
|
|
160
|
+
const decrease = e.key === '-' || e.code === 'Minus';
|
|
161
|
+
const reset = e.key === 'Backspace' || e.code === 'Backspace';
|
|
162
|
+
if (!increase && !decrease && !reset) return false;
|
|
163
|
+
|
|
164
|
+
if (increase) this.options.onFastForwardIncrease?.();
|
|
165
|
+
else if (decrease) this.options.onFastForwardDecrease?.();
|
|
166
|
+
else this.options.onFastForwardReset?.();
|
|
167
|
+
e.preventDefault();
|
|
168
|
+
e.stopImmediatePropagation();
|
|
169
|
+
return true;
|
|
170
|
+
}
|
|
140
171
|
}
|
|
@@ -49,7 +49,7 @@ export function getAcceptForLoadTypeSelection(selection: LoadTypeSelection): str
|
|
|
49
49
|
const exts =
|
|
50
50
|
selection === 'auto'
|
|
51
51
|
? LOAD_FORMAT_OPTIONS.flatMap((f) => f.extensions)
|
|
52
|
-
: LOAD_FORMAT_OPTIONS.find((f) => f.type === selection)?.extensions ?? [];
|
|
52
|
+
: (LOAD_FORMAT_OPTIONS.find((f) => f.type === selection)?.extensions ?? []);
|
|
53
53
|
return exts.join(',');
|
|
54
54
|
}
|
|
55
55
|
|
|
@@ -3,7 +3,9 @@
|
|
|
3
3
|
position: relative;
|
|
4
4
|
width: 768px;
|
|
5
5
|
margin-top: 12px;
|
|
6
|
-
transition:
|
|
6
|
+
transition:
|
|
7
|
+
opacity 0.6s ease,
|
|
8
|
+
transform 0.6s ease;
|
|
7
9
|
}
|
|
8
10
|
.c64-loader.hidden {
|
|
9
11
|
opacity: 0;
|
|
@@ -31,11 +33,14 @@
|
|
|
31
33
|
background: linear-gradient(90deg, #6c6cff 0%, #a8a8ff 50%, #6c6cff 100%);
|
|
32
34
|
background-size: 200% 100%;
|
|
33
35
|
border-radius: 2px;
|
|
34
|
-
transition: width 0.5s cubic-bezier(.22
|
|
36
|
+
transition: width 0.5s cubic-bezier(0.22, 0.61, 0.36, 1);
|
|
35
37
|
animation: c64-shimmer 1.5s linear infinite;
|
|
36
38
|
}
|
|
37
39
|
@keyframes c64-shimmer {
|
|
38
|
-
0%
|
|
39
|
-
|
|
40
|
+
0% {
|
|
41
|
+
background-position: 200% 0;
|
|
42
|
+
}
|
|
43
|
+
100% {
|
|
44
|
+
background-position: -200% 0;
|
|
45
|
+
}
|
|
40
46
|
}
|
|
41
|
-
|