A Python desktop utility that listens to global keyboard events and plays musical notes in real time.
Recommended Python version: 3.11 or 3.12 (best binary wheel support for audio packages).
- Runs continuously until manually stopped
- Captures keyboard input globally (even when this app is not focused)
- Maps all letter keys (
A-Z) to notes by default - Plays notes with minimal latency using preloaded audio
- Supports multi-key playback (chords)
- Prints key + note for debugging
- Supports custom mappings and instrument switching
- Supports one-shot sample slicing from a song (
--sample-map-file)
- Global key listener:
pynput - Audio playback:
pygame-ce(pygame.mixer) - Language:
Python 3
music_keyboard.py- main application coderequirements.txt- dependenciespyproject.toml- package metadata and keywordsmapping.example.json- example custom key mappingsample_map.example.json- example one-shot slice mappingdocs/KT.md- detailed knowledge-transfer documentCONTRIBUTING.md- contribution and pre-PR checks
python, music-keyboard, global-hotkeys, global-keyboard-listener, pynput, pygame-ce, audio-sampling, audio-slicing, sample-chopping, one-shot-sampler, soundboard, keyboard-sampler, real-time-audio, desktop-app, midi-inspired
- Create and activate a virtual environment:
python3 -m venv .venv
source .venv/bin/activate- Install dependencies:
pip install -r requirements.txtIf dependency resolution is stale, update pip first:
python -m pip install --upgrade pippython3 music_keyboard.pyStop with Ctrl+C.
Z X C V B N M:
Z -> C3X -> D3C -> E3V -> F3B -> G3N -> A3M -> B3
A S D F G H J K L:
A -> C4S -> D4D -> E4F -> F4G -> G4H -> A4J -> B4K -> C5L -> D5
Q W E R T Y U I O P:
Q -> E5W -> F5E -> G5R -> A5T -> B5Y -> C6U -> D6I -> E6O -> F6P -> G6
- Copy
mapping.example.jsonand edit values. - Run:
python3 music_keyboard.py --mapping-file mapping.example.jsonMapping JSON format:
{
"a": "C4",
"s": "D4"
}Use this mode when you want keys to trigger short chunks from one audio track.
Example config file: sample_map.example.json
Run:
python3 music_keyboard.py --sample-map-file sample_map.example.jsonSample map format:
{
"source": "instruments/engeyum kadhal/Engeyum Kaadhal.wav",
"keys": {
"a": { "start": 5.0, "end": 5.3, "label": "Slice A" },
"s": { "start": 5.3, "end": 5.6, "label": "Slice S" },
"d": "samples/chop_d.wav"
}
}Supported per-key spec formats:
"a": "path/to/file.wav": play whole file"a": [5.0, 5.3]: slice from top-levelsource"a": { "start": 5.0, "end": 5.3, "label": "Hook" }: labeled slice fromsource"a": { "file": "path.wav", "start": 1.2, "end": 1.5 }: slice from per-key file
Important:
- Slice mode (
start/end) currently requires WAV input. - One-shot sample-map mode replaces note/instrument mapping for that run.
python3 music_keyboard.py --instrument sine
python3 music_keyboard.py --instrument square
python3 music_keyboard.py --instrument triangle
python3 music_keyboard.py --instrument sawList available instruments:
python3 music_keyboard.py --list-instrumentsInteractive selection at startup:
python3 music_keyboard.py --choose-instrumentProvide a folder containing .wav files for every note used in your mapping, for example:
instruments/piano/C4.wav
instruments/piano/D4.wav
...
Run:
python3 music_keyboard.py --instrument instruments/pianoYou can also pass --instrument piano if the folder exists at instruments/piano.
- Keep
--bufferlow (default256is a good starting point) - Keep CPU load low when performing
- Use local WAV files if you want instrument-specific tone quality
Example:
python3 music_keyboard.py --buffer 128 --channels 48- macOS: you must grant Accessibility/Input Monitoring permission to Terminal (or your Python app), or global key capture will not work.
- Linux: global hooks may depend on X11/Wayland setup and permissions.
- Windows: usually works directly, but some environments may require running terminal as administrator.
This project uses pygame-ce in requirements.txt because it usually ships prebuilt wheels and avoids local SDL compilation.
If you still see an SDL build error:
- Use Python 3.11 or 3.12 in your venv.
- Recreate venv and reinstall:
deactivate
rm -rf .venv
python3.12 -m venv .venv
source .venv/bin/activate
pip install -U pip
pip install -r requirements.txt- If you must compile locally, install SDL dev libs:
brew install sdl2 sdl2_image sdl2_mixer sdl2_ttf pkg-config- The app already works while not focused.
- To hide the console window:
- Windows: launch with
pythonw.exe. - macOS/Linux: run from a launcher/service manager (launchd/systemd) if you want headless startup behavior.
- Windows: launch with
- Confirm local audio/sample files are not accidentally tracked.
- Update project metadata in
pyproject.toml(name, author, URLs). - Run a quick sanity check:
python3 -m py_compile music_keyboard.py- Commit and push:
git add .
git commit -m "Initial project setup: global music keyboard + sample slicing"
git push origin <your-branch>