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?)

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...

Desura

Desura

Official group with 10,091 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 Devs

Indie Devs

Hobbies & Interests group with 1,110 members, open to all members

A group dedicated to indie and standalone game development.

IndieDB

IndieDB

Official group with 2,014 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...

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...

Spotlight

Spotlight

Official group with 533 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
Garyn Dakari Nov 16 2010, 3:38pm says:

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

+2 votes     reply to comment
AcidBarrel
AcidBarrel Jul 27 2010, 5:58pm says:

Hey how are you doing buddy!

+2 votes     reply to comment
Original_Speeder
Original_Speeder Aug 7 2010, 12:45am 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

Level
Avatar
Avatar
Offline Since
Feb 25, 2014
Country
Brazil Brazil
Gender
Male
Member Watch
Track this member
Accolades
Desura IndieDB Spotlight
Statistics
Activity Points
321
Rank
7,593 of 475,554
Watchers
1 member
Time Online
1 second
Comments
33
Site Visits
2,060
Profile Visitors
2,750 (1 today)
Contact
Homepage
Agfgames.com
Private Message
Send Now
Email
Members Only
Steam
kelvinhart
Yahoo
Members Only
Google Talk
Members Only
iQew
iQew friends since Dec 6, 2010
Gamieon
Gamieon friends since Jun 4, 2011
shaulhadar
shaulhadar friends since Dec 12, 2011
hirato
hirato friends since Aug 7, 2010
AcidBarrel
AcidBarrel friends since Jul 27, 2010
JamV
JamV friends since Dec 27, 2010
Shadowscythe
Shadowscythe friends since Sep 23, 2010