gibberish-dsp
Version:
Gibberish is designed to be an optimized API for audio synthesis using per-sample techniques.
107 lines (85 loc) • 3.04 kB
JavaScript
const g = require( 'genish.js' ),
effect = require( './effect.js' )
module.exports = function( Gibberish ) {
const allPass = Gibberish.filters.genish.AllPass
const combFilter = Gibberish.filters.genish.Comb
const tuning = {
combCount: 8,
combTuning: [ 1116, 1188, 1277, 1356, 1422, 1491, 1557, 1617 ],
allPassCount: 4,
allPassTuning: [ 225, 556, 441, 341 ],
allPassFeedback:0.5,
fixedGain: 0.015,
scaleDamping: 0.4,
scaleRoom: 0.28,
offsetRoom: 0.7,
stereoSpread: 23
}
const Freeverb = inputProps => {
const props = Object.assign( {}, effect.defaults, Freeverb.defaults, inputProps ),
reverb = Object.create( effect )
let out
reverb.__createGraph = function() {
let isStereo = false
if( out === undefined ) {
isStereo = typeof props.input.isStereo !== 'undefined' ? props.input.isStereo : false
}else{
isStereo = out.input.isStereo
}
const combsL = [], combsR = []
const input = g.in( 'input' ),
inputGain = g.in( 'inputGain' ),
wet1 = g.in( 'wet1'),
wet2 = g.in( 'wet2' ),
dry = g.in( 'dry' ),
roomSize = g.in( 'roomSize' ),
damping = g.in( 'damping' )
const __summedInput = isStereo === true ? g.add( input[0], input[1] ) : input,
summedInput = g.mul( __summedInput, inputGain ),
attenuatedInput = g.memo( g.mul( summedInput, tuning.fixedGain ) )
// create comb filters in parallel...
for( let i = 0; i < 8; i++ ) {
combsL.push(
combFilter(
attenuatedInput,
tuning.combTuning[i],
g.mul(damping,.4),
g.mul( tuning.scaleRoom + tuning.offsetRoom, roomSize )
)
)
combsR.push(
combFilter(
attenuatedInput,
tuning.combTuning[i] + tuning.stereoSpread,
g.mul(damping,.4),
g.mul( tuning.scaleRoom + tuning.offsetRoom, roomSize )
)
)
}
// ... and sum them with attenuated input, use of let is deliberate here
let outL = g.add( attenuatedInput, ...combsL )
let outR = g.add( attenuatedInput, ...combsR )
// run through allpass filters in series
for( let i = 0; i < 4; i++ ) {
outL = allPass( outL, tuning.allPassTuning[ i ] + tuning.stereoSpread )
outR = allPass( outR, tuning.allPassTuning[ i ] + tuning.stereoSpread )
}
const outputL = g.add( g.mul( outL, wet1 ), g.mul( outR, wet2 ), g.mul( isStereo === true ? input[0] : input, dry ) ),
outputR = g.add( g.mul( outR, wet1 ), g.mul( outL, wet2 ), g.mul( isStereo === true ? input[1] : input, dry ) )
reverb.graph = [ outputL, outputR ]
}
reverb.__createGraph()
reverb.__requiresRecompilation = [ 'input' ]
out = Gibberish.factory( reverb, reverb.graph, ['fx','freeverb'], props )
return out
}
Freeverb.defaults = {
input: 0,
wet1: 1,
wet2: 0,
dry: .5,
roomSize: .925,
damping: .5,
}
return Freeverb
}