Coding in the music classroom

Gawain Hewitt
Friday, March 1, 2024

Anything is possible when you code your own music. Artist and composer Gawain Hewitt explores coding options that work for teachers and pupils, opening up new creative avenues.

Adobe Stock / Monsitj

What is music technology for?

Music technology can be defined as making music using machines and the sounds they make. In recent years, tools such as Apple Loops, presets and extensive sample libraries have given us access to a bewildering array of premade sounds and building blocks. However, as the number of easy-to-access products and options available continues to expand, are we in danger of teaching our young people how to consume rather than how to create music?

Computer music promised a new dawn for creativity and music, but it increasingly feels like we are being sold the next ‘hot new thing’, whether that is a keyboard, an app or a software subscription. Are we being encouraged to paint with numbers using someone else's ideas? If so, how do we equip students to begin a creative and engaged relationship with music technology that can be empowered and lifelong, leading them to a path of discovery?

A cellist, for example, develops a relationship with an instrument that takes years to master and possibly decades more. If we want music technology to offer a similar richness, then we have to get our hands dirty and play with the inner workings of content creation and computer coding.

A different way to interact

Much of the electronic musical world is made of code. In fact, we can live our whole lives never seeing and understanding what is happening when we play an instrument on our computer or smartphone. Playing with the underlying code can facilitate a deeper understanding of how the sonics of electronic music are made in today's world, taking away the mystery and opening up musical possibilities.

Furthermore, investigating the places where the sound is formed and defined in this way allows for a practical experience of being a creator of technology rather than a consumer of creativity. In a world full of phones, automation and voice-activated technology, it is powerful to experience how a machine does what we tell it, with computer code really just a series of simple, clear instructions written by a human.

Code is where the form and tone of our music technologies are set. This informs what we can do with them and provides the freedom to create what you want. Being able to invent your own musical instrument means you can be very specific to your own likes and needs.

Getting started

While coding can look difficult and intimidating, you are essentially telling the computer what to do in very small and clear steps. There has been a significant investment in teaching coding over the past 15 years, and there has never been a better time to have a go, with multiple educational resources designed to make coding accessible.

Scratch presents code as building blocks, allowing for a playful experience with the principles of coding, and P5.js is a version of JavaScript that offers beginners the opportunity to animate, draw and make sounds. For a coding experience that is focused only on music, Sonic Pi is a language full of sounds with excellent learning resources built in. Following the tutorial, you will quickly be looping drums and making sounds with the large number of instruments available.

In the classroom, a good place to start with coding is by making a computer play a specific sound using the built-in recording features in Scratch or following the first steps in Sonic Pi, for example. As with any new instrument, the process may feel slow and basic to start with, but with patience and perseverance you will quickly move to more interesting elements.

Expanding your options

Like learning any foreign language, you will need to grasp some new terms for musical concepts, as coding language can be used in place of musical speak. In Sonic Pi, for example, if you want the music to pause for a set time, you have to tell it to ‘sleep’ and, using a number, for how long – in contrast to the traditional terminology of ‘rest’. In general when coding, pitch may be referred to by name or using MIDI numbers; 60 is middle C. Changing these numbers and seeing what impact it has is a good way to start to experience music from a coder's point of view and coding's creative potential.

Code can allow you to organise your ideas into a type of recipe that can be used repeatedly – called functions. For example, you might decide that you want to play a sound of 440hz that lasts for 500 milliseconds several times. A function allows you to only write the code once but use it as often as you need.

Surprisingly, you can also code music in real time – a practice known as live coding. Code is written in the moment and executed straight away, allowing for improvisation and live performance. There is a whole culture of artists and musicians who create and perform using this technique, with events they perform at sometimes referred to as ‘algoraves’. Sam Aaron, the creator of Sonic Pi, has some excellent live videos on YouTube, making music with the code visible and shared.

While electronic musical instruments such as keyboards and drum machines all have code at their heart, platforms such as Arduino allow students to use code to make their own instruments. Combining Scratch with the Makey Makey electronics kit [outlined in a previous Music Teacher article, ‘Embracing music tech in Early Years’] would be a great place to start when using hardware and code together. Ambitious projects can be made with just these elements, allowing craft, code and music to work together.

Coding your own musical instrument

Here are two ways to make a simple musical instrument using two popular coding languages (in a web browser):

A building blocks approach with Scratch

Scratch uses blocks, like Lego, to allow you to create code by moving elements around. We can use the ‘play sound’ function to record and play our own sounds when a key is pressed. Follow the ‘Create’ tab to follow this guide.

  • Drag ‘when space key is pressed’ in Events (yellow) onto the main editor.
  • Connect ‘play sound meow until done’ in Sounds (light purple/mauve) to the bottom of the first piece.
  • Now press the spacebar to test out what you have created.
  • Next, use the dropdown menu next to ‘Meow’ to record your own sound.

You can then create challenges to set different keys to different sounds, or to change the pitch randomly.

Coding with words using p5.js


p5.js is a version of JavaScript made for artists. It makes fun things like animation and sound relatively easy to do. You will need to use the editor on p5.js to follow this guide. Click on ‘Start creating with the p5 Editor!’ and you will be taken to a page to start coding. In this example, we are making a synth that plays a random note when you press ‘click me’ with your mouse. Copy out the text very carefully, paying attention to capital letters and punctuation – as even a small mistake may stop the code from working. Some other pointers:

  • The text after ‘//’ is not needed for the code to work but explains what each section is doing.
  • You will need to click the ‘run’ button (a white triangle in a pink circle) to start the code. You can then set challenges to change the numbers in the code, to add more notes and to change the duration.

There is also a working version of this code available for you to edit and play with.

All are welcome

Learning to code music broadens the possibilities for creative music technology, opening up the space for different learning styles and allowing us all to have a closer relationship with the tools that we make music with. It also opens up new potential career pathways for young people, demonstrating the creativity in both music and coding.

Furthermore, if we invite musicians into the world of coding, and coders into the music space, we encourage more collaboration, choice and agency. The most creative use of code is seldom written by the best coders. And a good idea and open mind can achieve astonishing things, even turning more young people into creators rather than consumers