How to Detect Sound Output in Linux and Use It to Call a Program (Or Drive a Motor!)

How to Detect Sound Output in Linux and Use It to Call a Program (Or Drive a Motor!)How to Detect Sound Output in Linux and Use It to Call a Program (Or Drive a Motor!)
Drive a motor or light up an LED based on Linux sound output!
Zach Zach (233)
Total time: 15 minutes 
Updated: December 1st, 2019

This guide will show you how to detect Linux sound output and use it to run a program — like driving a servo, stepper motor or DC motor! I used this method to control a motor via my Raspberry Pi’s GPIO pins, turning my Furby into an Amazon Echo — but you can use it to call pretty much anything.

In a nutshell, there’s a specific audio status file that gets written to when sound is actively being output; we’ll monitor this file and its contents and hook into the state change to call our program.

Posted in these interests:

linux
PRIMARY
39 guides
code
68 guides

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).

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

Make both these scripts executable:

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

To make sure our shell script runs at boot:

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

Reboot your Pi:

sudo reboot

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

How to Detect Sound Output in Linux and Use It to Call a Program (Or Drive a Motor!)

How to Detect Sound Output in Linux and Use It to Call a Program (Or Drive a Motor!)How to Detect Sound Output in Linux and Use It to Call a Program (Or Drive a Motor!)
Drive a motor or light up an LED based on Linux sound output!
Zach Zach (233)
Total time: 15 minutes 
Updated: December 1st, 2019

This guide will show you how to detect Linux sound output and use it to run a program — like driving a servo, stepper motor or DC motor! I used this method to control a motor via my Raspberry Pi’s GPIO pins, turning my Furby into an Amazon Echo — but you can use it to call pretty much anything.

In a nutshell, there’s a specific audio status file that gets written to when sound is actively being output; we’ll monitor this file and its contents and hook into the state change to call our program.

Posted in these interests:

linux
PRIMARY
39 guides
code
68 guides

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).

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

Make both these scripts executable:

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

To make sure our shell script runs at boot:

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

Reboot your Pi:

sudo reboot

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

Jump to step

How to Detect Sound Output in Linux and Use It to Call a Program (Or Drive a Motor!)

How to Detect Sound Output in Linux and Use It to Call a Program (Or Drive a Motor!)How to Detect Sound Output in Linux and Use It to Call a Program (Or Drive a Motor!)
Drive a motor or light up an LED based on Linux sound output!
Zach Zach (233)
Total time: 15 minutes 
Updated: December 1st, 2019

This guide will show you how to detect Linux sound output and use it to run a program — like driving a servo, stepper motor or DC motor! I used this method to control a motor via my Raspberry Pi’s GPIO pins, turning my Furby into an Amazon Echo — but you can use it to call pretty much anything.

In a nutshell, there’s a specific audio status file that gets written to when sound is actively being output; we’ll monitor this file and its contents and hook into the state change to call our program.

Posted in these interests:

linux
PRIMARY
39 guides
code
68 guides

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).

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

Make both these scripts executable:

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

To make sure our shell script runs at boot:

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

Reboot your Pi:

sudo reboot

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

How to Detect Sound Output in Linux and Use It to Call a Program (Or Drive a Motor!)

How to Detect Sound Output in Linux and Use It to Call a Program (Or Drive a Motor!)How to Detect Sound Output in Linux and Use It to Call a Program (Or Drive a Motor!)
Drive a motor or light up an LED based on Linux sound output!
Zach Zach (233)
Total time: 15 minutes 
Updated: December 1st, 2019

This guide will show you how to detect Linux sound output and use it to run a program — like driving a servo, stepper motor or DC motor! I used this method to control a motor via my Raspberry Pi’s GPIO pins, turning my Furby into an Amazon Echo — but you can use it to call pretty much anything.

In a nutshell, there’s a specific audio status file that gets written to when sound is actively being output; we’ll monitor this file and its contents and hook into the state change to call our program.

Posted in these interests:

linux
PRIMARY
39 guides
code
68 guides

How to Detect Sound Output in Linux and Use It to Call a Program (Or Drive a Motor!)

linuxcode
Drive a motor or light up an LED based on Linux sound output!
Zach Zach (233)
Total time: 15 minutes 
Updated: December 1st, 2019
Zach
2
 
Mentioned here
I turned a Furby into an Amazon Echo. Introducing: Furlexa

Posted in these interests:

linux
PRIMARY
39 guides
code
68 guides
linux
PRIMARY
39 guides
code
68 guides
PRIMARY
Jump to step
Calling all writers!

We’re hiring. Write for Howchoo

2
 
In these interests
linux
PRIMARY
39 guides
code
68 guides
linux
PRIMARY
39 guides
code
68 guides
PRIMARY
Jump to step

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).

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

Make both these scripts executable:

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

To make sure our shell script runs at boot:

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

Reboot your Pi:

sudo reboot

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

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).

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).

