Ch. 10 – Music, Number, and Nature

Topics:   Connecting nature, music and number, Pythagorean theorem, music from math curves, sin() and cos() functions, the Python math library, visualizing oscillations, the harmonograph, sonifying oscillations, Kepler’s harmony of the world revisited.

In the previous chapters, we studied essential building blocks of music and computer science. We now know enough about music and programming to return to the themes introduced in chapter 1. In this chapter we will deepen our exploration into the connections between music, number, and nature, and introduce you to ideas that will hopefully inspire and guide you in your own personal journey into music and programming.

The Pythagoreans discovered that music harmony can be modeled by numbers. As Aristotle mentions, they thought that the principles of mathematics “were the principles of all things. Since, of these principles, numbers are by nature the first, and in numbers there seemed to see many resemblances to the things that exist and come into being” (Aristotle 1992, pp. 70-71).

 


Making music from math curves

A mathematical function describes a line or curve. These curves could be thought of as describing a musical gesture, for example a melodic contour. A function can be mapped to any musical parameter, e.g., pitch in the case of melodic curve, or volume in the case of an amplitude envelope. The mapping between the function values and the musical parameters is entirely up to the composer; for instance, a function may influence more than one parameter at a time. The work of Iannis Xenakis was inspired by this possibility.

This code sample (Ch. 10, p. 321) demonstrates how to create a simple melodic contour using the sin() function. This program creates the pianoroll shown in figure below, which traces the sine wave oscillation.

Melodic contour from a sine wave.

Melodic contour from a sine wave.

Here is the program:

The constant pi is approximately 3.141592653589793. It corresponds to half a circle (or 180 degrees). Accordingly, 2*pi is a full circle (or 360 degrees).

Next, we connect additional musical parameters to the sine function, namely note duration, dynamic, and panning. The pianoroll generated by the updated program is shown below:

Melodic contour from sine wave also mapped to duration of notes.

Melodic contour from sine wave also mapped to duration of notes.

Notice the distortion in the sine wave graph. Why does that happen? The sine wave graph assumes steady movement on the x-axis (time), i.e., constant note durations. Since we connected the sine function to note duration, the resultant note durations fluctuate, hence the distortion in the graph.This sine wave inside a sine wave is the basis for FM synthesis (or frequency modulation) – a technique used for creating realistic, rich sounds for synthesizers.

Here is the program:

 


Simulating a lateral harmonograph

The harmonograph is a device used to study what happens when you combine harmonic oscillations. It is closely related to the Pythagorean discoveries about musical intervals and their relationship to harmonic ratios, as well as to various other musical tunings and scales (Ashton, 2003).

A two-pendulum lateral harmonograph.

A two-pendulum lateral harmonograph.

Harmonographs are used to explore and easily visualize the relationships between different harmonic ratios.

A lateral harmonograph is a device with a pen attached to two pendula moving in orthogonal directions to each other.  As the pendula move, the pen draws geometric shapes on a paper.  Our controls include:

  • The length of the pendula — this affects the frequency of their oscillation.  By combining different frequency ratios (e.g., 2:3), we get different shapes (such as the one shown below).
  • The phase of the pendula, relative to one another.  In Python this is modeled by either using two sin() functions (same phase), or one sin() and one cos() function (opposite, or orthogonal) phase.

The code sample below (Ch. 10, p. 328) creates a display onto which it draws points that trace the movement of the virtual pen connected to the pendula.  It also outputs the current ratio setting as shown here:

Output generated by lateral harmonograph program (2:3 ratio, same phase).

Output generated by lateral harmonograph program (2:3 ratio, same phase).

Here is the program:

Notice the various harmonograph parameters (defined as constants at the program’s beginning).  Again this makes it easy to adjust the harmonograph to new settings.  Also, notice the commented-out statement in the loop.  It can be used to reverse the phase.

 


Simulating a rotary harmonograph

A rotary harmonograph is a device with a pen attached to two pendulums moving in orthogonal directions to each other. As the pendulums move, the pen draws geometric shapes on a paper. Additionally, the paper is placed on another pendulum mounted on a rotary bearing (i.e., gimbals), which adds another oscillation to the drawing.

A three-pendulum rotary harmonograph.

A three-pendulum rotary harmonograph.

The code sample below (Ch. 10, p. 330) draws points that trace the movement of the virtual pen connected to the pendulums. It also outputs the current ratio setting, as seen below:

Output generated by rotary harmonograph program (left) 2:3 ratio, opposite rotation - (right) 8:13 ratio, same rotation.

Output generated by rotary harmonograph program (left) 2:3 ratio, opposite rotation – (right) 8:13 ratio, same rotation.

Here is the program:

Again, notice the various harmonograph parameters (defined as constants at the program’s beginning).  This makes it easy to adjust the harmonograph to new settings.  Also, notice the commented-out statements in the loop.  They can be used to reverse the phase.

Non-integer ratios

Non-integer ratios correspond to musical intervals that are not harmonious, i.e., not pleasing to the ear.

Interestingly, such ratios generate chaotic behavior in the path traced by the harmonograph pen. When exploring, increase the value of variable times to allow the pen to trace orbits over several cycles, so that you can begin to see the behavior that emerges. For example, the figure below shows the shapes generated by particular non-harmonic ratios (with times set to 6).

(Left) Lateral harmonograph (5.01:4 ratio, same phase, 6 times/cycles).   (Right) Rotary harmonograph (5.01:4 ratio, same phase, 6 times/cycles).

(Left) Lateral harmonograph (5.01:4 ratio, same phase, 6 times/cycles).
(Right) Rotary harmonograph (5.01:4 ratio, same phase, 6 times/cycles).

Certain ratios result in paths that will never converge, that is they will never re-trace the same path.

It has been shown that the speed of convergence of the ratios (that is, how quickly they begin to re-trace the same path) corresponds to our perception of density (consonance and dissonance) of musical intervals (Legname 1998). This is a very important connection music, number, and nature.

 


Kepler’s Harmony of the World, No. 2

This code sample (Ch. 10, p. 334) explores a more advanced sonification of the planetary velocities we explored in chapter 7. Here we apply some of the same processes used in the lateral harmonograph to convert planetary velocities to music (or to sonify planetary velocities).

Here is the program:

Notice how the pitch of a planet remains the same for all generated notes – it depends on its planetary velocity.

The panning position and dynamic level of the notes associated with each planet are continually modulated using sin() and cos().   The rate of change is associated with the planetary velocity.  This way, different planets will vary at different speeds.

Since the planetary velocities have harmonic relationships to each other, this musical mapping sonifies these harmonies using the panning and dynamic musical dimensions.  The effect is breathtaking—dynamic variations give an impression of proximity (loud appears close to the listener, while soft appears far); and panning gives an impression of left to right movement.  Two orthogonal (harmonic) oscillations perceivable in one ever-changing sound.

This sonification requires a nice pair of stereo headphones (or speakers).