
#Persistent  ; Keep the script running until the user exits it.
Menu, Tray, Tip , Default Audio Device Changer
Menu, Tray, NoStandard

Menu, Tray, Add, Info, InfoMenu
Menu, Tray, Add, Exit, MenuExit

device1:="Headset"
device2:="Lautsprecher"

versionnumber := "1.1"

if not A_IsAdmin
{
   msgbox, 48,Administrator Rights Warning, Please run as Admin.
   sleep, 250
   exitapp
}


installed_in=%A_ScriptDir%
IfExist, %installed_in%\hyperaudio.ini
{
    iniread, device1, %installed_in%\hyperaudio.ini, Devices, Output1, %device1%
    iniread, device2, %installed_in%\hyperaudio.ini, Devices, Output2, %device2%
} else {
    iniwrite, %device1%, %installed_in%\hyperaudio.ini, Devices, Output1
    iniwrite, %device2%, %installed_in%\hyperaudio.ini, Devices, Output2
}


; http://www.daveamenta.com/2011-05/programmatically-or-command-line-change-the-default-sound-playback-device-in-windows-7/
Devices := {}
IMMDeviceEnumerator := ComObjCreate("{BCDE0395-E52F-467C-8E3D-C4579291692E}", "{A95664D2-9614-4F35-A746-DE8DB63617E6}")

; IMMDeviceEnumerator::EnumAudioEndpoints
; eRender = 0, eCapture, eAll
; 0x1 = DEVICE_STATE_ACTIVE
DllCall(NumGet(NumGet(IMMDeviceEnumerator+0)+3*A_PtrSize), "UPtr", IMMDeviceEnumerator, "UInt", 0, "UInt", 0x1, "UPtrP", IMMDeviceCollection, "UInt")
ObjRelease(IMMDeviceEnumerator)

; IMMDeviceCollection::GetCount
DllCall(NumGet(NumGet(IMMDeviceCollection+0)+3*A_PtrSize), "UPtr", IMMDeviceCollection, "UIntP", Count, "UInt")
Loop % (Count)
{
    ; IMMDeviceCollection::Item
    DllCall(NumGet(NumGet(IMMDeviceCollection+0)+4*A_PtrSize), "UPtr", IMMDeviceCollection, "UInt", A_Index-1, "UPtrP", IMMDevice, "UInt")

    ; IMMDevice::GetId
    DllCall(NumGet(NumGet(IMMDevice+0)+5*A_PtrSize), "UPtr", IMMDevice, "UPtrP", pBuffer, "UInt")
    DeviceID := StrGet(pBuffer, "UTF-16"), DllCall("Ole32.dll\CoTaskMemFree", "UPtr", pBuffer)

    ; IMMDevice::OpenPropertyStore
    ; 0x0 = STGM_READ
    DllCall(NumGet(NumGet(IMMDevice+0)+4*A_PtrSize), "UPtr", IMMDevice, "UInt", 0x0, "UPtrP", IPropertyStore, "UInt")
    ObjRelease(IMMDevice)

    ; IPropertyStore::GetValue
    VarSetCapacity(PROPVARIANT, A_PtrSize == 4 ? 16 : 24)
    VarSetCapacity(PROPERTYKEY, 20)
    DllCall("Ole32.dll\CLSIDFromString", "Str", "{A45C254E-DF1C-4EFD-8020-67D146A850E0}", "UPtr", &PROPERTYKEY)
    NumPut(14, &PROPERTYKEY + 16, "UInt")
    DllCall(NumGet(NumGet(IPropertyStore+0)+5*A_PtrSize), "UPtr", IPropertyStore, "UPtr", &PROPERTYKEY, "UPtr", &PROPVARIANT, "UInt")
    DeviceName := StrGet(NumGet(&PROPVARIANT + 8), "UTF-16")    ; LPWSTR PROPVARIANT.pwszVal
    DllCall("Ole32.dll\CoTaskMemFree", "UPtr", NumGet(&PROPVARIANT + 8))    ; LPWSTR PROPVARIANT.pwszVal
    ObjRelease(IPropertyStore)

    ObjRawSet(Devices, DeviceName, DeviceID)
}
ObjRelease(IMMDeviceCollection)
Return

$!WheelUp::Send {Volume_Up 5}
$!WheelDown::Send {Volume_Down 5}
currentDevice:=false
^F12:: 
currentDevice:=!currentDevice
if currentDevice
    SetDefaultEndpoint( GetDeviceID(Devices, device1) )
else
    SetDefaultEndpoint( GetDeviceID(Devices, device2) )
return

SetDefaultEndpoint(DeviceID)
{
    IPolicyConfig := ComObjCreate("{870af99c-171d-4f9e-af0d-e63df40c2bc9}", "{F8679F50-850A-41CF-9C72-430F290290C8}")
    DllCall(NumGet(NumGet(IPolicyConfig+0)+13*A_PtrSize), "UPtr", IPolicyConfig, "UPtr", &DeviceID, "UInt", 0, "UInt")
    ObjRelease(IPolicyConfig)
}

GetDeviceID(Devices, Name)
{
    For DeviceName, DeviceID in Devices
    If (InStr(DeviceName, Name))
        Return DeviceID
}

MenuExit:
    {
        ExitApp
    }
    return

InfoMenu:
    {
        Gui, 2:+AlwaysOnTop +Caption +MinimizeBox +ToolWindow ; +SysMenu

        Gui, 2:Font, s20, Impact
        Gui, 2:Add, Text, x15 y20 w280 r1 , HyperAudio, 2021

        Gui, 2:Font, s8, Lucida Sans Unicode
        Gui, 2:Add, Button, x255 y190 w50 h30 gInfoclose vInfoclose, OK
        Gui, 2:Add, Text, x15 y60 w200 +Left, Alt+WheelUp:`nAlt+WheelDown:
        Gui, 2:Add, Text, x105 y60 w200 +Left, Increase Volume +5`nDecrease Volume +5


        Gui, 2:Add, Text, x15 y100 w200 +Left, Ctrl+F12
        Gui, 2:Add, Text, x105 y100 w200 +Left, Toggle Audio Output Device`n(Configure Device Names in ini File)

        Gui, 2:Add, Text, x15 y150 w200 +Left, Version: %versionnumber%
        Gui, 2:Add, Text, x15 y175 w200 +Left, Shimao-Blackrock

        Gui, 2:Show, h235 w320, HyperAudio
    }
    return

Infoclose:
    {
        Gui, 2:Destroy
    }
    return
