From 90c7102b51e720d409e1e5597c7942b62e6a6e72 Mon Sep 17 00:00:00 2001 From: Fabio Alessandrelli Date: Mon, 18 May 2020 02:31:38 +0200 Subject: [PATCH] Move mix_rate, ouput_latency to AudioDriverManager Each driver used to define the (same) project settings values `audio/mix_rate` and `audio/output_latency`, but the setting names are not driver specific. Overriding is still possible via platform tags. --- drivers/alsa/audio_driver_alsa.cpp | 4 ++-- drivers/coreaudio/audio_driver_coreaudio.cpp | 6 +++--- drivers/pulseaudio/audio_driver_pulseaudio.cpp | 4 ++-- drivers/wasapi/audio_driver_wasapi.cpp | 2 +- drivers/xaudio2/audio_driver_xaudio2.cpp | 4 ++-- platform/android/audio_driver_jandroid.cpp | 4 ++-- platform/haiku/audio_driver_media_kit.cpp | 4 ++-- platform/javascript/audio_driver_javascript.cpp | 4 ++-- servers/audio/audio_driver_dummy.cpp | 4 ++-- servers/audio_server.cpp | 3 +++ servers/audio_server.h | 6 +++--- 11 files changed, 24 insertions(+), 21 deletions(-) diff --git a/drivers/alsa/audio_driver_alsa.cpp b/drivers/alsa/audio_driver_alsa.cpp index 414b0c28e02..90c3d3af836 100644 --- a/drivers/alsa/audio_driver_alsa.cpp +++ b/drivers/alsa/audio_driver_alsa.cpp @@ -38,7 +38,7 @@ #include Error AudioDriverALSA::init_device() { - mix_rate = GLOBAL_DEF("audio/mix_rate", DEFAULT_MIX_RATE); + mix_rate = GLOBAL_GET("audio/mix_rate"); speaker_mode = SPEAKER_MODE_STEREO; channels = 2; @@ -104,7 +104,7 @@ Error AudioDriverALSA::init_device() { // In ALSA the period size seems to be the one that will determine the actual latency // Ref: https://www.alsa-project.org/main/index.php/FramesPeriods unsigned int periods = 2; - int latency = GLOBAL_DEF("audio/output_latency", DEFAULT_OUTPUT_LATENCY); + int latency = GLOBAL_GET("audio/output_latency"); buffer_frames = closest_power_of_2(latency * mix_rate / 1000); buffer_size = buffer_frames * periods; period_size = buffer_frames; diff --git a/drivers/coreaudio/audio_driver_coreaudio.cpp b/drivers/coreaudio/audio_driver_coreaudio.cpp index d71d8cdabaa..48d0a295161 100644 --- a/drivers/coreaudio/audio_driver_coreaudio.cpp +++ b/drivers/coreaudio/audio_driver_coreaudio.cpp @@ -116,7 +116,7 @@ Error AudioDriverCoreAudio::init() { break; } - mix_rate = GLOBAL_DEF_RST("audio/mix_rate", DEFAULT_MIX_RATE); + mix_rate = GLOBAL_GET("audio/mix_rate"); zeromem(&strdesc, sizeof(strdesc)); strdesc.mFormatID = kAudioFormatLinearPCM; @@ -131,7 +131,7 @@ Error AudioDriverCoreAudio::init() { result = AudioUnitSetProperty(audio_unit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, kOutputBus, &strdesc, sizeof(strdesc)); ERR_FAIL_COND_V(result != noErr, FAILED); - int latency = GLOBAL_DEF_RST("audio/output_latency", DEFAULT_OUTPUT_LATENCY); + int latency = GLOBAL_GET("audio/output_latency"); // Sample rate is independent of channels (ref: https://stackoverflow.com/questions/11048825/audio-sample-frequency-rely-on-channels) buffer_frames = closest_power_of_2(latency * mix_rate / 1000); @@ -403,7 +403,7 @@ Error AudioDriverCoreAudio::capture_init() { break; } - mix_rate = GLOBAL_DEF_RST("audio/mix_rate", DEFAULT_MIX_RATE); + mix_rate = GLOBAL_GET("audio/mix_rate"); zeromem(&strdesc, sizeof(strdesc)); strdesc.mFormatID = kAudioFormatLinearPCM; diff --git a/drivers/pulseaudio/audio_driver_pulseaudio.cpp b/drivers/pulseaudio/audio_driver_pulseaudio.cpp index ce0b8ade95e..a6bc4f3b2c4 100644 --- a/drivers/pulseaudio/audio_driver_pulseaudio.cpp +++ b/drivers/pulseaudio/audio_driver_pulseaudio.cpp @@ -179,7 +179,7 @@ Error AudioDriverPulseAudio::init_device() { break; } - int latency = GLOBAL_DEF_RST("audio/output_latency", DEFAULT_OUTPUT_LATENCY); + int latency = GLOBAL_GET("audio/output_latency"); buffer_frames = closest_power_of_2(latency * mix_rate / 1000); pa_buffer_size = buffer_frames * pa_map.channels; @@ -237,7 +237,7 @@ Error AudioDriverPulseAudio::init() { thread_exited = false; exit_thread = false; - mix_rate = GLOBAL_DEF_RST("audio/mix_rate", DEFAULT_MIX_RATE); + mix_rate = GLOBAL_GET("audio/mix_rate"); pa_ml = pa_mainloop_new(); ERR_FAIL_COND_V(pa_ml == nullptr, ERR_CANT_OPEN); diff --git a/drivers/wasapi/audio_driver_wasapi.cpp b/drivers/wasapi/audio_driver_wasapi.cpp index 707e55cfcbd..cd1c08b717d 100644 --- a/drivers/wasapi/audio_driver_wasapi.cpp +++ b/drivers/wasapi/audio_driver_wasapi.cpp @@ -387,7 +387,7 @@ Error AudioDriverWASAPI::finish_capture_device() { } Error AudioDriverWASAPI::init() { - mix_rate = GLOBAL_DEF_RST("audio/mix_rate", DEFAULT_MIX_RATE); + mix_rate = GLOBAL_GET("audio/mix_rate"); Error err = init_render_device(); if (err != OK) { diff --git a/drivers/xaudio2/audio_driver_xaudio2.cpp b/drivers/xaudio2/audio_driver_xaudio2.cpp index df7d2488fd4..421cf6a8cf2 100644 --- a/drivers/xaudio2/audio_driver_xaudio2.cpp +++ b/drivers/xaudio2/audio_driver_xaudio2.cpp @@ -44,12 +44,12 @@ Error AudioDriverXAudio2::init() { pcm_open = false; samples_in = nullptr; - mix_rate = GLOBAL_DEF_RST("audio/mix_rate", DEFAULT_MIX_RATE); + mix_rate = GLOBAL_GET("audio/mix_rate"); // FIXME: speaker_mode seems unused in the Xaudio2 driver so far speaker_mode = SPEAKER_MODE_STEREO; channels = 2; - int latency = GLOBAL_DEF_RST("audio/output_latency", DEFAULT_OUTPUT_LATENCY); + int latency = GLOBAL_GET("audio/output_latency"); buffer_size = closest_power_of_2(latency * mix_rate / 1000); samples_in = memnew_arr(int32_t, buffer_size * channels); diff --git a/platform/android/audio_driver_jandroid.cpp b/platform/android/audio_driver_jandroid.cpp index 0ace7d03a19..09c981b3fa8 100644 --- a/platform/android/audio_driver_jandroid.cpp +++ b/platform/android/audio_driver_jandroid.cpp @@ -73,9 +73,9 @@ Error AudioDriverAndroid::init() { // __android_log_print(ANDROID_LOG_VERBOSE, "SDL", "SDL audio: opening device"); JNIEnv *env = ThreadAndroid::get_env(); - int mix_rate = GLOBAL_DEF_RST("audio/mix_rate", 44100); + int mix_rate = GLOBAL_GET("audio/mix_rate"); - int latency = GLOBAL_DEF_RST("audio/output_latency", 25); + int latency = GLOBAL_GET("audio/output_latency"); unsigned int buffer_size = next_power_of_2(latency * mix_rate / 1000); print_verbose("Audio buffer size: " + itos(buffer_size)); diff --git a/platform/haiku/audio_driver_media_kit.cpp b/platform/haiku/audio_driver_media_kit.cpp index 94c9e833681..2fbbeeb1764 100644 --- a/platform/haiku/audio_driver_media_kit.cpp +++ b/platform/haiku/audio_driver_media_kit.cpp @@ -39,11 +39,11 @@ int32_t *AudioDriverMediaKit::samples_in = nullptr; Error AudioDriverMediaKit::init() { active = false; - mix_rate = GLOBAL_DEF_RST("audio/mix_rate", DEFAULT_MIX_RATE); + mix_rate = GLOBAL_GET("audio/mix_rate"); speaker_mode = SPEAKER_MODE_STEREO; channels = 2; - int latency = GLOBAL_DEF_RST("audio/output_latency", DEFAULT_OUTPUT_LATENCY); + int latency = GLOBAL_GET("audio/output_latency"); buffer_size = next_power_of_2(latency * mix_rate / 1000); samples_in = memnew_arr(int32_t, buffer_size * channels); diff --git a/platform/javascript/audio_driver_javascript.cpp b/platform/javascript/audio_driver_javascript.cpp index 808260a3856..b52bd4ce60f 100644 --- a/platform/javascript/audio_driver_javascript.cpp +++ b/platform/javascript/audio_driver_javascript.cpp @@ -64,8 +64,8 @@ void AudioDriverJavaScript::process_capture(float sample) { } Error AudioDriverJavaScript::init() { - int mix_rate = GLOBAL_DEF_RST("audio/mix_rate", DEFAULT_MIX_RATE); - int latency = GLOBAL_DEF_RST("audio/output_latency", DEFAULT_OUTPUT_LATENCY); + int mix_rate = GLOBAL_GET("audio/mix_rate"); + int latency = GLOBAL_GET("audio/output_latency"); /* clang-format off */ _driver_id = EM_ASM_INT({ diff --git a/servers/audio/audio_driver_dummy.cpp b/servers/audio/audio_driver_dummy.cpp index 70d5ebbdedc..c539b582dab 100644 --- a/servers/audio/audio_driver_dummy.cpp +++ b/servers/audio/audio_driver_dummy.cpp @@ -39,11 +39,11 @@ Error AudioDriverDummy::init() { exit_thread = false; samples_in = nullptr; - mix_rate = DEFAULT_MIX_RATE; + mix_rate = GLOBAL_GET("audio/mix_rate"); speaker_mode = SPEAKER_MODE_STEREO; channels = 2; - int latency = GLOBAL_DEF_RST("audio/output_latency", DEFAULT_OUTPUT_LATENCY); + int latency = GLOBAL_GET("audio/output_latency"); buffer_frames = closest_power_of_2(latency * mix_rate / 1000); samples_in = memnew_arr(int32_t, buffer_frames * channels); diff --git a/servers/audio_server.cpp b/servers/audio_server.cpp index 7b1ac534b4d..09d2914e050 100644 --- a/servers/audio_server.cpp +++ b/servers/audio_server.cpp @@ -182,6 +182,9 @@ int AudioDriverManager::get_driver_count() { void AudioDriverManager::initialize(int p_driver) { GLOBAL_DEF_RST("audio/enable_audio_input", false); + GLOBAL_DEF_RST("audio/mix_rate", DEFAULT_MIX_RATE); + GLOBAL_DEF_RST("audio/output_latency", DEFAULT_OUTPUT_LATENCY); + int failed_driver = -1; // Check if there is a selected driver diff --git a/servers/audio_server.h b/servers/audio_server.h index 71b5ae5946b..80e244aacdb 100644 --- a/servers/audio_server.h +++ b/servers/audio_server.h @@ -80,9 +80,6 @@ public: SPEAKER_SURROUND_71, }; - static const int DEFAULT_MIX_RATE = 44100; - static const int DEFAULT_OUTPUT_LATENCY = 15; - static AudioDriver *get_singleton(); void set_singleton(); @@ -129,6 +126,9 @@ class AudioDriverManager { MAX_DRIVERS = 10 }; + static const int DEFAULT_MIX_RATE = 44100; + static const int DEFAULT_OUTPUT_LATENCY = 15; + static AudioDriver *drivers[MAX_DRIVERS]; static int driver_count;