Add AudioStream:decode;

This commit is contained in:
bjorn 2018-07-05 23:30:37 -07:00
parent 4e146197b4
commit fdef90b7bc
2 changed files with 31 additions and 14 deletions

View File

@ -81,9 +81,11 @@ int l_lovrAudioNewSource(lua_State* L) {
bool isStatic = soundDataRef || luaL_checkoption(L, 2, NULL, SourceTypes) == SOURCE_STATIC;
if (isStatic) {
SoundData* soundData = soundDataRef ? *soundDataRef : NULL;
if (soundDataRef) {
source = lovrSourceCreateStatic(*soundDataRef);
} else {
SoundData* soundData;
if (!soundData) {
if (streamRef) {
soundData = lovrSoundDataCreateFromAudioStream(*streamRef);
} else {
@ -91,23 +93,22 @@ int l_lovrAudioNewSource(lua_State* L) {
soundData = lovrSoundDataCreateFromBlob(blob);
lovrRelease(blob);
}
}
lovrAssert(soundData, "Could not create static Source");
source = lovrSourceCreateStatic(soundData);
lovrRelease(soundData);
lovrAssert(soundData, "Could not create static Source");
source = lovrSourceCreateStatic(soundData);
lovrRelease(soundData);
}
} else {
AudioStream* stream = streamRef ? *streamRef : NULL;
if (!stream) {
if (streamRef) {
source = lovrSourceCreateStream(*streamRef);
} else {
Blob* blob = luax_readblob(L, 1, "Source");
stream = lovrAudioStreamCreate(blob, 4096);
AudioStream* stream = lovrAudioStreamCreate(blob, 4096);
lovrAssert(stream, "Could not create stream Source");
source = lovrSourceCreateStream(stream);
lovrRelease(blob);
lovrRelease(stream);
}
lovrAssert(stream, "Could not create stream Source");
source = lovrSourceCreateStream(stream);
lovrRelease(stream);
}
luax_pushtype(L, Source, source);

View File

@ -1,5 +1,20 @@
#include "api.h"
#include "data/audioStream.h"
#include "data/soundData.h"
int l_lovrAudioStreamDecode(lua_State* L) {
AudioStream* stream = luax_checktype(L, 1, AudioStream);
int samples = lovrAudioStreamDecode(stream, NULL, 0);
if (samples > 0) {
SoundData* soundData = lovrSoundDataCreate(samples / stream->channelCount, stream->sampleRate, stream->bitDepth, stream->channelCount);
memcpy(soundData->blob.data, stream->buffer, samples * (stream->bitDepth / 8));
luax_pushtype(L, SoundData, soundData);
lovrRelease(soundData);
} else {
lua_pushnil(L);
}
return 1;
}
int l_lovrAudioStreamGetBitDepth(lua_State* L) {
AudioStream* stream = luax_checktype(L, 1, AudioStream);
@ -26,6 +41,7 @@ int l_lovrAudioStreamGetSampleRate(lua_State* L) {
}
const luaL_Reg lovrAudioStream[] = {
{ "decode", l_lovrAudioStreamDecode },
{ "getBitDepth", l_lovrAudioStreamGetBitDepth },
{ "getChannelCount", l_lovrAudioStreamGetChannelCount },
{ "getDuration", l_lovrAudioStreamGetDuration },