UNPKG

gibberish-dsp

Version:

Gibberish is designed to be an optimized API for audio synthesis using per-sample techniques.

53 lines (41 loc) 1.81 kB
let g = require( 'genish.js' ), instrument = require( './instrument.js' ) module.exports = function( Gibberish ) { let Hat = argumentProps => { let hat = Object.create( instrument ), tune = g.in( 'tune' ), scaledTune = g.memo( g.add( .4, tune ) ), decay = g.in( 'decay' ), gain = g.in( 'gain' ), loudness = g.in( 'loudness' ), triggerLoudness = g.in( '__triggerLoudness' ) let props = Object.assign( {}, Hat.defaults, argumentProps ) let baseFreq = g.mul( 325, scaledTune ), // range of 162.5 - 487.5 bpfCutoff = g.mul( g.param( 'bpfc', 7000 ), scaledTune ), hpfCutoff = g.mul( g.param( 'hpfc', 11000 ), scaledTune ), s1 = Gibberish.oscillators.factory( 'square', baseFreq, false ), s2 = Gibberish.oscillators.factory( 'square', g.mul( baseFreq,1.4471 ) ), s3 = Gibberish.oscillators.factory( 'square', g.mul( baseFreq,1.6170 ) ), s4 = Gibberish.oscillators.factory( 'square', g.mul( baseFreq,1.9265 ) ), s5 = Gibberish.oscillators.factory( 'square', g.mul( baseFreq,2.5028 ) ), s6 = Gibberish.oscillators.factory( 'square', g.mul( baseFreq,2.6637 ) ), sum = g.add( s1,s2,s3,s4,s5,s6 ), eg = g.decay( g.mul( decay, g.gen.samplerate * 2 ), { initValue:0 }), bpf = g.svf( sum, bpfCutoff, .5, 2, false ), envBpf = g.mul( bpf, eg ), hpf = g.filter24( envBpf, 0, hpfCutoff, 0 ), out = g.mul( hpf, g.mul( gain, g.mul( loudness, triggerLoudness ) ) ) hat.env = eg hat.isStereo = false const __hat = Gibberish.factory( hat, out, ['instruments','hat'], props ) return __hat } Hat.defaults = { gain: .5, tune: .6, decay:.1, loudness:1, __triggerLoudness:1 } return Hat }