Skip to contentSkip to conversation

Toggle GPIO if ALSA playbacks begins or ends

DISCUSSION Jörg Krause 1 participant 2 messages Aug 25, 2016 – Aug 26, 2016

lore
Share & tools
lore
#1 lore raw
Quoted 7 lines · Jörg Krause

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
_______________________________________________
Alsa-devel mailing list
[email protected]
http://mailman.alsa-project.org/mailman/listinfo/alsa-devel