Want to get a script on Linux to play a sound to get your attention, without embarrassing you if you’re in the middle of a call? I have found just the trick.

I have script that I use to rip CDs (technically illegal, but who cares; the law is an ass). It does two things:

  1. runs whipper to obtain an accurate rip, and
  2. plays a sound so that if I’m in another room I know it’s time to change the CD.

If the rip is successful, it plays a jaunty trumpet fanfare. If it fails, it plays a sad trombone.

Sometimes, it finishes when I’m in the middle of a work call, and I have to explain what the noise is. What if my computer could know when I’m using the microphone, and simply not do that?

Linux sound devices look like this in the proc filesystem:

/proc/asound/card{num}/pcm{num}{type}/sub{num}/...

card0 is the first card (and card1 is the second, and so on), pcm0p is the first playback (output) channel on the card, pcm0c is the first capture (input) channel on the card, and sub0 is the first PCM substream on the channel.

There are a few files within each substream directory, but the one that’s useful to us is status. When a substream isn’t in use, this contains just closed. When it is in use, however, we get a few pieces of information including that of the process that has it open:

owner_pid   : 88561

(Unsurprisingly, process 88561 corresponds to /usr/bin/pipewire.)

So all we need to do to check if a microphone is in use is to see if any of the status files beneath a pcm*c directory contains owner_pid.

With all that preamble, here’s a very basic shell function that plays a sound if and only if the microphone isn’t in use:

function fanfare () {
  grep -q owner_pid /proc/asound/card*/*c/sub*/status && return
  play "fanfare.mp3"
}

You can put this at the end of your test suite, hook it into your shell prompt to let you know that a long-running process has finished, or do whatever you wish, and your colleagues will be none the wiser.