├── .gitignore
├── LICENSE
├── README.md
├── index.js
└── package.json
/.gitignore:
--------------------------------------------------------------------------------
1 | ## Ignore Visual Studio temporary files, build results, and
2 | ## files generated by popular Visual Studio add-ons.
3 |
4 | # User-specific files
5 | *.suo
6 | *.user
7 | *.userosscache
8 | *.sln.docstates
9 |
10 | # User-specific files (MonoDevelop/Xamarin Studio)
11 | *.userprefs
12 |
13 | # Build results
14 | [Dd]ebug/
15 | [Dd]ebugPublic/
16 | [Rr]elease/
17 | [Rr]eleases/
18 | x64/
19 | x86/
20 | build/
21 | bld/
22 | [Bb]in/
23 | [Oo]bj/
24 |
25 | # Visual Studio 2015 cache/options directory
26 | .vs/
27 | *.sln
28 | *.njsproj
29 |
30 | # MSTest test Results
31 | [Tt]est[Rr]esult*/
32 | [Bb]uild[Ll]og.*
33 |
34 | # NUNIT
35 | *.VisualState.xml
36 | TestResult.xml
37 |
38 | # Build Results of an ATL Project
39 | [Dd]ebugPS/
40 | [Rr]eleasePS/
41 | dlldata.c
42 |
43 | # DNX
44 | project.lock.json
45 | artifacts/
46 |
47 | *_i.c
48 | *_p.c
49 | *_i.h
50 | *.ilk
51 | *.meta
52 | *.obj
53 | *.pch
54 | *.pdb
55 | *.pgc
56 | *.pgd
57 | *.rsp
58 | *.sbr
59 | *.tlb
60 | *.tli
61 | *.tlh
62 | *.tmp
63 | *.tmp_proj
64 | *.log
65 | *.vspscc
66 | *.vssscc
67 | .builds
68 | *.pidb
69 | *.svclog
70 | *.scc
71 |
72 | # Chutzpah Test files
73 | _Chutzpah*
74 |
75 | # Visual C++ cache files
76 | ipch/
77 | *.aps
78 | *.ncb
79 | *.opensdf
80 | *.sdf
81 | *.cachefile
82 |
83 | # Visual Studio profiler
84 | *.psess
85 | *.vsp
86 | *.vspx
87 |
88 | # TFS 2012 Local Workspace
89 | $tf/
90 |
91 | # Guidance Automation Toolkit
92 | *.gpState
93 |
94 | # ReSharper is a .NET coding add-in
95 | _ReSharper*/
96 | *.[Rr]e[Ss]harper
97 | *.DotSettings.user
98 |
99 | # JustCode is a .NET coding add-in
100 | .JustCode
101 |
102 | # TeamCity is a build add-in
103 | _TeamCity*
104 |
105 | # DotCover is a Code Coverage Tool
106 | *.dotCover
107 |
108 | # NCrunch
109 | _NCrunch_*
110 | .*crunch*.local.xml
111 |
112 | # MightyMoose
113 | *.mm.*
114 | AutoTest.Net/
115 |
116 | # Web workbench (sass)
117 | .sass-cache/
118 |
119 | # Installshield output folder
120 | [Ee]xpress/
121 |
122 | # DocProject is a documentation generator add-in
123 | DocProject/buildhelp/
124 | DocProject/Help/*.HxT
125 | DocProject/Help/*.HxC
126 | DocProject/Help/*.hhc
127 | DocProject/Help/*.hhk
128 | DocProject/Help/*.hhp
129 | DocProject/Help/Html2
130 | DocProject/Help/html
131 |
132 | # Click-Once directory
133 | publish/
134 |
135 | # Publish Web Output
136 | *.[Pp]ublish.xml
137 | *.azurePubxml
138 | ## TODO: Comment the next line if you want to checkin your
139 | ## web deploy settings but do note that will include unencrypted
140 | ## passwords
141 | #*.pubxml
142 |
143 | *.publishproj
144 |
145 | # NuGet Packages
146 | *.nupkg
147 | # The packages folder can be ignored because of Package Restore
148 | **/packages/*
149 | # except build/, which is used as an MSBuild target.
150 | !**/packages/build/
151 | # Uncomment if necessary however generally it will be regenerated when needed
152 | #!**/packages/repositories.config
153 |
154 | # Windows Azure Build Output
155 | csx/
156 | *.build.csdef
157 |
158 | # Windows Store app package directory
159 | AppPackages/
160 |
161 | # Visual Studio cache files
162 | # files ending in .cache can be ignored
163 | *.[Cc]ache
164 | # but keep track of directories ending in .cache
165 | !*.[Cc]ache/
166 |
167 | # Others
168 | ClientBin/
169 | [Ss]tyle[Cc]op.*
170 | ~$*
171 | *~
172 | *.dbmdl
173 | *.dbproj.schemaview
174 | *.pfx
175 | *.publishsettings
176 | node_modules/
177 | orleans.codegen.cs
178 |
179 | # RIA/Silverlight projects
180 | Generated_Code/
181 |
182 | # Backup & report files from converting an old project file
183 | # to a newer Visual Studio version. Backup files are not needed,
184 | # because we have git ;-)
185 | _UpgradeReport_Files/
186 | Backup*/
187 | UpgradeLog*.XML
188 | UpgradeLog*.htm
189 |
190 | # SQL Server files
191 | *.mdf
192 | *.ldf
193 |
194 | # Business Intelligence projects
195 | *.rdl.data
196 | *.bim.layout
197 | *.bim_*.settings
198 |
199 | # Microsoft Fakes
200 | FakesAssemblies/
201 |
202 | # Node.js Tools for Visual Studio
203 | .ntvs_analysis.dat
204 |
205 | # Visual Studio 6 build log
206 | *.plg
207 |
208 | # Visual Studio 6 workspace options file
209 | *.opt
210 |
211 | # LightSwitch generated files
212 | GeneratedArtifacts/
213 | _Pvt_Extensions/
214 | ModelManifest.xml
215 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2016 MexXxo
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # node-microphone
2 |
3 | 
4 | 
5 | 
6 | 
7 |
8 | ## Information
9 |
10 | node-microphone is a module that use arecord ALSA tools on Linux or SoX on Windows & OSX to start and stop recording audio from a USB Microphone in PCM
11 |
12 | ## Notice
13 |
14 | Version 0.1.0 is incompatible with 0.0.x !
15 | It is currently only tested with Windows (sox 14.4.2), will also be tested with Raspbian in the near future.
16 | As it uses ECMAScript 2015, it will probably not work with older Node versions (works with Node 5.10.0).
17 |
18 | For Windows, do not forget to add sox to your environment variables.
19 |
20 | ## Roadmap
21 |
22 | No official roadmap yet. If you experience issues, submit a PR and I will try to merge :-)
23 |
24 | ## Dependencies
25 |
26 | This library needs:
27 |
28 | * ALSA tools installed on the machine (`sudo apt-get install alsa-utils`) **for Linux**
29 | * SoX Tools installed on the machine **for Windows or OSX**
30 |
31 | ## Usage
32 |
33 | #### Simple example
34 |
35 | A simple example on how to use this module:
36 |
37 | let Mic = require('node-microphone');
38 | let mic = new Mic();
39 | let micStream = mic.startRecording();
40 | micStream.pipe( myWritableStream );
41 | setTimeout(() => {
42 | logger.info('stopped recording');
43 | mic.stopRecording();
44 | }, 3000);
45 | mic.on('info', (info) => {
46 | console.log(info);
47 | });
48 | mic.on('error', (error) => {
49 | console.log(error);
50 | });
51 |
52 |
53 | ## API
54 |
55 | ### new Class(options)
56 |
57 | Creates a new instance of the Microphone class. You can give an options object to the constructor with these parameters:
58 |
59 | | Option | Value | Default |
60 | |--------|-------|---------|
61 | | `endian` | `'big'` or `'little'` | `'little'` |
62 | | `bitwidth` | `8`, `16`, `24` * | `16` |
63 | | `encoding` | `'signed-integer'` or `'unsigned-integer'` | `'signed-integer'` |
64 | | `rate` | `8000`, `16000`, `44100` * | `16000` |
65 | | `channels` | `1`, `2` * | `1` (mono) |
66 | | `device` | `'hw:0,0'`, `'plughw:1,0'` * | |
67 | | `additionalParameters` | Array of raw string parameters to pass to `spawn()` | |
68 | | `useDataEmitter` | `true` or `false` - enables data via mic.on('data') | `false` | |
69 |
70 | * or any other value supported by arecord or sox.
71 |
72 | With sox, the `device` option is used as waveaudio driver.
73 |
74 | #### startRecording()
75 |
76 | Starts recording by creating a new child process with the options given to the constructor.
77 | Returns the recording PCM wave stream as Node stream.
78 |
79 | #### stopRecording();
80 |
81 | Stops the child process.
82 |
83 | #### events
84 |
85 | The Microphone class extends EventEmitter and emits info and error events.
86 | The [simple example](#simple-example) shows how to use them.
87 |
88 | ## CONTRIBUTORS
89 | Thanks to ashishbajaj99 and vincentsaluzzo for their node microphone modules.
90 |
91 | ## LICENSE
92 |
93 | (MIT License)
94 |
95 | Copyright (c) 2016 Carlos Knoke Flores
96 |
97 | Permission is hereby granted, free of charge, to any person obtaining
98 | a copy of this software and associated documentation files (the
99 | "Software"), to deal in the Software without restriction, including
100 | without limitation the rights to use, copy, modify, merge, publish,
101 | distribute, sublicense, and/or sell copies of the Software, and to
102 | permit persons to whom the Software is furnished to do so, subject to
103 | the following conditions:
104 |
105 | The above copyright notice and this permission notice shall be
106 | included in all copies or substantial portions of the Software.
107 |
108 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
109 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
110 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
111 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
112 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
113 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
114 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
115 |
--------------------------------------------------------------------------------
/index.js:
--------------------------------------------------------------------------------
1 | 'use strict';
2 | const isMac = require('os').type() == 'Darwin';
3 | const isWin = require('os').type().indexOf('Windows') > -1;
4 | const spawn = require('child_process').spawn;
5 | const EventEmitter = require('events');
6 |
7 | class Microphone extends EventEmitter {
8 | constructor(options) {
9 | super();
10 | this.ps = null;
11 |
12 | options = options || {};
13 | this.endian = options.endian || 'little';
14 | this.bitwidth = options.bitwidth || '16';
15 | this.encoding = options.encoding || 'signed-integer';
16 | this.rate = options.rate || '16000';
17 | this.channels = options.channels || '1';
18 | this.additionalParameters = options.additionalParameters || false;
19 | this.useDataEmitter = !!options.useDataEmitter;
20 | if (isWin) {
21 | this.device = options.device || 'default';
22 | }
23 | if (!isWin && !isMac) {
24 | this.device = options.device || 'plughw:1,0';
25 | this.format = undefined;
26 | this.formatEndian = undefined;
27 | this.formatEncoding = undefined;
28 |
29 | if (this.encoding === 'unsigned-integer') {
30 | this.formatEncoding = 'U';
31 | } else {
32 | this.formatEncoding = 'S';
33 | }
34 | if (this.endian === 'big') {
35 | this.formatEndian = 'BE';
36 | } else {
37 | this.formatEndian = 'LE';
38 | }
39 | this.format =
40 | this.formatEncoding + this.bitwidth + '_' + this.formatEndian;
41 | }
42 | }
43 |
44 | // end on silence - default threshold 0.5
45 | //'silence', '1', '0.1', options.threshold + '%',
46 | //'1', '1.0', options.threshold + '%'
47 |
48 | startRecording() {
49 | let audioOptions;
50 | if (this.ps === null) {
51 | if (isWin) {
52 | audioOptions = [
53 | '-b',
54 | this.bitwidth,
55 | '--endian',
56 | this.endian,
57 | '-c',
58 | this.channels,
59 | '-r',
60 | this.rate,
61 | '-e',
62 | this.encoding,
63 | '-t',
64 | 'waveaudio',
65 | this.device,
66 | '-p',
67 | ];
68 | if (this.additionalParameters) {
69 | audioOptions = audioOptions.concat(
70 | this.additionalParameters
71 | );
72 | }
73 | this.ps = spawn('sox', audioOptions);
74 | } else if (isMac) {
75 | audioOptions = [
76 | '-q',
77 | '-b',
78 | this.bitwidth,
79 | '-c',
80 | this.channels,
81 | '-r',
82 | this.rate,
83 | '-e',
84 | this.encoding,
85 | '-t',
86 | 'wav',
87 | '-',
88 | ];
89 | if (this.additionalParameters) {
90 | audioOptions = audioOptions.concat(
91 | this.additionalParameters
92 | );
93 | }
94 | this.ps = spawn('rec', audioOptions);
95 | } else {
96 | audioOptions = [
97 | '-c',
98 | this.channels,
99 | '-r',
100 | this.rate,
101 | '-f',
102 | this.format,
103 | '-D',
104 | this.device,
105 | ];
106 | if (this.additionalParameters) {
107 | audioOptions = audioOptions.concat(
108 | this.additionalParameters
109 | );
110 | }
111 | this.ps = spawn('arecord', audioOptions);
112 | }
113 | this.ps.on('error', (error) => {
114 | this.emit('error', error);
115 | });
116 | this.ps.stderr.on('error', (error) => {
117 | this.emit('error', error);
118 | });
119 | this.ps.stderr.on('data', (info) => {
120 | this.emit('info', info);
121 | });
122 | if (this.useDataEmitter) {
123 | this.ps.stdout.on('data', (data) => {
124 | this.emit('data', data);
125 | });
126 | }
127 | return this.ps.stdout;
128 | }
129 | }
130 |
131 | stopRecording() {
132 | if (this.ps) {
133 | this.ps.kill();
134 | this.ps = null;
135 | }
136 | }
137 | }
138 |
139 | module.exports = Microphone;
140 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "node-microphone",
3 | "version": "0.1.6",
4 | "description": "Allows Microphone access in node with arecord (Linux) and sox (Windows/OSX).",
5 | "private": false,
6 | "main": "index.js",
7 | "author": "Carlos Knoke Flores ",
8 | "engines": {
9 | "node": ">=4.0"
10 | },
11 | "license": "MIT",
12 | "keywords": [
13 | "microphone",
14 | "alsa",
15 | "mic",
16 | "record",
17 | "audio",
18 | "sox",
19 | "capture",
20 | "node-microphone",
21 | "arecord",
22 | "input"
23 | ],
24 | "dependencies": {},
25 | "scripts": {
26 | "test": ""
27 | },
28 | "repository": {
29 | "type": "git",
30 | "url": "git+https://github.com/MexXxo/node-microphone.git"
31 | },
32 | "bugs": {
33 | "url": "https://github.com/MexXxo/node-microphone/issues"
34 | },
35 | "homepage": "https://github.com/MexXxo/node-microphone#readme"
36 | }
37 |
--------------------------------------------------------------------------------