asigalov61 commited on
Commit
261f708
1 Parent(s): 4cee860

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +219 -0
app.py ADDED
@@ -0,0 +1,219 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # https://huggingface.co/spaces/asigalov61/MIDI-Search
2
+
3
+ import os
4
+
5
+ import time as reqtime
6
+ import datetime
7
+ from pytz import timezone
8
+
9
+ import numpy as np
10
+
11
+ import gradio as gr
12
+
13
+ import random
14
+
15
+ import zlib
16
+
17
+ from midi_to_colab_audio import midi_to_colab_audio
18
+
19
+ import TMIDIX
20
+
21
+ import matplotlib.pyplot as plt
22
+
23
+ #==========================================================================================================
24
+
25
+ def find_midi(title, artist):
26
+
27
+ print('=' * 70)
28
+ print('Loading MIDI file...')
29
+
30
+ #==================================================
31
+
32
+ print('Searching titles...Please wait...')
33
+ random.shuffle(AUX_DATA)
34
+
35
+ titles_index = []
36
+
37
+ for A in AUX_DATA:
38
+ titles_index.append(A[0])
39
+
40
+ search_string = ''
41
+
42
+ if title != '' and artist != '':
43
+ search_string = title + ' --- ' + artist
44
+
45
+ else:
46
+ search_string = title + artist
47
+
48
+ search_match = process.extract(query=search_string, choices=titles_index, limit=1)
49
+ search_index = titles_index.index(search_match[0][0])
50
+
51
+ print('Done!')
52
+ print('=' * 70)
53
+ print('Selected title:', AUX_DATA[search_index][0])
54
+ print('=' * 70)
55
+
56
+ outy = AUX_DATA[search_index][1]
57
+
58
+ print('Sample INTs', outy[:12])
59
+ print('=' * 70)
60
+
61
+ if len(outy) != 0:
62
+
63
+ song = outy
64
+ song_f = []
65
+
66
+ time = 0
67
+ dur = 0
68
+ vel = 90
69
+ pitch = 0
70
+ channel = 0
71
+
72
+ patches = [-1] * 16
73
+
74
+ channels = [0] * 16
75
+ channels[9] = 1
76
+
77
+ for ss in song:
78
+
79
+ if 0 <= ss < 256:
80
+
81
+ time += ss * 16
82
+
83
+ if 256 <= ss < 2304:
84
+
85
+ dur = ((ss-256) // 8) * 16
86
+ vel = (((ss-256) % 8)+1) * 15
87
+
88
+ if 2304 <= ss < 18945:
89
+
90
+ patch = (ss-2304) // 129
91
+
92
+ if patch < 128:
93
+
94
+ if patch not in patches:
95
+ if 0 in channels:
96
+ cha = channels.index(0)
97
+ channels[cha] = 1
98
+ else:
99
+ cha = 15
100
+
101
+ patches[cha] = patch
102
+ channel = patches.index(patch)
103
+ else:
104
+ channel = patches.index(patch)
105
+
106
+ if patch == 128:
107
+ channel = 9
108
+
109
+ pitch = (ss-2304) % 129
110
+
111
+ song_f.append(['note', time, dur, channel, pitch, vel, patch ])
112
+
113
+
114
+
115
+ x = []
116
+ y = []
117
+ c = []
118
+
119
+ colors = ['red', 'yellow', 'green', 'cyan',
120
+ 'blue', 'pink', 'orange', 'purple',
121
+ 'gray', 'white', 'gold', 'silver',
122
+ 'lightgreen', 'indigo', 'maroon', 'turquoise']
123
+
124
+ for s in [m for m in song_f if m[0] == 'note']:
125
+ x.append(s[1])
126
+ y.append(s[4])
127
+ c.append(colors[s[3]])
128
+
129
+ plt.close()
130
+ plt.figure(figsize=(14,5))
131
+ ax=plt.axes(title='MIDI Match Plot')
132
+ ax.set_facecolor('black')
133
+
134
+ plt.scatter(x,y, c=c)
135
+ plt.xlabel("Time in MIDI ticks")
136
+ plt.ylabel("MIDI Pitch")
137
+
138
+ output_signature = AUX_DATA[search_index][0]
139
+ track_name = 'Project Los Angeles'
140
+ text_encoding = 'ISO-8859-1'
141
+
142
+ list_of_MIDI_patches=[0, 24, 32, 40, 42, 46, 56, 71, 73, 0, 53, 19, 0, 0, 0, 0]
143
+
144
+ output_header = [1000,
145
+ [['set_tempo', 0, 1000000],
146
+ ['time_signature', 0, 4, 2, 24, 8],
147
+ ['track_name', 0, bytes(output_signature, text_encoding)]]]
148
+
149
+ patch_list = [['patch_change', 0, 0, list_of_MIDI_patches[0]],
150
+ ['patch_change', 0, 1, list_of_MIDI_patches[1]],
151
+ ['patch_change', 0, 2, list_of_MIDI_patches[2]],
152
+ ['patch_change', 0, 3, list_of_MIDI_patches[3]],
153
+ ['patch_change', 0, 4, list_of_MIDI_patches[4]],
154
+ ['patch_change', 0, 5, list_of_MIDI_patches[5]],
155
+ ['patch_change', 0, 6, list_of_MIDI_patches[6]],
156
+ ['patch_change', 0, 7, list_of_MIDI_patches[7]],
157
+ ['patch_change', 0, 8, list_of_MIDI_patches[8]],
158
+ ['patch_change', 0, 9, list_of_MIDI_patches[9]],
159
+ ['patch_change', 0, 10, list_of_MIDI_patches[10]],
160
+ ['patch_change', 0, 11, list_of_MIDI_patches[11]],
161
+ ['patch_change', 0, 12, list_of_MIDI_patches[12]],
162
+ ['patch_change', 0, 13, list_of_MIDI_patches[13]],
163
+ ['patch_change', 0, 14, list_of_MIDI_patches[14]],
164
+ ['patch_change', 0, 15, list_of_MIDI_patches[15]],
165
+ ['track_name', 0, bytes(track_name, text_encoding)]]
166
+
167
+ output = output_header + [patch_list + song_f]
168
+
169
+ with open(f"MIDI-Search-Sample.mid", 'wb') as f:
170
+ f.write(MIDI.score2midi(output))
171
+ audio = synthesis(MIDI.score2opus(output), soundfont_path)
172
+ yield AUX_DATA[search_index][0], "MIDI-Search-Sample.mid", (44100, audio), plt
173
+
174
+ #==========================================================================================================
175
+
176
+ if __name__ == "__main__":
177
+ parser = argparse.ArgumentParser()
178
+ parser.add_argument("--share", action="store_true", default=False, help="share gradio app")
179
+ parser.add_argument("--port", type=int, default=7860, help="gradio server port")
180
+ parser.add_argument("--max-gen", type=int, default=1024, help="max")
181
+
182
+ opt = parser.parse_args()
183
+
184
+ soundfont_path = "SGM-v2.01-YamahaGrand-Guit-Bass-v2.7.sf2"
185
+ meta_data_path = "Giant_Music_Transformer_Aux_Data.pickle"
186
+
187
+ print('Loading meta-data...')
188
+ with open(meta_data_path, 'rb') as f:
189
+ AUX_DATA = pickle.load(f)
190
+ print('Done!')
191
+
192
+ app = gr.Blocks()
193
+ with app:
194
+ gr.Markdown("<h1 style='text-align: center; margin-bottom: 1rem'>Advanced MIDI Search</h1>")
195
+ gr.Markdown("<h1 style='text-align: center; margin-bottom: 1rem'>Search and explore 179k+ MIDI titles</h1>")
196
+
197
+ gr.Markdown("![Visitors](https://api.visitorbadge.io/api/visitors?path=asigalov61.MIDI-Search&style=flat)\n\n"
198
+ "Giant Music Transformer Aux Data Demo\n\n"
199
+ "Please see [Giant Music Transformer](https://github.com/asigalov61/Giant-Music-Transformer) for more information and features\n\n"
200
+ "[Open In Colab]"
201
+ "(https://colab.research.google.com/github/asigalov61/Giant-Music-Transformer/blob/main/Giant_Music_Transformer_TTM.ipynb)"
202
+ " for all features"
203
+ )
204
+
205
+ title = gr.Textbox(label="Desired Song Title", value="Family Guy")
206
+ artist = gr.Textbox(label="Desired Song Artist", value="TV Themes")
207
+ submit = gr.Button()
208
+
209
+ gr.Markdown("# Search results")
210
+
211
+ output_midi_seq = gr.Textbox(label="Found MIDI search title")
212
+ output_audio = gr.Audio(label="Output MIDI search sample audio", format="mp3", elem_id="midi_audio")
213
+ output_plot = gr.Plot(label="Output MIDI search sample plot")
214
+ output_midi = gr.File(label="Output MIDI search sample MIDI", file_types=[".mid"])
215
+
216
+ run_event = submit.click(find_midi, [title, artist],
217
+ [output_midi_seq, output_midi, output_audio, output_plot])
218
+
219
+ app.queue(1).launch(server_port=opt.port, share=opt.share, inbrowser=True)