Looping, playback;

This commit is contained in:
bjorn 2020-05-18 16:05:01 -06:00 committed by Bjorn
parent c92b47e3a6
commit 205e9404e0
3 changed files with 28 additions and 18 deletions

View File

@ -7,7 +7,21 @@ static int l_lovrSourcePlay(lua_State* L) {
return 0;
}
static int l_lovrSourceIsLooping(lua_State* L) {
Source* source = luax_checktype(L, 1, Source);
lua_pushboolean(L, lovrSourceIsLooping(source));
return 1;
}
static int l_lovrSourceSetLooping(lua_State* L) {
Source* source = luax_checktype(L, 1, Source);
lovrSourceSetLooping(source, lua_toboolean(L, 2));
return 0;
}
const luaL_Reg lovrSource[] = {
{ "play", l_lovrSourcePlay },
{ "isLooping", l_lovrSourceIsLooping },
{ "setLooping", l_lovrSourceSetLooping },
{ NULL, NULL }
};

View File

@ -28,21 +28,9 @@ static struct {
static void onPlayback(ma_device* device, void* output, const void* input, uint32_t frames) {
ma_mutex_lock(&state.locks[0]);
Source* source = state.sources;
for (;;) {
if (!source) {
break;
}
if (!source->playing) {
continue;
}
}
for (Source* source = state.sources; source != NULL; source = source->next) {
for (Source** list = &state.sources, *source = *list; source != NULL; source = *list) {
if (!source->playing) {
*list = source->next;
source->tracked = false;
lovrRelease(Source, source);
continue;
@ -51,10 +39,16 @@ static void onPlayback(ma_device* device, void* output, const void* input, uint3
uint32_t n = source->sound->read(source->sound, source->offset, frames, output);
if (n < frames) {
source->offset = 0;
if (source->looping) {
source->offset = 0;
} else {
source->playing = false;
}
} else {
source->offset += n;
}
list = &source->next;
}
ma_mutex_unlock(&state.locks[0]);
@ -216,5 +210,5 @@ void lovrSourceSetTime(Source* source, uint32_t time) {
}
uint32_t lovrSourceGetDuration(Source* source) {
return 0; // TODO
return source->sound->frames;
}

View File

@ -32,13 +32,15 @@ static uint32_t lovrSoundDataReadOgg(SoundData* soundData, uint32_t offset, uint
}
uint32_t frames = 0;
uint32_t channels = soundData->channels;
float* p = data;
int n;
do {
n = stb_vorbis_get_samples_float_interleaved(soundData->decoder, soundData->channels, p, (int) (count - frames));
p += n * soundData->channels;
n = stb_vorbis_get_samples_float_interleaved(soundData->decoder, channels, p, count * channels);
p += n * channels;
frames += n;
count -= n;
} while (frames < count && n > 0);
soundData->cursor += frames;