00001
00025 #include <stdio.h>
00026 #include <stdlib.h>
00027 #include <string.h>
00028
00029 #include <SDL/SDL.h>
00030 #include <SDL/SDL_mixer.h>
00031
00032 #include "music.h"
00033 #include "main.h"
00034 #include "sdlengine.h"
00035 #include "hqrdepack.h"
00036 #include "resources.h"
00037
00039 #define MUSIC_FOLDER "music"
00040
00041 #define NUM_CD_TRACKS 10
00042
00043 #define FADE_MS 500
00044
00046 SDL_CD *cdrom;
00048 const char *cdname;
00049
00051 Mix_Music *current_track;
00052
00054 unsigned char * midiPtr;
00055
00056
00059 void music_volume(int volume)
00060 {
00061
00062 Mix_VolumeMusic(volume/2);
00063 }
00064
00067 void music_fade_in(int loops, int ms)
00068 {
00069 Mix_FadeInMusic(current_track, loops, ms);
00070 }
00071
00074 void music_fade_out(int ms)
00075 {
00076 while (!Mix_FadeOutMusic(ms) && Mix_PlayingMusic()) {
00077 SDL_Delay(100);
00078 }
00079 Mix_RewindMusic();
00080 }
00081
00082
00085 void play_track_music_cd(int track)
00086 {
00087 if(cdrom->numtracks == 10)
00088 {
00089 if(CD_INDRIVE(SDL_CDStatus(cdrom)))
00090 SDL_CDPlayTracks(cdrom, track, 0, 1, 0);
00091
00092 if(cfgfile.Debug)
00093 printf("Playing track (CD): %d\n",track);
00094 }
00095 }
00096
00098 void stop_track_music_cd()
00099 {
00100 if(cfgfile.UseCD)
00101 {
00102 if(cdrom!=NULL)
00103 {
00104 SDL_CDStop(cdrom);
00105
00106 if(cfgfile.Debug)
00107 printf("Stop CD track\n");
00108 }
00109 }
00110 }
00111
00114 void play_track_music_mp3(int track)
00115 {
00116 char musfile[256];
00117 if(cfgfile.UseMP3)
00118 sprintf(musfile, MUSIC_FOLDER "//%d.mp3",track);
00119 else
00120 sprintf(musfile, MUSIC_FOLDER "//%d.ogg",track);
00121
00122 stop_track_music_mp3();
00123
00124 music_fade_in(1,FADE_MS);
00125
00126 current_track = Mix_LoadMUS(musfile);
00127 music_volume(cfgfile.MusicVolume);
00128 Mix_PlayMusic(current_track, -1);
00129
00130 if(cfgfile.Debug)
00131 {
00132 if(cfgfile.UseMP3)
00133 printf("Playing track (MP3): %s\n",musfile);
00134 else
00135 printf("Playing track (OGG): %s\n",musfile);
00136 }
00137 }
00138
00140 void stop_track_music_mp3()
00141 {
00142 if (current_track != NULL)
00143 {
00144 Mix_FreeMusic(current_track);
00145 current_track = NULL;
00146
00147 if(cfgfile.Debug)
00148 printf("Stop MP3 track\n");
00149 }
00150 }
00151
00154 void play_track_music(int track)
00155 {
00156 if(!cfgfile.NoSound)
00157 {
00158 if(track==currentMusic)
00159 return;
00160 currentMusic = track;
00161
00162 stop_midi_music();
00163
00164 if(cfgfile.UseHQSnd)
00165 play_track_music_mp3(track);
00166 else
00167 if(cfgfile.UseCD)
00168 play_track_music_cd(track);
00169 }
00170 }
00171
00173 void stop_track_music()
00174 {
00175 if(!cfgfile.NoSound)
00176 {
00177 music_fade_out(FADE_MS);
00178
00179 if(cfgfile.UseHQSnd)
00180 stop_track_music_mp3();
00181 else
00182 if(cfgfile.UseCD)
00183 stop_track_music_cd();
00184 }
00185 }
00186
00189 void play_midi_music(int midiIdx, int loop)
00190 {
00191 if(!cfgfile.NoSound)
00192 {
00193 int midiSize;
00194 char filename[256];
00195 SDL_RWops *rw;
00196
00197 stop_track_music();
00198
00199 if(midiIdx==currentMusic)
00200 return;
00201 currentMusic = midiIdx;
00202
00203 if(cfgfile.UseHQSnd)
00204 {
00205 if(cfgfile.UseAVI)
00206 sprintf(filename,"%s", HQR_MIDI_MI_WIN_MP3_FILE);
00207 else
00208 sprintf(filename,"%s", HQR_MIDI_MI_WIN_OGG_FILE);
00209 }
00210 else
00211 sprintf(filename,"%s", HQR_MIDI_MI_WIN_FILE);
00212
00213 if(midiPtr)
00214 {
00215 music_fade_out(FADE_MS/2);
00216 stop_midi_music();
00217
00218 }
00219 midiPtr = (unsigned char*)malloc(hqr_entry_size(filename,midiIdx) * sizeof(unsigned char));
00220
00221 midiSize = hqr_get_entry(midiPtr, filename, midiIdx);
00222 rw = SDL_RWFromMem(midiPtr,midiSize);
00223
00224 current_track=Mix_LoadMUS_RW(rw);
00225
00226 music_fade_in(1,FADE_MS);
00227
00228 music_volume(cfgfile.MusicVolume);
00229
00230 if(Mix_PlayMusic(current_track, loop)==-1)
00231 printf("Error while playing music: %d \n",midiIdx);
00232
00233 if(cfgfile.Debug)
00234 {
00235 if(cfgfile.UseHQSnd)
00236 {
00237 if(cfgfile.UseAVI)
00238 printf("Playing Music (MP3): %d\n",midiIdx);
00239 else
00240 printf("Playing Music (OGG): %d\n",midiIdx);
00241 }
00242 else
00243 printf("Playing Music (MIDI): %d\n",midiIdx);
00244 }
00245 }
00246 }
00247
00249 void stop_midi_music()
00250 {
00251 if(!cfgfile.NoSound)
00252 {
00253 if (current_track != NULL)
00254 {
00255 Mix_FreeMusic(current_track);
00256 current_track = NULL;
00257 if(midiPtr != NULL)
00258 free(midiPtr);
00259
00260 if(cfgfile.Debug)
00261 printf("Stop MIDI music\n");
00262 }
00263 }
00264 }
00265
00267 int init_cdrom()
00268 {
00269 if(!cfgfile.NoSound)
00270 {
00271 int numOfCDROM;
00272 int cdNum;
00273
00274 numOfCDROM = SDL_CDNumDrives();
00275 if(cfgfile.Debug)
00276 printf("Found %d CDROM devices\n", numOfCDROM);
00277
00278 if (!numOfCDROM)
00279 {
00280 fprintf(stderr, "No CDROM devices available\n");
00281 return 0;
00282 }
00283
00284 for(cdNum=0; cdNum < numOfCDROM; cdNum++)
00285 {
00286 cdname = SDL_CDName(cdNum);
00287 if(cfgfile.Debug)
00288 printf("Testing drive %s\n", cdname);
00289 cdrom = SDL_CDOpen(cdNum);
00290 if(!cdrom)
00291 {
00292 if(cfgfile.Debug)
00293 fprintf(stderr, "Couldn't open CD drive: %s\n\n", SDL_GetError());
00294 }
00295 else
00296 {
00297 SDL_CDStatus(cdrom);
00298 if(cdrom->numtracks==NUM_CD_TRACKS)
00299 {
00300 printf("Assuming that it is LBA cd... %s\n\n",cdname);
00301 cdDir = "LBA";
00302 cfgfile.UseCD = 1;
00303 return 1;
00304 }
00305 }
00306
00307 cfgfile.UseCD = 0;
00308 SDL_CDClose(cdrom);
00309 }
00310
00311 cdrom=NULL;
00312
00313 printf("Can't find LBA CD!\n\n");
00314 }
00315 return 0;
00316 }