Check if the sound card is in use

 

Check if the sound card is in use

You might run into problems that you can’t play back or record music. In many cases this is caused by another process already using the sound card.

Stepo 1: Is there already something using the sound card?

To see if there is some process using the sound card, you can run the following snippet

for i in /proc/asound/card?/pcm*/sub?/hw_params; \
 do echo $i; cat $i; \
done

This will show you the state of all sound card devices. The pcm?p devices are for playback, the pcm?c devices for capturing.

The output might look like this:

for i in /proc/asound/card?/pcm*/sub?/hw_params; \ 
do echo $i; cat $i; \
done
/proc/asound/card0/pcm0p/sub0/hw_params
access: MMAP_INTERLEAVED
format: S32_LE
subformat: STD
channels: 2
rate: 48000 (48000/1)
period_size: 2048
buffer_size: 16384

You see here that there is a process that has selected 48kHz sample rate and 32bit/sample.

Note that playback and capturing have to use exactly the same parameters as they share the same hardware interface.
E.g. If you see a process is playing music at 48kHz/32bit, you can only record music at the same sample rate and bit depth.

Step 2: What process is using it?

If you found that there is something using the sound card (you might not hear something), next step is to identify the process that’s using it.
The easiest way is the lsof utility:

 lsof /dev/snd/pcm*

This will show you for all sound devices if there is a process using it, like this:

lsof /dev/snd/pcm*
COMMAND   PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
play    11333 root  mem    CHR 116,16          1797 /dev/snd/pcmC0D0p
play    11333 root    4u   CHR 116,16      0t0 1797 /dev/snd/pcmC0D0p

This example shows a process named play that’s using the sound card. You can now decide if this is fine or this process should not run. In this case you can kill it:

kill -KILL 11333
lsof /dev/snd/pcm*

The sounnd card has been release as the process was killed.

Note that the system might restart some processes automatically. In this case, you have to figure out what script is doing this and stop it.

Last updated: September 5, 2020