Creating a Virtual Piano and Drum Sequencer

I remember going from Chopin’s dainty melodies in my living room to Drake’s aggressive baseline in the car, pondering about the difference between the two. For starters, I don’t think Drake and piano have ever been in the same sentence. But seriously, it’s hard to identify classical instruments—like the piano—in genres like Pop, R&B, Rap, and EDM. So how exactly do producers like DJ Khaled or Daft Punk mix those sick beats?

Enter Digital Audio Workstations—or DAWs for short (maybe before that enter computers and before that electricity). A DAW is essentially software for making music, and today we are going to try to make some simplified versions of DAW components, like a virtual piano and drum sequencer.

Pictured is an example DAW, Garageband!

If you are curious about DAWs and more generally how computers make sound, be sure to check out my other blog post “A Few Notes on DAWS” which gives some more background.

So, all we need to do to make our piano is make each of our keyboard keys correspond to a particular note. We then see if a keyboard key is pressed and then play that note. To make the sketch more visually appealing, we draw the piano keys—both the white and black—and highlight them if pressed. In only 50 lines, we have a fully functional piano—take a look!

If you’d like to run the sketch click here!

The drum machine code is just as simple. We draw a grid where each row corresponds to a different instrument (various drums). Then we detect to see if a certain instrument is selected to be played at a certain point in time (each column refers to a specific time frame). Thus, when a user presses play, we go through each time frame playing the corresponding notes. The drum machine code is only a bit longer because we render a visual timer that helps us see when each note is played. 

If you’d like to run this sketch copy the code into Processing (make sure to get the SoundCipher library).

There you have it! The basis for making some cool sounds with a few lines of code. I know you may be wondering—but how can I play Rachmaninoff’s 2nd Piano Concerto with only 10 notes? This is an important lesson for programming in general: start simple then improve. Once you have a basic outline, you can always make code more complex and add more interesting features that are important to you. 

I hope you now have a deeper appreciation for the power of programming. I know growing up, I always thought programming was only used for brute-forcing a bunch of nasty calculations, but I hope you’ve seen the elegance and practical purpose it has. It’s one of the few things that allows you to explore in a new way some of your pre-existing passions. For me for instance, it allowed me to explore my passion for music and problem solving! And don’t forget that old maxim your piano teacher told you, “Practice makes perfect,” it applies to programming too! 

[wpforms id=”2359″ description=”true”]



Leave a Reply

Your email address will not be published. Required fields are marked *