Skip to main content

code-music

Code-generated music. Write Python, hear sound, export to Spotify.

Try it in your browser — no install needed.


Who are you?

🎧 Just want to listen Start here →
🎹 Want to make music (no theory needed) Start here →
🎼 Know your modes and want full control Start here →
💻 Want to understand or extend the code Start here →

What is this?

A Python library and tool set for generating music programmatically — from a single scale all the way to full multi-track songs with orchestral instruments, EDM synthesis, jazz arpeggios, and effects chains.

from code_music import Song, Track, Note, Chord, EffectsChain, scale, reverb, play

song = Song(title="My Track", bpm=120)

pad  = song.add_track(Track(instrument="pad",    volume=0.5))
lead = song.add_track(Track(instrument="piano",  volume=0.8))

pad.add(Chord("A", "min7", 3, duration=8.0))
lead.extend(scale("A", "pentatonic", octave=5))

song.effects = {"pad": EffectsChain().add(reverb, room_size=0.7, wet=0.4)}

play(song)  # hear it instantly
code-music my_track.py --flac    # → Spotify-ready FLAC

Install

pip install code-music              # from PyPI
pip install code-music[play]        # + real-time playback (sounddevice)

Or develop from source:

git clone https://github.com/Talador12/code-music
cd code-music
make dev          # creates .venv, installs deps, checks ffmpeg
make test         # full pytest suite

Requires Python 3.11+. For MP3/FLAC/OGG export: brew install ffmpeg.

What's in the box

Directory What's there
examples/ 8 step-by-step tutorials: hello world → live coding
songs/ 140 full songs across 55+ genres
samples/ 100+ short instrument and technique demos
scales/ 31 guided scale demos (all 12 keys), plus arpeggio mode
styles/ Theory profiles for 7 genres (deadmau5, Zedd, bebop, cinematic…)
scripts/ Interactive scale/arp player with progress bar
code_music/ The engine, synth, effects, export, notation, voice

Play something right now

# Songs
make play-trance_odyssey      # 90s uplifting trance
make play-tank_bebop          # Cowboy Bebop big-band jazz
make play-deep_space_drift    # Brian Eno ambient
make play-clarity_drive       # Zedd-style festival EDM
make play-symphony_no1        # original orchestral movement
make preview-voice_pacing_demo # narration vs rap pacing demo
# Scales and key relationships
make play-scales
make play-scales-arp          # same scales as arpeggios
make play-scales-group GROUP=world
make play-scale-circle_of_fifths # key relationship reference run

# Direct playback (no file written)
code-music songs/trance_odyssey.py --play

# Live coding: auto-render + play on every save
code-music songs/my_wip.py --watch --play

# Import and render a MIDI file
code-music dummy.py --import-midi my_track.mid -o remix.wav

# Generate a random song and play it (no script needed)
code-music --random
code-music --random jazz
# Generate a complete song from a genre template
from code_music import generate_song, detect_key, play

song = generate_song("lo_fi", bars=16, seed=42)
play(song)

# Analyze any song's key
root, mode, conf = detect_key(song)
print(f"{root} {mode} ({conf:.0%})")

Learn by example

The examples/ directory walks through the entire API step by step:

# Example What you'll learn
01 hello_world.py Notes, tracks, Song basics
02 chords_and_scales.py Chord shapes, scale(), modes
03 effects_chain.py EffectsChain, reverb, delay, compress
04 arrangement.py Section, repeat, Song.arrange
05 track_transforms.py transpose, loop, split, merge, stretch, filter
06 midi_roundtrip.py Export to MIDI, import back, remix
07 json_save_load.py Song.export_json / Song.load_json
08 live_coding.py --watch --play for instant feedback
09 generative.py generate_song() — full AI-composed songs
10 analysis.py detect_key() — Krumhansl-Kessler key analysis
code-music examples/01_hello_world.py --play    # start here
code-music --random jazz                        # or just generate one

Export to Spotify

make spotify     # renders all songs to dist/flac/ + prints upload link

Upload at https://artists.spotify.com → Music → Upload Track. Minimum: 30s, 44100 Hz stereo. FLAC preferred.

Sheet music

make notation-all   # LilyPond (.ly), ABC (.abc), MusicXML (.xml) for every song
  • LilyPond: lilypond dist/notation/lily/trance_odyssey.ly → PDF
  • ABC: paste dist/notation/abc/tank_bebop.abc into https://abc.rectanglered.com
  • MusicXML: open in MuseScore, Sibelius, Dorico, Finale

See docs/ for audience-specific guides.

Metadata

Release files for code-music 5.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for code-music 5.1.0
File Size Uploaded
code_music-5.1.0.tar.gz 366.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for code-music 5.1.0
File Interpreter ABI Platform
code_music-5.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 448.3 kB

Release files / code_music-5.1.0.tar.gz

Download URL code_music-5.1.0.tar.gz
Size 366.7 kB
Tags Source
SHA-256 checksum
How to use checksums
99f913d85b1a80555cc824a682b827e91200d193835d50ed9d3b56c50b78bd1a
BLAKE2b-256 checksum
How to use checksums
11bce833a3aba84c7c7dec2f96276e0d3ee9652e3a2078dd9313231c1b580669
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Apr 2, 2026.

Transparency log

Release files / code_music-5.1.0-py3-none-any.whl

Download URL code_music-5.1.0-py3-none-any.whl
Size 81.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7c5c81387165664aeb661c6b779b9b9a782bdba7960d32e984c3fd08355e5947
BLAKE2b-256 checksum
How to use checksums
2e8320232346961f95bc6732c7ce81848ce863a307ae3a521d0531bd7cb27413
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Apr 2, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

5.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page