From 2719eba1bab0ec38cb92cdad0c6ededd7f53acd9 Mon Sep 17 00:00:00 2001 From: Nevyn Bengtsson Date: Wed, 13 Jan 2021 13:22:29 +0100 Subject: [PATCH] lovrAudioSetCaptureFormat instead of mixing up using device with using format --- src/api/l_audio.c | 16 +++++++++++----- src/modules/audio/audio.c | 21 ++++++++++++++++----- src/modules/audio/audio.h | 3 ++- 3 files changed, 29 insertions(+), 11 deletions(-) diff --git a/src/api/l_audio.c b/src/api/l_audio.c index 55d4e049..de739485 100644 --- a/src/api/l_audio.c +++ b/src/api/l_audio.c @@ -104,12 +104,17 @@ static int l_lovrAudioGetDevices(lua_State *L) { return 1; } -static int l_lovrUseDevice(lua_State *L) { +static int l_lovrAudioUseDevice(lua_State *L) { AudioType type = luax_checkenum(L, 1, AudioType, "playback"); const char *name = lua_tostring(L, 2); - int sampleRate = lua_tointeger(L, 3); - SampleFormat format = luax_checkenum(L, 4, SampleFormat, "invalid"); - lovrAudioUseDevice(type, name, sampleRate, format); + 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); return 0; } @@ -122,7 +127,8 @@ static const luaL_Reg lovrAudio[] = { { "setListenerPose", l_lovrAudioSetListenerPose }, { "getCaptureStream", l_lovrAudioGetCaptureStream }, { "getDevices", l_lovrAudioGetDevices }, - { "useDevice", l_lovrUseDevice }, + { "useDevice", l_lovrAudioUseDevice }, + { "setCaptureFormat", l_lovrAudioSetCaptureFormat }, { NULL, NULL } }; diff --git a/src/modules/audio/audio.c b/src/modules/audio/audio.c index 615b9869..6ddbdb85 100644 --- a/src/modules/audio/audio.c +++ b/src/modules/audio/audio.c @@ -202,7 +202,7 @@ bool lovrAudioInitDevice(AudioType type) { lovrLog(LOG_WARN, "audio", "No audio playback device called '%s'; falling back to default.", state.config[AUDIO_PLAYBACK].deviceName); } config.playback.channels = OUTPUT_CHANNELS; - } else { + } else { // if AUDIO_CAPTURE ma_device_type deviceType = ma_device_type_capture; config = ma_device_config_init(deviceType); @@ -444,13 +444,24 @@ void lovrAudioFreeDevices(AudioDeviceArr *devices) { arr_free(devices); } -void lovrAudioUseDevice(AudioType type, const char *deviceName, int sampleRate, SampleFormat format) { +void lovrAudioSetCaptureFormat(SampleFormat format, int sampleRate) +{ + if (sampleRate) state.config[AUDIO_CAPTURE].sampleRate = sampleRate; + if (format != SAMPLE_INVALID) state.config[AUDIO_CAPTURE].format = format; + + // restart device if needed + ma_uint32 previousState = state.devices[AUDIO_CAPTURE].state; + if (previousState != MA_STATE_UNINITIALIZED && previousState != MA_STATE_STOPPED) { + lovrAudioStop(AUDIO_CAPTURE); + lovrAudioStart(AUDIO_CAPTURE); + } +} + +void lovrAudioUseDevice(AudioType type, const char *deviceName) { free(state.config[type].deviceName); state.config[type].deviceName = strdup(deviceName); - if (sampleRate) state.config[type].sampleRate = sampleRate; - if (format != SAMPLE_INVALID) state.config[type].format = format; - + // restart device if needed ma_uint32 previousState = state.devices[type].state; if (previousState != MA_STATE_UNINITIALIZED && previousState != MA_STATE_STOPPED) { lovrAudioStop(type); diff --git a/src/modules/audio/audio.h b/src/modules/audio/audio.h index e5843b86..64512b91 100644 --- a/src/modules/audio/audio.h +++ b/src/modules/audio/audio.h @@ -71,4 +71,5 @@ AudioDeviceArr* lovrAudioGetDevices(AudioType type); // free a list of devices returned from above call void lovrAudioFreeDevices(AudioDeviceArr *devices); -void lovrAudioUseDevice(AudioType type, const char *deviceName, int sampleRate, SampleFormat format); +void lovrAudioSetCaptureFormat(SampleFormat format, int sampleRate); +void lovrAudioUseDevice(AudioType type, const char *deviceName);