Tag: python

Functions (Python)

This tutorial module demonstrates how to create a function in Python. A function is a block of code that runs when called. In computational music research, functions may be used to define common tasks or operations that will be applied to different musical works or excerpts. The keyword def is used to define a function. […]

Transposition

This module demonstrates how to perform the post-tonal transposition operation on pitches and pitch classes. There are two ways of thinking about transposition in post-tonal music: as a way of measuring distance between notes, or as an operation that can be applied to notes. Let’s start with two notes, G4 and C5, given as 67 […]

Extracting Parts and Melodies

This module explains how to extract a part or melody from an existing piece of music. Rather than manually entering all of the notes of a melody, we can import a musical work into music21 and then return the MIDI note numbers. We can import a work from a file, or from the online music21 […]

Basic Math (Python)

This tutorial module surveys some of the basic math functions in Python. We can perform a mathematical operation on any numeric data type: Some additional functions include min (minimum) and max (maximum), which return the lowest and highest values in a sequence: You can use len to find the length of a list: Further functions […]

Pitch and Pitch Class

In this module, we’ll find the pitch class for each pitch of a melody. In post-tonal music, we draw a distinction between pitch and pitch class. Pitch refers to a specific note in a specific range. Pitch class refers to a class of notes that share the same name, plus enharmonic equivalents. A good analogy […]

Melodies and MIDI Notes

This module describes how to transform a melody into a sequence of MIDI notes using music21. There are many instances in computational analysis when it is useful to represent notes as numeric values. For example, using numbers can be useful for music theoretical analysis, and for performing mathematical operations, such as those common to post-tonal […]

Fundamentals (Python)

Python is a general-purpose programming language widely used in a variety of fields, including computational humanities research (and computational musicology in particular). Once you have Python installed, type the following and hit enter to print your first message: The print function displays the elements in parentheses in the console window. This can be a string […]

For Loop (Python)

A for loop is used to iterate over a list or other sequence. The for loop is a fundamental building block of Python programming. Let’s say we we have values stored in a list (notes, intervals, rhythms, etc.) and we want to step through or iterate through them in order to display them or perform […]

List Comprehension (Python)

A list comprehension is a concise way to build a list. It is often used in place of a for loop when creating a new list based on an existing list. For example, let’s say we have a list of pitches (MIDI note numbers) and we would like to generate a list of pitch classes: […]

Python Installation

Get started with Python by following the installation instructions. Here are a few options we recommend for running Python: Terminal (Mac) You can run Python directly in the Terminal on a Mac (Applications -> Utilities -> Terminal). This allow you to get going right away without installing any additional software. Using an IDE You can […]