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 🙏

© 2026 – Pkg Stats / Ryan Hefner

node-cpal

v0.1.1

Published

Node.js bindings for CPAL (Cross-Platform Audio Library)

Readme

node-cpal

Node.js bindings for CPAL (Cross-Platform Audio Library), providing low-level audio functionality for Node.js applications.

npm version License: ISC Build and Publish

Overview

node-cpal provides native Node.js bindings to the CPAL Rust library, giving Node.js developers access to low-level, cross-platform audio capabilities. This library enables audio device enumeration, audio playback, and recording with minimal latency across Windows, macOS, and Linux.

Features

  • Complete Audio Device Management

    • Enumerate audio hosts and devices
    • Get default input/output devices
    • Query device capabilities (formats, sample rates, channels)
  • Audio Stream Control

    • Create input (recording) streams
    • Create output (playback) streams
    • Write audio data to output streams
    • Read audio data from input streams
    • Pause, resume, and close streams
  • Developer-Friendly

    • Comprehensive TypeScript definitions
    • Promise-based API (optional)
    • Detailed error messages
  • Cross-Platform

    • Windows (WASAPI)
    • macOS (CoreAudio)
    • Linux (ALSA, JACK)

Installation

npm install node-cpal

Platform Support

node-cpal provides pre-built binaries for the following platforms:

  • Windows (x64)
  • macOS (x64 and ARM64/Apple Silicon)
  • Linux (x64 and ARM64)

The package automatically detects your platform and loads the appropriate binary.

Requirements

  • Node.js 14.0.0 or later
  • For building from source:
    • Rust toolchain (rustc, cargo)
    • Platform-specific audio development libraries:
      • Windows: No additional requirements
      • macOS: No additional requirements
      • Linux: ALSA development files (libasound2-dev on Debian/Ubuntu)

Basic Usage

const cpal = require('node-cpal');

// List all available audio hosts
const hosts = cpal.getHosts();
console.log('Available audio hosts:', hosts);

// Get the default output device
const outputDevice = cpal.getDefaultOutputDevice();
console.log('Default output device:', outputDevice);

// Create an output stream with default configuration
const config = cpal.getDefaultOutputConfig(outputDevice.deviceId);
const stream = cpal.createStream(
  outputDevice.deviceId,
  false, // false for output stream, true for input stream
  config,
  () => {} // Callback function (not needed for output streams)
);

// Close the stream when done
cpal.closeStream(stream);

Examples

For more comprehensive examples, check out the examples directory:

Each example includes detailed comments explaining how the code works.

API Reference

Host and Device Enumeration

getHosts(): string[]

Returns an array of available audio hosts on the system.

const hosts = cpal.getHosts();
// Example: ['CoreAudio']

getDevices(hostId?: string): string[]

Returns an array of all available audio devices for the specified host, or all hosts if not specified. Defaults to default host.

const devices = cpal.getDevices();

getDefaultInputDevice(): string

Returns the default input (recording) device ID.

const inputDevice = cpal.getDefaultInputDevice();

getDefaultOutputDevice(): string

Returns the default output (playback) device ID.

const outputDevice = cpal.getDefaultOutputDevice();

Device Configuration

getSupportedInputConfigs(deviceId: string): AudioDeviceConfig[]

Returns an array of supported input configurations for the specified device.

const inputDevice = cpal.getDefaultInputDevice();
const configs = cpal.getSupportedInputConfigs(inputDevice);

getSupportedOutputConfigs(deviceId: string): AudioDeviceConfig[]

Returns an array of supported output configurations for the specified device.

const outputDevice = cpal.getDefaultOutputDevice();
const configs = cpal.getSupportedOutputConfigs(outputDevice);

getDefaultInputConfig(deviceId: string): StreamConfig

Returns the default input configuration for the specified device.

const inputDevice = cpal.getDefaultInputDevice();
const config = cpal.getDefaultInputConfig(inputDevice);

getDefaultOutputConfig(deviceId: string): StreamConfig

Returns the default output configuration for the specified device.

const outputDevice = cpal.getDefaultOutputDevice();
const config = cpal.getDefaultOutputConfig(outputDevice);

Stream Management

createStream(deviceId: string, isInput: boolean, config: StreamConfig, callback?: (data: Float32Array) => void): StreamHandle

Creates an audio stream. For input streams, the callback function will be called with audio data.

// Creating an input stream
const inputDevice = cpal.getDefaultInputDevice();
const inputConfig = cpal.getDefaultInputConfig(inputDevice);

const inputStream = cpal.createStream(
  inputDevice,
  true, // true for input stream
  {
    sampleRate: inputConfig.sampleRate,
    channels: inputConfig.channels,
    format: 'f32',
  },
  (data) => {
    // Process incoming audio data
    console.log(`Received ${data.length} samples`);
  }
);

// Creating an output stream
const outputDevice = cpal.getDefaultOutputDevice();
const outputConfig = cpal.getDefaultOutputConfig(outputDevice);

const outputStream = cpal.createStream(
  outputDevice,
  false, // false for output stream
  {
    sampleRate: outputConfig.sampleRate,
    channels: outputConfig.channels,
    format: 'f32',
  },
  () => {} // No callback needed for output
);

writeToStream(streamHandle: StreamHandle, data: Float32Array): void

Writes audio data to an output stream.

// Write a buffer of audio data to the stream
cpal.writeToStream(outputStream, audioBuffer);

pauseStream(streamHandle: StreamHandle): void

Pauses an active stream.

cpal.pauseStream(stream);

resumeStream(streamHandle: StreamHandle): void

Resumes a paused stream.

cpal.resumeStream(stream);

closeStream(streamHandle: StreamHandle): void

Closes and cleans up a stream.

cpal.closeStream(stream);

isStreamActive(streamHandle: StreamHandle): boolean

Checks if a stream is currently active.

const isActive = cpal.isStreamActive(stream);
console.log(`Stream is ${isActive ? 'active' : 'inactive'}`);

Type Definitions

interface AudioDeviceConfig {
  minSampleRate: number;
  maxSampleRate: number;
  channels: number;
  format: 'i16' | 'u16' | 'f32';
}

interface StreamConfig {
  sampleRate: number;
  channels: number;
  format: 'i16' | 'u16' | 'f32';
}

interface StreamHandle {
  deviceId: string;
  streamId: string;
}

Building from Source

  1. Ensure you have the Rust toolchain installed (https://rustup.rs/)
  2. Clone the repository
  3. Install dependencies and build:
    npm install
    npm run build

Testing

Run the test suite:

npm test

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

License

ISC License

Acknowledgements

  • CPAL - The Rust Cross-Platform Audio Library
  • Neon - Rust bindings for writing safe and fast native Node.js modules

Publishing New Versions

This package uses GitHub Actions to build platform-specific binaries and publish them to npm. See PUBLISHING.md for detailed instructions on how to publish new versions.