asigalov61 commited on
Commit
2ffbebd
1 Parent(s): f4019fd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -14
app.py CHANGED
@@ -61,7 +61,10 @@ def generate(
61
 
62
  #=================================================================================================
63
 
64
- def GenerateMIDI(params):
 
 
 
65
 
66
  melody_chords_f = generate([3087, 3073+1, 3075+1], 512)
67
 
@@ -98,24 +101,55 @@ def GenerateMIDI(params):
98
 
99
  song_f.append(['note', time, dur, channel, pitch, vel ])
100
 
101
- detailed_stats = TMIDIX.Tegridy_SONG_to_MIDI_Converter(song_f,
102
- output_signature = 'Allegro Music Transformer',
103
- output_file_name = 'Allegro-Music-Transformer-Music-Composition',
104
- track_name='Project Los Angeles',
105
- list_of_MIDI_patches=[0, 24, 32, 40, 42, 46, 56, 71, 73, 0, 53, 19, 0, 0, 0, 0],
106
- number_of_ticks_per_quarter=500)
107
- print('=' * 70)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
108
 
109
  #=================================================================================================
110
 
111
- def cancel_run(mid_seq):
112
- if mid_seq is None:
113
  return None, None
114
- mid = tokenizer.detokenize(mid_seq)
115
  with open(f"output.mid", 'wb') as f:
116
- f.write(MIDI.score2midi(mid))
117
- audio = synthesis(MIDI.score2opus(mid), soundfont_path)
118
- return "output.mid", (44100, audio), [create_msg("visualizer_end", None)]
119
 
120
  def load_javascript(dir="javascript"):
121
  scripts_list = glob.glob(f"*.js")
@@ -176,6 +210,8 @@ if __name__ == "__main__":
176
  run_btn = gr.Button("generate", variant="primary")
177
  stop_btn = gr.Button("stop and output")
178
 
 
 
179
  output_audio = gr.Audio(label="output audio", format="mp3", elem_id="midi_audio")
180
  output_midi = gr.File(label="output midi", file_types=[".mid"])
181
  run_event = run_btn.click(GenerateMIDI, [output_midi_seq, output_midi, output_audio, js_msg])
 
61
 
62
  #=================================================================================================
63
 
64
+ def create_msg(name, data):
65
+ return {"name": name, "data": data}
66
+
67
+ def GenerateMIDI():
68
 
69
  melody_chords_f = generate([3087, 3073+1, 3075+1], 512)
70
 
 
101
 
102
  song_f.append(['note', time, dur, channel, pitch, vel ])
103
 
104
+
105
+ output_signature = 'Allegro Music Transformer'
106
+ output_file_name = 'Allegro-Music-Transformer-Music-Composition'
107
+ track_name='Project Los Angeles'
108
+ list_of_MIDI_patches=[0, 24, 32, 40, 42, 46, 56, 71, 73, 0, 53, 19, 0, 0, 0, 0]
109
+ number_of_ticks_per_quarter=500
110
+ text_encoding='ISO-8859-1'
111
+
112
+ output_header = [number_of_ticks_per_quarter,
113
+ [['track_name', 0, bytes(output_signature, text_encoding)]]]
114
+
115
+ patch_list = [['patch_change', 0, 0, list_of_MIDI_patches[0]],
116
+ ['patch_change', 0, 1, list_of_MIDI_patches[1]],
117
+ ['patch_change', 0, 2, list_of_MIDI_patches[2]],
118
+ ['patch_change', 0, 3, list_of_MIDI_patches[3]],
119
+ ['patch_change', 0, 4, list_of_MIDI_patches[4]],
120
+ ['patch_change', 0, 5, list_of_MIDI_patches[5]],
121
+ ['patch_change', 0, 6, list_of_MIDI_patches[6]],
122
+ ['patch_change', 0, 7, list_of_MIDI_patches[7]],
123
+ ['patch_change', 0, 8, list_of_MIDI_patches[8]],
124
+ ['patch_change', 0, 9, list_of_MIDI_patches[9]],
125
+ ['patch_change', 0, 10, list_of_MIDI_patches[10]],
126
+ ['patch_change', 0, 11, list_of_MIDI_patches[11]],
127
+ ['patch_change', 0, 12, list_of_MIDI_patches[12]],
128
+ ['patch_change', 0, 13, list_of_MIDI_patches[13]],
129
+ ['patch_change', 0, 14, list_of_MIDI_patches[14]],
130
+ ['patch_change', 0, 15, list_of_MIDI_patches[15]],
131
+ ['track_name', 0, bytes(track_name, text_encoding)]]
132
+
133
+ output = output_header + [patch_list + song_f]
134
+
135
+ midi_data = TMIDIX.score2midi(output, text_encoding)
136
+
137
+ with open(f"Allegro-Music-Transformer-Music-Composition", 'wb') as f:
138
+ f.write(midi_data)
139
+
140
+ audio = synthesis(TMIDIX.score2opus(output), 'SGM-v2.01-YamahaGrand-Guit-Bass-v2.7.sf2')
141
+
142
+ yield output, "Allegro-Music-Transformer-Music-Composition.mid", (44100, audio), [create_msg("visualizer_end", None)]
143
 
144
  #=================================================================================================
145
 
146
+ def cancel_run(output):
147
+ if output is None:
148
  return None, None
 
149
  with open(f"output.mid", 'wb') as f:
150
+ f.write(TMIDIX.score2midi(output))
151
+ audio = synthesis(TMIDIX.score2opus(output), 'SGM-v2.01-YamahaGrand-Guit-Bass-v2.7.sf2')
152
+ return "Allegro-Music-Transformer-Music-Composition.mid", (44100, audio), [create_msg("visualizer_end", None)]
153
 
154
  def load_javascript(dir="javascript"):
155
  scripts_list = glob.glob(f"*.js")
 
210
  run_btn = gr.Button("generate", variant="primary")
211
  stop_btn = gr.Button("stop and output")
212
 
213
+ output_midi_seq = gr.Variable()
214
+ output_midi_visualizer = gr.HTML(elem_id="midi_visualizer_container")
215
  output_audio = gr.Audio(label="output audio", format="mp3", elem_id="midi_audio")
216
  output_midi = gr.File(label="output midi", file_types=[".mid"])
217
  run_event = run_btn.click(GenerateMIDI, [output_midi_seq, output_midi, output_audio, js_msg])