chimera-jev-full-duplex / runtime /src /chimera-worklet.js
cdismus4's picture
Recreate repo with clean history: full Base v1 pack, student ear, runtime kit, model card
86a9d2b verified
Raw History Blame Contribute Delete
3.37 kB
// CHIMERA エンジンを AudioWorklet で駆動する。C ABI(engine_*)のみを使う。
class ChimeraEngineProcessor extends AudioWorkletProcessor {
constructor(options) {
super();
this.ready = false;
this.handle = 0;
this.x = null;
this.teleCounter = 0;
this.activeTail = 0;
this.curSeq = 0;
const bytes = options.processorOptions.wasmBytes;
WebAssembly.instantiate(bytes, {}).then(({ instance }) => {
this.x = instance.exports;
this.handle = this.x.engine_new(sampleRate);
this.ready = true;
this.port.postMessage({ type: 'ready' });
}).catch(e => this.port.postMessage({ type: 'error', message: String(e && e.message || e) }));
this.port.onmessage = (ev) => {
const m = ev.data;
if (!this.ready || m.type !== 'schedule') return;
try {
// 音源の土台(f0・有声)を発話ごとに設定する(CLI の set_source(f0) 相当)
const vals = new Float64Array(this.x.memory.buffer, this.x.engine_values(this.handle), 32);
vals.fill(NaN);
if (m.f0) vals[0] = m.f0;
vals[10] = 1; // voiced
this.x.engine_set_source(this.handle);
const recPtr = this.x.engine_records(this.handle);
new Uint8Array(this.x.memory.buffer, recPtr, m.records.byteLength).set(new Uint8Array(m.records));
const schedClock = this.x.engine_schedule(this.handle, m.count, 0.04, 0);
this.curSeq = m.seq || 0;
this.activeTail = 18;
this.teleCounter = 99;
this.port.postMessage({ type: 'scheduled', seq: this.curSeq, schedClock });
} catch (e) {
this.port.postMessage({ type: 'error', message: 'schedule: ' + String(e && e.message || e) });
}
};
}
process(inputs, outputs) {
if (!this.ready) return true;
try {
const ptr = this.x.engine_render(this.handle, 128);
const src = new Float32Array(this.x.memory.buffer, ptr, 128);
const out = outputs[0];
for (let c = 0; c < out.length; c++) out[c].set(src);
const tp = this.x.engine_telemetry(this.handle);
const head = new Float64Array(this.x.memory.buffer, tp, 16);
const outRms = head[6];
if (outRms > 0.001) this.activeTail = 16;
else if (this.activeTail > 0) this.activeTail--;
const stride = this.activeTail > 0 ? 8 : 32;
if (++this.teleCounter >= stride) {
this.teleCounter = 0;
const nArtic = head[11] | 0;
const nPh = head[12] | 0, nOr = head[13] | 0, nT = nPh + nOr;
const block = new Float64Array(this.x.memory.buffer, tp, 16 + nArtic + nT * 3);
let o = 16 + nArtic;
const take = (n) => { const a = Array.from(block.slice(o, o + n)); o += n; return a; };
const phAreas = take(nPh), orAreas = take(nOr), pressure = take(nT), flow = take(nT);
this.port.postMessage({
type: 'telemetry',
seq: this.curSeq,
artic: Array.from(block.slice(16, 16 + nArtic)),
phAreas, orAreas, pressure, flow,
velumPort: head[2],
outRms, f0: head[3], voicing: head[4], clock: head[10],
});
}
} catch (e) {
this.port.postMessage({ type: 'error', message: 'render: ' + String(e && e.message || e) });
this.ready = false;
}
return true;
}
}
registerProcessor('chimera-engine', ChimeraEngineProcessor);