UNPKG

@polygonjs/polygonjs

Version:

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

71 lines (70 loc) 2.08 kB
"use strict"; import { Distortion } from "tone/build/esm/effect/Distortion"; const DEFAULTS = { distortion: 0.4, oversample: "none" }; const OVER_SAMPLE_TYPES = ["2x", "4x", "none"]; import { TypedAudioNode } from "./_Base"; import { NodeParamsConfig, ParamConfig } from "../utils/params/ParamsConfig"; import { effectParamsOptions } from "./utils/EffectsController"; const paramCallback = (node) => { DistortionAudioNode.PARAM_CALLBACK_updateEffect(node); }; class DistortionAudioParamsConfig extends NodeParamsConfig { constructor() { super(...arguments); /** @param distortion */ this.distortion = ParamConfig.FLOAT(DEFAULTS.distortion, { range: [0, 1], rangeLocked: [true, true], ...effectParamsOptions(paramCallback) }); /** @param oversample */ this.oversample = ParamConfig.INTEGER(OVER_SAMPLE_TYPES.indexOf(DEFAULTS.oversample), { menu: { entries: OVER_SAMPLE_TYPES.map((name, value) => ({ name, value })) } }); } } const ParamsConfig = new DistortionAudioParamsConfig(); export class DistortionAudioNode extends TypedAudioNode { constructor() { super(...arguments); this.paramsConfig = ParamsConfig; } static type() { return "distortion"; } initializeNode() { this.io.inputs.setCount(1); } cook(inputContents) { const audioBuilder = inputContents[0]; const effect = this._effect(); const inputNode = audioBuilder.audioNode(); if (inputNode) { inputNode.connect(effect); } audioBuilder.setAudioNode(effect); this.setAudioBuilder(audioBuilder); } _effect() { return this.__effect__ = this.__effect__ || this._createEffect(); } _createEffect() { return new Distortion({ distortion: this.pv.distortion, oversample: OVER_SAMPLE_TYPES[this.pv.oversample] }); } static PARAM_CALLBACK_updateEffect(node) { node._updateEffect(); } _updateEffect() { const effect = this._effect(); effect.distortion = this.pv.distortion; effect.oversample = OVER_SAMPLE_TYPES[this.pv.oversample]; } }