@polygonjs/polygonjs
Version:
node-based WebGL 3D engine https://polygonjs.com
64 lines (63 loc) • 2.09 kB
JavaScript
;
import { TypedAudioNode } from "./_Base";
import { NodeParamsConfig, ParamConfig } from "../utils/params/ParamsConfig";
import { AmplitudeEnvelope } from "tone/build/esm/component/envelope/AmplitudeEnvelope";
export const ENVELOPE_DEFAULTS = {
attack: 0.01,
decay: 0.1,
release: 1,
sustain: 0.5
};
class AmplitudeEnvelopeAudioParamsConfig extends NodeParamsConfig {
constructor() {
super(...arguments);
/** @param The amount of time it takes for the envelope to go from 0 to it's maximum value. */
this.attack = ParamConfig.FLOAT(ENVELOPE_DEFAULTS.attack, {
range: [0, 0.1],
rangeLocked: [true, false]
});
/** The period of time after the attack that it takes for the envelope to fall to the sustain value. */
this.decay = ParamConfig.FLOAT(ENVELOPE_DEFAULTS.decay, {
range: [0, 1],
rangeLocked: [true, false]
});
/** The percent of the maximum value that the envelope rests at until the release is triggered. */
this.sustain = ParamConfig.FLOAT(ENVELOPE_DEFAULTS.sustain, {
range: [0, 1],
rangeLocked: [true, false]
});
/** The amount of time after the release is triggered it takes to reach 0. */
this.release = ParamConfig.FLOAT(ENVELOPE_DEFAULTS.release, {
range: [0, 1],
rangeLocked: [true, false]
});
}
}
const ParamsConfig = new AmplitudeEnvelopeAudioParamsConfig();
export class AmplitudeEnvelopeAudioNode extends TypedAudioNode {
constructor() {
super(...arguments);
this.paramsConfig = ParamsConfig;
}
static type() {
return "amplitudeEnveloppe";
}
initializeNode() {
this.io.inputs.setCount(1);
}
cook(inputContents) {
const audioBuilder = inputContents[0];
const ampEnv = new AmplitudeEnvelope({
attack: this.pv.attack,
decay: this.pv.decay,
release: this.pv.release,
sustain: this.pv.sustain
});
const inputNode = audioBuilder.audioNode();
if (inputNode) {
inputNode.connect(ampEnv);
}
audioBuilder.setAudioNode(ampEnv);
this.setAudioBuilder(audioBuilder);
}
}