File size: 28,577 Bytes
e9642f9
 
294c6ec
743aa2c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6481a43
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6373391
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6481a43
eda97ee
 
 
 
d25994f
 
 
6481a43
 
 
 
6373391
 
d25994f
 
 
 
 
684555c
d25994f
 
6481a43
 
 
 
 
 
684555c
 
 
 
6373391
6481a43
 
743aa2c
6481a43
6373391
cfa70b3
6481a43
6373391
 
cfa70b3
6481a43
 
 
 
 
 
 
684555c
d25994f
 
684555c
d25994f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eda97ee
d25994f
 
 
 
5ef91f5
 
d25994f
 
 
 
 
 
 
 
 
 
 
 
5ef91f5
 
d25994f
 
 
 
 
 
 
 
 
 
6481a43
 
 
 
 
684555c
d25994f
684555c
 
 
 
6481a43
d25994f
 
 
 
 
 
 
 
6481a43
684555c
d25994f
 
6481a43
 
d25994f
 
0ede4e4
6481a43
 
 
d25994f
cb1e380
 
d25994f
cb1e380
 
 
d25994f
 
 
 
 
 
 
 
 
 
 
 
 
fe8c885
d25994f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cb1e380
d25994f
 
 
 
 
 
cb1e380
 
5ef91f5
cb1e380
 
d25994f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0ede4e4
6373391
6481a43
6373391
 
d25994f
 
 
 
 
6481a43
 
743aa2c
6373391
cfa70b3
d25994f
6373391
 
 
6481a43
 
 
 
 
6373391
6481a43
6373391
 
6481a43
6373391
cfa70b3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d25994f
cb1e380
6373391
6481a43
6373391
6481a43
 
 
cb1e380
 
6481a43
d25994f
0ede4e4
cb1e380
 
 
 
35fbbe7
cb1e380
 
 
 
 
 
 
 
6481a43
6373391
6481a43
 
 
 
 
cb1e380
 
6481a43
 
 
 
cb1e380
 
6481a43
 
 
 
 
cb1e380
6481a43
6373391
 
cb1e380
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81592e1
cb1e380
 
 
 
 
 
 
 
 
 
81592e1
cb1e380
 
 
 
6373391
c8667b5
6373391
 
 
 
 
 
 
 
 
 
 
 
743aa2c
1b97a00
 
 
743aa2c
1b97a00
6373391
cb1e380
 
 
 
 
 
 
 
6373391
 
 
 
 
 
 
 
 
 
d25994f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cfa70b3
 
6373391
 
 
 
 
cfa70b3
6373391
 
 
 
 
d25994f
6373391
cfa70b3
6373391
cfa70b3
6373391
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5825808
 
 
 
743aa2c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6373391
6481a43
 
 
 
 
294c6ec
 
 
 
 
 
 
 
 
 
743aa2c
294c6ec
 
 
 
 
 
 
 
 
 
 
 
743aa2c
294c6ec
 
 
 
 
 
 
 
 
6481a43
256bea9
294c6ec
 
 
 
 
 
 
 
256bea9
6481a43
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0b1d3cf
6481a43
 
256bea9
6481a43
 
 
 
 
 
256bea9
6481a43
 
294c6ec
256bea9
294c6ec
256bea9
294c6ec
256bea9
 
294c6ec
 
6481a43
 
 
 
 
 
 
1b97a00
 
 
 
 
 
 
 
 
 
294c6ec
6481a43
 
294c6ec
 
 
 
6481a43
 
294c6ec
 
 
 
9958d06
6481a43
256bea9
294c6ec
 
 
256bea9
6481a43
 
 
294c6ec
6481a43
 
 
1b97a00
6481a43
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
const MIDI_OUTPUT_BATCH_SIZE=4;
//Do not change MIDI_OUTPUT_BATCH_SIZE. It will be automatically replaced.

/**
 * 自动绕过 shadowRoot 的 querySelector
 * @param {string} selector - 要查询的 CSS 选择器
 * @returns {Element|null} - 匹配的元素或 null 如果未找到
 */
