From b855ebb91ad8d97617ec1aa418b4add84670a07d Mon Sep 17 00:00:00 2001 From: chai Date: Fri, 27 Jul 2018 00:01:34 +0800 Subject: *change name --- src/libjin/Audio/SDL/Source.h | 116 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 src/libjin/Audio/SDL/Source.h (limited to 'src/libjin/Audio/SDL/Source.h') diff --git a/src/libjin/Audio/SDL/Source.h b/src/libjin/Audio/SDL/Source.h new file mode 100644 index 0000000..38f7ec4 --- /dev/null +++ b/src/libjin/Audio/SDL/Source.h @@ -0,0 +1,116 @@ +#ifndef __JIN_SOURCE_SDL_H +#define __JIN_SOURCE_SDL_H +#include "../../modules.h" +#if JIN_MODULES_AUDIO && JIN_AUDIO_SDLAUDIO + +#include +#include +#include +#include + +#include "../source.h" + +namespace jin +{ +namespace audio +{ + + typedef struct SDLSourceCommand; + class SDLSourceManager; + + class SDLSource : public Source + { + + public: + + ~SDLSource(); + + static SDLSource* createSource(const char* file); + static SDLSource* createSource(void* mem, size_t size); + + /* ISource interface */ + void play() override; + void stop() override; + void pause() override; + void resume() override; + void rewind() override; + bool isStopped() const override; + bool isPaused() const override; + void setPitch(float pitch) override; + // 调整上界 + void setVolume(float volume) override; + bool setLoop(bool loop) override; + void setRate(float rate) override; + + inline void handle(SDLSourceManager* manager, SDLSourceCommand* cmd); + inline void process(void* buffer, size_t size); + + private: + + SDLSource(); + + void decode_wav(void* mem, int size); + void decode_ogg(void* mem, int size); + + inline bool is(int state) const { return (status.state & state) == state; } + + struct + { + const void* data; // 音频数据 + int length; // data字节长度 + const void* end; // data结尾 = (unsigned char*)data + size + int samplerate; // 采样频率 + unsigned char bitdepth; // 每个sample的比特长度 + int samples; // sample数 = size / (bitdepth / 8) + unsigned char channels; // channel数1(mono)或2(stereo) + } raw; + + /* Procedure controller variable */ + struct + { + int pos; // 当前播放的sample + int pitch; // pitch + int state; // 当前状态 + bool loop; // loop or not + int volume; // 音量 + } status; + + }; + + class SDLSourceManager + { + + public: + + static SDLSourceManager* get(); + + /* Process function */ + static void processCommands(); + static void processSources(void* buffer, size_t size); + + static void removeSource(SDLSource* source); + static void pushSource(SDLSource* source); + static SDLSourceCommand* getCommand(); + static void pushCommand(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"; + } + }; + +} +} + +#endif // JIN_MODULES_AUDIO && JIN_AUDIO_SDLAUDIO +#endif \ No newline at end of file -- cgit v1.1-26-g67d0