forked from ozancaglayan/SPID
/
main.py
executable file
·182 lines (140 loc) · 6.51 KB
/
main.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
from PyQt4 import QtGui
from PyQt4 import QtCore
from PyQt4.phonon import Phonon
from ui.ui_mainwindow import Ui_SPIDMainWindow
from spidrecordwindow import SPIDRecordWindow
from spididentifywindow import SPIDIdentifyWindow
from Marf import Marf
import os
class SPID(QtGui.QDialog, Ui_SPIDMainWindow):
def __init__(self, parent=None):
QtGui.QDialog.__init__(self, parent)
self.setupUi(self)
# Create Phonon Music Player
self.phonon = Phonon.createPlayer(Phonon.MusicCategory)
self.phonon.stateChanged.connect(self.slotPhononStateChanged)
self.phonon.finished.connect(self.slotPhononPlaybackFinished)
# Connect signals for buttons
self.comboBoxUsers.currentIndexChanged.connect(self.reflectUserProperties)
self.pushButtonRecordSample.clicked.connect(self.slotShowRecordWindow)
# Enable button when item clicked
self.listWidgetEnrollments.itemClicked.connect(lambda: self.pushButtonPlay.setEnabled(True))
# Play sample if double-clicked
self.listWidgetEnrollments.itemDoubleClicked.connect(self.slotStartPlayback)
# Start/Stop Playback
self.pushButtonPlay.clicked.connect(self.slotStartPlayback)
self.pushButtonStop.clicked.connect(self.slotStopPlayback)
self.pushButtonTrain.clicked.connect(self.slotShowTrainingDialog)
self.pushButtonIdentify.clicked.connect(self.slotShowIdentifyDialog)
self.pushButtonAddSpeaker.clicked.connect(self.slotAddSpeaker)
self.pushButtonPlayTestingSample.clicked.connect(self.slotPlayTestingSample)
self.lineEditNewSpeaker.textEdited.connect(self.slotCheckNewSpeaker)
# Create Marf instance
self.marf = Marf()
# Fill speaker list
self.fill_speaker_list()
def fill_speaker_list(self):
# Fill user list with speakers
self.comboBoxUsers.clear()
for speaker in self.marf.get_all_speakers():
self.comboBoxUsers.addItem(speaker.s_name, QtCore.QVariant(speaker.s_id))
def slotCheckNewSpeaker(self, text):
# Enable/Disable button according to the available text
self.pushButtonAddSpeaker.setEnabled(bool(text))
def slotAddSpeaker(self):
s_name = self.lineEditNewSpeaker.text()
self.lineEditNewSpeaker.clear()
# Add speaker to marf DB
self.marf.add_speaker(unicode(s_name), [], [])
# Refresh the list
self.fill_speaker_list()
self.comboBoxUsers.setCurrentIndex(self.comboBoxUsers.findText(s_name))
def slotShowTrainingDialog(self):
self.groupBox.setEnabled(False)
QtGui.qApp.processEvents()
self.setWindowTitle("Training...")
QtGui.qApp.processEvents()
QtGui.QApplication.setOverrideCursor(QtGui.QCursor(QtCore.Qt.WaitCursor))
# Train
self.marf.write_speakers()
ret = self.marf.train()
# Restore cursor
QtGui.QApplication.restoreOverrideCursor()
QtGui.QApplication.restoreOverrideCursor()
self.setWindowTitle("Speaker Identification")
self.groupBox.setEnabled(True)
def slotPhononPlaybackFinished(self):
self.pushButtonStop.setEnabled(False)
def slotPhononStateChanged(self, state):
if state == Phonon.StoppedState:
self.pushButtonStop.setEnabled(False)
elif state == Phonon.PlayingState:
self.pushButtonStop.setEnabled(True)
def slotPlayTestingSample(self):
s_id, ok = self.comboBoxUsers.itemData(self.comboBoxUsers.currentIndex()).toInt()
# Take the first one, not so important
file_path = self.marf.get_testing_samples(s_id)[0]
file_path = self.marf.get_testing_sample_path(file_path)
self.phonon.setCurrentSource(Phonon.MediaSource(file_path))
self.phonon.play()
def slotStartPlayback(self, item):
if item:
file_path = item.text()
else:
file_path = self.listWidgetEnrollments.currentItem().text()
file_path = self.marf.get_training_sample_path(unicode(file_path))
# Set file path and start playing
self.phonon.setCurrentSource(Phonon.MediaSource(file_path))
self.phonon.play()
def slotStopPlayback(self):
self.phonon.stop()
def slotShowRecordWindow(self):
self._last_id, ok = self.comboBoxUsers.itemData(self.comboBoxUsers.currentIndex()).toInt()
recordWindow = SPIDRecordWindow(self, self._last_id)
recordWindow.show()
def slotIdentifyFinished(self):
self.groupBox.setEnabled(False)
QtGui.qApp.processEvents()
self.setWindowTitle("Identifying...")
QtGui.qApp.processEvents()
QtGui.QApplication.setOverrideCursor(QtGui.QCursor(QtCore.Qt.WaitCursor))
ret = self.marf.identify(os.path.basename(self._last_testing_filename))
# Restore cursor
QtGui.QApplication.restoreOverrideCursor()
QtGui.QApplication.restoreOverrideCursor()
self.setWindowTitle("Speaker Identification")
self.groupBox.setEnabled(True)
QtGui.QMessageBox.information(self,
"Identification Results",
"<b>Identified Speaker: </b>%s" % ret)
def slotShowIdentifyDialog(self):
identifyWindow = SPIDIdentifyWindow(self)
identifyWindow.show()
def slotSampleRecordingFinished(self):
fileName = os.path.basename(self._fileName)
if fileName.startswith("testing"):
self.marf.update_speaker(self._last_id, s_testing=fileName)
else:
self.marf.update_speaker(self._last_id, s_training=fileName)
# Refresh the list
self.reflectUserProperties(self.comboBoxUsers.currentIndex())
def reflectUserProperties(self, index):
s_id, ok = self.comboBoxUsers.itemData(index).toInt()
training_samples = self.marf.get_training_samples(s_id)
self.listWidgetEnrollments.clear()
testing_sample = bool(self.marf.get_testing_samples(s_id))
self.pushButtonPlayTestingSample.setEnabled(bool(testing_sample))
self.pushButtonPlay.setEnabled(False)
for sample in training_samples:
item = QtGui.QListWidgetItem(QtGui.QIcon(":/audio-ac3.png"),
sample,
self.listWidgetEnrollments)
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
QtGui.QApplication.setApplicationName("SPID")
spid = SPID()
spid.show()
app.exec_()