avatarSam Vaseghi

Summary

Mozart's "Musikalisches Würfelspiel" or "Musical Dice Game" exemplifies the intersection of music, mathematics, and early machine learning, showcasing his use of mathematical principles and probabilistic processes to create a game that generates unique musical compositions.

Abstract

Wolfgang Amadeus Mozart's approach to composition, as evidenced by his "Musical Dice Game," reveals a deep integration of musical aesthetics with mathematical structures. The game, which allows for the creation of a "Viennese Waltz" through the random selection of precomposed musical phrases based on dice rolls, demonstrates an understanding of combinatorial mathematics and probability. This process reflects a historical interest in automating music composition, making it accessible to those without formal musical training. The compositions generated by the game exhibit symmetry and variations akin to mathematical symmetry and self-similarity, with some of Mozart's works, such as his Piano Sonata №1 in C Major, even approximating the Golden Ratio. The game's design, which involves 11 variations for each of the 16 phrases in the minuet and 6 variations for each of the 16 phrases in the trio, leads to a vast number of possible musical outcomes. The contemporary relevance of Mozart's game is underscored by its connection to modern machine learning techniques, particularly in the context of natural language processing and the use of statistical distributions for decision-making under uncertainty.

Opinions

  • The author suggests that Mozart's music, while appreciated for its beauty, also embodies intricate mathematical patterns and symmetry, reflecting a deliberate and structured approach to composition.
  • Mozart's genius is recognized for his ability to creatively integrate mathematical principles into his compositions, creating music that is both intellectually engaging and emotionally compelling.
  • The "Musical Dice Game" is seen as an explicit application of mathematically grounded intelligence by Mozart, transcending mere aesthetic appeal to engage with mathematical concepts.
  • The game's structure, which uses dice rolls to select musical phrases, is likened to a primitive form of machine learning, with parallels drawn to contemporary concepts such as natural language processing and the generation of synthetic data.
  • The author posits that the game reflects an era's fascination with automating music composition, a pursuit that continues today with advanced algorithmic composition techniques and artificial intelligence.
  • The text implies that while Mozart's game operates on simple rules, the underlying principles of randomness and pattern generation resonate with the foundational aspects of modern machine learning models.
  • The author highlights the historical progression from Mozart's game to today's AI-driven music and text generation, emphasizing the evolution from deterministic rule-based systems to complex, context-aware models like transformers.
  • The text suggests that the desire for automation in music composition has deep historical roots, with Mozart's game serving as an early example of this human endeavor.

ON MOZART’S MUSIC & MATHEMATICS

Composing with Dice: Wolfgang Amadeus Mozart’s Fusion of Music, Math, and Machine Learning

Tracing the Lines Between Music, Mathematics, and Modern AI: Mozart’s Musical Dice Game “Musikalisches Würfelspiel”

Mozart's music is not only praised for its aesthetic appeal, but also for its intriguing mathematical structures, which have fascinated both musicians and mathematicians for centuries.

Mozart’s music exhibits, at the different hierarchies of a composition, manifolds of symmetry, where structures and themes are mirrored, paralleled or inverted in variations, akin to mathematical symmetry under translations and transformations. A theme might be moved up or down in pitch, for instance, but retains its structural integrity, creating a sense of dynamic balance and coherence in the music[1]. Mozart’s compositions often employ such variations and also self-similar repetitions, which can be considered a form of mathematical patterning. These repetitions are not exact but are iterated as modified in subtle ways to maintain the listener’s interest, demonstrating an understanding of how to create variation within a structured framework[2].

Indeed, these techniques are not only forms of mathematical patterning, but also artistic methods to engage the listener. Mozart’s compositions can be understood as a passage, a trajectory, of a highly organized and patterned dynamic system, which is complex and non-linear, but despite being in a vital non-equilibrium, always near-equilibrium, stably in resonance with the audience’s comprehension.

Regarding different emerging patterns, some of Mozart’s piano sonatas have also been analysed for their structural use of the Golden Ratio and iterative sequences such as the Fibonacci sequence, which are closely related mathematical concepts found in nature and art. For instance, the first movement of Mozart’s Piano Sonata №1 in C Major is structured such that the exposition, development, and recapitulation sections are in proportions that approximate the Golden Ratio[3].

