78 lines
2.9 KiB
JavaScript
78 lines
2.9 KiB
JavaScript
// Streamed sound for PHP (JumPHP\Audio\PcmStream): PHP appends blocks to /tmp/jumphp-pcm — rate (u32 LE),
|
|
// channels (u16), volume x 1000 (u16), length in bytes (u32), then signed 16-bit little-endian samples — and this
|
|
// module, after each frame, schedules them back to back on the Web Audio clock (no gap, no click). It tells PHP how
|
|
// much is still to be heard (a 'pcm' event {buffered: seconds}) so the game keeps the right amount ahead.
|
|
const FILE = '/tmp/jumphp-pcm';
|
|
const MAX_AHEAD = 0.6; // more queued than this (a tab back from the background): late blocks are dropped
|
|
|
|
export function install(api) {
|
|
let ctx = null;
|
|
let gain = null;
|
|
let next = 0; // when the next block starts (AudioContext time)
|
|
|
|
const context = () => {
|
|
if (ctx) return ctx;
|
|
// share SDL_mixer's context when it exists (the page's SOUND button suspends both)
|
|
ctx = window.SDL2?.audioContext || window.Module?.SDL2?.audioContext || null;
|
|
if (!ctx) {
|
|
const AC = window.AudioContext || window.webkitAudioContext;
|
|
if (!AC) return null;
|
|
ctx = new AC();
|
|
}
|
|
gain = ctx.createGain();
|
|
gain.connect(ctx.destination);
|
|
return ctx;
|
|
};
|
|
// browsers start audio only after a user gesture
|
|
api.gesture(() => { const c = context(); if (c && c.state === 'suspended') c.resume().catch(() => {}); });
|
|
|
|
api.on('pcm-clear', () => { next = 0; });
|
|
|
|
api.afterFrame(() => {
|
|
const FS = api.FS();
|
|
let bytes = null;
|
|
try {
|
|
if (!FS || !FS.analyzePath(FILE).exists) return report();
|
|
bytes = FS.readFile(FILE);
|
|
FS.unlink(FILE);
|
|
} catch (e) { return report(); }
|
|
const c = context();
|
|
if (!c) return;
|
|
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
for (let o = 0; o + 12 <= bytes.length;) {
|
|
const rate = view.getUint32(o, true);
|
|
const channels = view.getUint16(o + 4, true) === 1 ? 1 : 2;
|
|
const volume = view.getUint16(o + 6, true) / 1000;
|
|
const length = view.getUint32(o + 8, true);
|
|
o += 12;
|
|
if (o + length > bytes.length || !rate) break;
|
|
play(c, view, o, length, rate, channels, volume);
|
|
o += length;
|
|
}
|
|
report();
|
|
});
|
|
|
|
function play(c, view, offset, length, rate, channels, volume) {
|
|
const frames = Math.floor(length / (2 * channels));
|
|
if (!frames) return;
|
|
const now = c.currentTime;
|
|
if (next < now + 0.01) next = now + 0.03; // underrun (or the first block): restart a little ahead
|
|
if (next > now + MAX_AHEAD) return; // too far ahead: drop
|
|
const buffer = c.createBuffer(channels, frames, rate);
|
|
for (let ch = 0; ch < channels; ch++) {
|
|
const data = buffer.getChannelData(ch);
|
|
for (let i = 0, p = offset + ch * 2; i < frames; i++, p += 2 * channels) data[i] = view.getInt16(p, true) / 32768;
|
|
}
|
|
const source = c.createBufferSource();
|
|
source.buffer = buffer;
|
|
source.connect(gain);
|
|
gain.gain.value = volume;
|
|
source.start(next);
|
|
next += frames / rate;
|
|
}
|
|
|
|
function report() {
|
|
const buffered = ctx && next > ctx.currentTime ? next - ctx.currentTime : 0;
|
|
api.send('pcm', { buffered });
|
|
}
|
|
}
|