Skip to content

Instantly share code, notes, and snippets.

@charlieroberts
Last active August 20, 2016 15:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save charlieroberts/d18ae2206424009a1bea6f58255d1a4a to your computer and use it in GitHub Desktop.
Save charlieroberts/d18ae2206424009a1bea6f58255d1a4a to your computer and use it in GitHub Desktop.
A short demo of using genish.js in an HTML file with a scriptprocessor node.
<!--
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 Sine 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
// Assign it 1030 blocks (Float32s) of memory.
// FWIW cycle() takes about 1024 floats for its wavetable, which is shared between instances
// + 1 more for its phase accumulator.
// The final "true" argument prints the resulting function 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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment