awacke1 commited on
Commit
5eeabb2
·
verified ·
1 Parent(s): e8f8f3c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -26
app.py CHANGED
@@ -53,13 +53,14 @@ class MIDIManager:
53
  self.synthesizer = MidiSynthesizer(self.soundfont_path)
54
  self.loaded_midi = {} # midi_id: (file_path, midi_obj)
55
  self.modified_files = [] # Stores (midi_base64, audio_base64) tuples
 
56
  self.is_playing = False
57
  self.instruments = self.random_instrument_set()
58
  self.drum_beat = self.create_drum_beat()
59
  self.starter_midi = self.create_starter_midi()
60
- self.example_files = self.load_example_midis()
61
  self.loaded_midi["starter"] = ("Starter MIDI", self.starter_midi)
62
  self.preload_default_midi()
 
63
 
64
  def random_instrument_set(self):
65
  instrument_pool = [0, 24, 32, 48] # Piano, Guitar, Bass, Strings
@@ -69,12 +70,12 @@ class MIDIManager:
69
  return [(36, 100, 0), (42, 80, 50), (38, 90, 100), (42, 80, 150)] # Kick, hi-hat, snare, hi-hat
70
 
71
  def create_starter_midi(self):
72
- midi = MIDI.MIDIFile(5) # 4 instruments + 1 drum track
73
  for i, inst in enumerate(self.instruments):
74
  midi.addTrack()
75
  midi.addProgramChange(i, 0, 0, inst)
76
  for t in range(0, 400, 100):
77
- note = random.randint(60, 84) # C4 to C6
78
  midi.addNote(i, 0, note, t, 100, 100)
79
  midi.addTrack()
80
  for note, vel, time in self.drum_beat:
@@ -97,23 +98,16 @@ class MIDIManager:
97
  continue
98
  midi_id = f"example_{len(examples)}"
99
  midi = MIDI.load(file_path)
100
- new_midi = MIDI.MIDIFile(5)
101
- notes, _ = self.extract_notes_and_instruments(midi)
102
- for i, inst in enumerate(self.instruments):
103
- new_midi.addTrack()
104
- new_midi.addProgramChange(i, 0, 0, inst)
105
- for note, vel, time in notes:
106
- new_midi.addNote(i, 0, note, time, 100, vel)
107
- new_midi.addTrack()
108
- for note, vel, time in self.drum_beat:
109
- new_midi.addNote(4, 9, note, time, 100, vel)
110
- examples[midi_id] = (file_path, new_midi)
111
- self.loaded_midi.update(examples)
112
  return examples
113
 
114
  def load_midi(self, file_path):
115
  midi = MIDI.load(file_path)
116
- midi_id = f"midi_{len(self.loaded_midi) - len(self.example_files) - 1}"
117
  self.loaded_midi[midi_id] = (file_path, midi)
118
  return midi_id
119
 
@@ -158,9 +152,8 @@ class MIDIManager:
158
  with open(temp_midi, 'wb') as f:
159
  f.write(midi_output.getvalue())
160
  audio_output = io.BytesIO()
161
- # Placeholder for audio rendering; needs fluidsynth or similar
162
  self.synthesizer.play_midi(new_midi)
163
- audio_data = None # See Notes below
164
  if os.path.exists(temp_midi):
165
  os.remove(temp_midi)
166
 
@@ -215,6 +208,9 @@ def create_download_list():
215
  def get_midi_choices():
216
  return [(os.path.basename(path), midi_id) for midi_id, (path, _) in midi_processor.loaded_midi.items()]
217
 
 
 
 
218
  if __name__ == "__main__":
219
  parser = argparse.ArgumentParser()
220
  parser.add_argument("--port", type=int, default=7860)
@@ -249,20 +245,17 @@ if __name__ == "__main__":
249
 
250
  # Tab 2: Examples
251
  with gr.Tab("Examples"):
252
- example_select = gr.Dropdown(label="Select Example", choices=get_midi_choices(), value=None)
253
  example_output = gr.Audio(label="Example Preview", type="bytes", autoplay=True)
254
 
255
  def load_example(midi_id):
256
- if not midi_id:
257
  return None
258
- midi_data, audio_data = midi_processor.generate_variation(midi_id)
259
  midi_processor.play_with_loop(midi_data)
260
- return io.BytesIO(base64.b64decode(midi_data)), create_download_list()
261
 