😉 The Golden Ratio and Fibonacci numbers are mathematical concepts that have fascinated artists, architects, and musicians for centuries due to their appearance in nature and their aesthetic properties. The Golden Ratio, approximately equal to 1.618, and the Fibonacci sequence, where each number is the sum of the two preceding ones (0, 1, 1, 2, 3, 5, 8, 13, …), are often cited for their supposed presence in art and music, suggesting a universal aesthetic appeal.

Although these examples illustrate the mathematical underpinnings of Mozart’s music, the connection between music and mathematics in his work is more about the application of mathematical principles to create balance, symmetry, and beauty, rather than explicit mathematical puzzles or problems. Mozart’s genius lies in his artistic ability to creatively integrate these principles into his compositions, creating music that is both intellectually engaging and emotionally compelling.

While all of these concepts are implicit in Mozart's music, there is one exceptional work that demonstrates Mozart's explicit application of mathematically grounded intelligence: “Musical Dice Game.” (German “Musikalisches Würfelspiel”)— The game’s full title suggests it was designed for those with no knowledge of music or composition.

Published in 1792, by Mozart’s publisher Nikolaus Simrock in Berlin (K. 294dK3 or K. 516fK6) Source: Gallicia)

Mozart’s Dice Game Explained

This exceptional composition exemplifies Mozart's engagement with mathematical concepts, specifically probabilistic processes and inference.

😉 A minuet is a social dance of French origin for two people, usually in 3/4 time. A trio is typically a contrasting middle section of a minuet or a scherzo, often in a different key and texture but also in 3/4 time. A Viennese waltz is composed of two parts — the minuet, followed by the trio.

The game involves using dice to randomly select measures from a pre-composed set of options for each measure of a minuet and a trio, resulting in numerous possible compositions of a Viennese Waltz.

📌Possible number compositions are 11¹⁶ = 45.949.729.863.572.161

The game allows for the creation of music by assembling precomposed musical phrases based on the roll of dice. This reflects an understanding of combinatorial mathematics and probability. By associating the phrases as tokens, and the random process as a probability distribution generator for inference, the work represents a brilliant and pioneering ideation for some of today’s concepts of machine learning and natural language processing (NLP).

Mozart’s original composition tables (1787).

The game reflects a process that generates a Viennese waltz composed of two parts, the minuet followed by the trio, by selecting from a set of 272 precomposed musical fragments based on dice rolls.

The waltz consists of 32 phrases, 16 from the minuet and 16 from the trio. The selection of each phrase within these parts is determined by rolling dice, with the roll directing which specific fragment to use out of several options for each phrase:

For each of the 16 phrases in the minuet, two six-sided dice are rolled. The sum of these dice (ranging from 2 to 12) determines which of the variations for each phrase is selected. This suggests there are 11 variations for each phrase in the minuet, leading to a total of 176 (16 phrases time 11 variations) possible fragments for the minuet part.

The trio also consists of 16 phrases, with one six-sided die rolled for each phrase. The outcome of the die (ranging from 1 to 6) selects the variation for each phrase. This implies there are 6 variations for each phrase in the trio, totalling 96 (16 phrases times 6 variations) possible fragments for the trio part.

The 32 phrases, selected through dice rolls, will be sequentially concatenated to build the complete Viennese waltz composition.

A Pythonization of Mozart’s Dice Game

The above algorithm can be easily translated into a comprehensive python program that can be run in a python environment. The below code is one example that can be directly copied into a Jupyter Notebook or run as a python file via terminal. The phrases can be downloaded as collections of WAV files for clarinet, flute with harp, mbira, and piano in separate folders, each folder containing 272 files, via this link: mozart.zip (305 MB)

😉 If you prefer not to code, you may just skip this section, of course.

The WAV files are already named according to a convention that reflects their part, minuet or trio, phrase number, and variation. For example, minuet3–8.wav for the 8th variation of the 3rd phrase in the minuet, or trio15–6.wav for the 6th variation of the 15th phrase in the trio.

In your code, ensure that base_directory points to the correct location where you have extracted the WAV files. It can be, for instance ./mozart/piano/ directory relative to your notebook’s location.

Furthermore, the script uses pydub for audio manipulation. Ensure you have pydub installed in your python environment, e.g. pip install pydub. You should also haveffmpeg installed, as pydub uses it under the hood for processing different audio formats.

