使用网络音频工作集录制音频时的咔嗒声

时间:2018-12-03 09:38:24

标签: javascript audio electron web-audio web-audio-api

我创建了一个电子应用程序,该应用程序通过将块存储到数组中并随后将记录的块发送到主进程以编写wav文件,来用音频工作节点记录音频。该工作集还可以计算计量并检查是否裁剪了值,但是这些计算是在异步函数中进行的,过程函数无需等待它解析以防止缓冲区不足。此外,为了直接监视,将输入流连接到媒体流目标节点。整个设置在大多数情况下都非常有效,但是对于少量的录制音频文件,文件随机部分会产生明显的咔嗒声。奇怪的是,您在直接监视输出中没有听到这些咔嗒声。当查看文件的波形时,似乎有些样本只是丢失了频谱图中还显示的内容:

waveform
spectrogram

我测量了该方法每次运行所花费的时间,并记录了将花费超过2.9ms(128个样本/ 44100 kHz单声道=>〜2.9ms)的零件,有时花费的时间更长,但是这些部分不会出现噪音。甚至可能出现用于缓冲区欠载的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);

写入wave文件的代码:

// 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;

1 个答案:

答案 0 :(得分:0)

process函数中,每次调用该函数时,总是在f32buffer中创建一个新数组。这需要最终收集,因此我猜这是由GC收集您创建的所有垃圾引起的。

您可以使用chrome:// tracing获取更多详细信息,以获取有关此信息。按record,然后按Edit Categories,然后选择blink_gc和webaudio甚至音频。然后记录痕迹并检查图形以查看发生了什么。