00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #include "sound.h"
00029
00030 int Sound::init_done = 0;
00031
00032 #ifndef NOSOUND //use sound routines if NOSOUND is not defined
00033
00034 Sound::Sound(int max_samples){
00035 if(SOUND_DEBUG) cerr << "Initializing Sound class....." << endl;
00036 if(SOUND_DEBUG) cerr << "NUM_SAMPLES: " << max_samples << endl;
00037 if(!init_done){
00038
00039 NUM_SAMPLES = max_samples;
00040
00041
00042 spec.freq = 22050;
00043 spec.format = MIX_DEFAULT_FORMAT;
00044 spec.channels = MIX_DEFAULT_CHANNELS;
00045 spec.samples = 1024;
00046
00047
00048 if (Mix_OpenAudio(spec.freq, spec.format,
00049 spec.channels, spec.samples)==-1) {
00050 cerr << "Mix_OpenAudio: " << Mix_GetError() << endl;
00051
00052 exit(2);
00053 }
00054
00055
00056 Mix_AllocateChannels(NUM_SAMPLES);
00057
00058
00059 Mix_SetPanning(MIX_CHANNEL_POST, 127, 127);
00060
00061 bgm = NULL;
00062 bgm_name = "";
00063 bgm_loaded = 0;
00064 bgm_loop = 0;
00065
00066 sfx = new Mix_Chunk[NUM_SAMPLES];
00067
00068 sfx_name.assign(NUM_SAMPLES, "");
00069 for(int i=0;i<NUM_SAMPLES;i++){
00070 sfx_loaded[i] = 0;
00071 }
00072 init_done = 1;
00073 }
00074 if(SOUND_DEBUG) cerr << "Done initializing Sound class!" << endl;
00075 }
00076
00077 Sound::Sound(){
00078
00079 }
00080
00081 Sound::~Sound(){
00082 if(SOUND_DEBUG) cerr << "Deleting Sound class" << endl;
00083 Mix_FreeMusic(bgm);
00084 for(int i=0;i<NUM_SAMPLES;i++) Mix_FreeChunk(sfx+i);
00085 Mix_CloseAudio();
00086 }
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117 int Sound::load_bgm(string filename){
00118 if(SOUND_DEBUG) cerr << "Entering Sound::load_bgm" << endl;
00119 if ( (bgm = Mix_LoadMUS(filename.c_str())) == NULL ) {
00120 cerr << "Couldn't load bgm " << filename << ": " << SDL_GetError() << endl;
00121 exit(1);
00122 }
00123 if(SOUND_DEBUG) cerr << "bgm file: " << filename << endl;
00124 bgm_loaded = 1;
00125 bgm_name = new char [filename.size()+1];
00126 strcpy (bgm_name, filename.c_str());
00127 return(1);
00128 }
00129
00130 int Sound::load_sfx(string filename){
00131 vector<string>::iterator it = sfx_name.begin();
00132 if(SOUND_DEBUG) cerr << "Entering Sound::load_sfx" << endl;
00133 int i = 0;
00134 Mix_Chunk *mix;
00135 while(sfx_loaded[i]) i++;
00136 if(i >= NUM_SAMPLES) {cerr << "Oh boy, we need a bigger sound effects array!" << endl; exit(0);}
00137 if ( (mix = Mix_LoadWAV(filename.c_str()) ) == NULL ) {
00138 cerr << "Couldn't load sample " << filename << ": " << SDL_GetError() << endl;
00139 exit(1);
00140 }
00141 *(sfx+i) = *mix;
00142 sfx_name.insert(it+i, filename);
00143 if(SOUND_DEBUG) cerr << "Sound::load_sfx Loaded filename: " << sfx_name.at(i) << endl;
00144 sfx_loaded[i] = 1;
00145 return(i);
00146 }
00147
00148
00149 int Sound::get_status(){
00150 int i;
00151 if(bgm_loaded) return(1);
00152 for(i=0;i<NUM_SAMPLES;i++){
00153 if(sfx_loaded[i]) return(1);
00154 }
00155 return(0);
00156 }
00157
00158 int Sound::play_bgm(){
00159 if (bgm_loaded){
00160 Mix_PlayMusic(bgm, -1);
00161 play_flag = 1;
00162 }
00163 return(1);
00164 }
00165
00166
00167 int Sound::play_sample_name(string name){
00168 int i;
00169 string filename;
00170 for(i=0;i<NUM_SAMPLES;i++){
00171 filename = sfx_name.at(i);
00172
00173 if (filename.compare(name) == 0 && sfx_loaded[i]){
00174 if(SOUND_DEBUG) cerr << "play_sample_number channel: " << i << endl;
00175 Mix_PlayChannel(i, sfx+i, 0);
00176 play_flag = 1;
00177 }
00178
00179 }
00180 return(i);
00181 }
00182
00183
00184 int Sound::play_sample_number(int channel){
00185 if (channel >= 0 && channel < NUM_SAMPLES && sfx_loaded[channel]) {
00186 if(SOUND_DEBUG) cerr << "play_sample_number channel: " << channel << endl;
00187 Mix_PlayChannel(channel, sfx + channel, 0);
00188 play_flag = 1;
00189 }
00190 return(channel);
00191 }
00192
00193 int Sound::stop_all(){
00194 Mix_HaltChannel(-1);
00195 Mix_HaltMusic();
00196 play_flag = 0;
00197 return(1);
00198 }
00199
00200 int Sound::pause_all(){
00201 Mix_Pause(-1);
00202 Mix_PauseMusic();
00203 play_flag = 0;
00204 return(1);
00205 }
00206
00207 int Sound::resume_all(){
00208 Mix_Resume(-1);
00209 Mix_ResumeMusic();
00210 play_flag = 1;
00211 return(1);
00212 }
00213
00214 void Sound::raise_volume(){
00215 if (volume < MIX_MAX_VOLUME){
00216 volume += 10;
00217 if (volume > MIX_MAX_VOLUME){ volume = MIX_MAX_VOLUME; }
00218 Mix_Volume(-1, volume);
00219 Mix_VolumeMusic(volume);
00220 }
00221 }
00222
00223 void Sound::lower_volume(){
00224 if (volume > MIX_MAX_VOLUME){
00225 volume -= 10;
00226 if (volume < 0){ volume = 0; }
00227 Mix_Volume(-1, volume);
00228 Mix_VolumeMusic(volume);
00229 }
00230 }
00231
00232 void Sound::set_volume(int vol){
00233 if (vol >= 0 && vol <= MIX_MAX_VOLUME){
00234 volume = vol;
00235 Mix_Volume(-1, volume);
00236 Mix_VolumeMusic(volume);
00237 }
00238 }
00239
00240 #endif
00241
00242 #ifdef NOSOUND
00243
00244
00245 Sound::Sound(int NUM_SAMPLES){ }
00246 Sound::~Sound(){ }
00247 void Sound::load_bgm(string filename){ }
00248 void Sound::load_sfx(string filename){ }
00249 int Sound::get_status(){ return(1); }
00250 int Sound::play_bgm(){ return(1); }
00251 int Sound::play_sample_name(string name){ return(1); }
00252 int Sound::play_sample_number(int number){ return(1); }
00253 int Sound::stop_all(){ return(1); }
00254 int Sound::pause_all(){ return(1); }
00255 int Sound::resume_all(){ return(1); }
00256 void Sound::raise_volume(){ }
00257 void Sound::lower_volume(){ }
00258 void Sound::set_volume(int volume){ }
00259
00260 #endif