Script that detects if an active window has stopped playing sound

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
momsawmefap
Posts: 9
Joined: 17 May 2017, 13:03

Script that detects if an active window has stopped playing sound

25 Dec 2017, 19:27

Hello, Is it possible to write a script that can detect whether an active window (let's say a game or a video) that is currently playing sound, has stopped playing sound?
Like if you open the volume mixer on windows, you'll see that if a window is playing sound there will be a green spike flashing...
I searched a lot but i couldn't find anything.. thus creating this post here hopefully someone can help :P
what i'm trying to make is a script that will be running (on a timer, let's say.. SetTimer) that will detect if a sound has STOPPED playing from a specific window that's active
which should look something like this i think (i'm stupid so probably not :) )

Code: Select all

Scrolllock::
	SetTimer, Timer1, 10000
Return

Timer1: 
	IfWinExist, WindowName
	{
		Check_Sound_Command       //Command/Function that checks if sound is playing or not (over a 5 second period let's say since it's not instant)
		If ( False )
			WinRestore, WindowName      //I have my window minimized, this will bring it back up in case the sound stopped.
	}
	
Return
momsawmefap
Posts: 9
Joined: 17 May 2017, 13:03

Re: Script that detects if an active window has stopped playing sound

25 Dec 2017, 23:48

Update: I have spent the past few hours searching.. came up with nothing so far.
Is it possible that there is an ahk command, or probably build a function that can detect activity on the soundcard? cause on every post I came across they mentioned that there has to be a direct connection to the soundcard in order to detect if a program or anything at all is playing any sound or not (if there's sound activity). I don't understand a thing when it comes to these sorts of things, i'm not familiar with the ahk programming language at all.. if there's a command or a way to build a function to do so i'd appreciate posting it if possible :)

Thank you!
User avatar
noname
Posts: 516
Joined: 19 Nov 2013, 09:15

Re: Script that detects if an active window has stopped playing sound

26 Dec 2017, 11:19

You can use Kurolight's code used by HIAC , it has a function IsAudioPlaying() it returns 0 or 1.
Original Kurolight code
https://autohotkey.com/boards/viewtopic.php?f=5&t=40132

https://autohotkey.com/boards/viewtopic ... 6&p=186032



example with tooltip:

Code: Select all



loop
  {
  tooltip % IsAudioPlaying()
  sleep 100
  }

esc::exitapp



IsAudioPlaying() {
    AudioLevel := 0.0
    VA_IAudioMeterInformation_GetPeakValue(VA_GetAudioMeter(), AudioLevel)
    return (Round(AudioLevel, 4) > 0)
}
;---------------------------------------------------------
;VA code credited to Lexikos, I merely cut it down to the minimum I needed.
VA_GetAudioMeter(device_desc="playback")
{
    if ! device := VA_GetDevice(device_desc)
        return 0
    VA_IMMDevice_Activate(device, "{C02216F6-8C67-4B5B-9D00-D008E73E0064}", 7, 0, audioMeter)
    ObjRelease(device)
    return audioMeter
}

VA_GetDevice(device_desc="playback")
{
    static CLSID_MMDeviceEnumerator := "{BCDE0395-E52F-467C-8E3D-C4579291692E}"
        , IID_IMMDeviceEnumerator := "{A95664D2-9614-4F35-A746-DE8DB63617E6}"
    if !(deviceEnumerator := ComObjCreate(CLSID_MMDeviceEnumerator, IID_IMMDeviceEnumerator))
        return 0
    
    device := 0
    
    if VA_IMMDeviceEnumerator_GetDevice(deviceEnumerator, device_desc, device) = 0
        goto VA_GetDevice_Return
    
    if device_desc is integer
    {
        m2 := device_desc
        if m2 >= 4096 ; Probably a device pointer, passed here indirectly via VA_GetAudioMeter or such.
        {
            ObjAddRef(device := m2)
            goto VA_GetDevice_Return
        }
    }
    else
        RegExMatch(device_desc, "(.*?)\s*(?::(\d+))?$", m)
    
    if m1 in playback,p
        m1 := "", flow := 0 ; eRender
    else if m1 in capture,c
        m1 := "", flow := 1 ; eCapture
    else if (m1 . m2) = ""  ; no name or number specified
        m1 := "", flow := 0 ; eRender (default)
    else
        flow := 2 ; eAll
    
    if (m1 . m2) = ""   ; no name or number (maybe "playback" or "capture")
    {
        VA_IMMDeviceEnumerator_GetDefaultAudioEndpoint(deviceEnumerator, flow, 0, device)
        goto VA_GetDevice_Return
    }

    VA_IMMDeviceEnumerator_EnumAudioEndpoints(deviceEnumerator, flow, 1, devices)
    
    if m1 =
    {
        VA_IMMDeviceCollection_Item(devices, m2-1, device)
        goto VA_GetDevice_Return
    }
    
    VA_IMMDeviceCollection_GetCount(devices, count)
    index := 0
    Loop % count
        if VA_IMMDeviceCollection_Item(devices, A_Index-1, device) = 0
            if InStr(VA_GetDeviceName(device), m1) && (m2 = "" || ++index = m2)
                goto VA_GetDevice_Return
            else
                ObjRelease(device), device:=0

VA_GetDevice_Return:
    ObjRelease(deviceEnumerator)
    if (devices)
        ObjRelease(devices)
    
    return device ; may be 0
}

VA_GetDeviceName(device)
{
    static PKEY_Device_FriendlyName
    if !VarSetCapacity(PKEY_Device_FriendlyName)
        VarSetCapacity(PKEY_Device_FriendlyName, 20)
        ,VA_GUID(PKEY_Device_FriendlyName :="{A45C254E-DF1C-4EFD-8020-67D146A850E0}")
        ,NumPut(14, PKEY_Device_FriendlyName, 16)
    VarSetCapacity(prop, 16)
    VA_IMMDevice_OpenPropertyStore(device, 0, store)
    ; store->GetValue(.., [out] prop)
    DllCall(NumGet(NumGet(store+0)+5*A_PtrSize), "ptr", store, "ptr", &PKEY_Device_FriendlyName, "ptr", &prop)
    ObjRelease(store)
    VA_WStrOut(deviceName := NumGet(prop,8))
    return deviceName
}

VA_WStrOut(ByRef str) {
    str := StrGet(ptr := str, "UTF-16")
    DllCall("ole32\CoTaskMemFree", "ptr", ptr)  ; FREES THE STRING.
}
VA_GUID(ByRef guid_out, guid_in="%guid_out%") {
    if (guid_in == "%guid_out%")
        guid_in :=   guid_out
    if  guid_in is integer
        return guid_in
    VarSetCapacity(guid_out, 16, 0)
    DllCall("ole32\CLSIDFromString", "wstr", guid_in, "ptr", &guid_out)
    return &guid_out
}
VA_IMMDevice_OpenPropertyStore(this, Access, ByRef Properties) {
    return DllCall(NumGet(NumGet(this+0)+4*A_PtrSize), "ptr", this, "uint", Access, "ptr*", Properties)
}
VA_IMMDevice_Activate(this, iid, ClsCtx, ActivationParams, ByRef Interface) {
    return DllCall(NumGet(NumGet(this+0)+3*A_PtrSize), "ptr", this, "ptr", VA_GUID(iid), "uint", ClsCtx, "uint", ActivationParams, "ptr*", Interface)
}
VA_IMMDeviceEnumerator_GetDevice(this, id, ByRef Device) {
    return DllCall(NumGet(NumGet(this+0)+5*A_PtrSize), "ptr", this, "wstr", id, "ptr*", Device)
}
VA_IMMDeviceEnumerator_GetDefaultAudioEndpoint(this, DataFlow, Role, ByRef Endpoint) {
    return DllCall(NumGet(NumGet(this+0)+4*A_PtrSize), "ptr", this, "int", DataFlow, "int", Role, "ptr*", Endpoint)
}
VA_IMMDeviceEnumerator_EnumAudioEndpoints(this, DataFlow, StateMask, ByRef Devices) {
    return DllCall(NumGet(NumGet(this+0)+3*A_PtrSize), "ptr", this, "int", DataFlow, "uint", StateMask, "ptr*", Devices)
}
VA_IMMDeviceCollection_GetCount(this, ByRef Count) {
    return DllCall(NumGet(NumGet(this+0)+3*A_PtrSize), "ptr", this, "uint*", Count)
}
VA_IMMDeviceCollection_Item(this, Index, ByRef Device) {
    return DllCall(NumGet(NumGet(this+0)+4*A_PtrSize), "ptr", this, "uint", Index, "ptr*", Device)
}
VA_IAudioMeterInformation_GetPeakValue(this, ByRef Peak) {
    return DllCall(NumGet(NumGet(this+0)+3*A_PtrSize), "ptr", this, "float*", Peak)
}
momsawmefap
Posts: 9
Joined: 17 May 2017, 13:03

Re: Script that detects if an active window has stopped playing sound

26 Dec 2017, 20:06

Thank you for your reply, so it is possible to do...
The code you've posted is the function that can detect if a sound is playing? i don't quite understand how i can add it to the code i posted above.
If it's the full function, do i just paste it above my code and then call it in the part i need? or how does it work.. it's too complicated for me to read i don't know tons of things in it :(
Thank you for your time!!
Hiney

Re: Script that detects if an active window has stopped playing sound

26 Dec 2017, 21:14

momsawmefap wrote:Thank you for your reply, so it is possible to do...
The code you've posted is the function that can detect if a sound is playing? i don't quite understand how i can add it to the code i posted above.
If it's the full function, do i just paste it above my code and then call it in the part i need? or how does it work.. it's too complicated for me to read i don't know tons of things in it :(
Thank you for your time!!
also make sure the code can detect a specific program stopped playing sound. it looks like it checks if in general sound is playing code works thank you no name
momsawmefap
Posts: 9
Joined: 17 May 2017, 13:03

Re: Script that detects if an active window has stopped playing sound

27 Dec 2017, 01:03

Thanks for the help! although I barely understand anything in the code, it's too complicated for me I'm so sorry...
Can you please tell which parts of the codes I should modify? let's say if I want the code to detect from an active window, let's say a game or Chrome. And not in general.
Which commands should I use?

Thank you :)

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: KruschenZ, mikeyww, RickC, Spawnova and 140 guests