Kevin Pastor
Kevin Pastor

Reputation: 801

Limited playback rate using the Web Audio API

I'm trying to use the Web Audio API to create a simple synthesizer, but I'm having problem with the playback rate of an AudioBuffer. Its value seems to be limited when I try using a somewhat high value.

Here's my code sample. I first create a sample buffer containing a simple waveform that has as much samples as the sample rate (this creates a waveform at 1Hz if it was directly read). I then create an AudioBuffer that will contain the samples. Finaly, I create an AudioBufferSourceNode that will play the previous buffer in a loop at a some playback rate (which translate to an audible frequency).

const audioContext = new window.AudioContext();

const buffer = new Float32Array(audioContext.sampleRate);
for (let i = 0; i < buffer.length; i++) {
    buffer[i] = Math.sin(2 * Math.PI * i / audioContext.sampleRate);
}

const audioBuffer = new AudioBuffer({
    length: buffer.length,
    numberOfChannels: 1,
    sampleRate: audioContext.sampleRate
});
audioBuffer.copyToChannel(buffer, 0);

const sourceNode = new AudioBufferSourceNode(audioContext, {
    buffer: audioBuffer,
    loop: true,
    playbackRate: 440
});
sourceNode.connect(audioContext.destination);

sourceNode.start();

In this scenario, playbackRate seems to be limited at 1024. Any value higher than this will be not have any audible effect. I verified its maximum value (sourceNode.playbackRate.maxValue) and it's around 3.4e+38, which is way above what I'm trying to achieve.

I'm wondering if there's something I'm missing or if my understanding of this API is wrong. I'm using Google Chrome on the latest version if this changes anything.

Upvotes: 1

Views: 390

Answers (1)

Raymond Toy
Raymond Toy

Reputation: 6056

This is a bug in Chrome: https://crbug.com/712256. I believe Firefox may implement this correctly (but I did not check).

Note, also, that Chrome uses very simple interpolation. The quality of the output degrades quite a bit when the playbackRate is very different from 1.

Upvotes: 1

Related Questions