Workbench

Stupid Strudel Tricks

I found myself wanting to import a helper function into the Strudel REPL for reasons, and I know the thing to do would be to set up my own local custom REPL where I can load whatever I want, but I am lazy and I was also curious whether shenanigans like this would even work.

The answer is yes. I bundled my library into a script that uses an IIFE to assign all of its exports to a global variable, and a common pattern for loading JS code from a remote URL boils down to:

  • Fetch the script as a Blob
  • Make an ObjectURL from the blob
  • Create a new script element with the ObjectURL as its src
  • Add the new script element to the DOM

And it works! But I'd get an error the first time I evaluated the code, despite awaiting async code... the issue wound up being that I was trying to invoke my imported code before the newly-injected script element had finished loading.

A typical pattern for this is to add a listener to the load event on the script element, and use the callback to invoke your code, but that doesn't seem to play nicely with the Strudel REPL. So, I just resorted to an plain old while loop to wait for the function to be defined (with a timeout to prevent an infinite loop if it doesn't load for some reason)

Altogether, this is what I wound up with:

const res = await fetch('https://sound.intercrap.com/strudel/interleave-recursive.bundle.js');
const ilBlob = await res.blob();
const objURL = (window.URL ? URL : webkitURL).createObjectURL(ilBlob);
const sc = document.createElement('script');
sc.setAttribute('src', objURL);
sc.setAttribute('type', 'text/javascript');
sc.setAttribute('id', 'interleave-recursive');
document.head.appendChild(sc)
let start = new Date();
while (typeof interleaveRecursive === 'undefined') {
  if (new Date() - start > 5000) throw new Error('Couldn\'t load interleaveRecursive');
}

All of this feels klugey and ill-advised, but I had a bee in my bonnet to see if I could use FoxDot style interleaved patterns of pitch and duration in Strudel.

The answer to that is yes, sort of, but it goes against the grain in terms of how cycles work. But it raises some pretty interesting possibilities: https://strudel.cc/?zbR09cmu96Wr)


Thu Oct 24 2024 20:00:00 GMT-0400 (Eastern Daylight Time)