ALSA pcm hw_params hook (Loopback Notify Kludge)

Status
This old topic is closed. If you want to reopen this topic, contact a moderator using the "Report Post" button.
In case it's useful to anyone I thought I'd mention an alsa pcm hook I wrote to essentially do what the loopback pcm_notify event would do if it actually worked.

Basically you wrap the loopback device in this hook in an asoundrc file and the hook will execute the command of your choice when the hw_params are changed on the pcm device. This allows the capture end to release and reattach to the pcm device at the right times such that the playback end is free to set the hw_params of its choosing.

I've tested it with camilladsp using unmodified versions of mpd and squeezelite on a raspberry pi 4 running moOde audio. I switch between mpd and squeezelite using the standard moOde / LMS GUI options. In terms of audio files I have switched between 44100 and 48000 Hz sample rates and S16_LE and S24_LE formats.

In my setup it calls a pair of python scripts. One tells camilladsp to stop and close the loopback device. The other tells camilladsp to open the loopback device with the right sample rate and format and load the matching sample rate FIR filter for DRC.

Maybe this could also be helpful for people using brutefir and possibly other DSP programs I'm not aware of who don't wish to use an alsa plug device to do resampling and format conversion.

The hook is available on github.

GitHub - scripple/alsa_hook_hwparams: Run commands when an an ALSA pcm device is about to have it hw_params set or released. Useful for loopback.

The python scripts are not part of the package. They're too ugly to share.

I have no intention of adding any additional functionality to this hook. As I said just putting it out there in case it's useful to anyone.
 
I still cannot make the ALSA hook even touch a file in /tmp. It is not triggered obviously. I saw this SuperPlayer... The ugly hacked squeezelite samplerate auto switching machine, which might be helpful when I can make it do something, just anything.

I downloaded your program from GitHub - scripple/alsa_hook_hwparams: Run commands when an an ALSA pcm device is about to have it hw_params set or released. Useful for loopback., unzipped, cd to unpacked directory, run make and sudo make install. No errors.

My /etc/asound.conf looks like below before adding content from SuperPlayer... The ugly hacked squeezelite samplerate auto switching machine.

Code:
# Loopback device 0
pcm.mpd {
    type hw
    card "Loopback"
    device 0
    format S32_LE
    channels 2
}

pcm.squeeze {
    type hw
    card "Loopback"
    device 0
    format S32_LE
    channels 2
}

# Loopback device 1
pcm.camilla_in {
    type hw
    card "Loopback"
    device 1
    format S32_LE
    channels 2
}

# "sound_out" is the "real" hardware card (usbdac, soundcard etc...)
    pcm.sound_out {
    type hw
    card 0
    device 0
}

ctl.sound_out {
    type hw
    card 0
}
I've tested many variants of your configuration, which look like below in its original form. I added your configuration below the configuration above.

Code:
pcm_hook_type.hwparams {
  install "_snd_pcm_hook_hwparams_install"
  lib "libasound_module_pcm_hook_hwparams.so"
}

pcm.camilladsp {
  type hooks
  slave.pcm {
    type hw
    card 0
  }
  hooks.0 {
    type "hwparams"
    hook_args {
      opencmd "/usr/local/bin/startcamilla"
      closecmd "/usr/local/bin/stopcamilla"
    }
  }
}
How to adapt your configuration for the hook, to get at least a working asound.conf without spamming syslog with error messages?
 
I pulled the latest version.

I can get it to work with:
Code:
aplay -D hwparams -r 44100 -c 2 -f S32_LE < /dev/zero
I can get it to work with MPD as well, but not while MPD plays music.

May I ask:

  • What your audio_output settings for alsa looks like in mpd.conf?
  • What your full asound.conf looks like?
 
/etc/asound.conf
Code:
pcm_hook_type.hwparams {
  install "_snd_pcm_hook_hwparams_install"
  lib "libasound_module_pcm_hook_hwparams.so"
}

pcm.camilladsp {
  type hooks
  slave.pcm {
    type hw
    card 0
  }
  hooks.0 {
    type "hwparams"
    hook_args {
      opencmd "/usr/local/bin/startcamilla"
      closecmd "/usr/local/bin/stopcamilla"
    }
  }
}

ctl.camilladsp {
  type hw
  card D10s
}

alsa in mpd.conf
Code:
audio_output {
   type "alsa"
   name "ALSA default"
   device "camilladsp"
   mixer_type "hardware"
   mixer_control "D10s "
   mixer_device "hw:3"
   mixer_index "0"
   dop "no"
}
 
All alsa configs (/usr/share/..., /etc/asound.conf, ~/.asoundrc) are read during opening the device (snd_pcm_open). The behavior would mean MPD opens the device at start and does not close it between tracks, which has been discussed here recently as a common behavior.
 
mpd does not close it between tracks of the same sample rate/format but it does close it when you click stop/pause. The odd thing about mpd is that it still doesn't handle the change in /etc/asound.conf even after a the stop/resume cycle. But the device shows up as closed in /proc/asound when mpd is not playing.
 
Status
This old topic is closed. If you want to reopen this topic, contact a moderator using the "Report Post" button.