Music Embedding

Travis CI Codecov GitHub license GitHub release GitHub Stars Repo Size Code Size Code Quality

Music Embedding is an open source python package for representing music data based on music theory. It provides tools to convert melodic and harmonic sequences to and from pianorolls.

Features

  • Representation for music intervals

  • Create sequence of (harmonic or melodic) intervals from pianoroll presentation

  • Create pianoroll from a sequence of (harmonic or melodic) intervals

  • Break the sequence of intervals into smaller pieces e.g. bars

  • Compress the sequence of intervals using Run Length Encoding (RLE)

Why Music Embedding

Embedding is an underexplored area in the intersection of AI and music. While many works try to apply NLP-based embedding and automatic embedding (such as convolution), representing music data based on music theory is important. Music Embedding package aims to make employment of music theory easy to enhance the computationals music projects’ results. Some potential usecases are:

Installation

Music Embedding’s only prerequisit is the Numpy package. Music Embedding is developed and tested in interaction with Pypianoroll ; yet, any other code which can handle pianorolls should work just fine.

To install Music Embedding, please run pip install music_embedding. To build Music Embedding from source, please download the source and run python setup.py install.

Semantic Versioning Policy

Music Embedding uses x.y.z format to indicate the version where x is major versin number, y is minor version number, and z is the patch number.

Classes Documentation

For detailed documenation please visit:

Usage

Please visit the Getting Started page.

The following code snippet demonstrates how to convert a midi file into a sequence of harmonic intervals.

import music_embedding
import pypianoroll

if __name__ == '__main__':
        #opening midi file using pypianoroll
        midi_path = r'c:\Moonlight Sonata.mid'
        multi_track = pypianoroll.read(midi_path)

        #mergeing midi tracks into a single pianoroll so harmonic intervals can be extracted
        merged_piano_roll = multi_track.blend('max')

        #getting pianoroll of the first track
        pianoroll = multi_track.tracks[0].pianoroll

        #creating embedder object from music embedding package
        embedder = music_embedding.embedder.embedder()

        #extracting harmonic intervals
        harmonic_intervals = embedder.get_harmonic_intervals_from_pianoroll(pianoroll=pianoroll, ref_pianoroll=merged_piano_roll)

        #creating interval object from music embedding package
        interval = music_embedding.interval.interval()

        #printing the first 20 intervals
        for i in range(20):
                interval.set_specs_list(harmonic_intervals[i])
                print(interval)

Citing

Please cite this paper if you use Music Embedding in a published work.

An Example Use Case

This is a simple use case of Music Embedding. These diagrams show how different classical composers have used intervals. For more details please see this document.

Interval ratios

The diagram below shows a radar chart in which 15 composers with the most data in the dataset are compared with each other and the entire dataset. In this chart, the ratio of occurrences of minor and Major intervals are compared both harmonically and melodically. Also, the ratio of descending and ascending melodic intervals is shown. Hover the curser on the diagram for interactions.

The minor-Major ratio for melodic intervals in the entire dataset is 0.4773, which is lower than the same value for harmonic intervals. Brahms and Dvorak have the highest melodic ratios with 0.5343 and 0.5273 respectively. The lowest ratios belong to Haendel and Tchaikovsky with 0.4428 and 0.4431 respectively. Although the deviations in the melodic ratios are more than the deviations in the harmonic ratios, still they are within the same range. However, the melodic ratios of only Schubert, Brahms, Chopin, and Dvorak are greater than their harmonic ratios while for the rest of composers and the entire dataset the opposite is true.

The descending to ascending ratio for the entire dataset is 0.4030 which shows that the general trend is to use ascending intervals more often. Since the range of notes is limited, upward and downward movements need to cancel each other out to ensure the notes are in the feasible range. Having a descending to ascending ratio which is far from 0.5 concludes that ascending intervals are generally smaller than descending intervals; therefore, more ascending intervals are needed to compensate. In contrast to Bach who has the highest ratio (0.4611), Tchaikovsky has the lowest ratio (0.2351). This means that on average, out of every four intervals, Tchaikovsky has used only one descending interval.

Melodic intervals

The next four diagrams show the trends in each pair of melodic intervals happening next to each other. These diagrams show that unlike Mozart and Beethoven, Bach has used unison and octave intervals less frequently. Also, in all four diagrams, dim 5th has the lowest value. Furthermore, the strongest connection is consistently between Maj 2nd and min 7th. Hover the curser on the diagram for interactions.

Harmonic intervals

The next four diagrams show the trends in each pair of harmonic intervals happening next to each other. Consonant and dissonant intervals can clearly be seen in this figure. Maj 7th and min 2nd are strongly dissonant in music theory and in these diagrams we see how small their proportions are. In contrast, perfect intervals, which are considered to be consonant, have a dominant presence. Hover the curser on the diagram for interactions.

Issues

If you find a problem, please file a bug.

License

This project is licensed under the MIT License - see the LICENSE file for details.