function deepQuerySelector(selector) {
  /**
   * 在指定的根元素或文档对象下深度查询元素
   * @param {Element|Document} root - 要开始搜索的根元素或文档对象
   * @param {string} selector - 要查询的 CSS 选择器
   * @returns {Element|null} - 匹配的元素或 null 如果未找到
   */
  function deepSearch(root, selector) {
    // 在当前根元素下查找
    let element = root.querySelector(selector);
    if (element) {
      return element;
    }

    // 如果未找到,递归检查 shadow DOM
    const shadowHosts = root.querySelectorAll('*');

    for (let i = 0; i < shadowHosts.length; i++) {
      const host = shadowHosts[i];

      // 检查当前元素是否有 shadowRoot
      if (host.shadowRoot) {
        element = deepSearch(host.shadowRoot, selector);
        if (element) {
          return element;
        }
      }
    }
    // 未找到元素
    return null;
  }

  return deepSearch(this, selector);
}

Element.prototype.deepQuerySelector = deepQuerySelector;
Document.prototype.deepQuerySelector = deepQuerySelector;

function gradioApp() {
    const elems = document.getElementsByTagName('gradio-app')
    const gradioShadowRoot = elems.length == 0 ? null : elems[0].shadowRoot
    return !!gradioShadowRoot ? gradioShadowRoot : document;
}

uiUpdateCallbacks = []
msgReceiveCallbacks = []

function onUiUpdate(callback){
    uiUpdateCallbacks.push(callback)
}

function onMsgReceive(callback){
    msgReceiveCallbacks.push(callback)
}

function runCallback(x, m){
    try {
        x(m)
    } catch (e) {
        (console.error || console.log).call(console, e.message, e);
    }
}
function executeCallbacks(queue, m) {
    queue.forEach(function(x){runCallback(x, m)})
}

document.addEventListener("DOMContentLoaded", function() {
    var mutationObserver = new MutationObserver(function(m){
        executeCallbacks(uiUpdateCallbacks, m);
    });
    mutationObserver.observe( gradioApp(), { childList:true, subtree:true })
});

function HSVtoRGB(h, s, v) {
    let r, g, b, i, f, p, q, t;
    i = Math.floor(h * 6);
    f = h * 6 - i;
    p = v * (1 - s);
    q = v * (1 - f * s);
    t = v * (1 - (1 - f) * s);
    switch (i % 6) {
        case 0: r = v; g = t; b = p; break;
        case 1: r = q; g = v; b = p; break;
        case 2: r = p; g = v; b = t; break;
        case 3: r = p; g = q; b = v; break;
        case 4: r = t; g = p; b = v; break;
        case 5: r = v; g = p; b = q; break;
    }
    return {
        r: Math.round(r * 255),
        g: Math.round(g * 255),
        b: Math.round(b * 255)
    };
}

function isMobile(){
  return /(iPhone|iPad|iPod|iOS|Android|Windows Phone)/i.test(navigator.userAgent);
}

