HTML Audio recording until silence?
Asked Answered
B

3

19

I'm looking for a browser-based way of recording until a silence occurs.

HTML audio recording from the microphone is possible in Firefox and Chrome - using Recordmp3js see: http://nusofthq.com/blog/recording-mp3-using-only-html5-and-javascript-recordmp3-js/ and the code on github: http://github.com/nusofthq/Recordmp3js

I can't see a way to change that code to record until silence.

Record until silence can be done (and tuned) using Java for a native Android App - see here: Android audio capture silence detection

Google Voice Search demonstrates a browser can doit - but how can I using Javascript? Any ideas?

Brodeur answered 1/7, 2014 at 17:18 Comment(4)
I'm sure you could figure it out from the code used in pitch detection: github.com/cwilso/pitchdetectMagdeburg
Do you want true digital silence, or sound level below a threshold?Horripilation
a simple threshold would do me.Brodeur
Once the recording is made I know how to run through the audio buffer and threshold to get audio gaps - it's while it is being recorded the I don't know how to access the audio stream and throw a stop-recording event.Brodeur
G
14

If you use the Web Audio API, open up a live microphone audio capture by making a call to : navigator.getUserMedia , then create a node using : createScriptProcessor, then you assign to that node a callback for its event : onaudioprocess . Inside your callback function (below I use script_processor_analysis_node) you have access to the live real-time audio buffer to which you can then parse looking for silence (some length of time where amplitude is low [stays close to zero]).

for normal time domain audio curve see : array_time_domain which is populated fresh upon each call to callback script_processor_analysis_node ... similarly for frequency domain see array_freq_domain

Turn down your speaker volume or use headphones to avoid feedback from mic -> speaker -> mic ...

<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>capture microphone then show time & frequency domain output</title>

<script type="text/javascript">

var webaudio_tooling_obj = function () {

    var audioContext = new AudioContext();

    console.log("audio is starting up ...");

    var BUFF_SIZE_RENDERER = 16384;

    var audioInput = null,
    microphone_stream = null,
    gain_node = null,
    script_processor_node = null,
    script_processor_analysis_node = null,
    analyser_node = null;

    if (!navigator.getUserMedia)
        navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia ||
    navigator.mozGetUserMedia || navigator.msGetUserMedia;

    if (navigator.getUserMedia){

        navigator.getUserMedia({audio:true}, 
            function(stream) {
                start_microphone(stream);
            },
            function(e) {
                alert('Error capturing audio.');
            }
            );

    } else { alert('getUserMedia not supported in this browser.'); }

    // ---

    function show_some_data(given_typed_array, num_row_to_display, label) {

        var size_buffer = given_typed_array.length;
        var index = 0;

        console.log("__________ " + label);

        if (label === "time") {

            for (; index < num_row_to_display && index < size_buffer; index += 1) {

                var curr_value_time = (given_typed_array[index] / 128) - 1.0;

                console.log(curr_value_time);
            }

        } else if (label === "frequency") {

            for (; index < num_row_to_display && index < size_buffer; index += 1) {

                console.log(given_typed_array[index]);
            }

        } else {

            throw new Error("ERROR - must pass time or frequency");
        }
    }

    function process_microphone_buffer(event) {

        var i, N, inp, microphone_output_buffer;

        microphone_output_buffer = event.inputBuffer.getChannelData(0); // just mono - 1 channel for now
    }

    function start_microphone(stream){

        gain_node = audioContext.createGain();
        gain_node.connect( audioContext.destination );

        microphone_stream = audioContext.createMediaStreamSource(stream);
        microphone_stream.connect(gain_node); 

        script_processor_node = audioContext.createScriptProcessor(BUFF_SIZE_RENDERER, 1, 1);
        script_processor_node.onaudioprocess = process_microphone_buffer;

        microphone_stream.connect(script_processor_node);

        // --- enable volume control for output speakers

        document.getElementById('volume').addEventListener('change', function() {

            var curr_volume = this.value;
            gain_node.gain.value = curr_volume;

            console.log("curr_volume ", curr_volume);
        });

        // --- setup FFT

        script_processor_analysis_node = audioContext.createScriptProcessor(2048, 1, 1);
        script_processor_analysis_node.connect(gain_node);

        analyser_node = audioContext.createAnalyser();
        analyser_node.smoothingTimeConstant = 0;
        analyser_node.fftSize = 2048;

        microphone_stream.connect(analyser_node);

        analyser_node.connect(script_processor_analysis_node);

        var buffer_length = analyser_node.frequencyBinCount;

        var array_freq_domain = new Uint8Array(buffer_length);
        var array_time_domain = new Uint8Array(buffer_length);

        console.log("buffer_length " + buffer_length);

        script_processor_analysis_node.onaudioprocess = function() {

            // get the average for the first channel
            analyser_node.getByteFrequencyData(array_freq_domain);
            analyser_node.getByteTimeDomainData(array_time_domain);

            // draw the spectrogram
            if (microphone_stream.playbackState == microphone_stream.PLAYING_STATE) {

                show_some_data(array_freq_domain, 5, "frequency");
                show_some_data(array_time_domain, 5, "time"); // store this to record to aggregate buffer/file

// examine array_time_domain for near zero values over some time period

            }
        };
    }

}(); //  webaudio_tooling_obj = function()

</script>

</head>
<body>

    <p>Volume</p>
    <input id="volume" type="range" min="0" max="1" step="0.1" value="0.5"/>

</body>
</html>
Groves answered 1/7, 2014 at 20:9 Comment(2)
that sounds promising I'll take a look - and get back asapBrodeur
Hey Scott and @Brodeur How does this tell me when it is silent. I want to achieve something similar and want to trigger a function when it is silent and user has completed the voice inputCavie
S
9

This is an old post but I am sure many will have the same problem so I am posting my solution here. Use hark.js

Below is sample demo code i used for my electron app

  hark = require('./node_modules/hark/hark.bundle.js')

  navigator.getUserMedia({ audio : true}, onMediaSuccess, function(){});

  function onMediaSuccess(blog) {

    var options = {};
    var speechEvents = hark(blog, options);

    speechEvents.on('speaking', function() {
      console.log('speaking');
    });

    speechEvents.on('stopped_speaking', function() {
      console.log('stopped_speaking');
    });
  };
Sherrard answered 13/2, 2018 at 19:40 Comment(0)
T
0

The solution from @Scott Stensland doesn't allow me to parse for silence. I am getting the same value for when I parse the two arrays - that is I'm getting 0 always when parsing arrayFreqDomain and 128 always when parsing arrayTimeDomain

let analyser = context.createAnalyser();
analyser.smoothingTimeConstant = 0;
analyser.fftSize = 2048;
let buffLength = analyser.frequencyBinCount;
let arrayFreqDomain = new Uint8Array(buffLength);
let arrayTimeDomain = new Uint8Array(buffLength);
processor.connect(analyser);

processor.onaudioprocess = (event) => {
   /**
   *
   * Parse live real-time buffer looking for silence
   *
   **/
   analyser.getByteFrequencyData(arrayFreqDomain);
   analyser.getByteTimeDomainData(arrayTimeDomain);

   if (context.state === "running") {
        let sizeBuffer = arrayTimeDomain.length;
        let index = 0;
        for (; index < 5 && index < sizeBuffer; index += 1) {

            console.log(arrayTimeDomain[index]); <----
        }
   }
}
Tilda answered 13/2, 2019 at 15:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.