canopy icon indicating copy to clipboard operation
canopy copied to clipboard

No waveform rendered when using async calls in code

Open chrislo opened this issue 5 years ago • 5 comments

When I type the following code in Canopy:

async function fetchData() {
  const response    = await fetch('https://www.webaudioweekly.com/audio/1.wav');
  const arrayBuffer = await response.arrayBuffer();
  const audioBuffer = await context.decodeAudioData(arrayBuffer);

  return audioBuffer;
};

fetchData().then((audioBuffer) => {
  const bufferSource = context.createBufferSource();

  bufferSource.buffer = audioBuffer;
  bufferSource.connect(context.destination);

  bufferSource.start();
});

and hit the run button, no audio is played and no waveform is rendered. If I define context = new AudioContext(); in a JavaScript developer console and run the same code, I hear the audio as expected. This problem occurs in both FF65 and Chrome 72 (tested on OS X).

Discussing this with @hoch and @rtoy in the web audio slack channel, we think that the wrapping function may not work with async calls.

@rtoy suggested one approach might be to have a function that needs explicity calling from user code to start the rendering.

chrislo avatar Mar 13 '19 14:03 chrislo

🤔 could be done by hacking var footer = `` to start rendering after some manual trigger event

https://github.com/hoch/canopy/blob/b2345f83014dc42c8af991f4f534adc67c909085/docs/spiral-elements/spiral-coder/code-renderer.js#L19

haywirez avatar Jan 10 '20 13:01 haywirez

This works in my fork:

    var footer = setting.async
      ? "\ncontext.renderAsync = () => context.startRendering().then(this._handleCompletion.bind(this));\n"
      : "\ncontext.startRendering().then(this._handleCompletion.bind(this));\n";

haywirez avatar Jan 10 '20 13:01 haywirez

I'm happy to make a pull request, just wondering what approach would be preferred.

haywirez avatar Jan 10 '20 14:01 haywirez

The patch makes sense. I'll merge the PR if you can submit one. Thanks @haywirez!

hoch avatar Jan 15 '20 21:01 hoch

Still haven't got around to finalize the PR 😞 But my latest thinking is perhaps a quick check on the submitted code string (in editor text field) — if it contains a render() somewhere, then trigger the completion manually, otherwise call startRendering() at the end. Would be a better user experience as no explicit settings would be needed.

haywirez avatar Jun 09 '20 17:06 haywirez