Code for "Rhythm as an ordered phase of sound: how musical meter emerges in a statistical mechanical model"
Abstract
Purpose This repository was created to accompany the paper Rhythm as an ordered phase of sound: How musical meter emerges in a statistical mechanical model (Physical Review E, 2026). It contains code that has been created to generate accompanying figures from the paper and to aid in exploration of the model presented. Project Structure Code The main directory contains files relevant to running the code and generating outputs. The config.py file contains configurations for the code that allows the user to switch between CPU and GPU options for running the code. The CPU mode is recommended. statistical_mechanics_rhythm.py contains most of the raw Python scripts for generating the statistical distributions and visualizations, while Statistical Mechanics Rhythm Modeling.ipynb is the Jupyter Notebook that calls the code and displays the visualizations. It can be used for interactive exploration of our statistical mechanics rhythm model. The code in the .py file is kept separately from the notebook to reduce clutter and allow for easier execution of the code, but users are welcome to modify the code in either the python file or the jupyter notebook for more advanced exploration. Phase Diagram and Cello Suites There are two other directories within this project. One contains the Bach Cello Suites used in our paper and the other is an outputs directory. We have provided a sampling of .pickle files containing generated distributions of entire phase diagrams in this directory for your use, as generating phase diagrams can get lengthy at higher resolutions. You can also save your own distributions using the notebook. The outputs directory is also where phase diagram visualizations will be saved when you plot them in the notebook. Setup Information We have provided some environment.yml files that can be used to set up an anaconda environment specific to this code. With an installation of anaconda, you can set up the environment by first creating it usingconda env create -f environment.yml followed by activating the environment withconda activate sm-rhythmOnce the environment is set up and activated, you should be able to open the Statistical Mechanics Rhythm Modeling.ipynb Jupyter notebook file in Jupyter. There are four environment files to choose from based on if you want to enable your GPU for faster processing. It is recommended to use the standard environment file, as the GPU version of the code is still very experimental and contains some unsolved noise errors. If it is desired to get set up using pip, make sure Python 3.12 is installed and use requirements.txt to install via pip. Note that if an outdated version of numpy or pandas is used, then the included .pickle files will not be able to be loaded due to different encoding architecture. Notebook The notebook Statistical Mechanics Rhythm Modeling.ipynb is set up to roughly follow the structure of the paper and walk through the generation of most figures presented within. Each cell contains parameters than can be tweaked, and the effects on the resulting distributions can be observed. The notebook is currently set up to explore the free energy of the "Two-Site Model" at various temperatures and chemical potentials, the probability and note length distributions of any number of sites, their phase diagrams, and the points of interest in the 6 and 8 site models we investigated in the paper. The notebook also includes analysis of the Bach Suites and their statistical comparison to a distributions of a specified model.
Fields of Study & Subjects
Music Technology and Sound Studies
Musicology and Musical Analysis
Neuroscience and Music Perception
Publication details
- Journal / source
- Zenodo (CERN European Organization for Nuclear Research)
- Publication date
- 8 April 2026
- Document type
- Book