the place of strings to strum (2/2)
Usually, when I'm done with a project, I leave it alone (with a few exceptions). But when I was told that the lines looked like strings on a pegboard, my friend convinced me to make it musical.
Actually, he challenged me to build an audio component by the time he got home from our hangout. (Un)Fortunately, there were subway delays, so I had plenty of time to hook it up to the audio.js “library” I made for previous projects.
First, we construct a list of available instruments. Some come from my "library," and users who've built their own on toneMaker will see custom sounds as well. Next, we pick one:
This function attempts to pull all the JSON data tied to a specific name, then build an actual audioContext API instrument from those parameters.
I was already keeping track of the cursor's position for two reasons: to find the closest dot when you start or stop drawing a line, and to find the closest lines - above - when you're erasing them. (Note that the callback function is a stand-in for erasing... or whatever else we want to do!)
Since we're plucking strings, (not pegs,) the latter is more relevant. We also need to figure out how long the string is, using the Pythagorean Theorem, since wavelength is inversely proportional to frequency.
That's right: physics time. v = fλ, where v is the speed of sound, f is the frequency (ie, the note) and λ (lambda) is the wavelength (ie, the string length). Of course, this is cyberspace, so I also multiplied by an arbitrary constant to get the pitches in a comfortable range. ...How does it sound to you?
tl;dr: callbacks make code more flexible - they're not just for async stuff; small, clean functions with obvious inputs and outputs make for more reusable code; in addition to his famous triangles Theorem, Pythagoras also determined the ratios of intervals, as seen in the first screenshot
project: gridDrawer














