17. Transforming Fibonacci Numbers into Music.
By Bernd Klein. Last modified: 01 Feb 2022.
Fibonacci Sequence
The Fibonacci sequence or numbers - named after Leonardo of Pisa, also known as Fibonacci - have fascinated not only mathematicians for centuries. Fibonacci formulated an exercise about the rabbits and their reproduction: In the beginning there is one pair of rabbits (male and female), born out of the blue sky. It takes one month until they can mate. At the end of the second month the female gives birth to a new pair of rabbits. It works the same way with every newly born pair of rabbits, i.e. that it takes one month until the female one can mate and after another month she gives birth to a new pair of rabbits. Now let's suppose that every female rabbit will bring forth another pair of rabbits every month after the end of the first month. Be aware of the fact that these "mathematical" rabbits are immortal. So the population for the the generations look like this:
1, 1, 2, 3, 5, 8, 13, 21, ...
We can easily see that each new number is the sum of the previous two.
We get to music very soon, and feel free to skip the mathematics, but one thing is also worth mentioning. The Fibonacci numbers are strongly related to the Golden Ratio $\varphi$:
$$\varphi = {\frac {1 + \sqrt{5}} {2}}$$
because the quotient of last and the previous to last number in this seqence is getting closer and closer to $\varphi$:
$$\lim_{n\to\infty} { F_n \over F_{n-1}} = \varphi$$
($F_n stands for the n-th Fibonacci number)
The Fibonacci numbers, often presented in conjunction with the golden ratio, are a popular theme in culture. The Fibonacci numbers have been used in the visual art and architecture. The have been used in music very often. My favorite example is the song Lateralus by Tool. The text is rhythmically grouped in Fibonacci numbers. If you look at the following lines , you can count the syllables and you will get 1, 1, 2, 3, 5, 8, 5, 3, 13, 8, 5, 3:
Black
then
white are
all I see
in my infancy
Red and yellow then came to be,
reaching out to me
Lets me see
As below, so above and beyond, I imagine
Drawn beyond the lines of reason
Push the envelope,
watch it bend
We will create a piano score for Fibonacci numbers in this chapter. There is no unique way to do this. We will create both a PDF score our "composition" and a midi file, so that you can listen to the result. We will use LilyPond to create the score:
Live Python training
See our Python training courses
LilyPond
What is LilyPond? On the websitelypond.org they write the following:
LilyPond is a music engraving program, devoted to producing the highest-quality sheet music possible. It brings the aesthetics of traditionally engraved music to computer printouts. LilyPond is free software and part of the GNU Project.
You can learn more about Lilypond in the chapter Musical Scores with Python of our Python tutorial. The following page give you also a great impression how LilyPond works: 'Compiling' Music
%%writefile simple_example.ly
\version "2.14.1"
\include "english.ly"
\score {
\new Staff {
\key d \major
\numericTimeSignature
\time 2/4
<cs' d'' b''>16 <cs' d'' b''>8.
%% Here: the tie on the D's looks funny
%% Too tall? Left-hand endpoint is not aligned with the B tie?
~
<cs' d'' b''>8 [ <b d'' a''> ]
}
}
OUTPUT:
Overwriting simple_example.ly
!lilypond simple_example.ly
OUTPUT:
GNU LilyPond 2.20.0 Processing `simple_example.ly' Parsing... Interpreting music... Preprocessing graphical objects... Finding the ideal number of pages... Fitting music on 1 page... Drawing systems... Layout output to `/tmp/lilypond-R5QDyE'... Converting to `simple_example.pdf'... Deleting `/tmp/lilypond-R5QDyE'... Success: compilation successfully completed
You can see the result by looking at the pdf file simple_example.pdf. The original file from which the PDF was created is simple_example.ly
Fibonacci Score
The Fibonacci Function
To create our Fibonacci score we will use the following Fibonacci function. You can find further explanation - most probably not necessary for the understanding of this chapter - concerning the Fibonacci function in our chapter Recursive Functions and additionally in our chapters Memoization with Decorators and Generators and Iterators.
class FibonacciLike:
def __init__(self, i1=0, i2=1):
self.memo = {0:i1, 1:i2}
def __call__(self, n):
if n not in self.memo:
self.memo[n] = self.__call__(n-1) + self.__call__(n-2)
return self.memo[n]
fib = FibonacciLike()
Furthmore, we will use the function gcd, which calculates the greatest common divisor of two positive numbers:
def