Retro game music generation library with semantic API for 8-bit/16-bit style MIDI composition
Project description
flexinfer-chiptune
A Python library for generating 8-bit/16-bit style MIDI music and sound effects with a semantic API that agents can use to programmatically compose music.
Features
- Music Theory Foundation: Scales, chords, arpeggios with emotion-to-music mapping
- Game Theme Templates: Pre-built patterns for battle, boss, victory, overworld, etc.
- NES Constraints: Authentic 4-channel (2 pulse, triangle, noise) sound chip model
- Jingle Generator: Quick musical phrases for game events
- Sound Effects: Common game SFX (coin collect, jump, explosion, etc.)
- Agent-Friendly API: Natural language context → music generation
- MIDI Export: Standard MIDI files playable in any DAW
Installation
pip install flexinfer-chiptune
Or with uv:
uv add flexinfer-chiptune
Quick Start
Using the Composer API
from chiptune import ChiptuneComposer
# Create a heroic battle theme
composer = (
ChiptuneComposer.create(bpm=150, root="A", mode="minor")
.set_theme("battle")
.add_melody(contour="ascending", length_bars=8)
.add_bass(style="octave")
.add_arpeggio_layer()
.add_drums(pattern="driving")
)
# Export to MIDI
composer.export_midi("battle_theme.mid")
Using the Agent API
from chiptune import MusicAgent
agent = MusicAgent()
# Generate music from natural language context
music = agent.compose_for_context(
context="player enters the boss arena",
duration_seconds=30.0,
intensity=0.9,
)
# Generate a sound effect
sfx = agent.generate_sfx("coin_collect")
Using Jingles
from chiptune import Jingle
# Create quick jingles
victory = Jingle.victory_fanfare(root="C", tempo=140)
item = Jingle.item_get(root="G")
level_up = Jingle.level_up()
Using Sound Effects
from chiptune import SFXGenerator
sfx = SFXGenerator()
# Generate common game sounds
coin = sfx.coin_collect()
jump = sfx.jump()
explosion = sfx.explosion()
powerup = sfx.powerup()
# Save to file
sfx.save(coin, "coin.mid")
Semantic Mappings
Emotions → Musical Elements
| Emotion | Scale/Mode | Tempo | Character |
|---|---|---|---|
| Heroic | Major/Lydian | Fast | Driving, 4ths/5ths |
| Mysterious | Dorian | Medium | Syncopated |
| Danger | Phrygian/Locrian | Fast | Urgent, chromatic |
| Peaceful | Pentatonic | Slow | Flowing, 3rds/6ths |
| Sad | Minor/Aeolian | Slow | Simple, minor 3rds |
Game Contexts → Themes
| Context | Characteristics |
|---|---|
| Battle | Fast, minor, syncopated, driving drums |
| Boss | Epic, chromatic, heavy bass, intense |
| Victory | Major fanfare, I-IV-V-I, triumphant |
| Overworld | Adventurous, major, walking bass |
| Dungeon | Modal, sparse, atmospheric |
| Shop | Relaxed, major, simple rhythm |
API Reference
ChiptuneComposer
Main entry point for composing music:
create(bpm, root, mode)- Create a new composerset_mood(mood)- Configure for emotional characterset_theme(theme)- Configure for game contextadd_melody(contour, length_bars)- Generate melodyadd_bass(style)- Add bass lineadd_drums(pattern)- Add percussionadd_arpeggio_layer()- Add harmony arpeggiosexport_midi(path)- Export to MIDI fileto_midi_bytes()- Get MIDI as bytes
MusicAgent
Semantic interface for AI agents:
compose_for_context(context, duration, intensity)- Generate from descriptiongenerate_sfx(event)- Generate sound effectcompose_loop(mood, bars)- Generate loopable segment
Jingle
Quick musical phrases:
victory_fanfare()- Triumphant victory sounditem_get()- Item pickup jinglelevel_up()- Level up fanfarecoin_collect()- Quick coin soundgame_over()- Sad game over jingle
SFXGenerator
Game sound effects:
coin_collect()- Coin pickupjump()- Jump soundland()- Landing thudexplosion()- Explosiondamage()- Hurt/damagepowerup()- Power-up soundmenu_select()- Menu cursormenu_confirm()- Menu confirmationlaser()- Projectile/laserteleport()- Teleport sound
Development
# Clone the repository
git clone https://gitlab.flexinfer.ai/libs/py-chiptune.git
cd py-chiptune
# Install dependencies
uv sync
# Run tests
uv run pytest
# Run the demo
uv run python examples/demo.py
License
MIT License
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file flexinfer_chiptune-0.2.4-py3-none-any.whl.
File metadata
- Download URL: flexinfer_chiptune-0.2.4-py3-none-any.whl
- Upload date:
- Size: 39.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
43cb59c75f5ef1137208d62b6026599f36d4cdf7997717b09f28cff004e90999
|
|
| MD5 |
40f53d91e34d9b8cf5aac2b4f84b5530
|
|
| BLAKE2b-256 |
fca9fc67d1f34c4639ba00925318e3fec4db48ae2e6cf2d029746a98dc1c4b8f
|