Create an audio output monitor script

Mentioned here
How to Connect to a Raspberry Pi Remotely via SSH How to Connect to a Raspberry Pi Remotely via SSHThe preferred (and most common) method of connecting to your Pi to run commands.
How to Connect to a Raspberry Pi Remotely via SSHI turned a Furby into an Amazon Echo. Introducing: Furlexa

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

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

Create a script to run as a daemon

Make both these scripts executable:

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

Make both these scripts executable:

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

Make everything executable

To make sure our shell script runs at boot:

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

To make sure our shell script runs at boot:

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

Make sure everything runs at boot

Reboot your Pi:

sudo reboot

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

Reboot your Pi:

sudo reboot

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

Reboot your Pi to test!

Calling all writers!

We’re hiring. Write for Howchoo

Zach's profile pictureZach
Joined in 2015
Web developer, designer, tinkerer, and beer enthusiast living in Tampa, Florida.
Zach's profile picturehowchoo
Share this guide!
TwitterRedditEmailTextPinterest
Related to this guide:
Manjaro vs UbuntuManjaro vs Ubuntu
In these interests: ubuntulinux
Download and Install ManjaroDownload and Install Manjaro
Looking for a new flavor of Linux?
Ash's profile picture AshView
In these interests: linux
SSH Login Without a PasswordSSH Login Without a Password
There are a few reasons you might want to set up password-less login via SSH. Manual login For manual login, typing your password over and over is a pain—especially if you’re doing this frequently.
Tyler's profile picture TylerView
In these interests: sysadminlinux
Manjaro vs UbuntuManjaro vs Ubuntu
In these interests: ubuntulinux
Ash's profile pictureViewubuntulinux
Download and Install ManjaroDownload and Install Manjaro
Looking for a new flavor of Linux?
Ash's profile picture AshView
In these interests: linux
Ash's profile pictureViewlinux
SSH Login Without a PasswordSSH Login Without a Password
There are a few reasons you might want to set up password-less login via SSH. Manual login For manual login, typing your password over and over is a pain—especially if you’re doing this frequently.
Tyler's profile picture TylerView
In these interests: sysadminlinux
Tyler's profile pictureViewsysadminlinux
People also read:
If you are using Windows on your PC, it may be easier to code or program by running Linux or another Unix-based operating system alongside Windows.
Proceed with caution Some users have reported that these settings have messed up their trackpads.
Boot Camp is a great feature of mcOS, but it won’t help you install Linux. To do that we’re going to use a tool called rEFInd.
The Dirty Cow exploit is a serious exploit in the Linux kernel that allows users to gain root access to the system.
If you spend a lot of time staring at log files, you might want to consider installing CCZE. CCZE is a tool that color highlights your log files making them much easier to read.
Python 3 Windows 10
Get the latest edition of Python in just minutes.
Python Path Variable Windows 10
Run Python scripts in command prompt without typing the whole path.
Python Version
Not sure what version of Python you’re running? Time to find out!
If you are using Windows on your PC, it may be easier to code or program by running Linux or another Unix-based operating system alongside Windows.
Proceed with caution Some users have reported that these settings have messed up their trackpads.
Boot Camp is a great feature of mcOS, but it won’t help you install Linux. To do that we’re going to use a tool called rEFInd.
The Dirty Cow exploit is a serious exploit in the Linux kernel that allows users to gain root access to the system.
How to Set Up Linux on Your PC Using a Virtual Machine
The Perfect (almost) Touchpad Settings on Linux
How to Install and Dual-Boot Linux on a Mac
How to Fix the Dirty Cow Exploit on Raspberry Pi
What is the dirty cow exploit?
If you spend a lot of time staring at log files, you might want to consider installing CCZE. CCZE is a tool that color highlights your log files making them much easier to read.
Python 3 Windows 10
Get the latest edition of Python in just minutes.
Python Path Variable Windows 10
Run Python scripts in command prompt without typing the whole path.
Python Version
Not sure what version of Python you’re running? Time to find out!
How to colorize your logs with CCZE
How to Test Sendmail from the Shell
Python 3 Windows 10
Python 3 Windows 10How to Install Python 3 on Windows 10
Python Path Variable Windows 10
Python Path Variable Windows 10How to Add Python to the Path Variable on Windows 10
Python Version
Python VersionHow to Check Your Python Version
Posted in these interests:
linuxlinux
linux
PRIMARY
codecode
Code is poetry — one line at a time.
linuxlinux
linux
PRIMARY
PRIMARY
Explore
codecode
Code is poetry — one line at a time.
Explore
Discuss this guide:
We’re hiring!
Are you a passionate writer? We want to hear from you!
We’re hiring!
Are you a passionate writer? We want to hear from you!
View openings

Want to support Howchoo? When you buy a tool or material through one of our Amazon links, we earn a small commission as an Amazon Associate.

Donate

Leave a Reply

Your email address will not be published. Required fields are marked *