Thank you everyone for some great examples of how to take control of audio device switching in Windows 7. Here's my take on selecting from 3 devices, with volume OSD and screen sleep also.
Cheers!
Ian MacKenzie
; IM_AudioPlus.ahk May 25, 2011
;
; This is a modification of dan112123's audio device switcher with
; mrfantastic's on-screen volume display added in to create a
; complete device & volume package.
;
; It selects a specific device instead of switching back
; and forth between only two devices. When each device is
; activated, an initial volume and custom volume increment are set.
; The current device and volume increment are written to an .ini file
; for retrieval on startup.
;
; Desired_Device must match be the display name from Sound Playback
; menu, or the device will not be found.
#NoEnv
#SingleInstance force
SetBatchLines -1
COM_Init()
IniRead, Current_Device, AudioPlus_Storage.ini, APS, CD
IniRead, Vol_Increment, AudioPlus_Storage.ini, APS, VI
; -------------------
; START CONFIGURATION
; -------------------
; The amount by which to raise or lower the volume each time
; 1 unit yields 2% change
; Vol_Increment = 1
; How long to display the volume level bar graphs (in milliseconds)
vol_DisplayTime = 1250
; Transparency of window (0-255)
vol_TransValue = 180
; Bar's background colour
vol_CW = 000000
vol_Width = 250 ; width of bar
vol_Thick = 15 ; thickness of bar
; Bar's screen position
vol_PosX := A_ScreenWidth - vol_Width - 30
vol_PosY := A_ScreenHeight - vol_Thick - 100
; --------------------
; END OF CONFIGURATION
; --------------------
vol_BarOptionsMaster = 1:B1 ZH%vol_Thick% ZX8 ZY4 W%vol_Width% X%vol_PosX% Y%vol_PosY% CW%vol_CW%
return
^SC15D::
Run, mmsys.cpl
WinWait,Sound
Return
^Up::
Desired_Device = USB Hifi
Vol_Initial = 92
Vol_Increment = 1
GoSub, Device_Selector
Return
^Down::
Desired_Device = Speakers
Vol_Initial = 40
Vol_Increment = 2
GoSub, Device_Selector
Return
^Left::
Desired_Device = HDMI
Vol_Initial = 90
Vol_Increment = 1
GoSub, Device_Selector
Return
^WheelUp::
SC130::
{
Send {Volume_up %Vol_Increment%}
Gosub, vol_ShowBars
}
return
^WheelDown::
SC12E::
{
Send {Volume_down %Vol_Increment%}
Gosub, vol_ShowBars
}
return
~$Volume_mute::
Gosub, vol_ShowBars
return
;MediaCenter launch button - turns screen off
SC112::
Sleep, 200
SendMessage,0x112,0xF170,2,,Program Manager
return
;~$Volume_up::
;~$Volume_down::
vol_ShowBars:
; Get volumes in case the user or an external program changed them:
Sleep 10
vol_Master := VA_GetMasterVolume()
StringTrimRight, vol_OSD, vol_Master, 4
vol_Mute := VA_GetMasterMute()
if vol_Mute <> 1
{
vol_Colour = White
vol_Text := Current_Device . " " . vol_OSD . "%"
}
else
{
vol_Colour = Red
vol_Text := Current_Device . " (muted)"
}
; To prevent the "flashing" effect, only create the bar window if it doesn't already exist:
IfWinNotExist, VolumeOSDxyz
{
Progress, %vol_BarOptionsMaster% CB%vol_Colour% CT%vol_Colour%, , %vol_Text%, VolumeOSDxyz
WinSet, Transparent, %vol_TransValue%, VolumeOSDxyz
}
Progress, 1:%vol_Master%, , %vol_Text%
SetTimer, vol_BarOff, %vol_DisplayTime%
return
vol_BarOff:
SetTimer, vol_BarOff, off
Progress, 1:Off
return
Device_Selector:
IfWinExist Sound
{
WinKill Sound
}
Run, mmsys.cpl
WinWait,Sound
ControlGet, len, List, Count, SysListView321
Loop %len%
{
ControlSend,SysListView321,{Down}
ControlGet, Device_Info, List, Selected, SysListView321
IfInString, Device_Info, %Desired_Device%
{
ControlClick,&Set Default
Current_Device = %Desired_Device%
VA_SetMasterVolume(Vol_Initial)
IniWrite, %Current_Device%, AudioPlus_Storage.ini, APS, CD
IniWrite, %Vol_Increment%, AudioPlus_Storage.ini, APS, VI
Sleep 1500
ControlClick,OK
WinWaitClose
Return
}
}
MsgBox, The device %Desired_Device% was not found.
Return