Main Page | Class Hierarchy | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals

SDLSoundAgent Class Reference

Inheritance diagram for SDLSoundAgent:

Inheritance graph
[legend]
Collaboration diagram for SDLSoundAgent:

Collaboration graph
[legend]

Detailed Description

Sound and music.

Definition at line 13 of file SDLSoundAgent.h.

Public Member Functions

virtual int playSound (Mix_Chunk *sound, int volume, int loops=0)
 Play this sound.
virtual void playMusic (const Path &file, BaseMsg *finished)
 Play music.
virtual void stopMusic ()

Protected Member Functions

virtual void own_init ()
 Init sound subsystem.
virtual void own_shutdown ()
virtual void setSoundVolume (int volume)
 Set sound volume.
virtual void setMusicVolume (int volume)


Member Function Documentation

void SDLSoundAgent::own_init  )  [protected, virtual]
 

Init sound subsystem.

Exceptions:
SDLException when SDL_INIT_AUDIO cannot be initialized
MixException when SDL_mixer cannot be initialized

Reimplemented from SoundAgent.

Definition at line 28 of file SDLSoundAgent.cpp.

00029 {
00030     m_music = NULL;
00031     m_soundVolume = MIX_MAX_VOLUME;
00032     if(SDL_InitSubSystem(SDL_INIT_AUDIO) < 0) {
00033         throw SDLException(ExInfo("SDL_InitSubSystem"));
00034     }
00035 
00036     int frequency =
00037        OptionAgent::agent()->getAsInt("sound_frequency", MIX_DEFAULT_FREQUENCY);
00038     if(Mix_OpenAudio(frequency, MIX_DEFAULT_FORMAT, 2, 1024) < 0) {
00039         throw MixException(ExInfo("Mix_OpenAudio"));
00040     }
00041     Mix_AllocateChannels(16);
00042 
00043     SoundAgent::own_init();
00044 }

void SDLSoundAgent::own_shutdown  )  [protected, virtual]
 

Reimplemented from BaseAgent.

Definition at line 47 of file SDLSoundAgent.cpp.

00048 {
00049     stopMusic();
00050     Mix_CloseAudio();
00051     SDL_QuitSubSystem(SDL_INIT_AUDIO);
00052 }

void SDLSoundAgent::playMusic const Path file,
BaseMsg finished
[virtual]
 

Play music.

Parameters:
file path to music file (i.e. *.ogg)
finished send this message when music is finished. If finished is NULL, play music forever.

Implements SoundAgent.

Definition at line 112 of file SDLSoundAgent.cpp.

00114 {
00115     stopMusic();
00116 
00117     int loops = -1;
00118     if (finished) {
00119         ms_finished = finished;
00120         loops = 1;
00121     }
00122 
00123     m_music = Mix_LoadMUS(file.getNative().c_str());
00124     if (m_music && (0 == Mix_PlayMusic(m_music, loops))) {
00125         Mix_HookMusicFinished(musicFinished);
00126     }
00127     else {
00128         LOG_WARNING(ExInfo("cannot play music")
00129                 .addInfo("music", file.getNative())
00130                 .addInfo("Mix", Mix_GetError()));
00131     }
00132 }

int SDLSoundAgent::playSound Mix_Chunk *  sound,
int  volume,
int  loops = 0
[virtual]
 

Play this sound.

Parameters:
sound chunk to play
volume percentage sound volume
loops numer of loops. 0=play once, 1=play twice, -1=play infinite
Returns:
channel number where the sound is played, return -1 on error or when sound is NULL

Implements SoundAgent.

Definition at line 64 of file SDLSoundAgent.cpp.

00065 {
00066     int channel = -1;
00067     if (sound) {
00068         channel = Mix_PlayChannel(-1, sound, loops);
00069         if (-1 == channel) {
00070             //NOTE: maybe there are too few open channels
00071             LOG_WARNING(ExInfo("cannot play sound")
00072                     .addInfo("Mix", Mix_GetError()));
00073         }
00074         else {
00075             Mix_Volume(channel, m_soundVolume * volume / 100);
00076         }
00077     }
00078 
00079     return channel;
00080 }

void SDLSoundAgent::setMusicVolume int  volume  )  [protected, virtual]
 

Parameters:
volume percentage volume, e.g. 30=30

Implements SoundAgent.

Definition at line 138 of file SDLSoundAgent.cpp.

00139 {
00140     Mix_VolumeMusic(MIX_MAX_VOLUME * volume / 100);
00141 }

void SDLSoundAgent::setSoundVolume int  volume  )  [protected, virtual]
 

Set sound volume.

NOTE: all already running sound will get equals volume

Parameters:
volume percentage volume, e.g. 30=30

Implements SoundAgent.

Definition at line 89 of file SDLSoundAgent.cpp.

00090 {
00091     m_soundVolume = MIX_MAX_VOLUME * volume / 100;
00092     if (m_soundVolume > MIX_MAX_VOLUME) {
00093         m_soundVolume = MIX_MAX_VOLUME;
00094     }
00095     else if (m_soundVolume < 0) {
00096         m_soundVolume = 0;
00097     }
00098     Mix_Volume(-1, m_soundVolume);
00099 }

void SDLSoundAgent::stopMusic  )  [virtual]
 

Implements SoundAgent.

Definition at line 144 of file SDLSoundAgent.cpp.

00145 {
00146     if(Mix_PlayingMusic()) {
00147         Mix_HookMusicFinished(NULL);
00148         Mix_HaltMusic();
00149     }
00150     if (m_music) {
00151         Mix_FreeMusic(m_music);
00152         m_music = NULL;
00153     }
00154     if (ms_finished) {
00155         delete ms_finished;
00156         ms_finished = NULL;
00157     }
00158 }


The documentation for this class was generated from the following files:
Generated on Wed Jun 1 09:56:45 2005 for Fish Fillets - Next Generation by  doxygen 1.4.2