Advertisement

SDL audi callback ()

Started by January 02, 2005 07:21 PM
4 comments, last by Drew_Benton 20 years, 2 months ago
i have looked at the sdl audio stuff and its pretty easy to understand except for one think. What exactly should my audio callback function include? The docs i was reading said it will most likely be in a seprate thread? thanks[smile]
____________________________"This just in, 9 out of 10 americans agree that 1 out of 10 americans will disagree with the other 9"- Colin Mochrie
Quote:
Original post by raptorstrike
i have looked at the sdl audio stuff and its pretty easy to understand except for one think. What exactly should my audio callback function include? The docs i was reading said it will most likely be in a seprate thread?

thanks[smile]


According to "A Focus On SDL":

Quote:
The theorey behind the audio callback function is to give you, the programmer, ultimate control over what goes into the sound buffer.


Basically you set what you want to go into the sound buffer. In the book it has this:

Quote:

void FOSDLAudioCallBack(void* userdata, Uint8* buffer, int len)
{
int index;
for(index=0;index<len;index++)
{
buffer[index] = rand()%256; // randomly make sounds
}
}


I hope this helps some!

- Drew
Advertisement
yeah it helps but one last thing. How would i read an actual sound not just make random noise? what member of the spec would i make the buffer = to?
____________________________"This just in, 9 out of 10 americans agree that 1 out of 10 americans will disagree with the other 9"- Colin Mochrie
Quote:
Original post by raptorstrike
yeah it helps but one last thing. How would i read an actual sound not just make random noise? what member of the spec would i make the buffer = to?


You would have to stream it chunk by chunk from some source - using a wave file. In the book it has an example of using the SDL_LoadWAV to load in the data then streaming it with the callback. There is much more to it besides that - you have to convert/mix the data so it is correctly sounded. If you are really intrested in this - I'd suggest picking up a copy of the book - it explains a lot.

- Drew
this is alot more then i bargined for all i wanted to do is play some sound effects for a game [looksaround] I guess i have to figure this out thanks [smile]

EDIT: if its not to big do you think you could post the sample?
____________________________"This just in, 9 out of 10 americans agree that 1 out of 10 americans will disagree with the other 9"- Colin Mochrie
Quote:
Original post by raptorstrike
this is alot more then i bargined for all i wanted to do is play some sound effects for a game [looksaround] I guess i have to figure this out thanks [smile]

EDIT: if its not to big do you think you could post the sample?


Its kind of big and theres a lot of extras you have to do - otherwise it'd already be up [wink].

Now if you want to play sounds for your game - you can use SDL_mixer. Take a look at this tutorial from Cone3D. It goes over making a game that uses sound. I'd highly reccomend using it. Otherwise here is my code from a past project I used for sound. I hope it helps some:

Header file:
class Immortal_SDL_Audio{private:	union	{		Mix_Music *music;		Mix_Chunk *sound;	};	int type;public:	enum { AUDIO_MUSIC = 0, AUDIO_SOUND = 1 };	Immortal_SDL_Audio();	int Create(char* filename, int t);	void Cleanup();	void Fade_Music(int ms);	void Play_Music(int loops = 0);	void Fadeout_Music(int ms);	void Pause_Music();	void Resume_Music();	void Restart_Music();	void Stop_Music();	void Play_Sound(int channel);	void Fadeout_Sound(int channel,int ms);	void Pause_Sound(int channel);	void Resume_Sound(int channel);	void Stop_Sound(int channel);	Mix_Chunk * Get_Sound();	Mix_Music * Get_Music();};

Source file:
Immortal_SDL_Audio::Immortal_SDL_Audio(){}int Immortal_SDL_Audio::Create(char* filename, int t){	type = t;	if(type == AUDIO_MUSIC)	{		music = Mix_LoadMUS(filename);		if(!music)			return 0;	}	else if(type == AUDIO_SOUND)	{		sound = Mix_LoadWAV(filename);		if(!sound)			return 0;	}	return 1;}void Immortal_SDL_Audio::Cleanup(){	if(type == AUDIO_MUSIC)	{		Mix_FreeMusic(music);	}	else if(type == AUDIO_SOUND)	{		Mix_FreeChunk(sound);	}	}void Immortal_SDL_Audio::Play_Music(int loops){	if(type == AUDIO_SOUND)		return;	Stop_Music();	Mix_PlayMusic(music, loops);}void Immortal_SDL_Audio::Fadeout_Music(int ms){	if(type == AUDIO_SOUND)		return;	Mix_FadeOutMusic(ms);}void Immortal_SDL_Audio::Pause_Music(){	if(type == AUDIO_SOUND)		return;	Stop_Music();	Mix_PauseMusic();}void Immortal_SDL_Audio::Resume_Music(){	if(type == AUDIO_SOUND)		return;	Mix_ResumeMusic();}void Immortal_SDL_Audio::Restart_Music(){	if(type == AUDIO_SOUND)		return;	Mix_RewindMusic();}void Immortal_SDL_Audio::Stop_Music(){	if(type == AUDIO_SOUND)		return;	Mix_HaltMusic();}void Immortal_SDL_Audio::Play_Sound(int channel){	if(type == AUDIO_MUSIC)		return;	Stop_Sound(channel);	Mix_PlayChannel(channel,sound,0);}void Immortal_SDL_Audio::Fadeout_Sound(int channel,int ms){	if(type == AUDIO_MUSIC)		return;	Mix_FadeOutChannel(channel,ms);}void Immortal_SDL_Audio::Pause_Sound(int channel){	if(type == AUDIO_MUSIC)		return;	Mix_Pause(channel);}void Immortal_SDL_Audio::Resume_Sound(int channel){	if(type == AUDIO_MUSIC)		return;	Mix_Resume(channel);}void Immortal_SDL_Audio::Stop_Sound(int channel){	if(type == AUDIO_MUSIC)		return;	Mix_HaltChannel(channel);}Mix_Chunk* Immortal_SDL_Audio::Get_Sound(){	if(type == AUDIO_MUSIC)		return 0;	return sound;}Mix_Music* Immortal_SDL_Audio::Get_Music(){	if(type == AUDIO_SOUND)		return 0;	return music;}

Here is a sample of how to uses it:
/* Global variable */Immortal_SDL_Audio sound1;/* In your INIT function */if( !sound1.Create("filename.wav"),Immortal_SDL_Audio::AUDIO_SOUND) )    exit (-1); // sound cannot be loaded/* When you want to play */sound1.Play_Sound(0); // play on channel 0/* In your DEINIT function */sound1.Cleanup(); // clean up class

That's basically it for sounds! For music files - such as .xm,.ogg, or what ever else SDL_Mixer supports (NOT MP3!) you can do this:

/* Global variable */Immortal_SDL_Audio music1;/* In your INIT function */if( !music1.Create("filename.ogg"),Immortal_SDL_Audio::AUDIO_MUSIC) )    exit (-1); // sound cannot be loaded/* When you want to play */music1.Play_Music(); // play the music - only one at a time is possible /* In your DEINIT function */music1.Cleanup(); // clean up class


That's basically it, you can see the rest of the class - it just wraps up SDL_mixer , not much to it. That code was taken form the project - so it should compiler - make sure to include "SDL_mixer.h" and link in "SDL_mixer.lib". SDL_mixer is avalliable here.

- Drew

This topic is closed to new replies.

Advertisement