Skip to content

Instantly share code, notes, and snippets.

@vishnubob
Last active November 15, 2017 22:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vishnubob/257567a3d935500b5ec900c822befe25 to your computer and use it in GitHub Desktop.
Save vishnubob/257567a3d935500b5ec900c822befe25 to your computer and use it in GitHub Desktop.
Tables and Scales: Do'h-Ray-Meh

Tables and Scales: Do'h-Ray-Meh

Digital Music systems such as MIDI use integers to represent different notes. This method of representation is known an enumeration. For this exercise, we will write a program to build a hash table that translates note values to numerical values, and an array to translate numerical values back to note values. In order to accomplish this, we will need to know two pieces of information, and let our program enumerate our two lookup tables.

  1. What are the two pieces of information our program needs to enumerate the tables?
  2. Why use a hash table for note lookups, and an array for numerical lookups?

For now, we will ignore sharps and flats, and just utilize the "white keys" of a piano when building our table. A full octave is represented by eight notes: C D E F G A B C. When specifying note values, we will use scientific pitch notation.

  1. How many full octaves can we enumerate with an 8-bit unsigned integer? how about a 128-bit unsigned integer?
  2. What is the scientific pitch notation of Middle-C?

Exercise 1

Write a function that will accept two pieces of information (see question #1) and will generate two tables: a hash table that will translate note values to an 8-bit unsigned integer and an array that maps integers back to note values. Write a test function that verifies both tables agree with each other. Lastly, write two lookup functions that translate notes to integers and integers back to notes.

Now that we have a digital representation of note values, we can use it to perform operations on music. Later on, we will construct our own musical scores with code, and load in pre-recorded MIDI and Audio data for manipulation and playback, but right now, but we are just getting started! I hope you like lamb:

Exercise 2

B A G A B B B B A A A A B D D D
B A G A B B B B A A B A G G G G

These are the first two lines in Mary had a Little Lamb, but the notes are not specified in SPN. Write a function that will accept a multi-line string and an integer representing the octave, and reprint the song in numerical values.

Bonus Exercise

How would you support semitones (sharps and flats?) Can you re-write exercise one to support semitones? You can indicate the semitone by using a small character 's' for sharp and 'b' for flat, such that the note value looks like this: Cs2 or Bb-1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment