Skip to content

Instantly share code, notes, and snippets.

@slokhorst
Created February 7, 2018 23:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save slokhorst/9dde6da8304a4d71678fdb8b9ee397ae to your computer and use it in GitHub Desktop.
Save slokhorst/9dde6da8304a4d71678fdb8b9ee397ae to your computer and use it in GitHub Desktop.
Monitor ALSA microphone audio level with Node.js (VU meter)
const { spawn } = require('child_process');
arOptions = {
channels: 2,
rate: 16000,
format: 'S16_LE',
device: 'front:CARD=C920' // find out with `arecord -L`
}
callback = function(level) {
console.log("level: "+level)
};
const arProcess = spawn('arecord', [
'-c', arOptions.channels,
'-r', arOptions.rate,
'-f', arOptions.format,
'-D', arOptions.device,
'-V', 'mono'
], { stdio: ['ignore', 'ignore', 'pipe'] });
arProcess.stderr.on('data', function(data) {
let level = parseInt(String(data).substr(54,2));
if (isNaN(level)) {
console.log(String(data))
return;
}
callback(level);
});
@alexdufetel
Copy link

I'm trying to use this example in a setting where I want to both display volume and pipe the recording to another service (for hotword detection). When I change the stdio options from stdio: ['ignore', 'ignore', 'pipe'] to stdio: ['ignore', 'pipe', 'pipe'] the script freezes after a few seconds. Any idea what's going on? Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment