21 lines
644 B
JavaScript
21 lines
644 B
JavaScript
class AudioProcessor extends AudioWorkletProcessor {
|
|
process(inputs, outputs, parameters) {
|
|
const input = inputs[0];
|
|
if (input.length === 0) return true;
|
|
|
|
const channelData = input[0]; // 获取第一个声道的数据
|
|
const int16Data = new Int16Array(channelData.length);
|
|
|
|
// 转换为 16-bit PCM
|
|
for (let i = 0; i < channelData.length; i++) {
|
|
int16Data[i] = Math.max(-1, Math.min(1, channelData[i])) * 32767;
|
|
}
|
|
|
|
// 发送到主线程
|
|
this.port.postMessage(int16Data.buffer, [int16Data.buffer]);
|
|
|
|
return true; // 保持处理器运行
|
|
}
|
|
}
|
|
|
|
registerProcessor("audio-processor", AudioProcessor); |