Spaces:
Running
Running
#================================================================ | |
# https://huggingface.co/spaces/asigalov61/MIDI-Identification | |
#================================================================ | |
import os | |
import hashlib | |
import time | |
import datetime | |
from pytz import timezone | |
import copy | |
from collections import Counter | |
import random | |
import statistics | |
import gradio as gr | |
import TMIDIX | |
#========================================================================================================== | |
def ID_MIDI(input_midi, | |
render_type, | |
soundfont_bank, | |
render_sample_rate, | |
custom_render_patch, | |
render_align, | |
render_transpose_value, | |
render_transpose_to_C4, | |
render_output_as_solo_piano, | |
render_remove_drums | |
): | |
print('*' * 70) | |
print('Req start time: {:%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now(PDT))) | |
start_time = time.time() | |
print('=' * 70) | |
print('Loading MIDI...') | |
fn = os.path.basename(input_midi) | |
fn1 = fn.split('.')[0] | |
fdata = open(input_midi, 'rb').read() | |
input_midi_md5hash = hashlib.md5(fdata).hexdigest() | |
print('=' * 70) | |
print('Requested settings:') | |
print('=' * 70) | |
print('Input MIDI file name:', fn) | |
print('Input MIDI md5 hash', input_midi_md5hash) | |
print('-' * 70) | |
print('Render type:', render_type) | |
print('Soudnfont bank', soundfont_bank) | |
print('Audio render sample rate', render_sample_rate) | |
print('Custom MIDI render patch', custom_render_patch) | |
print('Align to bars:', render_align) | |
print('Transpose value:', render_transpose_value) | |
print('Transpose to C4', render_transpose_to_C4) | |
print('Output as Solo Piano', render_output_as_solo_piano) | |
print('Remove drums:', render_remove_drums) | |
print('=' * 70) | |
print('Processing MIDI...Please wait...') | |
#======================================================= | |
# START PROCESSING | |
raw_score = TMIDIX.midi2single_track_ms_score(fdata) | |
escore = TMIDIX.advanced_score_processor(raw_score, return_enhanced_score_notes=True)[0] | |
escore = TMIDIX.augment_enhanced_score_notes(escore, timings_divider=1) | |
first_note_index = [e[0] for e in raw_score[1]].index('note') | |
cscore = TMIDIX.chordify_score([1000, escore]) | |
meta_data = raw_score[1][:first_note_index] + [escore[0]] + [escore[-1]] + [raw_score[1][-1]] | |
print('Done!') | |
print('=' * 70) | |
print('Input MIDI metadata:', meta_data[:5]) | |
print('=' * 70) | |
print('Processing...Please wait...') | |
print('-' * 70) | |
new_md5_hash = hashlib.md5(open(new_fn,'rb').read()).hexdigest() | |
print('Done!') | |
print('=' * 70) | |
#======================================================== | |
output_midi_md5 = str(new_md5_hash) | |
output_midi_title = str(fn1) | |
output_midi_summary = str(meta_data) | |
output_midi = str(new_fn) | |
output_audio = (srate, audio) | |
output_plot = None | |
print('Output MIDI file name:', output_midi) | |
print('Output MIDI title:', output_midi_title) | |
print('Output MIDI hash:', output_midi_md5) | |
print('Output MIDI summary:', output_midi_summary[:5]) | |
print('=' * 70) | |
#======================================================== | |
print('Req end time: {:%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now(PDT))) | |
print('-' * 70) | |
print('Req execution time:', (time.time() - start_time), 'sec') | |
print('*' * 70) | |
#======================================================== | |
return output_midi_md5, output_midi_title, output_midi_summary, output_midi, output_audio, output_plot | |
#========================================================================================================== | |
if __name__ == "__main__": | |
PDT = timezone('US/Pacific') | |
print('=' * 70) | |
print('App start time: {:%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now(PDT))) | |
print('=' * 70) | |
app = gr.Blocks() | |
with app: | |
gr.Markdown("<h1 style='text-align: center; margin-bottom: 1rem'>MIDI Identification</h1>") | |
gr.Markdown("<h1 style='text-align: center; margin-bottom: 1rem'>Identify any MIDI</h1>") | |
gr.Markdown("This is a demo for tegridy-tools\n\n" | |
"Please see [tegridy-tools](https://github.com/asigalov61/tegridy-tools) GitHub repo for more information\n\n" | |
) | |
gr.Markdown("## Upload your MIDI") | |
input_midi = gr.File(label="Input MIDI", file_types=[".midi", ".mid", ".kar"], type="filepath") | |
submit = gr.Button("Render MIDI", variant="primary") | |
gr.Markdown("## MIDI identification results") | |
output_midi_md5 = gr.Textbox(label="Output MIDI md5 hash") | |
output_midi_title = gr.Textbox(label="Output MIDI title") | |
output_midi_summary = gr.Textbox(label="Output MIDI summary") | |
run_event = submit.click(ID_MIDI, [input_midi, | |
], | |
[output_midi_md5, | |
output_midi_title, | |
output_midi_summary, | |
]) | |
app.queue().launch() |