asigalov61 commited on
Commit
dc2151f
·
verified ·
1 Parent(s): f051b30

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +81 -70
app.py CHANGED
@@ -49,84 +49,91 @@ SOUDFONT_PATH = 'SGM-v2.01-YamahaGrand-Guit-Bass-v2.7.sf2'
49
 
50
  #==================================================================================
51
 
52
- print('Loading MIDI Loops Small Dataset...')
53
- print('=' * 70)
54
-
55
- midi_loops_dataset = pickle.load(open(hf_hub_download(repo_id='asigalov61/MIDI-Loops',
56
- filename='MIDI_Loops_Processed_Dataset_116909_Loops_CC_BY_NC_SA.pickle',
57
- repo_type='dataset'
58
- ),
59
- 'rb')
60
- )
61
-
62
- print('=' * 70)
63
- print('Done!')
64
- print('=' * 70)
65
- print('Loaded', len(midi_loops_dataset), 'MIDI Loops')
 
 
 
 
 
 
 
 
 
 
 
 
 
66
 
67
  #==================================================================================
68
 
69
- def find_matches(src_array, trg_array):
70
-
71
- matches = np.all(src_array == trg_array, axis=1)
72
- matching_indices = np.where(matches)[0]
73
-
74
- return matching_indices.tolist()
75
-
76
- #==================================================================================
77
-
78
- def find_closest_tuple(tuples_list, src_tuple):
79
-
80
- def euclidean_distance(t1, t2):
81
- return sum((a - b) ** 2 for a, b in zip(t1, t2)) ** 0.5
82
-
83
- closest_tuple = None
84
- min_distance = float('inf')
85
-
86
- for t in tuples_list:
87
- distance = euclidean_distance(t, src_tuple)
88
- if distance < min_distance:
89
- min_distance = distance
90
- closest_tuple = t
91
-
92
- return closest_tuple
93
 
94
  #==================================================================================
95
 
96
- def find_best_midx(midi_loops, midxs, trg_midx):
97
-
98
- all_midxs = midxs + [trg_midx]
99
- sidxs = [int(midx // 6) for midx in all_midxs]
100
-
101
- times_durs = []
102
-
103
- for sidx in sidxs:
104
-
105
- score = midi_loops[sidx][2]
106
- dtimes = [e[0] for e in score if e[0] != 0]
107
- durs = [e[1] for e in score]
108
-
109
- avg_dtime = int(sum(dtimes) / len(dtimes))
110
- avg_dur = int(sum(durs) / len(durs))
111
-
112
- mode_dtimes= statistics.mode(dtimes)
113
- mode_durs = statistics.mode(durs)
114
-
115
- times_durs.append([avg_dtime, avg_dur, mode_dtimes, mode_durs])
116
-
117
- best_time_dur = find_closest_tuple(times_durs[:-1], times_durs[-1])
118
-
119
- best_midx = midxs[times_durs.index(best_time_dur)]
120
-
121
- return best_midx
 
 
 
 
 
 
 
 
 
 
122
 
123
  #==================================================================================
124
 
125
- def Mix_Loops(max_num_loops,
126
- comp_loops_mult,
127
- chords_chunks_len,
128
- loops_chords_set_len
129
- ):
130
 
131
  #===============================================================================
132
 
@@ -456,6 +463,10 @@ with gr.Blocks() as demo:
456
  """)
457
 
458
  #==================================================================================
 
 
 
 
459
 
460
  gr.Markdown("## Mixing options")
461
 
@@ -473,7 +484,7 @@ with gr.Blocks() as demo:
473
  output_plot = gr.Plot(label="MIDI score plot")
474
  output_midi = gr.File(label="MIDI file", file_types=[".mid"])
475
 
476
- mix_btn.click(Mix_Loops,
477
  [
478
  max_num_loops,
479
  comp_loops_mult,
 
49
 
50
  #==================================================================================
51
 
52
+ def split_by_sublist(main_list, sublist):
53
+
54
+ n = len(sublist)
55
+ indices = []
56
+ result = []
57
+
58
+ # Find all starting indices where sublist matches main_list
59
+ for i in range(len(main_list) - n + 1):
60
+ if main_list[i:i + n] == sublist:
61
+ indices.append(i)
62
+
63
+ if not indices:
64
+ # If the sublist is not found, return the entire main_list as one segment
65
+ return [main_list]
66
+
67
+ # Use the indices to split the main_list
68
+ for idx, start_index in enumerate(indices):
69
+ if idx < len(indices) - 1:
70
+ # Slice from current sublist start to the next sublist start
71
+ end_index = indices[idx + 1]
72
+ segment = main_list[start_index:end_index]
73
+ else:
74
+ # For the last sublist occurrence, include the rest of the list
75
+ segment = main_list[start_index:]
76
+ result.append(segment)
77
+
78
+ return result
79
 
80
  #==================================================================================
81
 
82
+ def fuzzy_find(values, threshold=256):
83
+ result = set()
84
+ for i in range(len(values)):
85
+ for j in range(i+1, len(values)):
86
+ if abs(values[i] - values[j]) <= threshold:
87
+ result.add(values[j])
88
+ result.add(values[i])
89
+ return sorted(result)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90
 
91
  #==================================================================================
92
 
93
+ def kmp_search(main_list, sublist):
94
+ if not sublist:
95
+ return -1, -1 # Sublist is empty
96
+
97
+ # Preprocess the sublist to get the longest prefix-suffix array
98
+ lps = [0] * len(sublist)
99
+ length = 0 # Length of the previous longest prefix suffix
100
+ i = 1
101
+ while i < len(sublist):
102
+ if sublist[i] == sublist[length]:
103
+ length += 1
104
+ lps[i] = length
105
+ i += 1
106
+ else:
107
+ if length != 0:
108
+ length = lps[length - 1]
109
+ else:
110
+ lps[i] = 0
111
+ i += 1
112
+
113
+ # Start the search
114
+ i = j = 0 # Index for main_list and sublist
115
+ while i < len(main_list):
116
+ if main_list[i] == sublist[j]:
117
+ i += 1
118
+ j += 1
119
+ if j == len(sublist):
120
+ start_index = i - j
121
+ end_index = i - 1
122
+ return start_index, end_index
123
+ elif i < len(main_list) and main_list[i] != sublist[j]:
124
+ if j != 0:
125
+ j = lps[j - 1]
126
+ else:
127
+ i += 1
128
+ return -1, -1 # Sublist not found
129
 
130
  #==================================================================================
131
 
132
+ def Remix_MIDI(max_num_loops,
133
+ comp_loops_mult,
134
+ chords_chunks_len,
135
+ loops_chords_set_len
136
+ ):
137
 
138
  #===============================================================================
139
 
 
463
  """)
464
 
465
  #==================================================================================
466
+
467
+ gr.Markdown("## Upload source MIDI or select an example MIDI below")
468
+
469
+ input_midi = gr.File(label="Input MIDI", file_types=[".midi", ".mid", ".kar"])
470
 
471
  gr.Markdown("## Mixing options")
472
 
 
484
  output_plot = gr.Plot(label="MIDI score plot")
485
  output_midi = gr.File(label="MIDI file", file_types=[".mid"])
486
 
487
+ mix_btn.click(Remix_MIDI,
488
  [
489
  max_num_loops,
490
  comp_loops_mult,