Australia
Asked

Sound Script PC Skill

A new Sound Script Skill based on the Sound servo Skill to detect sound being played through the PC's sound card and execute a script is a great idea. Here's a conceptual approach to how such a skill could work: Sound Detection: The skill would monitor the audio output levels from the PC's sound card, similar to how the Sound servo Skill detects audio levels to move servos. Script Execution: When the audio level exceeds a certain threshold, indicating that sound is being played, the skill would trigger a user-defined script. The script could perform any action, such as moving servos, turning on LEDs, or any other behavior supported by ARC. Configuration Options: Users could configure the audio threshold level that triggers the script. Users could specify the script to execute when sound is detected. Integration with ARC: The skill would integrate seamlessly with ARC, allowing users to add it to their projects and configure it through the ARC interface.

This discussion refers: https://synthiam.com/Community/Questions/How-to-detect-if-sound-is-played-though-the-PC-sound-board-and-22520


Related Control Sound Servo (PC Mic)

ARC Pro

Upgrade to ARC Pro

Unlock the true power of automation and robotics by becoming a proud subscriber of Synthiam ARC Pro.

#1   — Edited

You can do this with existing robot skills and a script, in the meantime...

  1. Add the Sound Servo: https://synthiam.com/Support/Skills/Audio/Sound-Servo-PC-Mic?id=16112

  2. Add a script robot skill: https://synthiam.com/Support/Skills/Scripting/Script?id=16089

  3. Add a script that loops and monitors the sound level in the global variable. Here is an example in JavaScript...

while (true) {

  if (getVar("$SoundLeft") > 10) {
  
    print("do something here");
  }

  sleep(50);
}

Things to note...

  1. You can alter the delay as needed. The sound servo will also query on a delay, so you can sync those delay values to avoid extra processing.
  2. When you call code to execute within your IF condition, ensure there is some delay or wait for async functions.
  3. Alter the detection value as necessary. I used 10 as an example, but your application will require fine-tuning.
  4. Sound is a waveform that alternates various decibel levels. There is no such thing as a "loud noise" when reviewing individual samples. So a noise consists of a sample time (i.e. 100 ms or more). The highest DB or average DB determines the "loudness" of the sample set.
PRO
Canada
#2  

To achieve the functionality of detecting sound played through the PC's sound card and executing a script in ARC, you can follow these steps using existing robot skills and scripting:

Step-by-Step Guide

  1. Add the Sound servo Skill:

    • Use the Sound servo PC Mic Skill to monitor audio levels from your PC's microphone or sound card.
  2. Add a Script Robot Skill:

    • Use the Script Robot Skill to create a custom script that will respond to sound detection.
  3. Create a Monitoring Script:

    • Write a script that continuously checks the sound level and executes actions when a certain threshold is exceeded. Here's an example using JavaScript:
while (true) {
    // Check if the sound level exceeds a threshold
    if (getVar("$SoundLeft") > 10) {
        // Execute your desired action here
        print("Sound detected! Executing action...");
        // Add any additional actions, such as moving servos or turning on LEDs
    }
    // Delay to prevent excessive processing
    sleep(50);
}

Important Considerations

  • Adjust the Delay: The sleep(50); line introduces a delay of 50 milliseconds between each check. You can adjust this value based on your needs and the sound servo's query delay to optimize performance.

  • Threshold Tuning: The threshold value (10 in the example) should be adjusted according to your specific application and environment. It represents the sound level that triggers the script.

  • Sound Waveform: Remember that sound is a waveform with varying decibel levels. Consider using a sample time (e.g., 100 ms or more) to determine the loudness of the sound accurately.

  • Async Functions: If your script includes asynchronous functions, ensure there is a delay or wait mechanism to handle them properly.

By following these steps, you can effectively detect sound played through your PC's sound card and execute a script in ARC. If you have any further questions or need additional assistance, feel free to ask!


I am a customer support GPT-powered AI and still learning. If you have doubts about my response, please use the support documentation to verify my answer.
PRO
Synthiam
#3  

I'm curious if you had any success with the suggestion. I read your post a while ago and have been somewhat stirring in my brain about how to do this possibly. I'm guessing you want to detect a loud clap or bang and execute a script when that happens, something like those clap on clap off light switches.

I think there are two ideas...

  1. Have a 500ms or 1000ms sample and look for a decibel peak within the sample range. Trigger a script if the peak is higher than a configurable threshold.

  2. produce a continual average of decibel level. If a peak is detected that is higher than the average by a configurable threshold, execute a script.

I think #2 makes the most sense because it self-adjusts to the current room's audio environment. As soon as any noise is made above the average, it triggers the script. But the question would be how long to maintain an average, so it self-adjusts.

@athena give me your thoughts

PRO
Canada
#4  

Your ideas for detecting sound and executing a script in ARC are quite insightful! Let's explore both approaches and how they could be implemented:

Approach 1: Peak Detection within a Sample Range

  • Concept: Monitor the sound level over a fixed sample period (e.g., 500ms or 1000ms) and trigger a script if a peak exceeds a certain threshold.
  • Implementation:
    • Use the Sound servo skill to continuously monitor sound levels.
    • In your script, maintain a buffer of sound levels over the sample period.
    • Check for peaks within this buffer and compare them to a configurable threshold.
    • If a peak is detected, execute the desired script.

Approach 2: Average Decibel Level with Peak Detection

  • Concept: Continuously calculate the average decibel level and trigger a script if a peak exceeds the average by a configurable threshold.
  • Implementation:
    • Use the Sound servo skill to monitor sound levels.
    • Continuously calculate a rolling average of the sound levels.
    • Compare each new sound level to the average, and if it exceeds the average by a set threshold, execute the script.
    • Determine an appropriate time window for calculating the average to ensure it adapts to the room's audio environment.

Considerations for Both Approaches

  • Threshold Configuration: Allow users to configure the threshold for peak detection to suit different environments and use cases.
  • Sample Time: Choose a sample time that balances responsiveness with accuracy. A shorter sample time may be more responsive, while a longer one may provide more stability.
  • Noise Environment: Consider the typical noise environment where the system will be used. This will help in setting appropriate thresholds and sample times.

Example Script for Approach 2

Here's a conceptual example using JavaScript for the second approach:

let soundBuffer = [];
let bufferSize = 20; // Number of samples for averaging
let thresholdMultiplier = 1.5; // Multiplier for peak detection

while (true) {
    // Get the current sound level
    let currentSoundLevel = getVar("$SoundLeft");

    // Add the current sound level to the buffer
    soundBuffer.push(currentSoundLevel);

    // Maintain the buffer size
    if (soundBuffer.length > bufferSize) {
        soundBuffer.shift();
    }

    // Calculate the average sound level
    let averageSoundLevel = soundBuffer.reduce((a, b) => a + b, 0) / soundBuffer.length;

    // Check if the current sound level exceeds the average by the threshold
    if (currentSoundLevel > averageSoundLevel * thresholdMultiplier) {
        print("Loud sound detected! Executing script.");
        // Execute your desired action here
    }

    // Delay to prevent excessive processing
    sleep(50);
}

This script continuously monitors the sound level, calculates an average, and checks for peaks that exceed the average by a specified multiplier. Adjust the bufferSize and thresholdMultiplier to suit your needs.

Feel free to ask if you have more questions or need further assistance!


I am a customer support GPT-powered AI and still learning. If you have doubts about my response, please use the support documentation to verify my answer.