Skip to content
Merged
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
12 changes: 10 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
# synthlet

## 0.11.0

- Granite effect module `@synthlet/granite`

## 0.10.0

- VirtualAnalogFilter effect module `@synthlet/virtual-analog-filter`

## 0.9.0

- New ReverbDelay effect `@synthlet/reverb-delay`
- ReverbDelay effect module `@synthlet/reverb-delay`

## 0.8.0

- New KarplusStrong audio source `@synthlet/karplus-strong`
- KarplusStrong source module `@synthlet/karplus-strong`

## 0.7.0

Expand Down
33 changes: 33 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions packages/granite/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# @synthlet/granite

# 0.1.0

Initial release
5 changes: 5 additions & 0 deletions packages/granite/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# @synthlet/granite

> Granular processor

Part of [Synthlet](https://github.com/danigb/synthlet)
34 changes: 34 additions & 0 deletions packages/granite/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"name": "@synthlet/granite",
"version": "0.1.0",
"description": "Granular processor worklet",
"keywords": [
"granular",
"processor",
"synthlet"
],
"main": "dist/index.js",
"module": "dist/index.mjs",
"types": "dist/index.d.ts",
"files": [
"dist"
],
"author": "[email protected]",
"license": "MIT",
"publishConfig": {
"access": "public"
},
"devDependencies": {
"@types/jest": "^29.5.13",
"jest": "^29.7.0",
"ts-jest": "^29.1.1"
},
"jest": {
"preset": "ts-jest"
},
"scripts": {
"worklet": "esbuild src/worklet.ts --bundle --minify | sed -e 's/^/export const PROCESSOR = \\`/' -e 's/$/\\`;/' > src/processor.ts",
"lib": "tsup src/index.ts --sourcemap --dts --format esm,cjs",
"build": "npm run worklet && npm run lib"
}
}
119 changes: 119 additions & 0 deletions packages/granite/src/_worklet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
// DON'T EDIT THIS FILE unless inside scripts/_worklet.ts
// use ./scripts/copy_files.ts to copy this file to the right place
// the goal is to avoid external dependencies on packages

// A "Connector" is a function that takes an AudioContext and returns an AudioNode
// or an custom object with a connect method (that returns a disconnect method)
export type Connector<N extends AudioNode> = (context: AudioContext) => N;

export type ParamInput = number | Connector<AudioNode> | AudioNode;

type CreateWorkletOptions<N, P> = {
processorName: string;
paramNames: readonly string[];
workletOptions: (params: Partial<P>) => AudioWorkletNodeOptions;
postCreate?: (node: N) => void;
};

export type Disposable<N extends AudioNode> = N & { dispose: () => void };

export function createWorkletConstructor<
N extends AudioWorkletNode,
P extends Record<string, ParamInput>
>(options: CreateWorkletOptions<N, P>) {
return (
audioContext: AudioContext,
inputs: Partial<P> = {}
): Disposable<N> => {
const node = new AudioWorkletNode(
audioContext,
options.processorName,
options.workletOptions(inputs)
) as N;

(node as any).__PROCESSOR_NAME__ = options.processorName;
const connected = connectParams(node, options.paramNames, inputs);
options.postCreate?.(node);
return disposable(node, connected);
};
}

type ConnectedUnit = AudioNode | (() => void);

export function connectParams(
node: any,
paramNames: readonly string[],
inputs: any
): ConnectedUnit[] {
const connected: ConnectedUnit[] = [];

for (const paramName of paramNames) {
if (node.parameters) {
node[paramName] = node.parameters.get(paramName);
}
const param = node[paramName];
if (!param) throw Error("Invalid param name: " + paramName);
const input = inputs[paramName];
if (typeof input === "number") {
param.value = input;
} else if (input instanceof AudioNode) {
param.value = 0;
input.connect(param);
connected.push(input);
} else if (typeof input === "function") {
param.value = 0;
const source = input(node.context);
source.connect(param);
connected.push(source);
}
}

return connected;
}

export function disposable<N extends AudioNode>(
node: N,
dependencies?: ConnectedUnit[]
): Disposable<N> {
let disposed = false;
return Object.assign(node, {
dispose() {
if (disposed) return;
disposed = true;

node.disconnect();
(node as any).port?.postMessage({ type: "DISPOSE" });
if (!dependencies) return;

while (dependencies.length) {
const conn = dependencies.pop();
if (conn instanceof AudioNode) {
if (typeof (conn as any).dispose === "function") {
(conn as any).dispose?.();
} else {
conn.disconnect();
}
} else if (typeof conn === "function") {
conn();
}
}
},
});
}

export function createRegistrar(processorName: string, processor: string) {
return function (context: AudioContext): Promise<void> {
const key = "__" + processorName + "__";
if (key in context) return (context as any)[key];

if (!context.audioWorklet || !context.audioWorklet.addModule) {
throw Error("AudioWorklet not supported");
}

const blob = new Blob([processor], { type: "application/javascript" });
const url = URL.createObjectURL(blob);
const promise = context.audioWorklet.addModule(url);
(context as any)[key] = promise;
return promise;
};
}
Loading
Loading