import random
from pydub import AudioSegment

# Function to simulate rolling N six-sided dice and summing their outcomes
def roll_dice(n=1):
    return sum(random.randint(1, 6) for _ in range(n))

# Function to concatenate WAV files into a single output
def concatenate_wavs(output_path, wav_files):
    combined = AudioSegment.empty()
    for wav_file in wav_files:
        combined += AudioSegment.from_wav(wav_file)
    combined.export(output_path, format="wav")

# Base directory where your WAV files are stored
base_directory = "./mozart_wav/piano/"  # Adjust this path to where you've extracted the WAV files

# Constructing the sequences for the Minuet and Trio
# Minuet: 16 phrases, chosen based on the sum of two dice rolls
selected_minuet_files = [f"{base_directory}minuet{i}-{roll_dice(2)}.wav" for i in range(16)]
# Trio: 16 phrases, chosen based on the roll of one die
selected_trio_files = [f"{base_directory}trio{i}-{roll_dice(1)}.wav" for i in range(16)]

# Concatenate the selected measures into a single WAV file
output_path = "viennese_waltz_output.wav"
concatenate_wavs(output_path, selected_minuet_files + selected_trio_files)

print(f"Generated Viennese waltz saved to {output_path}")

After the script’s execution is completed, locate the output WAV file named viennese_waltz_output.wav. This file is your unique composition, generated by the program based on the rules of Mozart’s Dice Game.

Determinism Blended with Randomness

The probabilistic aspect of the Dice Game is quite fascinating and offers various simple parallels to concepts in machine learning, especially regarding the use of statistical distributions to make decisions, predictions, and inference.

The core of Mozart’s game relies on randomness introduced by dice rolls to select musical phrases from a predetermined set. This randomness, produced by a die as a random number generator, is a result of a probabilistic process, where the outcome is not deterministic but instead split up several possible outcomes, each with a certain probability.

For the minuet, two six-sided dice are rolled to choose one of 11 possible musical phrases for each of the 16 sections. The sum of the dice can range from 2 to 12, with different probabilities for each sum.

This introduces a non-uniform distribution, where some musical phrases (those associated with more common sums like 7) are more likely to be selected than others (like those associated with less common sums such as 2 or 12).

Probability distribution of the sum of two six-sided dice (for the simulation code see [**])

The probability distribution of the sum of two six-sided dice is a discrete distribution, which is central to the selection process. This distribution is not flat; it’s shaped like a pyramid, with the highest probability at 7 (6 out of 36 combinations, or about 16.67%), decreasing towards the extremes (2 and 12, each with 1 out of 36 combinations, or about 2.78%). This is discrete probability distribution is known as the binomial distribution in its specific case, or more generally, it could be described as a convolution of two uniform distributions, while each die roll being a uniform distribution.

For the trio, however, one six-sided die is rolled for each of the 16 sections. This introduces a uniform distribution, where each of the 6 musical phrases has an equal probability of being selected (1 out of 6, or about 16.67%).

😉 A uniform distribution is a type of probability distribution in which all outcomes are equally likely to occur. A binomial distribution is a probability distribution that describes the number of successes in a fixed number of independent trials, with only two possible outcomes (success or failure) and a constant probability of success.

A Pursuit of Automation in the Direction of Machine Learning

In machine learning, particularly in probabilistic models or when dealing with uncertainty, statistical distributions play a crucial role. One aspect is decision-making under uncertainty. Just as the Mozart’s Dice Game uses dice rolls to decide which musical phrases to select, many machine learning algorithms use probabilistic distributions to make predictions under uncertainty. Another aspect is sampling. The process of rolling dice and selecting musical phrases is akin to sampling from a distribution, a common practice in machine learning for tasks such as generating synthetic data, performing Monte Carlo simulations, or implementing stochastic gradient descent.

While Mozart’s Dice Game operates on a relatively simple and deterministic set of rules for sequence generation, its reliance on statistical distributions and randomness introduces a layer of complexity and unpredictability that resonates with the principles of machine learning.

But we can go further in our comparative viewpoint, drawing a fundamental parallel between the Mozart’s game and how today transformers in large language models, like GPT, predict the next token in a sequence based on statistical distributions. While the mechanisms differ in complexity and application, at a conceptual level, both processes involve making probabilistic decisions based on prior information.

