Main Page | Namespace List | Class Hierarchy | Alphabetical List | Data Structures | File List | Data Fields | Globals | Related Pages

CSMusic.cpp

Go to the documentation of this file.
00001 /**
00002     CSMusic
00003       CSMusic can only be created thru a loader (CSMusicLoader).
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 // constructor using "*.pic" file
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     // if Musics are to large - they get garbled during
00036     // conversion if in FULLSCREEN (Windows)
00037     if (isMP3(data.filename))
00038     {
00039         /* Note the last parameter is zero! */
00040         SMPEG_Info info;
00041         mMPEG = SMPEG_new(data.filename, &info, 0);
00042         if (!SMPEG_error( mMPEG ))
00043         {
00044             /* Play the movie, using SDL_mixer for audio */
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     // CSA TODO: Uncomment to play music!
00095     /*
00096     stop();
00097     if (mMPEG)
00098     {
00099         SDL_AudioSpec audiofmt;
00100         Uint16 format;
00101         int freq, channels;
00102 
00103         // Tell SMPEG what the audio format is 
00104         Mix_QuerySpec(&freq, &format, &channels);
00105         audiofmt.format = format;
00106         audiofmt.freq = freq;
00107         audiofmt.channels = channels;
00108         SMPEG_actualSpec(mMPEG, &audiofmt);
00109 
00110         // Hook in the MPEG music mixer 
00111         Mix_HookMusic(SMPEG_playAudioSDL, mMPEG);
00112         SMPEG_enableaudio(mMPEG, 1);
00113         SMPEG_play(mMPEG);
00114     }
00115     else if (mMusic)
00116     {
00117         Mix_PlayMusic(mMusic, 0);
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             /* Stop the movie and unhook SMPEG from the mixer */
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             /* Pause/Resume playback of an SMPEG object */
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             /* Pause/Resume playback of an SMPEG object */
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     // for now only simple checkking!
00207     // filename!
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 }

Generated on Wed Jul 14 00:43:31 2004 for CSLib by doxygen 1.3.6