How To Detect That Audio Is Currently Being Output In Linux And Use It To Call A Program
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.shEnter 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 doneChange 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*/statusYou can view each file as such. For example:
cat /proc/asound/card1/pcm0p/sub0/statusI 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).
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.shPaste 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 0Make 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.shMake sure everything runs at boot
To make sure our shell script runs at boot:
sudo update-rc.d go-furby-go.sh defaultsReboot your Pi to test!
Reboot your Pi:
sudo rebootPlay a sample audio file and you should see the desired function execute. You’re all done!
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.shEnter 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 doneChange 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*/statusYou can view each file as such. For example:
cat /proc/asound/card1/pcm0p/sub0/statusI 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).
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.shPaste 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 0Make 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.shMake sure everything runs at boot
To make sure our shell script runs at boot:
sudo update-rc.d go-furby-go.sh defaultsReboot your Pi to test!
Reboot your Pi:
sudo rebootPlay a sample audio file and you should see the desired function execute. You’re all done!
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.shEnter 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 doneChange 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*/statusYou can view each file as such. For example:
cat /proc/asound/card1/pcm0p/sub0/statusI 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).
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.shPaste 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 0Make 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.shMake sure everything runs at boot
To make sure our shell script runs at boot:
sudo update-rc.d go-furby-go.sh defaultsReboot your Pi to test!
Reboot your Pi:
sudo rebootPlay a sample audio file and you should see the desired function execute. You’re all done!
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.shEnter 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 doneChange 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*/statusYou can view each file as such. For example:
cat /proc/asound/card1/pcm0p/sub0/statusI 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).
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.shPaste 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 0Make 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.shMake sure everything runs at boot
To make sure our shell script runs at boot:
sudo update-rc.d go-furby-go.sh defaultsReboot your Pi to test!
Reboot your Pi:
sudo rebootPlay a sample audio file and you should see the desired function execute. You’re all done!
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.shEnter 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 doneChange 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*/statusYou can view each file as such. For example:
cat /proc/asound/card1/pcm0p/sub0/statusI 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).
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.shEnter 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 doneChange 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*/statusYou can view each file as such. For example:
cat /proc/asound/card1/pcm0p/sub0/statusI 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).
Create an audio output monitor script
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.shPaste 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 0Create 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.shPaste 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 0Create a script to run as a daemon
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.shMake 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.shMake everything executable
Make sure everything runs at boot
To make sure our shell script runs at boot:
sudo update-rc.d go-furby-go.sh defaultsMake sure everything runs at boot
To make sure our shell script runs at boot:
sudo update-rc.d go-furby-go.sh defaultsMake sure everything runs at boot
Reboot your Pi to test!
Reboot your Pi:
sudo rebootPlay a sample audio file and you should see the desired function execute. You’re all done!
Reboot your Pi to test!
Reboot your Pi:
sudo rebootPlay a sample audio file and you should see the desired function execute. You’re all done!