A lightweight tool library for converting human-friendly music notation to MIDI data.
Project description
notemidi
Language:
- English
- 中文 请查看README.zh.md。
This project is synchronized across Gitee, GitHub, and GitCode.
(Gitee username: dream-function; GitHub/GitCode username: dreamfunction)
Introduction
A lightweight tool library for converting human-friendly music notation to MIDI data.
notemidi allows you to write music in the most natural way: ('C4', 4) represents a C4 (MIDI 60) quarter note. It translates this "text notation" into MIDI‑ready data (pitch number + duration in seconds).
✨ Core Features
- Text notation:
('C4', 4)for a quarter note C4 - Dotted notes:
'4.'for a dotted quarter note - Rests:
'r'orNone - Additive durations:
'2+2+4'sums to 8 beats
📦 Relationship with MIDI Libraries
notemidi is not another MIDI I/O library (like mido, python-rtmidi). Instead it:
- Focuses on the "human input" layer
- Outputs standardised
(pitch, duration)data that can be consumed by any MIDI or audio library
Installation
pip install notemidi or copy the code directly into your project.
Usage
The core function of this module is translate (for most use cases, other functions are not needed). You can also find a DEFAULT constant inside the module whose value is 'translate'.
def translate(mlist, bpm=120, wnote=4, default_velocity=80): """ Convert a human-friendly melody list to a list of (MIDI pitch, seconds, velocity).
Parameters:
mlist: List of tuples (note, duration [, velocity])
note: 'C4', 'c4', 'F#3', 'r', 'R', None, or a MIDI number
duration: number (4=quarter, 8=eighth) or string ('4.'=dotted, '2+2+4'=additive)
velocity: optional, 0-127, passed through as-is; uses default_velocity if omitted
bpm: Beats per minute, default 120
wnote: Beats per whole note, default 4 (i.e. 4/4 time)
default_velocity: Default velocity value when not specified in tuple, default 80
Returns:
List of [(midi, seconds, velocity), ...]; rests are represented as None for the pitch.
Raises:
TypeError: Invalid input type
ValueError: Invalid duration or tuple length not 2 or 3
KeyError: Unknown note name
"""
Appendix(Other Functions)
def translate_dot(dur, bpm=120, wnote=4): """ Convert a dotted duration string (e.g., '4.') to seconds.
Parameters:
dur: dotted duration string (e.g., '4.' for dotted quarter note)
bpm: beats per minute
wnote: beats per whole note (default 4)
Returns:
Corresponding duration in seconds.
Raises:
TypeError: invalid format (not a digit followed by '.')
ValueError: duration <= 0
"""
def translate_duration(dur, bpm=120, wnote=4): """ Convert a plain numeric duration (e.g., 4) to seconds.
Parameters:
dur: numeric duration (e.g., 4 for quarter note, relative to whole note)
bpm: beats per minute
wnote: beats per whole note (default 4)
Returns:
Corresponding duration in seconds.
Raises:
TypeError: dur is not a number
ValueError: dur <= 0
"""
def translate_note(note): """ Convert a note name to MIDI pitch number; return None for rests.
Parameters:
note: note name ('C4', 'c4', 'F#3'), 'r'/'R'/None for rest, or a MIDI number
Returns:
MIDI pitch number, or None (rest).
Raises:
ValueError: input is not a string, number, or None
KeyError: note name not found in mapping table
"""
def translate_tie(dur, bpm=120, wnote=4): """ Parse an additive duration string (e.g., '2+2+4') and return total seconds.
Parameters:
dur: additive expression string, parts separated by '+',
each part can be a plain number or a dotted string
bpm: beats per minute
wnote: beats per whole note
Returns:
Total duration in seconds.
Raises:
ValueError: empty expression, consecutive '+', leading/trailing '+', or invalid part
"""
Contributing
Issues and Pull Requests are welcome.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
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 notemidi-0.2.0.tar.gz.
File metadata
- Download URL: notemidi-0.2.0.tar.gz
- Upload date:
- Size: 5.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
334107ba138a9232256e2d1e0b8cc38d334a338417a7a2aa146f711f6347cf6a
|
|
| MD5 |
8ac9f2004a30bd939c3b5edb4b70529f
|
|
| BLAKE2b-256 |
f8b16b26d753d9c5b74b849fb05059c39b4d97f4aec5feda505c24d7d2021ab3
|
File details
Details for the file notemidi-0.2.0-py3-none-any.whl.
File metadata
- Download URL: notemidi-0.2.0-py3-none-any.whl
- Upload date:
- Size: 6.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4d15a4a61ce077f406401413acd3b6922fadb113f19e2d1d991515e38e179844
|
|
| MD5 |
b573e012274273bca0bb2bb2ffe008a2
|
|
| BLAKE2b-256 |
bdb3a6aa0f21356abdd9c956c6ed3006f1c48fe97924832860586a1e0dd41024
|