Sign in to follow this  

Optimal streaming audio buffer size?

This topic is 4304 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

So I just added streaming music to my current project. I'm playing an Ogg Vorbis file through DirectSound and am noticing a perf hit with it. Changing the size of my buffer definately changes the behaviour of the perf problem so I was wondering if there is any rule of thumb for deciding on the size of such a buffer? If I make it bigger I go longer between having to load the data but if I make it smaller it doesn't have to load as much data at a time.

Share this post


Link to post
Share on other sites

Loading the data shouldn't be the bottle neck, unless using some other media than HDD. You can read the whole OGG file to memory and stream from there, although the loading will take a little while, but can be done in a thread so it isn't that bad.

I use OpenAL with OGG and if I remember correctly I use 4 buffers of size ... 16kb each.

You need to find the balance between the size and the amount of buffers. If there is too few buffers, the music might crack when some other thread is taking too much CPU power.

Share this post


Link to post
Share on other sites
There's no definitive rule as it depends on a number of factors, but here's some things to consider:

- typically seeking is slower than reading. eg. on a fast CD drive, it might take a third of a second to seek to the position you want, but in the next third of a second you could read potentially 2MB! (though this is strongly unlikely in practice). Similarly for hard drives, only less of an issue.

- therefore larger buffers mean fewer seeks, and the slightly longer reads is unlikely to be a problem. The downside of course is more memory, so you need to think about how much memory you can spare. But too small and you'll have too many seeks and the potential for stream starvation which won't sound nice!

- the size of the drive's own buffers/cache can affect speed. For example the system I've just finished a game for had a DVD cache of 128Kb, and we got the best streaming performance with reads of 128Kb. For a while we were reading 160Kb at a time but this meant a whole extra IO read for just 32Kb extra data. Dropping to 128Kb was a really big performance boost.

- all of this may very depending on whether you're streaming anything else (eg. game data) from the same device at the same time. If so, seeks will become even more expensive due to the data being in different places. In the extreme, you might need to optimise data layout on your disc such that commonly used data are contiguous to one another, and not fragmented... plus data on DVD is read faster but less reliably towards the outer edge... but I'd be surprised if your problem will be that hardcore!

- just in case you're doing synchronous IO reads... don't. (It's not clear from your post). Even when streaming from a hard drive, you might lose a whole frame to one head seek, and off optical media it would be hideous. Fire up another thread and do your file IO asynchronously.

As always, probably the best advice is to profile and experiment, and use whatever you find works best for your situation.

Share this post


Link to post
Share on other sites
I'm using OpenAL to stream an ogg for my game and I'm only using 2 buffers with 32k each. I'm not sure if that's the best way to do it but it seems to work fine on older computers as well. I did however put the streaming of the ogg into a seperate thread rather than in my game loop which seemed to have solved some issues with the sound cutting out and what not.

Share this post


Link to post
Share on other sites

This topic is 4304 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this