使用 Web Audio 剪辑:如何让 Firefox 表现得像 Chrome?

Clipping with Web Audio: How to make Firefox behave like Chrome?

Canopy 中执行的代码片段:

// @channels 1
// @duration 0.25
// @sampleRate 44100

var osc = new OscillatorNode(context);
var gain = context.createGain();
gain.gain.setValueAtTime(2, 0.25/2);
osc.connect(gain);
gain.connect(context.destination);
osc.start();

使用 Audacity 录制:

Chrome 74 个剪辑,Firefox 66 没有。

我可以让 Firefox 也剪掉信号吗?(这是我所期望的)

如果您想确保输出被裁剪,您至少可以通过三种方式实现:

  • 使用WaveShaperNode为您执行剪辑。 (随处可用)。
  • 使用 ScriptProcessorNode(已弃用)进行剪裁。
  • 使用 AudioWorkletNode 进行剪辑。 (尚未随处可用。)

示例:(由 OP 添加,请仔细检查)

// @channels 1
// @duration 0.25
// @sampleRate 44100

var osc = new OscillatorNode(context);
var gain = context.createGain();
gain.gain.setValueAtTime(2, 0.25/2);
osc.connect(gain);

var waveShaper = new WaveShaperNode(context, {
    curve: new Float32Array([-1, 1])
});
gain.connect(waveShaper);
waveShaper.connect(context.destination);

osc.start();