audio-activity

Detect audio activity of a MediaStream. Only works in modern browsers that support the Web Audio API.

Usage no npm install needed!

<script type="module">
  import audioActivity from 'https://cdn.skypack.dev/audio-activity';
</script>

README

audio-activity

Detect audio activity of a MediaStream. Only works in modern browsers that support the Web Audio API.

npm install audio-activity

See the live demo.

Usage

var audioActivity = require('audio-activity');

navigator.getUserMedia({ audio: true, video: false },
    function(stream) {
        var audio = audioActivity(stream, function(level) {
            // 'level' indicates the audio activity in percentage
            console.log(level);
        });

        setTimeout(function() {
            // Call the destroy method to cleanup resources
            audio.destroy();
        }, 5000);
    },
    function(e) {
        console.error(e);
    });

The callback function is optional, by omitting it it's still possible to read the activity level using the audio.get() method.

requestAnimationFrame(function draw() {
    var level = audio.get();
    console.log(level);

    requestAnimationFrame(draw);
});