lovr/src/api/l_audio.c

157 lines
3.8 KiB
C
Raw Normal View History

2017-12-10 20:40:37 +00:00
#include "api.h"
2017-01-03 03:09:33 +00:00
#include "audio/audio.h"
2019-05-18 00:11:22 +00:00
#include "data/blob.h"
2019-04-05 11:16:34 +00:00
#include "data/soundData.h"
#include "core/ref.h"
2020-12-25 19:50:26 +00:00
#include "core/util.h"
2019-05-13 10:53:17 +00:00
#include <stdlib.h>
2017-01-03 03:09:33 +00:00
2020-05-10 08:48:01 +00:00
StringEntry lovrAudioType[] = {
[AUDIO_PLAYBACK] = ENTRY("playback"),
[AUDIO_CAPTURE] = ENTRY("capture"),
{ 0 }
2018-07-06 05:08:14 +00:00
};
2020-09-28 00:13:00 +00:00
StringEntry lovrTimeUnit[] = {
[UNIT_SECONDS] = ENTRY("seconds"),
[UNIT_SAMPLES] = ENTRY("samples"),
{ 0 }
2018-07-05 03:11:52 +00:00
};
2017-01-03 03:09:33 +00:00
2020-05-10 08:48:01 +00:00
static int l_lovrAudioReset(lua_State* L) {
lovrAudioReset();
2017-01-06 01:00:53 +00:00
return 0;
}
2020-05-10 08:48:01 +00:00
static int l_lovrAudioStart(lua_State* L) {
2020-11-21 03:47:59 +00:00
AudioType type = luax_checkenum(L, 1, AudioType, "playback");
2020-05-10 08:48:01 +00:00
lovrAudioStart(type);
return 0;
}
2020-05-10 08:48:01 +00:00
static int l_lovrAudioStop(lua_State* L) {
2020-11-21 03:47:59 +00:00
AudioType type = luax_checkenum(L, 1, AudioType, "playback");
2020-05-10 08:48:01 +00:00
lovrAudioStop(type);
return 0;
}
2018-09-27 01:27:38 +00:00
static int l_lovrAudioGetVolume(lua_State* L) {
lua_pushnumber(L, lovrAudioGetVolume());
return 1;
}
2020-05-10 08:48:01 +00:00
static int l_lovrAudioSetVolume(lua_State* L) {
float volume = luax_checkfloat(L, 1);
lovrAudioSetVolume(volume);
return 0;
}
2018-09-27 01:27:38 +00:00
static int l_lovrAudioNewSource(lua_State* L) {
2018-07-06 05:08:14 +00:00
Source* source = NULL;
SoundData* soundData = luax_totype(L, 1, SoundData);
bool spatial = lua_isboolean(L, 2) ? lua_toboolean(L, 2) : true;
2018-07-06 05:08:14 +00:00
2020-05-10 08:48:01 +00:00
if (soundData) {
source = lovrSourceCreate(soundData, spatial);
2018-01-22 16:14:24 +00:00
} else {
2020-05-10 08:48:01 +00:00
Blob* blob = luax_readblob(L, 1, "Source");
soundData = lovrSoundDataCreateFromFile(blob, false);
lovrRelease(Blob, blob);
source = lovrSourceCreate(soundData, spatial);
2020-05-10 08:48:01 +00:00
lovrRelease(SoundData, soundData);
2018-01-22 16:14:24 +00:00
}
luax_pushtype(L, Source, source);
lovrRelease(Source, source);
2017-01-06 01:00:53 +00:00
return 1;
}
2020-11-25 21:28:43 +00:00
static int l_lovrAudioSetListenerPose(lua_State *L) {
float position[4], orientation[4];
int index = 1;
index = luax_readvec3(L, index, position, NULL);
index = luax_readquat(L, index, orientation, NULL);
lovrAudioSetListenerPose(position, orientation);
return 0;
}
2020-11-26 12:46:55 +00:00
static int l_lovrAudioGetCaptureDuration(lua_State *L) {
TimeUnit units = luax_checkenum(L, 1, TimeUnit, "seconds");
size_t sampleCount = lovrAudioGetCaptureSampleCount();
if (units == UNIT_SECONDS) {
lua_pushnumber(L, (double) sampleCount / LOVR_AUDIO_SAMPLE_RATE);
} else {
lua_pushinteger(L, sampleCount);
}
return 1;
}
static int l_lovrAudioCapture(lua_State* L) {
int index = 1;
size_t samples = lua_type(L, index) == LUA_TNUMBER ? lua_tointeger(L, index++) : lovrAudioGetCaptureSampleCount();
if (samples == 0) {
return 0;
}
SoundData* soundData = luax_totype(L, index++, SoundData);
size_t offset = soundData ? luaL_optinteger(L, index, 0) : 0;
if (soundData) {
lovrRetain(soundData);
}
soundData = lovrAudioCapture(samples, soundData, offset);
luax_pushtype(L, SoundData, soundData);
lovrRelease(SoundData, soundData);
return 1;
}
static int l_lovrAudioSetCaptureDevice(lua_State *L) {
//
return 0;
}
static int l_lovrAudioGetCaptureDevice(lua_State *L) {
//
return 0;
}
static int l_lovrAudioListCaptureDevices(lua_State *L) {
//
return 0;
}
2018-09-27 01:27:38 +00:00
static const luaL_Reg lovrAudio[] = {
2020-05-10 08:48:01 +00:00
{ "reset", l_lovrAudioReset },
{ "start", l_lovrAudioStart },
{ "stop", l_lovrAudioStop },
2017-03-11 11:08:07 +00:00
{ "getVolume", l_lovrAudioGetVolume },
{ "setVolume", l_lovrAudioSetVolume },
2020-05-10 08:48:01 +00:00
{ "newSource", l_lovrAudioNewSource },
2020-11-25 21:28:43 +00:00
{ "setListenerPose", l_lovrAudioSetListenerPose },
2020-11-26 12:46:55 +00:00
{ "capture", l_lovrAudioCapture },
{ "getCaptureDuration", l_lovrAudioGetCaptureDuration },
{ "setCaptureDevice", l_lovrAudioSetCaptureDevice },
{ "getCaptureDevice", l_lovrAudioGetCaptureDevice },
{ "listCaptureDevices", l_lovrAudioListCaptureDevices },
2017-03-11 11:08:07 +00:00
{ NULL, NULL }
};
2018-09-27 01:27:38 +00:00
int luaopen_lovr_audio(lua_State* L) {
2018-09-27 01:27:38 +00:00
lua_newtable(L);
2020-08-19 19:12:06 +00:00
luax_register(L, lovrAudio);
luax_registertype(L, Source);
2020-05-19 18:08:24 +00:00
AudioConfig config[2] = { { .enable = true, .start = true }, { .enable = true, .start = true } };
2020-05-10 08:48:01 +00:00
if (lovrAudioInit(config)) {
luax_atexit(L, lovrAudioDestroy);
}
2018-09-27 01:27:38 +00:00
return 1;
}