- arecord:
sudo apt-get install alsa-utils
- lodash:
npm install lodash
const Microphone = require('./path/to/microphone');
const mic = new Microphone();
mic.pipe(someWritable);
setTimeout(() => mic.stop(), 5000);
// @flow | |
'use strict'; | |
const PassThrough = require('stream').PassThrough; | |
const spawn = require('child_process').spawn; | |
const _ = require('lodash'); | |
type Options = { | |
channels?: number, | |
sampleRate?: number, | |
endianness?: 'LE', | |
byteRate?: 16 | |
}; | |
class Microphone extends PassThrough { | |
_proc: Object; | |
constructor (options?: Options) { | |
super(); | |
options = _.defaults(options || {}, { | |
channels: 1, | |
sampleRate: 16000, | |
endianness: 'LE', | |
byteRate: 16 | |
}); | |
this._proc = spawn('arecord', [ | |
'-q', | |
'-c', options.channels, | |
'-r', options.sampleRate, | |
'-f', `S${options.byteRate}_${options.endianness}`, // S=signed, 16bits, little-endian, | |
'-t', 'raw' | |
], { | |
stdio: [ 'ignore', 'pipe', 'ignore' ] // pipe stdout only | |
}); | |
this._proc.stdout.pipe(this); | |
} | |
stop () { | |
this._proc.kill(); | |
} | |
} | |
module.exports = Microphone; |