【发布时间】:2018-12-03 09:38:24
【问题描述】:
我创建了一个电子应用程序,它通过将块存储到一个数组中来使用音频工作节点节点记录音频,然后将记录的块发送到主进程以写入一个 wav 文件。该工作集还计算计量并检查值是否被裁剪,但是这些计算发生在异步函数中,而进程函数没有等待它解决以防止缓冲区欠载。此外,为了直接监控,输入流连接到媒体流目的地节点。整个设置在大多数情况下都运行良好,但对于少量录制的音频文件,文件的随机部分会出现明显的点击噪音。奇怪的是,您在直接监听输出端听不到这些咔哒声。查看文件的波形时,似乎有些样本只是缺少频谱图中也显示的内容:
我测量了处理方法每次运行所花费的时间,并记录了花费超过 2.9 毫秒(128 个样本/44100 kHz 单声道 => ~2.9 毫秒)的部分,有时它花费的时间比这更长,但点击这些部分不会出现噪音。是否有可能出现缓冲区欠载的 Web 音频 api,或者是否有一些内部缓冲区,并且发生这种情况时延迟会变得更糟?我只是无法弄清楚点击来自哪里。以下是代码的相关部分。
工作组代码:
const statsWindowSize = 1024 * 8; // ~5 stats per second for 44kHz
const clipThreshold = 0.98;
/* eslint-disable */
class RecordingWorkletProcessor extends AudioWorkletProcessor {
constructor() {
super();
this.isRecording = false;
this.clipping = false;
this.sampleIndex = 0;
this.sum = 0;
this.recordedBuffers = [];
this.writeIndex = 0;
this.port.onmessage = ({ data }) => {
if (data.type === 'startRecording') {
this.writeIndex = 0;
this.recordedBuffers = [];
this.isRecording = true;
} else if (data.type === 'stopRecording') {
this.port.postMessage({
type: 'recording',
buffers: this.recordedBuffers,
});
this.isRecording = false;
}
};
}
async computeStats(buffer) {
// ...removed to shorten the code snipped
}
process(inputs, outpus, parameters) {
const t0 = Date.now();
const writeIndex = this.writeIndex;
this.writeIndex += 1;
// Select the first input's first channel
const buffer0 = inputs[0][0];
// const { windowSize, clipThreshold, isRecording } = parameters;
if (this.isRecording) {
// Clone the data into a new Float32Array
const f32Buffer = new Float32Array(buffer0.length);
f32Buffer.set(buffer0);
this.recordedBuffers.splice(writeIndex, 0, f32Buffer);
}
// Detach the stats computation to prevent underruns
this.computeStats(buffer0);
// this.lastRunFinished = true;
if (this.isRecording) {
const t1 = Date.now();
const elapsedTime = t1 - t0;
if (elapsedTime > (128 / 44100) * 1000) {
const atPosition = (writeIndex * 128) / 44100;
this.port.postMessage({ type: 'underrun', elapsedTime, atPosition });
}
}
// Keep processor alive
return true;
}
}
/* eslint-enable */
registerProcessor('recording-worklet-processor', RecordingWorkletProcessor);
编写波形文件的代码:
// before these parts recordedBuffers will be send from the worklet via postMessage
// Merge all buffers from channel 1 into a single Float32Array
const totalByteLength = recordedBuffers.reduce(
(total, buf) => total + buf.byteLength,
0,
);
const header = Header({
sampleRate: ctx.sampleRate,
channels: 1,
bitsPerSample: 32,
audioFormat: IEEE_FLOAT,
byteLength: totalByteLength,
});
const wstream = createWriteStream(audioFilePath);
wstream.write(header);
// RealBuffer is just an alias for the node Buffer type
const chunks = RealBuffer.allocUnsafe(totalByteLength);
let offset = 0;
for (let i = 0; i < recordedBuffers.length; i++) {
const typedArray = recordedBuffers[i];
for (let j = 0; j < typedArray.length; j++) {
chunks.writeFloatLE(typedArray[j], offset);
offset += typedArray.BYTES_PER_ELEMENT;
}
}
wstream.write(chunks);
wstream.end();
创建标头的模块:
import { RealBuffer } from 'utils/io'; // An alias for the node Buffer type
export const PCM = 1;
export const IEEE_FLOAT = 3;
export const Header = ({
sampleRate,
channels,
bitsPerSample,
byteLength,
audioFormat,
}) => {
let offset = 0;
const buffer = RealBuffer.allocUnsafe(44);
const writeString = (str) => {
for (let i = 0; i < str.length; i += 1) {
buffer.writeUInt8(str.charCodeAt(i), offset + i);
}
offset += str.length;
};
const writeUint32 = (value) => {
buffer.writeUInt32LE(value, offset);
offset += 4;
};
const writeUint16 = (value) => {
buffer.writeUInt16LE(value, offset);
offset += 2;
};
const blockAlign = channels * (bitsPerSample / 8);
const byteRate = sampleRate * blockAlign;
const chunkSize = (byteLength / 8) - 8;
writeString('RIFF'); // ChunkID
writeUint32(chunkSize); // ChunkSize
writeString('WAVE'); // Format
writeString('fmt '); // Subchunk1ID
writeUint32(16); // Subchunk1Size
writeUint16(audioFormat); // AudioFormat (PCM=1,IEEE Float=3,...)
writeUint16(channels); // Channels
writeUint32(sampleRate); // SampleRate
writeUint32(byteRate); // ByteRate
writeUint16(blockAlign); // BlockAlign
writeUint16(bitsPerSample); // BitsPerSample
writeString('data'); // Subchunk2ID
writeUint32(byteLength); // Subchunk2Size
return buffer;
};
export default Header;
【问题讨论】:
标签: javascript audio electron web-audio-api