PencilCoder

Teacher's Guide: Code Music!

Overview

This lesson introduces the musical features embedded in Pencil Code. However, the goal is not learning to code music, per se. Rather, the lesson serves several curricular purposes. First, it provides an excellent context in which to discuss the concept of representation in coding. Second, it provides continued reinforcement of coding concepts introduced in the curriculum thus far, such as dot notation, attention to syntax, and use of strings. Pehaps most importantly, it lays some groundwork on which to build later. Musical features will be revisited again after the formal introduction of the object data type. The play function and Piano constructor both accept object arguments and provide a rich environment in which to expand understanding of that core data type.

More about the lesson

This lesson provides provides relatively more detail in the "Math and Computer Concepts" section than most, and only presents a few, relatively basic activities. This is partly because there are many features to introduce, but also because the lesson aims to let students explore on their own and get creative.

When coding their ABC-notation songs as strings, encourage students to use double quotes. Sets of single quotes are valid for creating strings, but their use in this context can lead to conflicts with the symbol for octaves: i.e., "C''" works but 'C''' will not.

The lesson encourages students to consider issues of readability when encoding music, noting that inserting spaces between notes is a useful step in this direction. Line breaks within strings provide another means to make a song written in ABC notation more readable. Thankfully, CoffeeScript is very flexible with string literals, i.e., representations of strings using quotes such as "this is a string literal". Unlike many languages, CoffeeScript permits including line breaks within regular quotes without the use of other special characters. As a result, the following one-liner,

Multiline quotes

can equivalently be coded with spaces and line breaks, making it much easier for humnas to process:

Multiline quotes

Notes to activities

The first two activities are designed to engage students with a mix of ABC-notation features prior to getting immersed in a larger composition. The subsequent activities are intended for further exploration and creativity.

As an alternative to coding music of their own, many students enjoy translating an existing piece of sheet music into ABC notation for use in Pencil Code. Including the term "letter notes" in web searches can help students locate music represented in an alternative, more rudimentary musical representation than Pencil Code, as illustrated below. Music written with "letter notes" can simplify the translation process, especially for students less familiar with sheet music.

Letter Music

Duet points to the Pencil Code Gym activities for music. Students exploring there will likely encounter some advanced material. Perhaps share with them that we will return to these features in a later music lesson (Musical Objects Interlude!).

Additional activities

  • Code a HappyBirthday program that adds images and other features to make the viewing experience more celebratory and festive.
  • PlayingHand: find an image of a hand and make it look like it is playing the keys. You will have to experiment to get the timing and the movements just right.
  • Rounds: If you didn't code a program using rounds as part of the Duet activity, now's your chance! For inspiration, check out this script on the Pencil Code Gym site. An easy way to get started is to copy that code, and modify it so that it has three pianos playing rounds, and that it also repeats twice.
  • A full-sized piano has 88 keys. Make a HugePiano program that instantiates a piano with even more keys than that. The "extra" notes get added on the left side of the keyboard (i.e, more low notes). Do the sounds produced by these keys match what you expect?

Beyond the lesson

There is an abundance of materials about music on the Pencil Code site. A great starting place is the "Pencil Code Gym", which can be reached from the main page by clicking on the Jam heading. The Gym provides a wide range of examples, which range from fairly simple, such as Make Chords, to scripts that make use of advanced sound properties via object notation, as in Rhythm. Clearly, some of the features illustrated in the gym surpass student knowledge at this point. However, they will get to work with much of this material after the introduction of objects, with the Musical Objects Interlude! lesson.

In addition to the examples in the Gym, the Pencil Code reference materials for musical functions and features are very informative. Links to reference pages for functions used in the Jam illustrations can be found below the coding interface on the Jam page. Additionally, a particularly useful page for beginners is the reference for the play function. This not only discusses the basics, but provides links to more advanced features. For example, from the play, one can navigate to a page that enables to user to use code to set the key signature for a song.

ABC Notation

The shorthand representation for music notation used in Pencil Code is ABC Notation. There are many more features than what is described in the lesson. For example, your students might find these useful:

stacatto
staccato notes are played with a dot before the note, as in ".C" or ".^F"
note duraction
There is some flexibility for writing notes of different lengths. Formally, a note of length quarter of the default is "C1/4", but we can write it "C/4" (the latter being what is shown in the lesson). Triplets can thus be written by weighting each note 2/3, e.g., "C2/3 C2/3 C2/3".
triplets
A shortcut for triplets is to preceed three notes by (3, as in "(3 C C C".

You can read more about ABC-Notation basics here or explore more advanced features here. Another useful and accessible resource is this ABC notation tutorial.

What can go wrong

A common mistake is to forget to use dot-notation, and thus unwittingly have the default turtle play a song rather than the piano sprite. If keys are not being animated during a song, this is the likely reason.

A second common source of confusion is mistakenly using familiar music symbols, such as # for sharp, rather than the ABC-Notation equivalent (^ in this case). As always, such seemingly small syntactic details are critical when programming.

Pedagogy

Data Representation

How data is represented is one of the key concepts of computer science and it is important to draw students attention to issues of data representation more generally. The shorthand representation for music notation used in Pencil Code is ABC notation. It is one of many possible representations for music, ranging from "letter music" (as illustrated above) to sheet music.

Multiline quotes

A comparison of these three representations of music—letter music, ABC notation, and sheet music—makes clear that each has its own merits as well as limitations. Letter music is very easy to read, but does not include much depth of detail. Sheet music, in contrast, contains much more detail, but requires specialized training to read. And while it is easy enough to store sheet music on a computer (i.e., as a pdf), sheet music does not represent music in a way that a computer program can easily process and convert into sound.

ABC Notation solves this problem. By representing notes and other music features using strings of regular keyboard characters, it can be parsed by the computer and converted into sound in a straightforward way. ABC Notation is much more nuanced than letter music, capable of representing any aspect of music that can appear in sheet music. And though it is more cumbersome for humans to work with than sheet music, it is a reasonable compromise.