ALSA bindings for Node.js
ALSA bindings for Node.js
You must have a system on which ALSA is supported with the
libasound2-dev package installed:
$ sudo apt-get install libasound2-dev
You'll also probably want to have an audio device for either playback or capture.
Install with npm:
$ npm install alsa
or via git:
$ npm install git+https://github.com/xdissent/node-alsa.git
alsa module exports 2 high-level classes for playback and recording, a simple wrapper class around PCM operations, and a subset of the low-level libasound2 functions.
var alsa = require'alsa'// The following variables represent the defaults for the Playback and Capture constructors.device = 'default' // ALSA default devicechannels = 2 // Stereorate = 44100 // Sample rateformat = alsalibasoundPCM_FORMAT_S16_LE // PCM format (signed 16 bit LE int)access = alsalibasoundPCM_ACCESS_RW_INTERLEAVED // Access modeasync = false; // Async-mode - YOU DO NOT WANT THIS!// The Capture class is a stream.Readable subclass.var capture = device channels rate format access async;capturepipeprocessstdout; // Treat it like any other readable stream.// The Playback class is a stream.Writable subclass.var playback = device channels rate format access async;processstdinpipeplayback; // Treat it like any other writable stream.// The Pcm class is a wrapper around common libasound2 functions.var pcm = device alsalibasoundPCM_STREAM_CAPTURE; // or PCM_STREAM_PLAYBACK// Open PCM device, set parameters, and prepare (chainable).pcmopenaccessaccessformatformatchannelschannelsraterateprepare;// PCM parameters are combo getters and setters.console.log'Channels: ' + pcmchannels;// Calculate some sizes and create a buffer.var bytesPerFrame = pcmbytesPerFrameframesPerBuffer = 1024bufferSize = framesPerBuffer * bytesPerFramebuffer = bufferSize;// Read frames from PCM device into the buffer, then get the raw frames.var readFrames = pcmreadibuffer framesPerBufferframes = bufferslice0 readFrames * bytesPerFrame;// Low level libasound2 api access works too (pcm.pcm is a libasound2 snd_pcm_t pointer).console.logalsalibasoundsnd_pcm_namepcmpcm;
Downsample audio captured from an ALSA device to a sample rate of 11025, in mono, encoded to 32 bit float PCM data and output to stdout:
alsa = require 'alsa'stream = require 'stream': -> streamTransformcall this# Naively drop 3 out of 4 samples, combine the interleaved left and right into# a single sample, then convert to 32 bit float PCM data.:# This is the naive part. Should be checking for partial samples.floats = chunklength / 4f = 0for i in 0...chunklengthby 16 # 4 bytes per frame downsampled by 4.l16 = chunkreadInt16LE i # Read left channel sample.r16 = chunkreadInt16LE i + 2 # Read right channel sample.# Sum samples and normalize as a floatfloatswriteFloatLE l16 + r16 / 65536ff += 4 # Skip ahead 4 bytes in buffer (32 bit float).@push floats # Write transformed samples to stream...callback # And we're done.# Create a Capture and a Downer and chain them to stdout.capture =downer =capturepipedownerpipeprocessstdout