floriangardin commited on
Commit
115df79
1 Parent(s): ffe5a89

choose chord progression

Browse files
Files changed (2) hide show
  1. .gitignore +5 -1
  2. app.py +46 -28
.gitignore CHANGED
@@ -1,3 +1,7 @@
1
 
2
  .idea/
3
- *.mid
 
 
 
 
 
1
 
2
  .idea/
3
+ *.mid
4
+ venv/
5
+ *.mid
6
+ *.wav
7
+ *.mp3
app.py CHANGED
@@ -1,70 +1,88 @@
1
  import gradio as gr
2
- from musiclang_predict import MusicLangPredictor # Assuming this is the correct import
 
3
  from midi2audio import FluidSynth
4
  import os
5
 
6
-
7
- def musiclang(nb_tokens, temperature, chord_progression, tempo):
8
  top_p = 1.0
9
  seed = 0
10
 
11
- # If chord progression is empty, we might set it to None or handle it accordingly
12
- if chord_progression.strip() == "":
13
- chord_progression = None # Assuming the predictor can handle None as "generate freely"
14
-
15
  # Initialize the MusicLangPredictor
16
  ml = MusicLangPredictor('musiclang/musiclang-v2')
17
- # Generate the score
18
- if chord_progression is None or chord_progression.strip() == "":
19
- score = ml.predict(
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
  nb_tokens=int(nb_tokens),
21
  temperature=float(temperature),
22
  topp=top_p,
23
- chord_progression=chord_progression, # Pass the chord progression, can be None
24
  rng_seed=seed
25
  )
26
  else:
27
- score = ml.predict_chords(
 
28
  chord_progression,
29
  time_signature=(4, 4),
30
  temperature=temperature,
31
  topp=top_p,
32
- rng_seed=seed)
 
33
 
34
- # Save the score as a MIDI file
 
 
35
  midi_path = 'test.mid'
36
- score.to_midi(midi_path, tempo=tempo, time_signature=(4, 4))
37
 
38
- # Convert MIDI to WAV then WAV to MP3
39
  wav_path = 'result.wav'
40
  mp3_path = 'result.mp3'
41
  FluidSynth("/usr/share/sounds/sf2/FluidR3_GM.sf2").midi_to_audio(midi_path, wav_path)
42
-
43
  os.system(f'ffmpeg -i {wav_path} -acodec libmp3lame -y -loglevel quiet -stats {mp3_path}')
44
 
45
- # Return the path to the MP3 for Gradio to display
46
-
47
- # Return both the MP3 path for Gradio to display and the MIDI file path for download
48
- return mp3_path, midi_path
49
-
50
 
51
- # Gradio interface with inputs for temperature, nb_tokens, chord progression, and tempo
52
  iface = gr.Interface(
53
  fn=musiclang,
54
  inputs=[
55
  gr.Number(label="Number of Tokens", value=1024, minimum=256, maximum=2048, step=256),
56
  gr.Slider(label="Temperature", value=0.9, minimum=0.1, maximum=1.0, step=0.1),
57
  gr.Textbox(label="Chord Progression", placeholder="Am CM Dm/F E7 Am", lines=2, value=""),
58
- gr.Slider(label="Tempo", value=120, minimum=60, maximum=240, step=1)
 
 
59
  ],
60
  outputs=[
61
  gr.Audio(label="Generated Music"),
62
- gr.File(label="Download MIDI")
 
63
  ],
64
  title="Controllable Symbolic Music Generation with MusicLang Predict",
65
- description="""Customize the music generation by specifying the number of tokens, temperature, chord progression, and tempo.
66
  \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).
67
- \nIf no chord progression is given, it generates a free sample with the specified number of tokens."""
68
  )
69
 
70
- iface.launch()
 
1
  import gradio as gr
2
+ from musiclang_predict import MusicLangPredictor
3
+ from musiclang import Score
4
  from midi2audio import FluidSynth
5
  import os
6
 
