From e22bbb58125f549076e0a593faccadf6919bacd4 Mon Sep 17 00:00:00 2001 From: karl Date: Sun, 10 Dec 2023 10:17:55 +0100 Subject: [PATCH] make audio rate and format configurable also for pipewire --- cava.c | 8 ++++---- config.c | 5 +++-- config.h | 2 +- example_files/config | 5 +++-- input/pipewire.c | 19 ++++++++++++++++++- 5 files changed, 29 insertions(+), 10 deletions(-) diff --git a/cava.c b/cava.c index 6c5658f..3a16e85 100644 --- a/cava.c +++ b/cava.c @@ -383,8 +383,8 @@ as of 0.4.0 all options are specified in config file, see in '/home/username/.co #endif case INPUT_FIFO: - audio.rate = p.fifoSample; - audio.format = p.fifoSampleBits; + audio.rate = p.samplerate; + audio.format = p.samplebits; thr_id = pthread_create(&p_thread, NULL, input_fifo, (void *)&audio); break; #ifdef PULSE @@ -417,8 +417,8 @@ as of 0.4.0 all options are specified in config file, see in '/home/username/.co #endif #ifdef PIPEWIRE case INPUT_PIPEWIRE: - audio.format = 16; - audio.rate = 44100; + audio.format = p.samplebits; + audio.rate = p.samplerate; thr_id = pthread_create(&p_thread, NULL, input_pipewire, (void *)&audio); break; #endif diff --git a/config.c b/config.c index 8446e42..90ace6c 100644 --- a/config.c +++ b/config.c @@ -676,6 +676,9 @@ bool load_config(char configPath[PATH_MAX], struct config_params *p, bool colors free(p->audio_source); + p->samplerate = iniparser_getint(ini, "input:sample_rate", 44100); + p->samplebits = iniparser_getint(ini, "input:sample_bits", 16); + enum input_method default_input = INPUT_FIFO; for (size_t i = 0; i < ARRAY_SIZE(default_methods); i++) { enum input_method method = default_methods[i]; @@ -694,8 +697,6 @@ bool load_config(char configPath[PATH_MAX], struct config_params *p, bool colors #endif case INPUT_FIFO: p->audio_source = strdup(iniparser_getstring(ini, "input:source", "/tmp/mpd.fifo")); - p->fifoSample = iniparser_getint(ini, "input:sample_rate", 44100); - p->fifoSampleBits = iniparser_getint(ini, "input:sample_bits", 16); break; #ifdef PULSE case INPUT_PULSE: diff --git a/config.h b/config.h index 5bbb700..aa8af23 100644 --- a/config.h +++ b/config.h @@ -97,7 +97,7 @@ struct config_params { enum orientation orientation; int userEQ_keys, userEQ_enabled, col, bgcol, autobars, stereo, raw_format, ascii_range, bit_format, gradient, gradient_count, fixedbars, framerate, bar_width, bar_spacing, - bar_height, autosens, overshoot, waves, fifoSample, fifoSampleBits, sleep_timer, sdl_width, + bar_height, autosens, overshoot, waves, samplerate, samplebits, sleep_timer, sdl_width, sdl_height, sdl_x, sdl_y, sdl_full_screen, draw_and_quit, zero_test, non_zero_test, reverse, sync_updates, continuous_rendering, disable_blanking; }; diff --git a/example_files/config b/example_files/config index ecd3b97..99fa684 100644 --- a/example_files/config +++ b/example_files/config @@ -80,8 +80,6 @@ ; method = fifo ; source = /tmp/mpd.fifo -; sample_rate = 44100 -; sample_bits = 16 ; method = shmem ; source = /squeezelite-AA:BB:CC:DD:EE:FF @@ -89,6 +87,9 @@ ; method = portaudio ; source = auto +; sample_rate = 44100 +; sample_bits = 16 + [output] diff --git a/input/pipewire.c b/input/pipewire.c index 80919d7..7933367 100644 --- a/input/pipewire.c +++ b/input/pipewire.c @@ -92,9 +92,26 @@ void *input_pipewire(void *audiodata) { data.stream = pw_stream_new_simple(pw_main_loop_get_loop(data.loop), "cava", props, &stream_events, &data); + enum spa_audio_format audio_format = SPA_AUDIO_FORMAT_S16; + + switch (data.cava_audio->format) { + case 8: + audio_format = SPA_AUDIO_FORMAT_S8; + break; + case 16: + audio_format = SPA_AUDIO_FORMAT_S16; + break; + case 24: + audio_format = SPA_AUDIO_FORMAT_S24; + break; + case 32: + audio_format = SPA_AUDIO_FORMAT_S32; + break; + }; + params[0] = spa_format_audio_raw_build( &b, SPA_PARAM_EnumFormat, - &SPA_AUDIO_INFO_RAW_INIT(.format = SPA_AUDIO_FORMAT_S16, .rate = data.cava_audio->rate)); + &SPA_AUDIO_INFO_RAW_INIT(.format = audio_format, .rate = data.cava_audio->rate)); pw_stream_connect(data.stream, PW_DIRECTION_INPUT, PW_ID_ANY, PW_STREAM_FLAG_AUTOCONNECT | PW_STREAM_FLAG_MAP_BUFFERS |