@alexanderolsen/silence-listener-node

中文文档
1.0.3发布于 2021/02/26MITCommonJS零依赖近 30 天下载 5863

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

原始使用文档
文档目录18 节
  1. Installation
  2. Setup
  3. Usage
  4. In modules:
  5. In HTML:
  6. Configuration
  7. API Reference
  8. connect
  9. disconnect
  10. subscribeToSilence
  11. unsubscribeFromSilence
  12. getters
  13. setters
  14. Examples
  15. Building From Source
  16. Uncaught (in promise) DOMException: The user aborted a requ…
  17. Contributing
  18. License

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.

版本与发布记录

@alexanderolsen/silence-listener-node 共发布 3 个版本,首次发布于 2021/02/25;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.0.3latest2021/02/265 年前
1.0.22021/02/265 年前
1.0.12021/02/255 年前

关于 @alexanderolsen/silence-listener-node 的常见问题

@alexanderolsen/silence-listener-node 是什么?

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

如何安装 @alexanderolsen/silence-listener-node?

在项目目录运行 npm install @alexanderolsen/silence-listener-node,或使用 pnpm add / yarn add / bun add。

@alexanderolsen/silence-listener-node 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/alexanderolsen__silence-listener-node 是否存在。

@alexanderolsen/silence-listener-node 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@alexanderolsen/silence-listener-node 可以商用吗?

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

@alexanderolsen/silence-listener-node 的下载量有多少?

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

@alexanderolsen/silence-listener-node 还在维护吗?

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