gibberish-dsp
Version:
Gibberish is designed to be an optimized API for audio synthesis using per-sample techniques.
96 lines (71 loc) • 3.09 kB
JavaScript
const g = require( 'genish.js' ),
effect = require( './effect.js' )
module.exports = function( Gibberish ) {
let __Chorus = inputProps => {
const props = Object.assign({}, __Chorus.defaults, effect.defaults, inputProps )
let out
const chorus = Object.create( effect )
chorus.__createGraph = function() {
const input = g.in('input'),
inputGain = g.in( 'inputGain' ),
freq1 = g.in('slowFrequency'),
freq2 = g.in('fastFrequency'),
amp1 = g.in('slowGain'),
amp2 = g.in('fastGain')
let isStereo = false
if( out === undefined ) {
isStereo = typeof props.input.isStereo !== 'undefined' ? props.input.isStereo : false
}else{
isStereo = out.input.isStereo
out.isStereo = isStereo
}
const leftInput = isStereo ? g.mul( input[0], inputGain ) : g.mul( input, inputGain )
const win0 = g.env( 'inversewelch', 1024 ),
win120 = g.env( 'inversewelch', 1024, 0, .333 ),
win240 = g.env( 'inversewelch', 1024, 0, .666 )
const slowPhasor = g.phasor( freq1, 0, { min:0 }),
slowPeek1 = g.mul( g.peek( win0, slowPhasor ), amp1 ),
slowPeek2 = g.mul( g.peek( win120, slowPhasor ), amp1 ),
slowPeek3 = g.mul( g.peek( win240, slowPhasor ), amp1 )
const fastPhasor = g.phasor( freq2, 0, { min:0 }),
fastPeek1 = g.mul( g.peek( win0, fastPhasor ), amp2 ),
fastPeek2 = g.mul( g.peek( win120, fastPhasor ), amp2 ),
fastPeek3 = g.mul( g.peek( win240, fastPhasor ), amp2 )
let sampleRate = Gibberish.ctx.sampleRate
const ms = sampleRate / 1000
const maxDelayTime = 1000 * ms
//console.log( 'sr:', sampleRate, 'ms:', ms, 'maxDelayTime:', maxDelayTime )
const time1 = g.mul( g.add( slowPeek1, fastPeek1, 5 ), ms ),
time2 = g.mul( g.add( slowPeek2, fastPeek2, 5 ), ms ),
time3 = g.mul( g.add( slowPeek3, fastPeek3, 5 ), ms )
const delay1L = g.delay( leftInput, time1, { size:maxDelayTime }),
delay2L = g.delay( leftInput, time2, { size:maxDelayTime }),
delay3L = g.delay( leftInput, time3, { size:maxDelayTime })
const leftOutput = g.add( delay1L, delay2L, delay3L )
if( isStereo ) {
const rightInput = g.mul( input[1], inputGain )
const delay1R = g.delay(rightInput, time1, { size:maxDelayTime }),
delay2R = g.delay(rightInput, time2, { size:maxDelayTime }),
delay3R = g.delay(rightInput, time3, { size:maxDelayTime })
// flip a couple delay lines for stereo effect?
const rightOutput = g.add( delay1R, delay2L, delay3R )
chorus.graph = [ g.add( delay1L, delay2R, delay3L), rightOutput ]
}else{
chorus.graph = leftOutput
}
}
chorus.__createGraph()
chorus.__requiresRecompilation = [ 'input' ]
out = Gibberish.factory( chorus, chorus.graph, ['fx','chorus'], props )
return out
}
__Chorus.defaults = {
input:0,
slowFrequency: .18,
slowGain:3,
fastFrequency:6,
fastGain:1,
inputGain:1
}
return __Chorus
}