UNPKG

genish.js

Version:
91 lines (69 loc) 3.19 kB
const gen = require('./gen.js'), dataUgen = require('./data.js') let proto = { basename:'peek', gen() { let genName = 'gen.' + this.name, inputs = gen.getInputs( this ), out, functionBody, next, lengthIsLog2, idx idx = inputs[1] lengthIsLog2 = (Math.log2( this.data.buffer.length ) | 0) === Math.log2( this.data.buffer.length ) if( this.mode !== 'simple' ) { functionBody = ` var ${this.name}_dataIdx = ${idx}, ${this.name}_phase = ${this.mode === 'samples' ? inputs[0] : inputs[0] + ' * ' + (this.data.buffer.length) }, ${this.name}_index = ${this.name}_phase | 0,\n` if( this.boundmode === 'wrap' ) { next = lengthIsLog2 ? `( ${this.name}_index + 1 ) & (${this.data.buffer.length} - 1)` : `${this.name}_index + 1 >= ${this.data.buffer.length} ? ${this.name}_index + 1 - ${this.data.buffer.length} : ${this.name}_index + 1` }else if( this.boundmode === 'clamp' ) { next = `${this.name}_index + 1 >= ${this.data.buffer.length - 1} ? ${this.data.buffer.length - 1} : ${this.name}_index + 1` } else if( this.boundmode === 'fold' || this.boundmode === 'mirror' ) { next = `${this.name}_index + 1 >= ${this.data.buffer.length - 1} ? ${this.name}_index - ${this.data.buffer.length - 1} : ${this.name}_index + 1` }else{ next = `${this.name}_index + 1` } if( this.interp === 'linear' ) { functionBody += ` ${this.name}_frac = ${this.name}_phase - ${this.name}_index, ${this.name}_base = memory[ ${this.name}_dataIdx + ${this.name}_index ], ${this.name}_next = ${next},` if( this.boundmode === 'ignore' ) { functionBody += ` ${this.name}_out = ${this.name}_index >= ${this.data.buffer.length - 1} || ${this.name}_index < 0 ? 0 : ${this.name}_base + ${this.name}_frac * ( memory[ ${this.name}_dataIdx + ${this.name}_next ] - ${this.name}_base )\n\n` }else{ functionBody += ` ${this.name}_out = ${this.name}_base + ${this.name}_frac * ( memory[ ${this.name}_dataIdx + ${this.name}_next ] - ${this.name}_base )\n\n` } }else{ functionBody += ` ${this.name}_out = memory[ ${this.name}_dataIdx + ${this.name}_index ]\n\n` } } else { // mode is simple functionBody = `memory[ ${idx} + ${ inputs[0] } ]` return functionBody } gen.memo[ this.name ] = this.name + '_out' return [ this.name+'_out', functionBody ] }, defaults : { channels:1, mode:'phase', interp:'linear', boundmode:'wrap' } } module.exports = ( input_data, index=0, properties ) => { let ugen = Object.create( proto ) //console.log( dataUgen, gen.data ) // XXX why is dataUgen not the actual function? some type of browserify nonsense... const finalData = typeof input_data.basename === 'undefined' ? gen.lib.data( input_data ) : input_data Object.assign( ugen, { 'data': finalData, dataName: finalData.name, uid: gen.getUID(), inputs: [ index, finalData ], }, proto.defaults, properties ) ugen.name = ugen.basename + ugen.uid return ugen }