Comparing Scratch [4] to Pencil Code [3] for teaching computing through music immediately reveals a major difference: Scratch represents notes as MIDI numbers, while Pencil Code represents notes as letters using ABC notation [1]. To someone who reads music, Pencil Code is clearly preferable because it is far easier to map, for example, a major third to C and E than 60 and 64. But to those who generate music algorithmically, ABC is not as easy as MIDI. For example, a major third in the key of D is not D and F, it is D and F#. Using MIDI values, however, the interval is always 4 semitones: 62 (D) + 4 = 66 (F#), just as before.

The Problem: Pencil Code is written in CoffeeScript [4], "a little language that compiles into JavaScript." Thus, one would think to use the String.fromChar-Code function to convert numeric MIDI to alphabetic ABC. Unfortunately, the conversion is not that straightforward, because ABC notation is based on a C scale, with A and B above G. That throws out a simple conversion. The 4th octave, the one that begins on middle C, is represented by all capital letters: C D E F G A B. For the 5th octave, apostrophes are added: C' D' E' F' G' A' B'. For the 6th octave, two apostrophes are added, and so on to the higher octaves. For the 3rd octave, the one below middle C, commas are added: C, D, E, F, G, A, B. And, as you can probably guess from the pattern, for the 2nd octave, two commas are added and so on to the lower octaves.


A Solution: If one wants both the clarity of ABC notation and the algorithmic power of MIDI values, a conversion is indeed necessary. The code below runs in Pencil Code and plays the chromatic scale from C3 to C6 by passing MIDI values 48 (C3) through 84 (C6) to a MIDItoABC function that is generalized to convert any MIDI value to its proper representation in ABC notation and play it for a specified duration.

A Note on ABC Duration Notation: Unrelated to MIDI but germane to ABC, duration strings are based on quarter notes, which have a duration of 1. A half note has a duration of 2, and a whole note a duration of 4. Going the other way, an eighth note has a duration of 1/2, designated as "/2". Similarly, a 16th note has a duration of "/4", and a 32nd note has a duration of "/8".

The code below includes a test routine to play the chromatic scale from C3 to C6 with random note durations from a half note ("2") to a 32nd note ("/8").


1. ABC Notation; Accessed 22 February 2016.

2. CoffeeScript; Accessed 22 February 2016.

3. PencilCode; Accessed 22 February 2016.

4. Scratch; Accessed 22 February 2016.


Jesse M. Heines
UMass Lowell—Computer Science
One University Avenue
Lowell, Massachusetts 01854 USA

Copyright held by author.

The Digital Library is published by the Association for Computing Machinery. Copyright © 2016 ACM, Inc.

Contents available in PDF
View Full Citation and Bibliometrics in the ACM DL.


There are no comments at this time.


To comment you must create or log in with your ACM account.