snapcast icon indicating copy to clipboard operation
snapcast copied to clipboard

Add adaptive resample to avoid click-sounds during synchronization when copying/removing samples

Open YetAnotherElectronicsChannel opened this issue 2 years ago • 0 comments

Hello,

first of all - this is an awesome project.

I'm working on my own implementation of a Snapcast-Client and have been digging a little bit deeper in the code. The Snapcast-Client synchronizes to the Server by copying or removing samples of incoming audio-stream when it is too much in the past or in the future.

For most cases, this method of syncing is hardly audible, however for some sort of music (e.g. piano, solo-voice sounds, speech) you can hear random clicking sound in the loudspeakers... a good example is this song here: https://open.spotify.com/track/5KqEPvurLuOoV64tNylPO6?si=xA4bszsrS06XGwec9OpqOQ

I have fixed this issue by using this resampling-algortihm from cpuimage: https://github.com/cpuimage/resampler

In my system, the audio from the snapcast-server runs into an Audio-Fifo which is timestamp-aware. The DMA which fetches the samples and forwards it to the I²S module in my microcontroller works on fixed buffer sizes of 512 stereo-samples. In my first implementation I limited the sample stuffing/copying to one (stereo-) sample per buffer.

Now I'm using this resampling algo. So whenever I'm too much in past, I take 511 (stereo-)samples out of the fifo and resample it to 512 samples, or if I'm too much in the past I take 513 stereo-samples and resample it also to 512. If the stream is in time, I just take 512 samples as they are.

I heard now several music with this and the syncing isn't absolutely audible anymore.

Here a screenshot of my code. My system runs fixed on 44.1 kHz and shows the spot where for example it uses the resampling for buffer instead of copying the sample. image

Maybe this is an idea to consider for the next release...