[alsa-devel] Toggle GPIO if ALSA playbacks begins or ends

Jörg Krause joerg.krause at embedded.rocks
Fri Aug 26 14:59:49 CEST 2016


On Do, 2016-08-25 at 20:27 +0200, Jörg Krause wrote:
> Hi,
> 
> I am looking for a way to toggle a GPIO if ALSA is about to play or
> has
> stopped to turn an external amplifier on/off.
> 
> Can it be done using libasound or can I use a plugin or write one?

After having a long night digging through ALSA's sourced I got it
working writing a PCM hook library. Basically, it does the following:

static int snd_pcm_hook_gpio_hook_close(snd_pcm_hook_t *hook)
{
	int fd;

	/*
	 * Set GPIO LOW
	 */

	return 0;
}

int _snd_pcm_hook_gpio_hook_install(snd_pcm_t *pcm, snd_config_t *conf)
{
	int fd;
	int err;

	snd_pcm_hook_t *h_close = NULL;

	err = snd_pcm_hook_add(&h_close, pcm, SND_PCM_HOOK_TYPE_CLOSE,
			       snd_pcm_hook_gpio_hook_close, NULL);
	if (err < 0)
		goto _err;

	/* Init and set GPIO HIGH
         */

	return 0;

_err:
	if (h_close)
		snd_pcm_hook_remove(h_close);

	return err;
}
SND_DLSYM_BUILD_VERSION(_snd_pcm_hook_gpio_hook_install,
SND_CONFIG_DLSYM_VERSION_HOOK);

For reference my asound.conf:

pcm.softvol {
    type softvol
    slave {
        pcm "gpio_hook"
    }
    control {
        name "Master"
        card 0
    }
}
pcm_hook_type.gpio_hook {
    lib "libasound_gpio_hook.so"
}
pcm.softvol_hook {
    type hooks
    slave {
        pcm "hw:0"
    }
    hooks.0 {
        type "gpio_hook"
    }
}

Jörg


More information about the Alsa-devel mailing list