Skip to main content

Introduction

This python module is designed to offer a collection of functions and classes that can be used to give a meaningful interpretation of a succession of midi note messages, in terms of rhythm and melody.

The developement of this project started with the idea of creating a parsing software that could be used to output rhythmic and melodic symbols, that could be further processed by some machine learning algorithm (based on Markov models, grammars or similar techniques).

The output symbols of the parsing are based on the Impro-Visor software notation (https://www.cs.hmc.edu/~keller/jazz/improvisor/) and the theory supporting it can be found at this link https://www.cs.hmc.edu/~keller/jazz/improvisor/papers.html .

Installation

The package can easily be installed using the pip package manager.

pip install abstract_melody_parser

MidiQueue

To parse note_on/note_off messages, a particular data structure called MidiQueue is used. This queue stores midi note messages with a timestamp, that is needed to parse the rhythmic structure of a melody. This data structure is supposed to collect only melodies, chords are not supported, if multiple note_on messages are pushed into it with timestamps too close with each other, only the first one will be mantained.

In order to insert a new note message with a timestamp included, the get_timestamp_message function must be used.

import abstract_melody_parser as amp

note_queue = amp.MidiNoteQueue()
note_queue.push(amp.get_timestamp_msg('note_on', 47))
# some temporal delay...
note_queue.push(amp.get_timestamp_msg('note_off', 47))

Before parsing a midi queue, it's suggested to clean the note_on messages that are still missing the relative note_off message.

note_queue.clean_unclosed_note_ons()

Some other methods are exposed for extra flexibility.

msg = note_queue.pop() # gets the oldest note message removing it from the queue
list_of_msg = note_queue.get_container() # to deal directly with the data container
musical_notes = note_queue.get_notes() # to obtain a list of notes in std notation
note_queue.clear() # clears the queue

The use of the MidiQueue for the melodic and rhythic parsing will be explained in the following sections.

Parsing melodies

The melody parsing allows to translate a midi note number in a abstract melody notation. This parser supports three different abstract melody symbols.

c: chord tone
l: color tone
x: random tone

An abstract melody can be realized in a particular chord; in order to obtain the correct abstract melody symbol, is necessary to input both a note in standard notation, and a chord. To obtain a note in standard notation from a midi note value, an additional parsing function is needed.

midi_msg = note_queue.pop() # getting a midi message
note_midi = midi_msg['note'] # getting the midi note number
note_std_notation = amp.parse_midi_note(note_midi) # from midi note number to std note notation
chord = 'CM' # C major chord
note_abstract_melody_notation = amp.parse_musical_note(note_std_notation, chord) # from std note notation to abstract note notation

The notes in standard notation are uppercase letters, and a sharp symbol can be present (diesis are not used)

musical_notes = ['C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#', 'A', 'A#', 'B']

To modify the particular tones (chord or color tones) of a chord or to add a new one, the chord_tones dictionary, inside the chord.py script can be edited.

# excerpt of the dictionary
chord_tones = {
    'CM': {
        'c': ['C', 'E', 'G'], # chord tones can be added here
        'l': ['B', 'D',  'F', 'A'] # color tones can be added here
    },
    # the list continues ...
}

Parsing rhythm

The rhythmic parsing process, allows to translate a series of notes composing a melody, in multiple symbols describing the duration of each note. These are the symbol used to represent the rhythm, they are related to a certain bpm and they refer to a 4/4 rhythmic signature.

1: whole note
2: half note
4: quarter note
4dot: quarter note dotted
4t: quarter note triplet
8: eight note
8t: eight note triplet
16: sixteenth note
16t: sixteenth note triplet

To define a rhythmic frame for the analysis, a duration dictionary must be created and updated every time the bpm changes.

bpm = 120.5
durations = amp.get_durations(bpm)

After defining the durations from the bpm, the parsing can follow. It will return a list of symbols for all the notes in the melody.

noteQueue.clean_unclosed_note_ons() # always do that before parsing
rhythmic_symbols = amp.parse_rhythm(note_queue, durations)

Putting all together

Sometimes the informative content of a melody can only be found in the rhythm or in the armonic relations between the notes and a chord, but in many other occasions, is the underlying relation between the two that really expresses the message that a musitian is trying to share with his/her performance. Following this perspective, it can be useful to perform both melodic and rhythmic parsing from a single MidiQueue, and read the resulting symbols as pairs. Let's suppose to parse a MidiQueue that contains a melody playing on a Dm chord

current_chord = 'Dm'
bpm = 125
durations = amp.get_durations(bpm)

noteQueue.clean_unclosed_note_ons() # again, don't forget to clean the unclosed note_on messages
notes = midi_queue.get_notes()

# list comprehension to map the std notes to an abstract melody
abstract_melody = [amp.parse_musical_note(note, current_chord) for note in notes]

# getting the rhythm too
rhythm = amp.parse_rhythm(midi_queue, durations)

The resulting lists can than be merged in a single one.

full_melody = [x+y for x in abstract_melody for y in rhythm]

Metadata

Release files for abstract-melody-parser 0.0.2

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

Source distribution (sdist)

Source distribution for abstract-melody-parser 0.0.2
File Size Uploaded Provenance Actions
abstract_melody_parser-0.0.2.tar.gz 8.0 kB Without provenance Details for abstract_melody_parser-0.0.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for abstract-melody-parser 0.0.2
File Interpreter ABI Platform Actions
abstract_melody_parser-0.0.2-py3-none-any.whl Python 3 none any Details for abstract_melody_parser-0.0.2-py3-none-any.whl

Total release size: 16.2 kB

Release files / abstract_melody_parser-0.0.2.tar.gz

Download URL abstract_melody_parser-0.0.2.tar.gz
Size 8.0 kB
Tags Source
SHA-256 checksum
How to use checksums
d0c0202b1b39824d9b97a2806d5d1ac6e75212d9ad12d80dfcba89783eeac6f7
BLAKE2b-256 checksum
How to use checksums
6bf7079c772462b60ed1d7024af5bf697eeaf505006d586f10a06722cbd68037
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.8.8

Release files / abstract_melody_parser-0.0.2-py3-none-any.whl

Download URL abstract_melody_parser-0.0.2-py3-none-any.whl
Size 8.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
706ac343ea897eed8391f36e120509503c7c3f1afc7279105143e709c65e8552
BLAKE2b-256 checksum
How to use checksums
7631b731eee4c97716f897b66ffd5923862a10ad3d3a194a3874e28cce72476c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.8.8

Security for abstract-melody-parser 0.0.2

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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.0.2 This release

2 release files No provenance

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