Starting up Sound
sound
Abstract
Reliably starting up sound in p5.js
Introduction
When using sound in p5.js, often one can run into an error that states something like Audio Context not started. Use the ideas in the following p5.js sketch to reliably start and stop your sound.
Take a look at the code ( you may need to stop the sketch):
- The
setupportion simply creates a canvas, and several variables that are needed. - It also creates an
Oscillatorobject (namedosc) - In the
drawsection is just a GUI to enable audio by any user action (click/tap/keypress) - There are several brief
functionsthereafter, based onmousePressed,touchStartedandkeyPressed, all pointing to another function calledhandleGesture -
handleGestureis an async function that starts audio, checks the audio context and only then allows theOscillator(osc)to start. This is the prescribed sequence.
NoteAsynchronous Programming in JavaScript
Async code allows a program to start a long-running task (like fetching data from a file). and continue with other tasks before the first one finishes.
Async code prevents the application from freezing, which is critical for user experience.
- The final function
toggleToneis just to ramp up or ramp down theOscillator(osc). You may not always need this, but this additional control may be useful. - The processing and other user interactivity that you wish to create can be always done in the
function draw(){}section.
While this may seem elaborate, this mechanism of enabling audio -> checking context -> switching on oscillators and then controlling them can help avoid a lot of grief in coding audio projects in p5.js.
References
- W3Schools. Asynchronous Programming. https://www.w3schools.com/js/js_async.asp
