asigalov61 commited on
Commit
5ef7697
·
verified ·
1 Parent(s): c28bd62

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +112 -1
app.py CHANGED
@@ -18,11 +18,122 @@ import tqdm
18
  from midi_to_colab_audio import midi_to_colab_audio
19
  import TMIDIX
20
 
 
 
21
  import matplotlib.pyplot as plt
22
 
23
  # =================================================================================================
24
 
25
- def read_MIDI(input_midi)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
 
27
  #===============================================================================
28
  raw_score = TMIDIX.midi2single_track_ms_score(input_midi)
 
18
  from midi_to_colab_audio import midi_to_colab_audio
19
  import TMIDIX
20
 
21
+ import numpy as np
22
+ from scipy.interpolate import make_interp_spline
23
  import matplotlib.pyplot as plt
24
 
25
  # =================================================================================================
26
 
27
+ def hsv_to_rgb(h, s, v):
28
+ if s == 0.0:
29
+ return v, v, v
30
+ i = int(h*6.0)
31
+ f = (h*6.0) - i
32
+ p = v*(1.0 - s)
33
+ q = v*(1.0 - s*f)
34
+ t = v*(1.0 - s*(1.0-f))
35
+ i = i%6
36
+ return [(v, t, p), (q, v, p), (p, v, t), (p, q, v), (t, p, v), (v, p, q)][i]
37
+
38
+ def generate_colors(n):
39
+ return [hsv_to_rgb(i/n, 1, 1) for i in range(n)]
40
+
41
+ def add_arrays(a, b):
42
+ return [sum(pair) for pair in zip(a, b)]
43
+
44
+ def plot_ms_SONG(ms_song,
45
+ preview_length_in_notes=0,
46
+ block_lines_times_list = None,
47
+ plot_title='ms Song',
48
+ max_num_colors=129,
49
+ drums_color_num=128,
50
+ plot_size=(11,4),
51
+ note_height = 0.75,
52
+ show_grid_lines=False,
53
+ return_plt = False,
54
+ timings_multiplier=1,
55
+ plot_curve_values=None,
56
+ save_plot=''
57
+ ):
58
+
59
+ '''Tegridy ms SONG plotter/vizualizer'''
60
+
61
+ notes = [s for s in ms_song if s[0] == 'note']
62
+
63
+ if (len(max(notes, key=len)) != 7) and (len(min(notes, key=len)) != 7):
64
+ print('The song notes do not have patches information')
65
+ print('Please add patches to the notes in the song')
66
+
67
+ else:
68
+
69
+ start_times = [(s[1] * timings_multiplier) / 1000 for s in notes]
70
+ durations = [(s[2] * timings_multiplier) / 1000 for s in notes]
71
+ pitches = [s[4] for s in notes]
72
+ patches = [s[6] for s in notes]
73
+
74
+ colors = generate_colors(max_num_colors)
75
+ colors[drums_color_num] = (1, 1, 1)
76
+
77
+ pbl = (notes[preview_length_in_notes][1] * timings_multiplier) / 1000
78
+
79
+ fig, ax = plt.subplots(figsize=plot_size)
80
+
81
+ # Create a rectangle for each note with color based on patch number
82
+ for start, duration, pitch, patch in zip(start_times, durations, pitches, patches):
83
+ rect = plt.Rectangle((start, pitch), duration, note_height, facecolor=colors[patch])
84
+ ax.add_patch(rect)
85
+
86
+ if plot_curve_values is not None:
87
+
88
+ min_val = min(plot_curve_values)
89
+ max_val = max(plot_curve_values)
90
+ spcva = [((value - min_val) / (max_val - min_val)) * 100 for value in plot_curve_values]
91
+
92
+
93
+ mult = int(math.ceil(max(add_arrays(start_times, durations)) / len(spcva)))
94
+ pcv = [value for value in spcva for _ in range(mult)][:int(max(add_arrays(start_times, durations)))+mult]
95
+
96
+ x = np.arange(len(pcv))
97
+ x_smooth = np.linspace(x.min(), x.max(), 300)
98
+ spl = make_interp_spline(x, pcv, k=3)
99
+ y_smooth = spl(x_smooth)
100
+ ax.plot(x_smooth, y_smooth, color='white')
101
+
102
+ # Set the limits of the plot
103
+ ax.set_xlim([min(start_times), max(add_arrays(start_times, durations))])
104
+ ax.set_ylim([min(y_smooth), max(y_smooth)])
105
+
106
+ # Set the background color to black
107
+ ax.set_facecolor('black')
108
+ fig.patch.set_facecolor('white')
109
+
110
+ if preview_length_in_notes > 0:
111
+ ax.axvline(x=pbl, c='white')
112
+
113
+ if block_lines_times_list:
114
+ for bl in block_lines_times_list:
115
+ ax.axvline(x=bl, c='white')
116
+
117
+ if show_grid_lines:
118
+ ax.grid(color='white')
119
+
120
+ plt.xlabel('Time (s)', c='black')
121
+ plt.ylabel('MIDI Pitch', c='black')
122
+
123
+ plt.title(plot_title)
124
+
125
+ if return_plt:
126
+ return fig
127
+
128
+ if save_plot == '':
129
+ plt.show()
130
+
131
+ else:
132
+ plt.savefig(save_plot)
133
+
134
+ # =================================================================================================
135
+
136
+ def read_MIDI(input_midi):
137
 
138
  #===============================================================================
139
  raw_score = TMIDIX.midi2single_track_ms_score(input_midi)