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-portaudio

v0.4.10

Published

Node Stream bindings for PortAudio.

Downloads

18

Readme

node-portaudio

A Node.js addon that provides a wrapper around the PortAudio library, enabling an application to record and play audio with cross platform support. With this library, you can create node.js streams that can be piped to or from other streams, such as files and network connections. This library supports back-pressure.

This is a fork of naudiodon, refactored by:

  • porting the entire codebase to TypeScript
  • adding better documentation
  • adding some more functionality

See forked repository for credits for corresponding contributions.

Note: This is a server side library. It is not intended as a means to play and record audio via a browser.

Installation

Install Node.js for your platform. This software has been developed against the long term stable (LTS) release. For ease of installation with other node packages, this package includes a copy of the dependent PortAudio library and so has no prerequisites.

node-portaudio is designed to be required or imported to use from your own application to provide async processing. For example:

npm install --save node-portaudio

For Raspberry Pi users, please note that this library is not intended for use with the internal sound card. Please use an external USB sound card or GPIO breakout board such as the Pi-DAC+ Full-HD Audio Card.

Using node-portaudio

If you are using regular Node.js, include the library with:

const portAudio = require('node-portaudio');

If you are using TypeScript, definitions have been provided and you can import the library with:

import * as portAudio from 'node-portaudio';

Listing devices

To get list of supported devices, call the getDevices() function.

const portAudio = require('node-portaudio');

console.log(portAudio.getDevices());

An example of the output is:

[ { id: 0,
    name: 'Built-in Microph',
    maxInputChannels: 2,
    maxOutputChannels: 0,
    defaultSampleRate: 44100,
    defaultLowInputLatency: 0.00199546485260771,
    defaultLowOutputLatency: 0.01,
    defaultHighInputLatency: 0.012154195011337868,
    defaultHighOutputLatency: 0.1,
    hostAPIName: 'Core Audio' },
  { id: 1,
    name: 'Built-in Input',
    maxInputChannels: 2,
    maxOutputChannels: 0,
    defaultSampleRate: 44100,
    defaultLowInputLatency: 0.00199546485260771,
    defaultLowOutputLatency: 0.01,
    defaultHighInputLatency: 0.012154195011337868,
    defaultHighOutputLatency: 0.1,
    hostAPIName: 'Core Audio' },
  { id: 2,
    name: 'Built-in Output',
    maxInputChannels: 0,
    maxOutputChannels: 2,
    defaultSampleRate: 44100,
    defaultLowInputLatency: 0.01,
    defaultLowOutputLatency: 0.002108843537414966,
    defaultHighInputLatency: 0.1,
    defaultHighOutputLatency: 0.012267573696145125,
    hostAPIName: 'Core Audio' } ]

Note that the device id parameter index value can be used as to specify which device to use for playback or recording with optional parameter deviceId.

Playing audio

Playing audio involves streaming audio data to an instance of AudioOutput.

const fs = require('fs');
const portAudio = require('node-portaudio');

// Create an instance of AudioOutput, which is a WriteableStream
const ao = new portAudio.AudioOutput({
  channelCount: 2,
  sampleFormat: portAudio.SampleFormat16Bit,
  sampleRate: 48000,
  deviceId : -1 // Use -1 or omit the deviceId to select the default device
});

// handle errors from the AudioOutput
ao.on('error', err => console.error);

// Create a stream to pipe into the AudioOutput
// Note that this does not strip the WAV header so a click will be heard at the beginning
const rs = fs.createReadStream('steam_48000.wav');

// setup to close the output stream at the end of the read stream
rs.on('end', () => ao.end());

// Start piping data and start streaming
rs.pipe(ao);
ao.start();

Recording audio

Recording audio invovles reading from an instance of AudioInput.

const fs = require('fs');
const portAudio = require('node-portaudio');

// Create an instance of AudioInput, which is a ReadableStream
const ai = new portAudio.AudioInput({
  channelCount: 2,
  sampleFormat: portAudio.SampleFormat16Bit,
  sampleRate: 44100
  deviceId : -1 // Use -1 or omit the deviceId to select the default device
});

// handle errors from the AudioInput
ai.on('error', err => console.error);

// Create a write stream to write out to a raw audio file
const ws = fs.createWriteStream('rawAudio.raw');

//Start streaming
ai.pipe(ws);
ai.start();

Note that this produces a raw audio file - wav headers would be required to create a wav file. However this basic example produces a file may be read by audio software such as Audacity, using the sample rate and format parameters set when establishing the stream.

To stop the recording, call ai.quit(). For example:

process.on('SIGINT', () => {
  console.log('Received SIGINT. Stopping recording.');
  ai.quit();
});

Troubleshooting

Linux - No Default Device Found

Ensure that when you compile portaudio that the configure scripts says "ALSA" yes.

Mac - Carbon Component Manager

You may see or have seen the following message during initilisation of the audio library on MacOS:

WARNING:  140: This application, or a library it uses, is using the deprecated Carbon Component Manager
for hosting Audio Units. Support for this will be removed in a future release. Also, this makes the host
incompatible with version 3 audio units. Please transition to the API's in AudioComponent.h.

A locally compiled version of the portaudio library is now included with the latest version of node-portaudio that uses more up-to-date APIs from Apple. The portaudio team are aware of this issue.

Status, support and further development

This library was created mostly to port to TypeScript and add some additional PortAudio functionality. It may be developed further if more features are requested. Additionally, you can create a PR to add some of your own functionality.

License

This software uses libraries from the PortAudio project. The license terms for PortAudio are stated to be an MIT license.