@echogarden/audio-io

中文文档
0.4.2发布于 2025/05/17MITTypeScriptES Module近 30 天下载 1.4万

Provides low-level audio I/O via platform-specific APIs. Includes precompiled addons for all supported platforms.

原始使用文档
文档目录7 节
  1. Installation
  2. Low-level audio output
  3. High-level playback methods
  4. Building the addons
  5. Still experimental, feedback is needed
  6. Future
  7. License

Echogarden Audio I/O

A Node.js package that provides low-level audio outputs (audio inputs are not implemented yet) for common audio APIs, on various platforms:

The code is very minimalistic and doesn't rely on any external libraries, only direct system calls.

For each platform, there's a single, independent cpp source file, which uses the Node Addon API (C++) to produce a Node.js addon that integrates with the Node.js runtime for the given platform.

The addons are distributed as precompiled binaries only, which means the package doesn't require any build-time postprocessing to install.

Installation

npm install @echogarden/audio-io

Low-level audio output

This example creates a low-level audio output using createAudioOutput, and uses a callback to directly write into the audio device output buffer. It is useful where direct, real-time control is needed or low-latency audio processing is done.

If you're more interested in playing an existing audio file, or audio buffer, you can skip this, and move to the high-level operations section below.

// Import module
import { createAudioOutput } from '@echogarden/audio-io'

// Define an audio output handler function
function audioOutputHandler(outputBuffer: Int16Array) {
    // Write 16-bit signed integer little-endian samples to `outputBuffer`.
    // If there are multiple channels, interleave them, like `LRLRLRLR..` for stereo.
}

// Create audio output, passing a configuration object and the handler
const audioOutput = await createAudioOutput({
    sampleRate: 44100, // Sample rate in Hz, should be an integer like 44100, 22050, 8000
    channelCount: 2, // Channel count, likely 1 (mono), or 2 (stereo)
    bufferDuration: 100.0, // Target audio output buffer duration, in milliseconds. Defaults to 100.0
}, audioOutputHandler)

// ...

// When done, call audioOutput.dispose(),
// which will close the audio output and free any associated memory or handles.
await audioOutput.dispose()

Notes:

  • Only 16-bit, signed integer, little-endian, interleaved buffers are currently supported. Ensure the audio data is converted to this format before writing it to the handler's buffer

Notes on bufferDuration:

  • On MME (Windows) and ALSA (Linux) bufferDuration will be used to directly compute the output buffer size
  • On Core Audio (macOS), it will be used to set the maximum buffer size, but the actual buffer size selected by the driver may be significantly smaller

High-level playback methods

These methods wrap around createAudioOutput and will internally create a new audio output, play the given audio data, and then dispose the audio output.

playWaveData(waveData: Uint8Array, options?: PlaybackOptions, positionCallback?: PositionCallback)

Play WAVE format bytes, given as a Uint8Array:

// Import modules
import { readFile } from 'fs/promises`'
import { playWaveData } from '@echogarden/audio-io'

const waveData: Uint8Array = await readFile('test.wav')

await playWaveData(waveData, { bufferDuration: 125 }) // 125ms buffer duration

positionCallback parameter accepts a callback that will be repeatedly called while the audio is being played, with sample-accurate playback position data. For example:

await playWaveData(waveData, {}, (callbackData) => {
    console.log(`Current audio time position: ${callbackData.timePosition}`)
    console.log(`Current sample offset: ${callbackData.sampleOffset}`)
})

positionCallback is also accepted by all following playback methods.

If you want to use the underlying WAVE encoder/decoder independently, you can import the @echogarden/wave-codec package directly into your project, see its documentation for more details.

playFloat32Channels(float32Channels: Float32Array[], sampleRate: number, options?: PlaybackOptions, positionCallback?: PositionCallback)

Play floating point samples, given as an array of Float32Array, where each Float32Array is a separate channel:

// Import module
import { playFloat32Channels } from '@echogarden/audio-io'

const float32Channels: Float32Array[] = //... retrieve audio samples

await playFloat32Channels(float32Channels, 44100) // 44100 Hz, default buffer duration

playInt16Samples(int16Samples: Int16Array, sampleRate: number, channelCount: number, options?: PlaybackOptions, positionCallback?: PositionCallback)

Play signed, 16-bit interleaved, PCM audio samples, given as an Int16Array:

// Import module
import { playInt16Samples } from '@echogarden/audio-io'

const int16Samples: Int16Array = //... retrieve audio samples

await playInt16Samples(int16Samples, 44100, 2) // 44100 Hz, 2 channels, default buffer duration

playTestTone(options?: TestToneOptions, positionCallback?: PositionCallback)

Play a stereo test tone (sine wave), to test if the audio output is working correctly.

import { playTestTone } from '@echogarden/audio-io'

await playTestTone({
    totalDuration: 1,
    sampleRate: 48000,
    bufferDuration: 100,
    frequencyLeft: 440,
    frequencyRight: 280,
})

Building the addons

Pre-built addons are bundled for all supported platforms.

To rebuild them yourself, see guide for building the addons.

Still experimental, feedback is needed

All code was written from scratch, meaning it hasn't been tested on a large variety of systems yet:

  • Windows addon has only been tested on Windows 11
  • Linux addon has only been tested in WSL2 (Ubuntu 24.04) and an Ubuntu 23.10 VM in VirtualBox (x64 only)
  • macOS addon has only been tested in a macOS 13 VM in VMWare (x64 only)

If you encounter any crashes, unexpected errors, or audio problems, like distortions or artifacts, it's likely they can be solved relatively easily. Just open an issue and let me know about it.

Future

  • Audio inputs would be implemented once audio outputs are sufficiently stabilized and tested
  • Option to list and select audio output devices, and to use a playback device other than the default one
  • Better handling of multichannel audio (more than 2 channels) by detecting output device properties. Currently it will error if default output device doesn't support the given number of channels
  • Add optional lower latency I/O on Windows via the WASAPI API (supported on Windows Vista or newer)

License

MIT

版本与发布记录

@echogarden/audio-io 共发布 15 个版本,首次发布于 2024/10/25;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.4.2latest2025/05/171.5 年前
0.4.12025/05/111.5 年前
0.4.02025/05/061.5 年前
0.3.02024/12/131.9 年前
0.2.32024/11/052 年前
0.2.22024/11/032 年前

关于 @echogarden/audio-io 的常见问题

@echogarden/audio-io 是什么?

Provides low-level audio I/O via platform-specific APIs. Includes precompiled addons for all supported platforms.

如何安装 @echogarden/audio-io?

在项目目录运行 npm install @echogarden/audio-io,或使用 pnpm add / yarn add / bun add。

@echogarden/audio-io 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@echogarden/audio-io 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@echogarden/audio-io 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=18。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@echogarden/audio-io 可以商用吗?

@echogarden/audio-io 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@echogarden/audio-io 的下载量有多少?

根据 npm 官方下载统计,@echogarden/audio-io 在 2026/10/13 – 2026/11/11 这 30 天内被下载 13,576 次,日均约 453 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@echogarden/audio-io 还在维护吗?

最新版本 0.4.2 发布于 2025/05/17(1.5 年前);至今共发布 15 个版本,其中过去 12 个月发布了 0 个;首次发布于 2024/10/25。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。