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. More information is provided in the reference textbook.
# This program demonstrates how to create a melody from a sine wave.
# It maps the sine function to a melodic (i.e., pitch) contour.
Next we connect additional musical parameters to the sine function, namely, note duration, dynamic, and panning. The piano roll generated by the updated program is shown below. Notice the distortion in the sine wave graph. Why does that happen?
# pitch contour, duration, dynamics (volume), and panning.
The harmonograph is used to study harmonic oscillations. It has a pen and two pendula moving in orthogonal directions. As the pendula move, the pen draws on paper.
There are two versions, lateral and rotary.
Harmonographs are used to visualize music intervals (harmonic ratios). For example, here are shapes generated from different harmonic ratios (i.e., 1:1, 2:1, and 3:2). Lateral harmonograph on the left; rotary harmonograph on the right.
A lateral harmonograph has a pen attached to two pendula moving in orthogonal directions (see below). As pendula move, the pen draws on paper.
Length of pendula — this affects frequency of oscillation. By combining different frequency ratios (e.g., 2:3), we get different shapes (as shown above).
Phase of pendula, relative to one another. This can be same, or reverse.
# in Python.
# Mathematics of Music, Wooden Books, p.19.
A rotary harmonograph has a pen attached to two pendulums moving in orthogonal directions. As the pendulums move, the pen draws on paper. The paper is placed on a third pendulum on a rotary bearing (i.e., gimbals). This provides another oscillation to the system.
This program can also dampen oscillations (via friction). To do so, uncomment the last two statements. This introduces more interesting shapes.
# and is modeled by either (sin, sin) or (cos, sin).
It generates the following shape.
NOTE: This is also the shape drawn by planet Venus on Earth’s sky (Venus rotates around the Sun about 13 times for every 8 Earth rotations). This observation (and trying to explain it) may have been the beginning of science (math, astronomy, physics) and music (scales) by the ancients. The above program distills all those centuries of knowledge development, in just a few lines.
Non-integer ratios correspond to musical intervals that are not harmonious (and not pleasing to the ear).
Such ratios generate chaotic paths as traced by the harmonograph. When exploring, increase the value of variable times to allow the pen to trace orbits over several cycles – to better see the behavior that emerges.
For example, here are two shapes generated by non-harmonic ratios (5.4 : 4, and 9.13 : 7).
(Left) Lateral harmonograph – 5.01 : 4 ratio, same phase, 6 times. (Right) Rotary harmonograph – 9.13 : 7 ratio, same phase, 6 times.
Certain ratios result in paths that will never converge (i.e., never re-trace the same path).
NOTE: The faster a ratio begins to retrace the same path, the more harmonious (consonant) it sounds to our ear. See Legname’s Theory on Density of Intervals (Legname 1998).
Here is another sonification of the planets, influenced by the harmonograph above (also see chapter 7).
This code sample (Ch. 10, p. 334) sonifies planetary velocities. It uses sines and cosines to simulate movement (sound spatialization).
# as per Bode's law).
"""Returns a part with a sonification of a planet's velocity."""
For more details, see this book.