1 |
import sys |
2 |
from PyQt5.QtWidgets import QApplication, QWidget, QInputDialog, QLineEdit, QFileDialog, QPushButton, QMessageBox, QLabel |
3 |
from PyQt5.QtGui import QIcon |
4 |
from PyQt5.QtCore import pyqtSlot |
5 |
from PyQt5 import QtCore |
6 |
|
7 |
from scipy.io import wavfile |
8 |
import numpy as np |
9 |
import os |
10 |
from sklearn.decomposition import FastICA |
11 |
|
12 |
|
13 |
class App(QWidget): |
14 |
|
15 |
audio1 = "" |
16 |
audio2 = "" |
17 |
|
18 |
def __init__(self): |
19 |
super().__init__() |
20 |
self.title = 'Blind Audio Seperation' |
21 |
self.left = 50 |
22 |
self.top = 50 |
23 |
self.width = 300 |
24 |
self.height = 250 |
25 |
self.initUI() |
26 |
|
27 |
def initUI(self): |
28 |
self.setWindowTitle(self.title) |
29 |
self.setGeometry(self.left, self.top, self.width, self.height) |
30 |
|
31 |
button1 = QPushButton('Choose First Audio', self) |
32 |
button1.setToolTip('This is an example button') |
33 |
button1.move(40, 70) |
34 |
button1.clicked.connect(self.on_click1) |
35 |
|
36 |
#self.path1 = QLabel(self) |
37 |
#self.path1.setGeometry(QtCore.QRect(40, 80, 5, 150)) |
38 |
|
39 |
button2 = QPushButton('Choose Second Audio', self) |
40 |
button2.setToolTip('This is an example button') |
41 |
button2.move(160, 70) |
42 |
button2.clicked.connect(self.on_click2) |
43 |
|
44 |
#self.path2 = QLabel(self) |
45 |
#self.path2.move(160, 100) |
46 |
|
47 |
self.button3 = QPushButton('Start Processing', self) |
48 |
self.button3.setToolTip('This is an example button') |
49 |
self.button3.move(100, 150) |
50 |
self.button3.clicked.connect(self.on_click3) |
51 |
|
52 |
self.show() |
53 |
|
54 |
@pyqtSlot() |
55 |
def on_click1(self): |
56 |
self.audio1 = self.openChoose1() |
57 |
#self.path1.setText(os.path.basename(self.audio1)) |
58 |
|
59 |
|
60 |
@pyqtSlot() |
61 |
def on_click2(self): |
62 |
self.audio2 = self.openChoose2() |
63 |
#self.path2.setText(os.path.basename(self.audio2)) |
64 |
|
65 |
@pyqtSlot() |
66 |
def on_click3(self): |
67 |
if self.audio1 != "" and self.audio2 != "": |
68 |
self.button3.setEnabled(False) |
69 |
self.start_processing() |
70 |
else: |
71 |
btnMsg = QMessageBox.question(self, 'Alert!' , 'Path not Chosen!',QMessageBox.Ok) |
72 |
|
73 |
def openChoose1(self): |
74 |
options = QFileDialog.Options() |
75 |
options |= QFileDialog.DontUseNativeDialog |
76 |
fileName, _ = QFileDialog.getOpenFileName(self, "Choose File 1", "","All Files (*);;Python Files (*.py)", options=options) |
77 |
if fileName: |
78 |
print(fileName) |
79 |
return fileName |
80 |
else: |
81 |
return "" |
82 |
|
83 |
def openChoose2(self): |
84 |
options = QFileDialog.Options() |
85 |
options |= QFileDialog.DontUseNativeDialog |
86 |
fileName, _ = QFileDialog.getOpenFileName(self, "Choose File 2", "","All Files (*);;Python Files (*.py)", options=options) |
87 |
if fileName: |
88 |
print(fileName) |
89 |
return fileName |
90 |
else: |
91 |
return "" |
92 |
|
93 |
|
94 |
def start_processing(self): |
95 |
fs_1, voice_1 = wavfile.read(self.audio1) |
96 |
fs_2, voice_2 = wavfile.read(self.audio2) |
97 |
m, = voice_1.shape |
98 |
voice_2 = voice_2[:m] |
99 |
|
100 |
S = np.c_[voice_1, voice_2] |
101 |
A = np.array([[1, 1], [0.5, 2]]) # Mixing matrix |
102 |
X = np.dot(S, A.T) # Generate observations |
103 |
# Compute ICA |
104 |
ica = FastICA() |
105 |
S_ = ica.fit(X).transform(X) # Get the estimated sources |
106 |
A_ = ica.mixing_ # Get estimated mixing matrix |
107 |
np.allclose(X, np.dot(S_, A_.T)) |
108 |
|
109 |
multiply_factor = 1000000; |
110 |
|
111 |
temp_output_1 = multiply_factor * S_[:, 0] |
112 |
temp_output_2 = multiply_factor * S_[:, 1] |
113 |
|
114 |
wavfile.write("Seperated_1" + ".wav", fs_2, temp_output_1.astype(np.int16)) |
115 |
wavfile.write("Seperated_2" + ".wav", fs_2, temp_output_2.astype(np.int16)) |
116 |
|
117 |
print("Done!") |
118 |
msgDone = QMessageBox.question(self, 'Alert!', 'Processing Done! Check File!', QMessageBox.Ok) |
119 |
self.button3.setEnabled(True) |
120 |
|
121 |
|
122 |
if __name__ == '__main__': |
123 |
app = QApplication(sys.argv) |
124 |
ex = App() |
125 |
sys.exit(app.exec_()) |