How To Detect That Audio Is Currently Being Output In Linux And Use It To Call A Program

By admin ·
1

Create an audio output monitor script

Connect to your Pi (or other Linux machine) and create the following file. The installation directory and filenames will vary based on your project, so be sure to update them however you’d like. The naming here is based on my driving my Furby’s DC motor while sound was being output.

sudo nano ~/furby/output-monitor.sh

Enter the following, save and exit:

#!/bin/bash DIR='/proc/asound/card1/pcm0p/sub0/status' CMD='python /home/pi/furby/furby.py' content='' while true do new_content=`cat $DIR` if [[ "$content" != "$new_content" ]]; then content=$new_content $CMD fi sleep 0.25 done

Change DIR above to point to the status file for your active audio device — I’m using a USB sound card, and this is the path for its status file.

To figure out which status file is in use for your audio device, simply play an audio sample:

speaker-test -t wav -c 6

.. and then view each of the status files and see which one contains the string “state: RUNNING”. There should be 4 status files total you’ll need to check, and they follow the pattern:

/proc/asound/card*/pcm0p/sub*/status

You can view each file as such. For example:

cat /proc/asound/card1/pcm0p/sub0/status

I found when using the Pimoroni Speaker pHAT that it used card0 (/proc/asound/card0/pcm0p/sub0/status), but your mileage may vary.

Change CMD above to point to the command you’d like to run when sound is detected (in my case, I’m executing a python script, furby.py, with the aforementioned file path).

2

Create a script to run as a daemon

This script will start the audio monitor script. Create the file:

sudo nano /etc/init.d/go-furby-go.sh

Paste the following, save, and exit:

#!/bin/bash # /etc/init.d/go-furby-go.sh case "$1" in start) echo "Starting go-furby-go.sh" /home/pi/furby/output-monitor.sh ;; stop) echo "Stopping go-furby-go.sh" killall output-monitor.sh ;; *) echo "Usage: /etc/init.d/go-furby-go.sh {start|stop}" exit 1 ;; esac exit 0
3

Make everything executable

Make both these scripts executable:

sudo chmod +x /etc/init.d/go-furby-go.sh sudo chmod +x /home/pi/furby/output-monitor.sh
4

Make sure everything runs at boot

To make sure our shell script runs at boot:

sudo update-rc.d go-furby-go.sh defaults
5

Reboot your Pi to test!

Reboot your Pi:

sudo reboot

Play a sample audio file and you should see the desired function execute. You’re all done!

6

Create an audio output monitor script

Connect to your Pi (or other Linux machine) and create the following file. The installation directory and filenames will vary based on your project, so be sure to update them however you’d like. The naming here is based on my driving my Furby’s DC motor while sound was being output.

sudo nano ~/furby/output-monitor.sh

Enter the following, save and exit:

#!/bin/bash DIR='/proc/asound/card1/pcm0p/sub0/status' CMD='python /home/pi/furby/furby.py' content='' while true do new_content=`cat $DIR` if [[ "$content" != "$new_content" ]]; then content=$new_content $CMD fi sleep 0.25 done

Change DIR above to point to the status file for your active audio device — I’m using a USB sound card, and this is the path for its status file.

To figure out which status file is in use for your audio device, simply play an audio sample:

speaker-test -t wav -c 6

.. and then view each of the status files and see which one contains the string “state: RUNNING”. There should be 4 status files total you’ll need to check, and they follow the pattern:

/proc/asound/card*/pcm0p/sub*/status

You can view each file as such. For example:

cat /proc/asound/card1/pcm0p/sub0/status

I found when using the Pimoroni Speaker pHAT that it used card0 (/proc/asound/card0/pcm0p/sub0/status), but your mileage may vary.

Change CMD above to point to the command you’d like to run when sound is detected (in my case, I’m executing a python script, furby.py, with the aforementioned file path).

7

Create a script to run as a daemon

This script will start the audio monitor script. Create the file:

sudo nano /etc/init.d/go-furby-go.sh

Paste the following, save, and exit:

#!/bin/bash # /etc/init.d/go-furby-go.sh case "$1" in start) echo "Starting go-furby-go.sh" /home/pi/furby/output-monitor.sh ;; stop) echo "Stopping go-furby-go.sh" killall output-monitor.sh ;; *) echo "Usage: /etc/init.d/go-furby-go.sh {start|stop}" exit 1 ;; esac exit 0
8

Make everything executable

Make both these scripts executable:

sudo chmod +x /etc/init.d/go-furby-go.sh sudo chmod +x /home/pi/furby/output-monitor.sh
9

Make sure everything runs at boot

To make sure our shell script runs at boot:

sudo update-rc.d go-furby-go.sh defaults
10

Reboot your Pi to test!

Reboot your Pi:

sudo reboot

Play a sample audio file and you should see the desired function execute. You’re all done!

