1
0
Fork 0
mirror of https://github.com/bjornbytes/lovr.git synced 2024-07-21 21:23:35 +00:00
lovr/src/api/l_source.c

333 lines
9.6 KiB
C
Raw Normal View History

2017-12-10 20:40:37 +00:00
#include "api.h"
#include "audio/audio.h"
2019-04-05 11:16:34 +00:00
#include "audio/source.h"
2019-05-20 09:47:33 +00:00
#include "core/maf.h"
2017-10-31 08:14:09 +00:00
#include <stdbool.h>
2017-01-06 01:00:53 +00:00
2019-02-17 22:52:22 +00:00
static int l_lovrSourceGetBitDepth(lua_State* L) {
2017-01-06 01:00:53 +00:00
Source* source = luax_checktype(L, 1, Source);
lua_pushinteger(L, lovrSourceGetBitDepth(source));
return 1;
}
2019-02-17 22:52:22 +00:00
static int l_lovrSourceGetChannelCount(lua_State* L) {
2017-01-06 01:00:53 +00:00
Source* source = luax_checktype(L, 1, Source);
lua_pushinteger(L, lovrSourceGetChannelCount(source));
2017-01-06 01:00:53 +00:00
return 1;
}
2019-02-17 22:52:22 +00:00
static int l_lovrSourceGetCone(lua_State* L) {
2017-02-26 20:50:51 +00:00
Source* source = luax_checktype(L, 1, Source);
float innerAngle, outerAngle, outerGain;
lovrSourceGetCone(source, &innerAngle, &outerAngle, &outerGain);
lua_pushnumber(L, innerAngle);
lua_pushnumber(L, outerAngle);
lua_pushnumber(L, outerGain);
return 3;
}
2019-02-17 22:52:22 +00:00
static int l_lovrSourceGetDuration(lua_State* L) {
2017-01-06 01:00:53 +00:00
Source* source = luax_checktype(L, 1, Source);
2018-07-05 03:11:52 +00:00
TimeUnit unit = luaL_checkoption(L, 2, "seconds", TimeUnits);
2017-01-06 04:21:16 +00:00
int duration = lovrSourceGetDuration(source);
2018-07-05 03:11:52 +00:00
if (unit == UNIT_SECONDS) {
2017-01-06 04:21:16 +00:00
lua_pushnumber(L, (float) duration / lovrSourceGetSampleRate(source));
} else {
lua_pushinteger(L, duration);
}
2017-01-06 01:00:53 +00:00
return 1;
}
2019-02-17 22:52:22 +00:00
static int l_lovrSourceGetFalloff(lua_State* L) {
2017-02-26 20:37:18 +00:00
Source* source = luax_checktype(L, 1, Source);
float reference, max, rolloff;
lovrSourceGetFalloff(source, &reference, &max, &rolloff);
lua_pushnumber(L, reference);
lua_pushnumber(L, max);
lua_pushnumber(L, rolloff);
return 3;
}
static int l_lovrSourceGetOrientation(lua_State* L) {
float orientation[4], angle, ax, ay, az;
Source* source = luax_checktype(L, 1, Source);
lovrSourceGetOrientation(source, orientation);
quat_getAngleAxis(orientation, &angle, &ax, &ay, &az);
lua_pushnumber(L, angle);
lua_pushnumber(L, ax);
lua_pushnumber(L, ay);
lua_pushnumber(L, az);
return 4;
}
2019-02-17 22:52:22 +00:00
static int l_lovrSourceGetPitch(lua_State* L) {
2017-01-06 04:32:22 +00:00
Source* source = luax_checktype(L, 1, Source);
lua_pushnumber(L, lovrSourceGetPitch(source));
return 1;
}
2019-02-19 02:07:28 +00:00
static int l_lovrSourceGetPose(lua_State* L) {
2019-06-03 14:13:52 +00:00
float position[4], orientation[4], angle, ax, ay, az;
2019-02-19 02:07:28 +00:00
Source* source = luax_checktype(L, 1, Source);
lovrSourceGetPosition(source, position);
lovrSourceGetOrientation(source, orientation);
quat_getAngleAxis(orientation, &angle, &ax, &ay, &az);
lua_pushnumber(L, position[0]);
lua_pushnumber(L, position[1]);
lua_pushnumber(L, position[2]);
lua_pushnumber(L, angle);
lua_pushnumber(L, ax);
lua_pushnumber(L, ay);
lua_pushnumber(L, az);
return 7;
}
2019-02-17 22:52:22 +00:00
static int l_lovrSourceGetPosition(lua_State* L) {
2019-06-03 14:13:52 +00:00
float position[4];
2019-02-19 02:07:28 +00:00
lovrSourceGetPosition(luax_checktype(L, 1, Source), position);
lua_pushnumber(L, position[0]);
lua_pushnumber(L, position[1]);
lua_pushnumber(L, position[2]);
return 3;
}
2019-02-17 22:52:22 +00:00
static int l_lovrSourceGetSampleRate(lua_State* L) {
2017-01-06 01:00:53 +00:00
Source* source = luax_checktype(L, 1, Source);
lua_pushinteger(L, lovrSourceGetSampleRate(source));
return 1;
}
2019-02-17 22:52:22 +00:00
static int l_lovrSourceGetType(lua_State* L) {
2018-08-17 02:10:26 +00:00
Source* source = luax_checktype(L, 1, Source);
lua_pushstring(L, SourceTypes[lovrSourceGetType(source)]);
return 1;
}
2019-02-17 22:52:22 +00:00
static int l_lovrSourceGetVelocity(lua_State* L) {
2019-06-03 14:13:52 +00:00
float velocity[4];
2019-02-19 02:07:28 +00:00
lovrSourceGetVelocity(luax_checktype(L, 1, Source), velocity);
lua_pushnumber(L, velocity[0]);
lua_pushnumber(L, velocity[1]);
lua_pushnumber(L, velocity[2]);
return 3;
}
2019-02-17 22:52:22 +00:00
static int l_lovrSourceGetVolume(lua_State* L) {
2017-01-06 04:27:32 +00:00
Source* source = luax_checktype(L, 1, Source);
lua_pushnumber(L, lovrSourceGetVolume(source));
return 1;
}
2019-02-17 22:52:22 +00:00
static int l_lovrSourceGetVolumeLimits(lua_State* L) {
2017-03-01 04:03:47 +00:00
Source* source = luax_checktype(L, 1, Source);
float min, max;
lovrSourceGetVolumeLimits(source, &min, &max);
lua_pushnumber(L, min);
lua_pushnumber(L, max);
return 2;
}
2019-02-17 22:52:22 +00:00
static int l_lovrSourceIsLooping(lua_State* L) {
2017-01-06 02:22:38 +00:00
lua_pushboolean(L, lovrSourceIsLooping(luax_checktype(L, 1, Source)));
return 1;
}
2019-02-17 22:52:22 +00:00
static int l_lovrSourceIsPaused(lua_State* L) {
2017-03-01 04:03:47 +00:00
lua_pushboolean(L, lovrSourceIsPaused(luax_checktype(L, 1, Source)));
2017-01-06 01:00:53 +00:00
return 1;
}
2019-02-17 22:52:22 +00:00
static int l_lovrSourceIsPlaying(lua_State* L) {
2017-03-01 04:03:47 +00:00
lua_pushboolean(L, lovrSourceIsPlaying(luax_checktype(L, 1, Source)));
return 1;
}
2019-02-17 22:52:22 +00:00
static int l_lovrSourceIsRelative(lua_State* L) {
2017-03-01 04:03:47 +00:00
lua_pushboolean(L, lovrSourceIsRelative(luax_checktype(L, 1, Source)));
2017-01-06 01:00:53 +00:00
return 1;
}
2019-02-17 22:52:22 +00:00
static int l_lovrSourceIsStopped(lua_State* L) {
2017-03-01 04:03:47 +00:00
lua_pushboolean(L, lovrSourceIsStopped(luax_checktype(L, 1, Source)));
2017-01-06 01:00:53 +00:00
return 1;
}
2019-02-17 22:52:22 +00:00
static int l_lovrSourcePause(lua_State* L) {
2017-01-06 01:00:53 +00:00
lovrSourcePause(luax_checktype(L, 1, Source));
return 0;
}
2019-02-17 22:52:22 +00:00
static int l_lovrSourcePlay(lua_State* L) {
Source* source = luax_checktype(L, 1, Source);
lovrSourcePlay(source);
lovrAudioAdd(source);
2017-01-06 01:00:53 +00:00
return 0;
}
2019-02-17 22:52:22 +00:00
static int l_lovrSourceResume(lua_State* L) {
2017-01-06 01:00:53 +00:00
lovrSourceResume(luax_checktype(L, 1, Source));
return 0;
}
2019-02-17 22:52:22 +00:00
static int l_lovrSourceRewind(lua_State* L) {
2017-01-06 01:00:53 +00:00
lovrSourceRewind(luax_checktype(L, 1, Source));
return 0;
}
2019-02-17 22:52:22 +00:00
static int l_lovrSourceSeek(lua_State* L) {
2017-01-06 04:21:16 +00:00
Source* source = luax_checktype(L, 1, Source);
2018-07-05 03:11:52 +00:00
TimeUnit unit = luaL_checkoption(L, 3, "seconds", TimeUnits);
2017-01-06 04:21:16 +00:00
2018-07-05 03:11:52 +00:00
if (unit == UNIT_SECONDS) {
float seconds = luax_checkfloat(L, 2);
2017-01-06 04:21:16 +00:00
int sampleRate = lovrSourceGetSampleRate(source);
lovrSourceSeek(source, (int) (seconds * sampleRate + .5f));
} else {
lovrSourceSeek(source, luaL_checkinteger(L, 2));
}
return 0;
}
2019-02-17 22:52:22 +00:00
static int l_lovrSourceSetCone(lua_State* L) {
2017-02-26 20:50:51 +00:00
Source* source = luax_checktype(L, 1, Source);
float innerAngle = luax_checkfloat(L, 1);
float outerAngle = luax_checkfloat(L, 2);
float outerGain = luax_checkfloat(L, 3);
2017-02-26 20:50:51 +00:00
lovrSourceSetCone(source, innerAngle, outerAngle, outerGain);
return 0;
}
2019-02-17 22:52:22 +00:00
static int l_lovrSourceSetFalloff(lua_State* L) {
2017-02-26 20:37:18 +00:00
Source* source = luax_checktype(L, 1, Source);
float reference = luax_checkfloat(L, 2);
float max = luax_checkfloat(L, 3);
float rolloff = luax_checkfloat(L, 4);
2017-02-26 20:37:18 +00:00
lovrSourceSetFalloff(source, reference, max, rolloff);
return 0;
}
2019-02-17 22:52:22 +00:00
static int l_lovrSourceSetLooping(lua_State* L) {
2017-01-06 02:22:38 +00:00
lovrSourceSetLooping(luax_checktype(L, 1, Source), lua_toboolean(L, 2));
return 0;
}
static int l_lovrSourceSetOrientation(lua_State* L) {
Source* source = luax_checktype(L, 1, Source);
float orientation[4];
luax_readquat(L, 2, orientation, NULL);
lovrSourceSetOrientation(source, orientation);
return 0;
}
2019-02-17 22:52:22 +00:00
static int l_lovrSourceSetPitch(lua_State* L) {
lovrSourceSetPitch(luax_checktype(L, 1, Source), luax_checkfloat(L, 2));
2017-01-06 04:32:22 +00:00
return 0;
}
2019-02-19 02:07:28 +00:00
static int l_lovrSourceSetPose(lua_State* L) {
2019-06-03 14:13:52 +00:00
float position[4], orientation[4];
2019-02-19 02:07:28 +00:00
int index = 2;
Source* source = luax_checktype(L, 1, Source);
index = luax_readvec3(L, index, position, NULL);
index = luax_readquat(L, index, orientation, NULL);
lovrSourceSetPosition(source, position);
lovrSourceSetOrientation(source, orientation);
return 0;
}
2019-02-17 22:52:22 +00:00
static int l_lovrSourceSetPosition(lua_State* L) {
Source* source = luax_checktype(L, 1, Source);
2019-06-03 14:13:52 +00:00
float position[4];
2018-11-27 23:03:05 +00:00
luax_readvec3(L, 2, position, NULL);
2019-02-19 02:07:28 +00:00
lovrSourceSetPosition(source, position);
return 0;
}
2019-02-17 22:52:22 +00:00
static int l_lovrSourceSetRelative(lua_State* L) {
2017-03-01 04:03:47 +00:00
Source* source = luax_checktype(L, 1, Source);
2017-10-31 08:14:09 +00:00
bool isRelative = lua_toboolean(L, 2);
2017-03-01 04:03:47 +00:00
lovrSourceSetRelative(source, isRelative);
return 0;
}
2019-02-17 22:52:22 +00:00
static int l_lovrSourceSetVelocity(lua_State* L) {
Source* source = luax_checktype(L, 1, Source);
2019-06-03 14:13:52 +00:00
float velocity[4];
2018-11-27 23:03:05 +00:00
luax_readvec3(L, 2, velocity, NULL);
2019-02-19 02:07:28 +00:00
lovrSourceSetVelocity(source, velocity);
return 0;
}
2019-02-17 22:52:22 +00:00
static int l_lovrSourceSetVolume(lua_State* L) {
lovrSourceSetVolume(luax_checktype(L, 1, Source), luax_checkfloat(L, 2));
2017-01-06 04:27:32 +00:00
return 0;
}
2019-02-17 22:52:22 +00:00
static int l_lovrSourceSetVolumeLimits(lua_State* L) {
lovrSourceSetVolumeLimits(luax_checktype(L, 1, Source), luax_checkfloat(L, 2), luax_checkfloat(L, 3));
2017-03-01 04:03:47 +00:00
return 0;
}
2019-02-17 22:52:22 +00:00
static int l_lovrSourceStop(lua_State* L) {
2017-01-06 01:00:53 +00:00
lovrSourceStop(luax_checktype(L, 1, Source));
return 0;
}
2017-01-06 04:21:16 +00:00
2019-02-17 22:52:22 +00:00
static int l_lovrSourceTell(lua_State* L) {
2017-01-06 04:21:16 +00:00
Source* source = luax_checktype(L, 1, Source);
2018-07-05 03:11:52 +00:00
TimeUnit unit = luaL_checkoption(L, 2, "seconds", TimeUnits);
2017-01-06 04:21:16 +00:00
int offset = lovrSourceTell(source);
2018-07-05 03:11:52 +00:00
if (unit == UNIT_SECONDS) {
2017-01-06 04:21:16 +00:00
lua_pushnumber(L, (float) offset / lovrSourceGetSampleRate(source));
} else {
lua_pushinteger(L, offset);
}
return 1;
}
2017-03-11 11:08:07 +00:00
const luaL_Reg lovrSource[] = {
{ "getBitDepth", l_lovrSourceGetBitDepth },
{ "getChannelCount", l_lovrSourceGetChannelCount },
2017-03-11 11:08:07 +00:00
{ "getCone", l_lovrSourceGetCone },
{ "getDuration", l_lovrSourceGetDuration },
{ "getFalloff", l_lovrSourceGetFalloff },
{ "getOrientation", l_lovrSourceGetOrientation },
2017-03-11 11:08:07 +00:00
{ "getPitch", l_lovrSourceGetPitch },
2019-02-19 02:07:28 +00:00
{ "getPose", l_lovrSourceGetPose },
2017-03-11 11:08:07 +00:00
{ "getPosition", l_lovrSourceGetPosition },
{ "getSampleRate", l_lovrSourceGetSampleRate },
2018-08-17 02:10:26 +00:00
{ "getType", l_lovrSourceGetType },
2017-03-11 11:08:07 +00:00
{ "getVelocity", l_lovrSourceGetVelocity },
{ "getVolume", l_lovrSourceGetVolume },
{ "getVolumeLimits", l_lovrSourceGetVolumeLimits },
{ "isLooping", l_lovrSourceIsLooping },
{ "isPaused", l_lovrSourceIsPaused },
{ "isPlaying", l_lovrSourceIsPlaying },
{ "isRelative", l_lovrSourceIsRelative },
{ "isStopped", l_lovrSourceIsStopped },
{ "pause", l_lovrSourcePause },
{ "play", l_lovrSourcePlay },
{ "resume", l_lovrSourceResume },
{ "rewind", l_lovrSourceRewind },
{ "seek", l_lovrSourceSeek },
{ "setCone", l_lovrSourceSetCone },
{ "setFalloff", l_lovrSourceSetFalloff },
{ "setLooping", l_lovrSourceSetLooping },
{ "setOrientation", l_lovrSourceSetOrientation },
2017-03-11 11:08:07 +00:00
{ "setPitch", l_lovrSourceSetPitch },
2019-02-19 02:07:28 +00:00
{ "setPose", l_lovrSourceSetPose },
2017-03-11 11:08:07 +00:00
{ "setPosition", l_lovrSourceSetPosition },
{ "setRelative", l_lovrSourceSetRelative },
{ "setVelocity", l_lovrSourceSetVelocity },
{ "setVolume", l_lovrSourceSetVolume },
{ "setVolumeLimits", l_lovrSourceSetVolumeLimits },
{ "stop", l_lovrSourceStop },
{ "tell", l_lovrSourceTell },
{ NULL, NULL }
};