Sign in to follow this  

Requirements for audio engine

This topic is 4731 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

Hello all, I've completed the input and logging modules for my engine, and now I want to get some sound code working. What all is required from an audio subsystem, though? Is it simply storing all API-dependant sound effect and music structures in a private list and accessing them by index? What's the deal? Thanks ahead of time, Ben

Share this post


Link to post
Share on other sites
1. Loading the sounds and managing them (- not loading the same sound twice, de-allocation)

2. Channel management - if you have lots of sounds then you have to manage the channels and prioritze which sounds should be played. In any case you have to check when a channel has finished playing and set it as available.

3. Streaming music

That's what i can think of at the moment. (2) is the hardest to implement.

If you look at FMOD you can see that it does all of these for you but is not free for commercial use. If you are just working on a hobby project then it's the way to go.

Share this post


Link to post
Share on other sites
Look into openAl (open audio language), it's like openGL for audio programming. It's cross platform, easy to set up and most sounds can be added with only four additional lines of code!
enjoy,
JDev

Share this post


Link to post
Share on other sites
Yeah I was thinking of using fmod eventually. Thanks, JDV, for reminding me of OpenAL. SDL_mixer could be used in the meantime because that's what I have access to at the moment. As you can tell, I'm about covering over the API with a restricting interface, and then its internals can be changed more easily. I'll do some more research and see what I come up with. Thanks guys.

Share this post


Link to post
Share on other sites
Quote:
Original post by Benjamin Heath
Hello all,

I've completed the input and logging modules for my engine, and now I want to get some sound code working. What all is required from an audio subsystem, though? Is it simply storing all API-dependant sound effect and music structures in a private list and accessing them by index? What's the deal?

Thanks ahead of time,
Ben


Well this is what I do to avoid the common 'use index' method. Here is some of my non-current source code for the Audio manager (sdl based):


Immortal_SDL_Audio_Manager::Immortal_SDL_Audio_Manager()
{
audio.resize(10);
names.resize(10,"");
}

Immortal_SDL_Audio_Manager::~Immortal_SDL_Audio_Manager()
{
Cleanup();
}

void Immortal_SDL_Audio_Manager::Cleanup()
{
if(audio.size() < 1)
return;
for(vector<Immortal_SDL_Audio>::iterator itr = audio.begin();itr != audio.end();itr++)
{
(*itr).Cleanup();
}
audio.resize(0);
names.resize(0);
}

int Immortal_SDL_Audio_Manager::FindNextSlot()
{
int index = 0;
for(index = 0;index<(int)audio.size();index++)
{
if(strcmp( names.at(index).c_str(), "") == 0)
break;
}
return index;
}

void Immortal_SDL_Audio_Manager::Remove(char* name)
{
int index = NameToInt(name);
audio.at( index ).Cleanup();
names.at( index ) = "";
}

int Immortal_SDL_Audio_Manager::NameToInt(char* name)
{
int ind = 0;
for(vector<string>::iterator itr = names.begin();itr != names.end(); itr++,ind++)
{
if( strcmp( (*itr).c_str(),name ) == 0 )
return ind;
}
return -1;
}

void Immortal_SDL_Audio_Manager::Remove(int index)
{
audio.at( index ).Cleanup();
names.at( index ) = "";
}

int Immortal_SDL_Audio_Manager::Add(char* filename,char* refname,int type)
{
int index = FindNextSlot();
if(index >= (int)audio.size())
{
audio.resize( (audio.size() + 5) );
names.resize( (names.size() + 5) );
}

names.at(index) = refname;

if( !audio.at(index).Create(filename,type) )
return 0;

return 1;
}

Immortal_SDL_Audio* Immortal_SDL_Audio_Manager::NameToAudio(string name)
{
int ind = 0;
for(vector<string>::iterator itr = names.begin(); itr != names.end(); itr++,ind++)
{
if( strcmp( (*itr).c_str(),name.c_str() ) == 0 )
return &audio.at(ind);
}
return 0;
}

Immortal_SDL_Audio* Immortal_SDL_Audio_Manager::operator()(string name)
{
return NameToAudio(name);
}



I decided to utilize STL so I can use physical names instead of indexs. In a program I decalre an Immortal_SDL_Audio variable. The first thing I do is call Add to add in a sound. Then, whenever I want to use the sound, I can use the overloaded () operator with a string of the audio I want.
Immortal_SDL_Audio var;
var("sound1") --> returns the audio associated with the name "sound1". I personally like this approach with the speed hits ocmpared to pure indexing. It makes using and trackingyour sounds oo much easier! I think the trade offs are definitly worth it.

Share this post


Link to post
Share on other sites
Quote:
Original post by Puzzler183
You would be better off with a list or map I beleive... You need iteration through it, back insertion and random deletion. Lists provide all of those in constant time.

Agreed. A (hash-)map is the best choice for this.

Share this post


Link to post
Share on other sites
If you have the inclination to purchase it, James Boer's Game Audio Programming is an excellent resource for this sort of thing. In the book he defines a set of interfaces for an audio library and takes you through implementing it on top of DirectSound and DirectMusic. From there it's fairly easy to create your own implementations for FMOD, OpenAl or whatever. The design he uses for the library is such that it can be applied to other subsystems as well. There are several good ideas to borrow in there even if you don't use his code.

Share this post


Link to post
Share on other sites
what I ended up doing (more for the learning I guess) - was doing the DirectSound route, but taking it a bit lower level.
DS is a bit weird in that it offers you many ways to do the same thing.. It effectivly has all the features mentioned (streaming, channel managment, etc) or it can have none of them, where all you get is a bunch of raw data buffers wihch get mixed.. So you end up doing most everything.

I ended up taking the later [wink] (typical me)

so I ended up with the 'sound engine' residing on a seperate, higher priority thread.. Sleeping for the majority of the time, but every 50ms it would update all the buffers, which were simple looping buffers 8k in length (not as easy as it sounds! :).. So it was effectivly streamed. Plus other stuff.
It actually worked _really_ well. Initially, I didn't have it on another thread, and just allocated buffers for the entire sound file, and the performance of this method was noticably worse. Plus of course it could go bad if the game froze up or such.

Another little advantage it had was that it just took in my own 'stream' class, which meant I could play sounds/music from the internet if I ever really had the need [smile] - not that I did though


It was a really good learning experience actually.

Making it backward compatible with older integrated AC-97 and the like was certainly a challange, but it worked. And in some cases it worked wher FMOD didn't (well, at least other programs I had that used FMOD didn't)


Honestly I feel a Heap would be the best data structure for the channels. As they could be kept sorted by 'importance' (loudness, distance, etc).. And all ops on it would be log(n).

[edit]

ohh yeah last thing I'd add, is that I initally tried to use OpenAL, but even their own demos simply did not work. And I have a SoundBlaster Live.
I'm sure it's probably fixed now, but that put me off it for a good while I think.

Share this post


Link to post
Share on other sites
Quote:
Original post by Aldacron
If you have the inclination to purchase it, James Boer's Game Audio Programming is an excellent resource for this sort of thing. In the book he defines a set of interfaces for an audio library and takes you through implementing it on top of DirectSound and DirectMusic. From there it's fairly easy to create your own implementations for FMOD, OpenAl or whatever. The design he uses for the library is such that it can be applied to other subsystems as well. There are several good ideas to borrow in there even if you don't use his code.
I just ordered it. Thanks for the tip. [smile]

Share this post


Link to post
Share on other sites

This topic is 4731 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