Author Topic: Sample playback latency when something else is playing  (Read 378 times)

YaLTeR

  • Posts: 2
Hello!

I'm working on a rhythm game in C# using ManagedBass and I'm investigating sample playback latency on Linux. My setup is Arch Linux x64 with PulseAudio 12.2 with the default config and BASS 2.4.14.1.

We're using streams for the game audio playback and samples for the so-called hitsounds which must play immediately on player input, so whenever the player presses a button a hitsound sample plays back. The way we play back samples are by using Bass.SampleGetChannel() immediately followed by Bass.ChannelPlay() of an existing pre-loaded sample.

The issue is as follows. Whenever there's nothing playing in the background (so neither the audio stream nor any samples are playing) this way of sample playback results in basically no latency. Perfect. However, whenever there is something already playing (the audio stream or another sample), the samples have a noticeable ~30ms latency.

Is there any way to ensure samples are always played with minimal possible latency, regardless of whether something else is currently being played? Maybe there are some buffering flags I'm missing?

Ian @ un4seen

  • Administrator
  • Posts: 21986
By default, BASS won't output any data unless something is playing, so the output buffer is empty when you start the first sample, resulting in lower latency. For more consistent latency, that can be disabled with the BASS_CONFIG_DEV_NONSTOP option. To lower the latency, you can reduce the size of the output buffer with the BASS_CONFIG_DEV_BUFFER option (also see BASS_CONFIG_DEV_PERIOD) before calling BASS_Init, but going too low may result in breaks in the sound. You can reduce the chances of that by allowing BASS to use realtime priority for its output thread by running as "root" or giving permission in your /etc/security/limits.conf file. For example:

Code: [Select]
yourusername - rtprio 20

YaLTeR

  • Posts: 2
Thank you very much! BASS_CONFIG_DEV_NONSTOP made the latency consistent and then I was able to bring it down to essentially zero via BASS_CONFIG_DEV_PERIOD and BASS_CONFIG_DEV_BUFFER. Everything sounds amazing.

smoogipoo

  • Posts: 1
@Ian, does the output buffer / device period still need to be considered if BASS is updated manually?

Ian @ un4seen

  • Administrator
  • Posts: 21986
Yes. BASS_Update and BASS_ChannelUpdate update playback buffers. The device buffer is separate (where data from the playback buffers get mixed).