misnaej commited on
Commit
996bed4
1 Parent(s): 661592c

added save MIDI button

Browse files
Files changed (1) hide show
  1. playground.py +68 -22
playground.py CHANGED
@@ -1,16 +1,15 @@
1
- import matplotlib.pyplot as plt
2
  import gradio as gr
3
  from load import LoadModel
4
  from generate import GenerateMidiText
5
- from constants import INSTRUMENT_CLASSES, INSTRUMENT_TRANSFER_CLASSES
6
  from decoder import TextDecoder
7
  from utils import get_miditok, index_has_substring
8
  from playback import get_music
9
  from matplotlib import pylab
10
  import sys
 
11
  import matplotlib
12
  from generation_utils import plot_piano_roll
13
- import numpy as np
14
 
15
  matplotlib.use("Agg")
16
 
@@ -91,13 +90,15 @@ def generator(
91
  genesis.generate_n_more_bars(add_bar_count) # for all instruments
92
  generated_text = genesis.get_whole_piece_from_bar_dict()
93
 
 
94
  decoder.get_midi(generated_text, "mixed.mid")
95
  mixed_inst_midi, mixed_audio = get_music("mixed.mid")
96
-
97
  inst_text = genesis.get_selected_track_as_text(inst_index)
98
- inst_midi_name = f"{instrument}.mid"
99
- decoder.get_midi(inst_text, inst_midi_name)
100
- _, inst_audio = get_music(inst_midi_name)
 
101
  piano_roll = plot_piano_roll(mixed_inst_midi)
102
  track["text"] = inst_text
103
  state.append(track)
@@ -113,6 +114,32 @@ def generator(
113
  )
114
 
115
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
116
  def instrument_row(default_inst, row_id):
117
  with gr.Row():
118
  row = gr.Variable(row_id)
@@ -140,19 +167,20 @@ def instrument_row(default_inst, row_id):
140
  # add_bars = gr.Checkbox(value=False, label="Add Bars")
141
  # add_bar_count = gr.Dropdown([1, 2, 4, 8], value=1, label="Add Bars")
142
  gen_btn = gr.Button("Generate")
143
- gen_btn.click(
144
- fn=generator,
145
- inputs=[row, regenerate, temp, density, inst, state, piece_by_track],
146
- outputs=[
147
- output_txt,
148
- inst_audio,
149
- piano_roll,
150
- state,
151
- mixed_audio,
152
- regenerate,
153
- piece_by_track,
154
- ],
155
- )
 
156
 
157
 
158
  with gr.Blocks() as demo:
@@ -162,6 +190,7 @@ with gr.Blocks() as demo:
162
  """ # Demo-App of The-Jam-Machine
163
  A Generative AI trained on text transcription of MIDI music """
164
  )
 
165
  track1_md = gr.Markdown(""" ## Mixed Audio and Piano Roll """)
166
  mixed_audio = gr.Audio(label="Mixed Audio")
167
  piano_roll = gr.Plot(label="Piano Roll", show_label=False)
@@ -179,11 +208,28 @@ with gr.Blocks() as demo:
179
  track1_md = gr.Markdown(""" ## TRACK 3 """)
180
  instrument_row("Synth Lead Square", 2)
181
  # instrument_row("Piano")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
182
 
183
  demo.launch(debug=True)
184
 
185
  """
186
- TODO: reset button
187
- TODO: add a button to save the generated midi
188
  TODO: add improvise button
 
 
189
  """
 
 
1
  import gradio as gr
2
  from load import LoadModel
3
  from generate import GenerateMidiText
4
+ from constants import INSTRUMENT_TRANSFER_CLASSES
5
  from decoder import TextDecoder
6
  from utils import get_miditok, index_has_substring
7
  from playback import get_music
8
  from matplotlib import pylab
9
  import sys
10
+ import os
11
  import matplotlib
12
  from generation_utils import plot_piano_roll
 
13
 
14
  matplotlib.use("Agg")
15
 
 
90
  genesis.generate_n_more_bars(add_bar_count) # for all instruments
91
  generated_text = genesis.get_whole_piece_from_bar_dict()
