Rapidly playing a sound via media source is overloading the buffer

djwhatley

New Member
I've written a Lua script to simulate an old-school terminal display and output text one character at a time, while also playing a sound effect for each character. To play the sound, it grabs a media source loaded up with the effect (~50ms in duration, 9KB as .wav) and restarts the source:

Lua:
function play_sfx1()
  local media_source_sfx1 = obs.obs_get_source_by_name(sfx1_source_name)
  if media_source_sfx1 then
    obs.obs_source_media_restart(media_source_sfx1)
    obs.obs_source_release(media_source_sfx1)
  end
end

The characters are printed out with a small delay between each one. I started at 50ms, and while it played fine through the audio monitor, the sound in the encoded output was very laggy. I bumped it up to 75ms, but the difference is small, and the problem remains. On checking the logs, I can see the issue:

Code:
15:27:02.977: Max audio buffering reached!
15:27:02.977: adding 960 milliseconds of audio buffering, total audio buffering is now 960 milliseconds (source: Startup SFX 1)
15:27:02.977:
15:27:02.998: Source Startup SFX 1 audio is lagging (over by 13160.93 ms) at max audio buffering. Restarting source audio.

So it's pretty clearly overloading the audio buffer, though I haven't been able to figure out why. The .wav is at the same sample rate I'm using in OBS (48kHz), and again the audio itself is only about 50ms long. It feels to me like it has to be related to rapidly restarting the audio source, but I've tried a lot of different things at this point, and can't seem to sort it out.

I'm considering just rewriting the entire thing in a web page and capturing the audio from that.. but before I do that, does anyone know of a way to solve this within OBS?
 

djwhatley

New Member
It seems worth noting that simply looping the media source does not result in this issue, even when playing it for far longer than the script repeats the sound. I reworked my script to take advantage of this, just looping the sound when printing out characters, and synchronizing the character display with the duration of the audio clip. This works fine, until I stop the clip and restart it looping again on another line, which runs into the max buffering issue again, though with less noticeable impact.

It has to be something about restarting the source that is causing this. I may spend some time looking at the OBS source code to see if I can figure it out, but probably the way I will ultimately have to solve this is to just pre-record the exact sequence of the sound that I want and play it only once at the start.
 
Top