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/silence-listener-node

v1.0.3

Published

Pseudo-AudioNode to notify when an audio stream becomes silent.

Downloads

65

Readme

silence-listener-node

AppVeyor Coverage Status Maintainability Depfu

SilenceListenerNode is a pseudo-AudioNode which invokes a callback function whenever a stream of audio becomes or unbecomes silent. Uses AudioWorklet when available, falling back to ScriptProcessorNode when Worklets are not available.

Installation

Install using NPM:

npm i @alexanderolsen/silence-listener-node

Setup

To utilize AudioWorklet functionality, you must copy the sln.worklet.js file in /node_modules/@alexanderolsen/silence-listener-node/dist/ to where the library can find it. The default location for this file is at the server root (/sln.worklet.js). This location can be changed using the options dict passed into createSilenceListenerNode():

createSilenceListenerNode(context, nChannels, { 
    pathToWorklet: '/some/path/to/sln.worklet.js', // default '/sln.worklet.js'
});

See Configuration for more instructions on using the options dict.

Usage

In modules:

import { createSilenceListenerNode } from '@alexanderolsen/silence-listener-node'; 

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

createSilenceListenerNode(context, nChannels, options)
    .then((silenceListenerNode) => {
        buffSource.connect(silenceListenerNode);
        silenceListenerNode.connect(context.destination);
        buffSource.start();
    });

or

const createSilenceListenerNode = require('@alexanderolsen/silence-listener-node').createSilenceListenerNode; 

(async function() {
    let context   = new AudioContext();
    let nChannels = 2;
    let options   = {}; // see **Configuration**
    
    let buffSource = context.createBufferSource();
    let silenceListenerNode = await createSilenceListenerNode(context, nChannels, options);
    
    buffSource.connect(silenceListenerNode);
    silenceListenerNode.connect(context.destination);
    
    buffSource.start();
})();

In HTML:

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

    SilenceListenerNode.createSilenceListenerNode(context, nChannels, options)
        .then((silenceListenerNode) => {
            buffSource.connect(silenceListenerNode);
            silenceListenerNode.connect(context.destination);
            buffSource.start();
        });
</script>

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

<script src="silence-listener-node.js"></script>

Configuration

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

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

// entries are defaults
let options = {
    nInputs:   1,   // The number of inputs connected to this node. Probably 1
    nOutputs:  1,   // The number of outputs connected to this node. Probably 1
    batchSize: 512, // Stuck at 128 for `AudioWorklet`s. Can be powers of 2 where 256 < batchSize < 16384
    
    silenceThreshold: Math.floor(44100 / batchSize), // This is the number of silent batches which must
                                                     // occur before silence callbacks are invoked.
    pathToWorklet: '/sln.worklet.js' //The path to the worklet file
}

createSilenceListenerNode(context, nChannels, options).then((silenceListenerNode) => { ... });

nInputs

The number of AudioNodes connected to this instance of SilenceListenerNode. Default 1.

nOutputs

The nubmer of outputs AudioNodes this instance of SilenceLisnerNode is connected to. Default 1.

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].

silenceThreshold

The number of silent batches which must occur before the audio stream will be flagged as silent. Default is (roughly) one second worth of audio.

pathToWorklet

The location of your sln.worklet.js file. Default is at server root.

API Reference

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

connect

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

disconnect

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

subscribeToSilence

/**
 * Begin receiving silence notifications
 *
 * @param  { Function(isSilent, timeSinceLast) } callback Invoked whenever audio becomes or unbecomes silent
 * @return { String } id of the subscription. use with unsubscribeFromSilence(id) to stop receiving notifications
 */
subscribeToSilence(callback) { ... }

unsubscribeFromSilence

/**
 * Stop receiving silence events for given ID.
 * 
 * @param { String } id Identifier for callback subscription
 */
unsubscribeFromSilence(id) { ... }

getters

get silent()  { ... }
get silence() { ... } // for simplicity

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

// e.g. let isSilent = silenceListenerNode.silent;

setters

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

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

Examples

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

cd silence-listener-node
http-server

and visit localhost:8080/examples/basic or localhost:8080/examples/react 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/silence-listener-node
cd silence-listener-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 SilenceListenerNode is unable to find your sln.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.