[Python] I need to automate a process on the PC when a specefic sound plays on it

Discussão em 'Python' iniciado por Stack, Janeiro 3, 2025.

  1. Stack

    Stack Membro Participativo

    I would like to know if it is possible to automate a simple click to stop sending automatic emails. For example; If I am using the 'Playstatic' program (it is an application to perform loop processes such as keystrokes and clicking), I would like to be able to stop it when the sound of another notification sounds on the same PC, such as an alarm. Would it be possible to create a program that automates a process when a specific sound plays? Thank you.

    My intention is for emails to stop being sent when a notification sounds; or what is the same, make an automatic click when a sound comes from the PC itself.

    I have tried to use python within my basic knowledge and with the help of a friend, but i don't know how to put the specific sound in the 'sounddevice' library and how to run the script on my pc. If anyone can help us, we would appreciate it. The script we are trying to make work is something similar to this:

    import sounddevice as sd
    import numpy as np
    import pyautogui
    import time

    # Volume threshold to detect sound
    VOLUME_THRESHOLD = 0.5 # Adjust this value according to the alarm intensity

    # Function to capture system audio
    def monitor_audio():
    print("Monitoring audio...")
    def callback(indata, frames, time, status):
    # Calculate the volume
    volume_norm = np.linalg.norm(indata) * 10
    print(f"Volumen: {volume_norm}") # show volume (opcional to calibrate)
    if volume_norm > VOLUME_THRESHOLD:
    print("Sound detected! Stop process.")
    stop_automation()
    exit(0) # Program ends

    # Open an audio stream
    with sd.InputStream(callback=callback):
    while True:
    time.sleep(0.1) # Small delay to reduce CPU usage

    # Function to stop the automated process
    def stop_automation():
    # Simulate a click in the application area (adjusts the coordinates)
    pyautogui.click(100, 100) # Change (100, 100) to the coordinates of the stop button
    print("Automation stopped.")

    # Start monitoring
    monitor_audio()

    Continue reading...

Compartilhe esta Página