UNPKG

mosfez-synth

Version:

A microtonal-aware synth engine library for web.

1 lines 8.14 kB
{"version":3,"file":"dsp-node.mjs","sources":["../src/internal/param-utils.ts","../src/dsp-node.ts"],"sourcesContent":["import { ParamValueObject } from \"../params\";\nimport { ParamDefinitionObject, ParamDefinition } from \"../dsp-node\";\n\nexport function validateParamDefinition(\n name: string,\n paramDef: ParamDefinition\n): ParamDefinition {\n if (!isConstant(paramDef) && !isVariable(paramDef)) {\n throw new Error(\n `param \"${name}\" must be a constant number or a string referring to a param name, but got ${paramDef}`\n );\n }\n return paramDef;\n}\n\nexport function validateParamDefinitionObject(\n paramDefs: ParamDefinitionObject\n): ParamDefinitionObject {\n for (const name in paramDefs) {\n validateParamDefinition(name, paramDefs[name] as ParamDefinition);\n }\n return paramDefs;\n}\n\nexport function isConstant(paramDef: ParamDefinition): paramDef is number {\n return typeof paramDef === \"number\";\n}\n\nexport function isVariable(paramDef: ParamDefinition): paramDef is string {\n return typeof paramDef === \"string\";\n}\n\nexport function resolveParam(\n params: Partial<ParamValueObject>,\n paramDef: ParamDefinition\n): number | undefined {\n if (isConstant(paramDef)) {\n return paramDef;\n } else if (isVariable(paramDef)) {\n return params[paramDef] as number;\n }\n return undefined;\n}\n","import type { compile } from \"mosfez-faust/faust\";\nimport type { VoiceController } from \"./internal/voice-controller\";\nimport {\n validateParamDefinitionObject,\n validateParamDefinition,\n} from \"./internal/param-utils\";\n\nexport type ParamDefinition = number | string;\n\nexport type ParamDefinitionObject = Record<string, ParamDefinition>;\n\nexport type DspNodeType = \"faust\" | \"poly\";\n\nexport type DspNodeSerialized = DspNodeFaustSerialized | DspNodePolySerialized;\n\nexport type DspNodeConfig = {\n type: DspNodeType;\n};\n\nexport class DspNode {\n type: DspNodeType;\n\n constructor(config: DspNodeConfig) {\n this.type = config.type;\n }\n\n static isFaustDspNode(DspNode: DspNode): DspNode is DspNodeFaust {\n return DspNode.type === \"faust\";\n }\n\n static isPolyDspNode(DspNode: DspNode): DspNode is DspNodePoly {\n return DspNode.type === \"poly\";\n }\n\n static isFaustDspNodeSerialized(\n serialized: DspNodeSerialized\n ): serialized is DspNodeFaustSerialized {\n return serialized.type === \"faust\";\n }\n\n static isPolyDspNodeSerialized(\n serialized: DspNodeSerialized\n ): serialized is DspNodePolySerialized {\n return serialized.type === \"poly\";\n }\n\n serialize(): DspNodeSerialized {\n throw new Error(\".serialize() can only be called on subclasses\");\n }\n}\n\n//\n// faust\n//\n\nexport type DspNodeFaustDependencies = {\n compile: typeof compile;\n};\n\nexport type DspNodeFaustConfig = {\n dsp: string;\n inputs?: DspNode[];\n paramDefs: ParamDefinitionObject;\n dependencies: DspNodeFaustDependencies;\n};\n\nexport type DspNodeFaustSerialized = {\n type: \"faust\";\n dsp: string;\n inputs: DspNodeSerialized[];\n paramDefs: ParamDefinitionObject;\n};\n\nexport class DspNodeFaust extends DspNode {\n dsp: string;\n inputs: DspNode[];\n paramDefs: ParamDefinitionObject;\n dependencies: DspNodeFaustDependencies;\n\n constructor(config: DspNodeFaustConfig) {\n super({\n type: \"faust\",\n });\n\n this.dsp = config.dsp;\n this.inputs = config.inputs ?? [];\n this.paramDefs = validateParamDefinitionObject(config.paramDefs);\n this.dependencies = config.dependencies;\n }\n\n serialize(): DspNodeFaustSerialized {\n const { dsp, paramDefs } = this;\n const inputs = this.inputs.map((input) => input.serialize());\n\n return {\n type: \"faust\",\n dsp,\n inputs,\n paramDefs,\n };\n }\n}\n\n//\n// poly\n//\n\nexport type DspNodePolyDependencies = {\n VoiceController: typeof VoiceController;\n};\n\nexport type DspNodePolyConfig = {\n input: DspNode;\n polyphony: number;\n paramCacheSize?: number;\n release: ParamDefinition;\n gate: ParamDefinition;\n dependencies: DspNodePolyDependencies;\n};\n\nexport type DspNodePolySerialized = {\n type: \"poly\";\n input: DspNodeSerialized;\n polyphony: number;\n paramCacheSize?: number;\n release: ParamDefinition;\n gate: ParamDefinition;\n};\n\nexport class DspNodePoly extends DspNode {\n input: DspNode;\n polyphony: number;\n paramCacheSize?: number;\n release: ParamDefinition;\n gate: ParamDefinition;\n dependencies: DspNodePolyDependencies;\n\n constructor(config: DspNodePolyConfig) {\n super({\n type: \"poly\",\n });\n\n this.input = config.input;\n this.polyphony = config.polyphony;\n this.paramCacheSize = config.paramCacheSize;\n this.release = validateParamDefinition(\"release\", config.release);\n this.gate = validateParamDefinition(\"gate\", config.gate);\n this.dependencies = config.dependencies;\n }\n\n serialize(): DspNodePolySerialized {\n const { polyphony, paramCacheSize, release, gate } = this;\n const input = this.input.serialize();\n\n return {\n type: \"poly\",\n input,\n polyphony,\n paramCacheSize,\n release,\n gate,\n };\n }\n}\n"],"names":[],"mappings":"AAAO,SAAS,uBAAuB,CAAC,IAAI,EAAE,QAAQ,EAAE;AACxD,EAAE,IAAI,CAAC,UAAU,CAAC,QAAQ,CAAC,IAAI,CAAC,UAAU,CAAC,QAAQ,CAAC,EAAE;AACtD,IAAI,MAAM,IAAI,KAAK,CAAC,CAAC,OAAO,EAAE,IAAI,CAAC,2EAA2E,EAAE,QAAQ,CAAC,CAAC,CAAC,CAAC;AAC5H,GAAG;AACH,EAAE,OAAO,QAAQ,CAAC;AAClB,CAAC;AACM,SAAS,6BAA6B,CAAC,SAAS,EAAE;AACzD,EAAE,KAAK,MAAM,IAAI,IAAI,SAAS,EAAE;AAChC,IAAI,uBAAuB,CAAC,IAAI,EAAE,SAAS,CAAC,IAAI,CAAC,CAAC,CAAC;AACnD,GAAG;AACH,EAAE,OAAO,SAAS,CAAC;AACnB,CAAC;AACM,SAAS,UAAU,CAAC,QAAQ,EAAE;AACrC,EAAE,OAAO,OAAO,QAAQ,KAAK,QAAQ,CAAC;AACtC,CAAC;AACM,SAAS,UAAU,CAAC,QAAQ,EAAE;AACrC,EAAE,OAAO,OAAO,QAAQ,KAAK,QAAQ,CAAC;AACtC;;ACbO,MAAM,OAAO,CAAC;AACrB,EAAE,IAAI,CAAC;AACP,EAAE,WAAW,CAAC,MAAM,EAAE;AACtB,IAAI,IAAI,CAAC,I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