7
+ def musiclang(nb_tokens, temperature, chord_progression, tempo, midi_file, bar_range):
 
8
  top_p = 1.0
9
  seed = 0
10
 
11
+ print(midi_file)
 
 
 
12
  # Initialize the MusicLangPredictor
13
  ml = MusicLangPredictor('musiclang/musiclang-v2')
14
+
15
+ if midi_file is not None:
16
+ # Load the MIDI file and use it as the score prompt
17
+ filepath = midi_file
18
+ start_bar, end_bar = map(int, bar_range.split("-"))
19
+ score = Score.from_midi(filepath, chord_range=(start_bar, end_bar))
20
+ else:
21
+ score = None # Default score is None if no MIDI file is uploaded
22
+
23
+ # Generate the score based on provided inputs and the uploaded MIDI file if available
24
+ if chord_progression.strip() == "" and score is None:
25
+ # Generate without specific chord progression or MIDI prompt
26
+ generated_score = ml.predict(
27
+ nb_tokens=int(nb_tokens),
28
+ temperature=float(temperature),
29
+ topp=top_p,
30
+ rng_seed=seed
31
+ )
32
+ elif score is not None:
33
+ # Generate using the uploaded MIDI file as a prompt
34
+ generated_score = ml.predict(
35
+ score=score, # Use the uploaded MIDI as the score prompt
36
  nb_tokens=int(nb_tokens),
37
  temperature=float(temperature),
38
  topp=top_p,
 
39
  rng_seed=seed
40
  )
41
  else:
42
+ # Generate with specific chord progression
43
+ generated_score = ml.predict_chords(
44
  chord_progression,
45
  time_signature=(4, 4),
46
  temperature=temperature,
47
  topp=top_p,
48
+ rng_seed=seed
49
+ )
50
 
51
+ chord_repr = generated_score.to_chord_repr()
52
+
53
+ # Save the generated score as a MIDI file
54
  midi_path = 'test.mid'
55
+ generated_score.to_midi(midi_path, tempo=tempo, time_signature=(4, 4))
56
 
57
+ # Convert MIDI to WAV then WAV to MP3 for playback
58
  wav_path = 'result.wav'
59
  mp3_path = 'result.mp3'
60
  FluidSynth("/usr/share/sounds/sf2/FluidR3_GM.sf2").midi_to_audio(midi_path, wav_path)
 
61
  os.system(f'ffmpeg -i {wav_path} -acodec libmp3lame -y -loglevel quiet -stats {mp3_path}')
62
 
63
+ # Return the MP3 path for Gradio to display and the MIDI file path for download
64
+ return mp3_path, midi_path, chord_repr
 
 
 
65
 
66
+ # Update Gradio interface to include MIDI file upload and bar range selection
67
  iface = gr.Interface(
68
  fn=musiclang,
69
  inputs=[
70
  gr.Number(label="Number of Tokens", value=1024, minimum=256, maximum=2048, step=256),
71
  gr.Slider(label="Temperature", value=0.9, minimum=0.1, maximum=1.0, step=0.1),
72
  gr.Textbox(label="Chord Progression", placeholder="Am CM Dm/F E7 Am", lines=2, value=""),
73
+ gr.Slider(label="Tempo", value=120, minimum=60, maximum=240, step=1),
74
+ gr.File(label="Upload MIDI File", type="filepath", file_types=[".mid", ".midi"]),
75
+ gr.Textbox(label="Bar Range", placeholder="0-4", value="0-4")
76
  ],
77
  outputs=[
78
  gr.Audio(label="Generated Music"),
79
+ gr.File(label="Download MIDI"),
80
+ gr.Textbox(label="Inferred output Chord Progression", lines=2, value="")
81
  ],
82
  title="Controllable Symbolic Music Generation with MusicLang Predict",
83
+ description="""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.
84
  \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).
85
+ \nIf no chord progression or MIDI file is given, it generates a free sample with the specified number of tokens."""
86
  )
87
 
88
+ iface.launch()