sdlmixer.h

Go to the documentation of this file.
00001 /*
00002   libwt - Vassilis Virvilis Toolkit - a widget library
00003   Copyright (C) 2006 Vassilis Virvilis <vasvir2@fastmail.fm>
00004  
00005   This library is free software; you can redistribute it and/or
00006   modify it under the terms of the GNU Lesser General Public
00007   License as published by the Free Software Foundation; either
00008   version 2.1 of the License, or (at your option) any later version.
00009   
00010   This library is distributed in the hope that it will be useful,
00011   but WITHOUT ANY WARRANTY; without even the implied warranty of
00012   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013   Lesser General Public License for more details.
00014   
00015   You should have received a copy of the GNU Lesser General Public
00016   License along with this library; if not, write to the
00017   Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00018   Boston, MA  02111-1307, SA.
00019 */
00020 
00021 #ifndef WT_SDLMIXER_H
00022 #define _SDLMIXER_H
00023 
00024 // \todo Music is not forward declarable
00025 #include <SDL/SDL_mixer.h>
00026 
00027 #include <boost/shared_ptr.hpp>
00028 
00029 #include <wt/flagmap.h>
00030 #include <wt/audio.h>
00031 
00032 struct Mix_Chunk;
00033 
00034 namespace Wt {
00035 
00036 /// Wrapper class around the audio mixer
00037 class SDLMixer : public Audio {
00038     friend class Singleton<Audio>;
00039 public:
00040     static Factory<Audio, SDLMixer> factory;
00041 
00042     //forward declaration needed
00043     class Channel;
00044     /// SDLMixer::Chunk implementation class
00045 class Chunk : public Audio::Chunk {
00046         friend class SDLMixer;
00047         friend class Channel;
00048     public:
00049         virtual void setVolume(int volume);
00050         virtual void play(int duration = -1);
00051         virtual void fadeIn(int ms, int duration = -1);
00052         virtual void fadeOut(int ms);
00053 
00054         /// pause chunk
00055         virtual void pause();
00056         /// resume chunk
00057         virtual void resume();
00058         /// stop chunk
00059         virtual void stop(int ms = 0);
00060 
00061         virtual bool playing() const;
00062         virtual bool paused() const;
00063 
00064         operator Mix_Chunk *() {
00065             return chunk_p.get();
00066         }
00067 
00068         operator const Mix_Chunk *() const {
00069             return chunk_p.get();
00070         }
00071 
00072         virtual Audio::Channel *channel() const;
00073 
00074         virtual Audio::Chunk *clone() const;
00075         virtual void detach();
00076 
00077         ~Chunk();
00078 
00079     protected:
00080         Chunk(Mix_Chunk *chunk);
00081 
00082     private:
00083         /// wraps up the Mix_Chunk deletion so we can use
00084         /// boost smart pointers with it
00085         class Deleter {
00086         public:
00087             void operator()(Mix_Chunk* c);
00088         };
00089 
00090         typedef boost::shared_ptr<Mix_Chunk> Ptr;
00091         Ptr chunk_p;
00092         int channel_id;
00093     };
00094 
00095     virtual Audio::Chunk* loadChunk(const std::string& filename) const;
00096 
00097 class Channel : public Audio::Channel {
00098         friend class Audio;
00099         friend class SDLMixer;
00100         friend class Chunk;
00101     public:
00102         virtual void setVolume(int);
00103         virtual void play(Audio::Chunk& chunk, int duration = -1);
00104         virtual void fadeIn(Audio::Chunk& chunk, int ms, int duration = -1);
00105         virtual void fadeOut(int ms);
00106         /// pause channel
00107         virtual void pause();
00108         /// resume channel
00109         virtual void resume();
00110         /// stop channel
00111         virtual void stop(int ms = 0);
00112 
00113         virtual bool playing() const;
00114         virtual bool paused() const;
00115         virtual int fading() const;
00116 
00117         virtual Audio::Chunk *chunk() const;
00118 
00119     protected:
00120         Channel(int id);
00121     private:
00122         Audio::Chunk *chunk_p;
00123         int channel_id;
00124     };
00125 
00126     virtual int allocateChannels(int channels);
00127 
00128     /// pause all channels
00129     virtual void pause();
00130     /// resume all channels
00131     virtual void resume();
00132     /// stop all channels
00133     virtual void stop(int ms = 0);
00134     virtual void fadeOut(int ms);
00135 
00136     virtual int playingChannels() const;
00137     virtual int pausedChannels() const;
00138 
00139     virtual bool isAvailable() const;
00140 
00141     //music
00142 class Music : public Audio::Music {
00143         friend class SDLMixer;
00144     protected:
00145         Music(Mix_Music *music_p);
00146     public:
00147         virtual int type() const;
00148 
00149         operator Mix_Music *() const {
00150             return music_p;
00151         }
00152 
00153         virtual ~Music();
00154     private:
00155         Mix_Music *music_p;
00156     };
00157 
00158     virtual Audio::Music *loadMusic(const std::string& filename);
00159     virtual void playMusic(Audio::Music&);
00160     virtual void fadeInMusic(Audio::Music&, int ms);
00161     virtual void fadeOutMusic(int ms);
00162     virtual void setMusicVolume(int volume);
00163     virtual void pauseMusic();
00164     virtual void rewindMusic();
00165     virtual bool setMusicPosition(double start_sec);
00166     virtual void stopMusic();
00167     virtual bool playingMusic() const;
00168     virtual bool pausedMusic() const;
00169     virtual int musicFading() const;
00170 
00171 protected:
00172     ~SDLMixer();
00173     SDLMixer();
00174     static Audio* load();
00175 
00176     virtual int minHWVolume() const;
00177     virtual int maxHWVolume() const;
00178 
00179     /// callback called when a channel finishes
00180     static void onChannelFinish(int channel);
00181 
00182 private:
00183     static FlagMap fadingTypeMap;
00184     static FlagMap musicTypeMap;
00185 };
00186 
00187 } // namespace Wt
00188 
00189 #endif // _SDLMIXER_H

Generated Fri Jul 28 19:23:00 2006.
Copyright © 1998-2003 by the respective authors.

This document is licensed under the terms of the GNU Free Documentation License and may be freely distributed under the conditions given by this license.