Skip to main content

A python package for music notation and generation

Project description

MusicLang Predict

MusicLang logo

MusicLang Predict is a tool to create original midi soundtracks with generative AI model. It can be used for different use cases :

  • Predict a new song from scratch (a fixed number of bars)
  • Continue a song from a prompt
  • Predict a new song from a template (see examples below)
  • Continue a song from a prompt and a template

To solve template generation use cases, we provide an interface to create a template from an existing midi file.

Our transformers models are hosted on Hugging Face and are available here : MusicLang.

We are based on the MusicLang music language. See : MusicLang for more information.

Installation

Install the musiclang-predict package with pip :

pip install musiclang-predict

How to use ?

  1. Create a new 8 bars song from scratch :
from musiclang_predict import predict, MusicLangTokenizer
from transformers import GPT2LMHeadModel

# Load model and tokenizer
model = GPT2LMHeadModel.from_pretrained('musiclang/musiclang-4k')
tokenizer = MusicLangTokenizer('musiclang/musiclang-4k')
soundtrack = predict(model, tokenizer, chord_duration=4, nb_chords=4, temperature=1.0)
soundtrack.to_midi('song.mid', tempo=120, time_signature=(4, 4))
  1. Or use an existing midi song as a song structure template :
from musiclang_predict import midi_file_to_template, predict_with_template, MusicLangTokenizer
from transformers import GPT2LMHeadModel

# Load model and tokenizer
model = GPT2LMHeadModel.from_pretrained('musiclang/musiclang-4k')
tokenizer = MusicLangTokenizer('musiclang/musiclang-4k')

template = midi_file_to_template('my_song.mid')
soundtrack = predict_with_template(template, model, tokenizer, temperature=1.0)
soundtrack.to_midi('song.mid', tempo=template['tempo'], time_signature=template['time_signature'])

See : MusicLang templates For a full description of our template format. It's only a dictionary containing information for each chord of the song and some metadata like tempo. You can even create your own without using a base midi file !

  1. Or even use a prompt and a template to create a song
from musiclang_predict import midi_file_to_template, predict_with_template, MusicLangTokenizer
from transformers import GPT2LMHeadModel
from musiclang import Score

# Load model and tokenizer
model = GPT2LMHeadModel.from_pretrained('musiclang/musiclang-4k')
tokenizer = MusicLangTokenizer('musiclang/musiclang-4k')
template = midi_file_to_template('my_song.mid')
# Take the first chord of the template as a prompt
prompt = Score.from_midi('my_prompt.mid', chord_range=(0, 4))
soundtrack = predict_with_template(template, model, tokenizer, 
                                   temperature=1.0,
                                   prompt=prompt,  # Prompt the model with a musiclang score
                                   prompt_included_in_template=True  # To say the prompt score is included in the template
                                   )
soundtrack.to_midi('song.mid', tempo=template['tempo'], time_signature=template['time_signature'])
  1. Chord prediction with a transformer model

a. Without a prompt :

from musiclang_predict import predict_chords, MusicLangTokenizer
from transformers import AutoModelForCausalLM, AutoTokenizer
from musiclang.library import *

# Load model and tokenizer
model = AutoModelForCausalLM.from_pretrained('musiclang/musiclang-chord-v2-4k')
tokenizer = MusicLangTokenizer('musiclang/musiclang-chord-v2-4k')
soundtrack = predict_chords(model, tokenizer, nb_chords=4, temperature=1.0)

# Give the chord a simple voicing (closed position chord)
soundtrack = soundtrack(b0, b1, b2, b3)

# Save it to midi
soundtrack.to_midi('song.mid', tempo=120, time_signature=(4, 4))

b. With a prompt :

from musiclang_predict import predict_chords, MusicLangTokenizer
from transformers import AutoModelForCausalLM, AutoTokenizer
from musiclang.library import *

prompt = (I % I.M) + (V % I.M)['6'].o(-1)

# Load model and tokenizer
model = AutoModelForCausalLM.from_pretrained('musiclang/musiclang-chord-v2-4k')
tokenizer = MusicLangTokenizer('musiclang/musiclang-chord-v2-4k')
soundtrack = predict_chords(model, tokenizer, nb_chords=4, prompt=prompt, temperature=1.0)

# Give the chord a simple voicing (closed position chord)
soundtrack = soundtrack(b0, b1, b2, b3)

# Save it to midi
soundtrack.to_midi('song.mid', tempo=120, time_signature=(4, 4))

Contact us

If you want to help shape the future of open source music generation, please contact us

License

The MusicLang predict package (this package) is licensed under the GPL-3.0 License.

However please note that specific licenses applies to each models. If you would like to use one of our model in your product or a custom one, please contact us. We are looking forward to hearing from you !

The MusicLang base language (musiclang package) is licensed under the BSD 3-Clause 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 Distribution

musiclang-predict-0.5.0.tar.gz (30.8 kB view details)

Uploaded Source

Built Distribution

musiclang_predict-0.5.0-py3-none-any.whl (32.2 kB view details)

Uploaded Python 3

File details

Details for the file musiclang-predict-0.5.0.tar.gz.

File metadata

  • Download URL: musiclang-predict-0.5.0.tar.gz
  • Upload date:
  • Size: 30.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.8.18

File hashes

Hashes for musiclang-predict-0.5.0.tar.gz
Algorithm Hash digest
SHA256 c3c921a02ea1496d8ad608760f62ddf7571b86674f26c636a65b1e567affd643
MD5 8aea550729973f65be47ecd83b1313c8
BLAKE2b-256 315f9cfd750c278a5262481f01d6465f01dbec2beed3bc94986f870fff5ed3ae

See more details on using hashes here.

File details

Details for the file musiclang_predict-0.5.0-py3-none-any.whl.

File metadata

File hashes

Hashes for musiclang_predict-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e6b81504e27f7b742712a6ce869a652d4668aae268c95b4c8a81f93fe5e7cd33
MD5 eb993e82f3e8ccffcc581508ad6ea7dd
BLAKE2b-256 3200903300f39a3514659f91d9b278be51b3f847879269c01489b9ccd8d6cc97

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page