From a670435239443f90d12fa2dd940bc3cfaf0cab6a Mon Sep 17 00:00:00 2001 From: bjorn Date: Thu, 5 Jul 2018 22:08:14 -0700 Subject: [PATCH] Load static and stream Sources; --- src/api.h | 1 + src/api/audio.c | 52 +++++++++++++++++++++++++++++++++------------- src/audio/source.c | 16 +++++++++++++- src/audio/source.h | 11 +++++++++- 4 files changed, 64 insertions(+), 16 deletions(-) diff --git a/src/api.h b/src/api.h index 6bb2b28f..5825236e 100644 --- a/src/api.h +++ b/src/api.h @@ -90,6 +90,7 @@ extern const char* MaterialTextures[]; extern const char* MeshDrawModes[]; extern const char* MeshUsages[]; extern const char* ShapeTypes[]; +extern const char* SourceTypes[]; extern const char* StencilActions[]; extern const char* TextureFormats[]; extern const char* TextureTypes[]; diff --git a/src/api/audio.c b/src/api/audio.c index 463bea82..1ac03bb6 100644 --- a/src/api/audio.c +++ b/src/api/audio.c @@ -3,6 +3,12 @@ #include "audio/source.h" #include "data/audioStream.h" +const char* SourceTypes[] = { + [SOURCE_STATIC] = "static", + [SOURCE_STREAM] = "stream", + NULL +}; + const char* TimeUnits[] = { [UNIT_SECONDS] = "seconds", [UNIT_SAMPLES] = "samples", @@ -13,7 +19,6 @@ int l_lovrAudioInit(lua_State* L) { lua_newtable(L); luaL_register(L, NULL, lovrAudio); luax_registertype(L, "Source", lovrSource); - lovrAudioInit(); return 1; } @@ -70,24 +75,43 @@ int l_lovrAudioIsSpatialized(lua_State* L) { } int l_lovrAudioNewSource(lua_State* L) { - void** type; - AudioStream* stream; - if ((type = luax_totype(L, 1, AudioStream)) != NULL) { - stream = *type; - } else { - Blob* blob = luax_readblob(L, 1, "Source"); - stream = lovrAudioStreamCreate(blob, 4096); - lovrRelease(blob); - if (!stream) { - luaL_error(L, "Could not decode Ogg audio source at '%s'", luaL_checkstring(L, 1)); - return 0; + Source* source = NULL; + SoundData** soundDataRef = luax_totype(L, 1, SoundData); + AudioStream** streamRef = luax_totype(L, 1, AudioStream); + bool isStatic = soundDataRef || luaL_checkoption(L, 2, NULL, SourceTypes) == SOURCE_STATIC; + + if (isStatic) { + SoundData* soundData = soundDataRef ? *soundDataRef : NULL; + + if (!soundData) { + if (streamRef) { + soundData = lovrSoundDataCreateFromAudioStream(*streamRef); + } else { + Blob* blob = luax_readblob(L, 1, "Source"); + soundData = lovrSoundDataCreateFromBlob(blob); + lovrRelease(blob); + } } + + lovrAssert(soundData, "Could not create static Source"); + source = lovrSourceCreateStatic(soundData); + lovrRelease(soundData); + } else { + AudioStream* stream = streamRef ? *streamRef : NULL; + + if (!stream) { + Blob* blob = luax_readblob(L, 1, "Source"); + stream = lovrAudioStreamCreate(blob, 4096); + lovrRelease(blob); + } + + lovrAssert(stream, "Could not create stream Source"); + source = lovrSourceCreateStream(stream); + lovrRelease(stream); } - Source* source = lovrSourceCreate(stream); luax_pushtype(L, Source, source); lovrRelease(source); - lovrRelease(stream); return 1; } diff --git a/src/audio/source.c b/src/audio/source.c index 3980d2d8..42f78ccc 100644 --- a/src/audio/source.c +++ b/src/audio/source.c @@ -27,10 +27,24 @@ static ALenum lovrSourceGetState(Source* source) { return state; } -Source* lovrSourceCreate(AudioStream* stream) { +Source* lovrSourceCreateStatic(SoundData* soundData) { Source* source = lovrAlloc(sizeof(Source), lovrSourceDestroy); if (!source) return NULL; + source->type = SOURCE_STATIC; + source->soundData = soundData; + alGenSources(1, &source->id); + alGenBuffers(SOURCE_BUFFERS, source->buffers); + lovrRetain(soundData); + + return source; +} + +Source* lovrSourceCreateStream(AudioStream* stream) { + Source* source = lovrAlloc(sizeof(Source), lovrSourceDestroy); + if (!source) return NULL; + + source->type = SOURCE_STREAM; source->stream = stream; alGenSources(1, &source->id); alGenBuffers(SOURCE_BUFFERS, source->buffers); diff --git a/src/audio/source.h b/src/audio/source.h index b38932a6..b47190ad 100644 --- a/src/audio/source.h +++ b/src/audio/source.h @@ -1,4 +1,5 @@ #include "data/audioStream.h" +#include "data/soundData.h" #include "util.h" #include #include @@ -8,6 +9,11 @@ #define SOURCE_BUFFERS 4 +typedef enum { + SOURCE_STATIC, + SOURCE_STREAM +} SourceType; + typedef enum { UNIT_SECONDS, UNIT_SAMPLES @@ -15,13 +21,16 @@ typedef enum { typedef struct { Ref ref; + SourceType type; AudioStream* stream; + SoundData* soundData; ALuint id; ALuint buffers[SOURCE_BUFFERS]; bool isLooping; } Source; -Source* lovrSourceCreate(AudioStream* stream); +Source* lovrSourceCreateStatic(SoundData* soundData); +Source* lovrSourceCreateStream(AudioStream* stream); void lovrSourceDestroy(void* ref); int lovrSourceGetBitDepth(Source* source); int lovrSourceGetChannelCount(Source* source);