@alexanderolsen/libsamplerate-js

中文文档
2.1.2发布于 2024/08/17MITTypeScriptCommonJS零依赖近 30 天下载 30.2万

Resample audio in node or browser using a webassembly port of libsamplerate.

原始使用文档
文档目录18 节
  1. Installation
  2. Usage
  3. In modules:
  4. In AudioWorklets :
  5. In HTML:
  6. API Reference
  7. simple
  8. full
  9. destroy
  10. Update outputSampleRate & inputSampleRate
  11. ConverterType
  12. Examples
  13. Node
  14. Web
  15. Benchmarks
  16. Building From Source
  17. Contributing
  18. License

libsamplerate-js

GitHub Workflow Status Coverage Status Maintainability Depfu

libsamplerate-js is a port of libsamplerate to Web Assembly exposed through a simple JS API for use in-browser or node. The simple API is ideal for resampling large pieces of audio. The full API is ideal for quickly resampling small portions (128+ samples) of a larger piece of audio such as audio received from a Websocket or WebRTC connection.

Features:

  • works both in-browser and in node!
  • 1-128 channels
  • 1-192000 sample rates
  • libsamplerate Full and Simple APIs
  • See the libsamplerate docs for much more (and better) info

Installation

Install using NPM:

npm i @alexanderolsen/libsamplerate-js

See Usage or API for more examples and instructions.

Usage

libsamplerate-js expects to receive Float32Array mono or multi-channel interleaved data, where each sample is -1 < sample < 1.

In modules:

import { create, ConverterType } from '@alexanderolsen/libsamplerate-js';

let converterType = ConverterType.SRC_SINC_BEST_QUALITY;
let nChannels = 2;
let inputSampleRate = 44100;
let outputSampleRate = 48000;

create(nChannels, inputSampleRate, outputSampleRate, {
  converterType: converterType, // default SRC_SINC_FASTEST. see API for more
}).then((src) => {
  let data = new Float32Array(44100);
  let resampledData = src.simple(data);
  src.destroy(); // clean up
});

or

const LibSampleRate = require('@alexanderolsen/libsamplerate-js');

let converterType = LibSampleRate.ConverterType.SRC_SINC_BEST_QUALITY;
let nChannels = 2;
let inputSampleRate = 44100;
let outputSampleRate = 48000;

LibSampleRate.create(nChannels, inputSampleRate, outputSampleRate, {
  converterType: converterType, // default SRC_SINC_FASTEST. see API for more
}).then((src) => {
  let data = new Float32Array(44100);
  let resampledData = src.full(data);
  src.destroy(); // clean up
});

In AudioWorklets:

// project.js
const audioContext = new AudioContext({ sampleRate: 44100 });
await audioContext.audioWorklet.addModule('processor.js');
// You can also bundle libsamplerate.worklet.js with your own application; cdn link provided for convenience
await audioContext.audioWorklet.addModule(
  'https://cdn.jsdelivr.net/npm/@alexanderolsen/libsamplerate-js/dist/libsamplerate.worklet.js'
);
// processor.js
const { create, ConverterType } = globalThis.LibSampleRate;

let nChannels = 1;
let inputSampleRate = 44100;
let outputSampleRate = 16000; // or another target sample rate

// somewhere in the declaration of your Processor:
create(nChannels, inputSampleRate, outputSampleRate, {
  converterType: ConverterType.SRC_SINC_BEST_QUALITY, // or some other quality
}).then((src) => {
  this.src = src;
});

See examples/worklet for a full implementation example. Configuring libsamplerate-js to work in AudioWorklets is less trival than it ought to be due to AudioWorklet limitations. Note that typing support is not avaialble for LibSampleRate within the context of AudioWorklets.

In HTML:

<script src="https://cdn.jsdelivr.net/npm/@alexanderolsen/libsamplerate-js"></script>
<script>
  var converterType = LibSampleRate.ConverterType.SRC_SINC_BEST_QUALITY;
  var nChannels = 2;
  var inputSampleRate = 44100;
  var outputSampleRate = 48000;

  LibSampleRate.create(nChannels, inputSampleRate, outputSampleRate, {
    converterType: converterType, // default SRC_SINC_FASTEST. see API for more
  }).then((src) => {
    var data = new Float32Array(44100);
    let resampledData = src.full(data);
    src.destroy(); // clean up
  });
