From 0a141bb4a0632145cb46ebcde2996c4e8ad1f447 Mon Sep 17 00:00:00 2001 From: kiritow <1362050620@qq.com> Date: Fri, 23 Jun 2017 11:24:50 +0800 Subject: [PATCH] Move Audio,SoundPlayer out from Music Part --- SDLWrapper/Audio.cpp | 33 +++++++++ SDLWrapper/Audio.h | 20 ++++++ SDLWrapper/Music.cpp | 156 ------------------------------------------- SDLWrapper/Music.h | 58 +--------------- SDLWrapper/Sound.cpp | 127 +++++++++++++++++++++++++++++++++++ SDLWrapper/Sound.h | 49 ++++++++++++++ 6 files changed, 230 insertions(+), 213 deletions(-) create mode 100644 SDLWrapper/Audio.cpp create mode 100644 SDLWrapper/Audio.h create mode 100644 SDLWrapper/Sound.cpp create mode 100644 SDLWrapper/Sound.h diff --git a/SDLWrapper/Audio.cpp b/SDLWrapper/Audio.cpp new file mode 100644 index 0000000..a55bbde --- /dev/null +++ b/SDLWrapper/Audio.cpp @@ -0,0 +1,33 @@ +#include "Audio.h" +#include "begin_code.h" +AudioPlayer::AudioPlayer() +{ + if (!_sysAudioCounter) + { + _sysAudio = new _Audio; + } + ++_sysAudioCounter; +} + +AudioPlayer::~AudioPlayer() +{ + --_sysAudioCounter; + if (!_sysAudioCounter) + { + delete _sysAudio; + } +} + +AudioPlayer::_Audio::_Audio() +{ + Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 1024); +} + +AudioPlayer::_Audio::~_Audio() +{ + Mix_CloseAudio(); +} + +AudioPlayer::_Audio* AudioPlayer::_sysAudio = nullptr; +int AudioPlayer::_sysAudioCounter = 0; +#include "end_code.h" diff --git a/SDLWrapper/Audio.h b/SDLWrapper/Audio.h new file mode 100644 index 0000000..1f0a62f --- /dev/null +++ b/SDLWrapper/Audio.h @@ -0,0 +1,20 @@ +#pragma once +#include "include.h" +#include "begin_code.h" +class AudioPlayer +{ +public: + AudioPlayer(); + ~AudioPlayer(); +private: + class _Audio + { + public: + _Audio(); + ~_Audio(); + }; + + static _Audio* _sysAudio; + static int _sysAudioCounter; +}; +#include "end_code.h" diff --git a/SDLWrapper/Music.cpp b/SDLWrapper/Music.cpp index 6a98f8a..4e508a9 100644 --- a/SDLWrapper/Music.cpp +++ b/SDLWrapper/Music.cpp @@ -1,33 +1,5 @@ #include "Music.h" #include "begin_code.h" -AudioPlayer::AudioPlayer() -{ - if (!_sysAudioCounter) - { - _sysAudio = new _Audio; - } - ++_sysAudioCounter; -} - -AudioPlayer::~AudioPlayer() -{ - --_sysAudioCounter; - if (!_sysAudioCounter) - { - delete _sysAudio; - } -} - -AudioPlayer::_Audio::_Audio() -{ - Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 1024); -} - -AudioPlayer::_Audio::~_Audio() -{ - Mix_CloseAudio(); -} - void Music::_set(Mix_Music* p)//private { _music.reset(p,Mix_FreeMusic); @@ -136,132 +108,4 @@ int MusicPlayer::SetMusicPosition(double position) return Mix_SetMusicPosition(position); } -void Sound::_set(Mix_Chunk* p) -{ - _sound.reset(p,Mix_FreeChunk); -} - -void Sound::_clear()//private -{ - _sound.reset(); -} - -Mix_Chunk* Sound::_get() -{ - return _sound.get(); -} - -//static -int SoundPlayer::GetDecoderNum() -{ - return Mix_GetNumChunkDecoders(); -} - -//static -std::string SoundPlayer::GetDecoderName(int index) -{ - return std::string(Mix_GetChunkDecoder(index)); -} - -SoundPlayer::SoundPlayer(int Channels) -{ - Mix_AllocateChannels(Channels); -} - -Sound SoundPlayer::loadSound(std::string Filename) 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; -} - -ChannelID SoundPlayer::playSound(Sound sound, int loops) 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); -} - - -AudioPlayer::_Audio* AudioPlayer::_sysAudio = nullptr; -int AudioPlayer::_sysAudioCounter = 0; #include "end_code.h" diff --git a/SDLWrapper/Music.h b/SDLWrapper/Music.h index 96efe4f..d7a3b51 100644 --- a/SDLWrapper/Music.h +++ b/SDLWrapper/Music.h @@ -3,24 +3,8 @@ #include #include #include "ErrorViewer.h" +#include "Audio.h" #include "begin_code.h" -class AudioPlayer -{ -public: - AudioPlayer(); - ~AudioPlayer(); -private: - class _Audio - { - public: - _Audio(); - ~_Audio(); - }; - - static _Audio* _sysAudio; - static int _sysAudioCounter; -}; - /// Forward Declaration class Music { @@ -63,45 +47,5 @@ private: Music m; }; -class Sound -{ -public: -protected: - Sound() = default; -private: - std::shared_ptr _sound; - void _set(Mix_Chunk*); - void _clear(); - Mix_Chunk* _get(); - friend class SoundPlayer; -}; -typedef int ChannelID; - -class SoundPlayer : public AudioPlayer -{ -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); -}; #include "end_code.h" diff --git a/SDLWrapper/Sound.cpp b/SDLWrapper/Sound.cpp new file mode 100644 index 0000000..fd12b8a --- /dev/null +++ b/SDLWrapper/Sound.cpp @@ -0,0 +1,127 @@ +#include "Sound.h" +#include "begin_code.h" +void Sound::_set(Mix_Chunk* p) +{ + _sound.reset(p,Mix_FreeChunk); +} + +void Sound::_clear()//private +{ + _sound.reset(); +} + +Mix_Chunk* Sound::_get() +{ + return _sound.get(); +} + +//static +int SoundPlayer::GetDecoderNum() +{ + return Mix_GetNumChunkDecoders(); +} + +//static +std::string SoundPlayer::GetDecoderName(int index) +{ + return std::string(Mix_GetChunkDecoder(index)); +} + +SoundPlayer::SoundPlayer(int Channels) +{ + Mix_AllocateChannels(Channels); +} + +Sound SoundPlayer::loadSound(std::string Filename) 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; +} + +ChannelID SoundPlayer::playSound(Sound sound, int loops) 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); +} +#include "end_code.h" diff --git a/SDLWrapper/Sound.h b/SDLWrapper/Sound.h new file mode 100644 index 0000000..b9bb6b6 --- /dev/null +++ b/SDLWrapper/Sound.h @@ -0,0 +1,49 @@ +#pragma once +#include "include.h" +#include +#include +#include "Audio.h" +#include "ErrorViewer.h" +#include "begin_code.h" +class Sound +{ +public: +protected: + Sound() = default; +private: + std::shared_ptr _sound; + void _set(Mix_Chunk*); + void _clear(); + Mix_Chunk* _get(); + friend class SoundPlayer; +}; + +typedef int ChannelID; + +class SoundPlayer : public AudioPlayer +{ +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); +}; +#include "end_code.h"