I am having issues using a microphone in a Node script.
The microphone (a SEEED Array mic) works when running the command line:
arecord -D plughw:1 --duration=10 -f cd -vv ~/rectest.wav
It does not work, however, when called from a node script:
var record = require('node-record-lpcm16');
var fs = require('fs');
var file = fs.createWriteStream('test.wav', { encoding: 'binary' });
record.start({
sampleRate : 48000,
verbose : true,
source: 'plughw:1',
recordProgram: 'arecord'
}).pipe(file);
This sample script runs fine on my mac, but not on my Rpi Zero.
On Rpi Zero have no error message, the file created is empty.
I have tried a number of things (including combinations of source - 1/1,1/1,0/0,1/0,0) but I am having a hard time finding a way to debug.
I have a Speaker Bonnet from Adafruit connected to the RPI (but get the same error when disconnecting it).
I am running node 8.9.3 and have built 'node-record-lpcm16' with no errors.
Any ideas what of what I could be doing wrong or ideas on how to further debug?
1 Answer 1
It seems like you have a small mistake regarding the naming of the device parameter. It's called device
and not source
, changing that should hopefully fix your problem.
Another possible small problem, which might actually not matter in this case, is that you are passing the encoding
parameter to createWriteStream
. This is meant for telling the stream how to convert JavaScript strings into binary data on disks, but the record
module will emit Buffer
instances and not strings.
Finally, a small tip is to use the dsnoop
module in alsa, so that multiple processes can record at the same time. This might not matter to you though.
Final program:
const record = require('node-record-lpcm16')
const fs = require('fs')
const file = fs.createWriteStream('test.wav')
record.start({
sampleRate: 48000,
verbose: true,
device: 'dsnoop:1,0',
recordProgram: 'arecord'
}).pipe(file)
Much of my knowledge here comes from writing a Vinyl LP streamer that streams my vinyl records to my Sonos, using a Raspberry PI Zero W. Feel free to check out the source for some inspiration: https://github.com/LinusU/lp-streamer