Spaces:
Sleeping
Sleeping
File size: 9,346 Bytes
b43c9c0 7b78b2e b43c9c0 7b78b2e b43c9c0 7b78b2e b43c9c0 676e005 fb87fc1 b43c9c0 676e005 b43c9c0 7b78b2e b43c9c0 d3532ff b43c9c0 70973b0 0952d52 70973b0 676e005 b43c9c0 7b78b2e b43c9c0 d3532ff 3b01d4f 0952d52 d3532ff ecd57a7 d3532ff 352f237 2d9a4a0 e385b84 2d9a4a0 e385b84 2d9a4a0 e385b84 2d9a4a0 e385b84 2d9a4a0 d3532ff 573ea3b d3532ff 573ea3b d3532ff 2c613a5 b43c9c0 fd7b81e 2d9a4a0 b43c9c0 2d9a4a0 b43c9c0 66d8263 b43c9c0 a56e703 66d8263 2d9a4a0 66d8263 a56e703 b43c9c0 573ea3b 2d9a4a0 25992bf 573ea3b 2d9a4a0 b43c9c0 66d8263 f285af7 d052125 f285af7 2d9a4a0 b43c9c0 25992bf b43c9c0 25992bf 2dcaa7d 25992bf 2dcaa7d 25992bf 139b81a 25992bf b43c9c0 25992bf b43c9c0 66d8263 25992bf b43c9c0 e769851 b43c9c0 05bd45f 352f237 b43c9c0 37eebe7 b43c9c0 2d9a4a0 b43c9c0 7b78b2e b43c9c0 7b78b2e b43c9c0 dc2151f e4ed439 573ea3b dc2151f b43c9c0 573ea3b b43c9c0 00b11aa 66d8263 b43c9c0 d3532ff 2d9a4a0 fd7b81e 2d9a4a0 fd7b81e 676e005 b43c9c0 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 |
#==================================================================================
# https://huggingface.co/spaces/asigalov61/MIDI-Cores-Match
#==================================================================================
print('=' * 70)
print('MIDI Cores Match Gradio App')
print('=' * 70)
print('Loading core MIDI Cores Match modules...')
import os
import copy
import statistics
import random
from collections import Counter
import time as reqtime
import datetime
from pytz import timezone
import tqdm
print('=' * 70)
print('Loading main MIDI Cores Match modules...')
import TMIDIX
import numpy as np
from midi_to_colab_audio import midi_to_colab_audio
from huggingface_hub import hf_hub_download
from datasets import load_dataset
import gradio as gr
print('=' * 70)
print('Loading aux MIDI Cores Match modules...')
import matplotlib.pyplot as plt
print('=' * 70)
print('Done!')
print('Enjoy! :)')
print('=' * 70)
#==================================================================================
SOUDFONT_PATH = 'SGM-v2.01-YamahaGrand-Guit-Bass-v2.7.sf2'
#==================================================================================
print('=' * 70)
midi_cores_dataset = load_dataset("asigalov61/MIDI-Cores")
print('=' * 70)
print('Prepping MIDI cores data...')
print('=' * 70)
all_core_chords = np.array(midi_cores_dataset['train']['core_chords'])
print('=' * 70)
print('Done!')
print('=' * 70)
#==================================================================================
def load_midi(midi_file):
print('Loading MIDI...')
raw_score = TMIDIX.midi2single_track_ms_score(midi_file)
escore_notes = TMIDIX.advanced_score_processor(raw_score, return_enhanced_score_notes=True)[0]
escore_notes = [e for e in escore_notes if e[6] < 80 or e[6] == 128]
escore_notes = TMIDIX.augment_enhanced_score_notes(escore_notes, sort_drums_last=True)
zscore = TMIDIX.recalculate_score_timings(escore_notes)
core_score, core_chords = TMIDIX.escore_notes_core(zscore)
print('Done!')
print('=' * 70)
print('MIDI has', len(core_chords), 'chords')
print('=' * 70)
return core_chords
#==================================================================================
def find_max_exact_match(src, trg):
"""
Find the row in the 2D trg array that has the maximum number of exact element matches with the 1D src array.
Parameters:
src (numpy.ndarray): 1D source array.
trg (numpy.ndarray): 2D target array.
Returns:
tuple: A tuple containing:
- int: Index of the row in trg with the maximum number of exact matches.
- int: Number of matched elements in that row.
"""
# Compare src with each row in trg and count exact matches
match_counts = np.sum(trg == src, axis=1)
# Find the index of the row with the maximum number of matches
max_match_idx = np.argmax(match_counts)
# Get the number of matched elements in the best-matching row
num_matched_elements = match_counts[max_match_idx]
return max_match_idx, num_matched_elements
#==================================================================================
def Match_Cores(input_midi):
#===============================================================================
print('=' * 70)
print('Req start time: {:%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now(PDT)))
start_time = reqtime.time()
print('=' * 70)
fn = os.path.basename(input_midi)
fn1 = fn.split('.')[0]
print('=' * 70)
print('Requested settings:')
print('=' * 70)
print('Input MIDI file name:', fn)
print('=' * 70)
#===============================================================================
src_core_chords = load_midi(input_midi.name)
#===============================================================================
print('Matching MIDI cores...')
print('=' * 70)
match_idx, num_matches = find_max_exact_match(src_core_chords, all_core_chords)
print('MAX MATCH IDX', match_idx)
print('NUM MATCHES', num_matches)
print('=' * 70)
print('Done!')
print('=' * 70)
#===============================================================================
print('Creating final MIDI score...')
final_core_score = midi_cores_dataset['train'][int(match_idx)]['core_score']
print('Done!')
print('=' * 70)
#===============================================================================
print('Rendering results...')
print('=' * 70)
print('Sample INTs', final_core_score[:15])
print('=' * 70)
song_f = []
if len(final_core_score) != 0:
time = 0
dur = 0
vel = 90
pitch = 0
channel = 0
patch = 0
for m in final_core_score:
if 0 <= m < 256:
time += m
elif 256 < m < 512:
dur = (m-256)
elif 512 <= m <= 640:
pat = (m-512)
elif 640 < m < 768:
ptc = (m-640)
elif 768 <= m <= 896:
vel = (m-768)
if pat != 128:
cha = pat // 8
if cha == 9:
cha += 1
else:
cha = 9
song_f.append(['note', time, dur, cha, ptc, vel, pat])
output_score, patches, overflow_patches = TMIDIX.patch_enhanced_score_notes(song_f)
fn1 = "MIDI-Cores-Match-Composition"
detailed_stats = TMIDIX.Tegridy_ms_SONG_to_MIDI_Converter(output_score,
output_signature = 'MIDI Cores Match',
output_file_name = fn1,
track_name='Project Los Angeles',
list_of_MIDI_patches=patches,
timings_multiplier=16
)
new_fn = fn1+'.mid'
audio = midi_to_colab_audio(new_fn,
soundfont_path=SOUDFONT_PATH,
sample_rate=16000,
volume_scale=10,
output_for_gradio=True
)
print('Done!')
print('=' * 70)
#========================================================
output_midi = str(new_fn)
output_audio = (16000, audio)
output_plot = TMIDIX.plot_ms_SONG(output_score,
plot_title=output_midi,
timings_multiplier=16,
return_plt=True
)
print('Output MIDI file name:', output_midi)
print('=' * 70)
#========================================================
print('-' * 70)
print('Req end time: {:%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now(PDT)))
print('-' * 70)
print('Req execution time:', (reqtime.time() - start_time), 'sec')
return output_audio, output_plot, output_midi
#==================================================================================
PDT = timezone('US/Pacific')
print('=' * 70)
print('App start time: {:%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now(PDT)))
print('=' * 70)
#==================================================================================
with gr.Blocks() as demo:
#==================================================================================
gr.Markdown("<h1 style='text-align: center; margin-bottom: 1rem'>MIDI Cores Match</h1>")
gr.Markdown("<h1 style='text-align: center; margin-bottom: 1rem'>Match MIDI cores</h1>")
gr.HTML("""
<p>
<a href="https://huggingface.co/spaces/asigalov61/MIDI-Cores-Match?duplicate=true">
<img src="https://huggingface.co/datasets/huggingface/badges/resolve/main/duplicate-this-space-md.svg" alt="Duplicate in Hugging Face">
</a>
</p>
""")
#==================================================================================
gr.Markdown("## Upload source MIDI")
gr.Markdown("### Source MIDI must have at least 128 chords")
input_midi = gr.File(label="Input MIDI", file_types=[".midi", ".mid", ".kar"])
mix_btn = gr.Button("Match", variant="primary")
gr.Markdown("## Mixing results")
output_audio = gr.Audio(label="MIDI audio", format="wav", elem_id="midi_audio")
output_plot = gr.Plot(label="MIDI score plot")
output_midi = gr.File(label="MIDI file", file_types=[".mid"])
mix_btn.click(Match_Cores,
[input_midi,
],
[output_audio,
output_plot,
output_midi
]
)
#==================================================================================
demo.launch()
#================================================================================== |