/src/audio.ahk

http://github.com/adammansfield/my-autohotkey-scripts · AutoHotKey · 117 lines · 88 code · 20 blank · 9 comment · 18 complexity · a8762de2c60c79845360d04a44400b25 MD5 · raw file

  1. ;; Hotkeys for controlling playback and recording audio devices.
  2. <!a::ToggleVolumeState()
  3. <!o::ToggleMicMute()
  4. Volume_Down::SoundSet("-1")
  5. Volume_Up::SoundSet("+1")
  6. ;; Toggles between default audio devices.
  7. ToggleAudioDevice()
  8. {
  9. static kAudioWindowTarget := "mmsys.cpl" ; Target to sound settings.
  10. static kAudioWindowTitle := "ahk_class #32770" ; The window title for the sound settings.
  11. static kAudioListControl := "SysListView321" ; The control name for the playback devices list.
  12. static kAudioSetDefaultControl := "&Set Default" ; The control name for the set default button.
  13. ; Enumeration of audio devices. The number corresponds to the
  14. ; position in the Playback devices list.
  15. static kAudioDeviceExternal := 1
  16. static kAudioDeviceInternal := 2
  17. static current_audio_device := kAudioDeviceExternal
  18. if (kAudioDeviceInternal == current_audio_device)
  19. {
  20. current_audio_device := kAudioDeviceExternal
  21. }
  22. else if (kAudioDeviceExternal == current_audio_device)
  23. {
  24. current_audio_device := kAudioDeviceInternal
  25. }
  26. else
  27. {
  28. MsgBox("Error: audio device """ . current_audio_device . """ is unknown")
  29. current_audio_device := kAudioDeviceDefault
  30. }
  31. Run(kAudioWindowTarget)
  32. err := WinWait(kAudioWindowTitle, 5)
  33. if (!err)
  34. {
  35. select_audio_device := "{Down " . current_audio_device . "}"
  36. ControlSend(kAudioListControl, select_audio_device, kAudioWindowTitle)
  37. ControlClick(kAudioSetDefaultControl, kAudioWindowTitle, "", "", "", "na")
  38. WinClose(kAudioWindowTitle)
  39. }
  40. }
  41. ;; Toggles mute for microphone.
  42. ToggleMicMute()
  43. {
  44. ; The name of the mircophone audio device.
  45. static kAudioMicrophoneDevice := "master:1"
  46. ; +1 will toggle current setting.
  47. SoundSet("+1", kAudioMicrophoneDevice, "mute")
  48. }
  49. ;; Toggles between headphones and speakers volumes states.
  50. ToggleVolumeState()
  51. {
  52. static kSpeakersVolume := 10
  53. static kHeadphonesVolume := 100
  54. static kStateSpeakers := 0
  55. static kStateHeadphones := 1
  56. static audio_state := kStateSpeakers
  57. try
  58. {
  59. ; To be safe assume headphone state if volume is loud.
  60. volume := VA_GetMasterVolume()
  61. if (kSpeakersVolume < volume)
  62. {
  63. audio_state := kStateHeadphones
  64. }
  65. if (kStateHeadphones == audio_state)
  66. {
  67. VA_SetMasterVolume(kSpeakersVolume)
  68. audio_state := kStateSpeakers
  69. }
  70. else if (kStateSpeakers == audio_state)
  71. {
  72. AsyncSpeak("Switch volume")
  73. result := Msgbox("Press ok to switch to headphone volume", 0, "Switch Volume", 2)
  74. if (MsgboxResult.Ok == result)
  75. {
  76. VA_SetMasterVolume(kHeadphonesVolume)
  77. audio_state := kStateHeadphones
  78. }
  79. else if (MsgboxResult.Timeout == result)
  80. {
  81. AsyncSpeak("Switch timed out")
  82. }
  83. else
  84. {
  85. throw Exception("Error: result of Msgbox() """ . result """ is unexpected")
  86. }
  87. }
  88. else
  89. {
  90. throw Exception("Error: audio state """ . audio_state . """ is unknown")
  91. }
  92. }
  93. catch e
  94. {
  95. HandleException(e)
  96. }
  97. }