File size: 6,478 Bytes
3888ab7
 
 
 
660b28d
 
800d0d0
660b28d
3888ab7
 
2278710
3888ab7
 
 
a2c6cfa
 
29090fa
6df655f
 
 
 
 
06d58f4
 
6df655f
 
3888ab7
 
6df655f
 
64e309b
986e4d1
660b28d
 
 
 
 
 
 
6df655f
64e309b
6df655f
 
 
2044878
6df655f
64e309b
6df655f
cf7d19c
d26b0ba
64e309b
d26b0ba
64e309b
d26b0ba
64e309b
d26b0ba
64e309b
 
6df655f
64e309b
6df655f
2044878
281d949
d26b0ba
 
 
6df655f
 
 
660b28d
d26b0ba
cf7d19c
6df655f
d26b0ba
b01de93
 
 
d26b0ba
 
 
 
6df655f
03cc79b
6df655f
d26b0ba
6df655f
 
 
 
 
 
e673413
6df655f
 
d26b0ba
6df655f
 
 
 
 
 
b01de93
e673413
6df655f
660b28d
5c6968d
 
91d5e8a
d26b0ba
 
660b28d
d26b0ba
 
 
 
e476fc7
d26b0ba
660b28d
 
 
 
 
a04859d
6df655f
 
 
3888ab7
800d0d0
 
 
 
 
 
 
3888ab7
 
 
eec4853
3888ab7
eec4853
a04859d
3888ab7
 
 
a04859d
 
e30e402
a04859d
03cc79b
 
3888ab7
03cc79b
988b6a3
a04859d
 
 
 
86dfaf8
3adc64e
a04859d
660b28d
a04859d
 
 
 
 
986e4d1
a04859d
c04453c
64e309b
a04859d
d85cb0d
7dd6e93
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
import argparse
import glob
import os.path

import time
import datetime
from pytz import timezone

import gradio as gr

import pickle
import tqdm
import json

import TMIDIX
from midi_to_colab_audio import midi_to_colab_audio

import copy
from collections import Counter
import random
import statistics

import matplotlib.pyplot as plt

#==========================================================================================================

in_space = os.getenv("SYSTEM") == "spaces"

#==========================================================================================================

def find_midi(search_string, search_options):
    print('=' * 70)
    print('Req start time: {:%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now(PDT)))
    start_time = time.time()
    
    print('=' * 70)
    print('Search string:', search_string)
    print('Search options:', search_options)
    
    print('=' * 70)
    print('Preparing to search...')
    
    #==================================================
    
    random.shuffle(AUX_DATA)
    
    search_data = []
    
    for A in AUX_DATA:
        data = ''
        if 'Titles' in search_options:
            data += A[1] + '\n\n'
        if 'Lyrics' in search_options:
            data += A[2] + '\n\n'
        if 'Summaries' in search_options:
            data += A[3] + '\n\n'

        search_data.append(data)
    
    print('Searching titles...Please wait...')
    
    search_match_data = TMIDIX.ascii_texts_search(search_data, search_string, deterministic_matching = True)
    
    search_match_text = search_match_data[0]
    search_match_ratio = search_match_data[1]
    search_match_index = search_data.index(search_match_text)
    
    print('Done!')
    print('=' * 70)
    print('Search match ratio:', search_match_ratio)
    print('Selected file/title:', AUX_DATA[search_match_index][:2])
    print('=' * 70)

    fn = AUX_DATA[search_match_index][0]
    title = AUX_DATA[search_match_index][1]
    lyric = AUX_DATA[search_match_index][2]
    summary = AUX_DATA[search_match_index][3]
    raw_score = AUX_DATA[search_match_index][4]
    single_track_score_notes = TMIDIX.advanced_score_processor(raw_score, 
                                                               return_score_analysis=False, 
                                                               return_enhanced_score_notes=True)[0]

    print('Sample INTs', raw_score[1][:5])
    print('=' * 70)
 
    x = []
    y = []
    c = []
    
    colors = ['red', 'yellow', 'green', 'cyan',
            'blue', 'pink', 'orange', 'purple',
            'gray', 'white', 'gold', 'silver',
            'lightgreen', 'indigo', 'maroon', 'turquoise']
    
    for s in single_track_score_notes:
        x.append(s[1])
        y.append(s[4])
        c.append(colors[s[3]])

    plt.close()
    plt.figure(figsize=(14,5))
    ax=plt.axes(title=title)
    ax.set_facecolor('black')
    
    plt.scatter(x,y, s=10, c=c)
    plt.xlabel("Time in MIDI ticks")
    plt.ylabel("MIDI Pitch")

    with open(fn+'.mid', 'wb') as f:
        f.write(TMIDIX.score2midi(raw_score))
    
    audio = midi_to_colab_audio(fn+'.mid', 
                        soundfont_path=soundfont_path, 
                        sample_rate=16000, # 44100
                        volume_scale=10,
                        output_for_gradio=True
                        )

    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')
    
    yield AUX_DATA[search_match_index][0], AUX_DATA[search_match_index][1], AUX_DATA[search_match_index][2], AUX_DATA[search_match_index][3],  fn+'.mid', (16000, audio), plt

#==========================================================================================================

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)

    parser = argparse.ArgumentParser()
    parser.add_argument("--share", action="store_true", default=False, help="share gradio app")
    parser.add_argument("--port", type=int, default=7860, help="gradio server port")
    
    opt = parser.parse_args()
    
    soundfonts = ["SGM-v2.01-YamahaGrand-Guit-Bass-v2.7.sf2", "Nice-Strings-PlusOrchestra-v1.6.sf2", "KBH-Real-Choir-V2.5.sf2"]

    app = gr.Blocks()
    with app:
        gr.Markdown("<h1 style='text-align: center; margin-bottom: 1rem'>Advanced MIDI Renderer</h1>")
        gr.Markdown("<h1 style='text-align: center; margin-bottom: 1rem'>Transform and render any MIDI</h1>")
        
        gr.Markdown("![Visitors](https://api.visitorbadge.io/api/visitors?path=asigalov61.Advanced-MIDI-Renderer&style=flat)\n\n"
                    "Los Angeles MIDI Dataset Demo\n\n"
                    "Please see [Los Angeles MIDI Dataset](https://github.com/asigalov61/Los-Angeles-MIDI-Dataset) for more information and features\n\n"
                    "[Open In Colab]"
                    "(https://colab.research.google.com/github/asigalov61/Los-Angeles-MIDI-Dataset/blob/main/Los_Angeles_MIDI_Dataset_Search_and_Explore.ipynb)"
                    " for all features\n\n"
                   )
        gr.Markdown("# Upload your MIDI")
        input_midi = gr.File(label="Input MIDI", file_types=[".midi", ".mid", ".kar"], type="binary")
        render_options = gr.Checkbox(["Render as-is", "Extract melody", "Transform"], value="Lyrics", label="MIDI render options")
        submit = gr.Button()

        gr.Markdown("# Render 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")
        output_audio = gr.Audio(label="Output MIDI audio", format="wav", elem_id="midi_audio")
        output_plot = gr.Plot(label="Output MIDI score plot")
        output_midi_lyric = gr.Textbox(label="Output Karaoke MIDI lyric")
        output_midi = gr.File(label="Output MIDI file", file_types=[".mid"])
        
        run_event = submit.click(find_midi, [search_string, search_options],
                                                  [output_midi_md5, output_midi_title, output_midi_lyric, output_midi_summary, output_midi, output_audio, output_plot])
        
    app.queue(1).launch(server_port=opt.port, share=opt.share, inbrowser=True)