In Mozart’s game, the choice of the next musical phrase, akin to a “token” in NLP, is determined by a simple, direct process involving dice rolls. The actual“training data” in this analogy is considered as the precomposed fragments Mozart created, each associated with a specific outcome of the dice rolls. The decision-making process is governed by the statistical distribution of the dice rolls, either uniform for the trio or a discrete pyramid-shaped distribution for the minuet.

However, a Lawrence Zbikowski, in his article Conceptualizing Music: Cognitive Structure, Theory, and Analysis states “in truth, chance played little part in the success of the music produced by such games. Instead, what was required of the compilers…[was] a little knowledge about how to put the game together and an understanding of the formal design of waltzes,…”[5]

Today’s transformers use a much more complex method to predict the next token, relying on the context provided by all preceding tokens. The “training data” consists of vast corpora of text the model has been trained on, learning the probabilities of token sequences.

A transformer in artificial intelligence, is a type of deep learning model that uses self-attention mechanisms to process sequential data, such as text, for tasks like translation, summarization, and text generation. Self-attention is a mechanism in neural networks that allows each position in a sequence to dynamically focus and assign based on a probabilistic process different levels of importance to other positions in the same sequence, enhancing the model’s understanding of context.

In Mozart’s game, practically, we would consider the process by which Mozart has selected and optimized his fragments as the training process of the model, and the optimal parameters implicit in the fragments he had elaborated. At each step, the model computes the probabilities of all possible next tokens based on the learned relationships in the training data. The whole process involves understanding context, nuances of language, and even stylistic elements, far beyond the simplicity of rolling dice.

Despite their technological advancements and distinctions, transformers generate, too, text by calculating the probability of each possible next token given the previous tokens, effectively composing text based on the patterns learned during training. This involves a much more technically sophisticated understanding of language and context, but is still fundamentally a process of making predictions based on statistical distributions. Mozart's precomposed fragments serve as a highly simplified analogy to a training data of maximized information for a language model, with the primary distinction being the degree of technological complexity and the mechanisms by which decisions are made.

Final Thoughts

Indeed, the objectives of the era in this regard were similar, emphasizing the human desire for automatization and popularization of music composition. This is akin to the current trends where artificial intelligence is applied to enable everyone to compose a text or produce music.

According to Stephen Hedges, “The ‘galant’ middle class in Europe was playing with mathematics. In this atmosphere of investigation and cataloguing, a systematic device that would seem to make it possible for anyone to write music was practically guaranteed popularity.”[6]

Likewise, Leonard Meyer, a composer, author, and philosopher, who contributed major works in the fields of aesthetic theory and compositional analysis in music writes, “Eighteenth-century composers constructed musical dice games while nineteenth century composers did not. … [W]hat constrained the choice of figures [in seventeenth- and eighteenth-century music] were the claims of taste, coherent expression and propriety, given the genre of work being composed, rather than the inner necessity of a gradually unfolding, underlying process [as in nineteenth century music].”[7]

Mozart’s Dice Game, identified as K516f or K294d and published shortly after Mozart’s death, allowed individuals to compose waltzes by only rolling dice to select from 176 precomposed musical fragments. The game’s full title suggests it was designed for those with no knowledge of music or composition, emphasizing the era’s interest in making music composition accessible through automated means.

😉 “Algorithmic composition” is creating music based on a set of deterministic and probabilistic rules or algorithms.

Indeed, the 18th century’s whimsical approach to music composition, including the use of dice games, was part of a broader exploration of algorithmic composition. This exploration has deep historical roots and has evolved significantly with technological advancements. The concept of algorithmic composition dates back centuries but has gained new dimensions with the advent of computers and artificial intelligence[8].

🤖 “We’ve created MuseNet, a deep neural network that can generate 4-minute musical compositions with 10 different instruments, and can combine styles from country to Mozart to the Beatles. MuseNet was not explicitly programmed with our understanding of music, but instead discovered patterns of harmony, rhythm, and style by learning to predict the next token in hundreds of thousands of MIDI files. MuseNet uses the same general-purpose unsupervised technology as GPT-2, a large-scale transformer model trained to predict the next token in a sequence, whether audio or text.” — OpenAI MuseNet (2019)

