aboutsummaryrefslogtreecommitdiff
path: root/src/libjin/audio/sdl
diff options
context:
space:
mode:
Diffstat (limited to 'src/libjin/audio/sdl')
-rw-r--r--src/libjin/audio/sdl/audio.cpp2
-rw-r--r--src/libjin/audio/sdl/audio.h2
-rw-r--r--src/libjin/audio/sdl/source.cpp63
-rw-r--r--src/libjin/audio/sdl/source.h14
4 files changed, 51 insertions, 30 deletions
diff --git a/src/libjin/audio/sdl/audio.cpp b/src/libjin/audio/sdl/audio.cpp
index b5cbc1b..5277a3d 100644
--- a/src/libjin/audio/sdl/audio.cpp
+++ b/src/libjin/audio/sdl/audio.cpp
@@ -24,7 +24,7 @@ namespace audio
zero(spec);
spec.freq = 44100;
spec.format = AUDIO_S16SYS;
- spec.channels = 2;
+ spec.channels = 1;
spec.samples = 2048;
spec.userdata = this;
spec.callback = defaultCallback;
diff --git a/src/libjin/audio/sdl/audio.h b/src/libjin/audio/sdl/audio.h
index 28b73f0..a0e63da 100644
--- a/src/libjin/audio/sdl/audio.h
+++ b/src/libjin/audio/sdl/audio.h
@@ -9,8 +9,6 @@ namespace jin
namespace audio
{
- class SDLSource;
-
class SDLAudio : public Audio
{
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();\
diff --git a/src/libjin/audio/sdl/source.h b/src/libjin/audio/sdl/source.h
index 29a3676..900568f 100644
--- a/src/libjin/audio/sdl/source.h
+++ b/src/libjin/audio/sdl/source.h
@@ -13,22 +13,18 @@ namespace jin
namespace audio
{
- struct SDLSourceCommand;
+ typedef struct SDLSourceCommand;
class SDLSource : public Source
{
public:
- enum Type
- {
- WAV = 1,
- OGG = 2,
- };
-
- SDLSource(Type format, void* mem, int size) ;
~SDLSource();
+ static SDLSource* createSource(SourceType format, const char* file);
+ static SDLSource* createSource(SourceType format, void* mem, size_t size);
+
/* ISource interface */
void play() override;
void stop() override;
@@ -45,6 +41,8 @@ namespace audio
private:
+ SDLSource();
+
friend class SDLSourceManager;
void loadWAV(void* mem, int size);