floriangardin commited on
Commit
4b77a87
1 Parent(s): f0a10e0

update message and tempo warning

Browse files
Files changed (1) hide show
  1. app.py +37 -19
app.py CHANGED
@@ -3,22 +3,27 @@ from musiclang_predict import MusicLangPredictor
3
  from musiclang import Score
4
  from midi2audio import FluidSynth
5
  import os
6
-
7
 
8
 
9
  def inner_loop(nb_tokens, temperature, chord_progression, tempo, midi_file, bar_range):
10
- top_p = 1.0
11
  seed = 0
12
 
13
  print(midi_file)
14
  # Initialize the MusicLangPredictor
15
  ml = MusicLangPredictor('musiclang/musiclang-v2')
16
-
 
17
  if midi_file is not None:
18
  # Load the MIDI file and use it as the score prompt
19
  filepath = midi_file
20
  start_bar, end_bar = map(int, bar_range.split("-"))
21
  score = Score.from_midi(filepath, chord_range=(start_bar, end_bar))
 
 
 
 
22
  else:
23
  score = None # Default score is None if no MIDI file is uploaded
24
 
@@ -45,7 +50,7 @@ def inner_loop(nb_tokens, temperature, chord_progression, tempo, midi_file, bar_
45
  generated_score = ml.predict_chords(
46
  chord_progression,
47
  score=score, # Use the uploaded MIDI as the score prompt
48
- time_signature=(4, 4),
49
  temperature=temperature,
50
  topp=top_p,
51
  rng_seed=seed
@@ -54,33 +59,40 @@ def inner_loop(nb_tokens, temperature, chord_progression, tempo, midi_file, bar_
54
  chord_repr = generated_score.to_chord_repr()
55
 
56
  # Save the generated score as a MIDI file
57
- midi_path = 'test.mid'
58
- generated_score.to_midi(midi_path, tempo=tempo, time_signature=(4, 4))
 
59
 
60
  # Convert MIDI to WAV then WAV to MP3 for playback
61
- wav_path = 'result.wav'
62
- mp3_path = 'result.mp3'
 
 
63
  FluidSynth("/usr/share/sounds/sf2/FluidR3_GM.sf2").midi_to_audio(midi_path, wav_path)
64
  os.system(f'ffmpeg -i {wav_path} -acodec libmp3lame -y -loglevel quiet -stats {mp3_path}')
65
- return mp3_path, midi_path, chord_repr
 
 
 
 
66
 
67
  def musiclang(nb_tokens, temperature, chord_progression, tempo, midi_file, bar_range):
68
  exception = None
69
- mp3_path, midi_path, chord_repr = None, None, None
70
  try:
71
- mp3_path, midi_path, chord_repr = inner_loop(nb_tokens, temperature, chord_progression, tempo, midi_file, bar_range)
 
72
  except Exception as e:
73
  exception = "Error : " + e.__class__.__name__ + " " + str(e)
74
-
75
  # Return the MP3 path for Gradio to display and the MIDI file path for download
76
- return mp3_path, midi_path, chord_repr, exception
77
 
78
  # Update Gradio interface to include MIDI file upload and bar range selection
79
  iface = gr.Interface(
80
  fn=musiclang,
81
  inputs=[
82
  gr.Number(label="Number of Tokens", value=1024, minimum=256, maximum=2048, step=256),
83
- gr.Slider(label="Temperature", value=0.9, minimum=0.1, maximum=1.0, step=0.1),
84
  gr.Textbox(label="Chord Progression (Optional)", placeholder="Am CM Dm/F E7 Am", lines=2, value=""),
85
  gr.Slider(label="Tempo", value=120, minimum=60, maximum=240, step=1),
86
  gr.File(label="Upload MIDI File", type="filepath", file_types=[".mid", ".midi"]),
@@ -90,15 +102,21 @@ iface = gr.Interface(
90
  gr.Audio(label="Generated Music"),
91
  gr.File(label="Download MIDI"),
92
  gr.Textbox(label="Inferred output Chord Progression", lines=2, value=""),
 
93
  gr.Textbox(label="Info Message") # Initially hidden, shown only if there's an error
94
  ],
95
  title="Controllable Symbolic Music Generation with MusicLang Predict",
96
  description="""
97
- \n Simple music gen application that wraps <a href="https://github.com/musiclang/musiclang_predict">musiclang predict</a>.
98
- Beware that the model generates a score, not audio : rendered audio is only to provide a quick preview of the generated music.
99
- \n Customize the music generation by specifying the number of tokens, temperature, chord progression, tempo, and optionally uploading a MIDI file to use as a prompt. Specify the bar range for the MIDI prompt.
100
- \nChord qualities: M, m, 7, m7, m7b5, sus2, sus4, M7, dim, dim7. You can also specify the bass if it belongs to the chord (e.g., Bm/D).
101
- If no chord progression or MIDI file is given, it generates a free sample with the specified number of tokens."""
 
 
 
 
 
102
  )
103
 
104
  iface.launch()
 
3
  from musiclang import Score
4
  from midi2audio import FluidSynth
5
  import os
6
+ import tempfile
7
 
8
 
9
  def inner_loop(nb_tokens, temperature, chord_progression, tempo, midi_file, bar_range):
10
+ top_p = 0.98
11
  seed = 0
12
 
13
  print(midi_file)
14
  # Initialize the MusicLangPredictor
15
  ml = MusicLangPredictor('musiclang/musiclang-v2')
16
+ tempo_message = "" # Default message if no MIDI file is uploaded
17
+ time_signature = (4, 4)
18
  if midi_file is not None:
19
  # Load the MIDI file and use it as the score prompt
20
  filepath = midi_file
21
  start_bar, end_bar = map(int, bar_range.split("-"))
22
  score = Score.from_midi(filepath, chord_range=(start_bar, end_bar))
23
+ tempo = score.config['tempo'] # Use the tempo from the MIDI file and change input
24
+ time_signature = score.config['time_signature']
25
+ time_signature = (time_signature[1], time_signature[2])
26
+ tempo_message = f"Warning : real tempo of file is : {int(tempo)} BPM." # Update message based on MIDI file
27
  else:
28
  score = None # Default score is None if no MIDI file is uploaded
29
 
 
50
  generated_score = ml.predict_chords(
51
  chord_progression,
52
  score=score, # Use the uploaded MIDI as the score prompt
53
+ time_signature=time_signature,
54
  temperature=temperature,
55
  topp=top_p,
56
  rng_seed=seed
 
59
  chord_repr = generated_score.to_chord_repr()
60
 
61
  # Save the generated score as a MIDI file
62
+ temp_midi_file = tempfile.NamedTemporaryFile(suffix=".mid", delete=False)
63
+ midi_path = temp_midi_file.name
64
+ generated_score.to_midi(midi_path, tempo=tempo, time_signature=time_signature)
65
 
66
  # Convert MIDI to WAV then WAV to MP3 for playback
67
+ temp_wav_file = tempfile.NamedTemporaryFile(suffix=".wav", delete=False)
68
+ temp_mp3_file = tempfile.NamedTemporaryFile(suffix=".mp3", delete=False)
69
+ wav_path = temp_wav_file.name
70
+ mp3_path = temp_mp3_file.name
71
  FluidSynth("/usr/share/sounds/sf2/FluidR3_GM.sf2").midi_to_audio(midi_path, wav_path)
72
  os.system(f'ffmpeg -i {wav_path} -acodec libmp3lame -y -loglevel quiet -stats {mp3_path}')
73
+
74
+ # Remove the temporary WAV file
75
+ os.remove(wav_path)
76
+
77
+ return mp3_path, midi_path, chord_repr, tempo_message
78
 
79
  def musiclang(nb_tokens, temperature, chord_progression, tempo, midi_file, bar_range):
80
  exception = None
81
+ mp3_path, midi_path, chord_repr, tempo_message = None, None, None, ""
82
  try:
83
+ mp3_path, midi_path, chord_repr, tempo_message = inner_loop(nb_tokens, temperature, chord_progression, tempo,
84
+ midi_file, bar_range)
85
  except Exception as e:
86
  exception = "Error : " + e.__class__.__name__ + " " + str(e)
 
87
  # Return the MP3 path for Gradio to display and the MIDI file path for download
88
+ return mp3_path, midi_path, chord_repr, tempo_message, exception
89
 
90
  # Update Gradio interface to include MIDI file upload and bar range selection
91
  iface = gr.Interface(
92
  fn=musiclang,
93
  inputs=[
94
  gr.Number(label="Number of Tokens", value=1024, minimum=256, maximum=2048, step=256),
95
+ gr.Slider(label="Temperature", value=0.95, minimum=0.1, maximum=1.0, step=0.1),
96
  gr.Textbox(label="Chord Progression (Optional)", placeholder="Am CM Dm/F E7 Am", lines=2, value=""),
97
  gr.Slider(label="Tempo", value=120, minimum=60, maximum=240, step=1),
98
  gr.File(label="Upload MIDI File", type="filepath", file_types=[".mid", ".midi"]),
 
102
  gr.Audio(label="Generated Music"),
103
  gr.File(label="Download MIDI"),
104
  gr.Textbox(label="Inferred output Chord Progression", lines=2, value=""),
105
+ gr.Textbox(label="Tempo Used", value=""), # Display the tempo used for generation
106
  gr.Textbox(label="Info Message") # Initially hidden, shown only if there's an error
107
  ],
108
  title="Controllable Symbolic Music Generation with MusicLang Predict",
109
  description="""
110
+ \n This is the demo for MusicLang Predict, which offers advanced controllability features and high-quality music generation by manipulating symbolic music. Control your music generation by :
111
+ \n - Specifying the number of tokens
112
+ \n - <b>The temperature</b>: the level of creativity of MusicLang. The higher the temperature, the more creative the generation. We suggest you test different levels to find the one that suits your needs
113
+ \n - <b>Chord progression</b> : Available chord qualities include: M, m, 7, m7b5, sus2, sus4, m7, M7, dim, dim0. You can also specify the bass if it belongs to the chord (e.g., Bm/D)
114
+ \n - <b>The tempo</b>
115
+ \n - <b>Uploading a MIDI</b> file to use as a prompt
116
+ \n - Selecting a bar range of the input file (For example 0-4 means first four bars)
117
+ \n Note: The model generates a score file, not an audio one. Therefore, the audio rendering in this notebook serves as a quick preview of the generated music. For an optimized experience, we recommend downloading the midi file and opening it in your favorite Digital Audio Workstation (DAW).”
118
+ \n If no chord progression or MIDI file is given, it generates a free sample with the specified number of tokens.
119
+ \n Need more info ? Visit <a href="https://github.com/musiclang/musiclang_predict">our Github</a>"""
120
  )
121
 
122
  iface.launch()