Hi all,
Things are moving along with learning Web Csound and p5.js, but I’m still figuring out how to work with async functions and race conditions in this environment (i.e. I’m new to the web development world). Here’s the latest iteration of my project. I’ve added a waveform visualization that uses .decodeAudioData()
in a function called generate_peaks()
on line 55 of setupCsound.js.
In order to get the waveform to be drawn, I had to add this test condition on line 18 of the draw()
function in sketch.js: if (sample_list[0]) {
. When you comment out the test condition (lines 18 and 26), the waveform doesn’t get drawn and an error message says that sample_list[0]
is undefined. Is that because there’s a race condition where generate_peaks()
hasn’t yet finished generating the sample_list
before the draw function is called in sketch.js? sample_list[0]
does eventually get defined because you can explore it in the JavaScript console.
While I have a workaround in line 18 of sketch.js, I wonder if there’s a better solution that defers execution of the draw()
function until generate_peaks()
has finished generating the sample_list
. That seems like a basic async function solution, but I can’t figure out how to implement it. Any suggestions?
Thanks for your patience and guidance as I trudge through the async world!
Jason