asigalov61 commited on
Commit
4543735
1 Parent(s): e0348d2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -42
app.py CHANGED
@@ -1,7 +1,7 @@
1
  import argparse
2
  import glob
3
  import os.path
4
-
5
  import time
6
  import datetime
7
  from pytz import timezone
@@ -28,57 +28,57 @@ in_space = os.getenv("SYSTEM") == "spaces"
28
 
29
  #==========================================================================================================
30
 
31
- def render_midi(search_string, render_options):
 
32
  print('=' * 70)
33
  print('Req start time: {:%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now(PDT)))
34
  start_time = time.time()
35
-
36
  print('=' * 70)
37
- print('Search string:', search_string)
38
- print('Search options:', search_options)
 
 
 
 
 
 
39
 
40
  print('=' * 70)
41
- print('Preparing to search...')
42
-
43
- #==================================================
44
-
45
- random.shuffle(AUX_DATA)
46
-
47
- search_data = []
48
-
49
- for A in AUX_DATA:
50
- data = ''
51
- if 'Titles' in search_options:
52
- data += A[1] + '\n\n'
53
- if 'Lyrics' in search_options:
54
- data += A[2] + '\n\n'
55
- if 'Summaries' in search_options:
56
- data += A[3] + '\n\n'
57
-
58
- search_data.append(data)
59
 
60
- print('Searching titles...Please wait...')
61
-
62
- search_match_data = TMIDIX.ascii_texts_search(search_data, search_string, deterministic_matching = True)
63
 
64
- search_match_text = search_match_data[0]
65
- search_match_ratio = search_match_data[1]
66
- search_match_index = search_data.index(search_match_text)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67
 
68
  print('Done!')
69
  print('=' * 70)
70
- print('Search match ratio:', search_match_ratio)
71
- print('Selected file/title:', AUX_DATA[search_match_index][:2])
72
  print('=' * 70)
73
 
74
- fn = AUX_DATA[search_match_index][0]
75
- title = AUX_DATA[search_match_index][1]
76
- lyric = AUX_DATA[search_match_index][2]
77
- summary = AUX_DATA[search_match_index][3]
78
- raw_score = AUX_DATA[search_match_index][4]
79
- single_track_score_notes = TMIDIX.advanced_score_processor(raw_score,
80
- return_score_analysis=False,
81
- return_enhanced_score_notes=True)[0]
82
 
83
  print('Sample INTs', raw_score[1][:5])
84
  print('=' * 70)
@@ -92,7 +92,7 @@ def render_midi(search_string, render_options):
92
  'gray', 'white', 'gold', 'silver',
93
  'lightgreen', 'indigo', 'maroon', 'turquoise']
94
 
95
- for s in single_track_score_notes:
96
  x.append(s[1])
97
  y.append(s[4])
98
  c.append(colors[s[3]])
@@ -103,14 +103,14 @@ def render_midi(search_string, render_options):
103
  ax.set_facecolor('black')
104
 
105
  plt.scatter(x,y, s=10, c=c)
106
- plt.xlabel("Time in MIDI ticks")
107
  plt.ylabel("MIDI Pitch")
108
 
109
  with open(fn+'.mid', 'wb') as f:
110
  f.write(TMIDIX.score2midi(raw_score))
111
 
112
  audio = midi_to_colab_audio(fn+'.mid',
113
- soundfont_path=soundfont_path,
114
  sample_rate=16000, # 44100
115
  volume_scale=10,
116
  output_for_gradio=True
 
1
  import argparse
2
  import glob
3
  import os.path
4
+ import hashlib
5
  import time
6
  import datetime
7
  from pytz import timezone
 
28
 
29
  #==========================================================================================================
30
 
31
+ def render_midi(input_midi, render_options):
32
+
33
  print('=' * 70)
34
  print('Req start time: {:%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now(PDT)))
35
  start_time = time.time()
 
36
  print('=' * 70)
37
+ print('Loading MIDI...')
38
+
39
+ fn = os.path.basename(input_midi)
40
+ fn1 = fn.split('.')[-2]
41
+
42
+ fdata = open(input_midi, 'rb').read()
43
+
44
+ input_midi_md5hash = hashlib.md5(fdata).hexdigest()
45
 
46
  print('=' * 70)
47
+ print('Input MIDI file name:', fn)
48
+ print('Input MIDI md5 hash', input_midi_md5hash)
49
+ print('Render options:', render_options)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50
 
51
+ print('=' * 70)
52
+ print('Processing MIDI...Please wait...')
 
53
 
54
+ #=======================================================
55
+ # START PROCESSING
56
+
57
+ raw_score = TMIDIX.midi2single_track_ms_score(fdata, recalculate_channels=False)
58
+
59
+ escore = TMIDIX.advanced_score_processor(raw_score, return_score_analysis=False, return_enhanced_score_notes=True)[0]
60
+
61
+ first_note_index = raw_score[1].index(escore[0][:6])
62
+
63
+ for e in escore:
64
+ e[1] = int(e[1] / 16)
65
+ e[2] = int(e[2] / 16)
66
+
67
+ # Sorting by patch, pitch, then by start-time
68
+
69
+ escore.sort(key=lambda x: x[6])
70
+ escore.sort(key=lambda x: x[4], reverse=True)
71
+ escore.sort(key=lambda x: x[1])
72
+
73
+ cscore = TMIDIX.chordify_score([1000, escore])
74
+
75
+ meta_data = raw_score[1][:first_note_index] + [escore[0]] + [escore[-1]] + [raw_score[1][-1]]
76
 
77
  print('Done!')
78
  print('=' * 70)
79
+ print('Input MIDI metadata:', meta_data)
 
80
  print('=' * 70)
81
 
 
 
 
 
 
 
 
 
82
 
83
  print('Sample INTs', raw_score[1][:5])
84
  print('=' * 70)
 
92
  'gray', 'white', 'gold', 'silver',
93
  'lightgreen', 'indigo', 'maroon', 'turquoise']
94
 
95
+ for s in escore:
96
  x.append(s[1])
97
  y.append(s[4])
98
  c.append(colors[s[3]])
 
103
  ax.set_facecolor('black')
104
 
105
  plt.scatter(x,y, s=10, c=c)
106
+ plt.xlabel("Time in ms")
107
  plt.ylabel("MIDI Pitch")
108
 
109
  with open(fn+'.mid', 'wb') as f:
110
  f.write(TMIDIX.score2midi(raw_score))
111
 
112
  audio = midi_to_colab_audio(fn+'.mid',
113
+ soundfont_path=soundfont[0],
114
  sample_rate=16000, # 44100
115
  volume_scale=10,
116
  output_for_gradio=True