Check when sound stops playing and display prompt

Get help with using AutoHotkey and its commands and hotkeys
Cassiopeia
Posts: 3
Joined: 15 Feb 2016, 09:02

Check when sound stops playing and display prompt

16 Jun 2016, 15:23

Hi there,
I've got a cool little code which prompts me to restart audio after I watch a video as I have memory issues and often forget that I am listening to something.
It works well but is currently just time based - I'd like to get it to physically listen to mic input and prompt based on that as currently prompts too frequently, but I don't want to set a longer timer because it stops me using the $Media_Play_Pause:: key (that's the trigger).

Currently I have this neat code which detects that sound IS playing, but I can't figure out how to reverse it to check when sound has stopped. I am shit at using variables, and I think that's the key, so I've come here for help.
The program records mic input to file only when sound is playing and the AHK script monitors the size of that file to check when it gets above threshold. I guess I need to compare its size every 30 seconds and check for a change in size of 320kb (22s of audio).

Code: Select all

#Persistent
Run, C:\Users\Guy S Jones\Desktop\mixmp3\Mixmp3.exe -s -50 -ql -mm,,,PID
SetTimer, SizeCheck, 100
Return

SizeCheck:
FileGetSize, Size, out.mp3, K

if (Size > 320) ;larger than 32KB
{
    ControlSend,, {ESC}, ahk_pid %PID%
    SetTimer, SizeCheck, Off
    GoTo, DoSomething
}
Return

DoSomething:
MsgBox, Sound Is playing
Exitapp 
User avatar
Masonjar13
Posts: 1514
Joined: 20 Jul 2014, 10:16
GitHub: Masonjar13
Location: Не Россия

Re: Check when sound stops playing and display prompt

16 Jun 2016, 16:45

You can get audio levels using the VA library.
OS: Windows 10 Pro | Editor: Notepad++
My Personal Function Library | Old Build - New Build
Cassiopeia
Posts: 3
Joined: 15 Feb 2016, 09:02

Re: Check when sound stops playing and display prompt

16 Jun 2016, 19:17

Thanks, that's really helpful. I made a working script here:

Code: Select all

;Re-maps the media playpause key to globally stop the active deck in virtual DJ 

;START SCRIPT
$Media_Play_Pause::
WinGet, WorkingWin ,, A ;Get current window ID
SetTitleMatchMode 2 ; 2: A window's title can contain WinTitle anywhere inside it to be a match. 
IfWinExist, Youtube,
	{
		WinActivate, Youtube,
		Send, k
		WinActivate ahk_id %WorkingWin%
		return
	}

IfWinExist, Radio 1,
	{
		WinActivate, Radio 1,
		Send, {Space}
		WinActivate ahk_id %WorkingWin%
	Return
	}

IfWinExist, Firefox,
	{
		WinActivate, Firefox,
		WinGet, MusicWin ,, A ;Get current window ID
		Send, {Space}
		WinActivate ahk_id %WorkingWin%
		Goto, Restartplay
	}

IfWinExist, VirtualDJ, 
	{
	WinActivate, VirtualDJ, 
	WinGet, MusicWin ,, A ;Get current window ID
	Send, {Media_Play_Pause}
	WinActivate ahk_id %WorkingWin%
	}
		
	;While sound is playing, do not prompt to restart playback.
	
	Restartplay:
	
	;Set up sound measurement
	#SingleInstance, Force
	audioMeter := VA_GetAudioMeter()
	VA_IAudioMeterInformation_GetPeakValue(audioMeter, peakValue)
	
	;Check peak sound level every 10s
	while (peakvalue > 0.1)
	{
		; Get the peak audioMeter value across all channels.
		VA_IAudioMeterInformation_GetPeakValue(audioMeter, peakValue)
		;msgbox %peakvalue%
		Sleep, 10000
	}
	
	;msgbox Music is not playing. Reset for another 10s
	sleep, 10000
	VA_IAudioMeterInformation_GetPeakValue(audioMeter, peakValue)
	if (peakvalue > 0.1)
	{
		;msgbox Playback restart detected. Will not prompt to play. 
		GoTo Restartplay		
	}
	
	
	;msgbox Still silent after second check. Ask if music playback desired.
	WinGet, WorkingWin ,, A ;Get current window ID
	MsgBox, 3, , Do you want to restart playback?, 60
	IfMsgBox Timeout
		Exit
	Else IfMsgBox No
		Goto, Restartplay
	Else IfMsgBox Cancel 
	{ 
		;Msgbox Leave all settings as they are currently. 
		Exit
	}

	IfMsgBox Yes 
	{
		WinActivate ahk_id %MusicWin%
	IfWinActive VirtualDJ
	{
		Send, {pgup 10}{Space}{pgdn 2} ;Move crossfader to allow fading back in
		WinActivate ahk_id %WorkingWin%
				
			Sleep 1000
			Loop, 4	
			{
				Sleep, 1000
				WinGet, WorkingWin ,, A ;Get current window ID
				WinActivate ahk_id %MusicWin%
				Send,{pgdn 2}
				WinActivate ahk_id %WorkingWin%
			}
			Exit
	}
	WinActivate ahk_id %MusicWin%
	Send,{Space}
	WinActivate ahk_id %WorkingWin%
	}

Exit
;END SCRIPT
Can you tell me how I can record the microphone levels with this? I'm using the following code but I don't understand why it still gives me output volume?

Code: Select all

#SingleInstance, Force
MeterLength = 20
audiomic := VA_GetDevice(microphone)
msgbox, %audiomic%
audioMeter := VA_GetAudioMeter(VA_GetDevice(microphone))

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)

