Skip to content

Commit

Permalink
Add camilla_exit_cmd to allow a program to run just before the
Browse files Browse the repository at this point in the history
camilladsp process is closed.  Mainly for storing gain and mute but
can be used for whatever.
  • Loading branch information
scripple committed Mar 9, 2022
1 parent 45c0254 commit 1a1b0a3
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 8 deletions.
28 changes: 21 additions & 7 deletions asound.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
}
22 changes: 21 additions & 1 deletion libasound_module_pcm_cdsp.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 1a1b0a3

Please sign in to comment.