UNPKG

@polygonjs/polygonjs

Version:

node-based WebGL 3D engine https://polygonjs.com

65 lines (64 loc) 2.06 kB
"use strict"; import { TypedAudioNode } from "./_Base"; import { NodeParamsConfig, ParamConfig } from "../utils/params/ParamsConfig"; import { AudioBuilder } from "../../../core/audio/AudioBuilder"; export const ENVELOPE_DEFAULTS = { attackCurve: "linear", attack: 0.01, decayCurve: "exponential", decay: 0.1, releaseCurve: "exponential", release: 1, sustain: 0.5 }; class EnvelopeAudioParamsConfig 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 EnvelopeAudioParamsConfig(); export class EnvelopeAudioNode extends TypedAudioNode { constructor() { super(...arguments); this.paramsConfig = ParamsConfig; } static type() { return "envelope"; } initializeNode() { this.io.inputs.setCount(0); } cook(inputContents) { const audioBuilder = new AudioBuilder(); audioBuilder.setEnvelopeParams({ attackCurve: "linear", attack: this.pv.attack, decayCurve: "exponential", decay: this.pv.decay, releaseCurve: "exponential", release: this.pv.release, sustain: this.pv.sustain }); this.setAudioBuilder(audioBuilder); } }