UNPKG

gibberish-dsp

Version:

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

72 lines (56 loc) 1.66 kB
const g = require( 'genish.js' ), effect = require( './effect.js' ) module.exports = function( Gibberish ) { const Tremolo = inputProps => { const props = Object.assign( {}, Tremolo.defaults, effect.defaults, inputProps ), tremolo = Object.create( effect ) let out tremolo.__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 } const input = g.in( 'input' ), inputGain = g.in( 'inputGain' ), frequency = g.in( 'frequency' ), amount = g.in( 'amount' ) const leftInput = isStereo ? g.mul( input[0], inputGain ) : g.mul( input, inputGain ) let osc if( props.shape === 'square' ) { osc = g.gt( g.phasor( frequency ), 0 ) }else if( props.shape === 'saw' ) { osc = g.gtp( g.phasor( frequency ), 0 ) }else{ osc = g.cycle( frequency ) } const mod = g.mul( osc, amount ) const left = g.sub( leftInput, g.mul( leftInput, mod ) ) if( isStereo === true ) { const rightInput = g.mul( input[1], inputGain ), right = g.mul( rightInput, mod ) tremolo.graph = [ left, right ] }else{ tremolo.graph = left } } tremolo.__createGraph() tremolo.__requiresRecompilation = [ 'input' ] out = Gibberish.factory( tremolo, tremolo.graph, ['fx','tremolo'], props ) return out } Tremolo.defaults = { input:0, frequency:2, amount: 1, shape:'sine' } return Tremolo }