Humanities: Music

Humanities: Music

Humans enjoy music. In almost every culture, people of all ages are making and listening to music whether it is through banging hands rhythmically against a table or composing an elaborate symphony with all types of instruments.

Music is a product of each of our cultures, but are there patterns and general principles which apply to most if not all music? By applying computational thinking, you can create a song and refine it using the patterns and principles found along the way. You might want to put on headphones or turn up your speakers. Then, press Play Pencil Code UI Play Button in Pencil Code below to generate some music.

The code above is choosing notes randomly from a Pentatonic scale. The Pentatonic scale includes the notes "C", "D", "E", "G", "A" and forms of it are used in many cultures around the world. The scale used is set on line 24 and you can change this to any of the other scales you see above. For example by deleting the line notes = pentatonic and replacing it with notes = japanese

Anytime you change some code, make sure to press PlayPencil Code UI Play Button to run the code. Now, the notes you are hearing are selected at random, so it is far from something a skilled musician might create. However, there might be some things you can do to that make it more pleasant.


The beat of each note establishes the rhythm for the song. If you were singing a song and clapping 4 times every second it would sound and feel very different than if you only clapped 1 time every second. Musicians use a common language to write and discuss the beat of a note so they can understand each other. Some of these beats are the whole note, half note, quarter note, and eighth note which could be thought of a clapping 1 time each second, 2 times each second, 4 times each second, and 8 times each second respectively. Currently our simulation includes quarter notes for the melody, which is the main focus of the music and whole notes for the bass which can serve as the rhythm of the song. Try adding some more beat types in line 27 and 28.

For example you could add eighth notes to the melody by changing line 27 to melodyBeats = [quarter, eighth] and similarly with with bassBeats on line 28.

After you modify the code, press play Pencil Code UI Play Button to run the code again. Which beats did you prefer for the melody and for the bass? Was there one that did not work well at all?

Intervals and Chords

The individual notes may sound good but it turns out that when certain notes are played they too can change the feeling of a song. Two notes played together or in succession are called an interval. When three or more notes are played simultaneously, it is called a chord. and there are many possible configurations. In the simulation above every note in the scale has the same chance of being randomly selected similar to either side of a coin having an equal chance of being face up when flipped. Try changing the values in biasForNoteson line 31. Each of these numbers corresponds to a note in the scale and indicates the probability of it being selected each time a note is added to the song. These values are all less than 1 and should add up approximately to 1 (100%).

One possibility would be to change the line to read biasForNotes = [0.5, 0.0, 0.0, 0.5, 0.0, 0.0, 0.0] which would result in only the 1st and 4th notes played in the song. Try picking more interesting values and see which ones you prefer. After you modify the code, press play Pencil Code UI Play Button to run the code again.


If one is good, is two better? Many bands have more than one singer and in an orchestra, multiple instruments will play the same or similar notes to each other. This approach is referred to as harmony and it can add a fuller sound as in a choir or add novel complexity when the notes are not the same but complementary.

Add another piano to the code to introduce harmony into your music by adding harmony to the list of pianos in line 23 so it says pianos = [melody, harmony, bass]. After you modify the code, press play Pencil Code UI Play Button to run the code again. Did adding harmony make you want to change other variables like the bias or beat?

There is much more you could continue to add or modify to the code to improve the music. Feel free to play with the code as much as you would like. If you get stuck you can always refresh the page or try out this code with all of the changes applied above. In the activity above you took a song and applied decomposition to break it into components that you could modify. After testing various options you discovered some settings that might have worked better than others. These patterns are known by those trained in musical theory and are experienced when you listen to music that makes you feel a certain way. These principles are so well known that music producers can algorithmically generate a rhythm or 'hook' to increase the chances of a song being a catchy hit.

For more information about the patterns and principles found in music you could search the Internet for circle of fifths, pythagorean tuning, chord progression, algorithmic composition, media computation. Potential standards this activity could align with if used with students.