Skip to content
66 changes: 37 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,18 @@ fn main() -> Result<(), tunelet::Error> {
### TypeScript

```ts
import { loadTunelet, Tunelet, defaultConfig } from "tunelet";
import {
loadTunelet,
Tunelet,
defaultConfig,
noteMaskFromKeyScale,
semitonesToRatio,
} from "tunelet";

await loadTunelet();
const config = defaultConfig();
config.resynthesis.pitchRatio = 1.25;
config.resynthesis.correction.notes = noteMaskFromKeyScale("a", "minor");
config.resynthesis.pitchRatio = semitonesToRatio(4);
const processor = new Tunelet(48000, 1024, config);

const input = new Int16Array(1024);
Expand Down Expand Up @@ -217,9 +224,9 @@ To prepare a processor, do these steps:
limit.
3. Create a `Processor` with that preparation and a `ProcessorConfig`.

Configuration structures implement `Default`. To select voices and effects,
edit their fields. To change the sample rate or the block limit, make a new
processor.
Configuration structures implement `Default`, and `ProcessorConfig::from_preset`
returns vocal presets. To select voices and effects, edit their fields. To
change the sample rate or the block limit, make a new processor.

Select the process function for your output layout:

Expand Down Expand Up @@ -440,7 +447,7 @@ these items:

Both environments use the same imports and the same initialization call. An
import of the package does not load the binary. Call `loadTunelet()` before
you use `Tunelet` or `defaultConfig()`.
you use `Tunelet`, `defaultConfig()` or the other helper functions.

- Node.js reads the packaged binary from a file URL relative to the module.
- Browsers fetch the served Wasm asset.
Expand Down Expand Up @@ -487,28 +494,29 @@ page that opens the JavaScript.

### TypeScript API

| Operation | TypeScript API |
| --------------------------------- | ------------------------------------------------------------------------ |
| Shared initialization | `loadTunelet(source?)` |
| Mono `Int16Array` to stereo | `process(input, left, right, events?)` |
| Mono `Float32Array` to stereo | `processF32(input, left, right, events?)` |
| In-place `Int16Array` mono | `processMono(audio, events?)` |
| In-place `Float32Array` mono | `processMonoF32(audio, events?)` |
| Fixed `Float32` buffer offsets | `inputF32Ptr`, `leftF32Ptr`, `rightF32Ptr` |
| Cached `Float32` memory views | `new TuneletAudioViews(memory, processor)` |
| Zero-copy `Float32` stereo | `processF32InPlace(frames, events?)` |
| Zero-copy `Float32` mono | `processMonoF32InPlace(frames, events?)` |
| Configuration | `config()`, `setConfig(config)`, `defaultConfig()` |
| Control activity | `controlActivity()` |
| Information about the preparation | `sampleRate`, `maxBlockFrames`, `latencyFrames` |
| Feedback | `feedback()`, `takeFeedback()` |
| Telemetry | `telemetry()`, `takeTelemetry()` |
| Formant envelopes | `formantEnvelope(maxFrequencyHz, input, output)` |
| `Int16Array` finite tail | `beginDrain()`, `drain(left, right)`, `drainMono(audio)` |
| `Float32Array` finite tail | `beginDrain()`, `drainF32(left, right)`, `drainMonoF32(audio)` |
| Zero-copy `Float32` tail | `beginDrain()`, `drainF32InPlace(frames)`, `drainMonoF32InPlace(frames)` |
| Configured tail budget | `tailFrames()` |
| Reset or release memory | `reset()`, `free()`, `views.dispose()` |
| Operation | TypeScript API |
| --------------------------------- | -------------------------------------------------------------------------------------------- |
| Shared initialization | `loadTunelet(source?)` |
| Mono `Int16Array` to stereo | `process(input, left, right, events?)` |
| Mono `Float32Array` to stereo | `processF32(input, left, right, events?)` |
| In-place `Int16Array` mono | `processMono(audio, events?)` |
| In-place `Float32Array` mono | `processMonoF32(audio, events?)` |
| Fixed `Float32` buffer offsets | `inputF32Ptr`, `leftF32Ptr`, `rightF32Ptr` |
| Cached `Float32` memory views | `new TuneletAudioViews(memory, processor)` |
| Zero-copy `Float32` stereo | `processF32InPlace(frames, events?)` |
| Zero-copy `Float32` mono | `processMonoF32InPlace(frames, events?)` |
| Configuration | `config()`, `setConfig(config)`, `defaultConfig()`, `presetConfig(preset)` |
| Scales and pitch ratios | `noteMaskFromKeyScale(key, scale)`, `semitonesToRatio(semitones)`, `ratioToSemitones(ratio)` |
| Control activity | `controlActivity()` |
| Information about the preparation | `sampleRate`, `maxBlockFrames`, `latencyFrames` |
| Feedback | `feedback()`, `takeFeedback()` |
| Telemetry | `telemetry()`, `takeTelemetry()` |
| Formant envelopes | `formantEnvelope(maxFrequencyHz, input, output)` |
| `Int16Array` finite tail | `beginDrain()`, `drain(left, right)`, `drainMono(audio)` |
| `Float32Array` finite tail | `beginDrain()`, `drainF32(left, right)`, `drainMonoF32(audio)` |
| Zero-copy `Float32` tail | `beginDrain()`, `drainF32InPlace(frames)`, `drainMonoF32InPlace(frames)` |
| Configured tail budget | `tailFrames()` |
| Reset or release memory | `reset()`, `free()`, `views.dispose()` |

### Audio buffers

Expand Down Expand Up @@ -573,7 +581,7 @@ These lifetime rules apply to `Float32` views:
Configuration objects contain all required fields. They use camelCase names
and show enums as string literals. To change the configuration, do these steps:

1. Start from `defaultConfig()` or `config()`.
1. Start from `defaultConfig()`, `presetConfig(preset)` or `config()`.
2. Edit the necessary fields.
3. Call `setConfig()`.

Expand Down
11 changes: 10 additions & 1 deletion scripts/normalize-jsdoc.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ const EXACT_REPLACEMENTS = [
["[`HarmonySource::ChordByName`]", '`"chordByName"`'],
["[`HarmonySource::ChordByDegree`]", '`"chordByDegree"`'],
["[`MultiVoiceMode::Choir`]", '`"choir"`'],
["[`Preset::LushChoir`]", '`"lushChoir"`'],
["`None`", "`undefined`"],
["`u64::MAX`", "`2n ** 64n - 1n`"],
["`0x1234_abcd`", "`0x1234abcdn`"],
Expand All @@ -124,7 +125,15 @@ const PROSE_REPLACEMENTS = [
[/\bearlier\s+in\s+the\s+slice\b/g, "earlier in the array"],
[
/A\s+scale\s+for\s+\[`NoteMask::from_key_scale`\]\./g,
"A major, natural minor or chromatic scale name.",
"A scale name for `noteMaskFromKeyScale`.",
],
[
/A\s+preset\s+vocal\s+chain\s+for\s+\[`ProcessorConfig::from_preset`\]\./g,
"A preset vocal chain name for `presetConfig`.",
],
[
/Each\s+preset\s+returns\s+a\s+complete\s+\[`ProcessorConfig`\]\s+that\s+passes\s+\[`ProcessorConfig::validate`\]\.\s+You\s+can\s+edit\s+any\s+field\s+before\s+you\s+give\s+the\s+configuration\s+to\s+\[`crate::Processor::new`\]\s+or\s+\[`crate::Processor::set_config`\]\./g,
"Each preset gives a complete, valid `ProcessorConfig`. You can edit any\nfield before you give the configuration to `new Tunelet` or\n`Tunelet.setConfig`.",
],
[
/Get\s+this\s+snapshot\s+from\s+\[`ProcessorConfig::control_activity`\]\s+or\s+\[`crate::Processor::control_activity`\]\./g,
Expand Down
127 changes: 124 additions & 3 deletions scripts/verify-pack.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -305,8 +305,16 @@ function verifyRuntimeImport(consumerDir, packageName) {
'assert.equal(typeof rootModule.Tunelet, "function");',
'assert.equal(typeof rootModule.TuneletAudioViews, "function");',
'assert.equal(typeof rootModule.defaultConfig, "function");',
'assert.equal(typeof rootModule.presetConfig, "function");',
'assert.equal(typeof rootModule.noteMaskFromKeyScale, "function");',
'assert.equal(typeof rootModule.semitonesToRatio, "function");',
'assert.equal(typeof rootModule.ratioToSemitones, "function");',
'assert.equal(typeof wasmModule.default, "function");',
'assert.equal(typeof wasmModule.initSync, "function");',
'assert.equal(typeof wasmModule.presetConfig, "function");',
'assert.equal(typeof wasmModule.noteMaskFromKeyScale, "function");',
'assert.equal(typeof wasmModule.semitonesToRatio, "function");',
'assert.equal(typeof wasmModule.ratioToSemitones, "function");',
"",
"const wasmExports = await rootModule.loadTunelet();",
"assert.ok(wasmExports);",
Expand Down Expand Up @@ -385,6 +393,85 @@ function verifyRuntimeImport(consumerDir, packageName) {
" processor.free();",
"}",
"",
"for (const presetName of [",
' "naturalTune",',
' "hardPopTune",',
' "lushChoir",',
' "diatonicHarmony",',
' "warmBroadcast",',
"]) {",
" const presetCfg = rootModule.presetConfig(presetName);",
" const presetProc = new rootModule.Tunelet(48000, 128, presetCfg);",
" try {",
" assert.equal(presetProc.sampleRate, 48000);",
" } finally {",
" presetProc.free();",
" }",
"}",
'const lushConfig = rootModule.presetConfig("lushChoir");',
"assert.equal(lushConfig.resynthesis.choirVoices, 8);",
"assert.equal(lushConfig.outputGain, Math.fround(0.35));",
'const harmonyPreset = rootModule.presetConfig("diatonicHarmony");',
'assert.equal(harmonyPreset.resynthesis.mode, "harmony");',
"assert.equal(harmonyPreset.resynthesis.harmony.voices, 2);",
'assert.throws(() => rootModule.presetConfig("invalidPreset"));',
"",
'assert.deepEqual(rootModule.noteMaskFromKeyScale("fSharp", "chromatic"), Array(12).fill(true));',
"assert.deepEqual(",
' rootModule.noteMaskFromKeyScale("a", "minor"),',
" [true, false, true, false, true, true, false, true, false, true, false, true],",
");",
"assert.deepEqual(",
' rootModule.noteMaskFromKeyScale("c", "harmonicMinor"),',
" [true, false, true, true, false, true, false, true, true, false, false, true],",
");",
"assert.deepEqual(",
' rootModule.noteMaskFromKeyScale("d", "dorian"),',
" [true, false, true, false, true, true, false, true, false, true, false, true],",
");",
"assert.deepEqual(",
' rootModule.noteMaskFromKeyScale("c", "blues"),',
" [true, false, false, true, false, true, true, true, false, false, true, false],",
");",
'assert.throws(() => rootModule.noteMaskFromKeyScale("c", "unknownScale"));',
'assert.throws(() => rootModule.noteMaskFromKeyScale("h", "major"));',
"",
"assert.equal(rootModule.semitonesToRatio(-24), 0.25);",
"assert.equal(rootModule.semitonesToRatio(-12), 0.5);",
"assert.equal(rootModule.semitonesToRatio(0), 1);",
"assert.equal(rootModule.semitonesToRatio(12), 2);",
"assert.equal(rootModule.semitonesToRatio(24), 4);",
"const majorThirdRatio = rootModule.semitonesToRatio(4);",
"assert.ok(Math.abs(majorThirdRatio - Math.pow(2, 4 / 12)) < 1e-6);",
"assert.equal(rootModule.ratioToSemitones(0.25), -24);",
"assert.equal(rootModule.ratioToSemitones(0.5), -12);",
"assert.equal(rootModule.ratioToSemitones(1), 0);",
"assert.equal(rootModule.ratioToSemitones(2), 12);",
"assert.equal(rootModule.ratioToSemitones(4), 24);",
"assert.ok(Math.abs(rootModule.ratioToSemitones(majorThirdRatio) - 4) < 1e-5);",
"assert.throws(() => rootModule.semitonesToRatio(-24.01));",
"assert.throws(() => rootModule.semitonesToRatio(24.01));",
"assert.throws(() => rootModule.semitonesToRatio(Number.NaN));",
"assert.throws(() => rootModule.semitonesToRatio(Number.POSITIVE_INFINITY));",
"assert.throws(() => rootModule.ratioToSemitones(0.24));",
"assert.throws(() => rootModule.ratioToSemitones(4.01));",
"assert.throws(() => rootModule.ratioToSemitones(Number.NaN));",
"assert.throws(() => rootModule.ratioToSemitones(Number.POSITIVE_INFINITY));",
"",
'const helperConfig = rootModule.presetConfig("naturalTune");',
'helperConfig.resynthesis.correction.notes = rootModule.noteMaskFromKeyScale("a", "harmonicMinor");',
"helperConfig.resynthesis.pitchRatio = majorThirdRatio;",
"const helperProcessor = new rootModule.Tunelet(48000, 128, helperConfig);",
"try {",
" assert.deepEqual(",
" helperProcessor.config().resynthesis.correction.notes,",
' rootModule.noteMaskFromKeyScale("a", "harmonicMinor"),',
" );",
" assert.equal(helperProcessor.config().resynthesis.pitchRatio, majorThirdRatio);",
"} finally {",
" helperProcessor.free();",
"}",
"",
"const config = rootModule.defaultConfig();",
"config.resynthesis.formant.enabled = true;",
"config.reverb.enabled = true;",
Expand Down Expand Up @@ -734,18 +821,30 @@ function verifyTypeDeclarations(consumerDir, packageName) {
" defaultConfig,",
" initSync as rootInitSync,",
" loadTunelet as namedLoadTunelet,",
" noteMaskFromKeyScale,",
" presetConfig,",
" ratioToSemitones,",
" semitonesToRatio,",
" Tunelet,",
" TuneletAudioViews,",
" type ControlActivity,",
" type ControlEvent,",
" type CorrectionScale,",
" type NoteMask,",
" type ParameterControl,",
" type PitchClass,",
" type Preset,",
" type ProcessorConfig,",
" type ResynthesisConfig,",
" type SyncInitInput as RootSyncInitInput,",
" type Telemetry,",
`} from ${pkgLiteral};`,
"import init, {",
" initSync as wasmInitSync,",
" noteMaskFromKeyScale as wasmNoteMaskFromKeyScale,",
" presetConfig as wasmPresetConfig,",
" ratioToSemitones as wasmRatioToSemitones,",
" semitonesToRatio as wasmSemitonesToRatio,",
" type InitInput,",
" type InitOutput,",
" type ParameterControl as WasmParameterControl,",
Expand All @@ -770,12 +869,26 @@ function verifyTypeDeclarations(consumerDir, packageName) {
" module: syncSource,",
" });",
" const baseConfig: ProcessorConfig = defaultConfig();",
' const selectedPreset: Preset = "lushChoir";',
' const selectedKey: PitchClass = "a";',
' const selectedScale: CorrectionScale = "harmonicMinor";',
" const selectedMask: NoteMask = noteMaskFromKeyScale(selectedKey, selectedScale);",
" const wasmMask: NoteMask = wasmNoteMaskFromKeyScale(selectedKey, selectedScale);",
" const presetConfiguration: ProcessorConfig = presetConfig(selectedPreset);",
" const wasmPresetConfiguration: ProcessorConfig = wasmPresetConfig(selectedPreset);",
" const shiftedRatio: number = semitonesToRatio(4);",
" const wasmShiftedRatio: number = wasmSemitonesToRatio(4);",
" const shiftedSemitones: number = ratioToSemitones(shiftedRatio);",
" const wasmShiftedSemitones: number = wasmRatioToSemitones(wasmShiftedRatio);",
" const configWithUndefined: ProcessorConfig = {",
" ...baseConfig,",
" ...presetConfiguration,",
" outputGain: baseConfig.outputGain,",
" resynthesis: {",
" ...baseConfig.resynthesis,",
" pitchRatio: shiftedRatio,",
" correction: {",
" ...baseConfig.resynthesis.correction,",
" notes: selectedMask,",
" humanize: optionalVoices,",
" flexTune: optionalVoices,",
" },",
Expand Down Expand Up @@ -878,6 +991,10 @@ function verifyTypeDeclarations(consumerDir, packageName) {
" void exportsFromWasm;",
" void syncExportsFromRoot;",
" void syncExportsFromWasm;",
" void wasmMask;",
" void wasmPresetConfiguration;",
" void shiftedSemitones;",
" void wasmShiftedSemitones;",
" return exportsFromRoot;",
"}",
];
Expand Down Expand Up @@ -1176,8 +1293,8 @@ function verifyNormalizedJsDoc(consumerDir, packageName) {

const throwsCount = [...dts.matchAll(/\* @throws /g)].length;
assert.ok(
throwsCount >= 14,
`Expected at least 14 @throws tags, found ${throwsCount}`,
throwsCount >= 18,
`Expected at least 18 @throws tags, found ${throwsCount}`,
);

const fallibleSignatures = [
Expand All @@ -1195,6 +1312,10 @@ function verifyNormalizedJsDoc(consumerDir, packageName) {
["takeTelemetry", /(\/\*\*(?:(?!\*\/)[\s\S])*\*\/)\s+takeTelemetry\(\): Telemetry;/],
["telemetry", /(\/\*\*(?:(?!\*\/)[\s\S])*\*\/)\s+telemetry\(\): Telemetry;/],
["defaultConfig", /(\/\*\*(?:(?!\*\/)[\s\S])*\*\/)\s+export function defaultConfig\(\): ProcessorConfig;/],
["noteMaskFromKeyScale", /(\/\*\*(?:(?!\*\/)[\s\S])*\*\/)\s+export function noteMaskFromKeyScale\(key: PitchClass, scale: CorrectionScale\): NoteMask;/],
["presetConfig", /(\/\*\*(?:(?!\*\/)[\s\S])*\*\/)\s+export function presetConfig\(preset: Preset\): ProcessorConfig;/],
["ratioToSemitones", /(\/\*\*(?:(?!\*\/)[\s\S])*\*\/)\s+export function ratioToSemitones\(ratio: number\): number;/],
["semitonesToRatio", /(\/\*\*(?:(?!\*\/)[\s\S])*\*\/)\s+export function semitonesToRatio\(semitones: number\): number;/],
];
for (const [label, pattern] of fallibleSignatures) {
const doc = extractMemberJsDoc(dts, pattern, label);
Expand Down
Loading
Loading