mirror of https://github.com/bjornbytes/lovr.git
Load static and stream Sources;
This commit is contained in:
parent
bc61e02f84
commit
a670435239
|
@ -90,6 +90,7 @@ extern const char* MaterialTextures[];
|
||||||
extern const char* MeshDrawModes[];
|
extern const char* MeshDrawModes[];
|
||||||
extern const char* MeshUsages[];
|
extern const char* MeshUsages[];
|
||||||
extern const char* ShapeTypes[];
|
extern const char* ShapeTypes[];
|
||||||
|
extern const char* SourceTypes[];
|
||||||
extern const char* StencilActions[];
|
extern const char* StencilActions[];
|
||||||
extern const char* TextureFormats[];
|
extern const char* TextureFormats[];
|
||||||
extern const char* TextureTypes[];
|
extern const char* TextureTypes[];
|
||||||
|
|
|
@ -3,6 +3,12 @@
|
||||||
#include "audio/source.h"
|
#include "audio/source.h"
|
||||||
#include "data/audioStream.h"
|
#include "data/audioStream.h"
|
||||||
|
|
||||||
|
const char* SourceTypes[] = {
|
||||||
|
[SOURCE_STATIC] = "static",
|
||||||
|
[SOURCE_STREAM] = "stream",
|
||||||
|
NULL
|
||||||
|
};
|
||||||
|
|
||||||
const char* TimeUnits[] = {
|
const char* TimeUnits[] = {
|
||||||
[UNIT_SECONDS] = "seconds",
|
[UNIT_SECONDS] = "seconds",
|
||||||
[UNIT_SAMPLES] = "samples",
|
[UNIT_SAMPLES] = "samples",
|
||||||
|
@ -13,7 +19,6 @@ int l_lovrAudioInit(lua_State* L) {
|
||||||
lua_newtable(L);
|
lua_newtable(L);
|
||||||
luaL_register(L, NULL, lovrAudio);
|
luaL_register(L, NULL, lovrAudio);
|
||||||
luax_registertype(L, "Source", lovrSource);
|
luax_registertype(L, "Source", lovrSource);
|
||||||
|
|
||||||
lovrAudioInit();
|
lovrAudioInit();
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
@ -70,24 +75,43 @@ int l_lovrAudioIsSpatialized(lua_State* L) {
|
||||||
}
|
}
|
||||||
|
|
||||||
int l_lovrAudioNewSource(lua_State* L) {
|
int l_lovrAudioNewSource(lua_State* L) {
|
||||||
void** type;
|
Source* source = NULL;
|
||||||
AudioStream* stream;
|
SoundData** soundDataRef = luax_totype(L, 1, SoundData);
|
||||||
if ((type = luax_totype(L, 1, AudioStream)) != NULL) {
|
AudioStream** streamRef = luax_totype(L, 1, AudioStream);
|
||||||
stream = *type;
|
bool isStatic = soundDataRef || luaL_checkoption(L, 2, NULL, SourceTypes) == SOURCE_STATIC;
|
||||||
} else {
|
|
||||||
Blob* blob = luax_readblob(L, 1, "Source");
|
if (isStatic) {
|
||||||
stream = lovrAudioStreamCreate(blob, 4096);
|
SoundData* soundData = soundDataRef ? *soundDataRef : NULL;
|
||||||
lovrRelease(blob);
|
|
||||||
if (!stream) {
|
if (!soundData) {
|
||||||
luaL_error(L, "Could not decode Ogg audio source at '%s'", luaL_checkstring(L, 1));
|
if (streamRef) {
|
||||||
return 0;
|
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);
|
luax_pushtype(L, Source, source);
|
||||||
lovrRelease(source);
|
lovrRelease(source);
|
||||||
lovrRelease(stream);
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -27,10 +27,24 @@ static ALenum lovrSourceGetState(Source* source) {
|
||||||
return state;
|
return state;
|
||||||
}
|
}
|
||||||
|
|
||||||
Source* lovrSourceCreate(AudioStream* stream) {
|
Source* lovrSourceCreateStatic(SoundData* soundData) {
|
||||||
Source* source = lovrAlloc(sizeof(Source), lovrSourceDestroy);
|
Source* source = lovrAlloc(sizeof(Source), lovrSourceDestroy);
|
||||||
if (!source) return NULL;
|
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;
|
source->stream = stream;
|
||||||
alGenSources(1, &source->id);
|
alGenSources(1, &source->id);
|
||||||
alGenBuffers(SOURCE_BUFFERS, source->buffers);
|
alGenBuffers(SOURCE_BUFFERS, source->buffers);
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
#include "data/audioStream.h"
|
#include "data/audioStream.h"
|
||||||
|
#include "data/soundData.h"
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
#include <AL/al.h>
|
#include <AL/al.h>
|
||||||
#include <AL/alc.h>
|
#include <AL/alc.h>
|
||||||
|
@ -8,6 +9,11 @@
|
||||||
|
|
||||||
#define SOURCE_BUFFERS 4
|
#define SOURCE_BUFFERS 4
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
SOURCE_STATIC,
|
||||||
|
SOURCE_STREAM
|
||||||
|
} SourceType;
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
UNIT_SECONDS,
|
UNIT_SECONDS,
|
||||||
UNIT_SAMPLES
|
UNIT_SAMPLES
|
||||||
|
@ -15,13 +21,16 @@ typedef enum {
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
Ref ref;
|
Ref ref;
|
||||||
|
SourceType type;
|
||||||
AudioStream* stream;
|
AudioStream* stream;
|
||||||
|
SoundData* soundData;
|
||||||
ALuint id;
|
ALuint id;
|
||||||
ALuint buffers[SOURCE_BUFFERS];
|
ALuint buffers[SOURCE_BUFFERS];
|
||||||
bool isLooping;
|
bool isLooping;
|
||||||
} Source;
|
} Source;
|
||||||
|
|
||||||
Source* lovrSourceCreate(AudioStream* stream);
|
Source* lovrSourceCreateStatic(SoundData* soundData);
|
||||||
|
Source* lovrSourceCreateStream(AudioStream* stream);
|
||||||
void lovrSourceDestroy(void* ref);
|
void lovrSourceDestroy(void* ref);
|
||||||
int lovrSourceGetBitDepth(Source* source);
|
int lovrSourceGetBitDepth(Source* source);
|
||||||
int lovrSourceGetChannelCount(Source* source);
|
int lovrSourceGetChannelCount(Source* source);
|
||||||
|
|
Loading…
Reference in New Issue