262
- example_select.change(load_example, inputs=[example_select],
263
- outputs=[example_output, "downloads"])
264
- gr.State(get_midi_choices()).change(lambda choices: gr.update(choices=choices),
265
- inputs=[gr.State()], outputs=[example_select])
266
 
267
  # Tab 3: Generate & Perform
268
  with gr.Tab("Generate & Perform"):
 
53
  self.synthesizer = MidiSynthesizer(self.soundfont_path)
54
  self.loaded_midi = {} # midi_id: (file_path, midi_obj)
55
  self.modified_files = [] # Stores (midi_base64, audio_base64) tuples
56
+ self.example_variations = {} # midi_id: (midi_base64, audio_base64) for pre-generated examples
57
  self.is_playing = False
58
  self.instruments = self.random_instrument_set()
59
  self.drum_beat = self.create_drum_beat()
60
  self.starter_midi = self.create_starter_midi()
 
61
  self.loaded_midi["starter"] = ("Starter MIDI", self.starter_midi)
62
  self.preload_default_midi()
63
+ self.load_example_midis() # Pre-generate example variations
64
 
65
  def random_instrument_set(self):
66
  instrument_pool = [0, 24, 32, 48] # Piano, Guitar, Bass, Strings
 
70
  return [(36, 100, 0), (42, 80, 50), (38, 90, 100), (42, 80, 150)] # Kick, hi-hat, snare, hi-hat
71
 
72
  def create_starter_midi(self):
73
+ midi = MIDI.MIDIFile(5)
74
  for i, inst in enumerate(self.instruments):
75
  midi.addTrack()
76
  midi.addProgramChange(i, 0, 0, inst)
77
  for t in range(0, 400, 100):
78
+ note = random.randint(60, 84)
79
  midi.addNote(i, 0, note, t, 100, 100)
80
  midi.addTrack()
81
  for note, vel, time in self.drum_beat:
 
98
  continue
99
  midi_id = f"example_{len(examples)}"
100
  midi = MIDI.load(file_path)
101
+ self.loaded_midi[midi_id] = (file_path, midi)
102
+ # Pre-generate variation for each example
103
+ midi_data, audio_data = self.generate_variation(midi_id)
104
+ examples[midi_id] = (midi_data, audio_data)
105
+ self.example_variations = examples
 
 
 
 
 
 
 
106
  return examples
107
 
108
  def load_midi(self, file_path):
109
  midi = MIDI.load(file_path)
110
+ midi_id = f"midi_{len(self.loaded_midi) - len(self.example_variations) - 1}"
111
  self.loaded_midi[midi_id] = (file_path, midi)
112
  return midi_id
113
 
 
152
  with open(temp_midi, 'wb') as f:
153
  f.write(midi_output.getvalue())
154
  audio_output = io.BytesIO()
 
155
  self.synthesizer.play_midi(new_midi)
156
+ audio_data = None # Placeholder; see Notes
157
  if os.path.exists(temp_midi):
158
  os.remove(temp_midi)
159
 
 
208
  def get_midi_choices():
209
  return [(os.path.basename(path), midi_id) for midi_id, (path, _) in midi_processor.loaded_midi.items()]
210
 
211
+ def get_example_choices():
212
+ return [(os.path.basename(path), midi_id) for midi_id, (path, _) in midi_processor.loaded_midi.items() if midi_id.startswith("example")]
213
+
214
  if __name__ == "__main__":
215
  parser = argparse.ArgumentParser()
216
  parser.add_argument("--port", type=int, default=7860)
 
245
 
246
  # Tab 2: Examples
247
  with gr.Tab("Examples"):
248
+ example_select = gr.Dropdown(label="Select Example", choices=get_example_choices(), value=None)
249
  example_output = gr.Audio(label="Example Preview", type="bytes", autoplay=True)
250
 
251
  def load_example(midi_id):
252
+ if not midi_id or midi_id not in midi_processor.example_variations:
253
  return None
254
+ midi_data, audio_data = midi_processor.example_variations[midi_id]
255
  midi_processor.play_with_loop(midi_data)
256
+ return io.BytesIO(base64.b64decode(midi_data))
257
 
258
+ example_select.change(load_example, inputs=[example_select], outputs=[example_output])
 
 
 
259
 
260
  # Tab 3: Generate & Perform
261
  with gr.Tab("Generate & Perform"):