soundDetect.au3 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. #include <WinAPICom.au3>
  2. #include <Process.au3>
  3. #include <Array.au3>
  4. Opt("MustDeclareVars", 1)
  5. Global Const $CLSCTX_INPROC_SERVER = 0x01 + 0x02 + 0x04 + 0x10
  6. Global Enum $eRender, $eCapture, $eAll, $EDataFlow_enum_count
  7. Global Enum $AudioSessionStateInactive, $AudioSessionStateActive, $AudioSessionStateExpired
  8. Global Const $eMultimedia = 1
  9. Global Const $sCLSID_MMDeviceEnumerator = "{BCDE0395-E52F-467C-8E3D-C4579291692E}"
  10. Global Const $sIID_IMMDeviceEnumerator = "{A95664D2-9614-4F35-A746-DE8DB63617E6}"
  11. Global Const $sTagIMMDeviceEnumerator = _
  12. "EnumAudioEndpoints hresult(int;dword;ptr*);" & _
  13. "GetDefaultAudioEndpoint hresult(int;int;ptr*);" & _
  14. "GetDevice hresult(wstr;ptr*);" & _
  15. "RegisterEndpointNotificationCallback hresult(ptr);" & _
  16. "UnregisterEndpointNotificationCallback hresult(ptr)"
  17. Global Const $sIID_IAudioMeterInformation = "{C02216F6-8C67-4B5B-9D00-D008E73E0064}"
  18. Global Const $sTagIAudioMeterInformation = "GetPeakValue hresult(float*);" & _
  19. "GetMeteringChannelCount hresult(dword*);" & _
  20. "GetChannelsPeakValues hresult(dword;float*);" & _
  21. "QueryHardwareSupport hresult(dword*);"
  22. Global Const $sIID_IMMDevice = "{D666063F-1587-4E43-81F1-B948E807363F}"
  23. Global Const $sTagIMMDevice = _
  24. "Activate hresult(clsid;dword;ptr;ptr*);" & _
  25. "OpenPropertyStore hresult(dword;ptr*);" & _
  26. "GetId hresult(wstr*);" & _
  27. "GetState hresult(dword*)"
  28. Global Const $sIID_IAudioSessionManager2 = "{77aa99a0-1bd6-484f-8bc7-2c654c9a9b6f}"
  29. Global Const $sTagIAudioSessionManager = "GetAudioSessionControl hresult(ptr;dword;ptr*);" & _
  30. "GetSimpleAudioVolume hresult(ptr;dword;ptr*);"
  31. Global Const $sTagIAudioSessionManager2 = $sTagIAudioSessionManager & "GetSessionEnumerator hresult(ptr*);" & _
  32. "RegisterSessionNotification hresult(ptr);" & _
  33. "UnregisterSessionNotification hresult(ptr);" & _
  34. "RegisterDuckNotification hresult(wstr;ptr);" & _
  35. "UnregisterDuckNotification hresult(ptr)"
  36. Global Const $sIID_IAudioSessionEnumerator = "{e2f5bb11-0570-40ca-acdd-3aa01277dee8}"
  37. Global Const $sTagIAudioSessionEnumerator = "GetCount hresult(int*);GetSession hresult(int;ptr*)"
  38. Global Const $sIID_IAudioSessionControl = "{f4b1a599-7266-4319-a8ca-e70acb11e8cd}"
  39. Global Const $sTagIAudioSessionControl = "GetState hresult(int*);GetDisplayName hresult(wstr*);" & _
  40. "SetDisplayName hresult(wstr);GetIconPath hresult(wstr*);" & _
  41. "SetIconPath hresult(wstr;ptr);GetGroupingParam hresult(ptr*);" & _
  42. "SetGroupingParam hresult(ptr;ptr);RegisterAudioSessionNotification hresult(ptr);" & _
  43. "UnregisterAudioSessionNotification hresult(ptr);"
  44. Global Const $sIID_IAudioSessionControl2 = "{bfb7ff88-7239-4fc9-8fa2-07c950be9c6d}"
  45. Global Const $sTagIAudioSessionControl2 = $sTagIAudioSessionControl & "GetSessionIdentifier hresult(wstr*);" & _
  46. "GetSessionInstanceIdentifier hresult(wstr*);" & _
  47. "GetProcessId hresult(dword*);IsSystemSoundsSession hresult();" & _
  48. "SetDuckingPreferences hresult(bool);"
  49. Global $bExit= not False
  50. _WinAPI_CoInitialize()
  51. Local $aApp =0
  52. Func _Exit()
  53. $bExit=False
  54. EndFunc
  55. Func _GetAppsPlayingSound()
  56. Local $pIMMDevice = 0
  57. Local $oMMDevice = 0
  58. Local $pIAudioSessionManager2 = 0
  59. Local $oIAudioSessionManager2 = 0
  60. Local $pIAudioSessionEnumerator = 0
  61. Local $oIAudioSessionEnumerator = 0
  62. Local $nSessions = 0
  63. Local $oMMDeviceEnumerator = 0
  64. Local $aApp[1][1]
  65. Local $pIAudioSessionControl2 = 0
  66. Local $oIAudioSessionControl2 = 0
  67. Local $oIAudioMeterInformation = 0
  68. Local $ProcessID = 0
  69. Local $fPeakValue = 0
  70. Local $iState = 0
  71. Local $oErrorHandler = 0
  72. $oErrorHandler = ObjEvent("AutoIt.Error", "_ErrFunc")
  73. $oMMDeviceEnumerator = ObjCreateInterface($sCLSID_MMDeviceEnumerator, $sIID_IMMDeviceEnumerator, $sTagIMMDeviceEnumerator)
  74. If @error Then Return $aApp
  75. If SUCCEEDED($oMMDeviceEnumerator.GetDefaultAudioEndpoint($eRender, $eMultimedia, $pIMMDevice)) Then ;eRender
  76. $oMMDevice = ObjCreateInterface($pIMMDevice, $sIID_IMMDevice, $sTagIMMDevice)
  77. $oMMDevice.Activate($sIID_IAudioSessionManager2, $CLSCTX_INPROC_SERVER, 0, $pIAudioSessionManager2)
  78. $oIAudioSessionManager2 = ObjCreateInterface($pIAudioSessionManager2, $sIID_IAudioSessionManager2, $sTagIAudioSessionManager2)
  79. $oIAudioSessionManager2.GetSessionEnumerator($pIAudioSessionEnumerator)
  80. $oIAudioSessionEnumerator = ObjCreateInterface($pIAudioSessionEnumerator, $sIID_IAudioSessionEnumerator, $sTagIAudioSessionEnumerator)
  81. $oIAudioSessionEnumerator.GetCount($nSessions)
  82. For $i = 0 To $nSessions - 1
  83. $oIAudioSessionEnumerator.GetSession($i, $pIAudioSessionControl2)
  84. $oIAudioSessionControl2 = ObjCreateInterface($pIAudioSessionControl2, $sIID_IAudioSessionControl2, $sTagIAudioSessionControl2)
  85. $oIAudioSessionControl2.GetState($iState)
  86. If $iState = $AudioSessionStateActive Then
  87. $oIAudioSessionControl2.GetProcessId($ProcessID)
  88. $oIAudioMeterInformation = ObjCreateInterface($pIAudioSessionControl2, $sIID_IAudioMeterInformation, $sTagIAudioMeterInformation)
  89. $oIAudioSessionControl2.AddRef
  90. $oIAudioMeterInformation.GetPeakValue($fPeakValue)
  91. If $fPeakValue > 0 Then
  92. ReDim $aApp[UBound($aApp) + 1][2]
  93. $aApp[UBound($aApp) - 1][0] = _ProcessGetName($ProcessID)
  94. $aApp[UBound($aApp) - 1][1] = $fPeakValue
  95. EndIf
  96. EndIf
  97. $fPeakValue = 0
  98. $iState = 0
  99. $ProcessID = 0
  100. $oIAudioMeterInformation = 0
  101. $oIAudioSessionControl2 = 0
  102. Next
  103. $oIAudioSessionEnumerator = 0
  104. $oIAudioSessionManager2 = 0
  105. $oMMDevice = 0
  106. $oMMDeviceEnumerator = 0
  107. If UBound($aApp) = 0 Then $aApp = 0
  108. Return $aApp
  109. Else
  110. Return 0
  111. EndIf
  112. EndFunc ;==>_GetAppsPlayingSound
  113. Func SUCCEEDED($hr)
  114. Return ($hr >= 0)
  115. EndFunc ;==>SUCCEEDED
  116. ; User's COM error function. Will be called if COM error occurs
  117. Func _ErrFunc($oError)
  118. ; Do anything here.
  119. ConsoleWrite(@ScriptName & " (" & $oError.scriptline & ") : ==> COM Error intercepted !" & @CRLF & _
  120. @TAB & "err.number is: " & @TAB & @TAB & "0x" & Hex($oError.number) & @CRLF & _
  121. @TAB & "err.windescription:" & @TAB & $oError.windescription & @CRLF & _
  122. @TAB & "err.description is: " & @TAB & $oError.description & @CRLF & _
  123. @TAB & "err.source is: " & @TAB & @TAB & $oError.source & @CRLF & _
  124. @TAB & "err.helpfile is: " & @TAB & $oError.helpfile & @CRLF & _
  125. @TAB & "err.helpcontext is: " & @TAB & $oError.helpcontext & @CRLF & _
  126. @TAB & "err.lastdllerror is: " & @TAB & $oError.lastdllerror & @CRLF & _
  127. @TAB & "err.scriptline is: " & @TAB & $oError.scriptline & @CRLF & _
  128. @TAB & "err.retcode is: " & @TAB & "0x" & Hex($oError.retcode) & @CRLF & @CRLF)
  129. EndFunc ;==>_ErrFunc