gibberish-dsp
Version:
Gibberish is designed to be an optimized API for audio synthesis using per-sample techniques.
77 lines (57 loc) • 2.27 kB
JavaScript
const g = require( 'genish.js' ),
filter = require( './filter.js' )
module.exports = function( Gibberish ) {
Gibberish.genish.svf = ( input, cutoff, Q, mode, isStereo=false, shouldConvertFreqQ=false ) => {
let d1 = g.data([0,0], 1, { meta:true }), d2 = g.data([0,0], 1, { meta:true }),
peekProps = { mode:'simple', interp:'none' }
if( shouldConvertFreqQ === true ) {
//Q = g.min( g.add(.01 , __Q), 1 )
cutoff = g.mul( g.max( .005, g.min( cutoff,.995 ) ), g.div( g.gen.samplerate, 4 ) )
}
let f1 = g.memo( g.mul( 2 * Math.PI, g.div( cutoff, g.gen.samplerate ) ) )
let oneOverQ = g.memo( g.div( 1, Q ) )
let l = g.memo( g.add( d2[0], g.mul( f1, d1[0] ) ) ),
h = g.memo( g.sub( g.sub( isStereo ? input[0] : input, l ), g.mul( Q, d1[0] ) ) ),
b = g.memo( g.add( g.mul( f1, h ), d1[0] ) ),
n = g.memo( g.add( h, l ) )
d1[0] = b
d2[0] = l
let out = g.selector( mode, l, h, b, n )
let returnValue
if( isStereo ) {
let d12 = g.data([0,0], 1, { meta:true }), d22 = g.data([0,0], 1, { meta:true })
let l2 = g.memo( g.add( d22[0], g.mul( f1, d12[0] ) ) ),
h2 = g.memo( g.sub( g.sub( input[1], l2 ), g.mul( Q, d12[0] ) ) ),
b2 = g.memo( g.add( g.mul( f1, h2 ), d12[0] ) ),
n2 = g.memo( g.add( h2, l2 ) )
d12[0] = b2
d22[0] = l2
let out2 = g.selector( mode, l2, h2, b2, n2 )
returnValue = [ out, out2 ]
}else{
returnValue = out
}
return returnValue
}
let SVF = inputProps => {
const svf = Object.create( filter )
const props = Object.assign( {}, SVF.defaults, filter.defaults, inputProps )
const isStereo = props.input.isStereo
// XXX NEEDS REFACTORING
const __out = Gibberish.factory(
svf,
//Gibberish.genish.svf( g.in('input'), g.mul( g.in('cutoff'), g.gen.samplerate / 5 ), g.sub( 1, g.in('Q') ), g.in('mode'), isStereo ),
Gibberish.genish.svf( g.in('input'), g.mul( g.in('cutoff'), g.gen.samplerate / 5 ), g.sub( 1, g.in('Q') ), g.in('mode'), isStereo, true ),
['filters','Filter12SVF'],
props
)
return __out
}
SVF.defaults = {
input:0,
Q: .65,
cutoff:.25,
mode:0
}
return SVF
}