MiczFlor / RPi-Jukebox-RFID

A Raspberry Pi jukebox, playing local music, podcasts, web radio and streams triggered by RFID cards, web app or home automation. All plug and play via USB. GPIO scripts available.
http://phoniebox.de
MIT License
1.41k stars 398 forks source link

Hack: Switchable Audio Interface with 2 different sound cards #1389

Open manajoe opened 3 years ago

manajoe commented 3 years ago

Hack: Switchable Audio Interface with 2 different sound cards

a little different to https://github.com/MiczFlor/RPi-Jukebox-RFID/pull/1213 as I didn't have one soundcard with two outputs, I tried to use my USB sound card for speakers and onboard sound card for headphones In the beginning it was a lot of trial and error to figure out, how both sound cards, names and IDs have to be used. But eventually I found a working setup for me, being able to store the separate volumes for each audio output

Maybe it's a too specific setup, but maybe someone can use this:

Prerequisites

pcm.headphone { type hw card 0 }

ctl.headphone { type hw card 0 }

- determine the names and hardware IDs of the sound cards:
`aplay -L | grep -B2 "Direct hardware device without any conversions"`

hw:CARD=Headphones,DEV=0 bcm2835 Headphones, bcm2835 Headphones Direct hardware device without any conversions

hw:CARD=Device,DEV=0 USB PnP Sound Device, USB Audio Direct hardware device without any conversions


`amixer -c 0 scontents`:
`Simple mixer control 'Headphone',0`

`amixer -c q scontents`:
`Simple mixer control 'PCM',0`
- configure 2 audio outputs in `/etc/mpd.conf`

audio_output { type "alsa" name "Headphone" # name as in output of "amixer -c 0 scontents"
device "hw:0,0" # optional # Device = output of aplay -L from above mixer_device "default" # mixer_device = "hw:" + Devicename after CARD= mixer_control "Headphone" # name as in output of "amixer -c 1 scontents" mixer_index "0" # optional }

audio_output { type "alsa" name "PCM" # name as in output of "amixer -c 1 scontents" device "hw:CARD=Device,DEV=0" # Device = output of aplay -L from above mixer_device "hw:Device" # mixer_device = "hw:" + Devicename after CARD= mixer_control "PCM" # name as in output of "amixer -c 1 scontents" mixer_index "0" # optional }

- create additional settings files with content "0" in `RPi-Jukebox-RFID/settings/`

Audio_Volume_Level_Headphone Audio_Volume_Level_PCM Max_Volume_Limit_Headphone Max_Volume_Limit_PCM

- added executable file `switch_audio_output.sh` to `RPi-Jukebox-RFID/scripts/userscripts/`

!/bin/bash

PATHDATA="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

Get active MPC Output

ACTIVE_OUTPUT=mpc outputs | awk '/enabled/ { print $2 }' echo $ACTIVE_OUTPUT "is active" #############################################################

Define variables for Output Names and IDs according to the IDs from output of "mpc outputs"

MPC_OUTPUT1="PCM" ALSA_OUTPUT1="1" MPC_OUTPUT2="Headphone" ALSA_OUTPUT2="0" ############################################################## if [ $ACTIVE_OUTPUT -eq 2 ] then echo "Activate PCM"

Save Volume and Max Volume settings for previous output

    $PATHDATA/../playout_controls.sh -c=getmaxvolume > $PATHDATA/../../settings/Max_Volume_Limit_$MPC_OUTPUT2
    $PATHDATA/../playout_controls.sh -c=getvolume > $PATHDATA/../../settings/Audio_Volume_Level_$MPC_OUTPUT2
    # Change Audio Output in config files
    sudo sed -i "s/defaults.pcm.card $ALSA_OUTPUT2/defaults.pcm.card $ALSA_OUTPUT1/" /usr/share/alsa/alsa.conf      
    sudo sed -i "s/defaults.ctl.card $ALSA_OUTPUT2/defaults.ctl.card $ALSA_OUTPUT1/" /usr/share/alsa/alsa.conf      
    sudo sed -i "s/$MPC_OUTPUT2/$MPC_OUTPUT1/" $PATHDATA/../../settings/Audio_iFace_Name
    # Change Audio Output in MPD
    mpc enable 1 && mpc disable 2
    # Get previous Volume and Max Volume for new output from file
    if [ ! -f  ${PATHDATA}/../../settings/Max_Volume_Limit_${MPC_OUTPUT1} ]
    then
        echo ${PATHDATA}/../../settings/Max_Volume_Limit > ${PATHDATA}/../../settings/Max_Volume_Limit_${MPC_OUTPUT1}
    fi
    if [ ! -f  ${PATHDATA}/../../settings/Audio_Volume_Level_${MPC_OUTPUT1} ]
    then
        echo ${PATHDATA}/../../settings/Startup_Volume > ${PATHDATA}/../../settings/Audio_Volume_Level_${MPC_OUTPUT1}
    fi

    $PATHDATA/../playout_controls.sh -c=setmaxvolume -v=$(<${PATHDATA}/../../settings/Max_Volume_Limit_${MPC_OUTPUT1})
    $PATHDATA/../playout_controls.sh -c=setvolume -v=$(<${PATHDATA}/../../settings/Audio_Volume_Level_${MPC_OUTPUT1})

elif [ $ACTIVE_OUTPUT -eq 1 ] then if [[ $1 == "default" ]] # I try to call the script with a parameter "default" within the startup-scripts.sh, to set the desired "default" audio output then echo "Nothing to do"

else

    echo "Activate Headphone"
    # Save Volume and Max Volume settings for previous output
    $PATHDATA/../playout_controls.sh -c=getmaxvolume > $PATHDATA/../../settings/Max_Volume_Limit_$MPC_OUTPUT1
    $PATHDATA/../playout_controls.sh -c=getvolume > $PATHDATA/../../settings/Audio_Volume_Level_$MPC_OUTPUT1    
    # Change Audio Output in config files       
    sudo sed -i "s/defaults.pcm.card $ALSA_OUTPUT1/defaults.pcm.card $ALSA_OUTPUT2/" /usr/share/alsa/alsa.conf
    sudo sed -i "s/defaults.ctl.card $ALSA_OUTPUT1/defaults.ctl.card $ALSA_OUTPUT2/" /usr/share/alsa/alsa.conf
    sudo sed -i "s/$MPC_OUTPUT1/$MPC_OUTPUT2/" $PATHDATA/../../settings/Audio_iFace_Name
    # Change Audio Output in MPD
    mpc enable 2 && mpc disable 1
    # Get previous Volume and Max Volume for new output from file   
    if [ ! -f  ${PATHDATA}/../../settings/Max_Volume_Limit_${MPC_OUTPUT2} ]
    then
        echo ${PATHDATA}/../../settings/Max_Volume_Limit > ${PATHDATA}/../../settings/Max_Volume_Limit_${MPC_OUTPUT2}
    fi
    if [ ! -f  ${PATHDATA}/../../settings/Audio_Volume_Level_${MPC_OUTPUT2} ]
    then
        echo ${PATHDATA}/../../settings/Startup_Volume > ${PATHDATA}/../../settings/Audio_Volume_Level_${MPC_OUTPUT2}
    fi      

    $PATHDATA/../playout_controls.sh -c=setmaxvolume -v=$(<${PATHDATA}/../../settings/Max_Volume_Limit_${MPC_OUTPUT2})
    $PATHDATA/../playout_controls.sh -c=setvolume -v=$(<${PATHDATA}/../../settings/Audio_Volume_Level_${MPC_OUTPUT2})       
fi

fi


- I call the `RPi-Jukebox-RFID/scripts/userscripts/switch_audio_output.sh` vie RFID card, GPIO button would also be possible
s-martin commented 3 years ago

Thanks! I’m wondering, if we should add this to the wiki or if this could integrated in the code base with a PR?

ddbkva commented 3 years ago

is it really working ???

Thilo-90 commented 3 years ago

I modified the script a bit. I cleared out the steps for saving the volume and named the devices to my likings. And the toggle is working just fine.

I am currently trying to get the command bash RPi-Jukebox-RFID/scripts/userscripts/switch_audio_output.sh, which toggles the output devices, onto a button. (see discussion-1638

ddbkva commented 2 years ago

how can activate this script ? bash RPi-Jukebox-RFID/scripts/userscripts/switch_audio_output.sh is not working my usb speaker are on Card 2 so i only change the settings at /etc/asound.conf ? i disconnect the hdmi so my usb speaker are on card 1

manajoe commented 2 years ago

@ddbkva I'm not sure, in my setup, exactly this command works Additionally, i did a chmod +x RPi-Jukebox-RFID/scripts/userscripts/switch_audio_output.sh. Did you also read https://github.com/MiczFlor/RPi-Jukebox-RFID/discussions/1638, maybe this helps?