INTERACT FORUM

Please login or register.

Login with username, password and session length
Advanced search  
Pages: [1]   Go Down

Author Topic: Buffering Strategy: How to have your cake and eat it  (Read 2486 times)

ZoFreX

  • Recent member
  • *
  • Posts: 12
Buffering Strategy: How to have your cake and eat it
« on: January 14, 2009, 02:37:42 pm »

(This is for developers, primarily for the ones making Media Center, but feel free to chip in if you like this / hate this)

I recently made a high level audio engine in C++ using OpenAL. That's not really important but it sets the scene. We had a bug at one point with gapless playback skipping, and it was only later I realised the precise implications of what was happening to make the bug (at the time I was more concerned with getting rid of it).

To buffer audio continuously without gaps in OpenAL you use a queue of small buffers. When a buffer has been played you pull it off the front of the queue, fill it with audio data, and put it on the end of the queue. Larger total buffer size means less skipping, less cpu usage, and less responsiveness when we wanted to change tracks.

Hm, that tradeoff sounds familiar ;)

What we had accidentally managed to do was to cut into the buffer queue. What this means, in OpenAL at least, is that it would be possible to have a very very large playback buffer, but when you wanted to change what was being played (a track change, or seeking in the current track) you could cut into that buffer, to bring the change about almost instantaneously! (To compensate for the lower buffer and to protect from skipping, you might want to hit the update loop more often than usual while in this phase). Once the change was made you could build up a large buffer again.

This is a best of both worlds option: No skipping, responsive program.

Of course, you probably aren't using OpenAL, and maybe what you're using can't do this. But I thought it might interest you that it is possible in at least one case. Let me know what you think :)

Disclaimer: I never did rigorous testing into this approach to find out if it had any side-effects, whether it worked on all platforms supported by OpenAL, etc. Don't blame me if you try to do this and it doesn't work / breaks your program!
Logged

Matt

  • Administrator
  • Citizen of the Universe
  • *****
  • Posts: 42323
  • Shoes gone again!
Re: Buffering Strategy: How to have your cake and eat it
« Reply #1 on: January 14, 2009, 03:02:11 pm »

This is a best of both worlds option: No skipping, responsive program.

Thanks for the post.

Media Center accomplishes this same goal of having cake and eating it too by using a two-tiered buffer.

DSPs and sound-card delivery happen just-in-time (~0.2 second latency is plenty for a decent machine), but much more data is format converted, resampled, buffered, etc. in a secondary buffer.

It's a pretty elegant system, I think.  And it scales well to multi-processor machines.
Logged
Matt Ashland, JRiver Media Center

ZoFreX

  • Recent member
  • *
  • Posts: 12
Re: Buffering Strategy: How to have your cake and eat it
« Reply #2 on: January 14, 2009, 03:23:08 pm »

That two-tier approach sounds very interesting, I like it!

However, it doesn't let track changes happen immediately with a big buffer does it? (If my understanding is correct)
Logged

Matt

  • Administrator
  • Citizen of the Universe
  • *****
  • Posts: 42323
  • Shoes gone again!
Re: Buffering Strategy: How to have your cake and eat it
« Reply #3 on: January 14, 2009, 04:10:15 pm »

That two-tier approach sounds very interesting, I like it!

However, it doesn't let track changes happen immediately with a big buffer does it? (If my understanding is correct)

It does, because you can dump (or cross-fade, etc.) any amount of the secondary buffer since it hasn't been committed to the soundcard.
Logged
Matt Ashland, JRiver Media Center

ZoFreX

  • Recent member
  • *
  • Posts: 12
Re: Buffering Strategy: How to have your cake and eat it
« Reply #4 on: January 14, 2009, 04:27:13 pm »

Aah, I get ya. So which buffer does the skips / latency slider modify?
Logged

Matt

  • Administrator
  • Citizen of the Universe
  • *****
  • Posts: 42323
  • Shoes gone again!
Re: Buffering Strategy: How to have your cake and eat it
« Reply #5 on: January 14, 2009, 06:11:31 pm »

Aah, I get ya. So which buffer does the skips / latency slider modify?

The just-in-time buffer.
Logged
Matt Ashland, JRiver Media Center

ZoFreX

  • Recent member
  • *
  • Posts: 12
Re: Buffering Strategy: How to have your cake and eat it
« Reply #6 on: January 16, 2009, 07:27:04 am »

Ok. Based on that I've tried making the just-in-time buffer really tiny. So far: Very fast seeking, no skips :D (probably helped by using WASAPI, if anyone else is reading this - in Vista / Windows 7 I always get skips when using DirectSound). Thanks for your input Matt, really interesting to learn what's going on under the hood!
Logged
Pages: [1]   Go Up