My experience of listening to audio files on my Raspberry Pi has been sub-par thus far. Either I have to rely on the HDMI audio out and listen to music via my in-build monitor speakers or pair my Bluetooth headphones. For some reason neither works for me. Audio output quality via monitor speakers is poor and the Bluetooth connection is also jittery making it an unpleasant experience.
I was looking for some workaround for this problem when it struck me that the Raspberry Pi must also be able to cast the music to my Google Nest mini and if somehow I can get that working, I will be able to solve the audio problem on both fronts – good quality sound and better connectivity. I found my answer in the form of a Python3 package called “catt”. catt stands for “Cast All The Things” and is a nifty Python library that allows the rendering of local files to Chromecast devices. Installing catt is just a simple command “pip install catt”. This installs catt for Python3. Once installed casting a local mp3 was even more straightforward. Just “catt cast ./localmusic.mp3” found my Nest Mini and started casting the audio to it.
The github page for catt gives enough primer to start using this utility. It has commands to scan for Chromecast devices, start/stop the casting, increase/decrease the volume, skip to next etc. “catt -h” lists out the help and describes what each option can do.
But I had something else on my mind. I wanted to build a small utility that will allow me to stream music from my local ~/Music folder and then also be able to control the catt behavior by issuing commands via a menu drive cli utility. Sure enough, I got started with that and ended up coding two scripts. One to control the menu items which allow starting/stopping of tracks, control volume, show play status etc. And another script to run the “catt cast” command to the Nest mini for playing the music. Here are the scripts and the whole thing in action.
#! /usr/bin/sh
loopplay=false
if [ $# -gt 0 ];then
loopplay=true
fi
musicfiles=`ls -1 ~/Music/*.mp3 2>/dev/null | wc -l `
if [ $musicfiles -eq 0 ]; then
read -p "No music files to play. Hit enter to quit"
exit
fi
count=1
while [ $count -le $musicfiles ]
do
catt cast "`ls -1 ~/Music/*.mp3 | head -$count | tail -1`"
count=`expr $count + 1`
if [ $count -gt $musicfiles ] && [ $loopplay == true ]
then
count=1
fi
done
Above script is responsible to cast the audio to Nest Mini and and capability to loop through the list of music files if required. The script to control catt behavior once the music is playing is below.
#! /usr/bin/sh
function display_menu(){
echo CATT-UI
echo "--------------------"
echo 1. Play Music files
echo 2. Play Music files in loop
echo 3. Volume Up
echo 4. Volume Down
echo 5. Play next
echo 6. Toggle Play
echo 7. Track info
echo 0. Quit
}
while [ 1 ]
do
clear
display_menu
read -p "Enter choice : " choice
case $choice in
0) catt stop
kill -9 `ps -ef | grep playmusic | grep -v grep | awk '{print $2}'` 2>/dev/null
clear
exit 0;;
1) mate-terminal -e ~/.local/bin/playmusic;;
2) mate-terminal -e ~/.local/bin/playmusic loop;;
3) catt volumeup;;
4) catt volumedown;;
5) catt skip;;
6) catt play_toggle;;
7) catt status
read -p "Hit Enter to continue...";;
*) echo "Not coded yet";;
esac
done
This surely is a fun way to control the Google Nest Mini from my Raspberry Pi. Helped me achieve better audio quality and better connectivity.