Ch. 5 – Iteration and Lists

Topics:   Iteration, Python for loop, arpeggiators, constants, list operations, range(), frange(), FX-35 Octoplus, DNA music.

Most processes in nature and in human culture involve iteration or repetition at different levels of scale.  For example, consider a tree and how it starts with a single branch (trunk), which gets split into 4-5 sub-branches at 60 (or so) degree angles, and it repeats this process until we reach the level of leaves.  Another example is the human body, which starts from a single cell, which gets subdivided repeatedly, eventually giving rise to the complex organism that is reading these words.


Play an arpeggio pattern using absolute pitches

This code sample (Ch. 5, p. 128) demonstrates how to play an arpeggio pattern using absolute pitches.  In other words, the arpeggio is fixed in a particular musical key (in this case C major).  It uses the Python for loop to iterate through a list of absolute pitches, stored in variable arpeggioPattern.

 


Play an arpeggio pattern using relative pitches

This code sample (Ch. 5, p. 131) demonstrates how to play an arpeggio pattern using relative pitches.  This makes the arpeggiator a little more flexible.  The relative pitches are added to a root note (e.g., C4) provided through user input.

 


Build a Scale Tutor

The following program (Ch. 5, p. 136) demonstrates how easy it is to build useful little tools, given the Python building blocks seen so far.  This program teaches music students the notes (pitches) in a particular scale.

 


Generate a piano roll interactively

Imagine having access to a special printer that can print large piano rolls for player pianos. This code sample (Ch 5, p. 138) demonstrates how to drive such a printer, i.e., how to construct an interactive piano roll generator.  This program also allows us to specify which MIDI instrument to use (imagine that the player pianos are fitted with a modern synthesizer module, so they can handle that).  For now, all user input is done via the keyboard (as typed text).   As is, the program accepts only one melodic line.  We leave, as an exercise, making this more flexible.  As seen below, this application presents several opportunities for iteration with lists.

 


Reverse the notes in a phrase

Since music is temporal, most musical transformations require performing an operation over and over on sequential data (e.g., notes). This iterative process is most naturally implemented by using a for loop to iterate over a list (e.g., a phrase).

This code sample (Ch. 5, p. 144) demonstrates how to reverse the notes in a phrase. This is equivalent to the Mod.retrograde() function.

In reality (i.e., if we were implementing the functionality of Mod.retrograde() precisely), we would be working with an arbitrary phrase already constructed. That is, we would not be constructing the phrase ourselves from a list of pitches and rhythms, as above. In the general case, we would need to ask the phrase to give us its notes, as follows:

Usually, there are more than one way to implement a particular operation. It is important to stop, think, and weigh alternatives; some them may be more efficient or more elegant.

Yet another way would be to iterate through the note list in reverse order directly, thus saving the computational effort of reversing the two lists, pitches and durations, after the loop. The idea is, since we are iterating through the note list already, let’s iterate backwards, i.e., starting from the last note and moving towards the first one.

The following code demonstrates this:

 


Create a music effect based on the DOD FX-35 guitar pedal

This code sample (Ch. 5, p. 147) shows how to implement a more advanced version of a classic guitar effect box, the DOD FX-35 Octoplus. The FX-35 Octoplus generates a note one octave below the original note, allowing guitarists to add “body” to their sound, or play bass lines on regular guitars. Here we generate one octave below plus a fifth below, creating a “power chord” effect from a single melodic line.  This example demonstrates how list operations allow us to transform musical data.

 


Create DNA music

Hofstadter in his seminal work “Gödel, Escher, Bach” explores repetition and similarities between genetic material (e.g., DNA) and music. In particular, he states:

Imagine the mRNA to be like a long piece of magnetic recording tape, and the ribosome to be like a tape recorder. As the tape passes through the playing head of the recorder, it is “read” and converted into music, or other sounds … When a “tape” of mRNA passes through the “playing head” of a ribosome, the “notes” produced are amino acids and the pieces of music they make up are proteins (Hofstadter, 1989, p.159)

Many researchers have explored ways to convert human proteins into music. This conversion (also known as sonification) allows people to better understand and study the complicated structures and interdependencies present in genetic material.

In one study, Takahashi and Miller made music from the amino acid sequence in the human thymidylate synthase A protein (Takahashi and Miller, 2007). They converted protein sequences into a range of 13 notes. In particular, they grouped similar amino acids together, and then used a unique chord to represent each group (e.g., a D minor chord). To identify differences between similar amino acid groups, they used chord variations (i.e., chord inversions). Also, they used additional genetic characteristics of each amino acid (i.e., codon distribution) to generate unique rhythm values.

Takahashi and Miller’s objective was to make genetic material more approachable to “the general public, young children, and vision-impaired scientists”(ibid, p. 405). Also, they considered the possibility that the theme provided by each protein could be used as inspiration for improvisation and elaboration, “which would allow the investigator/author to contribute an artistic component to the original melody”(ibid).

The code sample below (Ch. 5, p. 150) explores this possibility to elaborate on the musical theme generated from the human thymidylate synthase A protein, using the musical encoding of Takahashi and Miller. Here, we explore how repetition can represent the gradual unfolding and growth of genetic material in life.  This program makes use of Python list slices, which allow it to capture subsets (i.e., slices) of the protein sequence encoding, and incrementally build a larger musical phrase. Given the limited space, here we concentrate only on the first 13 amino acids (i.e., chords) of the protein.

As mentioned above, the program builds the piece incrementally by adding one new amino acid every iteration. It first creates a phrase consisting of the chord associated with the first amino acid, followed by some silence. Next, it creates a phrase with the chords associated with the first and second amino acids, plus some silence. Then, comes a phrase with the chords for first, second, and third amino acids, and so on. These phrases are stored in a list. After the list is completed, we use it in the second loop.