genish.js
Version:
64 lines (51 loc) • 2.07 kB
HTML
<!--
This file demonstrates how to create an audiocontext and scriptprocessor node to use with genish.js
that plays a sine wave.
-->
<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>
// globalize genish.js ugens
genish.export( window )
let tester = {
createContext() {
this.ctx = new ( AudioContext || webkitAudioContext )()
// IMPORTANT: you must tell genish your sample rate once the
// audiocontext has been made and it is known. Otherwise 44.1 KHz
// is assumed.
gen.samplerate = this.ctx.sampleRate
return this
},
createScriptProcessor() {
// create a mono scriptprocessor node
this.node = this.ctx.createScriptProcessor( 1024, 0, 1 )
// create an audio callback using cycle, which outputs a sinewave
// create our callback function and assign it 1030 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.
let callback = gen.createCallback( cycle(440), 1026, true )
// define the audio callback for our scriptprocessor node
this.node.onaudioprocess = function( audioProcessingEvent ) {
// get the typed Float32Array used for output
let outputBuffer = audioProcessingEvent.outputBuffer,
outChannel = outputBuffer.getChannelData( 0 )
// fill our output array with calls to the callback function
// created by genish.js
for( let sample = 0; sample < outChannel.length; sample++ ) {
// "callback" will be the function created by genish.js
outChannel[ sample ] = callback()
}
}
// connect our scriptprocessor node to our master output
this.node.connect( this.ctx.destination, 0, 0 )
return this
}
}
// generate our scriptprocessor node and connect it...
tester.createContext().createScriptProcessor()
</script>
</html>