
Security News
Axios Supply Chain Attack Reaches OpenAI macOS Signing Pipeline, Forces Certificate Rotation
OpenAI rotated macOS signing certificates after a malicious Axios package reached its CI pipeline in a broader software supply chain attack.
Node.js bindings for CPAL (Cross-Platform Audio Library), providing low-level audio functionality for Node.js applications.
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.
Complete Audio Device Management
Audio Stream Control
Developer-Friendly
Cross-Platform
npm install node-cpal
node-cpal provides pre-built binaries for the following platforms:
The package automatically detects your platform and loads the appropriate binary.
libasound2-dev on Debian/Ubuntu)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);
For more comprehensive examples, check out the examples directory:
Each example includes detailed comments explaining how the code works.
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(): stringReturns the default input (recording) device ID.
const inputDevice = cpal.getDefaultInputDevice();
getDefaultOutputDevice(): stringReturns the default output (playback) device ID.
const outputDevice = cpal.getDefaultOutputDevice();
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): StreamConfigReturns the default input configuration for the specified device.
const inputDevice = cpal.getDefaultInputDevice();
const config = cpal.getDefaultInputConfig(inputDevice);
getDefaultOutputConfig(deviceId: string): StreamConfigReturns the default output configuration for the specified device.
const outputDevice = cpal.getDefaultOutputDevice();
const config = cpal.getDefaultOutputConfig(outputDevice);
createStream(deviceId: string, isInput: boolean, config: StreamConfig, callback?: (data: Float32Array) => void): StreamHandleCreates 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): voidWrites audio data to an output stream.
// Write a buffer of audio data to the stream
cpal.writeToStream(outputStream, audioBuffer);
pauseStream(streamHandle: StreamHandle): voidPauses an active stream.
cpal.pauseStream(stream);
resumeStream(streamHandle: StreamHandle): voidResumes a paused stream.
cpal.resumeStream(stream);
closeStream(streamHandle: StreamHandle): voidCloses and cleans up a stream.
cpal.closeStream(stream);
isStreamActive(streamHandle: StreamHandle): booleanChecks if a stream is currently active.
const isActive = cpal.isStreamActive(stream);
console.log(`Stream is ${isActive ? 'active' : 'inactive'}`);
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;
}
npm install
npm run build
Run the test suite:
npm test
Contributions are welcome! Please feel free to submit a Pull Request.
git checkout -b feature/amazing-feature)git commit -m 'Add some amazing feature')git push origin feature/amazing-feature)ISC License
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.
FAQs
Node.js bindings for CPAL (Cross-Platform Audio Library)
We found that node-cpal demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 0 open source maintainers 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.

Security News
OpenAI rotated macOS signing certificates after a malicious Axios package reached its CI pipeline in a broader software supply chain attack.

Security News
Open source is under attack because of how much value it creates. It has been the foundation of every major software innovation for the last three decades. This is not the time to walk away from it.

Security News
Socket CEO Feross Aboukhadijeh breaks down how North Korea hijacked Axios and what it means for the future of software supply chain security.