A Platform for Algorithmic Composition on p5.js
by Chan Jun Shern
Google Summer of Code 2018
mentored by Jason Sigal
This summer was the Processing Foundation’s seventh year participating in Google Summer of Code. We received 112 applications, a significant increase from previous years, and were able to offer 16 positions. Over the next few weeks, we’ll be posting articles written by some of the GSoC students, explaining their projects in detail. The series will conclude with a wrap-up post of all the work done by this year’s cohort.
As a person with equal parts enthusiasm for music and code, I’ve always been intrigued by the idea of using technology to enhance and propel our experiences with music. So, for my 2018 Google Summer of Code project, I chose to work on algorithmic music composition with the p5.sound library.
p5.sound is an add-on library that equips p5.js with the ability to do all manner of audio-related tasks, from playing a sound file to applying audio effects directly on the browser. At the start of summer 2018, when I began my project, the p5.sound library provided basic support for synthesizing sounds at different frequencies, and gave access to the Web Audio clock, which allows for accurate audio-scheduling. These features provided a solid foundation for working with generative music, but there was still a lot of experimenting and fine-tuning to do before the library was ready for general composition use.
There were many questions to consider about how a composition program might look in the context of a p5.js sketch: How do we represent musical qualities like pitch and velocity in code? What about timing information? How do we write a program which handles composition tasks and visual animation simultaneously, and how do we make sure both tasks can interact and sync with one another? Most importantly, how do we make all of this simple and intuitive to use?
These were tough questions to answer well, but thankfully I had an amazing mentor, Jason Sigal, to help me. Jason created and maintains the p5.sound library, and has been part of GSoC in the past, as both a mentor and participant. He was able to give me a lot of useful advice on both the technical and non-technical side of things.
Contributions
Throughout the summer, Jason and I were able to produce eight new instructive examples related to algorithmic composition, starting from basics like how to play a note, to more advanced topics like demonstrating composition algorithms. These examples currently live on the p5.sound examples page, and are soon on their way to joining the main examples on the p5.js website.
Interestingly, we found that the more we worked on the examples and tried to make them sound good, the more we had to hand-engineer ideas from musical theory into the code. At the same time, we could never really know what results we’d get when we put new rules into the system. This was challenging yet exciting at the same time, and suggests that perhaps the role of algorithms in music will never be to replace humans entirely, but to facilitate new ideas and give us new ways to be creative.
In the process of building our examples, we also ended up fixing a number of bugs and adding better documentation for the library, so p5.sound is now in better shape than ever!
You can find more specifics on the contributions made during this project here.
A Tutorial On Algorithmic Music Composition
As I worked on the project, I realized that I might be able to use the examples we had created to put together a useful resource for others who want to work on generative music. With Jason’s guidance, I put together an interactive and explorable web tutorial, which encompasses all of the examples we built and the lessons we learned along the way.
Please check out the tutorial here!
Some features of the tutorial:
- More than 10 interactive demos running p5.js sketches,
- Sketches enabled and disabled according to scroll events using scrollMonitor,
- Pretty and readable code snippets using highlight.js
Final Thoughts
All in all, it has been a highly satisfying summer, and I can now confidently recommend p5.sound as a capable and reliable library for developing algorithmic music. The examples and final tutorial show these capabilities quite well, and hopefully the work done in this project will enable and inspire many users to create their own algorithmic music applications!