lovr/src/api/l_audio.c

183 lines
4.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
};
LOVR_USE_OCULUS_AUDIO This is a large patch which adds a new Oculus Audio spatializer. Oculus Audio is slightly different from the dummy spatializer in a few ways: - It *must* receive fixed-size input buffers, every time, always. - It can only handle a fixed number of spatialized sound sources at a time. - It has a concept of "tails"; the spatialization of a sound can continue after the sound itself ends (eg echo). Changes to audio.c were needed to support Oculus Audio's quirks: - audio.c now supports a "fixedBuffer" mode which invokes the generator/spatializer in fixed size chunks - Each source now has an intptr_t "memo" field that the spatializer may use to store whatever (Oculus spatializer uses this to handle the sound source limit). - The spatializer interface got a couple new methods: A "tail" method which returns a sound buffer after all sources are processed; and "create" and "destroy" methods that are called when a sound source is created or destroyed (Oculus spatializer uses this to populate/clear the "memo" field). Along the way some other miscellaneous changes got made: - lovr.audio.getSpatializerName() returns the current spatializer - Spatializer init now takes in "config in" and "config out" structs (Spatializer changes fields in config out to request things, currently fixed buffer mode). - lovr.conf now takes t.audio.spatializer (string name of desired spatializer) and t.audio.spatializerMaxSourcesHint (Spatializers with max sources limits like Oculus will use this as the limit). - audio.c went back to tracking position/orientation as vectors rather than a matrix - A file oculus_spatializer_math_shim.h was added containing a minimal copypaste of OVR_CAPI.h from Oculus SDK to support a ovrPoseStatef the spatializer API needs. This may have license consequences but we are probably OK via a combination of fair use and the fact that a user cannot use this header file without accepting Oculus's license through other means. Some work remains to be done, in particular there is an entire reverb feature I did not touch and LOVR_USE_OCULUS_AUDIO cannot be activated from tup. Oculus Spatializer works better when it has velocity and time information but this patch does not supply it.
2020-12-19 22:17:16 +00:00
#define AUDIO_SPATIALIZER_MAX_SOURCES_HINT 16
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-12-02 15:56:06 +00:00
bool started = lovrAudioStart(type);
lua_pushboolean(L, started);
return 1;
}
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;
}
2020-12-17 10:41:46 +00:00
static int l_lovrAudioIsRunning(lua_State* L) {
AudioType type = luax_checkenum(L, 1, AudioType, "playback");
bool isRunning = lovrAudioIsRunning(type);
lua_pushboolean(L, isRunning);
return 1;
}
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);
2020-12-02 15:35:38 +00:00
bool spatial = true;
if (lua_istable(L, 2)) {
lua_getfield(L, 2, "spatial");
if (lua_isboolean(L, -1)) {
spatial = lua_toboolean(L, -1);
}
lua_pop(L, 1);
}
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;
}
static int l_lovrAudioGetCaptureStream(lua_State* L) {
SoundData* soundData = lovrAudioGetCaptureStream();
2020-11-26 12:46:55 +00:00
luax_pushtype(L, SoundData, soundData);
return 1;
}
2020-12-10 14:27:09 +00:00
static int l_lovrAudioGetDevices(lua_State *L) {
AudioType type = luax_checkenum(L, 1, AudioType, "playback");
AudioDeviceArr *devices = lovrAudioGetDevices(type);
2020-12-10 21:33:52 +00:00
lua_newtable(L);
int listOfDevicesIdx = lua_gettop(L);
for(int i = 0; i < devices->length; i++) {
AudioDevice *device = &devices->data[i];
2020-12-10 21:33:52 +00:00
lua_newtable(L);
luax_pushenum(L, AudioType, device->type);
lua_setfield(L, -2, "type");
lua_pushstring(L, device->name);
lua_setfield(L, -2, "name");
lua_pushboolean(L, device->isDefault);
lua_setfield(L, -2, "isDefault");
lua_rawseti(L, listOfDevicesIdx, i + 1);
2020-12-10 14:27:09 +00:00
}
lovrAudioFreeDevices(devices);
2020-12-10 21:33:52 +00:00
return 1;
2020-11-26 12:46:55 +00:00
}
static int l_lovrAudioUseDevice(lua_State *L) {
AudioType type = luax_checkenum(L, 1, AudioType, "playback");
const char *name = lua_tostring(L, 2);
lovrAudioUseDevice(type, name);
return 0;
}
static int l_lovrAudioSetCaptureFormat(lua_State *L) {
SampleFormat format = luax_checkenum(L, 1, SampleFormat, "invalid");
int sampleRate = lua_tointeger(L, 2);
lovrAudioSetCaptureFormat(format, sampleRate);
2020-11-26 12:46:55 +00:00
return 0;
}
LOVR_USE_OCULUS_AUDIO This is a large patch which adds a new Oculus Audio spatializer. Oculus Audio is slightly different from the dummy spatializer in a few ways: - It *must* receive fixed-size input buffers, every time, always. - It can only handle a fixed number of spatialized sound sources at a time. - It has a concept of "tails"; the spatialization of a sound can continue after the sound itself ends (eg echo). Changes to audio.c were needed to support Oculus Audio's quirks: - audio.c now supports a "fixedBuffer" mode which invokes the generator/spatializer in fixed size chunks - Each source now has an intptr_t "memo" field that the spatializer may use to store whatever (Oculus spatializer uses this to handle the sound source limit). - The spatializer interface got a couple new methods: A "tail" method which returns a sound buffer after all sources are processed; and "create" and "destroy" methods that are called when a sound source is created or destroyed (Oculus spatializer uses this to populate/clear the "memo" field). Along the way some other miscellaneous changes got made: - lovr.audio.getSpatializerName() returns the current spatializer - Spatializer init now takes in "config in" and "config out" structs (Spatializer changes fields in config out to request things, currently fixed buffer mode). - lovr.conf now takes t.audio.spatializer (string name of desired spatializer) and t.audio.spatializerMaxSourcesHint (Spatializers with max sources limits like Oculus will use this as the limit). - audio.c went back to tracking position/orientation as vectors rather than a matrix - A file oculus_spatializer_math_shim.h was added containing a minimal copypaste of OVR_CAPI.h from Oculus SDK to support a ovrPoseStatef the spatializer API needs. This may have license consequences but we are probably OK via a combination of fair use and the fact that a user cannot use this header file without accepting Oculus's license through other means. Some work remains to be done, in particular there is an entire reverb feature I did not touch and LOVR_USE_OCULUS_AUDIO cannot be activated from tup. Oculus Spatializer works better when it has velocity and time information but this patch does not supply it.
2020-12-19 22:17:16 +00:00
static int l_lovrAudioGetSpatializerName(lua_State *L) {
lua_pushstring(L, lovrSourceGetSpatializerName());
return 1;
}
2018-09-27 01:27:38 +00:00
static const luaL_Reg lovrAudio[] = {
2020-05-10 08:48:01 +00:00
{ "start", l_lovrAudioStart },
{ "stop", l_lovrAudioStop },
2020-12-17 10:41:46 +00:00
{ "isRunning", l_lovrAudioIsRunning },
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 },
{ "getCaptureStream", l_lovrAudioGetCaptureStream },
2020-12-10 14:27:09 +00:00
{ "getDevices", l_lovrAudioGetDevices },
{ "useDevice", l_lovrAudioUseDevice },
{ "setCaptureFormat", l_lovrAudioSetCaptureFormat },
LOVR_USE_OCULUS_AUDIO This is a large patch which adds a new Oculus Audio spatializer. Oculus Audio is slightly different from the dummy spatializer in a few ways: - It *must* receive fixed-size input buffers, every time, always. - It can only handle a fixed number of spatialized sound sources at a time. - It has a concept of "tails"; the spatialization of a sound can continue after the sound itself ends (eg echo). Changes to audio.c were needed to support Oculus Audio's quirks: - audio.c now supports a "fixedBuffer" mode which invokes the generator/spatializer in fixed size chunks - Each source now has an intptr_t "memo" field that the spatializer may use to store whatever (Oculus spatializer uses this to handle the sound source limit). - The spatializer interface got a couple new methods: A "tail" method which returns a sound buffer after all sources are processed; and "create" and "destroy" methods that are called when a sound source is created or destroyed (Oculus spatializer uses this to populate/clear the "memo" field). Along the way some other miscellaneous changes got made: - lovr.audio.getSpatializerName() returns the current spatializer - Spatializer init now takes in "config in" and "config out" structs (Spatializer changes fields in config out to request things, currently fixed buffer mode). - lovr.conf now takes t.audio.spatializer (string name of desired spatializer) and t.audio.spatializerMaxSourcesHint (Spatializers with max sources limits like Oculus will use this as the limit). - audio.c went back to tracking position/orientation as vectors rather than a matrix - A file oculus_spatializer_math_shim.h was added containing a minimal copypaste of OVR_CAPI.h from Oculus SDK to support a ovrPoseStatef the spatializer API needs. This may have license consequences but we are probably OK via a combination of fair use and the fact that a user cannot use this header file without accepting Oculus's license through other means. Some work remains to be done, in particular there is an entire reverb feature I did not touch and LOVR_USE_OCULUS_AUDIO cannot be activated from tup. Oculus Spatializer works better when it has velocity and time information but this patch does not supply it.
2020-12-19 22:17:16 +00:00
{ "getSpatializerName", l_lovrAudioGetSpatializerName },
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);
LOVR_USE_OCULUS_AUDIO This is a large patch which adds a new Oculus Audio spatializer. Oculus Audio is slightly different from the dummy spatializer in a few ways: - It *must* receive fixed-size input buffers, every time, always. - It can only handle a fixed number of spatialized sound sources at a time. - It has a concept of "tails"; the spatialization of a sound can continue after the sound itself ends (eg echo). Changes to audio.c were needed to support Oculus Audio's quirks: - audio.c now supports a "fixedBuffer" mode which invokes the generator/spatializer in fixed size chunks - Each source now has an intptr_t "memo" field that the spatializer may use to store whatever (Oculus spatializer uses this to handle the sound source limit). - The spatializer interface got a couple new methods: A "tail" method which returns a sound buffer after all sources are processed; and "create" and "destroy" methods that are called when a sound source is created or destroyed (Oculus spatializer uses this to populate/clear the "memo" field). Along the way some other miscellaneous changes got made: - lovr.audio.getSpatializerName() returns the current spatializer - Spatializer init now takes in "config in" and "config out" structs (Spatializer changes fields in config out to request things, currently fixed buffer mode). - lovr.conf now takes t.audio.spatializer (string name of desired spatializer) and t.audio.spatializerMaxSourcesHint (Spatializers with max sources limits like Oculus will use this as the limit). - audio.c went back to tracking position/orientation as vectors rather than a matrix - A file oculus_spatializer_math_shim.h was added containing a minimal copypaste of OVR_CAPI.h from Oculus SDK to support a ovrPoseStatef the spatializer API needs. This may have license consequences but we are probably OK via a combination of fair use and the fact that a user cannot use this header file without accepting Oculus's license through other means. Some work remains to be done, in particular there is an entire reverb feature I did not touch and LOVR_USE_OCULUS_AUDIO cannot be activated from tup. Oculus Spatializer works better when it has velocity and time information but this patch does not supply it.
2020-12-19 22:17:16 +00:00
char *spatializer = NULL;
int spatializerMaxSourcesHint = AUDIO_SPATIALIZER_MAX_SOURCES_HINT;
luax_pushconf(L);
lua_getfield(L, -1, "audio");
if (lua_istable(L, -1)) {
lua_getfield(L, -1, "spatializerMaxSourcesHint");
if (lua_type(L, -1) == LUA_TNUMBER)
spatializerMaxSourcesHint = lua_tointeger(L, -1);
lua_pop(L, 1);
lua_getfield(L, -1, "spatializer");
if (lua_type(L, -1) == LUA_TSTRING)
spatializer = lua_tostring(L, -1);
lua_pop(L, 1);
}
lua_pop(L, 2);
SpatializerConfig config = {
.spatializer = spatializer,
.spatializerMaxSourcesHint=spatializerMaxSourcesHint
};
if (lovrAudioInit(config)) {
lovrAudioStart(AUDIO_PLAYBACK);
luax_atexit(L, lovrAudioDestroy);
}
2018-09-27 01:27:38 +00:00
return 1;
}