11

Create an audio output monitor script

Connect to your Pi (or other Linux machine) and create the following file. The installation directory and filenames will vary based on your project, so be sure to update them however you’d like. The naming here is based on my driving my Furby’s DC motor while sound was being output.

sudo nano ~/furby/output-monitor.sh

Enter the following, save and exit:

#!/bin/bash DIR='/proc/asound/card1/pcm0p/sub0/status' CMD='python /home/pi/furby/furby.py' content='' while true do new_content=`cat $DIR` if [[ "$content" != "$new_content" ]]; then content=$new_content $CMD fi sleep 0.25 done

Change DIR above to point to the status file for your active audio device — I’m using a USB sound card, and this is the path for its status file.

To figure out which status file is in use for your audio device, simply play an audio sample:

speaker-test -t wav -c 6

.. and then view each of the status files and see which one contains the string “state: RUNNING”. There should be 4 status files total you’ll need to check, and they follow the pattern:

/proc/asound/card*/pcm0p/sub*/status

You can view each file as such. For example:

cat /proc/asound/card1/pcm0p/sub0/status

I found when using the Pimoroni Speaker pHAT that it used card0 (/proc/asound/card0/pcm0p/sub0/status), but your mileage may vary.

Change CMD above to point to the command you’d like to run when sound is detected (in my case, I’m executing a python script, furby.py, with the aforementioned file path).

12

Create a script to run as a daemon

This script will start the audio monitor script. Create the file:

sudo nano /etc/init.d/go-furby-go.sh

Paste the following, save, and exit:

#!/bin/bash # /etc/init.d/go-furby-go.sh case "$1" in start) echo "Starting go-furby-go.sh" /home/pi/furby/output-monitor.sh ;; stop) echo "Stopping go-furby-go.sh" killall output-monitor.sh ;; *) echo "Usage: /etc/init.d/go-furby-go.sh {start|stop}" exit 1 ;; esac exit 0
13

Make everything executable

Make both these scripts executable:

sudo chmod +x /etc/init.d/go-furby-go.sh sudo chmod +x /home/pi/furby/output-monitor.sh
14

Make sure everything runs at boot

To make sure our shell script runs at boot:

sudo update-rc.d go-furby-go.sh defaults
15

Reboot your Pi to test!

Reboot your Pi:

sudo reboot

Play a sample audio file and you should see the desired function execute. You’re all done!

16

Create an audio output monitor script

Connect to your Pi (or other Linux machine) and create the following file. The installation directory and filenames will vary based on your project, so be sure to update them however you’d like. The naming here is based on my driving my Furby’s DC motor while sound was being output.

sudo nano ~/furby/output-monitor.sh

Enter the following, save and exit:

#!/bin/bash DIR='/proc/asound/card1/pcm0p/sub0/status' CMD='python /home/pi/furby/furby.py' content='' while true do new_content=`cat $DIR` if [[ "$content" != "$new_content" ]]; then content=$new_content $CMD fi sleep 0.25 done

Change DIR above to point to the status file for your active audio device — I’m using a USB sound card, and this is the path for its status file.

To figure out which status file is in use for your audio device, simply play an audio sample:

speaker-test -t wav -c 6

.. and then view each of the status files and see which one contains the string “state: RUNNING”. There should be 4 status files total you’ll need to check, and they follow the pattern:

/proc/asound/card*/pcm0p/sub*/status

You can view each file as such. For example:

cat /proc/asound/card1/pcm0p/sub0/status

I found when using the Pimoroni Speaker pHAT that it used card0 (/proc/asound/card0/pcm0p/sub0/status), but your mileage may vary.

Change CMD above to point to the command you’d like to run when sound is detected (in my case, I’m executing a python script, furby.py, with the aforementioned file path).

17

Create a script to run as a daemon

This script will start the audio monitor script. Create the file:

sudo nano /etc/init.d/go-furby-go.sh

Paste the following, save, and exit:

#!/bin/bash # /etc/init.d/go-furby-go.sh case "$1" in start) echo "Starting go-furby-go.sh" /home/pi/furby/output-monitor.sh ;; stop) echo "Stopping go-furby-go.sh" killall output-monitor.sh ;; *) echo "Usage: /etc/init.d/go-furby-go.sh {start|stop}" exit 1 ;; esac exit 0
18

Make everything executable

Make both these scripts executable:

sudo chmod +x /etc/init.d/go-furby-go.sh sudo chmod +x /home/pi/furby/output-monitor.sh
19

Make sure everything runs at boot

To make sure our shell script runs at boot:

sudo update-rc.d go-furby-go.sh defaults
20

Reboot your Pi to test!

Reboot your Pi:

sudo reboot

Play a sample audio file and you should see the desired function execute. You’re all done!

21

Create an audio output monitor script

