asigalov61 commited on
Commit
ecd66de
1 Parent(s): 348b9d0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +74 -62
app.py CHANGED
@@ -14,6 +14,7 @@ import numpy as np
14
  import os
15
  import random
16
  from collections import Counter
 
17
  from tqdm import tqdm
18
 
19
  import TMIDIX
@@ -23,7 +24,7 @@ from midi_to_colab_audio import midi_to_colab_audio
23
  # =================================================================================================
24
 
25
  def Generate_Chords_Progression(total_song_length_in_chords_chunks,
26
- chords_chunks_memory_length,
27
  chord_time_step,
28
  merge_chords_notes,
29
  melody_MIDI_patch_number,
@@ -38,7 +39,7 @@ def Generate_Chords_Progression(total_song_length_in_chords_chunks,
38
  print('=' * 70)
39
  print('Requested settings:')
40
  print('Total song length in chords chunks:', total_song_length_in_chords_chunks)
41
- print('Chords chunks memory length:', chords_chunks_memory_length)
42
  print('Chord time step:', chord_time_step)
43
  print('Merge chords notes max time:', merge_chords_notes)
44
  print('Melody MIDI patch number:', melody_MIDI_patch_number)
@@ -58,66 +59,77 @@ def Generate_Chords_Progression(total_song_length_in_chords_chunks,
58
  print('Generating...')
59
  print('=' * 70)
60
 
 
 
 
 
 
 
 
 
 
 
 
 
61
  matching_long_chords_chunks = []
62
-
63
  ridx = random.randint(0, len(all_long_chords_tokens_chunks)-1)
64
-
65
  matching_long_chords_chunks.append(ridx)
66
-
67
- max_song_len = 0
68
-
69
- tries = 0
70
-
71
- while len(matching_long_chords_chunks) < total_song_length_in_chords_chunks:
72
-
73
- matching_long_chords_chunks = []
74
-
75
- ridx = random.randint(0, len(all_long_chords_tokens_chunks)-1)
76
-
77
- matching_long_chords_chunks.append(ridx)
78
- seen = [ridx]
79
-
80
- for a in range(16):
81
-
82
- schunk = all_long_chords_tokens_chunks[matching_long_chords_chunks[-1]]
83
- trg_long_chunk = np.array(schunk[-chunk_size:])
84
- idxs = np.where((src_long_chunks == trg_long_chunk).all(axis=1))[0].tolist()
85
-
86
- if len(idxs) > 1:
87
- eidx = random.choice(idxs)
88
- tr = 0
89
- while eidx in seen and tr < 5:
90
- eidx = random.choice(idxs)
91
- tr += 1
92
-
93
- if eidx not in seen:
94
-
95
- matching_long_chords_chunks.append(eidx)
96
- seen.append(eidx)
97
-
98
- if chords_chunks_memory_length > 0:
99
- seen = seen[-chords_chunks_memory_length:]
100
- elif chords_chunks_memory_length == 0:
101
- seen = []
102
-
103
- else:
104
- break
105
-
106
- else:
107
- break
108
-
109
- if len(matching_long_chords_chunks) > max_song_len:
110
- print('Current song length:', len(matching_long_chords_chunks), 'chords chunks')
111
- print('=' * 70)
112
- final_song = matching_long_chords_chunks
113
-
114
- max_song_len = max(max_song_len, len(matching_long_chords_chunks))
115
-
116
- tries += 1
117
-
118
- if tries % 500 == 0:
119
- print('Number of passed tries:', tries)
120
- print('=' * 70)
121
 
122
  if len(matching_long_chords_chunks) > max_song_len:
123
  print(len(matching_long_chords_chunks))
@@ -339,8 +351,8 @@ if __name__ == "__main__":
339
 
340
  gr.Markdown("## Select generation options")
341
 
342
- total_song_length_in_chords_chunks = gr.Slider(4, 10, value=8, step=1, label="Total song length in chords chunks")
343
- chords_chunks_memory_length = gr.Slider(-1, 12, value=-1, step=1, label="Chords chunks memory length")
344
  chord_time_step = gr.Slider(100, 1000, value=500, step=50, label="Chord time step")
345
  merge_chords_notes = gr.Slider(0, 4000, value=1000, step=100, label="Merged chords notes max time")
346
  melody_MIDI_patch_number = gr.Slider(0, 127, value=40, step=1, label="Melody MIDI patch number")
@@ -359,7 +371,7 @@ if __name__ == "__main__":
359
 
360
  run_event = run_btn.click(Generate_Chords_Progression,
361
  [total_song_length_in_chords_chunks,
362
- chords_chunks_memory_length,
363
  chord_time_step,
364
  merge_chords_notes,
365
  melody_MIDI_patch_number,
 
14
  import os
15
  import random
16
  from collections import Counter
17
+ import math
18
  from tqdm import tqdm
19
 
20
  import TMIDIX
 
24
  # =================================================================================================
25
 
26
  def Generate_Chords_Progression(total_song_length_in_chords_chunks,
27
+ chords_chunks_memory_ratio,
28
  chord_time_step,
29
  merge_chords_notes,
30
  melody_MIDI_patch_number,
 
39
  print('=' * 70)
40
  print('Requested settings:')
41
  print('Total song length in chords chunks:', total_song_length_in_chords_chunks)
42
+ print('Chords chunks memory ratio:', chords_chunks_memory_ratio)
43
  print('Chord time step:', chord_time_step)
44
  print('Merge chords notes max time:', merge_chords_notes)
45
  print('Melody MIDI patch number:', melody_MIDI_patch_number)
 
59
  print('Generating...')
60
  print('=' * 70)
61
 
62
+ matching_long_chords_chunks = []
63
+
64
+ ridx = random.randint(0, len(all_long_chords_tokens_chunks)-1)
65
+
66
+ matching_long_chords_chunks.append(ridx)
67
+
68
+ max_song_len = 0
69
+
70
+ tries = 0
71
+
72
+ while len(matching_long_chords_chunks) < minimum_song_length_in_chords_chunks:
73
+
74
  matching_long_chords_chunks = []
75
+
76
  ridx = random.randint(0, len(all_long_chords_tokens_chunks)-1)
77
+
78
  matching_long_chords_chunks.append(ridx)
79
+ seen = [ridx]
80
+ gseen = [ridx]
81
+
82
+ for a in range(minimum_song_length_in_chords_chunks * 10):
83
+
84
+ if not matching_long_chords_chunks:
85
+ break
86
+
87
+ if len(matching_long_chords_chunks) > minimum_song_length_in_chords_chunks:
88
+ break
89
+
90
+ schunk = all_long_chords_tokens_chunks[matching_long_chords_chunks[-1]]
91
+ trg_long_chunk = np.array(schunk[-chunk_size:])
92
+ idxs = np.where((src_long_chunks == trg_long_chunk).all(axis=1))[0].tolist()
93
+
94
+ if len(idxs) > 1:
95
+
96
+ random.shuffle(idxs)
97
+
98
+ eidxs = [i for i in idxs if i not in seen]
99
+
100
+ if eidxs:
101
+ eidx = eidxs[0]
102
+ matching_long_chords_chunks.append(eidx)
103
+ seen.append(eidx)
104
+ gseen.append(eidx)
105
+
106
+ if 0 < chords_chunks_memory_ratio < 1:
107
+ seen = random.choices(gseen, k=math.ceil(len(gseen) * chords_chunks_memory_ratio))
108
+ elif chords_chunks_memory_ratio == 0:
109
+ seen = []
110
+
111
+ else:
112
+ gseen.pop()
113
+ matching_long_chords_chunks.pop()
114
+
115
+ else:
116
+ gseen.pop()
117
+ matching_long_chords_chunks.pop()
118
+
119
+
120
+ if len(matching_long_chords_chunks) > max_song_len:
121
+ print('Current song length:', len(matching_long_chords_chunks), 'chords chunks')
122
+ print('=' * 70)
123
+ final_song = matching_long_chords_chunks
124
+
125
+ max_song_len = max(max_song_len, len(matching_long_chords_chunks))
126
+
127
+ tries += 1
128
+
129
+ if tries % 500 == 0:
130
+ print('Number of passed tries:', tries)
131
+ print('=' * 70)
132
+
 
133
 
134
  if len(matching_long_chords_chunks) > max_song_len:
135
  print(len(matching_long_chords_chunks))
 
351
 
352
  gr.Markdown("## Select generation options")
353
 
354
+ total_song_length_in_chords_chunks = gr.Slider(4, 60, value=30, step=1, label="Total song length in chords chunks")
355
+ chords_chunks_memory_ratio = gr.Slider(0, 1, value=-1, step=0.1, label="Chords chunks memory ratio")
356
  chord_time_step = gr.Slider(100, 1000, value=500, step=50, label="Chord time step")
357
  merge_chords_notes = gr.Slider(0, 4000, value=1000, step=100, label="Merged chords notes max time")
358
  melody_MIDI_patch_number = gr.Slider(0, 127, value=40, step=1, label="Melody MIDI patch number")
 
371
 
372
  run_event = run_btn.click(Generate_Chords_Progression,
373
  [total_song_length_in_chords_chunks,
374
+ chords_chunks_memory_ratio,
375
  chord_time_step,
376
  merge_chords_notes,
377
  melody_MIDI_patch_number,