From fdef90b7bc05d5e49047d31ce4f2fcfd2029e36e Mon Sep 17 00:00:00 2001 From: bjorn Date: Thu, 5 Jul 2018 23:30:37 -0700 Subject: [PATCH] Add AudioStream:decode; --- src/api/audio.c | 29 +++++++++++++++-------------- src/api/types/audioStream.c | 16 ++++++++++++++++ 2 files changed, 31 insertions(+), 14 deletions(-) diff --git a/src/api/audio.c b/src/api/audio.c index 1ac03bb6..519352a0 100644 --- a/src/api/audio.c +++ b/src/api/audio.c @@ -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); diff --git a/src/api/types/audioStream.c b/src/api/types/audioStream.c index d4b3cd04..8b5c2b79 100644 --- a/src/api/types/audioStream.c +++ b/src/api/types/audioStream.c @@ -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 },