#!/bin/bash # Auto-detect HiFiBerry and configure ALSA echo "==========================================" echo "HiFiBerry Auto-Configuration" echo "==========================================" echo "" # Find HiFiBerry card number echo "Detecting HiFiBerry..." CARD_INFO=$(aplay -l | grep -i hifiberry) if [ -z "$CARD_INFO" ]; then echo "❌ HiFiBerry not found!" echo "" echo "Make sure:" echo "1. HiFiBerry is properly seated on GPIO pins" echo "2. /boot/firmware/config.txt has: dtoverlay=hifiberry-dacplusadcpro" echo "3. You've rebooted after changing config.txt" exit 1 fi echo "✓ HiFiBerry found:" echo "$CARD_INFO" echo "" # Extract card number CARD_NUM=$(echo "$CARD_INFO" | grep -oP 'card \K[0-9]+' | head -1) echo "Card number: $CARD_NUM" echo "" # Create ALSA config echo "Creating ~/.asoundrc..." cat > ~/.asoundrc << EOF # ALSA Configuration for HiFiBerry DAC+ADC Pro # Auto-generated configuration pcm.!default { type asym playback.pcm "plughw:${CARD_NUM},0" capture.pcm "plughw:${CARD_NUM},0" } ctl.!default { type hw card ${CARD_NUM} } EOF echo "✓ Created ~/.asoundrc with card ${CARD_NUM}" echo "" # Show the config echo "Configuration:" cat ~/.asoundrc echo "" # Set volume echo "Setting volume to 100%..." amixer -c ${CARD_NUM} sset Master 100% 2>/dev/null || echo "Master control not available" amixer -c ${CARD_NUM} sset PCM 100% 2>/dev/null || echo "PCM control not available" amixer -c ${CARD_NUM} sset Digital 100% 2>/dev/null || echo "Digital control not available" amixer -c ${CARD_NUM} sset Analogue 100% 2>/dev/null || echo "Analogue control not available" echo "" # Test speaker echo "Testing speaker..." echo "🔊 Playing test tone - you should hear a beep!" aplay -D plughw:${CARD_NUM},0 /usr/share/sounds/alsa/Front_Center.wav 2>/dev/null || \ speaker-test -D plughw:${CARD_NUM},0 -c 1 -t sine -f 440 -l 2 2>/dev/null echo "" echo "==========================================" echo "Configuration Complete!" echo "==========================================" echo "" echo "Your HiFiBerry is configured as card ${CARD_NUM}" echo "" echo "Test commands:" echo " aplay -D plughw:${CARD_NUM},0 /usr/share/sounds/alsa/Front_Center.wav" echo " speaker-test -D plughw:${CARD_NUM},0 -c 1 -t wav" echo "" echo "For Python script, use device index:" # Find PyAudio device index python3 << PYEOF import pyaudio audio = pyaudio.PyAudio() print("") for i in range(audio.get_device_count()): info = audio.get_device_info_by_index(i) if 'hifiberry' in info['name'].lower(): print(f" PyAudio device index: {i}") print(f" Device name: {info['name']}") break audio.terminate() PYEOF echo ""