lovr/src/api/audio.c

169 lines
4.1 KiB
C
Raw Normal View History

2017-03-11 10:25:39 +00:00
#include "api/audio.h"
#include "api/types/source.h"
2017-01-03 03:09:33 +00:00
#include "audio/audio.h"
2017-01-06 01:00:53 +00:00
#include "audio/source.h"
#include "loaders/source.h"
#include "filesystem/filesystem.h"
2017-01-03 03:09:33 +00:00
const luaL_Reg lovrAudio[] = {
2017-01-06 01:00:53 +00:00
{ "update", l_lovrAudioUpdate },
{ "getDopplerEffect", l_lovrAudioGetDopplerEffect },
{ "getOrientation", l_lovrAudioGetOrientation },
{ "getPosition", l_lovrAudioGetPosition },
{ "getVelocity", l_lovrAudioGetVelocity },
{ "getVolume", l_lovrAudioGetVolume },
2017-02-26 21:14:15 +00:00
{ "isSpatialized", l_lovrAudioIsSpatialized },
2017-01-06 01:00:53 +00:00
{ "newSource", l_lovrAudioNewSource },
{ "pause", l_lovrAudioPause },
{ "resume", l_lovrAudioResume },
{ "rewind", l_lovrAudioRewind },
{ "setDopplerEffect", l_lovrAudioSetDopplerEffect },
{ "setOrientation", l_lovrAudioSetOrientation },
{ "setPosition", l_lovrAudioSetPosition },
{ "setVelocity", l_lovrAudioSetVelocity },
{ "setVolume", l_lovrAudioSetVolume },
{ "stop", l_lovrAudioStop },
2017-01-03 03:09:33 +00:00
{ NULL, NULL }
};
int l_lovrAudioInit(lua_State* L) {
lua_newtable(L);
luaL_register(L, NULL, lovrAudio);
2017-01-06 01:00:53 +00:00
luax_registertype(L, "Source", lovrSource);
2017-01-06 04:21:16 +00:00
map_init(&TimeUnits);
map_set(&TimeUnits, "seconds", UNIT_SECONDS);
map_set(&TimeUnits, "samples", UNIT_SAMPLES);
2017-01-03 03:09:33 +00:00
lovrAudioInit();
return 1;
}
2017-01-06 01:00:53 +00:00
int l_lovrAudioUpdate(lua_State* L) {
lovrAudioUpdate();
return 0;
}
int l_lovrAudioGetDopplerEffect(lua_State* L) {
float factor, speedOfSound;
lovrAudioGetDopplerEffect(&factor, &speedOfSound);
lua_pushnumber(L, factor);
lua_pushnumber(L, speedOfSound);
return 2;
}
int l_lovrAudioGetOrientation(lua_State* L) {
2017-01-07 01:45:15 +00:00
float angle, ax, ay, az;
lovrAudioGetOrientation(&angle, &ax, &ay, &az);
lua_pushnumber(L, angle);
lua_pushnumber(L, ax);
lua_pushnumber(L, ay);
lua_pushnumber(L, az);
return 4;
}
int l_lovrAudioGetPosition(lua_State* L) {
float x, y, z;
lovrAudioGetPosition(&x, &y, &z);
lua_pushnumber(L, x);
lua_pushnumber(L, y);
lua_pushnumber(L, z);
return 3;
}
int l_lovrAudioGetVelocity(lua_State* L) {
float x, y, z;
lovrAudioGetVelocity(&x, &y, &z);
lua_pushnumber(L, x);
lua_pushnumber(L, y);
lua_pushnumber(L, z);
return 3;
}
int l_lovrAudioGetVolume(lua_State* L) {
lua_pushnumber(L, lovrAudioGetVolume());
return 1;
}
2017-02-26 21:14:15 +00:00
int l_lovrAudioIsSpatialized(lua_State* L) {
lua_pushnumber(L, lovrAudioIsSpatialized());
return 1;
}
2017-01-06 01:00:53 +00:00
int l_lovrAudioNewSource(lua_State* L) {
const char* filename = luaL_checkstring(L, 1);
if (!strstr(filename, ".ogg")) {
return luaL_error(L, "Only .ogg files are supported");
}
2017-03-11 09:37:00 +00:00
size_t size;
2017-01-06 01:00:53 +00:00
void* data = lovrFilesystemRead(filename, &size);
if (!data) {
return luaL_error(L, "Could not load source from file '%s'", filename);
}
2017-01-06 05:10:01 +00:00
SourceData* sourceData = lovrSourceDataFromFile(data, size);
2017-02-18 22:44:52 +00:00
Source* source = lovrSourceCreate(sourceData);
luax_pushtype(L, Source, source);
lovrRelease(&source->ref);
2017-01-06 01:00:53 +00:00
return 1;
}
int l_lovrAudioPause(lua_State* L) {
lovrAudioPause();
return 0;
}
int l_lovrAudioResume(lua_State* L) {
lovrAudioResume();
return 0;
}
int l_lovrAudioRewind(lua_State* L) {
lovrAudioRewind();
return 0;
}
int l_lovrAudioSetDopplerEffect(lua_State* L) {
float factor = luaL_checknumber(L, 1);
float speedOfSound = luaL_checknumber(L, 2);
lovrAudioSetDopplerEffect(factor, speedOfSound);
return 0;
}
int l_lovrAudioSetOrientation(lua_State* L) {
2017-01-07 01:45:15 +00:00
float angle = luaL_checknumber(L, 1);
float ax = luaL_checknumber(L, 2);
float ay = luaL_checknumber(L, 3);
float az = luaL_checknumber(L, 4);
lovrAudioSetOrientation(angle, ax, ay, az);
return 0;
}
int l_lovrAudioSetPosition(lua_State* L) {
float x = luaL_checknumber(L, 1);
float y = luaL_checknumber(L, 2);
float z = luaL_checknumber(L, 3);
lovrAudioSetPosition(x, y, z);
return 0;
}
int l_lovrAudioSetVelocity(lua_State* L) {
float x = luaL_checknumber(L, 1);
float y = luaL_checknumber(L, 2);
float z = luaL_checknumber(L, 3);
lovrAudioSetVelocity(x, y, z);
return 0;
}
int l_lovrAudioSetVolume(lua_State* L) {
float volume = luaL_checknumber(L, 1);
lovrAudioSetVolume(volume);
return 0;
}
int l_lovrAudioStop(lua_State* L) {
lovrAudioStop();
return 0;
}