diff options
Diffstat (limited to 'src/libjin/audio/sdl/source.cpp')
-rw-r--r-- | src/libjin/audio/sdl/source.cpp | 63 |
1 files changed, 44 insertions, 19 deletions
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 <exception> +#include <fstream> #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<SDLSource*> 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();\ |