UNPKG

genish.js

Version:
74 lines (54 loc) 2.14 kB
<html> <head> <title>genish.js ScriptProcessor FM demo</title> <script src='http://www.charlie-roberts.com/genish/dist/gen.lib.js'></script> </head> <body></body> <script> let callback let tester = { createContext() { this.ctx = new ( AudioContext || webkitAudioContext )() gen.samplerate = this.ctx.sampleRate return this }, createScriptProcessor() { this.node = this.ctx.createScriptProcessor( 1024, 0, 1 ), this.clearFunction = function() { return 0 }, this.callback = this.clearFunction this.node.onaudioprocess = function( audioProcessingEvent ) { let outputBuffer = audioProcessingEvent.outputBuffer; let outChannel = outputBuffer.getChannelData( 0 ) for( let sample = 0; sample < outChannel.length; sample++ ) { // "callback" will be the function created by genish.js outChannel[ sample ] = callback() } } this.node.connect( this.ctx.destination, 0, 0 ) return this } } // put genish functions in global namespace... // you certainly don't have to do this! If you don't, every genish.js // ugen needs to reference the genish object (so, genish.cycle, genish.mul etc.) genish.export( window ) let baseFrequency = 80, c2m = 1.4, index = .95 // create our oscillator for modulation let modulator = cycle( mul( baseFrequency, c2m ) ) // create an envelope lasting eight seconds let env = ad( 44, gen.samplerate * 8 ) // scale amplitude based on index value, re-assign modulator = mul( modulator, mul( baseFrequency, mul( index, env ) ) ) // create carrier oscillator and modulate frequency let carrier = cycle( add( baseFrequency, modulator ) ) // create our callback function and assign it 44100 blocks (Float32s) of memory. // fwiw cycle takes about 1024 for its wavetable, which is shared between instances. // The final "true" argument prints the results to the developers console. callback = gen.createCallback( mul( carrier, env ), 44100, true ) // now that our callback is created, generate our scriptprocessor node // and connect it... tester.createContext().createScriptProcessor() </script> </html>