const number2patch = ['Acoustic Grand', 'Bright Acoustic', 'Electric Grand', 'Honky-Tonk', 'Electric Piano 1', 'Electric Piano 2', 'Harpsichord', 'Clav', 'Celesta', 'Glockenspiel', 'Music Box', 'Vibraphone', 'Marimba', 'Xylophone', 'Tubular Bells', 'Dulcimer', 'Drawbar Organ', 'Percussive Organ', 'Rock Organ', 'Church Organ', 'Reed Organ', 'Accordion', 'Harmonica', 'Tango Accordion', 'Acoustic Guitar(nylon)', 'Acoustic Guitar(steel)', 'Electric Guitar(jazz)', 'Electric Guitar(clean)', 'Electric Guitar(muted)', 'Overdriven Guitar', 'Distortion Guitar', 'Guitar Harmonics', 'Acoustic Bass', 'Electric Bass(finger)', 'Electric Bass(pick)', 'Fretless Bass', 'Slap Bass 1', 'Slap Bass 2', 'Synth Bass 1', 'Synth Bass 2', 'Violin', 'Viola', 'Cello', 'Contrabass', 'Tremolo Strings', 'Pizzicato Strings', 'Orchestral Harp', 'Timpani', 'String Ensemble 1', 'String Ensemble 2', 'SynthStrings 1', 'SynthStrings 2', 'Choir Aahs', 'Voice Oohs', 'Synth Voice', 'Orchestra Hit', 'Trumpet', 'Trombone', 'Tuba', 'Muted Trumpet', 'French Horn', 'Brass Section', 'SynthBrass 1', 'SynthBrass 2', 'Soprano Sax', 'Alto Sax', 'Tenor Sax', 'Baritone Sax', 'Oboe', 'English Horn', 'Bassoon', 'Clarinet', 'Piccolo', 'Flute', 'Recorder', 'Pan Flute', 'Blown Bottle', 'Skakuhachi', 'Whistle', 'Ocarina', 'Lead 1 (square)', 'Lead 2 (sawtooth)', 'Lead 3 (calliope)', 'Lead 4 (chiff)', 'Lead 5 (charang)', 'Lead 6 (voice)', 'Lead 7 (fifths)', 'Lead 8 (bass+lead)', 'Pad 1 (new age)', 'Pad 2 (warm)', 'Pad 3 (polysynth)', 'Pad 4 (choir)', 'Pad 5 (bowed)', 'Pad 6 (metallic)', 'Pad 7 (halo)', 'Pad 8 (sweep)', 'FX 1 (rain)', 'FX 2 (soundtrack)', 'FX 3 (crystal)', 'FX 4 (atmosphere)', 'FX 5 (brightness)', 'FX 6 (goblins)', 'FX 7 (echoes)', 'FX 8 (sci-fi)', 'Sitar', 'Banjo', 'Shamisen', 'Koto', 'Kalimba', 'Bagpipe', 'Fiddle', 'Shanai', 'Tinkle Bell', 'Agogo', 'Steel Drums', 'Woodblock', 'Taiko Drum', 'Melodic Tom', 'Synth Drum', 'Reverse Cymbal', 'Guitar Fret Noise', 'Breath Noise', 'Seashore', 'Bird Tweet', 'Telephone Ring', 'Helicopter', 'Applause', 'Gunshot']
const number2drum_kits = {0: "Standard", 8: "Room", 16: "Power", 24: "Electric", 25: "TR-808", 32: "Jazz", 40: "Blush", 48: "Orchestra"}

class MidiVisualizer extends HTMLElement{
    constructor() {
        super();
        this.midiEvents = [];
        this.activeNotes = [];
        this.midiTimes = [];
        this.trackMap = new Map()
        this.patches = [];
        for (let i=0;i<16;i++){
            this.patches.push([[0,0]])
        }
        this.container = null;
        this.trackList = null
        this.pianoRoll = null;
        this.svg = null;
        this.timeLine = null;
        this.config = {
            noteHeight : 4,
            beatWidth: 32
        }
        if (isMobile()){
            this.config.noteHeight = 1;
            this.config.beatWidth = 16;
        }
        this.timePreBeat = 16
        this.svgWidth = 0;
        this.t1 = 0;
        this.totalTimeMs = 0
        this.playTime = 0
        this.playTimeMs = 0
        this.lastUpdateTime = 0
        this.colorMap = new Map();
        this.playing = false;
        this.timer = null;
        this.version = "v2"
        this.init();
    }

    init(){
        this.innerHTML=''
        const shadow = this.attachShadow({mode: 'open'});
        const style = document.createElement("style");
        style.textContent = ".note.active {stroke: black;stroke-width: 0.75;stroke-opacity: 0.75;}";
        const container = document.createElement('div');
        container.style.display="flex";
        container.style.height=`${this.config.noteHeight*128 + 25}px`;
        const trackListContainer = document.createElement('div');
        trackListContainer.style.width = "260px";
        trackListContainer.style.minWidth = "260px";
        trackListContainer.style.height = "100%";
        trackListContainer.style.display="flex";
        trackListContainer.style.flexDirection="column";
        const trackList = document.createElement('div');
        trackList.style.width = "100%";
        trackList.style.height = "100%";
        trackList.style.overflowY= "scroll";
        trackList.style.display="flex";
        trackList.style.flexDirection="column";
        trackList.style.flexGrow="1";
        const trackControls = document.createElement('div');
        trackControls.style.display="flex";
        trackControls.style.flexDirection="row";
        trackControls.style.width = "100%";
        trackControls.style.height = "50px";
        trackControls.style.minHeight = "50px";
        const allTrackBtn = document.createElement('button');
        allTrackBtn.textContent = "All";
        allTrackBtn.style.width = "50%";
        allTrackBtn.style.height = "100%";
        allTrackBtn.style.backgroundColor = "rgba(200, 200, 200, 0.3)";
        allTrackBtn.style.color = 'inherit';
        allTrackBtn.style.border = "none";
        allTrackBtn.style.cursor = 'pointer';
        let self = this;
        allTrackBtn.onclick = function (){
            self.trackMap.forEach((track, id) => {
                track.setChecked(true);
            })
        };
        const noneTrackBtn = document.createElement('button');
        noneTrackBtn.textContent = "None";
        noneTrackBtn.style.width = "50%";
        noneTrackBtn.style.height = "100%";
        noneTrackBtn.style.backgroundColor = "rgba(200, 200, 200, 0.3)";
        noneTrackBtn.style.color = 'inherit';
        noneTrackBtn.style.border = "none";
        noneTrackBtn.style.cursor = 'pointer';
        noneTrackBtn.onclick = function (){
            self.trackMap.forEach((track, id) => {
                track.setChecked(false);
            });
        };
        const pianoRoll = document.createElement('div');
        pianoRoll.style.overflowX= "scroll";
        pianoRoll.style.flexGrow="1";
        const svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
        svg.style.height = `${this.config.noteHeight*128}px`;
        svg.style.width = `${this.svgWidth}px`;
        const timeLine = document.createElementNS('http://www.w3.org/2000/svg', 'line');
        timeLine.style.stroke = "green"
        timeLine.style.strokeWidth = "2";

        if (isMobile()){
            trackListContainer.style.display = "none";
            timeLine.style.strokeWidth = "1";
        }
        shadow.appendChild(style)
        shadow.appendChild(container);
        container.appendChild(trackListContainer);
        trackListContainer.appendChild(trackList);
        trackListContainer.appendChild(trackControls);
        trackControls.appendChild(allTrackBtn);
        trackControls.appendChild(noneTrackBtn);
        container.appendChild(pianoRoll);
        pianoRoll.appendChild(svg);
        svg.appendChild(timeLine)
        this.container = container;
        this.trackList = trackList;
        this.pianoRoll = pianoRoll;
        this.svg = svg;
        this.timeLine= timeLine;
        for(let i = 0; i < 128 ; i++){
            this.colorMap.set(i, HSVtoRGB(i / 128, 1, 1))
        }
        this.setPlayTime(0);
    }

    addTrack(id, tr, cl, name, color){
        const track = {id, tr, cl, name, color, empty: true,
            lastCC: new Map(),
            instrument: cl===9?"Standard Drum":"Acoustic Grand",
            svg: document.createElementNS('http://www.w3.org/2000/svg', 'g'),
            ccPaths: new Map()
        }
        this.svg.appendChild(track.svg)
        const trackItem = this.createTrackItem(track);
        this.trackList.appendChild(trackItem);
        this.trackMap.set(id, track);
        return track;
    }

    getTrack(tr, cl){
        const id = tr * 16 + cl
        let track = this.trackMap.get(id)
        if (!!track){
            return track
        }
        let color = this.colorMap.get((this.trackMap.size*53)%128)
        return this.addTrack(id, tr, cl, `Track ${tr}, Channel ${cl}`, color)
    }

    createTrackItem(track) {
        const trackItem = document.createElement('div');
        trackItem.style.display = 'flex';
        trackItem.style.alignItems = 'center';
        trackItem.style.width = '100%';
        trackItem.style.position = 'relative';

        const colorBar = document.createElement('div');
        colorBar.style.width = '5%';
        colorBar.style.height = '100%';
        colorBar.style.position = 'absolute';
        colorBar.style.left = '0';
        colorBar.style.top = '0';
        let color = track.color;
        colorBar.style.backgroundColor = `rgb(${color.r}, ${color.g}, ${color.b})`;
        trackItem.appendChild(colorBar);

        const content = document.createElement('div');
        content.style.paddingLeft = '30px';
        content.style.flexGrow = '1';
        content.style.color = "grey"
        content.innerHTML = `<p>${track.name}<br>${track.instrument}</p>`;
        trackItem.appendChild(content);
        track.updateInstrument = function (instrument){
            track.instrument = instrument;
            content.innerHTML = `<p>${track.name}<br>${track.instrument}</p>`;
        }
        track.setEmpty = function (empty){
            if (empty!==track.empty){
                content.style.color = empty?"grey":"inherit";
            }
        }

        const toggleSwitch = document.createElement('input');
        toggleSwitch.type = 'checkbox';
        toggleSwitch.checked = true;
        toggleSwitch.style.marginLeft = 'auto';
        toggleSwitch.style.marginRight = '10px';
        toggleSwitch.style.width = '20px';
        toggleSwitch.style.height = '20px';
        toggleSwitch.style.cursor = 'pointer';

        toggleSwitch.onchange = function () {
            track.svg.setAttribute('visibility',toggleSwitch.checked? "visible" : "hidden")
        };
        track.setChecked = function (checked){
            toggleSwitch.checked = checked;
            track.svg.setAttribute('visibility',toggleSwitch.checked? "visible" : "hidden")
        }
        trackItem.appendChild(toggleSwitch);
        return trackItem;
    }

    clearMidiEvents(){
        this.pause()
        this.midiEvents = [];
        this.activeNotes = [];
        this.midiTimes = [];
        this.trackMap = new Map()
        this.patches = [];
        for (let i=0;i<16;i++){
            this.patches.push([[0,0]])
        }
        this.t1 = 0
        this.setPlayTime(0);
        this.totalTimeMs = 0;
        this.playTimeMs = 0
        this.lastUpdateTime = 0
        this.trackList.innerHTML = ''
        this.svgWidth = 0
        this.svg.innerHTML = ''
        this.svg.style.width = `${this.svgWidth}px`;
        this.svg.appendChild(this.timeLine)
    }

    appendMidiEvent(midiEvent){
        if(midiEvent instanceof Array && midiEvent.length > 0){

            this.t1 += midiEvent[1]
            let t = this.t1*this.timePreBeat + midiEvent[2]
            midiEvent = [midiEvent[0], t].concat(midiEvent.slice(3))
            if(midiEvent[0] === "note"){
                let track = midiEvent[2]
                let duration = 0
                let channel = 0
                let pitch = 0
                let velocity = 0
                if(this.version === "v1"){
                    duration = midiEvent[3]
                    channel = midiEvent[4]
                    pitch = midiEvent[5]
                    velocity = midiEvent[6]
                }else if (this.version === "v2"){
                    channel = midiEvent[3]
                    pitch = midiEvent[4]
                    velocity = midiEvent[5]
                    duration = midiEvent[6]
                }
                let vis_track = this.getTrack(track, channel);
                vis_track.setEmpty(false);
                let x = (t/this.timePreBeat)*this.config.beatWidth
                let y = (127 - pitch)*this.config.noteHeight
                let w = (duration/this.timePreBeat)*this.config.beatWidth
                let h = this.config.noteHeight
                this.svgWidth = Math.ceil(Math.max(x + w, this.svgWidth))
                let opacity = Math.min(1, velocity/127 + 0.1).toFixed(2)
                let rect = this.drawNote(vis_track, x,y,w,h, opacity)
                midiEvent.push(rect);
                this.setPlayTime(t);
                this.pianoRoll.scrollTo(this.svgWidth - this.pianoRoll.offsetWidth, this.pianoRoll.scrollTop)
            }else if(midiEvent[0] === "patch_change"){
                let track = midiEvent[2];
                let channel = midiEvent[3];
                this.patches[channel].push([t, midiEvent[4]]);
                this.patches[channel].sort((a, b) => a[0] - b[0]);
                this.getTrack(track, channel);
            }else if(midiEvent[0] === "control_change"){
                let track = midiEvent[2];
                let channel = midiEvent[3];
                let controller = midiEvent[4];
                let value = midiEvent[5];
                let vis_track = this.getTrack(track, channel);
                this.drawCC(vis_track, t, controller, value);
                this.setPlayTime(t);
            }
            this.midiEvents.push(midiEvent);
            this.svg.style.width = `${this.svgWidth}px`;
        }

    }

    drawNote(track, x, y, w, h, opacity) {
        if (!track.svg) {
          return null;
        }
        const rect = document.createElementNS('http://www.w3.org/2000/svg', 'rect');
        rect.classList.add('note');
        const color = track.color;
        rect.setAttribute('fill', `rgba(${color.r}, ${color.g}, ${color.b}, ${opacity})`);
        // Round values to the nearest integer to avoid partially filled pixels.
        rect.setAttribute('x', `${Math.round(x)}`);
        rect.setAttribute('y', `${Math.round(y)}`);
        rect.setAttribute('width', `${Math.round(w)}`);
        rect.setAttribute('height', `${Math.round(h)}`);
        track.svg.appendChild(rect);
        return rect
    }

    drawCC(track, t, controller, value){
        if (!track.svg) {
          return null;
        }
        let path = track.ccPaths.get(controller);
        let x = (t/this.timePreBeat)*this.config.beatWidth
        let y = (127 - value)*this.config.noteHeight
        if (!path){
            path = document.createElementNS('http://www.w3.org/2000/svg', 'path');
            path.setAttribute('visibility',"hidden");
            path.setAttribute('fill', "transparent");
            const color = track.color;
            path.setAttribute('stroke', `rgba(${color.r}, ${color.g}, ${color.b}, 0.6)`);
            path.setAttribute('stroke-width', "1");
            path.setAttribute('d',
                t===0?`M ${x} ${y}`:`M 0 ${127*this.config.noteHeight} H ${x} V ${y}`);
            track.svg.appendChild(path);
            track.ccPaths.set(controller, path);
            track.lastCC.set(controller, value);
            return path;
        }
        let lastVal = track.lastCC.get(controller);
        if(lastVal !== value){
            path.removeAttribute('visibility');
        }
        let d = path.getAttribute("d");
        d += `H ${x} V ${y}`
        path.setAttribute('d', d);
        return path
    }

    finishAppendMidiEvent(){
        this.pause()
        let midiEvents = this.midiEvents.sort((a, b)=>a[1]-b[1])
        let tempo = (60 / 120) * 10 ** 3
        let ms = 0
        let lastT = 0
        this.midiTimes.push({ms:ms, t: 0, tempo: tempo})
        midiEvents.forEach((midiEvent)=>{
            let t = midiEvent[1]
            ms += ((t- lastT) / this.timePreBeat) * tempo
            if(midiEvent[0]==="set_tempo"){
                tempo = (60 / midiEvent[3]) * 10 ** 3
                this.midiTimes.push({ms:ms, t: t, tempo: tempo})
            }
            if(midiEvent[0]==="note"){
                this.totalTimeMs = Math.max(this.totalTimeMs, ms + (midiEvent[3]/ this.timePreBeat)*tempo)
            }else{
                this.totalTimeMs = Math.max(this.totalTimeMs, ms);
            }
            lastT = t;
        })
        let x = (lastT/this.timePreBeat)*this.config.beatWidth;
        this.trackMap.forEach((track, id)=>{
            track.ccPaths.forEach((path, controller)=>{
                let d = path.getAttribute("d");
                d += `H ${x}`
                path.setAttribute('d', d);
            })
        })
    }

    setPlayTime(t){
        this.playTime = t
        let x = Math.round((t/this.timePreBeat)*this.config.beatWidth)
        this.timeLine.setAttribute('x1', `${x}`);
        this.timeLine.setAttribute('y1', '0');
        this.timeLine.setAttribute('x2', `${x}`);
        this.timeLine.setAttribute('y2', `${this.config.noteHeight*128}`);

        this.pianoRoll.scrollTo(Math.max(0, x - this.pianoRoll.offsetWidth/2), this.pianoRoll.scrollTop)

        this.trackMap.forEach((track, id)=>{
            let instrument = track.instrument
            let cl = track.cl;
            let patches = this.patches[cl]
            let p = 0
            for (let i = 0; i < patches.length ; i++){
                let tp = patches[i]
                if (t < tp[0])
                    break
                p = tp[1]
            }
            if (cl === 9){
                let drumKit = number2drum_kits[`${p}`];
                if (!!drumKit)
                    instrument = drumKit + " Drum";
            }else{
                instrument = number2patch[p]
            }
            if (instrument !== track.instrument)
                track.updateInstrument(instrument)
        });

        let dt = Date.now() - this.lastUpdateTime; // limit the update rate of ActiveNotes
        if(this.playing && dt > 50){
            let activeNotes = []
            this.removeActiveNotes(this.activeNotes)
            this.midiEvents.forEach((midiEvent)=>{
                if(midiEvent[0] === "note"){
                    let time = midiEvent[1]
                    let duration = this.version==="v1"? midiEvent[3]:midiEvent[6]
                    let note = midiEvent[midiEvent.length - 1]
                    if(time <=this.playTime && time+duration>= this.playTime){
                        activeNotes.push(note)
                    }
                }
            });
            this.addActiveNotes(activeNotes)
            this.lastUpdateTime = Date.now();
        }

    }

    setPlayTimeMs(ms){
        this.playTimeMs = ms
        let playTime = 0
        for(let i =0;i<this.midiTimes.length;i++){
            let midiTime = this.midiTimes[i]
            if(midiTime.ms>=ms){
                break;
            }
            playTime = midiTime.t + (ms-midiTime.ms) * this.timePreBeat / midiTime.tempo
        }
        this.setPlayTime(playTime)
    }

    addActiveNotes(notes){
        notes.forEach((note)=>{
            this.activeNotes.push(note)
            note.classList.add('active');
        });
    }

    removeActiveNotes(notes){
        notes.forEach((note)=>{
            let idx = this.activeNotes.indexOf(note)
            if(idx>-1)
                this.activeNotes.splice(idx, 1);
            note.classList.remove('active');
        });
    }

    play(){
        this.playing = true;
    }

    pause(){
        this.removeActiveNotes(this.activeNotes)
        this.playing = false;
    }


    bindAudioPlayer(audio){
        this.pause()
        audio.addEventListener("play", (event)=>{
            this.play()
        })
        audio.addEventListener("pause", (event)=>{
            this.pause()
        })
        audio.addEventListener("loadedmetadata", (event)=>{
            //I don't know why the calculated totalTimeMs is different from audio.duration*10**3
            this.totalTimeMs = audio.duration*10**3;
        })
    }

    bindWaveformCursor(cursor){
        let self = this;
        const callback = function(mutationsList, observer) {
            for(let mutation of mutationsList) {
                if (mutation.type === 'attributes' && mutation.attributeName === 'style') {
                    let progress = parseFloat(mutation.target.style.left.slice(0,-1))*0.01;
                    if(!isNaN(progress)){
                        self.setPlayTimeMs(progress*self.totalTimeMs);
                    }
                }
            }
        };
        const observer = new MutationObserver(callback);
        observer.observe(cursor, {
            attributes: true,
            attributeFilter: ['style']
        });
    }
}

customElements.define('midi-visualizer', MidiVisualizer);

(()=>{
    function midi_visualizer_setup(idx, midi_visualizer){
        let midi_visualizer_container_inited = null
        let midi_audio_audio_inited = null;
        let midi_audio_cursor_inited = null;
        onUiUpdate((m)=>{
            let app = gradioApp()
            let midi_visualizer_container = app.querySelector(`#midi_visualizer_container_${idx}`);
            if(!!midi_visualizer_container && midi_visualizer_container_inited!== midi_visualizer_container){
                midi_visualizer_container.appendChild(midi_visualizer)
                midi_visualizer_container_inited = midi_visualizer_container;
            }
            let midi_audio = app.querySelector(`#midi_audio_${idx}`);
            if (!!midi_audio){
                let midi_audio_cursor = midi_audio.deepQuerySelector(".cursor");
                if(!!midi_audio_cursor && midi_audio_cursor_inited!==midi_audio_cursor){
                    midi_visualizer.bindWaveformCursor(midi_audio_cursor)
                    midi_audio_cursor_inited = midi_audio_cursor
                }
                let midi_audio_audio = midi_audio.deepQuerySelector("audio");
                if(!!midi_audio_audio && midi_audio_audio_inited!==midi_audio_audio){
                    midi_visualizer.bindAudioPlayer(midi_audio_audio)
                    midi_audio_audio_inited = midi_audio_audio
                }
            }
        });
    }

    let midi_visualizers = []
    for (let i = 0; i < MIDI_OUTPUT_BATCH_SIZE ; i++){
        let midi_visualizer = document.createElement('midi-visualizer');
        midi_visualizers.push(midi_visualizer);
        midi_visualizer_setup(i, midi_visualizer)
    }

    let hasProgressBar = false;
    let output_tabs_inited = null;
    onUiUpdate((m)=>{
        let app = gradioApp()
        let output_tabs = app.querySelector("#output_tabs");
        if(!!output_tabs && output_tabs_inited!== output_tabs){
            output_tabs_inited = output_tabs;
        }
    });

    function createProgressBar(progressbarContainer){
        let parentProgressbar = progressbarContainer.parentNode;
        let divProgress = document.createElement('div');
        divProgress.className='progressDiv';
        let rect = progressbarContainer.getBoundingClientRect();
        divProgress.style.width = rect.width + "px";
        divProgress.style.background = "#b4c0cc";
        divProgress.style.borderRadius = "8px";
        let divInner = document.createElement('div');
        divInner.className='progress';
        divInner.style.color = "white";
        divInner.style.background = "#0060df";
        divInner.style.textAlign = "right";
        divInner.style.fontWeight = "bold";
        divInner.style.borderRadius = "8px";
        divInner.style.height = "20px";
        divInner.style.lineHeight = "20px";
        divInner.style.paddingRight = "8px"
        divInner.style.width = "0%";
        divProgress.appendChild(divInner);
        parentProgressbar.insertBefore(divProgress, progressbarContainer);
        hasProgressBar = true;
    }

    function removeProgressBar(progressbarContainer){
        let parentProgressbar = progressbarContainer.parentNode;
        let divProgress = parentProgressbar.querySelector(".progressDiv");
        parentProgressbar.removeChild(divProgress);
        hasProgressBar = false;
    }

    function setProgressBar(progress, total){
        if (!hasProgressBar)
            createProgressBar(output_tabs_inited)
        if (hasProgressBar && total === 0){
            removeProgressBar(output_tabs_inited)
            return
        }
        let parentProgressbar = output_tabs_inited.parentNode;
        // let divProgress = parentProgressbar.querySelector(".progressDiv");
        let divInner = parentProgressbar.querySelector(".progress");
        if(total===0)
            total = 1;
        divInner.style.width = `${(progress/total)*100}%`;
        divInner.textContent = `${progress}/${total}`;
    }

    onMsgReceive((msgs)=>{
        for(let msg of msgs){
            if(msg instanceof Array){
                msg.forEach((o)=>{handleMsg(o)});
            }else{
                handleMsg(msg);
            }
        }
    })
    function handleMsg(msg){
        let idx;
        switch (msg.name) {
            case "visualizer_clear":
                idx = msg.data[0];
                let ver = msg.data[1];
                midi_visualizers[idx].clearMidiEvents(false);
                midi_visualizers[idx].version = ver;
                break;
            case "visualizer_append":
                idx = msg.data[0];
                let events = msg.data[1];
                events.forEach( value => {
                    midi_visualizers[idx].appendMidiEvent(value);
                })
                break;
            case "visualizer_end":
                idx = msg.data;
                midi_visualizers[idx].finishAppendMidiEvent()
                midi_visualizers[idx].setPlayTime(0);
                break;
            case "progress":
                let progress = msg.data[0]
                let total = msg.data[1]
                setProgressBar(progress, total)
                break;
            default:
        }
    }
})();