I am a from Brazil, I like game, I like martial arts, I like computers, I like sci-fi and I LOVE making games.

Image RSS Feed Latest Screens
My programming environment
Blog RSS Feed Report abuse Latest Blog: Fixing buffer underrun...

0 comments by Original_Speeder on Aug 7th, 2010

The game had a serious problem: The awesome music from xaimus, caused slow-downs and buffer underruns, resulting in the music stuttering (among other undesired effects)

The reason for that, is how ClanLib handles streaming music: It waits for the buffer to end, and then it requests a hardcoded amount of data (1024*16 bytes), if this data is not calculated until the soundcard needs it, the result is a underrun.

The solution then was actually simple in theory, but hard to do for someone that never did it before, I would need to implement a ring buffer, and fill it in the background...

First, explaining what is a ring buffer: A ring buffer, is a buffer of fixed size, that you keep filling data, and reading data from it when necessary, when you read data, you overwrite the data that you just read with new data, it is called ring buffer, because when you reach the end, you start again in the beginning. This solve the problem because I fill it in advance, thus even if the music player slows-down it will take some time until it gets empty.

To fill it without slowing down everything else, or locking-up the game, I decided to make it in the background, so I learned how to use pthreads, a library to make threads (heh, you could not guess it, could you? :P), and it was really simple, all I needed to do was fire-up the player in another thread, and the player constantly fills the buffer, while clanlib reads the buffer, and both of them, before messing with the buffer, check if the other one is using it, so it avoid conflicts.

Although simple to do in theory, I never did that before, and I had several issues, first I accidentally made the buffer misbehave, by forgetting that it had stereo music, thus all addresses should have their values doubled, then I made some logic bugs, and some corner cases that caught me by surprise, like what happened when both pointers went to "0" but the buffer was full, the result was it re-filling again, resulting in the music suddenly skipping forward some seconds. Also I had the usual crashes caused by newbie errors, and etc...

After some extensive testing, seemly this solved all problems regarding music, now the game can play the awesome xaimus music without hassles! And I must say, that xaimus music is AWESOME! (well, for what other reason I would put it in the game?)

Start a group Groups
Awesome Games Factory

Awesome Games Factory

Developer & Publisher Must apply to join

Awesome Games Factory is a newcomer to the game industry, the company (under the name AGF) manufactured car repair equipment, but the current owner decided...



Entertainment & Press with 10,120 members, open to all members

Desura is a community driven digital distribution service for gamers, putting the best games, mods and downloadable content from developers at gamers...

Indie DB

Indie DB

Official with 2,180 members, open to all members

Indie games are changing the world, one giant pixel at a time. With Indie DB we aim to support independent developers and their games, by providing them...

Indie Devs

Indie Devs

Hobbies & Interests with 1,233 members, open to all members

A group dedicated to indie and standalone game development.

Robinson Technologies

Robinson Technologies

Developer & Publisher Must apply to join

Robinson Technologies is operated by the husband and wife team of Seth and Akiko Robinson and is located in Hiroshima, Japan. Seth has made a practice...



Official with 560 members, open to all members

Each month we bring to you new and old (updated) game and mod playthroughs, trailers and weekly recaps covering the mod and indie gaming scene, so sit...

Post comment Comments
Garyn Dakari Nov 16 2010 says:

I just wanna say, I really like your game and keep up the good work!

+2 votes     reply to comment
AcidBarrel Jul 27 2010 says:

Hey how are you doing buddy!

+2 votes     reply to comment
Original_Speeder Creator
Original_Speeder Aug 7 2010 replied:

Doing the game \o/

+2 votes   reply to comment
Post a Comment
click to sign in

You are not logged in, your comment will be anonymous unless you join the community today (totally free - or sign in with your social account on the right) which we encourage all contributors to do.

2000 characters limit; HTML formatting and smileys are not supported - text only

Offline Since
Feb 17, 2015
Brazil Brazil
Member Watch
Track this member
Desura Indie DB Spotlight
Activity Points
8,329 of 524,082
1 member
Time Online
1 second
Site Visits
Profile Visitors
3,454 (2 today)
Private Message
Send Now
Members Only
Members Only
Google Talk
Members Only
iQew friends since Dec 6, 2010
Gamieon friends since Jun 4, 2011
shaulhadar friends since Dec 12, 2011
hirato friends since Aug 7, 2010
AcidBarrel friends since Jul 27, 2010
JamV friends since Dec 27, 2010
Shadowscythe friends since Sep 23, 2010