Skip to content

Commit

Permalink
Move common audio backend settings like buffer size and offsets to th…
Browse files Browse the repository at this point in the history
…e "general" settings stanza. Improve the resync code a little.
  • Loading branch information
mikebrady committed Jul 21, 2017
1 parent d810d43 commit b7864b4
Show file tree
Hide file tree
Showing 11 changed files with 153 additions and 365 deletions.
92 changes: 84 additions & 8 deletions audio.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
* OTHER DEALINGS IN THE SOFTWARE.
*/

#include "common.h"
#include "audio.h"
#include "config.h"
#include <stdio.h>
Expand Down Expand Up @@ -58,14 +59,11 @@ extern audio_output audio_stdout;
#endif

static audio_output *outputs[] = {
#ifdef CONFIG_SNDIO
&audio_sndio,
#endif
#ifdef CONFIG_ALSA
&audio_alsa,
#endif
#ifdef CONFIG_PULSE
&audio_pulse,
#ifdef CONFIG_SNDIO
&audio_sndio,
#endif
#ifdef CONFIG_PA
&audio_pa,
Expand All @@ -76,14 +74,17 @@ static audio_output *outputs[] = {
#ifdef CONFIG_SOUNDIO
&audio_soundio,
#endif
#ifdef CONFIG_DUMMY
&audio_dummy,
#endif
#ifdef CONFIG_PIPE
&audio_pipe,
#endif
#ifdef CONFIG_STDOUT
&audio_stdout,
#endif
#ifdef CONFIG_DUMMY
&audio_dummy,
#endif
#ifdef CONFIG_PULSE
&audio_pulse,
#endif
NULL};

Expand Down Expand Up @@ -114,3 +115,78 @@ void audio_ls_outputs(void) {
(*out)->help();
}
}

