Can some one help me detect outside sounds/noises

I am trying to detect outside sounds but I don’t know how to if anyone can help me please do.

What exactly do you mean? From the microphone? When an enemy makes a sound?

From the microphone.
Thank you

Paste this code inside of a default script

if(!window.averageSound)
navigator.mediaDevices.getUserMedia({
  audio: true,
  video: false
}).then(function(stream) {
    const audioContext = new AudioContext();
    const analyser = audioContext.createAnalyser();
    const microphone = audioContext.createMediaStreamSource(stream);
    const scriptProcessor = audioContext.createScriptProcessor(2048, 1, 1);
    
    analyser.smoothingTimeConstant = 0.8;
    analyser.fftSize = 1024;
    
    microphone.connect(analyser);
    analyser.connect(scriptProcessor);
    scriptProcessor.connect(audioContext.destination);
    scriptProcessor.onaudioprocess = function() {
        const array = new Uint8Array(analyser.frequencyBinCount);
        analyser.getByteFrequencyData(array);
        const arraySum = array.reduce((a, value) => a + value, 0);
        window.averageSound = arraySum / array.length;
    };
}).catch(function(err) {
    /* handle the error */
    console.error(err);
});

Then the variable averageSound should go up and down based on how high you talk into your mic. Also note that you’ll need to enable access to your mic.

Credit:

Example file: Outside Sound.wick (4.7 KB)
File preview:

1 Like