Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- #
- # If you have a computer with two independent audio devices (e.g.
- # a laptop with embedded microphone and speaker and a USB headset)
- # and you'd like the volume control (volume up, down and mute) to
- # automatically switch to one device when it's plugged, this
- # script will do so.
- #
- # This script only controls playback (not the capture device).
- #
- # Version: 1.0
- # Author: [email protected]
- #
- # References
- # ==========
- # o An excellent qdbus tutorial:
- #
- # http://usrlocalbin.blogspot.ca/2008/04/qdbus-tutorial-part-one.html
- #
- # Glossary/Terms
- # ==============
- # o Default Device - the computer's (e.g. laptop) speakers. Normally
- # controlled by Volume Up/Down/Mute.
- #
- # o Preferred Device - a secondary device (e.g. USB headset) which,
- # when connected, you'd like to control via
- # Volume Up/Down/Mute.
- #
- # Variables to set in this Script
- # ===============================
- # Mixer variables
- # ---------------
- # PREFERRED_MIXER
- # DEFAULT_MIXER
- #
- # Device variables
- # ----------------
- # PREFERRED_DEVICE
- # DEFAULT_DEVICE
- #
- # Preferred Card's /unique/ Identification
- # PREFERRED_CARD
- #
- # Instructions
- # ============
- # Prerequisite
- # ------------
- # o kmix
- # o The `preferred device' is connected to your machine (e.g.
- # USB headset)
- #
- # One-time setup
- # --------------
- # Ensure your preferred device has the correct priority within KDE:
- #
- # o System Settings > Multimedia > Phonon
- # o Under the `Device Preference' tab, set the priority for your device.
- #
- # Determine the Default Mixer and Device
- # --------------------------------------
- # o kmix > Playback Devices
- #
- # Both devices /must/ be listed. If they're not listed, stop.
- #
- # o Settings > Select Master Channel ... > Current mixer: Playback Devices,
- # and ensure the default device is selected and if not, select it. Click
- # `OK' to exit.
- #
- # For example, the default device may be `Internal Audio Analog Stereo'
- #
- # o Set the DEFAULT_MIXER variable:
- #
- # Open a terminal window and fetch the `current Master Mixer' value
- # using `qdbus' and set the DEFAULT_MIXER variable:
- #
- # $ qdbus org.kde.kmix /Mixers org.kde.KMix.MixSet.currentMasterMixer
- #
- # o Set the DEFAULT_DEVICE variable using the result from:
- #
- # $ qdbus org.kde.kmix /Mixers org.kde.KMix.MixSet.currentMasterControl
- #
- # Determine the Preferred Mixer and Device
- # ----------------------------------------
- # o kmix > Playback Devices
- #
- # Both devices /must/ be listed. If they're not listed, stop.
- #
- # o Settings > Select Master Channel ... > Current mixer: Playback Devices,
- # and select the `other' device (e.g. USB headset)
- #
- # o Set the PREFERRED_MIXER variable using the result from:
- #
- # $ qdbus org.kde.kmix /Mixers org.kde.KMix.MixSet.currentMasterMixer
- #
- # o Set the PREFERRED_DEVICE variable using the result from:
- #
- # $ qdbus org.kde.kmix /Mixers org.kde.KMix.MixSet.currentMasterControl
- #
- # Setting PREFERRED_CARD
- # ----------------------
- # This script checks /proc/asound/cards for the existence of the
- # value specified by $PREFERRED_CARD It's important that the value
- # entered is unique; that it's not found in the value of the default
- # device.
- #
- # To determine a value, type the following:
- #
- # $ fgrep '[' /proc/asound/cards
- #
- # On my machine, I get the following values:
- #
- # 0 [Intel ]: HDA-Intel - HDA Intel
- # 1 [H760 ]: USB-Audio - Logitech Wireless Headset H760
- #
- # Given the above, as I want my Logitech to be the preferred device, I
- # set PREFERRED_CARD="H760"
- #
- # Running the script
- # ------------------
- # Place this script, say, in ~/bin and have the script run when you log in:
- #
- # System Settings > Startup and Shutdown > Autostart
- #
- # --- Configuration
- #
- #
- # Mixer
- #
- PREFERRED_MIXER="PulseAudio::Playback_Devices:1"
- DEFAULT_MIXER="PulseAudio::Playback_Devices:1"
- #
- # Device
- #
- PREFERRED_DEVICE="alsa_output.usb-GYROCOM_C_C_Co.__LTD_Audiotrak_ProDigy_CUBE-01-CUBE.analog-stereo"
- DEFAULT_DEVICE="alsa_output.pci-0000_00_1b.0.analog-stereo"
- #
- # Card identifier
- #
- PREFERRED_CARD="CUBE"
- #
- # The number of seconds between checks. The greater the number, the longer it'll
- # take for us to detect a change. :)
- #
- POLLING_FREQUENCY=3
- #
- # --- End of Configuration
- #
- #
- # Main
- #
- #
- # Loop forever
- #
- while [ 1 ] ; do
- CURRENT_MIXER=`qdbus org.kde.kmix /Mixers org.kde.KMix.MixSet.currentMasterMixer`
- CURRENT_DEVICE=`qdbus org.kde.kmix /Mixers org.kde.KMix.MixSet.currentMasterControl`
- #
- # For debugging, it's easier if we not call fgrep within the if
- #
- fgrep '[' /proc/asound/cards | fgrep $PREFERRED_CARD 2>&1 >/dev/null
- CARD_INSTALLED=$?
- if [ $CARD_INSTALLED -eq 0 ] ; then
- if [ "$CURRENT_MIXER" != "$PREFERRED_MIXER" -o "$CURRENT_DEVICE" != "$PREFERRED_DEVICE" ] ; then
- echo "`date` - Setting preferred device"
- qdbus org.kde.kmix /Mixers org.kde.KMix.MixSet.setCurrentMaster "$PREFERRED_MIXER" "$PREFERRED_DEVICE" 2>&1 > /dev/null
- fi
- else
- if [ "$CURRENT_MIXER" != "$DEFAULT_MIXER" -o "$CURRENT_DEVICE" != "$DEFAULT_DEVICE" ] ; then
- echo "`date` - Setting default device"
- qdbus org.kde.kmix /Mixers org.kde.KMix.MixSet.setCurrentMaster "$DEFAULT_MIXER" "$DEFAULT_DEVICE" 2>&1 > /dev/null
- fi
- fi
- sleep $POLLING_FREQUENCY
- done
- #
- # Never reached
- #
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement