npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

node-mic

v1.0.1

Published

Microphone streaming library for Node.js

Downloads

282

Readme

node-mic

A cross-platform Node.js package for recording audio streams from a microphone.

Installation

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

Example

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

API

Mic(options)

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)

mic.start()

Starts the recording. Emits the 'started' signal.

mic.stop()

Stops the recording. Emits the 'stopped' signal.

mic.pause()

Pauses the recording. Emits the 'paused' signal.

mic.resume()

Resumes the recording. Emits the 'unpaused' signal.

mic.getAudioStream()

This returns a simple Transform stream that contains the data. The stream can be directly piped to a speaker or a file.

mic.on('data')

This signal is emitted every time data is available. data is an audio waveform.

mic.on('exit')

This signal is emitted when the recording process has finished.

mic.on('error')

This signal is emitted whenever an error occurs.

License

The MIT License (MIT). See LICENSE for more information.

Copyright (c) 2021 Alex Shaw [email protected]

Forked from mic.

Copyright (c) 2016 Ashish Bajaj [email protected]