Detect Computer Sound

Example scripts and tips (replaces Old Scripts & Tips archive)

Moderators: Dorian (MJT support), JRL, Phil Pendlebury

Post Reply
User avatar
JRL
Automation Wizard
Posts: 3497
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Detect Computer Sound

Post by JRL » Tue Sep 08, 2015 4:30 pm

So I just started using Outlook last week. Thus far I'm not impressed. However I no longer have a choice. One of the irritations is that email notification pops up in a little window that closes after a short semi selectable time period (up to 30 whole seconds). Easy enough to remedy, I wrote a little Macro Scheduler script that detects the alerter window and puts up a dialog that tells me I have a message. Unfortunately the Outlook message box only pops up when the message stays in the "Inbox" folder. I have set up rules to place incoming messages into their appropriate folders so the Outlook alerter message box rarely pops up. On the other hand, Every message arrival gets a chime.

I searched the forum and discovered there is no known way to detect a sound on a computer. Searched the internet and found that there are windows APIs for doing this but they would require C programming and the samples seemed quite complicated. During my internet search I discovered there is a Microsoft program that comes with every OS since XP named sndvol.exe, (pre Win7 named sndvol32.exe). Playing with sndvol.exe I see that there are visual cues when the computer outputs sound. I like visual cues.

The script uses the Windows StretchBlt function to send a pixel sized reflection of a specific area in the volume control screen to a one pixel dialog that resides in a specified screen location. When the computer plays a sound the volume control screen gets a blip of color and that color is displayed on the one pixel dialog, GetPixelColor> is perpetually checking the color of the one pixel dialog and when the dialog changes color we know that a sound has been made.

If you want to place the dialog in a location other than 0,0 be aware that there are two places in the script where the dialog is moved to 0,0 and you need to change both of those locations. You also need to change the GetPixelColor> coordinates to the new dialog location.

The key combination WinKey + Esc will close the program. Be sure to use that key combination otherwise the exit process will not close the window device contexts properly.

While the program is running the volume control is unavailable. To make the volume control available use the key combination Winkey + F2. When through with the volume control just close the window.

I have only tried this on Win7. I'd be interested to hear if it works on Win 8 or 10


The info I found about sndvol.exe is on this Microsoft MSDN webpage. I find their closing comment amusing.
"Note that there is no way to programmatically access the functionality of this program."
Obviously they don't know about Macro Scheduler.


Code: Select all

OnEvent>key_down,VK27,8,VolumeMixerEchoQuit
OnEvent>key_down,VK113,8,ViewVolumeMixer

Dialog>dVolumeMixerEcho
object dVolumeMixerEcho: TForm
  Borderstyle = bsNone
  FormStyle = fsStayOnTop
  ClientHeight = 1
  ClientWidth = 1
end
EndDialog>dVolumeMixerEcho

Let>WIN_USEHANDLE=1
  MoveWindow>dVolumeMixerEcho.handle,0,0
Let>WIN_USEHANDLE=0

Show>dVolumeMixerEcho

RunProgram>%Sys_dir%\sndvol.exe
WaitWindowOpen>Volume Mixer - Speakers*
Wait>0.1
GetWindowHandle>Volume Mixer - Speakers*,vVolumeMixerWindowHandle
Let>WIN_USEHANDLE=1
  SetFocus>dVolumeMixerEcho.handle
  SetFocus>0
Let>WIN_USEHANDLE=0
  LibFunc>user32,GetDC,HDC1,dVolumeMixerEcho.handle
  LibFunc>user32,GetDC,HDC3,vVolumeMixerWindowHandle
  GoSub>MakeOpaque,vVolumeMixerWindowHandle,0
  LibFunc>Gdi32,StretchBlt,SBres,HDC1,0,0,1,1,HDC3,57,229,1,1,13369376
  Wait>0.1
  GetPixelColor>0,0,vBaseVolumeMixerEchoColor
  
Label>VolumeMixerEchoLoop
  LibFunc>Gdi32,StretchBlt,SBres,HDC1,0,0,1,1,HDC3,57,229,1,1,13369376
  GetPixelColor>0,0,vVolumeMixerEchoColor
  If>%vVolumeMixerEchoColor%<>%vBaseVolumeMixerEchoColor%
  
  
  
  
    //Here is where you place what you want to do when a sound is detected
    MDL>Sound Detected
    
    
    
    
  EndIf
  Let>WIN_USEHANDLE=1
    MoveWindow>dVolumeMixerEcho.handle,0,0
  Let>WIN_USEHANDLE=0
  ProcessExists>SndVol.exe,vPres
  If>vPres=False
    LibFunc>user32,ReleaseDC,vVolumeMixerWindowHandle,HDC3_1,HDC2
    RunProgram>%Sys_dir%\sndvol.exe
    WaitWindowOpen>Volume Mixer - Speakers*
    Wait>0.1
    GetWindowHandle>Volume Mixer - Speakers*,vVolumeMixerWindowHandle
    Let>WIN_USEHANDLE=1
      SetFocus>dVolumeMixerEcho.handle
      SetFocus>0
    Let>WIN_USEHANDLE=0
      LibFunc>user32,GetDC,HDC3,vVolumeMixerWindowHandle
      GoSub>MakeOpaque,vVolumeMixerWindowHandle,0
      LibFunc>Gdi32,StretchBlt,SBres,HDC1,0,0,1,1,HDC3,57,229,1,1,13369376
      Wait>0.1
      GetPixelColor>0,0,vBaseVolumeMixerEchoColor
  EndIf
  Wait>0.01
Goto>VolumeMixerEchoLoop

SRT>VolumeMixerEchoQuit
  LibFunc>user32,ReleaseDC,dVolumeMixerEcho.handle,HDC1_1,HDC1
  LibFunc>user32,ReleaseDC,vVolumeMixerWindowHandle,HDC3_1,HDC2
  ProcessExists>SndVol.exe,vPres
  If>vPres=True
    KillProcess>SndVol.exe
  EndIf
  Exit>0
END>VolumeMixerEchoQuit

SRT>ViewVolumeMixer
  GoSub>MakeOpaque,vVolumeMixerWindowHandle,255
END>ViewVolumeMixer


//Usage:
//GoSub>MakeOpaque,Window Handle,Opacity number (0-255) zero is invisible

SRT>MakeOpaque
  If>%MakeOpaque_var_2%=
    Let>MakeOpaque_var_2=255
  EndIf
  If>MakeOpaque_var_1<>
    //constants
    Let>GWL_EXSTYLE=-20
    Let>WS_EX_LAYERED=524288
    Let>LWA_ALPHA=2
    //get style attributes of window
    LibFunc>user32,GetWindowLongA,attribs,%MakeOpaque_var_1%,GWL_EXSTYLE
    Let>attribs={%attribs% OR %WS_EX_LAYERED%}
    //make window transparent
    LibFunc>user32,SetWindowLongA,swl,%MakeOpaque_var_1%,GWL_EXSTYLE,attribs
    LibFunc>user32,SetLayeredWindowAttributes,res,%MakeOpaque_var_1%,0,%MakeOpaque_var_2%,LWA_ALPHA
  EndIf
END>MakeOpaque

User avatar
Meryl
Staff
Posts: 124
Joined: Wed Sep 19, 2012 1:53 pm
Location: Texas
Contact:

Re: Detect Computer Sound

Post by Meryl » Tue Sep 15, 2015 2:03 pm

Anyone have this experience in Win 8 or 10. I don't use Outlook either.

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts