soundDetect.au3 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. HotKeySet("{F5}","_Exit")
  51. _WinAPI_CoInitialize()
  52. Local $aApp =0
  53. While $bExit
  54. $aApp = _GetAppsPlayingSound()
  55. If (UBound($aApp)>1) then
  56. For $i= 0 to UBound($aApp)-1
  57. ConsoleWrite(Int($aApp[$i][1]*100) & " - " & $aApp[$i][0] & @CRLF)
  58. ControlSetText('', '', 'Scintilla2', '')
  59. Next
  60. EndIf
  61. Sleep(100)
  62. WEnd
  63. _WinAPI_CoUninitialize()
  64. Func _Exit()
  65. $bExit=False
  66. EndFunc
  67. Func _GetAppsPlayingSound()
  68. Local $pIMMDevice = 0
  69. Local $oMMDevice = 0
  70. Local $pIAudioSessionManager2 = 0
  71. Local $oIAudioSessionManager2 = 0
  72. Local $pIAudioSessionEnumerator = 0
  73. Local $oIAudioSessionEnumerator = 0
  74. Local $nSessions = 0
  75. Local $oMMDeviceEnumerator = 0
  76. Local $aApp[1][1]
  77. Local $pIAudioSessionControl2 = 0
  78. Local $oIAudioSessionControl2 = 0
  79. Local $oIAudioMeterInformation = 0
  80. Local $ProcessID = 0
  81. Local $fPeakValue = 0
  82. Local $iState = 0
  83. Local $iVolume = 10
  84. Local $oErrorHandler = 0
  85. $oErrorHandler = ObjEvent("AutoIt.Error", "_ErrFunc")
  86. $oMMDeviceEnumerator = ObjCreateInterface($sCLSID_MMDeviceEnumerator, $sIID_IMMDeviceEnumerator, $sTagIMMDeviceEnumerator)
  87. If @error Then Return $aApp
  88. If SUCCEEDED($oMMDeviceEnumerator.GetDefaultAudioEndpoint($eRender, $eMultimedia, $pIMMDevice)) Then ;eRender
  89. $oMMDevice = ObjCreateInterface($pIMMDevice, $sIID_IMMDevice, $sTagIMMDevice)
  90. $oMMDevice.Activate($sIID_IAudioSessionManager2, $CLSCTX_INPROC_SERVER, 0, $pIAudioSessionManager2)
  91. $oIAudioSessionManager2 = ObjCreateInterface($pIAudioSessionManager2, $sIID_IAudioSessionManager2, $sTagIAudioSessionManager2)
  92. $oIAudioSessionManager2.GetSessionEnumerator($pIAudioSessionEnumerator)
  93. $oIAudioSessionEnumerator = ObjCreateInterface($pIAudioSessionEnumerator, $sIID_IAudioSessionEnumerator, $sTagIAudioSessionEnumerator)
  94. $oIAudioSessionEnumerator.GetCount($nSessions)
  95. For $i = 0 To $nSessions - 1
  96. $oIAudioSessionEnumerator.GetSession($i, $pIAudioSessionControl2)
  97. $oIAudioSessionControl2 = ObjCreateInterface($pIAudioSessionControl2, $sIID_IAudioSessionControl2, $sTagIAudioSessionControl2)
  98. $oIAudioSessionControl2.GetState($iState)
  99. If $iState = $AudioSessionStateActive Then
  100. $oIAudioSessionControl2.GetProcessId($ProcessID)
  101. $oIAudioMeterInformation = ObjCreateInterface($pIAudioSessionControl2, $sIID_IAudioMeterInformation, $sTagIAudioMeterInformation)
  102. $oIAudioSessionControl2.AddRef
  103. $oIAudioMeterInformation.GetPeakValue($fPeakValue)
  104. If $fPeakValue > 0 Then
  105. ReDim $aApp[UBound($aApp) + 1][2]
  106. $aApp[UBound($aApp) - 1][0] = _ProcessGetName($ProcessID)
  107. $aApp[UBound($aApp) - 1][1] = $fPeakValue
  108. EndIf
  109. EndIf
  110. $fPeakValue = 0
  111. $iState = 0
  112. $ProcessID = 0
  113. $oIAudioMeterInformation = 0
  114. $oIAudioSessionControl2 = 0
  115. Next
  116. $oIAudioSessionEnumerator = 0
  117. $oIAudioSessionManager2 = 0
  118. $oMMDevice = 0
  119. $oMMDeviceEnumerator = 0
  120. If UBound($aApp) = 0 Then $aApp = 0
  121. Return $aApp
  122. Else
  123. Return 0
  124. EndIf
  125. EndFunc ;==>_GetAppsPlayingSound
  126. Func SUCCEEDED($hr)
  127. Return ($hr >= 0)
  128. EndFunc ;==>SUCCEEDED
  129. ; User's COM error function. Will be called if COM error occurs
  130. Func _ErrFunc($oError)
  131. ; Do anything here.
  132. ConsoleWrite(@ScriptName & " (" & $oError.scriptline & ") : ==> COM Error intercepted !" & @CRLF & _
  133. @TAB & "err.number is: " & @TAB & @TAB & "0x" & Hex($oError.number) & @CRLF & _
  134. @TAB & "err.windescription:" & @TAB & $oError.windescription & @CRLF & _
  135. @TAB & "err.description is: " & @TAB & $oError.description & @CRLF & _
  136. @TAB & "err.source is: " & @TAB & @TAB & $oError.source & @CRLF & _
  137. @TAB & "err.helpfile is: " & @TAB & $oError.helpfile & @CRLF & _
  138. @TAB & "err.helpcontext is: " & @TAB & $oError.helpcontext & @CRLF & _
  139. @TAB & "err.lastdllerror is: " & @TAB & $oError.lastdllerror & @CRLF & _
  140. @TAB & "err.scriptline is: " & @TAB & $oError.scriptline & @CRLF & _
  141. @TAB & "err.retcode is: " & @TAB & "0x" & Hex($oError.retcode) & @CRLF & @CRLF)
  142. EndFunc ;==>_ErrFunc