@polygonjs/polygonjs
Version:
node-based WebGL 3D engine https://polygonjs.com
79 lines (78 loc) • 2.49 kB
JavaScript
;
import { TypedAudioNode } from "./_Base";
import { NodeParamsConfig, ParamConfig } from "../utils/params/ParamsConfig";
import { NodeEvent } from "../../poly/NodeEvent";
import { AudioType } from "../../poly/registers/nodes/types/Audio";
const DEFAULT_INPUTS_COUNT = 4;
class SwitchAudioParamsConfig extends NodeParamsConfig {
constructor() {
super(...arguments);
/** @param sets which input is used */
this.input = ParamConfig.INTEGER(0, {
range: [0, 3],
rangeLocked: [true, true],
callback: (node) => {
SwitchAudioNode.PARAM_CALLBACK_setInputsEvaluation(node);
}
});
/** @param number of inputs that this node can merge geometries from */
this.inputsCount = ParamConfig.INTEGER(DEFAULT_INPUTS_COUNT, {
range: [1, 32],
rangeLocked: [true, false],
separatorBefore: true,
callback: (node) => {
SwitchAudioNode.PARAM_CALLBACK_setInputsCount(node);
}
});
}
}
const ParamsConfig = new SwitchAudioParamsConfig();
export class SwitchAudioNode extends TypedAudioNode {
constructor() {
super(...arguments);
this.paramsConfig = ParamsConfig;
}
static type() {
return AudioType.SWITCH;
}
initializeNode() {
this.io.inputs.setCount(0, 4);
this.io.inputs.onEnsureListenToSingleInputIndexUpdated(async () => {
await this._callbackUpdateInputsEvaluation();
});
this.params.onParamsCreated("update inputs", () => {
this._callbackUpdateInputsCount();
});
}
async cook(inputContents) {
const inputIndex = this.pv.input;
if (this.io.inputs.hasInput(inputIndex)) {
const container = await this.containerController.requestInputContainer(inputIndex);
if (container) {
const audioBuilder = container.coreContent();
if (audioBuilder) {
this.setAudioBuilder(audioBuilder);
return;
}
}
} else {
this.states.error.set(`no input ${inputIndex}`);
}
}
async _callbackUpdateInputsEvaluation() {
if (this.p.input.isDirty()) {
await this.p.input.compute();
}
this.io.inputs.listenToSingleInputIndex(this.pv.input);
}
static PARAM_CALLBACK_setInputsEvaluation(node) {
node._callbackUpdateInputsEvaluation();
}
_callbackUpdateInputsCount() {
this.io.inputs.setCount(1, this.pv.inputsCount);
this.emit(NodeEvent.INPUTS_UPDATED);
}
static PARAM_CALLBACK_setInputsCount(node) {
node._callbackUpdateInputsCount();
}
}