├── .npmignore ├── .gitignore ├── package.json ├── index.js └── README.md /.npmignore: -------------------------------------------------------------------------------- 1 | .nide 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .nide 2 | node_modules/ -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "microphone", 3 | "description": "microphone is a simple module that use `arecord` ALSA tools method to capture sound from a USB Microphone", 4 | "version": "1.0.8", 5 | "author": "Vincent Saluzzo", 6 | "contributors": [ 7 | { 8 | "name": "JiaJian", 9 | "email": "its@jiajian.me" 10 | } 11 | ], 12 | "dependencies": { 13 | "lame": "1.0.3" 14 | }, 15 | "devDependencies": {}, 16 | "homepage": "http://github.com/vincentsaluzzo/node-microphone", 17 | "keywords": [ 18 | "microphone", 19 | "alsa", 20 | "mic", 21 | "record", 22 | "audio", 23 | "capture" 24 | ], 25 | "repository": { 26 | "type": "git", 27 | "url": "https://github.com/vincentsaluzzo/node-microphone.git" 28 | }, 29 | "bugs": "http://github.com/vincentsaluzzo/node-microphone/issues" 30 | } 31 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var isMacOrWin = require('os').type() == 'Darwin' || require('os').type().indexOf('Windows') > -1; 2 | var spawn = require('child_process').spawn 3 | var PassThrough = require('stream').PassThrough; 4 | var lame = require('lame'); 5 | 6 | var ps = null; 7 | 8 | var audio = new PassThrough; 9 | var info = new PassThrough; 10 | 11 | var start = function(options) { 12 | options = options || {}; 13 | 14 | if(ps == null) { 15 | ps = isMacOrWin 16 | ? spawn('sox', ['-d', '-t', 'dat', '-p']) 17 | : spawn('arecord', ['-D', 'plughw:1,0', '-f', 'dat']); 18 | 19 | if(options.mp3output === true) { 20 | var encoder = new lame.Encoder( { 21 | channels: 2, 22 | bitDepth: 16, 23 | sampleRate: 44100 24 | }); 25 | 26 | ps.stdout.pipe(encoder); 27 | encoder.pipe(audio); 28 | ps.stderr.pipe(info); 29 | 30 | } else { 31 | ps.stdout.pipe(audio); 32 | ps.stderr.pipe(info); 33 | 34 | } 35 | } 36 | }; 37 | 38 | var stop = function() { 39 | if(ps) { 40 | ps.kill(); 41 | ps = null; 42 | } 43 | }; 44 | 45 | exports.audioStream = audio; 46 | exports.infoStream = info; 47 | exports.startCapture = start; 48 | exports.stopCapture = stop; 49 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Information 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 |
Packagemicrophone
Descriptionmicrophone is a simple module that use `arecord` ALSA tools on Linux or SoX on OSX & Windows method to capture sound from a USB Microphone
12 | 13 | ## Dependencies 14 | 15 | This library need 16 | 17 | * ALSA tools installed on the machine (`sudo apt-get install alsa-utils`) **for Linux** 18 | * SoX Tools installed on the machine **for Windows or OSX** 19 | 20 | ## Usage 21 | 22 | #### Simple example 23 | 24 | A simple example which capture sound and redirect it to stdout. 25 | 26 | var mic = require('microphone'); 27 | 28 | mic.startCapture(); 29 | 30 | mic.audioStream.on('data', function(data) { 31 | process.stdout.write(data); 32 | }); 33 | 34 | ## API 35 | 36 | #### startCapture(options) 37 | 38 | Start the process and pipe the stdout of ALSA `arecord` tool to audioStream 39 | 40 | By default, the outputing sound are PCM WAVE format, if you want a MP3 format 41 | pass `true` as `mp3Output` in the options passed in arguments. 42 | 43 | (Example : `mic.startCapture({'mp3output' : true});`) 44 | 45 | #### stopCapture(); 46 | 47 | Stop the process 48 | 49 | #### audioStream 50 | 51 | Give the audio stream where sound captured will be pushed 52 | 53 | #### infoStream 54 | 55 | Give an information stream which display informations printed on `stderr` by the process 56 | 57 | ## CONTRIBUTORS 58 | 59 | * @JiaJian *(for Windows support)* 60 | * @guileen *(for OSX support)* 61 | * @jrf0110 62 | 63 | 64 | ## LICENSE 65 | 66 | (MIT License) 67 | 68 | Copyright (c) 2012 Vincent Saluzzo 69 | 70 | Permission is hereby granted, free of charge, to any person obtaining 71 | a copy of this software and associated documentation files (the 72 | "Software"), to deal in the Software without restriction, including 73 | without limitation the rights to use, copy, modify, merge, publish, 74 | distribute, sublicense, and/or sell copies of the Software, and to 75 | permit persons to whom the Software is furnished to do so, subject to 76 | the following conditions: 77 | 78 | The above copyright notice and this permission notice shall be 79 | included in all copies or substantial portions of the Software. 80 | 81 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 82 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 83 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 84 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 85 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 86 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 87 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. --------------------------------------------------------------------------------