Yes! You heard right, we can compose music by using programming. It sounds strange but we can code music by using Sonic Pi application. Sonic Pi is an excellent example of combining creativity and programming, and it encourages people to use both sides of their brain. It is a Raspberry Pi-based music synthesizer and sequencer but now you can use it for windows and Mac also. Here you don’t need any musical instruments or keyboard to play music but you will play music by writing code. If you don't like music, it's a fun way to generate some unusual noises. And if you like music, your imagination is your limit. In this tutorial, we will introduce you to the Sonic Pi application and at end of this tutorial, we will compose a Happy Birthday tune by using Sonic Pi.
Introduction to Sonic Pi
Sonic Pi is a code-based music creation and performance tool and it allows you to create and perform music using code. Sonic Pi is an open-source programming environment that uses the process of making new sounds to explore and teach programming principles. It is developed by Sam Aaron at the University of Cambridge Computer Laboratory to produce a free live coding synth for everyone. Sonic Pi may be used to program, write, and play in a variety of classical and current styles, from Canons to Dubstep. It can be used for live coding music. Some musicians have chosen to compose with computer code rather than traditional acoustic and electronic instruments. This is known as "live coding," in which a DJ takes the stage with a laptop, opens a coding interface, and creates songs in real time.
Launching Sonic Pi on Raspberry Pi
Sonic Pi supports many operating systems including Windows, macOS, Linux. It comes pre-installed in Raspberry Pi with Raspbian OS. You'll find it in the programming menu if you already have a Raspbian-based setup. If your Pi OS doesn’t have Sonic Pi installed on it, then it can be downloaded and installed from the given link.
To launch Sonic Pi on raspberry Pi, go to Start Menu and then click on the Programming option and select Sonic Pi. Now you will see the Sonic Pi window on the Raspberry Pi desktop.
Programming Sonic Pi to Generate Happy Birthday Tune
You can code any music using this Sonic Pi application, here we are going to code Sonic Pi to generate happy birthday music. As a beginner, we have no idea about any musical instruments and I am assuming that you have no knowledge about music so firstly you have to know about keyboard nodes for happy birthday music. In the below image, you can see the keyboard nodes required for generating a happy birthday tune.
Open the Sonic Pi application and the below window will open on your desktop screen. The Sonic Pi interface consists of three major windows. The Programming Panel, which is the largest, is where you write your code. A log panel is also available, which displays information about your application as it runs. When you click the help button at the top of the window, the third panel with help documentation displays along the bottom. This includes basic information for the programming language (for sonic pi), as well as many synth sounds, samples, and more. There is also a plethora of ready-to-use Sonic Pi examples available!
You write your code in the code editor to make music. Basically, it's your instrument. Copy below lines of code and paste in Code editor.
x = 0.25 use_synth :piano play :g4 sleep x play :g4 sleep x play :a4 sleep x * 2 play :g4 sleep x * 2 play :c5 sleep x * 2 play :b4 sleep x * 4 play :g4 sleep x play :g4 sleep x play :a4 sleep x * 2 play :g4 sleep x * 2 play :d5 sleep x * 2 play :c5 sleep x * 4 play :g4 sleep x play :g4 sleep x play :g5 sleep x * 2 play :e5 sleep x * 2 play :c5 sleep x * 2 play :b4 sleep x * 2 play :a4 sleep x * 3 play :f5 sleep x play :f5 sleep x play :e5 sleep x * 2 play :c5 sleep x * 2 play :d5 sleep x * 2 play :c5 sleep x * 6
Now, click on the run button at the top left corner of the Sonic Pi window and your music will start playing.
You can also see your music with the execution of code in scope viewer. The scope viewer displays real-time visual feedback, such as wavelengths, as they are formed.
So, like this, you can compose any music by using Sonic Pi on Raspberry Pi. Hope you enjoyed the project and learned something useful. If you have any questions, please leave them in the comment section below or use our forum to start a discussion on this.