;To use this script you must first be running AutoHotkey L 110703 or newer to guarantee compatibility
; (Choose the unicode 32 bit version when installing)
;AND you must download VA21.zip from:
; http://www.autohotkey.com/community/viewtopic.php?f=2&t=23792
; Once VA21.zip is downloaded, locate your Autohotkey folder and create a subfolder called "Lib"
; Place VA.ahk in that Lib folder
;
;Note: This will not work on win XP or older, since it uses the vista audio whatchamacallit
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
#Persistent
#SingleInstance, Force
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
;This variable controls how many minutes of idle time are needed before sleeping
idletime := 10
;Convert minutes to milliseconds
idletime *= 1000*60
AudioIdleCount := 0
;Setup the timer
;Set the timer to such a length that will allow us to check the sound
; output level 6 times during the 2nd half of the idle period
; note that we're going to skip audio monitoring during the first half of the idle period
slice := idletime / 12
Settimer, IdleTimer, %slice%
;the next 3 lines are debug code
;debugtime := 0
;TrayTip, Idletest, Idletest`nDebug Info, 20, 17
;SetTimer, RemoveTrayTip, 5000
return
IdleTimer:
;Force AudioIdleCount to zero for the first half of the idle period
if(A_TimeIdlePhysical < (slice*6))
{
AudioIdleCount := 0
return
}
;Check for mouse/kb input during 2nd half of idle period
if(A_TimeIdlePhysical < (slice*(AudioIdleCount+6)))
{
return
}
;continue only if mouse/keyboard are idle
audioMeter := VA_GetAudioMeter()
VA_IAudioMeterInformation_GetMeteringChannelCount(audioMeter, channelCount)
; "The peak value for each channel is recorded over one device
; period and made available during the subsequent device period."
VA_GetDevicePeriod("capture", devicePeriod)
; Get the peak value across all channels.
VA_IAudioMeterInformation_GetPeakValue(audioMeter, peakValue)
if(peakValue < 0.00001)
{
AudioIdleCount++
}
else
{
AudioIdleCount := 0 ;we still have some sound playing, so reset the counter
}
if(AudioIdleCount < 7)
return
;MsgBox, idle!
;Set this back to zero so it will stay awake once it wakes back up!
AudioIdleCount := 0
; Call the Windows API function "SetSuspendState" to have the system suspend or hibernate.
; Windows 95/NT4: Since this function does not exist, the following call would have no effect.
; Parameter #1: Pass 1 instead of 0 to hibernate rather than suspend.
; Parameter #2: Pass 1 instead of 0 to suspend immediately rather than asking each application for permission.
; Parameter #3: Pass 1 instead of 0 to disable all wake events.
;DllCall("PowrProf\SetSuspendState", "int", 0, "int", 0, "int", 0)
;uncomment the above line to sleep instead of shut down
Shutdown, 1
return
;debugging code
RemoveTrayTip:
SetTimer, RemoveTrayTip, Off
TrayTip
;return
; To have a TrayTip permanently displayed, use a timer to refresh it periodically:
SetTimer, RefreshTrayTip, 1000
Gosub, RefreshTrayTip ; Call it once to get it started right away.
return
RefreshTrayTip:
debugtime := A_TimeIdlePhysical / 1000
TrayTip, Idletest Debug Info, A_TimeIdlePhysical = %debugtime%`nAudioIdleCount = %AudioIdleCount%, , 16
return