.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/musiclang_api/05_control_chord_progression.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_musiclang_api_05_control_chord_progression.py: Control the chord progression of the generation =================================================== MusicLang allows you to control the chord progression of your song. In this example : - We generate a 5 bars song with a classic I vi ii6 V7 chord progression - We let the model decide the last bar chord By using and modifying this example you can create a features that involves enforcing a given chord progression. Check :ref:`chords` to check the chord scale progression syntax. .. GENERATED FROM PYTHON SOURCE LINES 15-48 .. code-block:: Python import os from maidi import MidiScore, instrument import maidi.chords_symbols as cs from maidi.integrations.api import MusicLangAPI # Assuming API_URL and MUSICLANG_API_KEY are set in the environment MUSICLANG_API_KEY = os.getenv("MUSICLANG_API_KEY") # Create a 4 bar template with the given instruments score = MidiScore.from_empty( instruments=[instrument.ELECTRIC_PIANO], nb_bars=5, ts=(4, 4), tempo=120 ) # Get the controls (the prompt) for this score mask, tags, chords = score.get_empty_controls(prevent_silence=True) mask[:, :] = 1 # Regenerate everything in the score # We control the chord progression here (I, vi, ii6, V7, unspecified in D major) : chords = [(cs.I, cs.D, cs.major, cs._root_position), (cs.VI, cs.D, cs.major, ''), # Triad in root position can also be written as empty string '' (cs.II, cs.D, cs.major, cs._6), (cs.V, cs.D, cs.major, cs._7), None # Let the model choose the last chord ] # Call the musiclang API to predict the score api = MusicLangAPI(api_key=MUSICLANG_API_KEY, verbose=True) predicted_score = api.predict(score, mask, tags=tags, chords=chords, async_mode=False, polling_interval=3 ) predicted_score.write("predicted_score.mid") .. _sphx_glr_download_auto_examples_musiclang_api_05_control_chord_progression.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: 05_control_chord_progression.ipynb <05_control_chord_progression.ipynb>` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: 05_control_chord_progression.py <05_control_chord_progression.py>` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: 05_control_chord_progression.zip <05_control_chord_progression.zip>` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_