00001 #include "constants.h"
00002 #include "sound.h"
00003
00004 Mix_Chunk *SoundHandler :: sounds[NR_SOUNDS];
00005 Mix_Music *SoundHandler :: music;
00006 int SoundHandler :: soundChannels[NR_SOUNDS];
00007 bool SoundHandler :: soundOn;
00008 bool SoundHandler :: musicOn;
00009
00010 bool SoundHandler :: init( )
00011 {
00012
00013
00014 int audio_rate = 22050;
00015 Uint16 audio_format = AUDIO_S16;
00016 int audio_channels = 2;
00017 int audio_buffers = 1024;
00018
00019
00020
00021 if(Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers))
00022 {
00023 printf("Unable to open audio!\n");
00024 return false;
00025 }
00026
00027
00028 sounds[SND_EXPLOSION] = Mix_LoadWAV( SOUND_PATH "explosion.wav");
00029 sounds[SND_THRUST] = Mix_LoadWAV( SOUND_PATH "thrust.wav");
00030
00031 music = 0;
00032
00033 soundOn = true;
00034 musicOn = true;
00035
00036 return true;
00037 }
00038
00039 void SoundHandler :: cleanUp()
00040 {
00041
00042 if ( sounds[SND_EXPLOSION] )
00043 Mix_FreeChunk( sounds[SND_EXPLOSION] );
00044 if ( sounds[SND_THRUST] )
00045 Mix_FreeChunk( sounds[SND_THRUST] );
00046
00047 if ( music )
00048 Mix_FreeMusic( music );
00049
00050 Mix_CloseAudio();
00051 }
00052
00053 int SoundHandler :: playSound( int soundNr, int channel, int nrLoops, int playBeforeFinished )
00054 {
00055 if ( !soundOn )
00056 return -1;
00057
00058 if ( soundNr >= NR_SOUNDS || !sounds[soundNr] )
00059 return -1;
00060
00061 Mix_Chunk *chunk = sounds[soundNr];
00062 if( channel == -1 || !Mix_Playing( channel ) )
00063 channel = Mix_PlayChannel(-1, sounds[soundNr], nrLoops);
00064
00065 Mix_Volume( channel, MIX_MAX_VOLUME );
00066 return channel;
00067 }
00068
00069 void SoundHandler :: stopSound( int channel, bool fadeOut, int nrMilliSecs )
00070 {
00071 if ( !soundOn )
00072 return;
00073
00074 if ( !fadeOut )
00075 Mix_HaltChannel( channel );
00076 else
00077 {
00078 Mix_FadeOutChannel( channel, nrMilliSecs );
00079 }
00080 }
00081
00082 void SoundHandler :: playMusic( string musicFile )
00083 {
00084 if ( !soundOn )
00085 return;
00086
00087
00088 if ( music )
00089 {
00090 stopMusic();
00091 Mix_FreeMusic( music );
00092 }
00093
00094
00095 music = Mix_LoadMUS( musicFile.c_str() );
00096 if(!music)
00097 {
00098 printf("Mix_LoadMUS(%s): %s\n", musicFile.c_str(), Mix_GetError());
00099
00100 }
00101
00102 playMusic();
00103 }
00104
00105 void SoundHandler :: playMusic( bool fade )
00106 {
00107 if ( !musicOn )
00108 return;
00109
00110 if ( music )
00111 {
00112 Mix_VolumeMusic( MIX_MAX_VOLUME );
00113 Mix_RewindMusic();
00114
00115 if ( fade )
00116 Mix_FadeInMusic( music, -1, 1000 );
00117 else
00118 Mix_PlayMusic( music, -1 );
00119
00120 }
00121 }
00122
00123 void SoundHandler :: stopMusic( bool fadeOut )
00124 {
00125 if ( !music || !Mix_PlayingMusic() )
00126 return;
00127
00128 if ( fadeOut && Mix_FadingMusic() == MIX_NO_FADING )
00129 {
00130 Mix_FadeOutMusic( 1000 );
00131 }
00132 else
00133 {
00134 Mix_HaltMusic();
00135 }
00136
00137 }
00138
00139 void SoundHandler :: setMusicVolume( int vol )
00140 {
00141 Mix_VolumeMusic( vol );
00142 }
00143
00144 void SoundHandler :: setSoundsOn( bool val )
00145 {
00146 soundOn = val;
00147 }
00148
00149 void SoundHandler :: setMusicOn( bool val )
00150 {
00151 musicOn = val;
00152
00153 if ( !musicOn )
00154 stopMusic();
00155 else
00156 playMusic( true );
00157 }