Cris1 Posted January 29, 2019 Posted January 29, 2019 (edited) Hey guys. I would like a little help, I am a beginner, I have already broken my head and I have not achieved my goal. I would like my script to run an application when it detected sound through the sound card and when that sound stopped the application was closed. The script has to run in continuous looping, when some sound or music starts, for example notepad.exe (1 instance) will open and when the sound stops the notepad will be closed. Anyone can help me please?Script below. DanyFirex code. expandcollapse popup#include <WinAPICom.au3> #include <Process.au3> #include <Array.au3> Opt("MustDeclareVars", 1) Global Const $CLSCTX_INPROC_SERVER = 0x01 + 0x02 + 0x04 + 0x10 Global Enum $eRender, $eCapture, $eAll, $EDataFlow_enum_count Global Enum $AudioSessionStateInactive, $AudioSessionStateActive, $AudioSessionStateExpired Global Const $eMultimedia = 1 Global Const $sCLSID_MMDeviceEnumerator = "{BCDE0395-E52F-467C-8E3D-C4579291692E}" Global Const $sIID_IMMDeviceEnumerator = "{A95664D2-9614-4F35-A746-DE8DB63617E6}" Global Const $sTagIMMDeviceEnumerator = _ "EnumAudioEndpoints hresult(int;dword;ptr*);" & _ "GetDefaultAudioEndpoint hresult(int;int;ptr*);" & _ "GetDevice hresult(wstr;ptr*);" & _ "RegisterEndpointNotificationCallback hresult(ptr);" & _ "UnregisterEndpointNotificationCallback hresult(ptr)" Global Const $sIID_IAudioMeterInformation = "{C02216F6-8C67-4B5B-9D00-D008E73E0064}" Global Const $sTagIAudioMeterInformation = "GetPeakValue hresult(float*);" & _ "GetMeteringChannelCount hresult(dword*);" & _ "GetChannelsPeakValues hresult(dword;float*);" & _ "QueryHardwareSupport hresult(dword*);" Global Const $sIID_IMMDevice = "{D666063F-1587-4E43-81F1-B948E807363F}" Global Const $sTagIMMDevice = _ "Activate hresult(clsid;dword;ptr;ptr*);" & _ "OpenPropertyStore hresult(dword;ptr*);" & _ "GetId hresult(wstr*);" & _ "GetState hresult(dword*)" Global Const $sIID_IAudioSessionManager2 = "{77aa99a0-1bd6-484f-8bc7-2c654c9a9b6f}" Global Const $sTagIAudioSessionManager = "GetAudioSessionControl hresult(ptr;dword;ptr*);" & _ "GetSimpleAudioVolume hresult(ptr;dword;ptr*);" Global Const $sTagIAudioSessionManager2 = $sTagIAudioSessionManager & "GetSessionEnumerator hresult(ptr*);" & _ "RegisterSessionNotification hresult(ptr);" & _ "UnregisterSessionNotification hresult(ptr);" & _ "RegisterDuckNotification hresult(wstr;ptr);" & _ "UnregisterDuckNotification hresult(ptr)" Global Const $sIID_IAudioSessionEnumerator = "{e2f5bb11-0570-40ca-acdd-3aa01277dee8}" Global Const $sTagIAudioSessionEnumerator = "GetCount hresult(int*);GetSession hresult(int;ptr*)" Global Const $sIID_IAudioSessionControl = "{f4b1a599-7266-4319-a8ca-e70acb11e8cd}" Global Const $sTagIAudioSessionControl = "GetState hresult(int*);GetDisplayName hresult(wstr*);" & _ "SetDisplayName hresult(wstr);GetIconPath hresult(wstr*);" & _ "SetIconPath hresult(wstr;ptr);GetGroupingParam hresult(ptr*);" & _ "SetGroupingParam hresult(ptr;ptr);RegisterAudioSessionNotification hresult(ptr);" & _ "UnregisterAudioSessionNotification hresult(ptr);" Global Const $sIID_IAudioSessionControl2 = "{bfb7ff88-7239-4fc9-8fa2-07c950be9c6d}" Global Const $sTagIAudioSessionControl2 = $sTagIAudioSessionControl & "GetSessionIdentifier hresult(wstr*);" & _ "GetSessionInstanceIdentifier hresult(wstr*);" & _ "GetProcessId hresult(dword*);IsSystemSoundsSession hresult();" & _ "SetDuckingPreferences hresult(bool);" Global $bExit= not False HotKeySet("{ESC}","_Exit") _WinAPI_CoInitialize() Local $aApp =0 While $bExit $aApp = _GetAppsPlayingSound() If IsArray($aApp) then ConsoleWrite("!SoundDetected!!!" & @CRLF) For $i= 0 to UBound($aApp)-1 ConsoleWrite($aApp[$i][0] & @CRLF) Next ConsoleWrite("!End" & @CRLF) EndIf Sleep(100) WEnd _WinAPI_CoUninitialize() Func _Exit() $bExit=False EndFunc Func _GetAppsPlayingSound() Local $pIMMDevice = 0 Local $oMMDevice = 0 Local $pIAudioSessionManager2 = 0 Local $oIAudioSessionManager2 = 0 Local $pIAudioSessionEnumerator = 0 Local $oIAudioSessionEnumerator = 0 Local $nSessions = 0 Local $oMMDeviceEnumerator = 0 Local $aApp[0] Local $pIAudioSessionControl2 = 0 Local $oIAudioSessionControl2 = 0 Local $oIAudioMeterInformation = 0 Local $ProcessID = 0 Local $fPeakValue = 0 Local $iState = 0 Local $iVolume = 0 Local $oErrorHandler = 0 $oErrorHandler = ObjEvent("AutoIt.Error", "_ErrFunc") $oMMDeviceEnumerator = ObjCreateInterface($sCLSID_MMDeviceEnumerator, $sIID_IMMDeviceEnumerator, $sTagIMMDeviceEnumerator) If @error Then Return $aApp If SUCCEEDED($oMMDeviceEnumerator.GetDefaultAudioEndpoint($eRender, $eMultimedia, $pIMMDevice)) Then ;eRender $oMMDevice = ObjCreateInterface($pIMMDevice, $sIID_IMMDevice, $sTagIMMDevice) $oMMDevice.Activate($sIID_IAudioSessionManager2, $CLSCTX_INPROC_SERVER, 0, $pIAudioSessionManager2) $oIAudioSessionManager2 = ObjCreateInterface($pIAudioSessionManager2, $sIID_IAudioSessionManager2, $sTagIAudioSessionManager2) $oIAudioSessionManager2.GetSessionEnumerator($pIAudioSessionEnumerator) $oIAudioSessionEnumerator = ObjCreateInterface($pIAudioSessionEnumerator, $sIID_IAudioSessionEnumerator, $sTagIAudioSessionEnumerator) $oIAudioSessionEnumerator.GetCount($nSessions) For $i = 0 To $nSessions - 1 $oIAudioSessionEnumerator.GetSession($i, $pIAudioSessionControl2) $oIAudioSessionControl2 = ObjCreateInterface($pIAudioSessionControl2, $sIID_IAudioSessionControl2, $sTagIAudioSessionControl2) $oIAudioSessionControl2.GetState($iState) If $iState = $AudioSessionStateActive Then $oIAudioSessionControl2.GetProcessId($ProcessID) $oIAudioMeterInformation = ObjCreateInterface($pIAudioSessionControl2, $sIID_IAudioMeterInformation, $sTagIAudioMeterInformation) $oIAudioSessionControl2.AddRef $oIAudioMeterInformation.GetPeakValue($fPeakValue) If $fPeakValue > 0 Then ReDim $aApp[UBound($aApp) + 1][2] $aApp[UBound($aApp) - 1][0] = _ProcessGetName($ProcessID) $aApp[UBound($aApp) - 1][1] = $fPeakValue EndIf EndIf $fPeakValue = 0 $iState = 0 $ProcessID = 0 $oIAudioMeterInformation = 0 $oIAudioSessionControl2 = 0 Next $oIAudioSessionEnumerator = 0 $oIAudioSessionManager2 = 0 $oMMDevice = 0 $oMMDeviceEnumerator = 0 If UBound($aApp) = 0 Then $aApp = 0 Return $aApp Else Return 0 EndIf EndFunc ;==>_GetAppsPlayingSound Func SUCCEEDED($hr) Return ($hr >= 0) EndFunc ;==>SUCCEEDED ; User's COM error function. Will be called if COM error occurs Func _ErrFunc($oError) ; Do anything here. ConsoleWrite(@ScriptName & " (" & $oError.scriptline & ") : ==> COM Error intercepted !" & @CRLF & _ @TAB & "err.number is: " & @TAB & @TAB & "0x" & Hex($oError.number) & @CRLF & _ @TAB & "err.windescription:" & @TAB & $oError.windescription & @CRLF & _ @TAB & "err.description is: " & @TAB & $oError.description & @CRLF & _ @TAB & "err.source is: " & @TAB & @TAB & $oError.source & @CRLF & _ @TAB & "err.helpfile is: " & @TAB & $oError.helpfile & @CRLF & _ @TAB & "err.helpcontext is: " & @TAB & $oError.helpcontext & @CRLF & _ @TAB & "err.lastdllerror is: " & @TAB & $oError.lastdllerror & @CRLF & _ @TAB & "err.scriptline is: " & @TAB & $oError.scriptline & @CRLF & _ @TAB & "err.retcode is: " & @TAB & "0x" & Hex($oError.retcode) & @CRLF & @CRLF) EndFunc ;==>_ErrFunc Edited January 29, 2019 by Cris1 Code tags added
Moderators Melba23 Posted January 29, 2019 Moderators Posted January 29, 2019 Cris1, Welcome to the AutoIt forums. Forum etiquette is to wait at least 24hrs before bumping your own thread - please observe this in future. And when you post code please use Code tags - see here how to do it. Then you get a scrolling box and syntax colouring as you can see above now I have added the tags. Thanks in advance for your cooperation. M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area
Danyfirex Posted January 29, 2019 Posted January 29, 2019 Hello. You said you have broken your head trying to do it but as far I can see the code is exactly as I wrote it years ago... Saludos Nine and Earthshine 1 1 Danysys.com AutoIt... UDFs: VirusTotal API 2.0 UDF - libZPlay UDF - Apps: Guitar Tab Tester - VirusTotal Hash Checker Examples: Text-to-Speech ISpVoice Interface - Get installed applications - Enable/Disable Network connection PrintHookProc - WINTRUST - Mute Microphone Level - Get Connected NetWorks - Create NetWork Connection ShortCut
Cris1 Posted January 29, 2019 Author Posted January 29, 2019 (edited) 17 minutes ago, Danyfirex said: Hello. You said you have broken your head trying to do it but as far I can see the code is exactly as I wrote it years ago... Saludos Yes Dani, is your code, sorry about that. When I said broken my head I mean, I can't make Works changing your code. When the sound start I want open an application, when the sound stop I want this process close. Can you help me with this please ? Edited January 29, 2019 by Cris1
Cris1 Posted January 29, 2019 Author Posted January 29, 2019 1 hour ago, Melba23 said: Cris1, Welcome to the AutoIt forums. Forum etiquette is to wait at least 24hrs before bumping your own thread - please observe this in future. And when you post code please use Code tags - see here how to do it. Then you get a scrolling box and syntax colouring as you can see above now I have added the tags. Thanks in advance for your cooperation. M23 Thanks Melba
Danyfirex Posted January 29, 2019 Posted January 29, 2019 mmm I see. Check this example: expandcollapse popup#include <WinAPICom.au3> #include <Process.au3> #include <Array.au3> Opt("MustDeclareVars", 1) Global Const $CLSCTX_INPROC_SERVER = 0x01 + 0x02 + 0x04 + 0x10 Global Enum $eRender, $eCapture, $eAll, $EDataFlow_enum_count Global Enum $AudioSessionStateInactive, $AudioSessionStateActive, $AudioSessionStateExpired Global Const $eMultimedia = 1 Global Const $sCLSID_MMDeviceEnumerator = "{BCDE0395-E52F-467C-8E3D-C4579291692E}" Global Const $sIID_IMMDeviceEnumerator = "{A95664D2-9614-4F35-A746-DE8DB63617E6}" Global Const $sTagIMMDeviceEnumerator = _ "EnumAudioEndpoints hresult(int;dword;ptr*);" & _ "GetDefaultAudioEndpoint hresult(int;int;ptr*);" & _ "GetDevice hresult(wstr;ptr*);" & _ "RegisterEndpointNotificationCallback hresult(ptr);" & _ "UnregisterEndpointNotificationCallback hresult(ptr)" Global Const $sIID_IAudioMeterInformation = "{C02216F6-8C67-4B5B-9D00-D008E73E0064}" Global Const $sTagIAudioMeterInformation = "GetPeakValue hresult(float*);" & _ "GetMeteringChannelCount hresult(dword*);" & _ "GetChannelsPeakValues hresult(dword;float*);" & _ "QueryHardwareSupport hresult(dword*);" Global Const $sIID_IMMDevice = "{D666063F-1587-4E43-81F1-B948E807363F}" Global Const $sTagIMMDevice = _ "Activate hresult(clsid;dword;ptr;ptr*);" & _ "OpenPropertyStore hresult(dword;ptr*);" & _ "GetId hresult(wstr*);" & _ "GetState hresult(dword*)" Global Const $sIID_IAudioSessionManager2 = "{77aa99a0-1bd6-484f-8bc7-2c654c9a9b6f}" Global Const $sTagIAudioSessionManager = "GetAudioSessionControl hresult(ptr;dword;ptr*);" & _ "GetSimpleAudioVolume hresult(ptr;dword;ptr*);" Global Const $sTagIAudioSessionManager2 = $sTagIAudioSessionManager & "GetSessionEnumerator hresult(ptr*);" & _ "RegisterSessionNotification hresult(ptr);" & _ "UnregisterSessionNotification hresult(ptr);" & _ "RegisterDuckNotification hresult(wstr;ptr);" & _ "UnregisterDuckNotification hresult(ptr)" Global Const $sIID_IAudioSessionEnumerator = "{e2f5bb11-0570-40ca-acdd-3aa01277dee8}" Global Const $sTagIAudioSessionEnumerator = "GetCount hresult(int*);GetSession hresult(int;ptr*)" Global Const $sIID_IAudioSessionControl = "{f4b1a599-7266-4319-a8ca-e70acb11e8cd}" Global Const $sTagIAudioSessionControl = "GetState hresult(int*);GetDisplayName hresult(wstr*);" & _ "SetDisplayName hresult(wstr);GetIconPath hresult(wstr*);" & _ "SetIconPath hresult(wstr;ptr);GetGroupingParam hresult(ptr*);" & _ "SetGroupingParam hresult(ptr;ptr);RegisterAudioSessionNotification hresult(ptr);" & _ "UnregisterAudioSessionNotification hresult(ptr);" Global Const $sIID_IAudioSessionControl2 = "{bfb7ff88-7239-4fc9-8fa2-07c950be9c6d}" Global Const $sTagIAudioSessionControl2 = $sTagIAudioSessionControl & "GetSessionIdentifier hresult(wstr*);" & _ "GetSessionInstanceIdentifier hresult(wstr*);" & _ "GetProcessId hresult(dword*);IsSystemSoundsSession hresult();" & _ "SetDuckingPreferences hresult(bool);" Global $g_bExit = Not False Global $g_bIsRunning = False ;Program To start and Program to Close Global $_gAppToRun = "notepad.exe" Global $g_AppToFinish = "notepad.exe" HotKeySet("{ESC}", "_Exit") _WinAPI_CoInitialize() Local $aApp = 0 While $g_bExit $aApp = _GetAppsPlayingSound() _RunCloseAppIfSoundPlayingByProcessName("chrome.exe",$aApp) Sleep(100) WEnd _WinAPI_CoUninitialize() Func _RunCloseAppIfSoundPlayingByProcessName($sProcessName, $aApp) If Not $g_bIsRunning Then If _ArraySearch($aApp, $sProcessName)>-1 Then ShellExecute($_gAppToRun) $g_bIsRunning = True EndIf EndIf If $g_bIsRunning Then If _ArraySearch($aApp, $sProcessName)=-1 Then ProcessClose($g_AppToFinish) $g_bIsRunning = False EndIf EndIf EndFunc ;==>_RunCloseAppIfSoundPlayingByProcessName Func _Exit() $g_bExit = False EndFunc ;==>_Exit Func _GetAppsPlayingSound() Local $pIMMDevice = 0 Local $oMMDevice = 0 Local $pIAudioSessionManager2 = 0 Local $oIAudioSessionManager2 = 0 Local $pIAudioSessionEnumerator = 0 Local $oIAudioSessionEnumerator = 0 Local $nSessions = 0 Local $oMMDeviceEnumerator = 0 Local $aApp[0] Local $pIAudioSessionControl2 = 0 Local $oIAudioSessionControl2 = 0 Local $oIAudioMeterInformation = 0 Local $ProcessID = 0 Local $fPeakValue = 0 Local $iState = 0 Local $iVolume = 0 Local $oErrorHandler = 0 $oErrorHandler = ObjEvent("AutoIt.Error", "_ErrFunc") $oMMDeviceEnumerator = ObjCreateInterface($sCLSID_MMDeviceEnumerator, $sIID_IMMDeviceEnumerator, $sTagIMMDeviceEnumerator) If @error Then Return $aApp If SUCCEEDED($oMMDeviceEnumerator.GetDefaultAudioEndpoint($eRender, $eMultimedia, $pIMMDevice)) Then ;eRender $oMMDevice = ObjCreateInterface($pIMMDevice, $sIID_IMMDevice, $sTagIMMDevice) $oMMDevice.Activate($sIID_IAudioSessionManager2, $CLSCTX_INPROC_SERVER, 0, $pIAudioSessionManager2) $oIAudioSessionManager2 = ObjCreateInterface($pIAudioSessionManager2, $sIID_IAudioSessionManager2, $sTagIAudioSessionManager2) $oIAudioSessionManager2.GetSessionEnumerator($pIAudioSessionEnumerator) $oIAudioSessionEnumerator = ObjCreateInterface($pIAudioSessionEnumerator, $sIID_IAudioSessionEnumerator, $sTagIAudioSessionEnumerator) $oIAudioSessionEnumerator.GetCount($nSessions) For $i = 0 To $nSessions - 1 $oIAudioSessionEnumerator.GetSession($i, $pIAudioSessionControl2) $oIAudioSessionControl2 = ObjCreateInterface($pIAudioSessionControl2, $sIID_IAudioSessionControl2, $sTagIAudioSessionControl2) $oIAudioSessionControl2.GetState($iState) If $iState = $AudioSessionStateActive Then $oIAudioSessionControl2.GetProcessId($ProcessID) $oIAudioMeterInformation = ObjCreateInterface($pIAudioSessionControl2, $sIID_IAudioMeterInformation, $sTagIAudioMeterInformation) $oIAudioSessionControl2.AddRef $oIAudioMeterInformation.GetPeakValue($fPeakValue) If $fPeakValue > 0 Then ReDim $aApp[UBound($aApp) + 1][2] $aApp[UBound($aApp) - 1][0] = _ProcessGetName($ProcessID) $aApp[UBound($aApp) - 1][1] = $fPeakValue EndIf EndIf $fPeakValue = 0 $iState = 0 $ProcessID = 0 $oIAudioMeterInformation = 0 $oIAudioSessionControl2 = 0 Next $oIAudioSessionEnumerator = 0 $oIAudioSessionManager2 = 0 $oMMDevice = 0 $oMMDeviceEnumerator = 0 If UBound($aApp) = 0 Then $aApp = 0 Return $aApp Else Return $aApp EndIf EndFunc ;==>_GetAppsPlayingSound Func SUCCEEDED($hr) Return ($hr >= 0) EndFunc ;==>SUCCEEDED ; User's COM error function. Will be called if COM error occurs Func _ErrFunc($oError) ; Do anything here. ConsoleWrite(@ScriptName & " (" & $oError.scriptline & ") : ==> COM Error intercepted !" & @CRLF & _ @TAB & "err.number is: " & @TAB & @TAB & "0x" & Hex($oError.number) & @CRLF & _ @TAB & "err.windescription:" & @TAB & $oError.windescription & @CRLF & _ @TAB & "err.description is: " & @TAB & $oError.description & @CRLF & _ @TAB & "err.source is: " & @TAB & @TAB & $oError.source & @CRLF & _ @TAB & "err.helpfile is: " & @TAB & $oError.helpfile & @CRLF & _ @TAB & "err.helpcontext is: " & @TAB & $oError.helpcontext & @CRLF & _ @TAB & "err.lastdllerror is: " & @TAB & $oError.lastdllerror & @CRLF & _ @TAB & "err.scriptline is: " & @TAB & $oError.scriptline & @CRLF & _ @TAB & "err.retcode is: " & @TAB & "0x" & Hex($oError.retcode) & @CRLF & @CRLF) EndFunc ;==>_ErrFunc Saludos argumentum, trancexx and Gianni 3 Danysys.com AutoIt... UDFs: VirusTotal API 2.0 UDF - libZPlay UDF - Apps: Guitar Tab Tester - VirusTotal Hash Checker Examples: Text-to-Speech ISpVoice Interface - Get installed applications - Enable/Disable Network connection PrintHookProc - WINTRUST - Mute Microphone Level - Get Connected NetWorks - Create NetWork Connection ShortCut
Cris1 Posted January 29, 2019 Author Posted January 29, 2019 11 minutes ago, Danyfirex said: Thanks Dany, but nothing happen here.
Danyfirex Posted January 29, 2019 Posted January 29, 2019 I forget to tell you how to use it. Basically run the script. Play a sound from google chrome maybe using youtube. So you'll se a notepad started when sound start and will be closed when you stop the sound. Saludos Danysys.com AutoIt... UDFs: VirusTotal API 2.0 UDF - libZPlay UDF - Apps: Guitar Tab Tester - VirusTotal Hash Checker Examples: Text-to-Speech ISpVoice Interface - Get installed applications - Enable/Disable Network connection PrintHookProc - WINTRUST - Mute Microphone Level - Get Connected NetWorks - Create NetWork Connection ShortCut
Cris1 Posted January 29, 2019 Author Posted January 29, 2019 Wokrs like a charm. Thank you very much Dany
Gianni Posted January 29, 2019 Posted January 29, 2019 something similar about this matter is an interesting script that waits for the sound emitted by 3 claps/finger to start an action. The script that you can find at the below link, will read a recipe in steps, and each next step is activated by clapping the fingers 3 times. maybe it can interest you. Chimp small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....
Cris1 Posted January 31, 2019 Author Posted January 31, 2019 On 29/01/2019 at 6:42 PM, Chimp said: something similar about this matter is an interesting script that waits for the sound emitted by 3 claps/finger to start an action. The script that you can find at the below link, will read a recipe in steps, and each next step is activated by clapping the fingers 3 times. maybe it can interest you. Do you have a code example working ? a script that waits for the sound emitted by 3 claps/finger to start an action. I tried make work here but is too complex to me.
Gianni Posted January 31, 2019 Posted January 31, 2019 the FlashCook.rar file attached to the above pointed post (https://www.autoitscript.com/forum/topic/83481-bass-function-library-sound-and-music-functions/?do=findComment&comment=1145132) contains a nice ready to be used example. Danyfirex 1 Chimp small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now