File size: 1,200 Bytes
86a9d2b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | // マイク入力専用の軽量 AudioWorkletProcessor(destination へ一切接続しない)。
// ScriptProcessorNode のようにメインスレッド停止で AudioContext 再生を巻き込んで
// 音途切れ・無音を起こす問題を避けるため、オーディオ入力タップを独立させる。
class MicTapProcessor extends AudioWorkletProcessor {
constructor() {
super();
this.chunkSize = 1024; // 48kHz で約 21.3ms ごとに送出
this.buf = new Float32Array(this.chunkSize);
this.fill = 0;
}
process(inputs) {
const ch = inputs && inputs[0] && inputs[0][0];
if (!ch || ch.length === 0) return true;
let off = 0;
while (off < ch.length) {
const take = Math.min(ch.length - off, this.chunkSize - this.fill);
this.buf.set(ch.subarray(off, off + take), this.fill);
this.fill += take;
off += take;
if (this.fill >= this.chunkSize) {
const out = this.buf;
this.port.postMessage({ type: 'pcm', samples: out.buffer }, [out.buffer]);
this.buf = new Float32Array(this.chunkSize);
this.fill = 0;
}
}
return true;
}
}
registerProcessor('mic-tap', MicTapProcessor);
|