Welcome back! Thanks for coming back! I know I promised we’d start off with turning
some numbers into music, but I wanted to take a quick detour back into generating those
Step 2, Part Deux
By the end of the last post I’d generated a set of graph coordinates for the letter A,
and ending up with a JSON array that looked, in part, like this:
I said that we had to do this 25 more times, which is true, but after about 3 times, I noticed a few things.
First, it’s really kind of annoying to open the Processing file, change the letter "a" to "b" every time, rerun it,
realize I missed one, and then have to go back and redo "a" because I overwrote some of the data.
Second, because of the not-particularly-scientific way I created the individual letter chart images, they’re not all
the same height, meaning I need to find the dimensions of the image before I start.
Using imagemagick this is at least an easy task
And then I just need to open the Processing code, update the sketch dimensions, double check that I’ve
changed all instances of the letter character to the correct one, and…
Wait a minute! A series of easily performable but repetitive tasks? This sounds like a job for
I’ve been getting really into Elixir, so let’s use that.
Fortunately for us, Processing now includes a command line option, processing-java
that takes a path to our sketch’s directory, a flag to --run or --build, and any
number of arguments.
In Elixir, we can sequence our calls to identify and processing-java to do just that:
Then, in our processing sketch, we just need to make a couple small changes:
Next we add an Elixir function to iterate through the alphabet
And let’s see how this goes:
That time is in microseconds, so we’re looking at 71 seconds. Not bad, but since
we’re using Elixir, we might as well use one of its great features: spawning
processes to do work in different, independent threads.
A few changes to the Elixir code later
and we run the code again
33 seconds! Much better. As you can see, we’re not concerned about the order the
processes complete in, since they’re entirely independent of each other.
And so, 33 seconds later, we’ve got 26 JSON files, each with a set of
coordinates. Now we can move on to the music!
Thanks for reading! If you liked this post, and want to know when the next one
is coming out, follow me on Twitter (link below)!