cpal icon indicating copy to clipboard operation
cpal copied to clipboard

Having accumulated delay on wasapi

Open namse opened this issue 1 year ago • 2 comments

I was playing sound using cpal wasapi in release mode, and I found that the buffer length of the data callback was different from the actual expected sample rate.

The peculiar thing is that this delay difference increases in steps by 4000 bytes every about 1.5 seconds. In other words, the delay does not increase, but it suddenly increases once, and it accumulates, again and again.

So, in the environment I tested, a delay of about 1 second was sometimes generated when playing music for 1-2 minutes.

I wonder if this is something that actually happens. If so, do I have to implement playback sync myself?

I heard that rustdesk which uses cpal also has a similar issue https://github.com/rustdesk/rustdesk/issues/534

namse avatar Dec 30 '23 09:12 namse

I didn't get any delay with using wasapi directly with the same logic with https://github.com/HEnquist/wasapi-rs/blob/master/examples/playsine.rs#L73

Only cpal makes delay for me.

namse avatar Dec 30 '23 10:12 namse

....

EDIT: I didn't realize that you're playing while I am capturing. Could it be a sampling rate mismatch between the device and your samples?

EDIT 2: in case audio format mismatch is the cause: the example by HEnquist supports audio format conversion (using the WasApi AUDCLNT_STREAMFLAGS_AUTOCONVERTPCM flag I suppose), including sample rate conversion. Perhaps CPAL does not support it?

dheijl avatar Dec 30 '23 19:12 dheijl