Created
February 7, 2018 23:32
-
-
Save slokhorst/9dde6da8304a4d71678fdb8b9ee397ae to your computer and use it in GitHub Desktop.
Monitor ALSA microphone audio level with Node.js (VU meter)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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!