Switching audio between usb DAC and hdmi

Hello.

I’ve received my brand new RP4, and installed Dietpi right away on it.
(What a wonderfull easy to use distribution, thanks to all people working on it !)


What i want to do with it :

  • Audiophile upnp renderer to usb DAC to hifi system

  • Steamlink box to play game on my tv using tv’s build in speakers


    So right now here i am :

  • Upnp Renderer : OK done, got gmediarender running, USB DAC (Topping D50S) selected, everything goes fine, work like a charm.
    For those interested, i have a Synology NAS with his basic Multimedia server for DLNA, BubbleUpnp Server to provide Openhome compatibility and Linn Kazoo setup on a android phone to control everything. Easy to set up.

  • I have installed LXDE, Tightvnc server, Steamlink ok.
    Now, when i plug my hdmi cable to tv, i dont have any image.
    My config is :
    vc4-fkms-v3d-pi4 : OpenGL | 1920 x 1080
    GPU / RAM Memory split : 512
    Even after reboot, i have no image on tv.
    Well, i’m gonna dig on google to find that solution, it’s not my main question.

My question is, how can i easly switch between USB DAC to HDMI (video and audio) when i want listen music on my hifi system or play on tv with tv speakers ?

Do i have to go to dietpi-config > Audio option > Sound card and change it manually every time ? Or could there be a easiet way ?

Thanks =)

Hi, did you ever find out how to do this? I have a similar situation. Thanks!

Hello.

No, i’ve finally installed Volumio and i’m just using it as an audio streamer.
So, sorry, i can’t help you.

The changes you make in dietpi-config are stored at

/boot/dietpi.txt

there is a line:

# Sound card
CONFIG_SOUNDCARD=SOMETHING

For HDMI output it is: “CONFIG_SOUNDCARD=rpi-bcm2835-hdmi”
For USB dac it is: “CONFIG_SOUNDCARD=usb-dac”

I guess you could write a script to change that line and reboot your Pi, bc you have to reboot to make the change happen.


Sometimes you have to use the auto-conversion plugin if you don’t get sound with your USB DAC, but idk where this config is stored, you can enable it also in dietpi-config. (see https://dietpi.com/forum/t/no-audio-from-usb-dac/4996/1)


The script “HDMItoUSB.sh” could look something like:

 #!/bin/bash
 # script to change from HDMI to USB sound
sed -i 's/rpi-bcm2835-hdmi/usb-dac/g' /boot/dietpi.txt
echo "Soundcard changed from HDMI to USB"
echo "machine will reboot in 5 seconds"
sleep 5
reboot

to run the script:

sudo path/to/script/HDMItoUSB.sh

To change from USB to HDMI you just have to change

sed -i 's/rpi-bcm2835-hdmi/usb-dac/g' /boot/dietpi.txt

to

sed -i 's/usb-dac/rpi-bcm2835-hdmi/g' /boot/dietpi.txt

You could even check which soundcard is activated right now and change to the other one, done in one script:

#!/bin/bash
if grep -q CONFIG_SOUNDCARD=rpi-bcm2835-hdmi "/boot/dietpi.txt"; then
    sed -i 's/rpi-bcm2835-hdmi/usb-dac/g' /boot/dietpi.txt
    echo "Soundcard changed from HDMI to USB"
    echo "machine will reboot in 5 seconds"
    sleep 5
    reboot
else
    sed -i 's/usb-dac/rpi-bcm2835-hdmi/g' /boot/dietpi.txt
    echo "Soundcard changed from USB to HDMI"
    echo "machine will reboot in 5 seconds"
    sleep 5
    reboot
fi

I think you have to run the script with root permissions to make changes in /boot/ and to be able to reboot.

These are just theoretical thoughts and this has to be tested! I’m quite new to Linux and scripting stuff and I do not take legal responsibility for anything written here :wink:


ADDENDUM:
You can also output the sound through HDMI and USB simultaneously, says this thread at stackexchange:
https://raspberrypi.stackexchange.com/questions/55593/output-sound-through-hdmi-and-usb-sound-card

I digged deeper and I’m not sure if it is enough just to change this one line bc with USB soundcards ALSA get’s involved and I read a lot of stuff where they had to fiddle with the alsa.conf

But I made the script a little bit prettier to change the line in /boot/dietpi.txt, to bypass dietpi-config.

#!/bin/bash
if      grep -q CONFIG_SOUNDCARD=rpi-bcm2835-hdmi "/boot/dietpi.txt"; then
        sed -i 's/rpi-bcm2835-hdmi/usb-dac/g' /boot/dietpi.txt
        echo -e "\e[32mSoundcard changed from HDMI to USB.\e[39m"

elif    grep -q CONFIG_SOUNDCARD=usb-dac "/boot/dietpi.txt"; then
        sed -i 's/usb-dac/rpi-bcm2835-hdmi/g' /boot/dietpi.txt
        echo -e "\e[32mSoundcard changed from USB to HDMI.\e[39m"

elif    grep -q CONFIG_SOUNDCARD=none "/boot/dietpi.txt"; then
        sed -i 's/CONFIG_SOUNDCARD=none/CONFIG_SOUNDCARD=rpi-bcm2835-hdmi/g' /boot/dietpi.txt
        echo -e "\e[32mNo soundcard was set, changing it to HDMI now.\e[39m"

else
        device=$(grep 'CONFIG_SOUNDCARD=' "/boot/dietpi.txt" | sed 's/^.*=//' );
        echo -e "\e[32mThe soundcard was set to $device."
        echo -e "Setting it to HDMI now. \e[39m"
        sed -i '/CONFIG_SOUNDCARD=/c\CONFIG_SOUNDCARD=rpi-bcm2835-hdmi' /boot/dietpi.txt

fi