From 4f3ea475b0c72cf83175cc69bdd128b7a306523b Mon Sep 17 00:00:00 2001 From: chai Date: Sun, 27 May 2018 17:39:09 +0800 Subject: =?UTF-8?q?=E6=9B=B4=E6=96=B0=E9=9F=B3=E9=A2=91=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/libjin/audio/sdl/source.h | 118 ++++++++++++++++++++++++------------------ 1 file changed, 69 insertions(+), 49 deletions(-) (limited to 'src/libjin/audio/sdl/source.h') 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 #include #include - -#include "3rdparty/wav/wav.h" -#define STB_VORBIS_HEADER_ONLY -#include "3rdparty/stb/stb_vorbis.c" -#undef STB_VORBIS_HEADER_ONLY +#include #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; // channel数1或2 + 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 commands; - static std::stack commandsPool; - static Command* getCommand(); - static void collectCommand(Command* cmd); - - static std::vector 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 commands; + static std::stack commandsPool; + static std::vector sources; // processing sources + + }; + + class SourceException : public std::exception + { + const char * what() const throw () + { + return "Load Source Exception"; + } }; } -- cgit v1.1-26-g67d0