Connect to your Pi (or other Linux machine) and create the following file. The installation directory and filenames will vary based on your project, so be sure to update them however you’d like. The naming here is based on my driving my Furby’s DC motor while sound was being output.

sudo nano ~/furby/output-monitor.sh

Enter the following, save and exit:

#!/bin/bash DIR='/proc/asound/card1/pcm0p/sub0/status' CMD='python /home/pi/furby/furby.py' content='' while true do new_content=`cat $DIR` if [[ "$content" != "$new_content" ]]; then content=$new_content $CMD fi sleep 0.25 done

Change DIR above to point to the status file for your active audio device — I’m using a USB sound card, and this is the path for its status file.

To figure out which status file is in use for your audio device, simply play an audio sample:

speaker-test -t wav -c 6

.. and then view each of the status files and see which one contains the string “state: RUNNING”. There should be 4 status files total you’ll need to check, and they follow the pattern:

/proc/asound/card*/pcm0p/sub*/status

You can view each file as such. For example:

cat /proc/asound/card1/pcm0p/sub0/status

I found when using the Pimoroni Speaker pHAT that it used card0 (/proc/asound/card0/pcm0p/sub0/status), but your mileage may vary.

Change CMD above to point to the command you’d like to run when sound is detected (in my case, I’m executing a python script, furby.py, with the aforementioned file path).

22

Create an audio output monitor script

Connect to your Pi (or other Linux machine) and create the following file. The installation directory and filenames will vary based on your project, so be sure to update them however you’d like. The naming here is based on my driving my Furby’s DC motor while sound was being output.

sudo nano ~/furby/output-monitor.sh

Enter the following, save and exit:

#!/bin/bash DIR='/proc/asound/card1/pcm0p/sub0/status' CMD='python /home/pi/furby/furby.py' content='' while true do new_content=`cat $DIR` if [[ "$content" != "$new_content" ]]; then content=$new_content $CMD fi sleep 0.25 done

Change DIR above to point to the status file for your active audio device — I’m using a USB sound card, and this is the path for its status file.

To figure out which status file is in use for your audio device, simply play an audio sample:

speaker-test -t wav -c 6

.. and then view each of the status files and see which one contains the string “state: RUNNING”. There should be 4 status files total you’ll need to check, and they follow the pattern:

/proc/asound/card*/pcm0p/sub*/status

You can view each file as such. For example:

cat /proc/asound/card1/pcm0p/sub0/status

I found when using the Pimoroni Speaker pHAT that it used card0 (/proc/asound/card0/pcm0p/sub0/status), but your mileage may vary.

Change CMD above to point to the command you’d like to run when sound is detected (in my case, I’m executing a python script, furby.py, with the aforementioned file path).

23

Create an audio output monitor script

24

Create a script to run as a daemon

This script will start the audio monitor script. Create the file:

sudo nano /etc/init.d/go-furby-go.sh

Paste the following, save, and exit:

#!/bin/bash # /etc/init.d/go-furby-go.sh case "$1" in start) echo "Starting go-furby-go.sh" /home/pi/furby/output-monitor.sh ;; stop) echo "Stopping go-furby-go.sh" killall output-monitor.sh ;; *) echo "Usage: /etc/init.d/go-furby-go.sh {start|stop}" exit 1 ;; esac exit 0
25

Create a script to run as a daemon

This script will start the audio monitor script. Create the file:

sudo nano /etc/init.d/go-furby-go.sh

Paste the following, save, and exit:

#!/bin/bash # /etc/init.d/go-furby-go.sh case "$1" in start) echo "Starting go-furby-go.sh" /home/pi/furby/output-monitor.sh ;; stop) echo "Stopping go-furby-go.sh" killall output-monitor.sh ;; *) echo "Usage: /etc/init.d/go-furby-go.sh {start|stop}" exit 1 ;; esac exit 0
26

Create a script to run as a daemon

27

Make everything executable

Make both these scripts executable:

sudo chmod +x /etc/init.d/go-furby-go.sh sudo chmod +x /home/pi/furby/output-monitor.sh
28

Make everything executable

Make both these scripts executable:

sudo chmod +x /etc/init.d/go-furby-go.sh sudo chmod +x /home/pi/furby/output-monitor.sh
29

Make everything executable

30

Make sure everything runs at boot

To make sure our shell script runs at boot:

sudo update-rc.d go-furby-go.sh defaults
31

Make sure everything runs at boot

To make sure our shell script runs at boot:

sudo update-rc.d go-furby-go.sh defaults
32

Make sure everything runs at boot

33

Reboot your Pi to test!

Reboot your Pi:

sudo reboot

Play a sample audio file and you should see the desired function execute. You’re all done!

34

Reboot your Pi to test!

Reboot your Pi:

sudo reboot

Play a sample audio file and you should see the desired function execute. You’re all done!

35

Reboot your Pi to test!