JSFiddle - React, Tailwind, and code Playground

by taoist

JavaScript

var context = new webkitAudioContext()

/*
 * TemplateFilter with Builder pattern
 */
function FilterTemplate(builder) {
    this.context = builder._context;
    this.context.type = builder._type;    
    this.context.gain.value = builder._gainValue;    
    this.context.Q.value = builder._qValue;                  
    this.context.frequency.value = builder._frequencyValue;
}

// Builder
FilterTemplate.Builder = function () {
    this._context = context.createBiquadFilter();
    this._type = 5;    
    this._gainValue = null;    
    this._qValue = 1;                  
    this._frequencyValue = 80;
    
    this.context = function (val) { 
        this._context = val; return this; 
    };
    
    this.type = function (val) { 
        this._type = val; return this; 
    };
    
    this.gainValue = function (val) { 
        this._gainValue = val; return this; 
    };
    
    this.qValue = function (val) { 
        this._qValue = val; return this; 
    };
    
    this.frequencyValue = function (val) { 
        this._frequencyValue = val; return this; 
    };
};

// build FilterTemplates
var filter1 = new FilterTemplate(
    (new FilterTemplate.Builder()).frequencyValue(80)
);

var filter2 = new FilterTemplate(
    (new FilterTemplate.Builder()).frequencyValue(80).qValue(2)
);

console.log(filter1, filter2);