Skip to content

Instantly share code, notes, and snippets.

@haydenmc
Created August 22, 2025 15:59
Show Gist options
  • Select an option

  • Save haydenmc/4abc62cf99a21aa4fe64392a29bcd1b1 to your computer and use it in GitHub Desktop.

Select an option

Save haydenmc/4abc62cf99a21aa4fe64392a29bcd1b1 to your computer and use it in GitHub Desktop.
Start Snapclient
[Unit]
Description=Snapclient for Speakers
After=network.target sound.target
[Service]
User=speaker
ExecStart=/usr/local/bin/start_snapcast.sh 10.x.x.x CLIENT_NUMBER /dev/snd/by-id/usb-Creative_Technology_Ltd_Sound_Blaster_Play__3_YDSB1730431006108F-00
Restart=on-failure
[Install]
WantedBy=multi-user.target
#!/bin/bash
# Usage check
if [[ $# -ne 3 ]]; then
echo "Usage: $0 <snapserver_hostname> <snapclient_instance_number> <path_to_by-id_symlink>"
exit 1
fi
SNAPSERVER_HOSTNAME="$1"
INSTANCE_ID="$2"
BYID_PATH="$3"
# Check if the path exists
if [[ ! -e "$BYID_PATH" ]]; then
echo "Error: '$BYID_PATH' does not exist."
exit 2
fi
# Resolve to actual ALSA device path
REAL_DEV=$(readlink -f "$BYID_PATH")
# Extract card number (e.g., controlC3 → 3)
if [[ "$REAL_DEV" =~ controlC([0-9]+) ]]; then
CARD_NUM="${BASH_REMATCH[1]}"
else
echo "Error: Couldn't extract card number from '$REAL_DEV'"
exit 3
fi
# Read the ALSA card ID (e.g., S3, S4_1, etc.)
CARD_ID_FILE="/proc/asound/card${CARD_NUM}/id"
if [[ ! -f "$CARD_ID_FILE" ]]; then
echo "Error: Card ID file not found at '$CARD_ID_FILE'"
exit 4
fi
CARD_ID=$(cat "$CARD_ID_FILE")
# Run snapclient -l and extract matching line
DEVICE_LINE=$(snapclient -l | grep -E "^[0-9]+: plughw:CARD=${CARD_ID},DEV=0")
if [[ -z "$DEVICE_LINE" ]]; then
echo "Error: No matching plughw device found for ALSA ID '$CARD_ID'"
exit 2
fi
# Extract the device number (e.g., 55:)
DEVICE_NUM=$(echo "$DEVICE_LINE" | awk -F: '{print $1}' | xargs)
# Output result
echo "Instance ID: $INSTANCE_ID"
echo "ALSA Card Number: $CARD_NUM"
echo "ALSA Card ID: $CARD_ID"
echo "Snapclient Device Number: $DEVICE_NUM"
# Adjust volume to 100%
echo "Adjusting volume..."
amixer -c $CARD_NUM set Speaker 100%
# Start snapclient
echo "Starting snapclient..."
snapclient -h $SNAPSERVER_HOSTNAME -s $DEVICE_NUM -i $INSTANCE_ID
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment