asigalov61 commited on
Commit
a71ce75
1 Parent(s): 276900b

Update javascript/app.js

Browse files
Files changed (1) hide show
  1. javascript/app.js +384 -371
javascript/app.js CHANGED
@@ -1,187 +1,207 @@
1
  function gradioApp() {
2
- const elems = document.getElementsByTagName('gradio-app')
3
- const gradioShadowRoot = elems.length == 0 ? null : elems[0].shadowRoot
4
- return !!gradioShadowRoot ? gradioShadowRoot : document;
5
  }
6
 
7
- uiUpdateCallbacks = []
8
- msgReceiveCallbacks = []
9
 
10
- function onUiUpdate(callback){
11
- uiUpdateCallbacks.push(callback)
12
  }
13
 
14
- function onMsgReceive(callback){
15
- msgReceiveCallbacks.push(callback)
16
  }
17
 
18
- function runCallback(x, m){
19
- try {
20
- x(m)
21
- } catch (e) {
22
- (console.error || console.log).call(console, e.message, e);
23
- }
24
  }
 
25
  function executeCallbacks(queue, m) {
26
- queue.forEach(function(x){runCallback(x, m)})
 
 
27
  }
28
 
29
- document.addEventListener("DOMContentLoaded", function() {
30
- var mutationObserver = new MutationObserver(function(m){
31
- executeCallbacks(uiUpdateCallbacks, m);
32
- });
33
- mutationObserver.observe( gradioApp(), { childList:true, subtree:true })
34
  });
35
 
36
- (()=>{
37
- let mse_receiver_inited = null
38
- onUiUpdate(()=>{
39
- let app = gradioApp()
40
- let msg_receiver = app.querySelector("#msg_receiver");
41
- if(!!msg_receiver && mse_receiver_inited !== msg_receiver){
42
- let mutationObserver = new MutationObserver(function(ms){
43
- ms.forEach((m)=>{
44
- m.addedNodes.forEach((node)=>{
45
- if(node.nodeName === "P"){
46
- let obj = JSON.parse(node.innerText);
47
- if(obj instanceof Array){
48
- obj.forEach((o)=>{executeCallbacks(msgReceiveCallbacks, o);});
49
- }else{
50
- executeCallbacks(msgReceiveCallbacks, obj);
51
- }
52
- }
53
- })
54
- })
55
- });
56
- mutationObserver.observe( msg_receiver, {childList:true, subtree:true, characterData:true})
57
- console.log("receiver init");
58
- mse_receiver_inited = msg_receiver;
59
- }
60
- })
61
- })()
 
 
 
 
 
 
62
 
63
  function HSVtoRGB(h, s, v) {
64
- let r, g, b, i, f, p, q, t;
65
- i = Math.floor(h * 6);
66
- f = h * 6 - i;
67
- p = v * (1 - s);
68
- q = v * (1 - f * s);
69
- t = v * (1 - (1 - f) * s);
70
- switch (i % 6) {
71
- case 0: r = v; g = t; b = p; break;
72
- case 1: r = q; g = v; b = p; break;
73
- case 2: r = p; g = v; b = t; break;
74
- case 3: r = p; g = q; b = v; break;
75
- case 4: r = t; g = p; b = v; break;
76
- case 5: r = v; g = p; b = q; break;
77
- }
78
- return {
79
- r: Math.round(r * 255),
80
- g: Math.round(g * 255),
81
- b: Math.round(b * 255)
82
- };
 
 
 
 
 
 
 
 
 
 
 
 
83
  }
84
 
85
- class MidiVisualizer extends HTMLElement{
86
- constructor() {
87
- super();
88
- this.midiEvents = [];
89
- this.activeNotes = [];
90
- this.midiTimes = [];
91
- this.wrapper = null;
92
- this.svg = null;
93
- this.timeLine = null;
94
- this.config = {
95
- noteHeight : 4,
96
- beatWidth: 32
97
- }
98
- this.tickPreBeat = 500
99
- this.svgWidth = 0;
100
- this.playTime = 0
101
- this.playTimeMs = 0
102
- this.colorMap = new Map();
103
- this.playing = false;
104
- this.timer = null;
105
- this.init();
106
- }
107
-
108
- init(){
109
- this.innerHTML=''
110
- const shadow = this.attachShadow({mode: 'open'});
111
- const style = document.createElement("style");
112
- const wrapper = document.createElement('div');
113
- style.textContent = ".note.active {stroke: black;stroke-width: 0.75;stroke-opacity: 0.75;}";
114
- wrapper.style.overflowX= "scroll"
115
- const svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
116
- svg.style.height = `${this.config.noteHeight*128}px`;
117
- svg.style.width = `${this.svgWidth}px`;
118
- const timeLine = document.createElementNS('http://www.w3.org/2000/svg', 'line');
119
- timeLine.style.stroke = "green"
120
- timeLine.style.strokeWidth = 2;
121
- shadow.appendChild(style)
122
- shadow.appendChild(wrapper);
123
- wrapper.appendChild(svg);
124
- svg.appendChild(timeLine)
125
- this.wrapper = wrapper;
126
- this.svg = svg;
127
- this.timeLine= timeLine;
128
- this.setPlayTime(0);
129
- }
130
-
131
- clearMidiEvents(){
132
- this.pause()
133
- this.midiEvents = [];
134
- this.activeNotes = [];
135
- this.midiTimes = [];
136
- this.colorMap.clear()
137
- this.setPlayTime(0);
138
- this.playTimeMs = 0
139
- this.svgWidth = 0
140
- this.svg.innerHTML = ''
141
- this.svg.style.width = `${this.svgWidth}px`;
142
- this.svg.appendChild(this.timeLine)
143
- }
144
-
145
- appendMidiEvent(midiEvent){
146
- if(midiEvent instanceof Array && midiEvent.length > 0){
147
- if(midiEvent[0] === "note"){
148
- let t = midiEvent[1]
149
- let duration = midiEvent[2]
150
- let channel = midiEvent[3]
151
- let pitch = midiEvent[4]
152
- let velocity = midiEvent[5]
153
- let x = (t/this.tickPreBeat)*this.config.beatWidth
154
- let y = (127 - pitch)*this.config.noteHeight
155
- let w = (duration/this.tickPreBeat)*this.config.beatWidth
156
- let h = this.config.noteHeight
157
- this.svgWidth = Math.ceil(Math.max(x + w, this.svgWidth))
158
- let color = this.getColor(0, channel)
159
- let opacity = Math.min(1, velocity/127 + 0.1).toFixed(2)
160
- let rect = this.drawNote(x,y,w,h, `rgba(${color.r}, ${color.g}, ${color.b}, ${opacity})`)
161
- midiEvent.push(rect)
162
- this.setPlayTime(t);
163
- this.wrapper.scrollTo(this.svgWidth - this.wrapper.offsetWidth, 0)
164
- }
165
- this.midiEvents.push(midiEvent);
166
- this.svg.style.width = `${this.svgWidth}px`;
167
- }
168
-
169
  }
 
170
 
171
- getColor(track, channel) {
172
  const colors = [
173
- [255, 0, 0], // Red
174
- [255, 255, 0], // Yellow
175
- [0, 128, 0], // Green
176
- [0, 255, 255], // Cyan
177
- [0, 0, 255], // Blue
178
- [255, 192, 203], // Pink
179
- [255, 165, 0], // Orange
180
- [128, 0, 128], // Purple
181
- [128, 128, 128], // Gray
182
- [255, 255, 255], // White
183
- [255, 215, 0], // Gold
184
- [192, 192, 192] // Silver
185
  ];
186
 
187
  // Calculate an index based on the track and channel
@@ -192,225 +212,218 @@ class MidiVisualizer extends HTMLElement{
192
 
193
  // Return the RGB color in the format "rgb(r, g, b)"
194
  return { r, g, b };
 
195
 
 
 
 
196
  }
197
-
198
-
199
- drawNote(x, y, w, h, fill) {
200
- if (!this.svg) {
201
- return null;
202
- }
203
- const rect = document.createElementNS('http://www.w3.org/2000/svg', 'rect');
204
- rect.classList.add('note');
205
- rect.setAttribute('fill', fill);
206
- // Round values to the nearest integer to avoid partially filled pixels.
207
- rect.setAttribute('x', `${Math.round(x)}`);
208
- rect.setAttribute('y', `${Math.round(y)}`);
209
- rect.setAttribute('width', `${Math.round(w)}`);
210
- rect.setAttribute('height', `${Math.round(h)}`);
211
- this.svg.appendChild(rect);
212
- return rect
213
- }
214
-
215
- finishAppendMidiEvent(){
216
- this.pause()
217
- let midiEvents = this.midiEvents.sort((a, b)=>a[1]-b[1])
218
- let tempo = (60 / 120) * 10 ** 3
219
- let ms = 0
220
- let lastT = 0
221
- this.midiTimes.push({ms:ms, t: 0, tempo: tempo})
222
- midiEvents.forEach((midiEvent)=>{
223
- let t = midiEvent[1]
224
- ms += ((t- lastT) / this.tickPreBeat) * tempo
225
- if(midiEvent[0]==="set_tempo"){
226
- tempo = midiEvent[2]
227
- this.midiTimes.push({ms:ms, t: t, tempo: tempo})
228
- }
229
- lastT = t
230
- })
231
- }
232
-
233
- setPlayTime(t){
234
- this.playTime = t
235
- let x = Math.round((t/this.tickPreBeat)*this.config.beatWidth)
236
- this.timeLine.setAttribute('x1', `${x}`);
237
- this.timeLine.setAttribute('y1', '0');
238
- this.timeLine.setAttribute('x2', `${x}`);
239
- this.timeLine.setAttribute('y2', `${this.config.noteHeight*128}`);
240
-
241
- this.wrapper.scrollTo(Math.max(0, x - this.wrapper.offsetWidth/2), 0)
242
-
243
- if(this.playing){
244
- let activeNotes = []
245
- this.removeActiveNotes(this.activeNotes)
246
- this.midiEvents.forEach((midiEvent)=>{
247
- if(midiEvent[0] === "note"){
248
- let time = midiEvent[1]
249
- let duration = midiEvent[2]
250
- let note = midiEvent[midiEvent.length - 1]
251
- if(time <=this.playTime && time+duration>= this.playTime){
252
- activeNotes.push(note)
253
- }
254
- }
255
- })
256
- this.addActiveNotes(activeNotes)
257
- }
258
- }
259
-
260
- setPlayTimeMs(ms){
261
- this.playTimeMs = ms
262
- let playTime = 0
263
- for(let i =0;i<this.midiTimes.length;i++){
264
- let midiTime = this.midiTimes[i]
265
- if(midiTime.ms>=ms){
266
- break;
267
- }
268
- playTime = midiTime.t + (ms-midiTime.ms) * this.tickPreBeat / midiTime.tempo
269
  }
270
- this.setPlayTime(playTime)
271
- }
272
-
273
- addActiveNotes(notes){
274
- notes.forEach((note)=>{
275
- this.activeNotes.push(note)
276
- note.classList.add('active');
277
- });
278
- }
279
-
280
- removeActiveNotes(notes){
281
- notes.forEach((note)=>{
282
- let idx = this.activeNotes.indexOf(note)
283
- if(idx>-1)
284
- this.activeNotes.splice(idx, 1);
285
- note.classList.remove('active');
286
- });
287
  }
288
-
289
- play(){
290
- this.playing = true;
291
- this.timer = setInterval(() => {
292
- this.setPlayTimeMs(this.playTimeMs + 10)
293
- }, 10);
294
- }
295
-
296
- pause(){
297
- if(!!this.timer)
298
- clearInterval(this.timer)
299
- this.removeActiveNotes(this.activeNotes)
300
- this.timer = null;
301
- this.playing = false;
302
  }
 
 
303
 
 
 
 
 
 
 
304
 
305
- bindAudioPlayer(audio){
306
- this.pause()
307
- audio.addEventListener("play", (event)=>{
308
- this.play()
309
- })
310
- audio.addEventListener("pause", (event)=>{
311
- this.pause()
312
- })
313
- audio.addEventListener("timeupdate", (event)=>{
314
- this.setPlayTimeMs(event.target.currentTime*10**3)
315
- })
316
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
317
  }
318
 
319
  customElements.define('midi-visualizer', MidiVisualizer);
320
 
321
- (()=>{
322
- let midi_visualizer_container_inited = null
323
- let midi_audio_inited = null;
324
- let midi_visualizer = document.createElement('midi-visualizer')
325
-
326
- if(window.innerWidth < 300){
327
- midi_visualizer.config.noteHeight = 1
328
- midi_visualizer.config.beatWidth = 8
329
- }else if(window.innerWidth < 600){
330
- midi_visualizer.config.noteHeight = 2
331
- midi_visualizer.config.beatWidth = 16
332
- }else if(window.innerWidth < 1280){
333
- midi_visualizer.config.noteHeight = 4
334
- midi_visualizer.config.beatWidth = 32
335
- }else {
336
- midi_visualizer.config.noteHeight = 4
337
- midi_visualizer.config.beatWidth = 64
338
- }
339
- midi_visualizer.svg.style.height = `${midi_visualizer.config.noteHeight*128}px`; //reload svg height
340
-
341
- onUiUpdate((m)=>{
342
- let app = gradioApp()
343
- let midi_visualizer_container = app.querySelector("#midi_visualizer_container");
344
- if(!!midi_visualizer_container && midi_visualizer_container_inited!== midi_visualizer_container){
345
- midi_visualizer_container.appendChild(midi_visualizer)
346
- midi_visualizer_container_inited = midi_visualizer_container;
347
- }
348
- let midi_audio = app.querySelector("#midi_audio > audio");
349
- if(!!midi_audio && midi_audio_inited!==midi_audio){
350
- midi_visualizer.bindAudioPlayer(midi_audio)
351
- midi_audio_inited = midi_audio
352
- }
353
- })
354
-
355
- function createProgressBar(progressbarContainer){
356
- let parentProgressbar = progressbarContainer.parentNode;
357
- let divProgress = document.createElement('div');
358
- divProgress.className='progressDiv';
359
- let rect = progressbarContainer.getBoundingClientRect();
360
- divProgress.style.width = rect.width + "px";
361
- divProgress.style.background = "#b4c0cc";
362
- divProgress.style.borderRadius = "8px";
363
- let divInner = document.createElement('div');
364
- divInner.className='progress';
365
- divInner.style.color = "white";
366
- divInner.style.background = "#0060df";
367
- divInner.style.textAlign = "right";
368
- divInner.style.fontWeight = "bold";
369
- divInner.style.borderRadius = "8px";
370
- divInner.style.height = "20px";
371
- divInner.style.lineHeight = "20px";
372
- divInner.style.paddingRight = "8px"
373
- divInner.style.width = "0%";
374
- divProgress.appendChild(divInner);
375
- parentProgressbar.insertBefore(divProgress, progressbarContainer);
376
  }
377
-
378
- function removeProgressBar(progressbarContainer){
379
- let parentProgressbar = progressbarContainer.parentNode;
380
- let divProgress = parentProgressbar.querySelector(".progressDiv");
381
- parentProgressbar.removeChild(divProgress);
382
  }
383
-
384
- function setProgressBar(progressbarContainer, progress, total){
385
- let parentProgressbar = progressbarContainer.parentNode;
386
- let divProgress = parentProgressbar.querySelector(".progressDiv");
387
- let divInner = parentProgressbar.querySelector(".progress");
388
- if(total===0)
389
- total = 1;
390
- divInner.style.width = `${(progress/total)*100}%`;
391
- divInner.textContent = `${progress}/${total}`;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
392
  }
393
-
394
- onMsgReceive((msg)=>{
395
- switch (msg.name) {
396
- case "visualizer_clear":
397
- midi_visualizer.clearMidiEvents();
398
- createProgressBar(midi_visualizer_container_inited)
399
- break;
400
- case "visualizer_append":
401
- midi_visualizer.appendMidiEvent(msg.data);
402
- break;
403
- case "progress":
404
- let progress = msg.data[0]
405
- let total = msg.data[1]
406
- setProgressBar(midi_visualizer_container_inited, progress, total)
407
- break;
408
- case "visualizer_end":
409
- midi_visualizer.finishAppendMidiEvent()
410
- midi_visualizer.setPlayTime(0);
411
- removeProgressBar(midi_visualizer_container_inited);
412
- break;
413
- default:
414
- }
415
- })
416
- })();
 
1
  function gradioApp() {
2
+ const elems = document.getElementsByTagName('gradio-app');
3
+ const gradioShadowRoot = elems.length === 0 ? null : elems[0].shadowRoot;
4
+ return !!gradioShadowRoot ? gradioShadowRoot : document;
5
  }
6
 
7
+ const uiUpdateCallbacks = [];
8
+ const msgReceiveCallbacks = [];
9
 
10
+ function onUiUpdate(callback) {
11
+ uiUpdateCallbacks.push(callback);
12
  }
13
 
14
+ function onMsgReceive(callback) {
15
+ msgReceiveCallbacks.push(callback);
16
  }
17
 
18
+ function runCallback(x, m) {
19
+ try {
20
+ x(m);
21
+ } catch (e) {
22
+ (console.error || console.log).call(console, e.message, e);
23
+ }
24
  }
25
+
26
  function executeCallbacks(queue, m) {
27
+ queue.forEach(function (x) {
28
+ runCallback(x, m);
29
+ });
30
  }
31
 
32
+ document.addEventListener('DOMContentLoaded', function () {
33
+ const mutationObserver = new MutationObserver(function (m) {
34
+ executeCallbacks(uiUpdateCallbacks, m);
35
+ });
36
+ mutationObserver.observe(gradioApp(), { childList: true, subtree: true });
37
  });
38
 
39
+ (function () {
40
+ let mse_receiver_inited = null;
41
+ onUiUpdate(() => {
42
+ const app = gradioApp();
43
+ const msg_receiver = app.querySelector('#msg_receiver');
44
+ if (!!msg_receiver && mse_receiver_inited !== msg_receiver) {
45
+ const mutationObserver = new MutationObserver(function (ms) {
46
+ ms.forEach((m) => {
47
+ m.addedNodes.forEach((node) => {
48
+ if (node.nodeName === 'P') {
49
+ const obj = JSON.parse(node.innerText);
50
+ if (obj instanceof Array) {
51
+ obj.forEach((o) => {
52
+ executeCallbacks(msgReceiveCallbacks, o);
53
+ });
54
+ } else {
55
+ executeCallbacks(msgReceiveCallbacks, obj);
56
+ }
57
+ }
58
+ });
59
+ });
60
+ });
61
+ mutationObserver.observe(msg_receiver, {
62
+ childList: true,
63
+ subtree: true,
64
+ characterData: true,
65
+ });
66
+ console.log('receiver init');
67
+ mse_receiver_inited = msg_receiver;
68
+ }
69
+ });
70
+ })();
71
 
72
  function HSVtoRGB(h, s, v) {
73
+ let r, g, b, i, f, p, q, t;
74
+ i = Math.floor(h * 6);
75
+ f = h * 6 - i;
76
+ p = v * (1 - s);
77
+ q = v * (1 - f * s);
78
+ t = v * (1 - (1 - f) * s);
79
+ switch (i % 6) {
80
+ case 0:
81
+ (r = v), (g = t), (b = p);
82
+ break;
83
+ case 1:
84
+ (r = q), (g = v), (b = p);
85
+ break;
86
+ case 2:
87
+ (r = p), (g = v), (b = t);
88
+ break;
89
+ case 3:
90
+ (r = p), (g = q), (b = v);
91
+ break;
92
+ case 4:
93
+ (r = t), (g = p), (b = v);
94
+ break;
95
+ case 5:
96
+ (r = v), (g = p), (b = q);
97
+ break;
98
+ }
99
+ return {
100
+ r: Math.round(r * 255),
101
+ g: Math.round(g * 255),
102
+ b: Math.round(b * 255),
103
+ };
104
  }
105
 
106
+ class MidiVisualizer extends HTMLElement {
107
+ constructor() {
108
+ super();
109
+ this.midiEvents = [];
110
+ this.activeNotes = [];
111
+ this.midiTimes = [];
112
+ this.wrapper = null;
113
+ this.svg = null;
114
+ this.timeLine = null;
115
+ this.config = {
116
+ noteHeight: 4,
117
+ beatWidth: 32,
118
+ };
119
+ this.tickPreBeat = 500;
120
+ this.svgWidth = 0;
121
+ this.playTime = 0;
122
+ this.playTimeMs = 0;
123
+ this.colorMap = new Map();
124
+ this.playing = false;
125
+ this.timer = null;
126
+ this.init();
127
+ }
128
+
129
+ init() {
130
+ this.innerHTML = '';
131
+ const shadow = this.attachShadow({ mode: 'open' });
132
+ const style = document.createElement('style');
133
+ const wrapper = document.createElement('div');
134
+ style.textContent = '.note.active {stroke: black;stroke-width: 0.75;stroke-opacity: 0.75;}';
135
+ wrapper.style.overflowX = 'scroll';
136
+ const svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
137
+ svg.style.height = `${this.config.noteHeight * 128}px`;
138
+ svg.style.width = `${this.svgWidth}px`;
139
+ const timeLine = document.createElementNS('http://www.w3.org/2000/svg', 'line');
140
+ timeLine.style.stroke = 'green';
141
+ timeLine.style.strokeWidth = 2;
142
+ shadow.appendChild(style);
143
+ shadow.appendChild(wrapper);
144
+ wrapper.appendChild(svg);
145
+ svg.appendChild(timeLine);
146
+ this.wrapper = wrapper;
147
+ this.svg = svg;
148
+ this.timeLine = timeLine;
149
+ this.setPlayTime(0);
150
+ }
151
+
152
+ clearMidiEvents() {
153
+ this.pause();
154
+ this.midiEvents = [];
155
+ this.activeNotes = [];
156
+ this.midiTimes = [];
157
+ this.colorMap.clear();
158
+ this.setPlayTime(0);
159
+ this.playTimeMs = 0;
160
+ this.svgWidth = 0;
161
+ this.svg.innerHTML = '';
162
+ this.svg.style.width = `${this.svgWidth}px`;
163
+ this.svg.appendChild(this.timeLine);
164
+ }
165
+
166
+ appendMidiEvent(midiEvent) {
167
+ if (midiEvent instanceof Array && midiEvent.length > 0) {
168
+ if (midiEvent[0] === 'note') {
169
+ const t = midiEvent[1];
170
+ const duration = midiEvent[2];
171
+ const channel = midiEvent[3];
172
+ const pitch = midiEvent[4];
173
+ const velocity = midiEvent[5];
174
+ const x = (t / this.tickPreBeat) * this.config.beatWidth;
175
+ const y = (127 - pitch) * this.config.noteHeight;
176
+ const w = (duration / this.tickPreBeat) * this.config.beatWidth;
177
+ const h = this.config.noteHeight;
178
+ this.svgWidth = Math.ceil(Math.max(x + w, this.svgWidth));
179
+ const color = this.getColor(0, channel);
180
+ const opacity = (Math.min(1, velocity / 127 + 0.1)).toFixed(2);
181
+ const rect = this.drawNote(x, y, w, h, `rgba(${color.r}, ${color.g}, ${color.b}, ${opacity})`);
182
+ midiEvent.push(rect);
183
+ this.setPlayTime(t);
184
+ this.wrapper.scrollTo(this.svgWidth - this.wrapper.offsetWidth, 0);
185
+ }
186
+ this.midiEvents.push(midiEvent);
187
+ this.svg.style.width = `${this.svgWidth}px`;
 
 
188
  }
189
+ }
190
 
191
+ getColor(track, channel) {
192
  const colors = [
193
+ [255, 0, 0], // Red
194
+ [255, 255, 0], // Yellow
195
+ [0, 128, 0], // Green
196
+ [0, 255, 255], // Cyan
197
+ [0, 0, 255], // Blue
198
+ [255, 192, 203], // Pink
199
+ [255, 165, 0], // Orange
200
+ [128, 0, 128], // Purple
201
+ [128, 128, 128], // Gray
202
+ [255, 255, 255], // White
203
+ [255, 215, 0], // Gold
204
+ [192, 192, 192], // Silver
205
  ];
206
 
207
  // Calculate an index based on the track and channel
 
212
 
213
  // Return the RGB color in the format "rgb(r, g, b)"
214
  return { r, g, b };
215
+ }
216
 
217
+ drawNote(x, y, w, h, fill) {
218
+ if (!this.svg) {
219
+ return null;
220
  }
221
+ const rect = document.createElementNS('http://www.w3.org/2000/svg', 'rect');
222
+ rect.classList.add('note');
223
+ rect.setAttribute('fill', fill);
224
+ // Round values to the nearest integer to avoid partially filled pixels.
225
+ rect.setAttribute('x', `${Math.round(x)}`);
226
+ rect.setAttribute('y', `${Math.round(y)}`);
227
+ rect.setAttribute('width', `${Math.round(w)}`);
228
+ rect.setAttribute('height', `${Math.round(h)}`);
229
+ this.svg.appendChild(rect);
230
+ return rect;
231
+ }
232
+
233
+ finishAppendMidiEvent() {
234
+ this.pause();
235
+ const midiEvents = this.midiEvents.sort((a, b) => a[1] - b[1]);
236
+ let tempo = (60 / 120) * 10 ** 3;
237
+ let ms = 0;
238
+ let lastT = 0;
239
+ this.midiTimes.push({ ms: ms, t: 0, tempo: tempo });
240
+ midiEvents.forEach((midiEvent) => {
241
+ const t = midiEvent[1];
242
+ ms += ((t - lastT) / this.tickPreBeat) * tempo;
243
+ if (midiEvent[0] === 'set_tempo') {
244
+ tempo = midiEvent[2];
245
+ this.midiTimes.push({ ms: ms, t: t, tempo: tempo });
246
+ }
247
+ lastT = t;
248
+ });
249
+ }
250
+
251
+ setPlayTime(t) {
252
+ this.playTime = t;
253
+ const x = Math.round((t / this.tickPreBeat) * this.config.beatWidth);
254
+ this.timeLine.setAttribute('x1', `${x}`);
255
+ this.timeLine.setAttribute('y1', '0');
256
+ this.timeLine.setAttribute('x2', `${x}`);
257
+ this.timeLine.setAttribute('y2', `${this.config.noteHeight * 128}`);
258
+ this.wrapper.scrollTo(Math.max(0, x - this.wrapper.offsetWidth / 2), 0);
259
+
260
+ if (this.playing) {
261
+ const activeNotes = [];
262
+ this.removeActiveNotes(this.activeNotes);
263
+ this.midiEvents.forEach((midiEvent) => {
264
+ if (midiEvent[0] === 'note') {
265
+ const time = midiEvent[1];
266
+ const duration = midiEvent[2];
267
+ const note = midiEvent[midiEvent.length - 1];
268
+ if (time <= this.playTime && time + duration >= this.playTime) {
269
+ activeNotes.push(note);
270
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
271
  }
272
+ });
273
+ this.addActiveNotes(activeNotes);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
274
  }
275
+ }
276
+
277
+ setPlayTimeMs(ms) {
278
+ this.playTimeMs = ms;
279
+ let playTime = 0;
280
+ for (let i = 0; i < this.midiTimes.length; i++) {
281
+ const midiTime = this.midiTimes[i];
282
+ if (midiTime.ms >= ms) {
283
+ break;
284
+ }
285
+ playTime = midiTime.t + ((ms - midiTime.ms) * this.tickPreBeat) / midiTime.tempo;
 
 
 
286
  }
287
+ this.setPlayTime(playTime);
288
+ }
289
 
290
+ addActiveNotes(notes) {
291
+ notes.forEach((note) => {
292
+ this.activeNotes.push(note);
293
+ note.classList.add('active');
294
+ });
295
+ }
296
 
297
+ removeActiveNotes(notes) {
298
+ notes.forEach((note) => {
299
+ const idx = this.activeNotes.indexOf(note);
300
+ if (idx > -1) this.activeNotes.splice(idx, 1);
301
+ note.classList.remove('active');
302
+ });
303
+ }
304
+
305
+ play() {
306
+ this.playing = true;
307
+ this.timer = setInterval(() => {
308
+ this.setPlayTimeMs(this.playTimeMs + 10);
309
+ }, 10);
310
+ }
311
+
312
+ pause() {
313
+ if (!!this.timer) clearInterval(this.timer);
314
+ this.removeActiveNotes(this.activeNotes);
315
+ this.timer = null;
316
+ this.playing = false;
317
+ }
318
+
319
+ bindAudioPlayer(audio) {
320
+ this.pause();
321
+ audio.addEventListener('play', (event) => {
322
+ this.play();
323
+ });
324
+ audio.addEventListener('pause', (event) => {
325
+ this.pause();
326
+ });
327
+ audio.addEventListener('timeupdate', (event) => {
328
+ this.setPlayTimeMs(event.target.currentTime * 10 ** 3);
329
+ });
330
+ }
331
  }
332
 
333
  customElements.define('midi-visualizer', MidiVisualizer);
334
 
335
+ (function () {
336
+ let midi_visualizer_container_inited = null;
337
+ let midi_audio_inited = null;
338
+ const midi_visualizer = document.createElement('midi-visualizer');
339
+
340
+ if (window.innerWidth < 300) {
341
+ midi_visualizer.config.noteHeight = 1;
342
+ midi_visualizer.config.beatWidth = 8;
343
+ } else if (window.innerWidth < 600) {
344
+ midi_visualizer.config.noteHeight = 2;
345
+ midi_visualizer.config.beatWidth = 16;
346
+ } else if (window.innerWidth < 1280) {
347
+ midi_visualizer.config.noteHeight = 4;
348
+ midi_visualizer.config.beatWidth = 32;
349
+ } else {
350
+ midi_visualizer.config.noteHeight = 4;
351
+ midi_visualizer.config.beatWidth = 64;
352
+ }
353
+ midi_visualizer.svg.style.height = `${midi_visualizer.config.noteHeight * 128}px`; // Reload svg height
354
+
355
+ onUiUpdate((m) => {
356
+ const app = gradioApp();
357
+ const midi_visualizer_container = app.querySelector('#midi_visualizer_container');
358
+ if (!!midi_visualizer_container && midi_visualizer_container_inited !== midi_visualizer_container) {
359
+ midi_visualizer_container.appendChild(midi_visualizer);
360
+ midi_visualizer_container_inited = midi_visualizer_container;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
361
  }
362
+ const midi_audio = app.querySelector('#midi_audio > audio');
363
+ if (!!midi_audio && midi_audio_inited !== midi_audio) {
364
+ midi_visualizer.bindAudioPlayer(midi_audio);
365
+ midi_audio_inited = midi_audio;
 
366
  }
367
+ });
368
+
369
+ function createProgressBar(progressbarContainer) {
370
+ const parentProgressbar = progressbarContainer.parentNode;
371
+ const divProgress = document.createElement('div');
372
+ divProgress.className = 'progressDiv';
373
+ const rect = progressbarContainer.getBoundingClientRect();
374
+ divProgress.style.width = rect.width + 'px';
375
+ divProgress.style.background = '#b4c0cc';
376
+ divProgress.style.borderRadius = '8px';
377
+ const divInner = document.createElement('div');
378
+ divInner.className = 'progress';
379
+ divInner.style.color = 'white';
380
+ divInner.style.background = '#0060df';
381
+ divInner.style.textAlign = 'right';
382
+ divInner.style.fontWeight = 'bold';
383
+ divInner.style.borderRadius = '8px';
384
+ divInner.style.height = '20px';
385
+ divInner.style.lineHeight = '20px';
386
+ divInner.style.paddingRight = '8px';
387
+ divInner.style.width = '0%';
388
+ divProgress.appendChild(divInner);
389
+ parentProgressbar.insertBefore(divProgress, progressbarContainer);
390
+ }
391
+
392
+ function removeProgressBar(progressbarContainer) {
393
+ const parentProgressbar = progressbarContainer.parentNode;
394
+ const divProgress = parentProgressbar.querySelector('.progressDiv');
395
+ parentProgressbar.removeChild(divProgress);
396
+ }
397
+
398
+ function setProgressBar(progressbarContainer, progress, total) {
399
+ const parentProgressbar = progressbarContainer.parentNode;
400
+ const divProgress = parentProgressbar.querySelector('.progressDiv');
401
+ const divInner = parentProgressbar.querySelector('.progress');
402
+ if (total === 0) total = 1;
403
+ divInner.style.width = `${(progress / total) * 100}%`;
404
+ divInner.textContent = `${progress}/${total}`;
405
+ }
406
+
407
+ onMsgReceive((msg) => {
408
+ switch (msg.name) {
409
+ case 'visualizer_clear':
410
+ midi_visualizer.clearMidiEvents();
411
+ createProgressBar(midi_visualizer_container_inited);
412
+ break;
413
+ case 'visualizer_append':
414
+ midi_visualizer.appendMidiEvent(msg.data);
415
+ break;
416
+ case 'progress':
417
+ const progress = msg.data[0];
418
+ const total = msg.data[1];
419
+ setProgressBar(midi_visualizer_container_inited, progress, total);
420
+ break;
421
+ case 'visualizer_end':
422
+ midi_visualizer.finishAppendMidiEvent();
423
+ midi_visualizer.setPlayTime(0);
424
+ removeProgressBar(midi_visualizer_container_inited);
425
+ break;
426
+ default:
427
  }
428
+ });
429
+ })();