-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmidiToVideo.py
600 lines (513 loc) · 20.2 KB
/
midiToVideo.py
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
import numpy as np
import cv2 as cv
# Create a black image
#img = np.ones((512,512,3), np.uint8)*128 # gray background
from sage.all import *
import numpy as np
import pandas,sys
import statsmodels.api as sm
#print(img)
# Draw a diagonal blue line with thickness of 5 px
#cv.line(img,(0,0),(511,511),(255,0,0),5)
# drawing a rectangle:
#cv.rectangle(img,(384,0),(510,128),(0,255,0),3)
# drawing a circle:
#cv.circle(img,(447,63), 63, (0,0,255), -1)
import music21 as m21
from itertools import product
pitchToZ12 = dict(zip(["C","C#","D","D#","E","F","F#","G","G#","A","A#","B"],range(12)))
Z12ToPitch = dict(zip(range(12),["C","C#","D","D#","E","F","F#","G","G#","A","A#","B"]))
import numpy as np
def xml_to_list(xml):
xml_data = m21.converter.parse(xml)
score = []
for part in xml_data.parts:
parts = []
print(part)
for note in part.flat.notesAndRests:
if type(note)==m21.note.Rest:
print("rest", note, note.duration.quarterLength)
duration = float(note.duration.quarterLength)
vol = 32 #note.volume.velocity
pitches= tuple([64])
parts.append(tuple([float(note.offset),pitches,duration,vol,1]))
elif type(note)==m21.chord.Chord:
print("chord ",note,note.duration.quarterLength)
pitches = sorted([e.pitch.midi for e in note]) # todo: think about chords
vol = note[0].volume.velocity
if vol is None:
vol = int(note[0].volume.realized * 127)
else:
vol = int(vol)
duration = float(note.duration.quarterLength)
parts.append(tuple([float(note.offset),tuple(pitches),duration,vol,0]))
else:
print("note", note,note.duration.quarterLength)
start = note.offset
duration = float(note.quarterLength)
pitches = tuple([note.pitch.midi])
#print(pitch,duration,note.volume)
vol = note.volume.velocity
if vol is None:
vol = int(note.volume.realized * 127)
parts.append(tuple([float(note.offset),pitches,duration,vol,0]) )
score.append(parts)
print( [ len(part) for part in score])
return score
def parseXml(fp):
return xml_to_list(fp)
def draw_circle(image, pp, color, radius=0):
x,y = pp
image = cv.circle(image, (x,y), color=color, thickness=cv.FILLED,radius=radius)
return image
def draw_line(image, start,end, color):
image = cv.line(image, start, end, color, thickness=2)
return image
#img = draw_point(img,(256,256),(255,255,255))
def ff(a=1,b=6,c=-14,x=1,y=1/2,z=np.complex(0,1)/3):
i = np.complex(0,1)
return (lambda t: x*np.exp(a*i*t)+y*np.exp(b*i*t)+z*np.exp(c*i*t))
def FF(nn = [1,3],m=2, k=1, aa=[1,2]):
if all([n % m == k for n in nn]) and len(aa)==len(nn) and np.gcd(k,m)==1:
i = np.complex(0,1)
return (lambda t: sum([ aa[j]*np.exp(nn[j]*i*t) for j in range(len(aa)) ]))
else:
return None
def draw_curve(img, ff, mm, color, rr=120,number_of_points = 100,return_points = False):
points = []
def compute_point(ff,k,start,step,rr,mm):
t = start+k*step
z = ff(t)
x,y = z.real,z.imag
# scale:
x = x*rr
y = y*rr
# translate:
x,y = x+mm[0],y+mm[1]
# round to integers
x,y = int(x),int(y)
return x,y
start = 0.0
end = 2*np.pi*14
N = number_of_points
step = (end-start)/N
for k in range(N-1):
x,y = compute_point(ff,k,start,step,rr,mm)
x2,y2 = compute_point(ff,k+1,start,step,rr,mm)
#print(x,y)
points.append((x,y))
img = draw_line(img, (x,y),(x2,y2), color=color)
points.append((x2,y2))
if return_points: return img,points
return img
def color_img(img):
ret, thresh = cv.threshold(img, 127, 255, 0)
num_labels, labels = cv.connectedComponents(thresh,connectivity=8)
# Map component labels to hue val
label_hue = np.uint8(179*labels/np.max(labels))
blank_ch = 255*np.ones_like(label_hue)
labeled_img = cv.merge([label_hue, blank_ch, blank_ch])
# cvt to BGR for display
labeled_img = cv.cvtColor(labeled_img, cv.COLOR_HSV2BGR)
# set bg label to black
labeled_img[label_hue==0] = 0
return labeled_img
#img = draw_circle(img,(256,256), 63, (0,0,255),number_of_points = 10000)
def getImgNrs(start_duration,end_duration,bpm,fps):
N_img_start = int(np.round(fps*60*start_duration/(bpm),0))
N_img_end = int(np.round(fps*60*end_duration/bpm,0))
return (N_img_start,N_img_end)
def convertScore(scores,bpm=70,fps=25,verbose=False):
#determine max durations:
maxDurs = [0 for k in range(len(scores))]
startsAndDurs = [0 for k in range(len(scores))]
pitchSet = set([])
volumeSet = set([])
partCounter = 0
for part in scores:
for note in part:
start,pitches, duration, volume, rest = note
maxDurs[partCounter] += duration
if startsAndDurs[partCounter] < start+duration:
startsAndDurs[partCounter] = start+duration
partCounter+=1
maxDur = np.max(startsAndDurs)
print(startsAndDurs)
print(maxDur)
print(bpm)
Nimgs = int(np.round(60*fps*maxDur/bpm,0))
print(Nimgs)
imgs2Notes = dict([])
#fill dictionary with notes per image
for part in scores:
dur = 0
for note in part:
start,pitches, duration, volume, rest = note
print(start,pitches,duration,volume,rest)
for pitch in pitches:
pitchSet.add(pitch)
volumeSet.add(volume)
start_img, end_img = getImgNrs(start_duration=start,end_duration = start+duration,bpm=bpm,fps=fps)
if verbose: print(note,start_img,end_img)
for k in range(start_img,end_img+1):
if k in imgs2Notes.keys():
imgs2Notes[k].append((note,start_img,end_img))
else:
imgs2Notes[k] = [(note,start_img,end_img)]
dur += duration
return imgs2Notes,pitchSet,volumeSet
def create_video(imgs,videoname="./opencv_videos/video.avi",fps=25):
fourcc = cv.VideoWriter_fourcc(*"X264")
height,width,x = imgs[0].shape
print(width,height,x,fps,videoname)
framesPerSecond = fps
video = cv.VideoWriter(videoname, fourcc, framesPerSecond, (width, height))
cnt = 0
for img in imgs:
#print(cnt,img.shape)
video.write(img)
cnt += 1
video.release()
return video
def compute_color(pitch,volume,t,N,noteCounter,lN,start_img,end_img):
tScaled = (t-start_img)/(end_img-start_img+1)
return (int(tScaled*pitch*2*np.sin(2*np.pi*t/N)),int(tScaled*volume*2*np.sin(2*np.pi*t/N)),int(tScaled*(noteCounter/lN)*128))
def compute_radius(pitch,volume,t,N,noteCounter,lN,start_img,end_img):
tScaled = (t-start_img)/(end_img-start_img+1)
return max(1,int(tScaled*np.abs((volume)*np.cos(2*np.pi*t/N))))
#!/usr/bin/python
import numpy as np
import random
# Check if a point is inside a rectangle
def rect_contains(rect, point) :
if point[0] < rect[0] :
return False
elif point[1] < rect[1] :
return False
elif point[0] > rect[2] :
return False
elif point[1] > rect[3] :
return False
return True
# Draw a point
def draw_point(img, p, color ) :
cv.circle( img, p, 2, color, cv.FILLED, cv2.CV_AA, 0 )
# Draw voronoi diagram
def draw_voronoi(img, subdiv,color) :
( facets, centers) = subdiv.getVoronoiFacetList([])
r,g,b = color
lf = len(facets)
for i in range(0,len(facets)) :
ifacet_arr = []
for f in facets[i] :
ifacet_arr.append(f)
ifacet = np.array(ifacet_arr, np.int)
color = (255-i/lf*r, i/lf*g, i/lf*b)
cv.fillConvexPoly(img, ifacet, color, cv.LINE_AA, 0);
ifacets = np.array([ifacet])
cv.polylines(img, ifacets, True, (0, 0, 0), 1, cv.LINE_AA, 0)
cv.circle(img, (centers[i][0], centers[i][1]), 3, (0, 0, 0), cv.FILLED, cv.LINE_AA, 0)
return img
def make_video_with_circles(imgs2Notes,pitchSet,volumeSet,videoname="./opencv_videos/video.avi",fps=25,verbose=False):
fourcc = cv.VideoWriter_fourcc(*"X264")
height,width = 512,512
print(width,height,fps,videoname)
framesPerSecond = fps
video = cv.VideoWriter(videoname, fourcc, framesPerSecond, (width, height))
cnt = 0
print("volumeSet = ",volumeSet)
print("pitchSet = ", pitchSet)
N = len(imgs2Notes.keys())
import random
mv = min(volumeSet)
Mv = max(volumeSet)
dv = Mv-mv
if dv ==0:
dv = 1
mp = min(pitchSet)
Mp = max(pitchSet)
dp = Mp-mp
if dp ==0:
dp = 1
dx = 10
dy = 10
breite = 512-2*dx
hoehe = 512-2*dy
img = np.ones((512,512,3), np.uint8)*0 # white background
rb0 = np.random.randint(1,breite)
rh0 = np.random.randint(1,hoehe)
pitchList = sorted(list(pitchSet))
X0,invPitchDict = getCoordinatesOfPitchList(pitchList,dx,breite)
for t in range(N):
img = np.ones((512,512,3), np.uint8)*0 # white background
rb = np.random.randint(-5*dx,5*dx)
rh = np.random.randint(-5*dy,5*dy)
notes = imgs2Notes[t]
noteCounter = 0
lN = len(notes)
if verbose: print(t,"/",N," img")
for tt in notes:
note,start_img,end_img = tt
start,pitches, duration, volume,rest = note
if rest==1:
continue
#print(t,note)
for pitch in pitches:
volumeScaled = (volume-mv)/dv
pitchScaled = (pitch-mp)/dp
x,y = dx+int(pitchScaled*rb),dy+int(volumeScaled*rh)
x0,y0 = [int(a) for a in X0[invPitchDict[pitch]]]
x = x0 + rb
y = y0 + rh
radius = compute_radius(pitch,volume,t,N,noteCounter,lN,start_img,end_img)
color = compute_color(pitch,volume,t,N,noteCounter,lN,start_img,end_img)
img = draw_circle(img,pp=(x,y), color = color, radius=radius)
noteCounter += 1
noteCounter = 0
# zeichne helle kreise
for tt in notes:
note,start_img,end_img = tt
start,pitches, duration, volume,rest = note
#print(t,note)
if rest==1:
continue
for pitch in pitches:
pitchScaled = (pitch-mp)/dp
volumeScaled = (volume-mv)/dv
#ff = FF(nn=[(volume//64)*m+k,(pitch//64)*m+k,2],m=m,k=k,aa=[(volume+pitch)/x*np.sin(t*np.pi*2/N)/2.0 for x in [128,128,128]])
#img = draw_curve(img,ff,(x,y),(0,0,0),rr=10,number_of_points = 1000) # 011.png
radius = compute_radius(pitch,volume,t,N,noteCounter,lN,start_img,end_img)
color = compute_color(pitch,volume,t,N,noteCounter,lN,start_img,end_img)
x0,y0 = [int(a) for a in X0[invPitchDict[pitch]]]#dx+int(pitchScaled*breite),dy+int(volumeScaled*hoehe)
m,k=3,2
ff = FF(nn=[(volume//64)*m+k,(pitch//64)*m+k,2],m=m,k=k,aa=[(volume+pitch)/x*np.sin(t*np.pi*2/N)/2.0 for x in [128,128,128]])
img = draw_curve(img,ff,(x0,y0),color=(255-color[0],255-color[1],255-color[2]),rr=radius,number_of_points = 100)
#img = draw_circle(img,pp=(x0,y0),color=(255-color[0],255-color[1],255-color[2]),radius=radius)
noteCounter += 1
#labeled_img = color_img(img)
#imgs.append(labeled_img)
video.write(img)
video.release()
return True
def make_video_with_voronoi(imgs2Notes,pitchSet,volumeSet,videoname="./opencv_videos/video.avi",fps=25,verbose=False):
fourcc = cv.VideoWriter_fourcc(*"X264")
height,width = 512,512
print(width,height,fps,videoname)
framesPerSecond = fps
video = cv.VideoWriter(videoname, fourcc, framesPerSecond, (width, height))
cnt = 0
print("volumeSet = ",volumeSet)
print("pitchSet = ", pitchSet)
N = len(imgs2Notes.keys())
import random
mv = min(volumeSet)
Mv = max(volumeSet)
dv = Mv-mv
if dv ==0:
dv = 1
mp = min(pitchSet)
Mp = max(pitchSet)
dp = Mp-mp
if dp ==0:
dp = 1
dx = 10
dy = 10
breite = 512-2*dx
hoehe = 512-2*dy
img = np.ones((512,512,3), np.uint8)*255 # white background
rb0 = np.random.randint(breite//2,breite)
rh0 = np.random.randint(hoehe//2,hoehe)
size = img.shape
rect = (0,0,size[1],size[0])
for t in range(N):
#img = np.ones((512,512,3), np.uint8)*255 # white background
rb = np.random.randint(1,breite)
rh = np.random.randint(1,hoehe)
try:
notes = imgs2Notes[t]
except:
continue
noteCounter = 0
lN = len(notes)
if verbose: print(t,"/",N," img")
subdiv = cv.Subdiv2D(rect);
# draw voronoi
noteCounter = 0
for tt in notes:
note,start_img,end_img = tt
start,pitches, duration, volume,rest = note
volumeScaled = (volume-mv)/dv
if rest==1:
continue
#print(t,note)
for pitch in pitches:
pitchScaled = (pitch-mp)/dp
#print(pitchScaled)
radius = compute_radius(pitch,volume,t,N,noteCounter,lN,start_img,end_img)
color = compute_color(pitch,volume,t,N,noteCounter,lN,start_img,end_img)
color = (255-color[0],color[1],color[2])
x0,y0 = dx+int(pitchScaled*breite),dy+int(volumeScaled*hoehe)
subdiv.insert((x0,y0))
img = draw_voronoi(img,subdiv,color)
noteCounter += 1
# draw circles
noteCounter = 0
for tt in notes:
note,start_img,end_img = tt
start,pitches, duration, volume,rest = note
if rest==1:
continue
volumeScaled = (volume-mv)/dv
#print(t,note)
for pitch in pitches:
pitchScaled = (pitch-mp)/dp
radius = compute_radius(pitch,volume,t,N,noteCounter,lN,start_img,end_img)
color = compute_color(pitch,volume,t,N,noteCounter,lN,start_img,end_img)
#print(pitchScaled)
x0,y0 = dx+int(pitchScaled*breite),dy+int(volumeScaled*hoehe)
#subdiv.insert((x0,y0))
img = draw_circle(img,pp=(x0,y0),color=(255-color[0],255-color[1],255-color[2]),radius=radius)
## symmetric objects:
##m,k=3,2
##ff = FF(nn=[(volume//64)*m+k,(pitch//64)*m+k,2],m=m,k=k,aa=[(volume+pitch)/x*np.sin(t*np.pi*2/N)/2.0 for x in [128,128,128]])
##img = draw_curve(img,ff,(x0,y0),color,rr=2*radius,number_of_points = 1000)
noteCounter += 1
#labeled_img = color_img(img)
#imgs.append(labeled_img)
video.write(img)
video.release()
return True
def kernPause(a1,a2):
return 1*(a1==a2)
def kernPitch(k1,k2):
q = getRational(k2-k1)
a,b = q.numerator(),q.denominator()
return gcd(a,b)**2/(a*b)
def kernDuration(k1,k2):
return min(k1,k2)/max(k1,k2)
def kernVolume(k1,k2):
return min(k1,k2)/max(k1,k2)
def getRational(k):
alpha = 2**(1/12.0)
x = RDF(alpha**k).n(50)
return x.nearby_rational(max_error=0.01*x)
def getCoordinatesOfPitchList(pitchList,dx,breite):
M0 = matrix([[kernPitch(t1,t2) for t1 in pitchList] for t2 in pitchList],ring=RDF)
if not M0.is_positive_definite():
M0+= matrix.identity(len(pitchSet))*0.1
from sklearn.decomposition import PCA
from sklearn.decomposition import KernelPCA
from sklearn.preprocessing import MinMaxScaler
stdScaler = MinMaxScaler(feature_range=(dx,breite-dx))
KPCA = KernelPCA(2,kernel='precomputed')
Ch0 = KPCA.fit_transform(np.array(M0))
X0 = [x for x in 1.0*Ch0]
print(X0)
X0 = stdScaler.fit_transform(X0)
print(X0)
invPitchDict = dict(zip(pitchList,range(len(pitchList))))
return X0, invPitchDict
def make_video_with_symmetry(imgs2Notes,pitchSet,volumeSet,videoname="./opencv_videos/video.avi",fps=25,verbose=False):
fourcc = cv.VideoWriter_fourcc(*"X264")
height,width = 512,512
print(width,height,fps,videoname)
framesPerSecond = fps
video = cv.VideoWriter(videoname, fourcc, framesPerSecond, (width, height))
cnt = 0
print("volumeSet = ",volumeSet)
print("pitchSet = ", pitchSet)
dx = 50
dy = 10
breite = 512-2*dx
hoehe = 512-2*dy
pitchList = sorted(list(pitchSet))
X0,invPitchDict = getCoordinatesOfPitchList(pitchList,dx,breite)
N = len(imgs2Notes.keys())
import random
mv = min(volumeSet)
Mv = max(volumeSet)
dv = Mv-mv
if dv ==0:
dv = 1
mp = min(pitchSet)
Mp = max(pitchSet)
dp = Mp-mp
if dp ==0:
dp = 1
#img = np.ones((512,512,3), np.uint8)*255 # white background
rb0 = np.random.randint(breite//2,breite)
rh0 = np.random.randint(hoehe//2,hoehe)
Npoints = len(pitchSet)*5
for t in range(N):
img = np.ones((512,512,3), np.uint8)*255 # white background
try:
notes = imgs2Notes[t]
except:
continue
print(t,"/",N," img")
m,k=3,2
ln = len(notes)
ps = []
vs = []
ds = []
ts = []
size = img.shape
rect = (0,0,size[1],size[0])
subdiv = cv.Subdiv2D(rect)
points = []
for tt in notes:
note,start_img,end_img = tt
tScaled = (t-start_img+1)/(end_img-start_img+1)
start,pitches, duration, volume,rest = note
print(note)
pitchesScaled = []
for p in pitches:
pScaled = min(max(0,int((p-mp)/dp*Npoints)),Npoints-1)
pitchesScaled.append(p)
points.append([int(x) for x in X0[invPitchDict[p]].tolist()])
ps.append(pitchesScaled)
vs.append(volume)
ds.append(duration)
ts.append(tScaled)
print("ts = ", ts)
print("points = ", points)
print("ps = ", ps)
nn = [(i)*m+k for i in range(4)]
aa = [((i+1)/4.0) for i in range(4)]
print("nn = ", nn)
print("aa = ", aa)
#ff = FF(nn=nn,m=m,k=k,aa=aa)
#img,points = draw_curve(img,ff,(256,256),(0,0,0),rr=int(30*np.median(vs)/64),number_of_points = Npoints,return_points=True)
for pp in X0:
print(pp)
subdiv.insert(tuple([int(x) for x in pp]))
img = draw_voronoi(img,subdiv,(128,32,255))
c = 0
for i in range(ln):
pitchesScaled = ps[i]
for p in pitchesScaled:
radius = max(10,int((vs[i]+ts[i])/4))
color = (int(ts[i]*2*np.sin(2*np.pi*t/N)),int(ts[i]*vs[i]*2*np.sin(2*np.pi*t/N)),int(ts[i]*vs[i]*2*np.cos(2*np.pi*t/N)*128))
img = draw_circle(img,pp=points[c],color=color,radius=radius)
c += 1
#img = color_img(img)
video.write(img)
video.release()
return True
def make_video(imgs2Notes,pitchSet,volumeSet,videoname="./opencv_videos/video.avi",fps=25,video_type="voronoi",verbose=False):
if video_type=="circle": return make_video_with_circles(imgs2Notes,pitchSet,volumeSet,videoname,fps,verbose)
if video_type=="voronoi": return make_video_with_voronoi(imgs2Notes,pitchSet,volumeSet,videoname,fps,verbose)
if video_type=="symmetry": return make_video_with_symmetry(imgs2Notes,pitchSet,volumeSet,videoname,fps,verbose)
import sys
print(sys.argv)
midi = sys.argv[1] #"./mix_of_midis/midi/markov-3_120bpm_1min_muttertag.mid"
scores = parseXml(fp=midi)
fps = int(sys.argv[2])
bpm = int(sys.argv[3])
print("fps = ", fps, "bpm = ", bpm)
imgs2Notes,pitchSet,volumeSet = convertScore(scores,bpm=bpm,fps=fps,verbose=False)
#print(volumeSet)
#print(imgs2Notes)
vn = sys.argv[4]
video_type = sys.argv[5]
make_video(imgs2Notes,pitchSet,volumeSet,videoname=vn,fps=fps,video_type=video_type,verbose=True)