-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEmashupper.py
504 lines (421 loc) · 18.9 KB
/
Emashupper.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
# -*- coding:utf-8 -*-
from sys import platform as sys_pf
if sys_pf == 'darwin':
import matplotlib
matplotlib.use("Qt4Agg")
from Tkinter import *
from lib import pre, mashup
from pandas import read_csv
import os
from subprocess import call,Popen
import numpy as np
import pygame
import matplotlib.pyplot as plt
import librosa.display
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt4agg import NavigationToolbar2QT as NavigationToolbar
from matplotlib.figure import Figure
#Please Modify This Parameter Only#
DataLocationA='../db'
##########
SongPgzLocation = os.path.join(DataLocationA,'pgz/')
PgzLocation= os.path.join(DataLocationA,'pgz/')
WavLocation= os.path.join(DataLocationA,'song/')
from random import randint
from PyQt4 import QtGui
from PyQt4 import QtCore
pygame.mixer.init()
pygame.mixer.set_num_channels(2)
LoadMode = 1
# 0 would load pgz seperately, which might be slower but use less memory
# 1 would load all pgz at once, which might be faster but use more memory
def pathJoin(path,fileName):
a = path
if a.endswith('/'):
b = a + fileName
else :
b = a + '/' + fileName
return b
def nameJoin(path,fileName):
a = path
b = fileName
a = "".join(str(a).split(' '))
b = "".join(str(b).split(' '))
return a+b
class Figure_Canvas(FigureCanvas):
def __init__(self, parent=None, width=11, height=5, dpi=100):
fig = Figure(figsize=(width, height), dpi=100)
FigureCanvas.__init__(self, fig)
self.setParent(parent)
self.axes = fig.add_subplot(111)
def draw(self,x,y,title):
self.axes.clear()
self.axes.set_title(title)
self.axes.plot(x[0],y)
class Window(QtGui.QWidget):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.parent = parent
self.initVar()
self.initUI()
def initVar(self):
# initialize public variable
self.csv = ''
self.cateIndex = -1
self.cateName = ''
self.seedIndex = -1
self.seedSegCount = -1
self.seedName = ''
self.seed = [None] * self.seedSegCount #PreAudio list of seed song
self.mashup = [None] * self.seedSegCount #PreAudio list of selected candidated song
self.mashuppedSig = None
def initUI(self):
#initialize UI
self.setGeometry(150, 30, 830, 540)
self.setWindowTitle('Emashupper')
palette1 = QtGui.QPalette()
palette1.setColor(self.backgroundRole(), QtGui.QColor(192,253,123))
palette1.setBrush(self.backgroundRole(), QtGui.QBrush(QtGui.QPixmap('./material/bg.jpg').scaledToHeight(540)))
self.setPalette(palette1)
self.setAutoFillBackground(True)
self.listbox()
self.actionElements()
# self.graphicview = QtGui.QGraphicsView()
# self.graphicview.setObjectName("graphicview")
def listbox(self):
# show all category list
self.cateListWidget = QtGui.QListWidget(self)
self.catelist = []
s=filter(lambda x: os.path.isdir(os.path.join(WavLocation, x)), os.listdir(WavLocation))
for cate in s:
self.cateListWidget.addItem(cate)
self.catelist.append(cate)
self.cateListWidget.setAutoFillBackground(True)
self.cateListWidget.setStyleSheet('''
color: white;
background-image: url('./material/Starry_sky.png');
''')
self.cateListWidget.setFont(QtGui.QFont("Courier",15))
self.cateListWidget.itemSelectionChanged.connect(self.onselect)
#set header
self.cateListWidget.move(30, 90)
self.cateListWidget.resize(225, 245)
self.cateListWidget.show()
def onselect(self):
# event helper for listbox slection
# Note here that Tkinter passes an event object to onselect()
if not self.cateListWidget.selectedItems():
print ("Please select a category first.")
else:
index = self.cateListWidget.currentRow()
value = self.cateListWidget.currentItem().text()
self.cateIndex = index
self.cateName = str(value)
print 'selected %d: "%s"' % (index, value)
def actionElements(self):
# initialze buttons,labels...
seed_generate = QtGui.QPushButton("Generate Random Seed -> ")
seed_generate.setFont(QtGui.QFont("Courier",15))
seed_generate.setStyleSheet('''
background-image: url('./material/button.png');
background-color: rgba(255, 255, 255, 0);
''')
seed_generate.clicked.connect(self.seedGenerate)
self.seedNameShow = QtGui.QLabel("\nSeed: ")
self.seedNameShow.setFont(QtGui.QFont("Courier",15))
self.progressBar = QtGui.QLabel("\n")
self.progressBar.setFont(QtGui.QFont("Courier",15))
load_file = QtGui.QPushButton("Load Seed Song Segments ")
load_file.setFont(QtGui.QFont("Courier",15))
load_file.setStyleSheet('''
background-image: url('./material/button.png');
background-color: rgba(255, 255, 255, 0);
''')
load_file.clicked.connect(self.load)
play_seed = QtGui.QPushButton("Play Seed Song")
play_seed.setFont(QtGui.QFont("Courier",15))
play_seed.setStyleSheet('''
background-image: url('./material/button.png');
background-color: rgba(255, 255, 255, 0);
''')
play_seed.clicked.connect(self.playSeed)
stop_seed = QtGui.QPushButton("Stop Playing Seed")
stop_seed.setFont(QtGui.QFont("Courier",15))
stop_seed.setStyleSheet('''
background-image: url('./material/button.png');
background-color: rgba(255, 255, 255, 0);
''')
stop_seed.clicked.connect(self.stopPlaySeed)
show_seed = QtGui.QPushButton("Show Seed Wave")
show_seed.setFont(QtGui.QFont("Courier",15))
show_seed.setStyleSheet('''
background-image: url('./material/button.png');
background-color: rgba(255, 255, 255, 0);
''')
show_seed.clicked.connect(self.seedShow)
do_mash = QtGui.QPushButton("Mashup")
do_mash.setFont(QtGui.QFont("Courier",15))
do_mash.setStyleSheet('''
background-image: url('./material/button.png');
background-color: rgba(255, 255, 255, 0);
''')
do_mash.clicked.connect(self.mashupSong)
play_mash = QtGui.QPushButton("Play Mashupped Song")
play_mash.setFont(QtGui.QFont("Courier",15))
play_mash.setStyleSheet('''
background-image: url('./material/button.png');
background-color: rgba(255, 255, 255, 0);
''')
play_mash.clicked.connect(self.playMashuped)
stop_mash = QtGui.QPushButton("Stop Playing")
stop_mash.setFont(QtGui.QFont("Courier",15))
stop_mash.setStyleSheet('''
background-image: url('./material/button.png');
background-color: rgba(255, 255, 255, 0);
''')
stop_mash.clicked.connect(self.stopPlayMashuped)
show_mash = QtGui.QPushButton("Show Mashupped Song Wave")
show_mash.setFont(QtGui.QFont("Courier",15))
show_mash.setStyleSheet('''
background-image: url('./material/button.png');
background-color: rgba(255, 255, 255, 0);
''')
show_mash.clicked.connect(self.showMashuped)
layout0 = QtGui.QVBoxLayout()
layout0.addWidget(seed_generate)
layout0.setAlignment(QtCore.Qt.AlignTop)
emp = QtGui.QLabel("")
layout2 = QtGui.QVBoxLayout()
layout2.setAlignment(QtCore.Qt.AlignTop)
layout2.addWidget(self.seedNameShow)
layout2.addWidget(emp)
layout2.addWidget(emp)
layout2.addWidget(load_file)
layout2.addWidget(emp)
layout2.addWidget(play_seed)
layout2.addWidget(emp)
layout2.addWidget(stop_seed)
layout2.addWidget(emp)
layout2.addWidget(show_seed)
layout3 = QtGui.QVBoxLayout()
layout3.setAlignment(QtCore.Qt.AlignTop)
layout3.addWidget(self.progressBar)
layout3.addWidget(emp)
layout3.addWidget(emp)
layout3.addWidget(do_mash)
layout3.addWidget(emp)
layout3.addWidget(play_mash)
layout3.addWidget(emp)
layout3.addWidget(stop_mash)
layout3.addWidget(emp)
layout3.addWidget(show_mash)
layout = QtGui.QHBoxLayout()
layout.addLayout(layout0)
layout.addStretch()
layout.addLayout(layout2)
layout.addStretch()
layout.addLayout(layout3)
self.setLayout(layout)
def load(self):
c = self.csv # To simpilfy
self.seedIndex = c.index[c['song name'] == self.seedName].tolist()[0]
self.seedSegCount= self.csv['segmentation count'][self.seedIndex]
print self.seedIndex
print self.seedName
self.seed = [None] * self.seedSegCount
for i in xrange(0,self.seedSegCount):
name = pathJoin(PgzLocation+self.cateName+'/inst/',self.seedName+'(inst)'+'_'+str(i+1)+'.pgz')
self.seed[i] = pre.load(name)
print 'loaded ',self.seed[i].name
self.progressBar.setText("\nDone Loading Segments.")
def seedGenerate(self):
#open the songlist(csv) of the chosen category, and show the ramdomly choice
self.csv = read_csv(PgzLocation+self.cateName+'/metadata.csv')
# print self.cateName
songs = [s.rstrip('\n') for s in self.csv['song name']]
self.seedName = songs[randint(1,len(songs)-1)]
seedLabel = self.seedName[:18]
last = self.seedName[18:]
while len(last) >= 24:
seedLabel = seedLabel + '\n' + last[:24]
last = last[24:]
seedLabel = seedLabel + '\n' + last
self.seedNameShow.setText("\nSeed: "+seedLabel)
def seedShow(self):
signal = self.seed[0].signal
for i in self.seed[1:]:
signal = np.concatenate((signal,i.signal))
# dr = Figure_Canvas()
# dr.draw([[i for i in xrange(len(signal))],self.seed[0].sr], signal,'Seed Song Waveplot')
# graphicscene = QtGui.QGraphicsScene()
# graphicscene.addWidget(dr)
# self.graphicview.setScene(graphicscene)
# self.graphicview.setWindowTitle('Seed Song Waveplot')
# self.graphicview.show()
librosa.display.waveplot(signal, sr=self.seed[0].sr)
plt.title('Seed Song Waveplot')
plt.show()
def playSeed(self):
seedsound = pygame.mixer.Sound(os.path.join(WavLocation+self.cateName+'/inst/',self.seedName+'(inst)'+ '_1.wav'))
if not pygame.mixer.Channel(1).get_busy():
pygame.mixer.Channel(1).play(seedsound)
# if sys.platform == 'darwin':
# Popen(["afplay",os.path.join(WavLocation+self.cateName+'/inst/',self.seedName+'(inst)'+ '_1.wav')])
def stopPlaySeed(self):
pygame.mixer.Channel(1).stop()
def mashupLoadAtOnce(self):
self.mashup = [None]*self.seedSegCount
mashabilityList = [None]*self.seedSegCount
mashupedIndex = [] # saved already mashuped seg index to filter repeat
threshold = 0.82
seg = []
# load all segmentation
for candIndex,candName in enumerate(self.csv['song name']):
if candIndex == self.seedIndex:
continue
for candSegIndex in xrange(1,self.csv['segmentation count'][candIndex]+1):
candSegPath = pathJoin(PgzLocation+self.cateName+'/inst/',candName+'(inst)'+'_'+str(candSegIndex)+'.pgz')
seg.append(pre.load(candSegPath))
print "loaded all segments"
#Mashupping
for seedSegNow in xrange(0,self.seedSegCount):
# iterate all segmentations in seed song
maxMashability = -1000
maxSeg = None
maxIndex = -1
for segIndex ,cand in enumerate(seg):
# iterate all segmentation
masha = pre.Mashability(self.seed[seedSegNow], cand).mash()
if maxMashability < masha:
maxMashability = masha
maxSeg = cand
maxIndex = segIndex
if segIndex % 10 == 0:
print 'compared : ',segIndex, ' of ' ,len(seg)
print 'maxSeg = ',maxSeg.name
mashabilityList[seedSegNow] = maxMashability
vocalSegPath = pathJoin(SongPgzLocation+self.cateName+'/vocal/',maxSeg.name[:maxSeg.name.rfind('(inst')] + '(vocal)' + maxSeg.name[maxSeg.name.rfind('_'):]+'.pgz')
print 'Mashed File: '+ maxSeg.name[:maxSeg.name.rfind('(inst')] + '(vocal)' + maxSeg.name[maxSeg.name.rfind('_'):]+'.wav'
if maxMashability >= threshold :
self.mashup[seedSegNow] = pre.load(vocalSegPath)
else:
print('Mashability = '+str(maxMashability)+' < Threshold , Skip ...')
print "Mashed : #"+str(seedSegNow+1)+" of "+str(self.seedSegCount)
del seg[maxIndex]
print 'selected : '
for index, candSeg in enumerate(self.mashup):
if candSeg :
print candSeg.name,' : ', mashabilityList[index]
seg = [None] * self.seedSegCount
for i in xrange(self.seedSegCount):
# overlay cand and seed
if self.mashup[i] :
instSegPath = pathJoin(WavLocation+self.cateName+'/inst/',maxSeg.name + '.wav')
instSig, instSr = librosa.load(instSegPath,sr = None)
seg[i] = mashup.overlay(instSig, instSr, self.seed[i].signal,self.seed[i].sr, self.mashup[i].signal, self.mashup[i].sr)
else :
seg[i] = self.seed[i].signal
# bridging
if i == 0 :
signal = seg[i]
else:
signal = mashup.bridging(signal, self.seed[0].sr,seg[i], self.seed[0].sr,5000)
self.mashuppedSig = signal
self.saveMashuped()
self.progressBar.setText("\nMashupped Song\nhas been saved.")
def mashupLoadSeperately(self):
self.mashup= [None]*self.seedSegCount
mashabilityList = [None]*self.seedSegCount
mashupedDic={}
threshold = 0.82
for seedSegNow in xrange(0,self.seedSegCount):
# iterate all segmentations in seed song
maxMashability = -1000
candSeg = None
maxSeg = None
for candIndex,candName in enumerate(self.csv['song name']):
# iterate all songs in database
if candIndex == self.seedIndex :
continue
for candSegIndex in xrange(1,self.csv['segmentation count'][candIndex]+1):
# iterate all segmentations in candidate song
candSegPath = pathJoin(PgzLocation+self.cateName+'/inst/',candName+'_'+str(candSegIndex)+'.pgz')
if canSedPath[:-4] in mashupedDic:
print canSedPath, ' is already mashupped '
continue
candSeg = pre.load(candSegPath)
mash = pre.Mashability(self.seed[seedSegNow], candSeg).mash()
if maxMashability < mash:
maxMashability = mash
maxSeg = candSeg
print 'compared :',candName
if maxSeg is not None:
print 'maxSeg = ',maxSeg.name
mashabilityList[seedSegNow] = maxMashability
print 'Mashed : #',seedSegNow+1 ,' of ', self.seedSegCount
vocalSegPath = pathJoin(SongPgzLocation+self.cateName+'/vocal/',maxSeg.name[:maxSeg.name.rfind('(inst')] + '(vocal)' + maxSeg.name[maxSeg.name.rfind('_'):]+'.pgz')
if maxMashability >= threshold :
self.mashup[seedSegNow] = pre.load(vocalSegPath)
else: print('Mashability = ' + maxMashability + ' < Threshold , Skip...')
mashupedDic.add({maxSeg.name:True})
print 'selected : '
for index, candSeg in enumerate(self.mashup):
print candSeg.name,' : ', mashabilityList[index]
seg = [None] * self.seedSegCount
for i in xrange(self.seedSegCount):
# overlay cand and seed
if self.mashup[i] :
instSegPath = pathJoin(cateLocation+self.cateName+WavLocation,maxSeg.name + '.wav')
instSig, instSr = librosa.load(instSegPath,sr = None)
seg[i] = mashup.overlay(instSig, instSr, self.seed[i].signal,self.seed[i].sr, self.mashup[i].signal, self.mashup[i].sr)
else :
seg[i] = self.seed[i].signal
# bridging
if i == 0 :
signal = seg[i]
else:
signal = mashup.bridging(signal, self.seed[0].sr,seg[i], self.seed[0].sr,5000)
self.mashuppedSig = signal
self.saveMashuped()
def mashupSong(self):
if LoadMode == 0:
print 'Load canidate seperately'
self.mashupLoadSeperately()
elif LoadMode == 1:
print 'Load canidate at once'
self.mashupLoadAtOnce()
def saveMashuped(self):
outputFile = str(nameJoin(str(nameJoin('./',self.seedName)),'_mashupped.wav'))
librosa.output.write_wav(outputFile,self.mashuppedSig,self.seed[0].sr)
mashup.volume_adjust(outputFile)
print ('\nDone processing mashupped song.')
def playMashuped(self):
mashsound = pygame.mixer.Sound(str(nameJoin(self.seedName,'_mashupped(NORMALIZED).wav')))
if not pygame.mixer.Channel(0).get_busy():
pygame.mixer.Channel(0).play(mashsound)
# if sys.platform == 'darwin':
# Popen(["afplay",str(nameJoin(self.seedName,'_mashupped(NORMALIZED).wav'))])
def stopPlayMashuped(self):
pygame.mixer.Channel(0).stop()
def showMashuped(self):
if len(self.mashuppedSig) >= 1 :
# dr = Figure_Canvas()
# dr.draw([[i for i in xrange(len(self.mashuppedSig))],self.seed[0].sr], self.mashuppedSig,'Mashupped Song Waveplot')
# graphicscene = QtGui.QGraphicsScene()
# graphicscene.addWidget(dr)
# self.graphicview.setScene(graphicscene)
# self.graphicview.setWindowTitle('Mashupped Song Waveplot')
# self.graphicview.show()
librosa.display.waveplot(self.mashuppedSig, sr=self.seed[0].sr)
plt.title('Mashupped Song Waveplot')
plt.show()
####################################################################
if __name__ == '__main__':
print 'Data : ',DataLocationA
print 'pgz(inst) : ', PgzLocation
print 'pgz(vocal) : ', SongPgzLocation
print 'wav : ', WavLocation
app = QtGui.QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())