Move Audio,SoundPlayer out from Music Part

This commit is contained in:
Kirigaya Kazuto 2017-06-23 11:24:50 +08:00
parent f4f4327b7a
commit 0a141bb4a0
6 changed files with 230 additions and 213 deletions

33
SDLWrapper/Audio.cpp Normal file
View File

@ -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"

20
SDLWrapper/Audio.h Normal file
View File

@ -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"

View File

@ -1,33 +1,5 @@
#include "Music.h" #include "Music.h"
#include "begin_code.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 void Music::_set(Mix_Music* p)//private
{ {
_music.reset(p,Mix_FreeMusic); _music.reset(p,Mix_FreeMusic);
@ -136,132 +108,4 @@ int MusicPlayer::SetMusicPosition(double position)
return Mix_SetMusicPosition(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" #include "end_code.h"

View File

@ -3,24 +3,8 @@
#include <memory> #include <memory>
#include <string> #include <string>
#include "ErrorViewer.h" #include "ErrorViewer.h"
#include "Audio.h"
#include "begin_code.h" #include "begin_code.h"
class AudioPlayer
{
public:
AudioPlayer();
~AudioPlayer();
private:
class _Audio
{
public:
_Audio();
~_Audio();
};
static _Audio* _sysAudio;
static int _sysAudioCounter;
};
/// Forward Declaration /// Forward Declaration
class Music class Music
{ {
@ -63,45 +47,5 @@ private:
Music m; Music m;
}; };
class Sound
{
public:
protected:
Sound() = default;
private:
std::shared_ptr<Mix_Chunk> _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" #include "end_code.h"

127
SDLWrapper/Sound.cpp Normal file
View File

@ -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"

49
SDLWrapper/Sound.h Normal file
View File

@ -0,0 +1,49 @@
#pragma once
#include "include.h"
#include <memory>
#include <string>
#include "Audio.h"
#include "ErrorViewer.h"
#include "begin_code.h"
class Sound
{
public:
protected:
Sound() = default;
private:
std::shared_ptr<Mix_Chunk> _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"