Source:getDuration; Source:get/setTime;

This commit is contained in:
bjorn 2020-11-21 20:42:15 -07:00 committed by Bjorn
parent f7169ec236
commit 533939e673
3 changed files with 51 additions and 3 deletions

View File

@ -1,5 +1,6 @@
#include "api.h"
#include "audio/audio.h"
#include "data/soundData.h"
static int l_lovrSourcePlay(lua_State* L) {
Source* source = luax_checktype(L, 1, Source);
@ -50,6 +51,50 @@ static int l_lovrSourceSetVolume(lua_State* L) {
return 0;
}
static int l_lovrSourceGetDuration(lua_State* L) {
Source* source = luax_checktype(L, 1, Source);
TimeUnit units = luax_checkenum(L, 2, TimeUnit, "seconds");
SoundData* sound = lovrSourceGetSoundData(source);
if (units == UNIT_SECONDS) {
lua_pushnumber(L, (double) sound->frames / sound->sampleRate);
} else {
lua_pushinteger(L, sound->frames);
}
return 1;
}
static int l_lovrSourceGetTime(lua_State* L) {
Source* source = luax_checktype(L, 1, Source);
TimeUnit units = luax_checkenum(L, 2, TimeUnit, "seconds");
uint32_t offset = lovrSourceGetTime(source);
if (units == UNIT_SECONDS) {
SoundData* sound = lovrSourceGetSoundData(source);
lua_pushnumber(L, (double) offset / sound->sampleRate);
} else {
lua_pushinteger(L, offset);
}
return 1;
}
static int l_lovrSourceSetTime(lua_State* L) {
Source* source = luax_checktype(L, 1, Source);
TimeUnit units = luax_checkenum(L, 3, TimeUnit, "seconds");
if (units == UNIT_SECONDS) {
double seconds = luaL_checknumber(L, 2);
SoundData* sound = lovrSourceGetSoundData(source);
lovrSourceSetTime(source, (uint32_t) (seconds * sound->sampleRate + .5));
} else {
lovrSourceSetTime(source, luaL_checkinteger(L, 2));
}
return 0;
}
const luaL_Reg lovrSource[] = {
{ "play", l_lovrSourcePlay },
{ "pause", l_lovrSourcePause },
@ -59,5 +104,8 @@ const luaL_Reg lovrSource[] = {
{ "setLooping", l_lovrSourceSetLooping },
{ "getVolume", l_lovrSourceGetVolume },
{ "setVolume", l_lovrSourceSetVolume },
{ "getDuration", l_lovrSourceGetDuration },
{ "getTime", l_lovrSourceGetTime },
{ "setTime", l_lovrSourceSetTime },
{ NULL, NULL }
};

View File

@ -326,6 +326,6 @@ void lovrSourceSetTime(Source* source, uint32_t time) {
ma_mutex_unlock(&state.locks[AUDIO_PLAYBACK]);
}
uint32_t lovrSourceGetDuration(Source* source) {
return source->sound->frames;
SoundData* lovrSourceGetSoundData(Source* source) {
return source->sound;
}

View File

@ -47,4 +47,4 @@ float lovrSourceGetVolume(Source* source);
void lovrSourceSetVolume(Source* source, float volume);
uint32_t lovrSourceGetTime(Source* source);
void lovrSourceSetTime(Source* source, uint32_t sample);
uint32_t lovrSourceGetDuration(Source* source);
struct SoundData* lovrSourceGetSoundData(Source* source);