Ch. 2 – Elements of Music and Code

Topics:  Fundamentals of music, the Python music library, notes, rests, variables, integers and floats, arithmetic operations, input and output, coding a program.

This chapter provides an overview of music representations, and corresponding ways to represent data and information in Python.  This chapter is mainly for people with little or no background in music or computer programming.


Notes

A note in Python consists mainly of pitch and duration.  (It may also include information about dynamic and panning.)

Pitch

The pitch of a note specifies how high or low the note sounds.  In standard music notation, pitch is represented by the vertical placement of a note on the staff.

In Python, pitches are represented by a letter (C, D, E, F, G, A, or B) followed by the octave (or register) of the pitch (as seen below).

PitchesOnStaff

These symbols (e.g., C4) stand for integers and range from 0 to 127.
For example, C4 (middle C on a piano) is 60. This follows the MIDI standard, which represents pitches from 0 (lowest pitch) to 127 (highest pitch). MIDI supports a total of 10 octaves. For comparison, a standard, 88-key piano offers about 7 octaves.

Also see MIDI pitch constants.

Duration

The duration of a note specifies how long it sounds.  In standard notation, duration is represented by the type of note head used, and by the attached vertical lines.

In Python, durations are represented by a real number (float).  Common durations include whole (1/1), half (1/2), quarter (1/4), and eighth (1/8) notes (see below).

DurationsOnStaff

Also see MIDI duration constants.


Play a musical note

Let’s revisit our first program (Ch.2, p. 34).  Again, this program demonstrates how to play a single musical note.

Now that you know more about note representation in Python, you may change this program to play different notes.

(To play a melody, see code samples in chapter 3.)


Find the octave of a MIDI note

Since notes are represented by numbers (pitches are integers, durations are floats), we can use arithmetic to analyze or synthesize music.

Fact:  Music is numbers, and numbers are music.

To demonstrate this, the following program (Ch. 2, p. 45) shows how to calculate the octave of a MIDI pitch.  It also shows how to do input and output, as well as perform arbitrary calculations.