[Python] Problem with a simple script in which the Librosa Python library does not work well...

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

  1. Stack

    Stack Membro Participativo

    I have a problem with the script that I detail below;

    My intention is to automate a click the moment an alarm sounds on my PC.

    I have used the 'Sounddevice', 'Librosa', and 'PyAutoGui' libraries, in which I upload an audio file that would play on my PC at a specific time; an audio that would play on my PC externally to the script so that my mouse makes 3 automatic clicks.

    The problem is that it does not correctly detect the audio file of the alarm that I have given it and confuses it with any other sound, or even sometimes makes automatic clicks with absolute silence.

    There is no way I can get the script to stop with the sound I want (the file I loaded inside is a specific alarm with a duration of 3 seconds)

    When you run the following script, what it prints are the distances at which the sound threshold of my alarm would be from any sound emitted by your PC.

    If the program interprets that there is a distance between 0 and what you have marked in the 'THRESHOLD' unit, the program will stop and make 3 clicks on a coordinate.

    I have tried changing the sample rate number in the following line: 'y, sr= librosa.load(file, sr=44100)', or the unit number of 'THRESOLD', but it still does not detect the sound correctly (stops randomly, with other sounds or even silence, sometimes)

    My intention is: my script should stop when the alarm that I have uploaded to the REFERENCE_AUDIO_FILE unit sounds on my PC.

    import sounddevice as sd
    import numpy as np
    import librosa
    import time
    import pyautogui
    from scipy.spatial.distance import euclidean

    # Configuration
    REFERENCE_AUDIO_FILE = "CLEFAIRYPRO.mp3" # Path to the reference audio file
    DURATION = 120 # Monitoring time in seconds
    CLICK_INTERVAL = 0.1 # Interval between clicks
    THRESHOLD = 40 # Similarity threshold for detection

    # Load the reference audio file
    def load_reference_audio(file):
    print("Loading reference audio...")
    y, sr = librosa.load(file, sr=44100)
    mfcc = librosa.feature.mfcc(y=y, sr=sr)
    return mfcc.mean(axis=1)

    # Monitor audio in real-time
    def monitor_audio(reference_features):
    print("Monitoring audio...")

    def callback(indata, frames, time, status):
    y = indata[:, 0] # Use the first audio channel
    mfcc = librosa.feature.mfcc(y=y, sr=44100)
    current_features = mfcc.mean(axis=1)

    # Calculate the distance between the current sound and the reference sound
    distance = euclidean(reference_features, current_features)
    print(f"Distance to reference sound: {distance}")

    if distance < THRESHOLD: # If the distance is below the threshold
    print("Sound detected! Performing three clicks.")
    perform_clicks()
    exit() # Stop the program after detection

    with sd.InputStream(callback=callback):
    time.sleep(DURATION)

    # Perform three mouse clicks
    def perform_clicks():
    for _ in range(3):
    pyautogui.click(-1519, 342) # Change (-1519, 342) to the desired coordinates
    print("Click performed.")
    time.sleep(CLICK_INTERVAL)

    # Run the program
    if __name__ == "__main__":
    reference_features = load_reference_audio(REFERENCE_AUDIO_FILE)
    monitor_audio(reference_features)

    Continue reading...

Compartilhe esta Página