Problem with Javascript code, trying to make it so that if detects claps or loud pitched noises...

This is a place for questions about any other programming language.
Post Reply
User avatar
Tom Mac
New User
Posts: 4
Joined: 22 May 2018, 08:37

Problem with Javascript code, trying to make it so that if detects claps or loud pitched noises...

Post by Tom Mac » 28 Oct 2018, 23:48

i want to make it so that if it detects loud noises for more than 5 seconds it will trigger a message but i don't know what my mistake is. Basically, i tried to make it so that on the last part if it detects the first clap then the interval function starts and while it is detecting claps, if it goes for 5 seconds then it will say baby is crying.

Code: Select all

  <style>* {box-sizing: border-box;}</style>
  <p>If you're baby is cryin clap your hands!</p>
    var Recording = function(cb){
      var recorder = null;
      var recording = true;
      var audioInput = null;
      var volume = null;
      var audioContext = null;
      var callback = cb;

      navigator.getUserMedia = navigator.getUserMedia    || navigator.webkitGetUserMedia ||
                               navigator.mozGetUserMedia || navigator.msGetUserMedia;

          function(e){ //success
            var AudioContext = window.AudioContext || window.webkitAudioContext;
            audioContext = new AudioContext();
            volume = audioContext.createGain(); // creates a gain node
            audioInput = audioContext.createMediaStreamSource(e); // creates an audio node from the mic stream
            audioInput.connect(volume);// connect the stream to the gain node
            recorder = audioContext.createScriptProcessor(2048, 1, 1);

            recorder.onaudioprocess = function(e){
                if(!recording) return;
                var left = e.inputBuffer.getChannelData(0);
                //var right = e.inputBuffer.getChannelData(1);
                callback(new Float32Array(left));
            volume.connect(recorder);// connect the recorder
          function(e){ //failure
            alert('Error capturing audio.');
      } else {
        alert('getUserMedia not supported in this browser.');

    var lastClap = (new Date()).getTime();


    function detectClap(data){
      var t = (new Date()).getTime();
      if(t - lastClap < 200) return false; // TWEAK HERE
      var zeroCrossings = 0, highAmp = 0;
      for(var i = 1; i < data.length; i++){
        if(Math.abs(data[i]) > 0.3) highAmp++; // TWEAK HERE
        if(data[i] > 0 && data[i-1] < 0 || data[i] < 0 && data[i-1] > 0) zeroCrossings++;
      if(highAmp > 20 && zeroCrossings > 30){ // TWEAK HERE
        //console.log(highAmp+' / '+zeroCrossings);
        lastClap = t;

        return true;
      return false;

    var seconds = 0

    var rec = new Recording(function(data){
      while (detectClap(data)){
        myVar = setInterval(myTimer, 1000);
        function myTimer(){
          seconds = seconds + 1
        if(seconds = 3){
          alert("Baby is crying")


1 x

"I won't run away anymore... I won't go back on my word... that is my ninja way!" - Naruto

User avatar
Mr. MacKenty
Site Admin
Posts: 88
Joined: 28 Apr 2018, 17:06
Answers: 3

Re: Problem with Javascript code, trying to make it so that if detects claps or loud pitched noises...

Post by Mr. MacKenty » 29 Oct 2018, 08:47

Hey Tom !

I understand your problem. I'm confused what you are trying to do to diagnose the problem.

I think your problem has a few parts:

1. assign a variable to volume
2. assign a variable to check the difference between start time / end time

I'm wondering how do you know your code works or doesn't work?

Please review this article:
Please review this article: ... avascript/

The second link is especially good. The key thing is you have to understand how to debug your code. It shouldn't be a mystery. some classic strategies are to to only run a small piece of code and then slowly add small bits.

Trust me when I tell you: invest 2 hours into learning more about debugging and life will be SO MUCH EASIER for you.

I wish you the best of luck, and I stand ready to answer another question for you.
0 x

Post Reply