From 90a2ec2b18d7d3a36fa3106c4d063b1048d8d98e Mon Sep 17 00:00:00 2001 From: chai Date: Mon, 28 May 2018 22:57:57 +0800 Subject: =?UTF-8?q?=E4=BF=AE=E6=94=B9=E8=AF=BB=E5=8F=96wav=E9=94=99?= =?UTF-8?q?=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/libjin/audio/sdl/source.cpp | 63 ++++++++++++++++++++++++++++------------- 1 file changed, 44 insertions(+), 19 deletions(-) (limited to 'src/libjin/audio/sdl/source.cpp') diff --git a/src/libjin/audio/sdl/source.cpp b/src/libjin/audio/sdl/source.cpp index 1537d2f..bfe090b 100644 --- a/src/libjin/audio/sdl/source.cpp +++ b/src/libjin/audio/sdl/source.cpp @@ -1,4 +1,5 @@ #include +#include #include "../../math/math.h" #include "../../utils/macros.h" @@ -12,7 +13,7 @@ namespace jin namespace audio { - struct SDLSourceCommand + typedef struct SDLSourceCommand { typedef enum Action { @@ -38,11 +39,11 @@ namespace audio SDLSource* source; }; - typedef enum STATUS + typedef MASK enum STATUS { - PLAYING = 2, - PAUSED = 4, - STOPPED = 8 + PLAYING = 1, + PAUSED = 2, + STOPPED = 4 }; #define Command SDLSourceCommand @@ -54,26 +55,56 @@ namespace audio shared std::vector Manager::sources; shared Manager* Manager::manager = nullptr; - SDLSource::SDLSource(Type format, void* mem, int size) + SDLSource* SDLSource::createSource(SourceType format, const char* file) { - memset(&status, 0, sizeof(status)); - memset(&raw, 0, sizeof(raw)); + std::ifstream fs(file, std::ios::binary); + fs.seekg(0,std::ios::end); + int size = fs.tellg(); + fs.seekg(0, std::ios::beg); + char* buffer = (char*)malloc(size); + memset(buffer, 0, size); + fs.read(buffer, size); + SDLSource* source = createSource(format, buffer, size); + free(buffer); + return source; + } + + SDLSource* SDLSource::createSource(SourceType format, void* mem, size_t size) + { + if (mem == nullptr) + return nullptr; + SDLSource* source = new SDLSource(); + memset(&source->status, 0, sizeof(status)); + memset(&source->raw, 0, sizeof(raw)); try { switch (format) { case WAV: - loadWAV(mem, size); + source->loadWAV(mem, size); break; case OGG: - loadOGG(mem, size); + source->loadOGG(mem, size); break; } } - catch(SourceException& exp) + catch (SourceException& exp) { - + delete source; + return nullptr; }; + return source; + } + + SDLSource::SDLSource() + { + } + + SDLSource::~SDLSource() + { + delete raw.data; + raw.end = 0; + raw.data = 0; } void SDLSource::loadWAV(void* mem, int size) @@ -81,7 +112,7 @@ namespace audio wav_t wav; if (wav_read(&wav, mem, size) == 0) { - raw.data = wav.data; + raw.data = wav.data; raw.size = wav.length * wav.bitdepth / 8; raw.end = (char*)raw.data + raw.size; raw.rate = wav.samplerate; @@ -108,12 +139,6 @@ namespace audio } } - SDLSource::~SDLSource() - { - - } - - #define ActionNone(T)\ do{\ Command* cmd = Manager::get()->getCommand();\ -- cgit v1.1-26-g67d0