1
0
mirror of https://github.com/godotengine/godot.git synced 2025-11-22 15:06:45 +00:00

Corrections to audio buffer size calculations

This commit is contained in:
Marcelo Fernandez
2017-08-29 16:47:44 -03:00
parent 06d7e36898
commit f231eadc9e
13 changed files with 129 additions and 60 deletions

View File

@@ -31,6 +31,7 @@
#ifdef ALSA_ENABLED
#include "os/os.h"
#include "project_settings.h"
#include <errno.h>
@@ -44,7 +45,7 @@ Error AudioDriverALSA::init() {
samples_in = NULL;
samples_out = NULL;
mix_rate = GLOBAL_DEF("audio/mix_rate", 44100);
mix_rate = GLOBAL_DEF("audio/mix_rate", DEFAULT_MIX_RATE);
speaker_mode = SPEAKER_MODE_STEREO;
channels = 2;
@@ -86,19 +87,25 @@ Error AudioDriverALSA::init() {
status = snd_pcm_hw_params_set_rate_near(pcm_handle, hwparams, &mix_rate, NULL);
CHECK_FAIL(status < 0);
int latency = GLOBAL_DEF("audio/output_latency", 25);
buffer_size = closest_power_of_2(latency * mix_rate / 1000);
// 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);
buffer_frames = closest_power_of_2(latency * mix_rate / 1000);
buffer_size = buffer_frames * periods;
period_size = buffer_frames;
// set buffer size from project settings
status = snd_pcm_hw_params_set_buffer_size_near(pcm_handle, hwparams, &buffer_size);
CHECK_FAIL(status < 0);
// make period size 1/8
period_size = buffer_size >> 3;
status = snd_pcm_hw_params_set_period_size_near(pcm_handle, hwparams, &period_size, NULL);
CHECK_FAIL(status < 0);
unsigned int periods = 2;
if (OS::get_singleton()->is_stdout_verbose()) {
print_line("audio buffer frames: " + itos(period_size) + " calculated latency: " + itos(period_size * 1000 / mix_rate) + "ms");
}
status = snd_pcm_hw_params_set_periods_near(pcm_handle, hwparams, &periods, NULL);
CHECK_FAIL(status < 0);