Today, the desire for automation in music composition has expanded to sophisticated algorithmic composition techniques and the development of software that can make music even in real-time. Modern approaches include probabilistic processes, rule-based systems, and artificial intelligence. These systems are capable of not only composing music but are also being designed to listen and respond to music being performed, resulting in interactive and adaptive music experiences. This field, known as “live electronics,” represents a significant advancement from the simple dice games of Mozart’s era[8].

🤖 “We’re introducing Jukebox, a neural net that generates music, including rudimentary singing, as raw audio in a variety of genres and artist styles. We’re releasing the model weights and code, along with a tool to explore the generated samples.” — OpenAI Jukebox (2020)

Computational models of perception and cognition of music are being developed to enhance the creative process by allowing computers to critique and improve their compositions. These advancements reflect a continuing fascination with automating music composition, pushing the boundaries of creativity and exploring new possibilities for musical expression[8].

Mozart’s Dice Game from the 18th century represents an early example of the human desire to automate music composition, a desire that has grown and transformed with technological advancements. Today's algorithmic composition techniques and interactive music systems demonstrate the continuing evolution of this field, blending creativity with computational power and artificial intelligence to explore new musical frontiers[8][9].◼︎

Sources

[1] The Mathematics of Mozart’s Music https://www.npr.org/templates/story/story.php?storyId=5175371 [2] Mozart’s Formula, Beethoven and Group Theory https://fundamentals-of-piano-practice.readthedocs.io/chapter1/ch1_research/IV.4.html [3] Great Music and the Fibonacci Sequence https://www.lancaster.ac.uk/stor-i-student-sites/carla-pinkney/2022/02/14/great-music-and-the-fibonacci-sequence/ [4] Wayne, Kevin; Mozart Musical Dice Game http://nifty.stanford.edu/2023/wayne-musical-dice-game/ [5] Zbikowski, Lawrence M. (2002). Conceptualizing Music: Cognitive Structure, Theory, and Analysis, pp. 142–143. ISBN 9780195187977. Quoted in Berkowitz, Aaron (2010). The Improvising Mind: Cognition and Creativity in the Musical Moment, p. 65. ISBN 9780199590957. [6] Hedges, Stephen A. (1978). “Dice Music in the Eighteenth Century”, pp. 184–185, Music & Letters, vol. 59, pp. 180–187. Quoted in Berkowitz (2010), p. 65n60. [7] Meyer, Leonard (1989). Style and Music: Theory, History, and Ideology, p. 193. ISBN 9780226521527. [8] A Brief History of Algorithmic Composition https://ccrma.stanford.edu/~blackrse/algorithm.html [9] Dicing with Mozart: Just as whimsical musicians in the 18th https://www.newscientist.com/article/mg13217994-300-dicing-with-mozart-just-as-whimsical-musicians-in-the-18th/

[*] Credits: The 272 phrases were published in 1792 by Mozart’s publisher Nikolaus Simrock and attributed to Mozart. The WAV files were generated using MuseScore and the open-source MuseScore_General.sf3 soundfont; they were post-processed using ffmpeg. The images of the dice are minor modifications to those created by Steaphan Greene and were released under a CC BY-SA 3.0 license by Wayne, K. [4].

[**] Python simulation code for probability distribution of the sum of two six-sided dice.

import numpy as np
import matplotlib.pyplot as plt

# Generate all possible outcomes for rolling two six-sided dice
outcomes = np.arange(2, 13) # Possible sums range from 2 to 12
probabilities = np.zeros(len(outcomes))
# Calculate the probability of each sum
for i in range(1, 7): # Dice 1 possibilities
    for j in range(1, 7): # Dice 2 possibilities
        probabilities[i + j - 2] += 1
probabilities /= 36 # There are 36 possible outcomes (6x6)
# Plotting the distribution
plt.figure(figsize=(10, 6))
plt.bar(outcomes, probabilities, color='skyblue', edgecolor='black')
plt.title('Probability Distribution of the Sum of Two Six-Sided Dice')
plt.xlabel('Sum of Dice')
plt.ylabel('Probability')
plt.xticks(outcomes)
plt.grid(axis='y', linestyle='--')
plt.show()
Mathematics
Music
Classical Music
Artificial Intelligence
Machine Learning
Recommended from ReadMedium