WebAudio API: Change pitch of samples (for example mp3)

- 1 answer

Ad

I figured I can stretch a sample using playbackRate but how do I set the pitch? Im trying to achieve this without using any libraries.

I found something about a doppler pitch effect in the specification but other than that I couldnt find anything truly relevant to my objective. Just a simple pitch shift, is this even possible? If so then how?

Edit: Could it be that playbackRate changes the pitch as well but the browser applies some sort of correction? I think this might be the case. I have to disable it then if that is the case and possible.

Ad

Answer

Ad

There is no "simple" pitch shift for samples. There are lots of ways to change the pitch without changing the playback rate - granular resynthesis, phase-shift vocoding - but none of them are trivial, and none are baked into the Web Audio API. (Doppler is being removed, by the way, since it was pretty hacky.) playbackRate is literally the rate at which the samples will be run through - which will affect both "speed" and apparent pitch (of pitched samples).

I implemented a pitch-shifting (granular resynthesis) effect in the Input Effects demo - using the jungle.js library - but this isn't a trivial thing to do. HTML's <audio> element does stretching, but doesn't specify the algorithm, which leaves it open to a lot of interpretations.

Ad
source: stackoverflow.com
Ad