RustAudio / cpal

Cross-platform audio I/O library in pure Rust
Apache License 2.0
2.71k stars 361 forks source link

latency in playback? #103

Closed bburdette closed 6 years ago

bburdette commented 8 years ago

I'm writing a program to run faust dsp code (which compiles to c++) from inside rust. This appears to be working fine, but I'm running into a latency issue.

You can change parameters to the faust dsp code to alter its output. I've got a simple patch that just outputs white noise with a single paramter, Volume.

What I'm seeing is that when I change the volume, it takes several seconds for the alteration to propogate out to the sound that I'm hearing from my laptop speakers. I'm using voice.append_data to hand over my dsp output values to cpal.

I suspect that cpal has a several second buffer internally which is getting filled up; when I change the volume it happens right away but that goes onto the back of the cpal queue and it takes a while for the change to be heard.

If this is the case, I don't really see how to configure it in cpal. Maybe there's something I'm missing? For this DSP stuff I really want to have latency as low as possible.

tomaka commented 8 years ago

I suspect that cpal has a several second buffer internally which is getting filled up; when I change the volume it happens right away but that goes onto the back of the cpal queue and it takes a while for the change to be heard.

If this is the case, I don't really see how to configure it in cpal. Maybe there's something I'm missing? For this DSP stuff I really want to have latency as low as possible.

That's probably what happens. You should try submitting fewer samples at once. Don't call append_data(size_of_data) but append_data(min(size_of_data, size_of_ten_milliseconds_of_data)) instead.

bburdette commented 8 years ago

ah, that does appear to be helpful, although after a while it reverts to having latency again. I'm not properly tracking how quickly samples need to be produced, I'll try monitoring the amount of time that has passed and feeding samples to cpal accordingly.

pacmancoder commented 8 years ago

I think cpal really lack of buffer size setting. I making emulator at the moment and I need to make synchronization of sound with video. With portaudio it was pretty easy, but I decided to switch from it because of very tricky compilation on windows and bugs. It would be great if it will be possible to set buffer size for small latency, because using thread::sleep for waiting before next buffer fill is very inaccurate and produces many sound glitches. Can this be implemented?

mitchmindtree commented 6 years ago

I've added an issue about providing an API for requesting certain buffer sizes at #205. The original issue of data submission size can no longer be an issue with the current CPAL API (there is no longer an append_data method). Going to close this in favour of #205.