2.0.7 • Published 4 months ago

@hamitzor/node-audio v2.0.7

Weekly downloads
-
License
MIT
Repository
github
Last release
4 months ago

Key Features

  • Access well-known audio I/O APIs
    • Windows: WASAPI, DirectSound
    • Linux: ALSA, JACK and PulseAudio
  • No additional library/software needed
    • Direct access to the aforementioned APIs through C++
  • Prebuilds are available, so don't worry about building it yourself
    • A single npm install should get you ready to go
  • Supports Node.js versions >= 18.19.0
  • Probe available audio devices
  • Stream audio to output devices
  • Stream audio from input devices
  • The library implemented fully with Node.js streams
    • Convenient to use with network I/O, file I/O and other streams on Node.js
    • Comes with all the advantages of Node.js streams
  • Fully configurable audio streaming, allowing configuring
    • sample rate
    • bit depth
    • frame size
    • number of channels

Installation

Install it using npm or yarn

npm install @hamitzor/node-audio

or

yarn add @hamitzor/node-audio

As simple as that, no additional library/software required for installation. If you run into trouble during installation, don't hesitate to create an issue at Github.

Note Only Windows and Linux are supported at the moment.

Usage

To see some complete examples, you can check out the examples repository.

But here are some examples:

Read from microphone

const { createAudioReadStream, probeDevices, LowLevelAudioApi } = require('@hamitzor/node-audio')

// Get the default input device
const { defaultInputDevice } = probeDevices()

// Create a read stream
const audioStream = createAudioReadStream({
  api: LowLevelAudioApi.WASAPI, // On Linux, can be changed to, e.g. ALSA
  deviceId: defaultInputDevice.id, // The device to read from
  channels: 1, // The number of channels
  sampleRate: 48000, // Sample rate
  bufferFrames: 1920, // Frame size: number of samples in a frame
  format: PCMFormat.RTAUDIO_SINT16, // 16-bit signed integer (16-bit depth)
})

// Do whatever you want with the stream

// E.g. read 3840 bytes from it
const data = audioStream.read(3840)

// Or, pass it to a file write stream to save to a file
const { pipeline } = require('stream')
const { createWriteStream } = require('fs')

pipeline(audioStream, createWriteStream('somefile.raw'))


// Or, pass it to a TCP connection to send it over network
const { createWriteStream } = require('fs')

const server = net.createServer(connection => {
  pipeline(audioStream, connection)
})

// Or do anything you want that is achievable with a readable Node.js stream

Stream audio to output devices

const { createAudioWriteStream, probeDevices, LowLevelAudioApi } = require('@hamitzor/node-audio')

// Get the default output device
const { defaultOutputDevice } = probeDevices()

// Create a write stream
const audioStream = createAudioWriteStream({
  api: LowLevelAudioApi.WASAPI, // On Linux, can be changed to, e.g. ALSA
  deviceId: defaultOutputDevice.id, // The device to stream to
  channels: 1, // The number of channels
  sampleRate: 48000, // Sample rate
  bufferFrames: 1920, // Frame size: number of samples in a frame
  format: PCMFormat.RTAUDIO_SINT16, // 16-bit signed integer (16-bit depth)
})

// Do whatever you want with the stream

// E.g. write 3840 bytes to it
const data = audioStream.write(new Uint8Array(3840).fill(0))

// Or, pass it to a file read stream to playback a file
const { pipeline } = require('stream')
const { createReadStream } = require('fs')

pipeline(createReadStream('somefile.raw'), audioStream)


// Or, pass it to a TCP connection for playback over network
const { createWriteStream } = require('fs')

const server = net.createServer(connection => {
  pipeline(connection, audioStream)
})

// Or do anything you want that is achievable with a writable Node.js stream

Credits

This packages uses the C++ library named rtaudio under the hood. To check it out, visit https://github.com/thestk/rtaudio.

License

MIT

2.0.7

4 months ago

2.0.6

4 months ago