gibberish-dsp
Version:
Gibberish is designed to be an optimized API for audio synthesis using per-sample techniques.
77 lines (60 loc) • 1.84 kB
JavaScript
let g = require( 'genish.js' ),
effect = require( './effect.js' )
module.exports = function( Gibberish ) {
let BitCrusher = inputProps => {
const props = Object.assign( { bitCrusherLength: 44100 }, BitCrusher.defaults, effect.defaults, inputProps ),
bitCrusher = Object.create( effect )
let out
bitCrusher.__createGraph = function() {
let isStereo = false
if( out === undefined ) {
isStereo = typeof props.input.isStereo !== 'undefined' ? props.input.isStereo : false
}else{
isStereo = out.input.isStereo
out.isStereo = isStereo
}
let input = g.in( 'input' ),
inputGain = g.in( 'inputGain' ),
bitDepth = g.in( 'bitDepth' ),
sampleRate = g.in( 'sampleRate' ),
leftInput = isStereo ? input[ 0 ] : input,
rightInput = isStereo ? input[ 1 ] : null
let storeL = g.history(0)
let sampleReduxCounter = g.counter( sampleRate, 0, 1 )
let bitMult = g.pow( g.mul( bitDepth, 16 ), 2 )
let crushedL = g.div( g.floor( g.mul( g.mul( leftInput, inputGain ), bitMult ) ), bitMult )
let outL = g.switch(
sampleReduxCounter.wrap,
crushedL,
storeL.out
)
if( isStereo ) {
let storeR = g.history(0)
let crushedR = g.div( g.floor( g.mul( g.mul( rightInput, inputGain ), bitMult ) ), bitMult )
let outR = g.switch(
sampleReduxCounter.wrap,
crushedR,
storeL.out
)
bitCrusher.graph = [ outL, outR ]
}else{
bitCrusher.graph = outL
}
}
bitCrusher.__createGraph()
bitCrusher.__requiresRecompilation = [ 'input' ]
out = Gibberish.factory(
bitCrusher,
bitCrusher.graph,
['fx','bitCrusher'],
props
)
return out
}
BitCrusher.defaults = {
input:0,
bitDepth:.5,
sampleRate: .5
}
return BitCrusher
}