Skip to content

Commit

Permalink
make audio rate and format configurable also for pipewire
Browse files Browse the repository at this point in the history
  • Loading branch information
karlstav committed Dec 10, 2023
1 parent c5d36aa commit e22bbb5
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 10 deletions.
8 changes: 4 additions & 4 deletions cava.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions config.c
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion config.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand Down
5 changes: 3 additions & 2 deletions example_files/config
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,16 @@

; method = fifo
; source = /tmp/mpd.fifo
; sample_rate = 44100
; sample_bits = 16

; method = shmem
; source = /squeezelite-AA:BB:CC:DD:EE:FF

; method = portaudio
; source = auto

; sample_rate = 44100
; sample_bits = 16


[output]

Expand Down
19 changes: 18 additions & 1 deletion input/pipewire.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down

0 comments on commit e22bbb5

Please sign in to comment.