About this project
Musicpy is a music programming language implemented as a Python library. Its goal is to let users write music with a concise, human-readable syntax built on music theory and algorithms, and it is fully compatible with Python, so ordinary Python code can interact with it.
Core capabilities
- Represent notes, chords, melodies, rhythms and volumes with compact syntax.
- Perform music-theory operations and design algorithmic composition logic.
- Export musicpy structures to MIDI files, and load MIDI files back into musicpy data structures for further processing.
- Read and write MusicXML files when the optional musicxml dependencies are installed.
- Play music directly, with a play function that can block until playback finishes via wait=True.
- Include a daw module (optional dependencies) for working with soundfonts.
Data structures
- note, chord and scale are the basic classes.
- The note class is congruent to integers and can be used as an int.
- The chord class behaves like a set of notes, and can be treated as a set of integers, a vector, or a matrix when chord progressions are combined.
- These classes support arithmetic use, which the author connects to linear algebra and discrete mathematics, and to experimental styles such as serialism, aleatoric and minimalist music.
Installation and platform notes
- Requires Python 3.7 or newer; install with pip install musicpy.
- Optional extras: pip install musicpy[musicxml], pip install musicpy[daw], or both together.
- Works on Windows, macOS and Linux.
- On Linux, pygame must be older than 2.0.3 for the play function to work correctly, and freepats is needed (for example sudo apt-get install freepats on Ubuntu).
- If no sound is heard, setting wait=True in play can help because some IDEs do not wait for playback.
- On Linux or macOS, the daw module dependency sf2_loader requires extra configuration steps.
Documentation and tooling
- Detailed tutorials are in the project wiki and on Read the Docs; the wiki is updated alongside new releases.
- A PDF and Markdown copy of the wiki is offered for download.
- A separate musicpy_editor project provides an editor with real-time compilation and execution and some syntactic sugar.
Example
A short example shows a nylon-string guitar playing broken chords over a progression, built from chord expressions combined with operators and then passed to play with a bpm and instrument number.
Background
The author began developing musicpy in October 2019 and continues to update it frequently. A stated motivation is to represent how music is constructed from a music-theory perspective rather than only storing notes and tempo, and to give composing AI a music-theory interface instead of relying solely on deep learning and large datasets. The README also notes that familiarity with basic music theory is recommended for effective use.
Comments
0 Rating appears after 10 ratings
Sign in to join the discussion.