by rock_artist · 7y ago · 0 comments
Timers in general will jitter.
Audio processing usually involves a callback to process an audio block (array of audio samples... floats).
To simplify things, this audio block runs on realtime thread and copies the block to desired output. you're guaranteed it'll be continuous between each call. (you can cause overflow/stutter if you hold it for too long).
With setTimer approaches each callback WILL have offset. But if you need to synchronize things or your metronome needs to run with other audio, it WILL be jerky. That's why a proper metronome would also be 'sample accurate'.
WebAudio / AudioContext was made exactly to solve that!
similar to common audio processing standard, you end up getting a callback and usually need to use WebAsm/C++ under the hood to keep the golden DSP rule - never allocate on block processing callback.
btw, on Firefox 69 (macOS) only the pre-scheduled audio made a sound :)
The next attempt is AudioWorklet, which runs in a separate js context and on the audio thread. Sadly still exclusively in Chrome, so I can't be bothered to invest heavily in it.
> btw, on Firefox 69 (macOS) only the pre-scheduled audio made a sound :)
Also similar experience here, same version. Also "stop" didn't stop the pre-scheduled audio. It continued until 20 ticks.
Once you get the hang of it it’s all about DSP. FFT, filter design etc. Also most actual DSP code is C/C++ depends on deployment needs.
another approach is to try Faust, PureData. Look those up and see what’s looks as a good starting point for you.