gibberish-dsp
Version:
Gibberish is designed to be an optimized API for audio synthesis using per-sample techniques.
90 lines (71 loc) • 2.5 kB
JavaScript
let g = require( 'genish.js' ),
ugen = require( '../ugen.js' )(),
__proxy= require( '../workletProxy.js' )
module.exports = function( Gibberish ) {
const proxy = __proxy( Gibberish )
const Bus = Object.create( ugen )
Object.assign( Bus, {
gain: {
set( v ) {
this.mul.inputs[ 1 ] = v
Gibberish.dirty( this )
},
get() {
return this.mul[ 1 ]
}
},
__addInput( input ) {
this.sum.inputs.push( input )
Gibberish.dirty( this )
},
create( _props ) {
const props = Object.assign({}, Bus.defaults, { inputs:[0] }, _props )
// MUST PREVENT PROXY
// Othherwise these binops are created in the worklet and sent
// across the thread to be instantiated, and then instantiated again
// when the bus is created in the processor thread, messing up the various
// uids involved. By preventing proxying the binops are only created
// a single time when the bus is sent across the thread.
Gibberish.preventProxy = true
const sum = Gibberish.binops.Add( ...props.inputs )
const mul = Gibberish.binops.Mul( sum, props.gain )
Gibberish.preventProxy = false
const graph = Gibberish.Panner({ input:mul, pan: props.pan })
graph.sum = sum
graph.mul = mul
graph.disconnectUgen = Bus.disconnectUgen
graph.__properties__ = props
const out = props.__useProxy__ === true ? proxy( ['Bus'], props, graph ) : graph
Object.defineProperty( out, 'gain', Bus.gain )
if( false && Gibberish.preventProxy === false && Gibberish.mode === 'worklet' ) {
const meta = {
address:'add',
name:['Bus'],
props,
id:graph.id
}
Gibberish.worklet.port.postMessage( meta )
Gibberish.worklet.port.postMessage({
address:'method',
object:graph.id,
name:'connect',
args:[]
})
}
return out
},
disconnectUgen( ugen ) {
let removeIdx = this.sum.inputs.indexOf( ugen )
if( removeIdx !== -1 ) {
this.sum.inputs.splice( removeIdx, 1 )
Gibberish.dirty( this )
}
},
// can't include inputs here as it will be sucked up by Gibber,
// instead pass during Object.assign() after defaults.
defaults: { gain:1, pan:.5, __useProxy__:true }
})
const constructor = Bus.create.bind( Bus )
constructor.defaults = Bus.defaults
return constructor
}