Advertisement
Guest User

Untitled

a guest
Dec 5th, 2014
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.40 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. #
  4. # If you have a computer with two independent audio devices (e.g.
  5. # a laptop with embedded microphone and speaker and a USB headset)
  6. # and you'd like the volume control (volume up, down and mute) to
  7. # automatically switch to one device when it's plugged, this
  8. # script will do so.
  9. #
  10. # This script only controls playback (not the capture device).
  11. #
  12. # Version: 1.0
  13. #
  14. # References
  15. # ==========
  16. # o An excellent qdbus tutorial:
  17. #
  18. # http://usrlocalbin.blogspot.ca/2008/04/qdbus-tutorial-part-one.html
  19. #
  20. # Glossary/Terms
  21. # ==============
  22. # o Default Device - the computer's (e.g. laptop) speakers. Normally
  23. # controlled by Volume Up/Down/Mute.
  24. #
  25. # o Preferred Device - a secondary device (e.g. USB headset) which,
  26. # when connected, you'd like to control via
  27. # Volume Up/Down/Mute.
  28. #
  29. # Variables to set in this Script
  30. # ===============================
  31. # Mixer variables
  32. # ---------------
  33. # PREFERRED_MIXER
  34. # DEFAULT_MIXER
  35. #
  36. # Device variables
  37. # ----------------
  38. # PREFERRED_DEVICE
  39. # DEFAULT_DEVICE
  40. #
  41. # Preferred Card's /unique/ Identification
  42. # PREFERRED_CARD
  43. #
  44. # Instructions
  45. # ============
  46. # Prerequisite
  47. # ------------
  48. # o kmix
  49. # o The `preferred device' is connected to your machine (e.g.
  50. # USB headset)
  51. #
  52. # One-time setup
  53. # --------------
  54. # Ensure your preferred device has the correct priority within KDE:
  55. #
  56. # o System Settings > Multimedia > Phonon
  57. # o Under the `Device Preference' tab, set the priority for your device.
  58. #
  59. # Determine the Default Mixer and Device
  60. # --------------------------------------
  61. # o kmix > Playback Devices
  62. #
  63. # Both devices /must/ be listed. If they're not listed, stop.
  64. #
  65. # o Settings > Select Master Channel ... > Current mixer: Playback Devices,
  66. # and ensure the default device is selected and if not, select it. Click
  67. # `OK' to exit.
  68. #
  69. # For example, the default device may be `Internal Audio Analog Stereo'
  70. #
  71. # o Set the DEFAULT_MIXER variable:
  72. #
  73. # Open a terminal window and fetch the `current Master Mixer' value
  74. # using `qdbus' and set the DEFAULT_MIXER variable:
  75. #
  76. # $ qdbus org.kde.kmix /Mixers org.kde.KMix.MixSet.currentMasterMixer
  77. #
  78. # o Set the DEFAULT_DEVICE variable using the result from:
  79. #
  80. # $ qdbus org.kde.kmix /Mixers org.kde.KMix.MixSet.currentMasterControl
  81. #
  82. # Determine the Preferred Mixer and Device
  83. # ----------------------------------------
  84. # o kmix > Playback Devices
  85. #
  86. # Both devices /must/ be listed. If they're not listed, stop.
  87. #
  88. # o Settings > Select Master Channel ... > Current mixer: Playback Devices,
  89. # and select the `other' device (e.g. USB headset)
  90. #
  91. # o Set the PREFERRED_MIXER variable using the result from:
  92. #
  93. # $ qdbus org.kde.kmix /Mixers org.kde.KMix.MixSet.currentMasterMixer
  94. #
  95. # o Set the PREFERRED_DEVICE variable using the result from:
  96. #
  97. # $ qdbus org.kde.kmix /Mixers org.kde.KMix.MixSet.currentMasterControl
  98. #
  99. # Setting PREFERRED_CARD
  100. # ----------------------
  101. # This script checks /proc/asound/cards for the existence of the
  102. # value specified by $PREFERRED_CARD It's important that the value
  103. # entered is unique; that it's not found in the value of the default
  104. # device.
  105. #
  106. # To determine a value, type the following:
  107. #
  108. # $ fgrep '[' /proc/asound/cards
  109. #
  110. # On my machine, I get the following values:
  111. #
  112. # 0 [Intel ]: HDA-Intel - HDA Intel
  113. # 1 [H760 ]: USB-Audio - Logitech Wireless Headset H760
  114. #
  115. # Given the above, as I want my Logitech to be the preferred device, I
  116. # set PREFERRED_CARD="H760"
  117. #
  118.  
  119. # Running the script
  120. # ------------------
  121. # Place this script, say, in ~/bin and have the script run when you log in:
  122. #
  123. # System Settings > Startup and Shutdown > Autostart
  124. #
  125. # --- Configuration
  126. #
  127.  
  128. #
  129. # Mixer
  130. #
  131. PREFERRED_MIXER="PulseAudio::Playback_Devices:1"
  132. DEFAULT_MIXER="PulseAudio::Playback_Devices:1"
  133.  
  134. #
  135. # Device
  136. #
  137. PREFERRED_DEVICE="alsa_output.usb-GYROCOM_C_C_Co.__LTD_Audiotrak_ProDigy_CUBE-01-CUBE.analog-stereo"
  138. DEFAULT_DEVICE="alsa_output.pci-0000_00_1b.0.analog-stereo"
  139.  
  140. #
  141. # Card identifier
  142. #
  143. PREFERRED_CARD="CUBE"
  144.  
  145. #
  146. # The number of seconds between checks. The greater the number, the longer it'll
  147. # take for us to detect a change. :)
  148. #
  149. POLLING_FREQUENCY=3
  150.  
  151. #
  152. # --- End of Configuration
  153. #
  154.  
  155. #
  156. # Main
  157. #
  158.  
  159. #
  160. # Loop forever
  161. #
  162. while [ 1 ] ; do
  163. CURRENT_MIXER=`qdbus org.kde.kmix /Mixers org.kde.KMix.MixSet.currentMasterMixer`
  164. CURRENT_DEVICE=`qdbus org.kde.kmix /Mixers org.kde.KMix.MixSet.currentMasterControl`
  165.  
  166. #
  167. # For debugging, it's easier if we not call fgrep within the if
  168. #
  169. fgrep '[' /proc/asound/cards | fgrep $PREFERRED_CARD 2>&1 >/dev/null
  170. CARD_INSTALLED=$?
  171.  
  172. if [ $CARD_INSTALLED -eq 0 ] ; then
  173. if [ "$CURRENT_MIXER" != "$PREFERRED_MIXER" -o "$CURRENT_DEVICE" != "$PREFERRED_DEVICE" ] ; then
  174. echo "`date` - Setting preferred device"
  175. qdbus org.kde.kmix /Mixers org.kde.KMix.MixSet.setCurrentMaster "$PREFERRED_MIXER" "$PREFERRED_DEVICE" 2>&1 > /dev/null
  176. fi
  177. else
  178. if [ "$CURRENT_MIXER" != "$DEFAULT_MIXER" -o "$CURRENT_DEVICE" != "$DEFAULT_DEVICE" ] ; then
  179. echo "`date` - Setting default device"
  180. qdbus org.kde.kmix /Mixers org.kde.KMix.MixSet.setCurrentMaster "$DEFAULT_MIXER" "$DEFAULT_DEVICE" 2>&1 > /dev/null
  181. fi
  182. fi
  183. sleep $POLLING_FREQUENCY
  184. done
  185.  
  186. #
  187. # Never reached
  188. #
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement