fix(audio): decode 8-bit PCM WAVs synchronously and preload FluidSynth assets
Eliminates per-sound async decodeAudioData latency and lazy SoundFont loading delays in the TeaVM webclient while preserving main-thread FluidSynth audio stability.
### Web Audio Pipeline
- src/main/webapp/index.html:
- Added decodeWav8MonoSync(ctx, bytes) to decode 8-bit unsigned mono PCM WAVs from Sounds.java synchronously into an AudioBuffer with decodeAudioData fallback.
- Added preloadFluidSynthAssets() on page load to initialize JSSynth and fetch TimGM6mb.sf2 before the first user gesture, and added midiPlaySeq cancellation across async playMidi() steps.
- Configured AudioContext with { latencyHint: 'interactive' }, guarded playPcm() against suspended contexts, and kept FluidSynth ScriptProcessorNode at 4096 samples to avoid main-thread underruns.
- src/main/java/org/scape2006/web/DesktopHooks.java:
- Skipped JS sound marshaling when vol <= 0.0f and forwarded delayMs to window.scapeWebAudio.playPcm(arr, vol, delayMs).
- src/main/webapp/sw.js:
- Bumped CACHE_VERSION to 2006scape-web-v6 to invalidate stale cached web assets.
This commit is contained in:
1 parent
02c3d8fb10
commit
6737a936df
3 files changed
+83
-20
No files matched your search
@@ -107,12 +107,13 @@ public final class DesktopHooks {
|
||||
|
||||
public static void playSound(byte[] buffer, int offset, int length, int level, int delay) {
|
||||
if (buffer == null || length <= 0 || offset + length > buffer.length) return;
|
||||
float vol = soundVolume * Math.max(0f, Math.min(1f, level / 100.0f));
|
||||
if (vol <= 0.0f) return;
|
||||
Uint8Array arr = Uint8Array.create(length);
|
||||
for (int i = 0; i < length; i++) {
|
||||
arr.set(i, (short) (buffer[offset + i] & 0xFF));
|
||||
}
|
||||
float vol = soundVolume * Math.max(0f, Math.min(1f, level / 100.0f));
|
||||
jsPlayPcm(arr, vol);
|
||||
jsPlayPcm(arr, vol, Math.max(0, delay));
|
||||
}
|
||||
|
||||
public static void setSoundVolume(int config) {
|
||||
@@ -484,9 +485,9 @@ public final class DesktopHooks {
|
||||
}
|
||||
}
|
||||
|
||||
@JSBody(params = {"arr", "vol"}, script =
|
||||
"if (window.scapeWebAudio && window.scapeWebAudio.playPcm) window.scapeWebAudio.playPcm(arr, vol);")
|
||||
private static native void jsPlayPcm(Uint8Array arr, float vol);
|
||||
@JSBody(params = {"arr", "vol", "delayMs"}, script =
|
||||
"if (window.scapeWebAudio && window.scapeWebAudio.playPcm) window.scapeWebAudio.playPcm(arr, vol, delayMs);")
|
||||
private static native void jsPlayPcm(Uint8Array arr, float vol, int delayMs);
|
||||
|
||||
@JSBody(params = {"arr", "vol"}, script =
|
||||
"if (window.scapeWebAudio && window.scapeWebAudio.playMidi) window.scapeWebAudio.playMidi(arr, vol);")
|
||||
|
||||
Reference in new issue
Block a user