00001
00002
00003
00004
00005
00006
00007 #ifdef WIN32
00008 #pragma warning(disable : 4786 )
00009 #endif
00010 #include "CSMusic.h"
00011 #include "CSXMLHelper.h"
00012
00013 using namespace std;
00014 const char *CSMusic::CLASS = "CSMusic";
00015
00016 CSMusicLoader CSMusicLoader::INSTANCE;
00017
00018
00019 CSMusic::CSMusic(const std::string &filename)
00020 {
00021 static char *functionName="CSMusic";
00022 LOG_ENTER
00023 CSMusicData data;
00024 mMusic = 0;
00025 mMPEG = 0;
00026 loadMusicData(filename, data);
00027 initialize(data);
00028 LOG_EXIT
00029 }
00030
00031 void CSMusic::initialize(const CSMusicData &data)
00032 {
00033 static char *functionName="initialize";
00034 LOG_ENTER
00035
00036
00037 if (isMP3(data.filename))
00038 {
00039
00040 SMPEG_Info info;
00041 mMPEG = SMPEG_new(data.filename, &info, 0);
00042 if (!SMPEG_error( mMPEG ))
00043 {
00044
00045 SMPEG_enablevideo(mMPEG, 0);
00046 SMPEG_enableaudio(mMPEG, 0);
00047 }
00048 else
00049 {
00050 SMPEG_delete(mMPEG);
00051 mMPEG = 0;
00052 mMusic = Mix_LoadMUS(data.filename);
00053 }
00054 }
00055 else
00056 {
00057 mMusic = Mix_LoadMUS(data.filename);
00058 }
00059 LOG_EXIT
00060 }
00061
00062 CSMusic::CSMusic(const CSMusic &music)
00063 {
00064 static char *functionName="CSMusic";
00065 LOG_ENTER
00066 mMusic = music.mMusic;
00067 LOG_EXIT
00068 }
00069
00070 CSMusic::~CSMusic()
00071 {
00072 static char *functionName="~CSMusic";
00073 LOG_ENTER
00074 stop();
00075
00076 if (mMPEG)
00077 {
00078 SMPEG_delete(mMPEG);
00079 mMPEG = 0;
00080 }
00081
00082 if (mMusic)
00083 {
00084 Mix_FreeMusic(mMusic);
00085 mMusic = 0;
00086 }
00087 LOG_EXIT
00088 }
00089
00090 void CSMusic::play()
00091 {
00092 static char *functionName="play";
00093 LOG_ENTER
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120 LOG_EXIT
00121 }
00122
00123 void CSMusic::stop()
00124 {
00125 static char *functionName="stop";
00126 LOG_ENTER
00127 int frequency;
00128 Uint16 format;
00129 int channels;
00130 int state = Mix_QuerySpec(&frequency, &format, &channels);
00131 if (state)
00132 {
00133 if (mMPEG)
00134 {
00135
00136 SMPEG_stop(mMPEG);
00137 Mix_HookMusic(NULL, NULL);
00138 SMPEG_enableaudio(mMPEG, 0);
00139 }
00140 else if (mMusic)
00141 {
00142 Mix_HaltMusic();
00143 }
00144 }
00145 LOG_EXIT
00146 }
00147
00148 void CSMusic::pause()
00149 {
00150 static char *functionName="pause";
00151 LOG_ENTER
00152 int frequency;
00153 Uint16 format;
00154 int channels;
00155 int state = Mix_QuerySpec(&frequency, &format, &channels);
00156 if (state)
00157 {
00158 if (mMPEG)
00159 {
00160
00161 SMPEG_pause(mMPEG);
00162 }
00163 else if (mMusic)
00164 {
00165 Mix_PauseMusic();
00166 }
00167 }
00168 LOG_EXIT
00169 }
00170
00171 void CSMusic::resume()
00172 {
00173 static char *functionName="resume";
00174 LOG_ENTER
00175 int frequency;
00176 Uint16 format;
00177 int channels;
00178 int state = Mix_QuerySpec(&frequency, &format, &channels);
00179 if (state)
00180 {
00181 if (mMPEG)
00182 {
00183
00184 SMPEG_pause(mMPEG);
00185 }
00186 else if (mMusic)
00187 {
00188 Mix_ResumeMusic();
00189 }
00190 }
00191 LOG_EXIT
00192 }
00193
00194 void CSMusic::loadMusicData(const std::string &filename, CSMusicData &data)
00195 {
00196 static char *functionName="loadMusicData";
00197 LOG_ENTER
00198 data.filename = strdup(filename.c_str());
00199 LOG_EXIT
00200 }
00201
00202 CSMusic::isMP3(const char *name)
00203 {
00204 static char *functionName="isMP3";
00205 LOG_ENTER
00206
00207
00208 if (strstr(name,".mp3") ||
00209 strstr(name,".Mp3") ||
00210 strstr(name,".mP3") ||
00211 strstr(name,".MP3"))
00212 {
00213 LOG_EXIT
00214 return true;
00215 }
00216 LOG_EXIT
00217 return false;
00218 }