Loop
{
    ; Get the peak value across all channels.
    VA_IAudioMeterInformation_GetPeakValue(audioMeter, peakValue)    
    meter := MakeMeter(peakValue, MeterLength)

        meter .= "`n" MakeMeter(NumGet(peakValues, A_Index*4-4, "float"), MeterLength)

    ToolTip, %meter%
    Sleep, %deviceperiod%
}

MakeMeter(fraction, size)
{
    global MeterLength
    Loop % fraction*size
        meter .= "|"
    Loop % (1-fraction)*size
        meter .= "."
    meter .= "  " fraction
    return meter
}
User avatar
Capn Odin
Posts: 1308
Joined: 23 Feb 2016, 19:45
Location: Denmark

Re: Check when sound stops playing and display prompt

16 Jun 2016, 19:29

Cassiopeia wrote:Can you tell me how I can record the microphone levels with this? I'm using the following code but I don't understand why it still gives me output volume?

Code: Select all

#SingleInstance, Force
MeterLength = 20
audiomic := VA_GetDevice(microphone)
msgbox, %audiomic%
audioMeter := VA_GetAudioMeter(VA_GetDevice(microphone))

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)

Loop
{
    ; Get the peak value across all channels.
    VA_IAudioMeterInformation_GetPeakValue(audioMeter, peakValue)    
    meter := MakeMeter(peakValue, MeterLength)

        meter .= "`n" MakeMeter(NumGet(peakValues, A_Index*4-4, "float"), MeterLength)

    ToolTip, %meter%
    Sleep, %deviceperiod%
}

MakeMeter(fraction, size)
{
    global MeterLength
    Loop % fraction*size
        meter .= "|"
    Loop % (1-fraction)*size
        meter .= "."
    meter .= "  " fraction
    return meter
}
In your script are the variable microphone containing a string ?
Please excuse my spelling I am dyslexic.
Cassiopeia
Posts: 3
Joined: 15 Feb 2016, 09:02

Re: Check when sound stops playing and display prompt

17 Jun 2016, 05:41

No, that's the whole script. If I add quotation marks then the script does nothing.
User avatar
Masonjar13
Posts: 1514
Joined: 20 Jul 2014, 10:16
GitHub: Masonjar13
Location: Не Россия

Re: Check when sound stops playing and display prompt

17 Jun 2016, 06:12

http://www.autohotkey.net/~Lexikos/docs ... _GetDevice
It's getting device 0, which would be the first found device. Read the link above.
OS: Windows 10 Pro | Editor: Notepad++
My Personal Function Library | Old Build - New Build

Return to “Ask For Help”

Who is online

Users browsing this forum: botman, hawkthorne, malcev, TAC109, vsub and 133 guests