I use both the p5js online editor and Observable in my teaching (though I’m stronger at the former than the latter). I wanted to show the students how one of my examples—a simple sound visualizer—built in the p5js online editor could translate to Observable.
Following Tom MacWright’s p5js notebook, I have basic p5js examples working. However, I can’t seem to figure out how to import the p5js sound library. See link: Untitled / Jon E. Froehlich | Observable
I’ve tried both of these approaches but neither work:
Gotcha. Thanks. I really appreciate your time and help. I’ll keep digging but will focus on other activities for the students that are more straightforward for now.
Hey @trebor and @jonfroehlich thanks for linking me into this thread! As you found I have made a few notebooks and variations on using p5 and the p5 sound extension in the context of observablehq.
The latest version you’ve linked is indeed a bit more complex, as I use mutable cells to break apart the one-way data flow in observable notebooks a bit to help debug what is going on along the way (as you can inspect the state variables instead of trying to console log them).
You can look at this older sound visualizer I made which is a little simpler in that it sticks closer to the p5 instance mode approach with one-way data flow, if that is easier to follow:
The newer one you linked to has a bit of a better approach for the actual sound visualization, as I’ve since discovered the use of Mel and bark scaling and a few other things I’d like to mess around with. The problem with my original approach was with too many “magic” numbers for tweaking the visualization and scaling, and I made several other variations as well that are since deleted.
I have also messed around with a few different approaches for using p5 in a notebook. The current approach is the best outside of perhaps using “import with” syntax for a singleton p5 sketch that is more “observablehq-ified” but it can be hard to follow. If you’re coming from the p5 world and don’t want to go all-in on observable hq then the linked visualizer above should be a little more strait forward.
As I mentioned, with the newer visualizer I’m using some mutable cells for the my state (which sort of act as global variables) which enable a “soft” reference to those cells, such that the cell is not added as a dependency and doesn’t re-execute when the mutable cell value changes. You can choose to make such a reference dropping the mutable keyword as desired. This helps debugging because I can inspect the the data from a specific frame while the sketch is running and that is “frozen in time” to have the state “unrolled” in that way. It also allows me to add more “observablehq idiomatic” interactions over time, though they don’t really like mutable cell feature lol.
Newer visualizer:
@trebor@jonfroehlich would be great to chat more about the audio visualization if that’s your focus as this topic has become something of an obsession of mine