void parse_general_audio_options(void) {
/* this must be called after the output device has been initialised, so that the default values are set before any options are chosen */
int value;
double dvalue;
if (config.cfg != NULL) {

/* Get the desired buffer size setting (deprecated). */
if (config_lookup_int(config.cfg, "general.audio_backend_buffer_desired_length", &value)) {
if ((value < 0) || (value > 66150)) {
inform("The setting general.audio_backend_buffer_desired_length is deprecated. "
"Use alsa.audio_backend_buffer_desired_length_in_seconds instead.");
die("Invalid audio_backend_buffer_desired_length value: \"%d\". It "
"should be between 0 and "
"66150, default is %d",
value,(int)(config.audio_backend_buffer_desired_length*44100));
} else {
inform("The setting general.audio_backend_buffer_desired_length is deprecated. "
"Use general.audio_backend_buffer_desired_length_in_seconds instead.");
config.audio_backend_buffer_desired_length = 1.0 * value / 44100;
}
}

/* Get the desired buffer size setting in seconds. */
if (config_lookup_float(config.cfg, "general.audio_backend_buffer_desired_length_in_seconds",
&dvalue)) {
if ((dvalue < 0) || (dvalue > 1.5)) {
die("Invalid audio_backend_buffer_desired_length_in_seconds value: \"%f\". It "
"should be between 0 and "
"1.5, default is %.3f seconds",
dvalue,config.audio_backend_buffer_desired_length);
} else {
config.audio_backend_buffer_desired_length = dvalue;
}
}

/* Get the latency offset (deprecated). */
if (config_lookup_int(config.cfg, "general.audio_backend_latency_offset", &value)) {
if ((value < -66150) || (value > 66150)) {
inform("The setting general.audio_backend_latency_offset is deprecated. "
"Use general.audio_backend_latency_offset_in_seconds instead.");
die("Invalid audio_backend_latency_offset value: \"%d\". It "
"should be between -66150 and +66150, default is 0",
value);
} else {
inform("The setting general.audio_backend_latency_offset is deprecated. "
"Use general.audio_backend_latency_offset_in_seconds instead.");
config.audio_backend_latency_offset = 1.0 * value / 44100;
}
}

/* Get the latency offset in seconds. */
if (config_lookup_float(config.cfg, "general.audio_backend_latency_offset_in_seconds", &dvalue)) {
if ((dvalue < -1.0) || (dvalue > 1.5)) {
die("Invalid audio_backend_latency_offset_in_seconds \"%f\". It "
"should be between -1.0 and +1.5, default is 0 seconds",
dvalue);
} else {
config.audio_backend_latency_offset = dvalue;
}
}

/* Get the desired length of the silent lead-in. */
if (config_lookup_float(config.cfg, "general.audio_backend_silent_lead_in_time",
&dvalue)) {
if ((dvalue < 0.05) || (dvalue > 4)) {
die("Invalid audio_backend_silent_lead_in_time \"%f\". It "
"must be between 0.050 and 4.0 seconds. Omit setting to use the default value, which is approximately the latency specified by the source (typically 2 seconds). A value greater than the latency is ignored.",
dvalue);
} else {
config.audio_backend_silent_lead_in_time = dvalue;
}
}
}
}
1 change: 1 addition & 0 deletions audio.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,6 @@ typedef struct {

audio_output *audio_get_output(char *name);
void audio_ls_outputs(void);
void parse_general_audio_options(void);

#endif //_AUDIO_H
78 changes: 5 additions & 73 deletions audio_alsa.c
Original file line number Diff line number Diff line change
Expand Up @@ -158,90 +158,22 @@ static int init(int argc, char **argv) {
int value;
double dvalue;

// set up default values first
set_period_size_request = 0;
set_buffer_size_request = 0;
config.alsa_use_playback_switch_for_mute = 1;

config.audio_backend_latency_offset = 0;
config.audio_backend_buffer_desired_length = 0.15;
config.audio_backend_buffer_desired_length = 0.15;

// get settings from settings file first, allow them to be overridden by
// command line options

// do the "general" audio options. Note, these options are in the "general" stanza!
parse_general_audio_options();

if (config.cfg != NULL) {

/* Get the desired buffer size setting. */
if (config_lookup_int(config.cfg, "alsa.audio_backend_buffer_desired_length", &value)) {
if ((value < 0) || (value > 66150)) {
inform("The setting alsa.audio_backend_buffer_desired_length is deprecated. "
"Use alsa.audio_backend_buffer_desired_length_in_seconds instead.");
pthread_mutex_unlock(&alsa_mutex);
die("Invalid alsa audio backend buffer desired length \"%d\". It "
"should be between 0 and "
"66150, default is 6615",
value);
} else {
inform("The setting alsa.audio_backend_buffer_desired_length is deprecated. "
"Use alsa.audio_backend_buffer_desired_length_in_seconds instead.");
config.audio_backend_buffer_desired_length = 1.0 * value / 44100;
}
}

/* Get the desired length of the period of silence before the audio starts. */
if (config_lookup_float(config.cfg, "alsa.audio_backend_silent_lead_in_time",
&dvalue)) {
if ((dvalue < 0.05) || (dvalue > 4)) {
pthread_mutex_unlock(&alsa_mutex);
die("Invalid alsa audio_backend_silent_lead_in_time \"%f\". It "
"must be between 0.050 and 4.0 seconds. Omit setting to use the default value, which is approximately the latency specified by the source (typically 2 seconds). A value greater than the latency is ignored.",
dvalue);
} else {
config.audio_backend_silent_lead_in_time = dvalue;
}
}

/* Get the desired buffer size setting. */
if (config_lookup_float(config.cfg, "alsa.audio_backend_buffer_desired_length_in_seconds",
&dvalue)) {
if ((dvalue < 0) || (dvalue > 1.5)) {
pthread_mutex_unlock(&alsa_mutex);
die("Invalid alsa audio backend buffer desired time \"%f\". It "
"should be between 0 and "
"1.5, default is 0.15 seconds",
dvalue);
} else {
config.audio_backend_buffer_desired_length = dvalue;
}
}

/* Get the latency offset. */
if (config_lookup_int(config.cfg, "alsa.audio_backend_latency_offset", &value)) {
if ((value < -66150) || (value > 66150)) {
inform("The setting alsa.audio_backend_latency_offset is deprecated. "
"Use alsa.audio_backend_latency_offset_in_seconds instead.");
pthread_mutex_unlock(&alsa_mutex);
die("Invalid alsa audio backend buffer latency offset \"%d\". It "
"should be between -66150 and +66150, default is 0",
value);
} else {
inform("The setting alsa.audio_backend_latency_offset is deprecated. "
"Use alsa.audio_backend_latency_offset_in_seconds instead.");
config.audio_backend_latency_offset = 1.0 * value / 44100;
}
}

/* Get the latency offset. */
if (config_lookup_float(config.cfg, "alsa.audio_backend_latency_offset_in_seconds", &dvalue)) {
if ((dvalue < -1.0) || (dvalue > 1.5)) {
pthread_mutex_unlock(&alsa_mutex);
die("Invalid alsa audio backend buffer latency offset time \"%f\". It "
"should be between -1.0 and +1.5, default is 0 seconds",
dvalue);
} else {
config.audio_backend_latency_offset = dvalue;
}
}

/* Get the Output Device Name. */
if (config_lookup_string(config.cfg, "alsa.output_device", &str)) {
alsa_out_dev = (char *)str;
Expand Down
78 changes: 7 additions & 71 deletions audio_ao.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,81 +47,17 @@ static int init(int argc, char **argv) {
ao_initialize();
int driver = ao_default_driver_id();
ao_option *ao_opts = NULL;

// set up default values first

config.audio_backend_buffer_desired_length = 1.0;
config.audio_backend_latency_offset = 0;

// get settings from settings file first, allow them to be overridden by command line options

if (config.cfg != NULL) {
/* Get the desired length of the period of silence before the audio starts. */
if (config_lookup_float(config.cfg, "ao.audio_backend_silent_lead_in_time",
&dvalue)) {
if ((dvalue < 0.05) || (dvalue > 4)) {
pthread_mutex_unlock(&alsa_mutex);
die("Invalid ao audio_backend_silent_lead_in_time \"%f\". It "
"must be between 0.050 and 4.0 seconds. Omit setting to use the default value, which is approximately the latency specified by the source (typically 2 seconds). A value greater than the latency is ignored.",
dvalue);
} else {
config.audio_backend_silent_lead_in_time = dvalue;
}
}

/* Get the desired buffer size setting. */
if (config_lookup_int(config.cfg, "ao.audio_backend_buffer_desired_length", &value)) {
if ((value < 0) || (value > 66150)) {
inform("The setting ao.audio_backend_buffer_desired_length is deprecated. "
"Use ao.audio_backend_buffer_desired_length_in_seconds instead.");
die("Invalid ao audio backend buffer desired length \"%d\". It "
"should be between 0 and "
"66150, default is 6615",
value);
} else {
inform("The setting ao.audio_backend_buffer_desired_length is deprecated. "
"Use ao.audio_backend_buffer_desired_length_in_seconds instead.");
config.audio_backend_buffer_desired_length = 1.0 * value / 44100;
}
}

/* Get the desired buffer size setting. */
if (config_lookup_float(config.cfg, "ao.audio_backend_buffer_desired_length_in_seconds",
&dvalue)) {
if ((dvalue < 0) || (dvalue > 1.5)) {
die("Invalid ao audio backend buffer desired time \"%f\". It "
"should be between 0 and "
"1.5, default is 0.15 seconds",
dvalue);
} else {
config.audio_backend_buffer_desired_length = dvalue;
}
}

/* Get the latency offset. */
if (config_lookup_int(config.cfg, "ao.audio_backend_latency_offset", &value)) {
if ((value < -66150) || (value > 66150)) {
inform("The setting ao.audio_backend_latency_offset is deprecated. "
"Use ao.audio_backend_latency_offset_in_seconds instead.");
die("Invalid ao audio backend buffer latency offset \"%d\". It "
"should be between -66150 and +66150, default is 0",
value);
} else {
inform("The setting ao.audio_backend_latency_offset is deprecated. "
"Use ao.audio_backend_latency_offset_in_seconds instead.");
config.audio_backend_latency_offset = 1.0 * value / 44100;
}
}

/* Get the latency offset. */
if (config_lookup_float(config.cfg, "ao.audio_backend_latency_offset_in_seconds", &dvalue)) {
if ((dvalue < -1.0) || (dvalue > 1.5)) {
die("Invalid ao audio backend buffer latency offset time \"%f\". It "
"should be between -1.0 and +1.5, default is 0 seconds",
dvalue);
} else {
config.audio_backend_latency_offset = dvalue;
}
}
}
// get settings from settings file first, allow them to be overridden by
// command line options

// do the "general" audio options. Note, these options are in the "general" stanza!
parse_general_audio_options();

optind = 1; // optind=0 is equivalent to optind=1 plus special behaviour
argv--; // so we shift the arguments to satisfy getopt()
Expand Down
47 changes: 7 additions & 40 deletions audio_pa.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,51 +42,18 @@ void stream_write_cb(pa_stream *stream, size_t requested_bytes, void *userdata);

static int init(int argc, char **argv) {

// default values
// set up default values first
config.audio_backend_buffer_desired_length = 0.35;
config.audio_backend_latency_offset = 0;

// get settings from settings file first, allow them to be overridden by
// command line options

// get settings from settings file

// do the "general" audio options. Note, these options are in the "general" stanza!
parse_general_audio_options();

// now the specific options
if (config.cfg != NULL) {
const char *str;
double dvalue;
/* Get the desired length of the period of silence before the audio starts. */
if (config_lookup_float(config.cfg, "pa.audio_backend_silent_lead_in_time",
&dvalue)) {
if ((dvalue < 0.05) || (dvalue > 4)) {
die("Invalid pa audio_backend_silent_lead_in_time \"%f\". It "
"must be between 0.050 and 4.0 seconds. Omit setting to use the default value, which is approximately the latency specified by the source (typically 2 seconds). A value greater than the latency is ignored.",
dvalue);
} else {
config.audio_backend_silent_lead_in_time = dvalue;
}
}

/* Get the desired buffer size setting. */
if (config_lookup_float(config.cfg, "pa.audio_backend_buffer_desired_length_in_seconds",
&dvalue)) {
if ((dvalue < 0) || (dvalue > 1.5)) {
die("Invalid pa audio_backend_buffer_desired_length_in_seconds \"%f\". It "
"should be between 0 and "
"1.5, default is 0.35 seconds",
dvalue);
} else {
config.audio_backend_buffer_desired_length = dvalue;
}
}

/* Get the latency offset. */
if (config_lookup_float(config.cfg, "pa.audio_backend_latency_offset_in_seconds", &dvalue)) {
if ((dvalue < -1.0) || (dvalue > 1.5)) {
die("Invalid pa audio_backend_latency_offset_in_seconds \"%f\". It "
"should be between -1.0 and +1.5, default is 0 seconds",
dvalue);
} else {
config.audio_backend_latency_offset = dvalue;
}
}

/* Get the Application Name. */
if (config_lookup_string(config.cfg, "pa.application_name", &str)) {
Expand Down
Loading

0 comments on commit b7864b4

Please sign in to comment.