Load static and stream Sources;

This commit is contained in:
bjorn 2018-07-05 22:08:14 -07:00
parent bc61e02f84
commit a670435239
4 changed files with 64 additions and 16 deletions

View File

@ -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[];

View File

@ -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;
}

View File

@ -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);

View File

@ -1,4 +1,5 @@
#include "data/audioStream.h"
#include "data/soundData.h"
#include "util.h"
#include <AL/al.h>
#include <AL/alc.h>
@ -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);