Skip to content

Commit

Permalink
feat: Synthlet object
Browse files Browse the repository at this point in the history
  • Loading branch information
danigb committed Oct 13, 2023
1 parent feeb20b commit b45291f
Show file tree
Hide file tree
Showing 11 changed files with 91 additions and 54 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ osc.disconnect();
But you can choose which ones to load if you don't need or want the full library:

```js
import { loadKarplusStrongOscillatorNode } from "synthlet";
import { loadKarplusStrongOscillatorProcessor } from "synthlet";

const ks = await loadKarplusStrongOscillatorNode(context);
const ks = await loadKarplusStrongOscillatorProcessor(context);

const osc = ks({ note: 60 }).connect(context.destination);
osc.disconnect();
Expand All @@ -103,9 +103,9 @@ osc.disconnect();
The load function returns a promise to a function that create audio nodes:

```js
import { loadLfoNode } from "synthlet";
import { loadLfoProcessor } from "synthlet";

const createLfo = await loadLfoNode(context);
const createLfo = await loadLfoProcessor(context);
const lfo = createLfo({ frequency: 10 }); // lfo is an AudioNode
```

Expand Down
4 changes: 2 additions & 2 deletions src/adsr/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import { PROCESSOR } from "./processor";
export type AdsrOptions = GenerateNodeOptions<typeof PARAMS>;
export type AdsrNode = GenerateNodeType<typeof PARAMS>;

export const loadAdsrNode = createLoader(PROCESSOR);
export const loadAdsrProcessor = createLoader(PROCESSOR);
export const Adsr = createConstructor<AdsrNode, AdsrOptions>(
"AdsrWorklet",
PARAMS
);
export const loadAdsr = loadWorklet(loadAdsrNode, Adsr);
export const loadAdsr = loadWorklet(loadAdsrProcessor, Adsr);
4 changes: 2 additions & 2 deletions src/impulse/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ export const Impulse = createConstructor<ImpulseNode, ImpulseOptions>(
"ImpulseWorklet",
ImpulseParams
);
export const loadImpulseNode = createLoader(PROCESSOR);
export const loadImpulse = loadWorklet(loadImpulseNode, Impulse);
export const loadImpulseProcessor = createLoader(PROCESSOR);
export const loadImpulse = loadWorklet(loadImpulseProcessor, Impulse);
72 changes: 45 additions & 27 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
import { loadAdsr, loadAdsrNode } from "./adsr/index";
import { loadImpulse, loadImpulseNode } from "./impulse/index";
import { loadKarplusStrongOscillatorNode } from "./karplus-strong/index";
import { loadLfo, loadLfoNode } from "./lfo/index";
import { loadPcmOscillatorNode } from "./pcm-oscillator/index";
import { loadSequencer, loadSequencerNode } from "./sequencer/index";
import { loadAdsr, loadAdsrProcessor } from "./adsr/index";
import { loadImpulse, loadImpulseProcessor } from "./impulse/index";
import {
loadKarplusStrongOscillator,
loadKarplusStrongOscillatorProcessor,
} from "./karplus-strong/index";
import { loadLfo, loadLfoProcessor } from "./lfo/index";
import {
loadPcmOscillator,
loadPcmOscillatorProcessor,
} from "./pcm-oscillator/index";
import { loadSequencer, loadSequencerProcessor } from "./sequencer/index";
import { chain, createKeyboard, createTrigger } from "./utils/index";
import { loadVaFilterNode } from "./va-filter/index";
import { loadVaOscillator, loadVaOscillatorNode } from "./va-oscillator/index";
import { loadWtOscillatorNode } from "./wt-oscillator/index";
import { loadVaFilterProcessor } from "./va-filter/index";
import {
loadVaOscillator,
loadVaOscillatorProcessor,
} from "./va-oscillator/index";
import { loadWtOscillatorProcessor } from "./wt-oscillator/index";

export * from "./adsr/index";
export * from "./impulse/index";
Expand All @@ -23,15 +32,15 @@ export * from "./wt-oscillator/index";

export async function loadSynthletNodes(context: AudioContext) {
await Promise.all([
loadAdsrNode(context),
loadImpulseNode(context),
loadKarplusStrongOscillatorNode(context),
loadLfoNode(context),
loadPcmOscillatorNode(context),
loadVaFilterNode(context),
loadVaOscillatorNode(context),
loadWtOscillatorNode(context),
loadSequencerNode(context),
loadAdsrProcessor(context),
loadImpulseProcessor(context),
loadKarplusStrongOscillatorProcessor(context),
loadLfoProcessor(context),
loadPcmOscillatorProcessor(context),
loadSequencerProcessor(context),
loadVaFilterProcessor(context),
loadVaOscillatorProcessor(context),
loadWtOscillatorProcessor(context),
]);
return context;
}
Expand All @@ -40,25 +49,34 @@ type PromiseType<T> = T extends Promise<infer U> ? U : never;
export type Synthlet = PromiseType<ReturnType<typeof loadSynthlet>>;

export async function loadSynthlet(context: AudioContext) {
const [adsr, impulse, lfo, sequencer, osc] = await Promise.all([
loadAdsr(context),
loadImpulse(context),
loadLfo(context),
loadSequencer(context),
loadVaOscillator(context),
]);
const [adsr, impulse, ks, lfo, pcm, sequencer, filter, osc, wt] =
await Promise.all([
loadAdsr(context),
loadImpulse(context),
loadKarplusStrongOscillator(context),
loadLfo(context),
loadPcmOscillator(context),
loadSequencer(context),
loadVaFilterProcessor(context),
loadVaOscillator(context),
loadWtOscillatorProcessor(context),
]);
const trigger = () => createTrigger(context);
const keyboard = () => createKeyboard(context);

return {
impulse,
adsr,
chain,
context,
lfo,
filter,
impulse,
keyboard,
ks,
lfo,
osc,
pcm,
sequencer,
trigger,
wt,
};
}
8 changes: 7 additions & 1 deletion src/karplus-strong/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ import {
GenerateNodeType,
createConstructor,
createLoader,
loadWorklet,
} from "../worklet-utils";
import { KarplusStrongOscillatorParams } from "./karplus-strong-oscillator";
import { PROCESSOR } from "./processor";

export const loadKarplusStrongOscillatorNode = createLoader(PROCESSOR);
export const loadKarplusStrongOscillatorProcessor = createLoader(PROCESSOR);
export type KarplusStrongOscillatorOptions = GenerateNodeOptions<
typeof KarplusStrongOscillatorParams
>;
Expand All @@ -19,3 +20,8 @@ export const KarplusStrongOscillator = createConstructor<
KarplusStrongOscillatorNode,
KarplusStrongOscillatorOptions
>("KarplusStrongOscillatorWorklet", KarplusStrongOscillatorParams);

export const loadKarplusStrongOscillator = loadWorklet(
loadKarplusStrongOscillatorProcessor,
KarplusStrongOscillator
);
4 changes: 2 additions & 2 deletions src/lfo/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ export type LfoNode = GenerateNodeType<typeof PARAMS>;

export const Lfo = createConstructor<LfoNode, LfoOptions>("LfoWorklet", PARAMS);

export const loadLfoNode = createLoader(PROCESSOR);
export const loadLfo = loadWorklet(loadLfoNode, Lfo);
export const loadLfoProcessor = createLoader(PROCESSOR);
export const loadLfo = loadWorklet(loadLfoProcessor, Lfo);
20 changes: 11 additions & 9 deletions src/pcm-oscillator/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
GenerateNodeType,
addDisconnect,
addParams,
createLoader,
loadWorklet,
Expand All @@ -16,24 +17,25 @@ export type PcmOscillatorNode = GenerateNodeType<typeof PARAMS>;

export const PcmOscillator = (
context: AudioContext,
options: PcmOscillatorOptions
options?: PcmOscillatorOptions
) => {
const node = new AudioWorkletNode(context, "PcmOscillatorWorklet", {
numberOfInputs: 1,
numberOfOutputs: 1,
});
addParams(node, PARAMS);
addDisconnect(node);

if (options) {
const audioBuffer = options.source;
const channelData = audioBuffer.getChannelData(0);
node.port.postMessage({ type: "AUDIO", buffer: channelData });
}

const audioBuffer = options.source;
const channelData = audioBuffer.getChannelData(0);
node.port.postMessage({ type: "AUDIO", buffer: channelData });
node.port.onmessage = (event) => {
console.log("RECEIVED", event.data);
};
return node as PcmOscillatorNode;
};
export const loadPcmOscillatorNode = createLoader(PROCESSOR);
export const loadPcmOscillatorProcessor = createLoader(PROCESSOR);
export const loadPcmOscillator = loadWorklet(
loadPcmOscillatorNode,
loadPcmOscillatorProcessor,
PcmOscillator
);
4 changes: 2 additions & 2 deletions src/sequencer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ export const Sequencer = createConstructor<SequencerNode, SequencerOptions>(
"SequencerWorklet",
PARAMS
);
export const loadSequencerNode = createLoader(PROCESSOR);
export const loadSequencer = loadWorklet(loadSequencerNode, Sequencer);
export const loadSequencerProcessor = createLoader(PROCESSOR);
export const loadSequencer = loadWorklet(loadSequencerProcessor, Sequencer);
4 changes: 3 additions & 1 deletion src/va-filter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@ import {
GenerateNodeType,
createConstructor,
createLoader,
loadWorklet,
} from "../worklet-utils";
import { PROCESSOR } from "./processor";
import { VaFilterParams } from "./va-filter";

export { VA_FILTER_TYPE_NAMES, VaFilterType } from "./va-filter";

export const loadVaFilterNode = createLoader(PROCESSOR);
export const loadVaFilterProcessor = createLoader(PROCESSOR);
export type VaFilterOptions = GenerateNodeOptions<typeof VaFilterParams>;
export type VaFilterNode = GenerateNodeType<typeof VaFilterParams>;

export const VaFilter = createConstructor<VaFilterNode, VaFilterOptions>(
"VaFilterWorklet",
VaFilterParams
);
export const loadVaFilter = loadWorklet(loadVaFilterProcessor, VaFilter);
7 changes: 5 additions & 2 deletions src/va-oscillator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@ export {
export type VaOscillatorOptions = GenerateNodeOptions<typeof PARAMS>;
export type VaOscillatorNode = GenerateNodeType<typeof PARAMS>;

export const loadVaOscillatorNode = createLoader(PROCESSOR);
export const loadVaOscillatorProcessor = createLoader(PROCESSOR);
export const VaOscillator = createConstructor<
VaOscillatorNode,
VaOscillatorOptions
>("VaOscillatorWorklet", PARAMS);
export const loadVaOscillator = loadWorklet(loadVaOscillatorNode, VaOscillator);
export const loadVaOscillator = loadWorklet(
loadVaOscillatorProcessor,
VaOscillator
);
10 changes: 8 additions & 2 deletions src/wt-oscillator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import {
addDisconnect,
addParams,
createLoader,
loadWorklet,
setWorkletOptions,
} from "../worklet-utils";
import { PROCESSOR } from "./processor";
import { PARAMS } from "./wt-oscillator";

export * from "./load-wavetable";

export const loadWtOscillatorNode = createLoader(PROCESSOR);
export const loadWtOscillatorProcessor = createLoader(PROCESSOR);

export type WtOscillatorOptions = GenerateNodeOptions<typeof PARAMS>;

Expand All @@ -21,7 +22,7 @@ export type WtOscillatorNode = GenerateNodeType<typeof PARAMS> & {

export const WtOscillator = (
context: AudioContext,
options: WtOscillatorOptions
options?: WtOscillatorOptions
) => {
const node = new AudioWorkletNode(context, "WtOscillatorWorklet", {
numberOfInputs: 0,
Expand All @@ -41,3 +42,8 @@ export const WtOscillator = (

return node as WtOscillatorNode;
};

export const loadWtOscillator = loadWorklet(
loadWtOscillatorProcessor,
WtOscillator
);

0 comments on commit b45291f

Please sign in to comment.