diff --git a/SDLWrapper/Sound.cpp b/SDLWrapper/Sound.cpp index fd12b8a..5852cd2 100644 --- a/SDLWrapper/Sound.cpp +++ b/SDLWrapper/Sound.cpp @@ -1,20 +1,140 @@ #include "Sound.h" #include "begin_code.h" +//private void Sound::_set(Mix_Chunk* p) { _sound.reset(p,Mix_FreeChunk); } - -void Sound::_clear()//private +//private +void Sound::_clear() { _sound.reset(); } - -Mix_Chunk* Sound::_get() +//private +Mix_Chunk* Sound::_get() const { return _sound.get(); } +Sound::Sound(const std::string& WAVFilename) +{ + _set(Mix_LoadWAV(WAVFilename.c_str())); +} + +bool Sound::isReady() const +{ + return (_get()!=nullptr); +} + +void Sound::release() +{ + _clear(); +} + +//private +void Channel::_set(int ChannelID) +{ + _id=ChannelID; +} +//private +int Channel::_get() const +{ + return _id; +} +//private +void Channel::_clear() +{ + _id=-1; +} +//protected +Channel::Channel() +{ + _id=-1; +} + +Channel& Channel::playSound(Sound sound, int loops) throw (ErrorViewer) +{ + int cret=Mix_PlayChannel(_get(),sound._get(),loops); + if(cret==-1) + { + ErrorViewer e; + e.fetch(); + throw e; + } + _set(cret); + return *this; +} + +Channel& Channel::fadeIn(Sound sound, int loops, int ms) throw (ErrorViewer) +{ + int cret=Mix_FadeInChannel(_get(),sound._get(),loops,ms); + if(cret==-1) + { + ErrorViewer e; + e.fetch(); + throw e; + } + _set(cret); + return *this; +} + + +int Channel::fadeOut(int ms) +{ + return Mix_FadeOutChannel(_get(), ms); +} + +void Channel::pause() +{ + Mix_Pause(_get()); +} + +void Channel::resume() +{ + Mix_Resume(_get()); +} + +int Channel::stop() +{ + return Mix_HaltChannel(_get()); +} + +int Channel::setPanning(uint8_t left, uint8_t right) +{ + return Mix_SetPanning(_get(),left,right); +} + +int Channel::setPosition(int16_t angle, uint8_t distance) +{ + return Mix_SetPosition(_get(),angle,distance); +} + +int Channel::setDistance(uint8_t distance) +{ + return Mix_SetDistance(_get(),distance); +} + +int Channel::setReverseStereo(int flip) +{ + return Mix_SetReverseStereo(_get(),flip); +} + +int Channel::addEffect(Mix_EffectFunc_t f, Mix_EffectDone_t d, void* arg) +{ + return Mix_RegisterEffect(_get(),f,d,arg); +} + +int Channel::removeEffect(Mix_EffectFunc_t f) +{ + return Mix_UnregisterEffect(_get(),f); +} + +int Channel::removeAllEffect() +{ + return Mix_UnregisterAllEffects(_get()); +} + + //static int SoundPlayer::GetDecoderNum() { @@ -32,96 +152,17 @@ SoundPlayer::SoundPlayer(int Channels) Mix_AllocateChannels(Channels); } -Sound SoundPlayer::loadSound(std::string Filename) throw(ErrorViewer) +Channel SoundPlayer::playSound(Sound sound, int loops) throw(ErrorViewer) { - Mix_Chunk* temp = Mix_LoadWAV(Filename.c_str()); - if (temp == NULL) - { - ErrorViewer e; - e.fetch(); - throw e; - } - Sound s; - s._set(temp); - return s; + Channel c; + c.playSound(sound,loops); + return c; } -ChannelID SoundPlayer::playSound(Sound sound, int loops) throw(ErrorViewer) +Channel SoundPlayer::fadeIn(Sound sound, int loops, int ms) throw(ErrorViewer) { - ChannelID id; - if (-1 == (id = Mix_PlayChannel(-1, sound._get(), loops))) - { - ErrorViewer e; - e.fetch(); - throw e; - } - return id; -} - -ChannelID SoundPlayer::fadein(Sound sound, int loops, int ms) throw(ErrorViewer) -{ - ChannelID id; - if (-1 == (id = Mix_FadeInChannel(-1, sound._get(), loops, ms))) - { - ErrorViewer e; - e.fetch(); - throw e; - } - return id; -} - -int SoundPlayer::fadeout(ChannelID id, int ms) -{ - return Mix_FadeOutChannel(id, ms); -} - -void SoundPlayer::pause(ChannelID id) -{ - Mix_Pause(id); -} - -void SoundPlayer::resume(ChannelID id) -{ - Mix_Resume(id); -} - -int SoundPlayer::stop(ChannelID id) -{ - return Mix_HaltChannel(id); -} - -int SoundPlayer::setPanning(ChannelID id, uint8_t left, uint8_t right) -{ - return Mix_SetPanning(id,left,right); -} - -int SoundPlayer::setPosition(ChannelID id, int16_t angle, uint8_t distance) -{ - return Mix_SetPosition(id,angle,distance); -} - -int SoundPlayer::setDistance(ChannelID id, uint8_t distance) -{ - return Mix_SetDistance(id,distance); -} - -int SoundPlayer::setReverseStereo(ChannelID id, int flip) -{ - return Mix_SetReverseStereo(id,flip); -} - -int SoundPlayer::addEffect(ChannelID id,Mix_EffectFunc_t f, Mix_EffectDone_t d, void* arg) -{ - return Mix_RegisterEffect(id,f,d,arg); -} - -int SoundPlayer::removeEffect(ChannelID id,Mix_EffectFunc_t f) -{ - return Mix_UnregisterEffect(id,f); -} - -int SoundPlayer::removeAllEffect(ChannelID id) -{ - return Mix_UnregisterAllEffects(id); + Channel c; + c.fadeIn(sound,loops,ms); + return c; } #include "end_code.h" diff --git a/SDLWrapper/Sound.h b/SDLWrapper/Sound.h index b9bb6b6..b1f677c 100644 --- a/SDLWrapper/Sound.h +++ b/SDLWrapper/Sound.h @@ -8,17 +8,50 @@ class Sound { public: -protected: Sound() = default; + Sound(const std::string& WAVFilename); + bool isReady() const; + void release(); private: std::shared_ptr _sound; void _set(Mix_Chunk*); void _clear(); - Mix_Chunk* _get(); - friend class SoundPlayer; + Mix_Chunk* _get() const; + + friend class Channel; }; -typedef int ChannelID; +class Channel +{ +public: + Channel& playSound(Sound sound,int loops) throw (ErrorViewer); + Channel& fadeIn(Sound sound,int loops,int ms) throw (ErrorViewer); + + int fadeOut(int ms); + void pause(); + void resume(); + int stop(); + + /// Experimental + int setPanning(uint8_t left,uint8_t right); + int setPosition(int16_t angle,uint8_t distance); + int setDistance(uint8_t distance); + int setReverseStereo(int flip); + + /// Experimental: Direct Add/Remove Effect + int addEffect(Mix_EffectFunc_t f, Mix_EffectDone_t d, void *arg); + int removeEffect(Mix_EffectFunc_t f); + int removeAllEffect(); +protected: + Channel(); +private: + void _set(int ChannelID); + int _get() const; + void _clear(); + + int _id; + friend class SoundPlayer; +}; class SoundPlayer : public AudioPlayer { @@ -26,24 +59,8 @@ public: static int GetDecoderNum(); static std::string GetDecoderName(int index); - SoundPlayer(int Channels = 16); - Sound loadSound(std::string Filename) throw (ErrorViewer); - ChannelID playSound(Sound sound, int loops) throw (ErrorViewer); - ChannelID fadein(Sound sound, int loops, int ms) throw (ErrorViewer); - int fadeout(ChannelID id, int ms); - void pause(ChannelID id); - void resume(ChannelID id); - int stop(ChannelID id); - - /// Experimental - int setPanning(ChannelID id,uint8_t left,uint8_t right); - int setPosition(ChannelID id,int16_t angle,uint8_t distance); - int setDistance(ChannelID id,uint8_t distance); - int setReverseStereo(ChannelID id,int flip); - - /// Experimental: Direct Add/Remove Effect - int addEffect(ChannelID id,Mix_EffectFunc_t f, Mix_EffectDone_t d, void *arg); - int removeEffect(ChannelID id,Mix_EffectFunc_t f); - int removeAllEffect(ChannelID id); + SoundPlayer(int NumChannels = 16); + Channel playSound(Sound sound, int loops) throw (ErrorViewer); + Channel fadeIn(Sound sound, int loops, int ms) throw (ErrorViewer); }; #include "end_code.h"