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 🙏

© 2024 – Pkg Stats / Ryan Hefner

@alexanderolsen/feeder-node

v1.1.3

Published

Feed mono or stereo audio data for playback. Ideal for real-time audio.

Downloads

32

Readme

FeederNode

AppVeyor Coverage Status Maintainability Depfu

FeederNode is a pseudo-AudioNode which can be fed audio data for instant playback. This library is ideal for consuming real-time audio data, e.g. data received from WebRTC or Websocket connections. Uses modern technologies such as WebAssembly, AudioWorklet, and Web Workers if they're available, falling back to more older systems if not.

Features:

Installation

Install using NPM:

npm i @alexanderolsen/feeder-node

Setup

To utilize Web Worker, WebAssembly, and AudioWorklet functionality, you must copy the helper files in /node_modules/@alexanderolsen/feeder-node/dist/ to where feeder-node can find them. The default location for these files is at the server root. This location can be changed using the following variables in the options dict passed into createFeederNode():

createFeederNode(context, nChannels, { 
    pathToWasm:    '/some/path/to/libsamplerate.wasm',     // default '/libsamplerate.wasm'
    pathToWorklet: '/some/path/to/feeder-node.worklet.js', // default '/feeder-node.worklet.js'
    pathToWorker:  '/some/path/to/feeder-node.worker.js'   // default '/feeder-node.worker.js'
});

See Configuration for more instructions on using the options dict.

Usage

FeederNode expects to receive mono or interleaved multi-channel data. Any TypedArray can be used; values will be converted to Float32s where -1 < sample < 1.

In modules:

import { createFeederNode } from '@alexanderolsen/feeder-node'; 

let context   = new AudioContext();
let nChannels = 2;
let options = {}; // see **Configuration**

createFeederNode(context, nChannels, options)
    .then((feederNode) => {
        feederNode.connect(context.destination);
        feederNode.feed(new Float32Array(512));
    });

or

const createFeederNode = require('@alexanderolsen/feeder-node').createfeederNode; 

(async function() {
    let context   = new AudioContext();
    let nChannels = 2;
    let options = {}; // see **Configuration**

    let feederNode = await createFeederNode(context, nChannels, options);
    feederNode.connect(context.destination);
    feederNode.feed(new Float32Array(512));
})();

In HTML:

<script src="https://cdn.jsdelivr.net/npm/@alexanderolsen/feeder-node"></script>
<script>
    var context   = new AudioContext();
    var nChannels = 2;
    var options = {}; // see **Configuration**

    FeederNode.createFeederNode(context, nChannels, options)
        .then((feederNode) => {
            feederNode.connect(context.destination);
            feederNode.feed(new Float32Array(512));
        });
    
</script>

Or use the feeder-node.js file in the dist folder:

<script src="feeder-node.js"></script>

Configuration

When creating a FeederNode instance, you have number of options available:

let context   = new AudioContext();
let nChannels = 2;

// entries are defaults
let options = {
    inputSampleRate:     context.sampleRate, // Nothing surprising here

    batchSize:           (512 || 128), // Stuck at 128 for `AudioWorklet`s. 
    bufferThreshold:     4096,         // Number of samples to buffer before propagating to dstination
    bufferLength:        192000,       // Length of RingBuffer. See ring-buffer.js for more
    resampConverterType: 2,            // See **resampConverterType**

    pathToWorklet:       '/feeder-node.processor.js', // Set to location of your feeder-node.processor.js
    pathToWorker:        '/feeder-node.worker.js',    // Set to location of your feeder-node.worker.js
    pathToWasm:          '/libsamplrate.wasm.js'      // set to location of your libsamplerate.wasm
}

createFeederNode(context, nChannels, options).then((feederNode) => { ... });

batchSize

Modifies the batch size processed by ScriptProcessorNode. This does not affect AudioWorklets as they're stuck at 128. If using ScriptProcessorNode, must be one of the following: [256, 512, 1024, 2048, 4096, 8192, 16384].

bufferThreshold

FeederNode buffers this many samples (per channel) before propagating to the next AudioNode in the graph. Higher values (16000-32000) can be useful to guarantee seamless audio if playing back in real-time, though lower values result in lower latency. If FeederNode runs out of data, buffer this many sample again before propagating.

bufferLength

The total amount of data which can be buffered at a time. If you try to buffer more data than this, you'll end up overwriting older data.

resampConverterType

Converter types are as follows. More information can be found at the libsamplerate website.

const ConverterType = {
    SRC_SINC_BEST_QUALITY: 0,   // highest quality, slowest
    SRC_SINC_MEDIUM_QUALITY: 1, // 
    SRC_SINC_FASTEST: 2,        // in-between
    SRC_ZERO_ORDER_HOLD: 3,     // poor quality, "blindingly" fast
    SRC_LINEAR: 4               // poor quality, "blindingly" fast
}

inputSampleRate

Sample rate of incoming data. Will automatically be resampled to AudioContext.sampleRate.

API Reference

Once you've created the FeederNode using createFeederNode() or FeederNode.createFeederNode(), the returned object exposes:

connect

/**
 * Connects FeederNode to the specific destination AudioNode
 *
 * @param {AudioNode} destination The node to connect to
 */
connect(destination) { ... }

disconnect

/** Disconnects from the currently-connected AudioNode */
disconnect() { ... }

feed

/**
 * Feeds raw PCM audio data to the underlying node. Any kind of TypedArray can be submitted - FeederNode
 * will automatically convert to Float32 and scale to -1 < n < 1.
 *
 * @param {TypedArray} data Any TypedArray. Conversion will be done automatically
 */
feed(data) { ... }

getters

get bufferLength() { ... }
get nChannels() { ... }
get batchSize() { ... }

get numberOfInputs() { ... }
get numberOfOutputs() { ... }
get channelCount() { ... }
get channelCountMode() { ... }
get channelInterpretation() { ... }

// e.g. let bufferLength = feederNode.bufferlength;

setters

set channelCount(channelCount) { ... }
set channelCountMode(channelCountMode) { ... }
set channelInterpretation(channelInterpretation) { ... }

// e.g. silenceListenerNode.channelInterpretation = 'speakers';

overrides

It can be useful to get notified when FeederNode runs out of data, or has resumed playing:

/** Overwrite these for Backend state callbacks */
onBackendReady() {}
onBackendPlaying() {}
onBackendStarved() {}

// e.g. feederNode.onBackendStarved = () => { console.log('feederNode ran out of data!' ) }

Examples

Run any server (http-server, etc) from the project directory:

cd feeder-node
http-server

and visit localhost:8080/examples/continuous or localhost:8080/examples/chunk-feeder in a browser. Examples must be hosted from the root directory, as they need to access the files in dist.

Building From Source

git clone https://github.com/aolsenjazz/feeder-node
cd feeder-node
npm run watch

Production files are placed in the dist directory.

Uncaught (in promise) DOMException: The user aborted a request.

This super unhelpful error message occurs when FeederNode is unable to find your feeder-node.worklet.js. To fix this, make sure your options.pathToWorklet is set correctly and that the file is actually reachable at that location.

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

License

Licenses are available in LICENSE.md.