00001 #include "sdl_mixer/eMusicTrackSDLMixer.h"
00002
00003
00004
00005 #include "rSDL.h"
00006 #include "rScreen.h"
00007 #include "defs.h"
00008
00009 #include "tString.h"
00010 #include "tLinkedList.h"
00011 #include "tDirectories.h"
00012 #include "tConfiguration.h"
00013 #include "tConsole.h"
00014 #include "tError.h"
00015 #include "eGameObject.h"
00016 #include "eCoord.h"
00017 #include <iostream>
00018
00019 #include "eSoundMixer.h"
00020 #include "tIniFile.h"
00021
00022
00023 #include <math.h>
00024
00025
00026 #define SOUND_OFF 0
00027 #define SOUND_LOW 1
00028 #define SOUND_MED 2
00029 #define SOUND_HIGH 3
00030
00031
00032
00033
00034
00035
00036
00037 bool eMusicTrack::musicIsPlaying = 0;
00038 eMusicTrack* eMusicTrack::currentMusic = 0;
00039
00040 eMusicTrack::eMusicTrack() {
00041 #ifdef HAVE_LIBSDL_MIXER
00042
00043 if(! currentMusic) currentMusic = this;
00044 Init(true);
00045 #endif // DEDICATED
00046 }
00047
00048 eMusicTrack::eMusicTrack(const char* filename, bool isinstalled) {
00049 #ifdef HAVE_LIBSDL_MIXER
00050 Init(isinstalled);
00051
00052 tString musFile = tString(filename);
00053 m_CurrentSong = tSong(musFile);
00054 LoadSong( m_CurrentSong );
00055 #endif // DEDICATED
00056 }
00057
00058 void eMusicTrack::Init(bool isinstalled) {
00059 #ifdef HAVE_LIBSDL_MIXER
00060 m_IsInstalled = isinstalled;
00061 m_Music = NULL;
00062 m_Playlist = tNEW(tPlayList)();
00063 m_Loop = false;
00064 m_Volume = 100;
00065 m_HasSong = false;
00066 m_mixer = eSoundMixer::GetMixer();
00067 m_SequencePos = m_Tracklist.begin();
00068 #endif
00069 }
00070
00071 eMusicTrack::~eMusicTrack() {
00072 #ifdef HAVE_LIBSDL_MIXER
00073
00074 std::map<tString, Mix_Music*>::iterator trackIter;
00075
00076 for(trackIter = m_Tracklist.begin(); trackIter != m_Tracklist.end(); trackIter++) {
00077 Mix_FreeMusic( (*trackIter).second );
00078 }
00079
00080 delete m_Playlist;
00081 #endif // DEDICATED
00082 }
00083
00084 void eMusicTrack::UnloadSong() {
00085 #ifdef HAVE_LIBSDL_MIXER
00086 if(m_Music) Mix_FreeMusic(m_Music);
00087 m_Pos = 0.0;
00088 m_StartTime = 0;
00089 m_HasSong = false;
00090 #endif
00091 }
00092
00093 bool eMusicTrack::LoadSong(tSong thesong) {
00094 #ifdef HAVE_LIBSDL_MIXER
00095 tString extension;
00096 m_Filename = thesong.location;
00097
00098 if(m_Music) Mix_FreeMusic(m_Music);
00099
00100 extension = thesong.location.GetFileMimeExtension();
00101 if(extension == ".aatrack") {
00102 LoadAATrack(thesong);
00103 } else {
00104 LoadVorbisTrack(thesong);
00105 }
00106 std::map<tString, tString>::iterator trackIter;
00107
00108
00109 tString basepath("music/");
00110
00111 for(trackIter = thesong.tracklist.begin(); trackIter != thesong.tracklist.end(); trackIter++) {
00112
00113
00114
00115
00116
00117 basepath = (*trackIter).second;
00118
00119 m_Tracklist[(*trackIter).first] = LoadFile(basepath);
00120 }
00121 m_HasSong = true;
00122 #endif // DEDICATED
00123 return true;
00124 }
00125
00126 void eMusicTrack::LoadAATrack(tSong& thesong) {
00127 #ifdef HAVE_LIBSDL_MIXER
00128 tIniFile theFile(thesong.location);
00129
00130
00131 thesong.author = theFile.GetValue("header", "author");
00132 thesong.year = theFile.GetValue("header", "year");
00133 thesong.record = theFile.GetValue("header", "record");
00134 thesong.title = theFile.GetValue("header", "title");
00135 thesong.genre = theFile.GetValue("header", "genre");
00136
00137 tString isInstalledS = theFile.GetValue("header", "installed");
00138 if(isInstalledS == "yes") m_IsInstalled = true;
00139 else m_IsInstalled = false;
00140
00141 std::cout << thesong.title << " by " << thesong.author << "\n";
00142 thesong.tracklist = theFile.GetGroup("tracks");
00143 thesong.sequence = theFile.GetGroup("sequence");
00144 #endif
00145 }
00146
00147
00148 void eMusicTrack::LoadVorbisTrack(tSong& thesong) {
00149 #ifdef HAVE_LIBSDL_MIXER
00150
00151
00152 thesong.author = "Unknown";
00153 thesong.year = "Unknown";
00154 thesong.record = "Unknown";
00155 thesong.title = "Unknown";
00156 thesong.genre = "Unknown";
00157
00158 thesong.tracklist[tString("only")] = thesong.location;
00159 thesong.sequence[tString("0")] = "only";
00160 std::cout << "Track: " << thesong.location << "\n";
00161 thesong.title = thesong.location;
00162 #endif
00163 }
00164
00165 Mix_Music* eMusicTrack::LoadFile(const char* filename) {
00166 Mix_Music* theMusic = NULL;
00167 #ifdef HAVE_LIBSDL_MIXER
00168
00169 theMusic = Mix_LoadMUS( filename );
00170 if(!theMusic) {
00171
00172 tOutput error;
00173 error.SetTemplateParameter(1 , filename );
00174 error.SetTemplateParameter(2 , SDL_GetError() );
00175 error << "$sound_error_unknown";
00176 con << error;
00177 }
00178 #endif // DEDICATED
00179 return theMusic;
00180 }
00181
00182 void eMusicTrack::MusicFinished() {
00183 #ifdef HAVE_LIBSDL_MIXER
00184 m_SequencePos++;
00185
00186 if(m_SequencePos == m_Tracklist.end()) {
00187 m_mixer->SongFinished();
00188 return;
00189 }
00190 m_SequenceChange = true;
00191 std::cout << "Sequence change\n";
00192 #endif
00193 }
00194
00195 bool eMusicTrack::Play() {
00196 #ifdef HAVE_LIBSDL_MIXER
00197
00198
00199
00200 if(Mix_PlayingMusic() && !m_IsPlaying) {
00201 currentMusic->FadeOut();
00202 }
00203
00204 m_SequencePos = m_Tracklist.begin();
00205
00206 if(m_SequencePos == m_Tracklist.end()) {
00207 std::cout << "For some reason, this song has no tracks in the tracklist\n";
00208 musicIsPlaying = false;
00209 return false;
00210 } else {
00211 std::cout << "Should be playing the first sequence\n";
00212 m_HasSong = true;
00213 PlayCurrentSequence();
00214 }
00215
00216 #endif
00217 return musicIsPlaying;
00218 }
00219
00220 void eMusicTrack::PlayCurrentSequence() {
00221 #ifdef HAVE_LIBSDL_MIXER
00222 int pmres;
00223
00224 pmres = Mix_PlayMusic((*m_SequencePos).second, 1);
00225
00226 if( pmres == 0) {
00227 Mix_VolumeMusic(m_Volume);
00228 musicIsPlaying = true;
00229 m_IsPlaying = true;
00230 currentMusic = this;
00231 std::cout << "Playing the sequence\n";
00232 } else {
00233 std::cout << "Didn't play the sequence " << (*m_SequencePos).first << "\n";
00234 musicIsPlaying = false;
00235 m_HasSong = false;
00236 }
00237 #endif // DEDICATED
00238 }
00239
00240 void eMusicTrack::Previous() {
00241 #ifdef HAVE_LIBSDL_MIXER
00242 tSong newSong;
00243 tString musFile;
00244
00245 int numTries = 0;
00246 bool foundSong = false;
00247
00248 if(! m_Playlist->empty() ) {
00249 do {
00250 numTries++;
00251 m_CurrentSong = m_Playlist->GetPreviousSong();
00252 foundSong = LoadSong( m_CurrentSong );
00253 } while(!foundSong && numTries < 5);
00254 if(foundSong) {
00255 m_Pos = 0.0;
00256
00257 Play();
00258 m_isDirty = false;
00259 }
00260 }
00261 #endif
00262 }
00263
00264 void eMusicTrack::Next() {
00265 #ifdef HAVE_LIBSDL_MIXER
00266 tSong newSong;
00267 tString musFile;
00268 int numTries = 0;
00269 bool foundSong = false;
00270
00271 if(! m_Playlist->empty() ) {
00272 do {
00273 numTries++;
00274 m_CurrentSong = m_Playlist->GetNextSong();
00275 foundSong = LoadSong( m_CurrentSong );
00276 } while(!foundSong && numTries < 5);
00277 if(foundSong) {
00278 m_Pos = 0.0;
00279 m_HasSong = true;
00280 Play();
00281 m_isDirty = false;
00282 }
00283 }
00284 #endif
00285 }
00286
00287 void eMusicTrack::Pause() {
00288 #ifdef HAVE_LIBSDL_MIXER
00289 if(Mix_PausedMusic()==0) {
00290 Mix_PauseMusic();
00291 } else {
00292 Mix_ResumeMusic();
00293 }
00294 #endif
00295 }
00296
00297 void eMusicTrack::VolumeUp() {
00298 #ifdef HAVE_LIBSDL_MIXER
00299 m_Volume += 5;
00300 if(m_Volume > MIX_MAX_VOLUME) m_Volume = MIX_MAX_VOLUME;
00301 Mix_VolumeMusic(m_Volume);
00302 #endif
00303 }
00304
00305 void eMusicTrack::VolumeDown() {
00306 #ifdef HAVE_LIBSDL_MIXER
00307 m_Volume -= 5;
00308 if(m_Volume < 0) m_Volume = 0;
00309 Mix_VolumeMusic(m_Volume);
00310 #endif
00311 }
00312
00313 void eMusicTrack::FadeOut() {
00314 #ifdef HAVE_LIBSDL_MIXER
00315 if(m_IsPlaying) {
00316 if(Mix_PlayingMusic()) {
00317 Mix_FadeOutMusic(200);
00318 }
00319 }
00320 m_Pos = (double) (SDL_GetTicks() - m_StartTime) / 1000.0;
00321
00322 #endif // DEDICATED
00323 }
00324
00325 void eMusicTrack::LoadPlaylist(const char* filename) {
00326 #ifdef HAVE_LIBSDL_MIXER
00327 if(m_Playlist != NULL) delete m_Playlist;
00328
00329 m_Playlist = new tPlayList();
00330 m_Playlist->SetPlaylist(usePlaylist);
00331 if ( strlen(filename) > 0 )
00332 m_Playlist->LoadPlaylist(filename);
00333
00334 if( !m_Playlist->empty()) m_Playlist->Randomize();
00335 #endif
00336 }
00337
00338 void eMusicTrack::SetDirty() {
00339 #ifdef HAVE_LIBSDL_MIXER
00340 m_isDirty = true;
00341 #endif
00342 }
00343
00344 void eMusicTrack::Update() {
00345 #ifdef HAVE_LIBSDL_MIXER
00346 if(m_HasSong) {
00347
00348
00349 if(m_SequenceChange==true) {
00350 PlayCurrentSequence();
00351 m_SequenceChange = false;
00352 std::cout << "Sequence change updating\n";
00353 }
00354 if(m_Playlist) {
00355 if(m_isDirty) {
00356 Next();
00357 }
00358 }
00359 }
00360 #endif
00361 }
00362