92
 
93
+ # save the mix midi and get the mix audio
94
  decoder.get_midi(generated_text, "mixed.mid")
95
  mixed_inst_midi, mixed_audio = get_music("mixed.mid")
96
+ # get the instrument text MIDI
97
  inst_text = genesis.get_selected_track_as_text(inst_index)
98
+ # save the instrument midi and get the instrument audio
99
+ decoder.get_midi(inst_text, f"{instrument}.mid")
100
+ _, inst_audio = get_music(f"{instrument}.mid")
101
+ # generate the piano roll
102
  piano_roll = plot_piano_roll(mixed_inst_midi)
103
  track["text"] = inst_text
104
  state.append(track)
 
114
  )
115
 
116
 
117
+ def generated_text_from_state(state):
118
+ generated_text_from_state = "PIECE_START "
119
+ for track in state:
120
+ generated_text_from_state += track["text"]
121
+ return generated_text_from_state
122
+
123
+
124
+ def save_midi_to_folder(state, midi_path):
125
+ # check if midi_path exists using os
126
+ if os.path.exists(midi_path):
127
+ print(f"The path {midi_path} already exists")
128
+ else:
129
+ os.mkdir(midi_path)
130
+ print(f"Path '{midi_path}' created")
131
+
132
+ # making sure that the path ends with a slash
133
+ if midi_path[-1] != "/":
134
+ midi_path += "/"
135
+
136
+ generated_text = generated_text_from_state(state)
137
+ decoder.get_midi(generated_text, f"{midi_path}your_awesome_midi.mid")
138
+ status = f"MIDI file saved to: {midi_path}your_awesome_midi.mid"
139
+
140
+ return status
141
+
142
+
143
  def instrument_row(default_inst, row_id):
144
  with gr.Row():
145
  row = gr.Variable(row_id)
 
167
  # add_bars = gr.Checkbox(value=False, label="Add Bars")
168
  # add_bar_count = gr.Dropdown([1, 2, 4, 8], value=1, label="Add Bars")
169
  gen_btn = gr.Button("Generate")
170
+
171
+ gen_btn.click(
172
+ fn=generator,
173
+ inputs=[row, regenerate, temp, density, inst, state, piece_by_track],
174
+ outputs=[
175
+ output_txt,
176
+ inst_audio,
177
+ piano_roll,
178
+ state,
179
+ mixed_audio,
180
+ regenerate,
181
+ piece_by_track,
182
+ ],
183
+ )
184
 
185
 
186
  with gr.Blocks() as demo:
 
190
  """ # Demo-App of The-Jam-Machine
191
  A Generative AI trained on text transcription of MIDI music """
192
  )
193
+
194
  track1_md = gr.Markdown(""" ## Mixed Audio and Piano Roll """)
195
  mixed_audio = gr.Audio(label="Mixed Audio")
196
  piano_roll = gr.Plot(label="Piano Roll", show_label=False)
 
208
  track1_md = gr.Markdown(""" ## TRACK 3 """)
209
  instrument_row("Synth Lead Square", 2)
210
  # instrument_row("Piano")
211
+ #
212
+ track1_md = gr.Markdown(""" ## SAVE AS MIDI file - enter a valid path """)
213
+ with gr.Row():
214
+ # with gr.Column(scale=1, min_width=100):
215
+ saving_path = gr.Textbox(
216
+ value="/Users/ChatGPT/Downloads/TheJamMachine/",
217
+ show_label=False,
218
+ label="Saving Path",
219
+ interactive=True,
220
+ )
221
+ # with gr.Column(scale=1, min_width=100):
222
+ gen_btn = gr.Button("Save MIDI")
223
+ # with gr.Column(scale=1, min_width=100):
224
+ status = gr.Textbox(show_label=False)
225
+
226
+ gen_btn.click(fn=save_midi_to_folder, inputs=[state, saving_path], outputs=status)
227
+
228
 
229
  demo.launch(debug=True)
230
 
231
  """
 
 
232
  TODO: add improvise button
233
+ TODO: cleanup input output of generator
234
+ TODO: add a way to add bars
235
  """