Subject:

Xonotic now-playing MPD script


Date: Message-Id: https://www.5snb.club/posts/2022/xonotic-now-playing/
X-Go-Listen-To: Laura Les - Haunted

First off, the script:

xon_mpc_artist="$(mpc status -f "%artist%" | head -n1 | sed 's/;/;/g' | sed 's/\^/^^/g')"
xon_mpc_title="$(mpc status -f "%title%" | head -n1 | sed 's/;/;/g' | sed 's/\^/^^/g')"
xon_mpc_album="$(mpc status -f "%album%" | head -n1 | sed 's/;/;/g' | sed 's/\^/^^/g')"

if [ "$playing" = true ]; then
echo "say /me ^8np^7 ^x6b6$xon_mpc_artist ^xa44${xon_mpc_title:0:60} ^x345$xon_mpc_album" | head -n1 > "$HOME/.local/share/xonotic/.xonotic/data/np.cfg"
else
echo "" > "$HOME/.local/share/xonotic/.xonotic/data/np.cfg"
fi

And on the xonotic side, it’s just bind n "exec np.cfg".

A now-playing script just lets you press a button in-game and broadcast whatever track you’re currently playing in chat.

My general strategy for a now-playing is to write a command to a file every time the current song changes, and then, in-game, bind a key to just execute that file as commands.

I use i3blocks as a i3 status bar, which runs every time there’s a status change on my songs (it uses mpc idle at the top). That’s a convenient place to run this code, but as long as it runs often enough to not be out of date, it doesn’t really matter how you run this. It’s not particularly perf sensitive, as it won’t be making any network calls in the way a last.fm based script would be.

Then it’s just a matter of correctly formatting the track and escaping the special characters. I want to use /me here for better formatting, which means I can’t quote anything, quotes are treated literally. But replacing semicolon with a greek question mark works Well Enough for me.

It looks like this:

*5225225(happy) np Namii Let’s All Love Namii Don’t Let The Dark In.

The structure is “np” to indicate it’s a now-playing message, then the artist (Namii) in green, the track name (Let’s All Love Namii) in red, and finally the album (Don’t Let The Dark In.) in a bluish grey.

I use colors here instead of dashes to keep the length down, being mindful trying to not be spammy. Same reason I will truncate the track title if it’s too long.