#!/bin/bash # detect-i2s-device.sh — Probe for available I2S audio devices # and create /run/i2s-audio-dev containing the best candidate. # Called by detect-i2s-device.service before pd-synth-i2s starts. set -euo pipefail DEVICE_FILE="/run/i2s-audio-dev" write_dev() { echo "$1" > "$DEVICE_FILE"; echo "[DETECT] I2S device: $1"; exit 0; } # Try named I2S devices first (most reliable) for dev in "hw:I2S,0" "hw:MI2S,0" "hw:i2s,0" "hw:mi2s,0"; do if aplay -l 2>/dev/null | grep -qi "${dev%,*}"; then write_dev "$dev" fi done # Try probing ALSA device list for anything that looks like I2S while read -r card; do id=$(echo "$card" | awk '{print $2}' | tr -d ':') name=$(echo "$card" | cut -d: -f2- | xargs) if echo "$name" | grep -qi -E "(i2s|mi2s|quat|tert|dac|audio)"; then write_dev "hw:$id,0" fi done < <(cat /proc/asound/cards 2>/dev/null || true) # Fallback to hw:0,0 (first audio device, may be HDMI or analog) if aplay -l 2>/dev/null | grep -q "^card 0"; then write_dev "hw:0,0" fi # Nothing found echo "[DETECT] No I2S audio device found" >&2 echo "" > "$DEVICE_FILE" exit 1