Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

153
Views
Defining a function and a variable in HTML5 audio code

I am not a programmer, I'm just trying to make a project for my work. I tried to learn some Javascript 10 years ago, but I haven't been working with programming since that time. The question is simple for real programmers.

I've found some code that shows the level of audio from microphone input. What do I need to do:

  • define a function to execute this code (or a part of it). This function must return this number: console.log(Math.round(average)).
  • define a variable that takes the result of this function so that I should be able to take this variable and pass it to another function.

Thank you.

navigator.mediaDevices.getUserMedia({ audio: true }).then(function(stream) {
  audioContext = new AudioContext();
  analyser = audioContext.createAnalyser();
  microphone = audioContext.createMediaStreamSource(stream);
  javascriptNode = audioContext.createScriptProcessor(2048, 1, 1);
  analyser.smoothingTimeConstant = 0.8;
  analyser.fftSize = 1024;
  microphone.connect(analyser);
  analyser.connect(javascriptNode);
  javascriptNode.connect(audioContext.destination);
  javascriptNode.onaudioprocess = function() {
      var array = new Uint8Array(analyser.frequencyBinCount);
      analyser.getByteFrequencyData(array);
      var values = 0;

      var length = array.length;
      for (var i = 0; i < length; i++) {
        values += (array[i]);
      }

      var average = values / length;

      if(Math.round(average)>15)
      {
        console.log(Math.round(average));
        document.getElementById("lvl").innerHTML = Math.round(average)-10;
      }
    
  }
  })
  .catch(function(err) {
    /* handle the error */
});

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!