diff options
Diffstat (limited to 'src/libjin/audio/sdl/source.h')
-rw-r--r-- | src/libjin/audio/sdl/source.h | 118 |
1 files changed, 69 insertions, 49 deletions
diff --git a/src/libjin/audio/sdl/source.h b/src/libjin/audio/sdl/source.h index 682ff50..d97590e 100644 --- a/src/libjin/audio/sdl/source.h +++ b/src/libjin/audio/sdl/source.h @@ -4,11 +4,7 @@ #include <vector> #include <queue> #include <stack> - -#include "3rdparty/wav/wav.h" -#define STB_VORBIS_HEADER_ONLY -#include "3rdparty/stb/stb_vorbis.c" -#undef STB_VORBIS_HEADER_ONLY +#include <exception> #include "../source.h" @@ -17,15 +13,21 @@ namespace jin namespace audio { + struct SDLSourceCommand; + class SDLSource : public Source { public: - SDLSource() {} - ~SDLSource() {} + enum Type + { + WAV = 1, + OGG = 2, + }; - void generateSamples(Uint8* streamIn, int len); + SDLSource(Type format, void* mem, int size) ; + ~SDLSource(); /* ISource interface */ void play() override; @@ -41,52 +43,70 @@ namespace audio bool setLoop(bool loop) override; void setRate(float rate) override; - /* process function */ - static void processCommand(); - static void processSource(); - static void removeSource(SDLSource* source); - private: - struct Command + friend class SDLSourceManager; + + void loadWAV(void* mem, int size); + void loadOGG(void* mem, int size); + + inline bool is(int state) { return (status & state) == state; } + + struct { - typedef enum Action - { - Nothing = 0, - Play, - Stop, - Pause, - Resume, - Rewind, - SetVolume, - SetLoop, - SetRate, - }; - Action action; - union { - int _integer; - float _float; - bool _boolean; - const char* _string; - }; - SDLSource* source; - }; + const void* data; // Ƶ + int length; // dataֽڳ + void* end; // dataβ = (unsigned char*)data + length + int rate; // Ƶ + unsigned char bitdepth; // ÿsampleıس + int samples; // sample = length / (bitdepth / 8) + unsigned char channel; // channel12 + char silence; // 0 + } raw; + + /* Procedure controller variable */ + int pos; // ǰŵλ + int pitch; // pitch + int status; // ǰ״̬ + bool loop; // loop or not + + }; + + class SDLSourceManager + { + + public: + + static SDLSourceManager* get(); - static std::queue<Command*> commands; - static std::stack<Command*> commandsPool; - static Command* getCommand(); - static void collectCommand(Command* cmd); - - static std::vector<SDLSource*> sources; - - void* data; - void* pos; - int length; - int pitch; - bool paused; - bool playing; - bool stopped; + /* Process function */ + static void processCommands(); + static void processSources(Uint8* buffer, int len); + + private: + + friend class SDLSource; + + static void removeSource(SDLSource* source); + static void pushSource(SDLSource* source); + static SDLSourceCommand* getCommand(); + static void pushCommand(SDLSourceCommand* cmd); + static void collectCommand(SDLSourceCommand* cmd); + static SDLSourceManager* manager; + + static std::queue<SDLSourceCommand*> commands; + static std::stack<SDLSourceCommand*> commandsPool; + static std::vector<SDLSource*> sources; // processing sources + + }; + + class SourceException : public std::exception + { + const char * what() const throw () + { + return "Load Source Exception"; + } }; } |