Product
Introducing SSO
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
node-mic
Advanced tools
Readme
A cross-platform Node.js package for recording audio streams from a microphone.
Node-mic uses arecord on Linux and sox on Windows and macOS. If sox is not already installed, node-mic will attempt to download the latest version.
On the new M1 Mac, that requires installing brew.sh (Intel Macs should work fine without it).
On Linux, it will warn if arecord is not found and remind you to install it using your package manager.
On Debian-based distros, try running this:
sudo apt-get update && sudo apt-get install alsa-utils
On RedHat-based distros, try running this:
sudo dnf install alsa-utils
After that's done, you can try testing it to make sure your microphone is detected.
arecord temp.wav
import fs from 'fs';
import NodeMic from 'node-mic';
const mic = new NodeMic({
rate: 16000,
channels: 1,
threshold: 6
});
const micInputStream = mic.getAudioStream();
const outputFileStream = fs.createWriteStream('output.raw');
micInputStream.pipe(outputFileStream);
micInputStream.on('data', (data) => {
// Do something with the data.
});
micInputStream.on('error', (err) => {
console.log(`Error: ${err.message}`);
});
micInputStream.on('started', () => {
console.log('Started');
setTimeout(() => {
mic.pause();
}, 5000);
});
micInputStream.on('stopped', () => {
console.log('Stopped');
});
micInputStream.on('paused', () => {
console.log('Paused');
setTimeout(() => {
mic.resume();
}, 5000);
});
micInputStream.on('unpaused', () => {
console.log('Unpaused');
setTimeout(() => {
mic.stop();
}, 5000);
});
micInputStream.on('silence', () => {
console.log('Silence');
});
micInputStream.on('exit', (code) => {
console.log(`Exited with code: ${code}`);
});
mic.start();
The above example should save the output to a file named output.raw
. Note that arecord pipes the 44 byte WAV header, whereas SOX does not. So we need to provide the file format details to the player:
aplay -f S16_LE -r 16000 -c 1 output.raw # Linux
or
play -b 16 -e signed -c 1 -r 16000 output.raw # Windows/Mac
Creates a new microphone object. It inherits all the events from stream.Transform.
Accepts the following parameters (all optional):
options
endian
: Endian format. (default little
)bitwidth
: Bit size. (default: 16
)encoding
: Signed or unsigned. (default: signed-integer
)rate
: Bit rate. (default: 16000
)channels
: Number of channels. (default: 1
)device
: Which device to use. Linux only. (default: plughw:0,0
)threshold
: The 'silence'
signal is raised after reaching these many consecutive frames. (default: 0
)fileType
: Recording format. raw
or wav
. (default: raw
)debug
: If true
then extra debug information is printed. (default: false
)Starts the recording. Emits the 'started'
signal.
Stops the recording. Emits the 'stopped'
signal.
Pauses the recording. Emits the 'paused'
signal.
Resumes the recording. Emits the 'unpaused'
signal.
This returns a simple Transform
stream that contains the data. The stream can be directly piped to a speaker or a file.
This signal is emitted every time data is available. data
is an audio waveform.
This signal is emitted when the recording process has finished.
This signal is emitted whenever an error occurs.
The MIT License (MIT). See LICENSE for more information.
Copyright (c) 2021 Alex Shaw alex.shaw.as@gmail.com
Forked from mic.
Copyright (c) 2016 Ashish Bajaj bajaj.ashish@gmail.com
FAQs
Microphone streaming library for Node.js
The npm package node-mic receives a total of 12 weekly downloads. As such, node-mic popularity was classified as not popular.
We found that node-mic demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.