Skip to content

Instantly share code, notes, and snippets.

@Sothatsit
Created July 30, 2021 06:43
Show Gist options
  • Save Sothatsit/e225386e5084d4b37e31c505c82606cf to your computer and use it in GitHub Desktop.
Save Sothatsit/e225386e5084d4b37e31c505c82606cf to your computer and use it in GitHub Desktop.
Reset all USB devices, and move all dangling audio streams to re-connected external Schitt Modi DAC.
#!/bin/bash
if [[ $EUID != 0 ]] ; then
echo This must be run as root!
exit 1
fi
# Reset USB devices.
for xhci in /sys/bus/pci/drivers/?hci_hcd ; do
if ! cd $xhci ; then
echo Weird error. Failed to change directory to $xhci
exit 1
fi
echo Resetting devices from $xhci...
for i in ????:??:??.? ; do
if [[ "$i" != "????:??:??.?" ]]; then
echo " .. resetting $i"
echo -n "$i" > unbind
echo -n "$i" > bind
fi
done
done
echo ""
# Wait for the Schitt Modi to be recognised as an audio output.
newSink="alsa_output.usb-Schiit_Audio_Schiit_Modi_3-00.analog-stereo"
while ! sudo -u "#1000" XDG_RUNTIME_DIR=/run/user/1000 pactl list short sinks | grep "$newSink"; do
echo "Waiting for Schitt Modi..."
sleep 1
done
echo ""
# Switch to Schitt Modi audio output.
sudo -u "#1000" XDG_RUNTIME_DIR=/run/user/1000 pactl set-default-sink "$newSink"
sudo -u "#1000" XDG_RUNTIME_DIR=/run/user/1000 pactl list short sink-inputs|while read stream; do
streamId=$(echo $stream|cut '-d ' -f1)
echo "moving stream $streamId"
sudo -u "#1000" XDG_RUNTIME_DIR=/run/user/1000 pactl move-sink-input "$streamId" "$newSink"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment