gibberish-dsp
Version:
Gibberish is designed to be an optimized API for audio synthesis using per-sample techniques.
112 lines (88 loc) • 3.37 kB
JavaScript
/*
* This files creates a factory generating polysynth constructors.
*/
const g = require( 'genish.js' )
const __proxy = require( '../workletProxy.js' )
module.exports = function( Gibberish ) {
const proxy = __proxy( Gibberish )
const TemplateFactory = ( ugen, propertyList, _envCheck ) => {
const Template = props => {
const properties = Object.assign( {}, { isStereo:true, maxVoices:4 }, props )
//const synth = properties.isStereo === true ? Object.create( stereoProto ) : Object.create( monoProto )
const synth = properties.isStereo === true ? Gibberish.Bus2({ __useProxy__:false }) : Gibberish.Bus({ __useProxy__:false })
Object.assign(
synth,
{
maxVoices: properties.maxVoices,
voiceCount: 0,
envCheck: _envCheck,
dirty: true,
ugenName: 'poly' + ugen.name + '_' + synth.id + '_' + ( properties.isStereo ? 2 : 1 ),
properties
},
Gibberish.mixins.polyinstrument
)
properties.panVoices = true//false//properties.isStereo
synth.callback.ugenName = synth.ugenName
const storedId = properties.id
if( properties.id !== undefined ) delete properties.id
const voices = []
for( let i = 0; i < synth.maxVoices; i++ ) {
properties.id = synth.id +'_'+i
voices[i] = ugen( properties )
if( Gibberish.mode === 'processor' )
voices[i].callback.ugenName = voices[i].ugenName
voices[i].isConnected = false
//synth.__voices[i] = proxy( ['instruments', ugen.name], properties, synth.voices[i] )
}
let _propertyList
if( properties.isStereo === false ) {
_propertyList = propertyList.slice( 0 )
const idx = _propertyList.indexOf( 'pan' )
if( idx > -1 ) _propertyList.splice( idx, 1 )
}
properties.id = storedId
TemplateFactory.setupProperties( synth, ugen, properties.isStereo ? propertyList : _propertyList )
const p = proxy( ['instruments', 'Poly'+ugen.name], properties, synth )
// proxy workaround nightmare... if we include the voices when we create
// the proxy, they wind up being strangely unaddressable. perhaps they
// are being overwritting in the Processor.ugens map object?
// manually adding each one seems to work around the problem
if( Gibberish.mode === 'worklet' ) {
p.voices = []
let count = 0
for( let v of voices ) {
Gibberish.worklet.port.postMessage({
address: 'addObjectToProperty',
object: synth.id,
name:'voices',
key:count,
value:v.id
})
p.voices[ count ] = v
count++
}
}
return p
}
return Template
}
TemplateFactory.setupProperties = function( synth, ugen, props ) {
for( let property of props ) {
if( property === 'pan' || property === 'id' ) continue
Object.defineProperty( synth, property, {
configurable:true,
get() {
return synth.properties[ property ] || ugen.defaults[ property ]
},
set( v ) {
synth.properties[ property ] = v
for( let child of synth.voices ) {
child[ property ] = v
}
}
})
}
}
return TemplateFactory
}