Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,7 @@ these items:
- One Wasm binary.
- The generated bindings.
- A shared `loadTunelet` entry point.
- A browser `AudioWorklet` adapter (`createTuneletNode` and `tunelet/worklet`).

### Load the module

Expand Down Expand Up @@ -416,6 +417,7 @@ page that opens the JavaScript.
| Operation | TypeScript API |
| --------------------------------- | -------------------------------------------------------- |
| Shared initialization | `loadTunelet(source?)` |
| Browser AudioWorklet node | `createTuneletNode(context, options?)` |
| Mono PCM to planar stereo | `process(input, left, right, events?)` |
| In-place mono downmix | `processMono(audio, events?)` |
| Configuration | `config()`, `setConfig(config)`, `defaultConfig()` |
Expand All @@ -437,9 +439,24 @@ frame limit. These rules apply:
first.
- Buffers must stay attached during each call.

Web Audio uses Float32 samples, which you must convert to and from 16-bit
PCM. This package does not install an AudioWorklet or an integration with an
audio device.
Direct `Tunelet` calls use `Int16Array` samples. For Web Audio `Float32Array`
graphs in browsers, call `createTuneletNode(audioContext)` to register
`tunelet/worklet` and construct a `TuneletWorkletNode`:

```ts
import { createTuneletNode } from "tunelet";

const context = new AudioContext();
const mic = await navigator.mediaDevices.getUserMedia({ audio: true });
const node = await createTuneletNode(context);
context.createMediaStreamSource(mic).connect(node).connect(context.destination);
```

`TuneletWorkletProcessor` installs an internal UTF-8 shim, reuses pre-allocated
`Int16Array` buffers and throttles `feedback` and `telemetry` reports to a UI
interval (50 ms by default). Contexts at 44.1 kHz or 48 kHz run at their native
rate. Contexts at other rates (such as 88.2 kHz or 96 kHz) resample each block
to and from an internal 48 kHz `Tunelet` instance.

### Configuration objects

Expand Down
9 changes: 8 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
"types": "./dist/index.d.ts",
"import": "./dist/index.js"
},
"./worklet": {
"types": "./dist/worklet.d.ts",
"import": "./dist/worklet.js"
},
"./wasm": {
"types": "./pkg/tunelet.d.ts",
"import": "./pkg/tunelet.js"
Expand All @@ -26,7 +30,10 @@
"pkg/tunelet.d.ts",
"pkg/tunelet_bg.wasm"
],
"sideEffects": false,
"sideEffects": [
"./dist/worklet.js",
"./dist/utf8-shim.js"
],
"scripts": {
"build": "npm run build:wasm && npm run build:ts",
"build:wasm": "wasm-pack build --target web --out-dir pkg --release --no-pack --features wasm --locked && node --input-type=module -e \"import fs from 'node:fs'; fs.rmSync('pkg/.gitignore', { force: true })\"",
Expand Down
36 changes: 31 additions & 5 deletions scripts/verify-pack.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* This script packs the npm archive, checks its eight files, runs
* `loadTunelet()` and checks the TypeScript declarations.
* This script packs the npm archive, checks its files, runs `loadTunelet()`
* and checks the TypeScript declarations.
*/
import assert from "node:assert/strict";
import { execFileSync } from "node:child_process";
Expand All @@ -14,6 +14,12 @@ const EXPECTED_ARCHIVE_PATHS = [
"package/README.md",
"package/dist/index.d.ts",
"package/dist/index.js",
"package/dist/utf8-shim.d.ts",
"package/dist/utf8-shim.js",
"package/dist/worklet-protocol.d.ts",
"package/dist/worklet-protocol.js",
"package/dist/worklet.d.ts",
"package/dist/worklet.js",
"package/package.json",
"package/pkg/tunelet.d.ts",
"package/pkg/tunelet.js",
Expand Down Expand Up @@ -86,7 +92,7 @@ function packArchive(packDir) {
return path.posix.join("pack", archives[0]);
}

/** Check that the archive contains only the eight expected files. */
/** Check that the archive contains only the expected files. */
function verifyArchiveEntries(tempDir, relativeArchivePath) {
const rawList = execFileSync("tar", ["-tzf", relativeArchivePath], {
cwd: tempDir,
Expand Down Expand Up @@ -155,13 +161,17 @@ function verifyRuntimeImport(consumerDir, packageName) {
"",
"const packageName = process.argv[2];",
"const rootModule = await import(packageName);",
"const workletModule = await import(`${packageName}/worklet`);",
"const wasmModule = await import(`${packageName}/wasm`);",
"",
'assert.equal(typeof rootModule.loadTunelet, "function");',
'assert.equal(typeof rootModule.createTuneletNode, "function");',
"assert.equal(rootModule.default, rootModule.loadTunelet);",
'assert.equal(typeof rootModule.initSync, "function");',
'assert.equal(typeof rootModule.Tunelet, "function");',
'assert.equal(typeof rootModule.defaultConfig, "function");',
'assert.equal(typeof workletModule.TuneletWorkletProcessor, "function");',
'assert.equal(workletModule.TUNELET_WORKLET_PROCESSOR_NAME, "tunelet");',
'assert.equal(typeof wasmModule.default, "function");',
'assert.equal(typeof wasmModule.initSync, "function");',
"",
Expand All @@ -188,28 +198,38 @@ function verifyRuntimeImport(consumerDir, packageName) {
});
}

/** Check TypeScript resolution for the root entry and the `./wasm` entry. */
/** Check TypeScript resolution for the root, `./worklet` and `./wasm` entries. */
function verifyTypeDeclarations(consumerDir, packageName) {
const checkTsPath = path.join(consumerDir, "check.ts");
const pkgLiteral = JSON.stringify(packageName);
const workletLiteral = JSON.stringify(`${packageName}/worklet`);
const wasmLiteral = JSON.stringify(`${packageName}/wasm`);
fs.writeFileSync(
checkTsPath,
[
"import loadTunelet, {",
" createTuneletNode,",
" defaultConfig,",
" initSync as rootInitSync,",
" loadTunelet as namedLoadTunelet,",
" Tunelet,",
" type ProcessorConfig,",
" type TuneletWorkletNode,",
`} from ${pkgLiteral};`,
"import {",
" TUNELET_WORKLET_PROCESSOR_NAME,",
" TuneletWorkletProcessor,",
`} from ${workletLiteral};`,
"import init, {",
" initSync as wasmInitSync,",
" type InitInput,",
" type InitOutput,",
`} from ${wasmLiteral};`,
"",
"export async function verifyTypes(source?: InitInput): Promise<void> {",
"export async function verifyTypes(",
" source?: InitInput,",
" context?: BaseAudioContext,",
"): Promise<void> {",
" const exportsFromRoot: InitOutput = await loadTunelet(source);",
" const exportsFromNamed: InitOutput = await namedLoadTunelet(source);",
" const exportsFromWasm: InitOutput = await init(source);",
Expand All @@ -220,6 +240,12 @@ function verifyTypeDeclarations(consumerDir, packageName) {
" const right = new Int16Array(128);",
" processor.process(input, left, right);",
" processor.free();",
" if (context) {",
" const node: TuneletWorkletNode = await createTuneletNode(context);",
" node.dispose();",
" }",
" void TUNELET_WORKLET_PROCESSOR_NAME;",
" void TuneletWorkletProcessor;",
" void rootInitSync;",
" void wasmInitSync;",
" void exportsFromRoot;",
Expand Down
Loading