From 1a1b0a3e452f87372881ffaa9391a11d0ff6d541 Mon Sep 17 00:00:00 2001 From: scripple Date: Tue, 8 Mar 2022 18:21:12 -0700 Subject: [PATCH] Add camilla_exit_cmd to allow a program to run just before the camilladsp process is closed. Mainly for storing gain and mute but can be used for whatever. --- asound.conf | 28 +++++++++++++++++++++------- libasound_module_pcm_cdsp.c | 22 +++++++++++++++++++++- 2 files changed, 42 insertions(+), 8 deletions(-) diff --git a/asound.conf b/asound.conf index 83414f4..eda8538 100644 --- a/asound.conf +++ b/asound.conf @@ -167,6 +167,21 @@ pcm.camilladsp { # raspberry pi to turn on your amp for example. start_cmd "/path/to/start_command" + # An option not directly related to camilladsp. A command to run when + # the CamillaDSP process is closed. (Meaning when a playback program + # closes the audio device or changes the HW params.) Use it for + # whatever you want. A good example is storing the gain and mute + # settings of CamillaDSP for use with the vol_file option below. + # + # Note this command is called just before the CamillaDSP process is + # closed. That is not necessarily when you are done listening to music. + # So unlike the start_cmd this might not work well as a signal to turn + # off your amp. Also note that CamillaDSP is still expecting audio + # while this command is running so it may cause a harmless warning + # about a buffer underrun to be emitted by CamillaDSP if you have + # CamillaDSP's log level set sufficiently high. + camilla_exit_cmd "/path/to/camilla_exit_command" + # Volume Parameter Passing Option # This options allows the plugin to pass gain and mute options on the # command line to CamillaDSP in case one uses CamillaDSP's Volume or @@ -183,13 +198,12 @@ pcm.camilladsp { # integer >= 0. If > 0 the -m (mute) option is passed to CamillaDSP. # If == 0 the -m is omitted. # - # To be useful another means of updating this file will have to exist - # such as a program that polls CamillaDSP's websocket for volume and - # mute values and writes them to this file. - # # If the file is missing or improperly formatted the plugin will # thrown an error during HW parameter setup. - # As this requires extra software to be useful it is commented out - # by default in this example asound.conf. - #vol_file "/path/to/volume_file_with_gain_and_mute_values" + # + # To be useful another means of updating this file will have to exist. + # One possibility is a program called by the exit_cmd option that + # queries CamillaDSP's websocket for volume and mute values and writes + # them to this file. + vol_file "/path/to/volume_file_with_gain_and_mute_values" } diff --git a/libasound_module_pcm_cdsp.c b/libasound_module_pcm_cdsp.c index 9c42dd4..a9fa6d4 100644 --- a/libasound_module_pcm_cdsp.c +++ b/libasound_module_pcm_cdsp.c @@ -208,6 +208,9 @@ typedef struct { bool first_revent; // A command to run at init - turn on audio system for example char *start_cmd; + // A command to run just before the CamillaDSP process is ended + // A good time to update the gain and mute values for example + char *camilla_exit_cmd; } cdsp_t; #if SND_LIB_VERSION < 0x010106 @@ -762,6 +765,8 @@ static void free_cdsp(cdsp_t **pcm) { free((void *)(*pcm)->ext_samp_token); if((*pcm)->start_cmd) free((void *)(*pcm)->start_cmd); + if((*pcm)->camilla_exit_cmd) + free((void *)(*pcm)->camilla_exit_cmd); pthread_mutex_destroy(&(*pcm)->mutex); pthread_cond_destroy(&(*pcm)->pause_cond); free((void *)*pcm); @@ -809,6 +814,15 @@ static int cdsp_hw_params(snd_pcm_ioplug_t *io, snd_pcm_hw_params_t *params __at static int cdsp_hw_free(snd_pcm_ioplug_t *io) { cdsp_t *pcm = io->private_data; debug("Freeing HW\n"); + int err = 0; + if(pcm->camilla_exit_cmd) { + debug("Calling camilla_exit_cmd: %s\n", pcm->camilla_exit_cmd); + // Call the camilla_exit_cmd + err = system(pcm->camilla_exit_cmd); + if(err != 0) { + SNDERR("Error executing camilla_exit_cmd %s\n", pcm->camilla_exit_cmd); + } + } debug("Stopping Camilla\n"); if (close_transport(pcm) == -1) return -errno; @@ -818,7 +832,8 @@ static int cdsp_hw_free(snd_pcm_ioplug_t *io) { waitpid(pcm->cpid, NULL, 0); pcm->cpid = -1; } - return 0; + if(err > 0) return -err; + return err; } // A check that get_boundary still works @@ -1307,6 +1322,11 @@ SND_PCM_PLUGIN_DEFINE_FUNC(cdsp) { if((err = alloc_copy_string(&pcm->start_cmd, temp)) < 0) goto _err; continue; } + if(strcmp(id, "camilla_exit_cmd") == 0) { + if((err = snd_config_get_string(n, &temp)) < 0) goto _err; + if((err = alloc_copy_string(&pcm->camilla_exit_cmd, temp)) < 0) goto _err; + continue; + } err = -EINVAL; goto _err; }