</script>

Or use the libsamplerate.js file in the dist folder:

<script src="libsamplerate.js"></script>

API Reference

Once you've created the JS wrapper using create() or LibSampleRate.create(), the returned object exposes:

simple

/**
 * Calls the libsamplerate `simple` API. This should be used when resampling one individual chunk of audio,
 * and no more calls to are required. If more calls are required, use the `full` API. If the array submitted
 * is > 4MB, audio will be broken up into chunks and the `full` API will be used
 *
 * More (and better) info available at: http://www.mega-nerd.com/SRC/api_simple.html
 *
 * @param  {Float32Array}         dataIn  Float32Array containing mono|interleaved audio data where -1 < dataIn[i] < 1
 * @return {Float32Array}                 The resampled data
 */
simple(dataIn) { ... }

full

/**
 * Calls the libsamplerate `full` API. This should be used when resampling several chunks of the
 * sample audio, e.g. receiving a live stream from WebRTC/websocket API.
 *
 * More (and better) info available at: http://www.mega-nerd.com/SRC/api_full.html
 *
 * @param {Float32Array}      dataIn              Float32Array containing mono|interleaved audio data where -1 < dataIn[i] < 1
 * @param {Float32Array|null} dataOut             Optionally, pass a Float32Array to avoid allocating an extra array for every esampling operation
 * @param {Object|null}       outLength           Amount of data written to dataOut
 * @param {number}            [outLength.frames]  The number of frames in the output

 * @return {Float32Array} The resampled data. If dataOut != null, dataOut is returned
 */
full(dataIn, dataOut=null, outLength = null) { ... }

destroy

/**
 * Cleans up WASM SRC resources. Once this is called on an instance, that instance must be
 * reinitialized with src.init() before it can be used again.
 */
destroy() { ... }

Update outputSampleRate & inputSampleRate

let nChannels = 2;
let inputSampleRate = 44100;
let outputSampleRate = 48000;

create(nChannels, inputSampleRate, outputSampleRate).then((src) => {
  let data = new Float32Array(44100);
  let resampled48k = src.simple(data); // returns ~48000 samples
  src.outputSampleRate = 96000;
  let resampled96k = src.simple(data); // returns ~96000 samples
});

ConverterType

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
};

Examples

Node

cd libsamplerate-js/examples/cli
node index.js 48000 result.wav

and listen to to the result

Web

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

cd libsamplerate-js
http-server

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

Benchmarks

Get a sense of how long resampling operations take in your environment:

cd libsamplerate-js
http-server

and visit localhost:8080/benchmarks. A minimalistic UI is provided to test different batch sizes, APIs, sample rates, and ConverterTypes.

Building From Source

Before you can compile the WASM code you need to download and install Empscripten and activate PATH variables for the current terminal. To build and compile the JS + WASM resources from source, run:

git clone https://github.com/aolsenjazz/libsamplerate-js
cd libsamplerate-js
npm i
npm run compile-wasm
npm run build

You can also build with docker (either from scratch or the wasm only):

git clone https://github.com/aolsenjazz/libsamplerate-js
cd libsamplerate-js
git submodule update --init
cd scripts/library/
docker build -t gcc-emscripten .
cd ../../
npm run compile-library-docker
npm run compile-wasm-docker
npm run build

Production files are placed in the dist directory.

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/libsamplerate-js 共发布 27 个版本,首次发布于 2021/01/13;过去 12 个月发布了 0 个版本。

版本发布日期距今
2.1.2latest2024/08/172 年前
2.1.12024/01/232 年前
2.1.02023/10/163 年前
2.0.42023/07/093 年前
2.0.32022/11/273 年前
2.0.22022/11/273 年前

关于 @alexanderolsen/libsamplerate-js 的常见问题

@alexanderolsen/libsamplerate-js 是什么?

Resample audio in node or browser using a webassembly port of libsamplerate.

如何安装 @alexanderolsen/libsamplerate-js?

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

@alexanderolsen/libsamplerate-js 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@alexanderolsen/libsamplerate-js 支持 ESM 还是 CommonJS?

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

@alexanderolsen/libsamplerate-js 可以商用吗?

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

@alexanderolsen/libsamplerate-js 的下载量有多少?

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

@alexanderolsen/libsamplerate-js 还在维护吗?

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