;Get audio I/O device change notifications
;coded by rover 2k12
#AutoIt3Wrapper_UseX64=n
#include "BassWASAPI.au3"
#include "Bass.au3"
OnAutoItExitRegister("_Exit")
Global Const $BASS_WASAPI_NOTIFY_DEFINPUT = 2
Global $aDev[11] = ["NETWORKDEVICE", "SPEAKERS", "LINELEVEL", "HEADPHONES", "MICROPHONE", "HEADSET", "HANDSET", "DIGITAL", "SPDIF", "HDMI", "UNKNOWN"]
Global $iCBProc, $aNotify[3], $bRet, $aRet, $sErr = "+ "
$bRet = _BASS_WASAPI_Startup()
If Not $bRet Then $sErr = "! "
ConsoleWrite($sErr & '! _BASS_WASAPI_Startup = ' & $bRet & ' @error: ' & @error & ' @extended code: ' & @extended & @CRLF)
$iCBProc = DllCallbackRegister("_WasapiNotifyProc", "none", "dword;dword;int_ptr")
$bRet = _BASS_WASAPI_SetNotify(DllCallbackGetPtr($iCBProc))
If Not $bRet Then $sErr = "! "
ConsoleWrite($sErr & '! _BASS_WASAPI_SetNotify = ' & $bRet & ' @error: ' & @error & ' @extended code: ' & @extended & @CRLF & @CRLF)
While 1
Sleep(1000)
If $aNotify[0] Then
;ConsoleWrite("!Notify/Device: " & $aNotify[1] &":"& $aNotify[2] & @LF)
If $aNotify[1] = $BASS_WASAPI_NOTIFY_DEFINPUT Then ConsoleWrite('This device is now the Default Output: ' & @CRLF)
$aRet = __BASS_WASAPI_GetdeviceInfo($aNotify[2])
;ConsoleWrite('->Error code: ' & @error & @crlf) ;### Debug Console
If @error Then ContinueLoop
;~ For $i In $aRet
;~ ConsoleWrite("+ " & $i & @LF)
;~ Next
ConsoleWrite('>Device Name: ' & $aRet[0] & @CRLF)
ConsoleWrite('>Device ID : ' & $aRet[1] & @CRLF)
ConsoleWrite('+Device Type: ' & $aDev[$aRet[2]] & @CRLF)
ConsoleWrite('-Device Status: ' & $aRet[3] & @CRLF & @CRLF) ; 3 = BASS_DEVICE_ENABLED = 1 + BASS_DEVICE_DEFAULT = 2
$aNotify[0] = 0
EndIf
WEnd
Func _Exit()
_BASS_WASAPI_SetNotify(0) ;send null to cancel callback
DllCallbackFree($iCBProc)
EndFunc ;==>_Exit
Func _WasapiNotifyProc($Notify, $Device, $User)
;#forceref $Notify, $Device, $User
;callback run in a thread
;can't run functions from here, only check global array in main loop
$aNotify[0] = $User
$aNotify[1] = $Notify
$aNotify[2] = $Device
EndFunc ;==>_WasapiNotifyProc
Func _BASS_WASAPI_SetNotify($pCB = 0, $iUser = 1)
;rover 2k12
Local $BASSWASAPI_ret_ = DllCall($_ghBassWASAPIDll, "int", "BASS_WASAPI_SetNotify", "ptr", $pCB, "int_ptr", $iUser)
If @error Then Return SetError(1, 1, 0) <> 0
If $BASSWASAPI_ret_[0] = 0 Then Return SetError(_BASS_ErrorGetCode(), 0, 0) <> 0
Return SetError(_BASS_ErrorGetCode(), 0, 1) <> 0
EndFunc ;==>_BASS_WASAPI_SetNotify
Func __BASS_WASAPI_GetdeviceInfo($Device)
;rover 2k12
;fixed version of _BASS_WASAPI_GetdeviceInfo from BassWASAPI.au3 RC1 by BrettF
Local Const $tagBASS_WASAPI_DEVICEINFO = "ptr name;" & _
"ptr id;" & _
"dword type;" & _
"dword flags;" & _
"float minperiod;" & _
"float defperiod;" & _
"dword mixfreq;" & _
"dword mixchans;"
Local $aBASS_WASAPI_DEVICEINFO[8]
Local $tBASS_WASAPI_DEVICEINFO = DllStructCreate($tagBASS_WASAPI_DEVICEINFO)
Local $aBASSWASAPI_ret_ = DllCall($_ghBassWASAPIDll, "bool", "BASS_WASAPI_GetDeviceInfo", "dword", $Device, "struct*", $tBASS_WASAPI_DEVICEINFO)
If @error Then Return SetError(1, 1, 0)
If $aBASSWASAPI_ret_[0] = 0 Then Return SetError(_BASS_ErrorGetCode(), 0, 0)
Local $tTxtBuf, $pTxtBuf, $aLen
For $i = 0 To 7
If $i <= 1 Then
$pTxtBuf = DllStructGetData($tBASS_WASAPI_DEVICEINFO, $i + 1)
$aLen = DllCall('kernel32.dll', 'int', 'lstrlenA', 'ptr', $pTxtBuf)
If @error Then ContinueLoop
$tTxtBuf = DllStructCreate("char[" & $aLen[0] + 1 & "]", $pTxtBuf)
$aBASS_WASAPI_DEVICEINFO[$i] = DllStructGetData($tTxtBuf, 1)
Else
$aBASS_WASAPI_DEVICEINFO[$i] = DllStructGetData($tBASS_WASAPI_DEVICEINFO, $i + 1)
EndIf
Next
Return $aBASS_WASAPI_DEVICEINFO
EndFunc ;==>__BASS_WASAPI_GetdeviceInfo
所需库文件 http://www.signa5.com/autoit/BASS/BASS.zip
not test!! |