forked from JoseDiogo/PIMP-U.Jr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·298 lines (235 loc) · 10.1 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
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
#!/usr/bin/env python3
import sys
import os
import search
import download
import player
import speech
from PyQt5 import QtWidgets, QtCore
from gui import Ui_MainWindow
try:
import sense_hat
import icons
except ModuleNotFoundError:
print('Couldn\'t import sense_hat')
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
signal_start_transcribing = QtCore.pyqtSignal()
signal_start_searching = QtCore.pyqtSignal(str)
signal_start_downloading = QtCore.pyqtSignal(str)
def __init__(self):
super(MainWindow, self).__init__()
self.setupUi(self)
self.ext = '.flac'
self.playlist_file = 'playlist.txt'
self.playlist = list()
if os.path.isfile(self.playlist_file):
with open(self.playlist_file, 'r') as file:
for line in file.readlines():
self.playlist.append(line.strip())
# 1 GiB
self.cache_size = 1073741824
while self.calculate_size() > self.cache_size:
os.remove(self.oldest_file())
if len(self.playlist) > 0:
self.current_video_id = self.playlist[0]
else:
self.current_video_id = str()
self.music_player = player.Player()
try:
self.sense_hat = sense_hat.SenseHat()
except NameError:
self.sense_hat = None
self.downloader_thread = QtCore.QThread()
self.downloader = Downloader(self)
self.downloader.moveToThread(self.downloader_thread)
self.signal_start_downloading.connect(self.downloader.download)
self.downloader_thread.start()
self.search_thread = QtCore.QThread()
self.search = YoutubeSearch(self)
self.search.moveToThread(self.search_thread)
self.signal_start_searching.connect(self.search.search)
self.search_thread.start()
self.horizontalSlider.setValue(100)
self.horizontalSlider.valueChanged.connect(self.update_volume)
self.pushButton_close.clicked.connect(self.close)
self.pushButton_mute.clicked.connect(self.media_mute)
self.pushButton_play.clicked.connect(self.media_play)
self.pushButton_pause.clicked.connect(self.media_pause)
self.pushButton_stop.clicked.connect(self.media_stop)
self.pushButton_search.clicked.connect(self.start_text)
self.lineEdit.returnPressed.connect(self.start_text)
self.pushButton_forwards.clicked.connect(self.media_forward)
self.pushButton_back.clicked.connect(self.media_backward)
if speech.found_module:
self.voice_thread = QtCore.QThread()
self.voice_recognition = VoiceRecognition(self)
self.voice_recognition.moveToThread(self.voice_thread)
self.signal_start_transcribing.connect(self.voice_recognition.transcribe)
self.voice_thread.start()
self.pushButton_voice.clicked.connect(self.start_voice)
else:
self.pushButton_voice.setEnabled(False)
if self.sense_hat is not None:
self.sense_hat.stick.direction_up = self.volume_up
self.sense_hat.stick.direction_down = self.volume_down
self.sense_hat.stick.direction_left = self.media_backward
self.sense_hat.stick.direction_right = self.media_forward
def search_download_and_play(self, search_terms):
self.signal_start_searching.emit(search_terms)
def start_text(self):
self.search_download_and_play(self.lineEdit.text())
def start_voice(self):
self.signal_start_transcribing.emit()
@QtCore.pyqtSlot(str)
def start_voice_2(self, transcription):
if transcription.lower() == 'pause':
self.media_pause()
elif transcription.lower() == 'play':
self.media_play()
elif transcription.lower() == 'stop':
self.media_stop()
elif transcription.lower() == 'forward' or transcription.lower() == 'forwards' \
or transcription.lower() == 'next':
self.media_forward()
elif transcription.lower() == 'backward' or transcription.lower() == 'backwars' \
or transcription.lower() == 'back' or transcription.lower() == 'previous':
self.media_backward()
else:
self.search_download_and_play(transcription)
def update_volume(self):
volume = self.horizontalSlider.value() + 1
self.music_player.set_volume(volume)
self.label_volume.setText(str(volume) + '%')
if self.sense_hat is not None:
if volume < 33:
self.sense_hat.set_pixels(icons.volume_min)
elif volume < 66:
self.sense_hat.set_pixels(icons.volume_med)
else:
self.sense_hat.set_pixels(icons.volume_max)
def media_mute(self):
self.music_player.mute()
self.pushButton_mute.setText('Unmute')
if self.sense_hat is not None:
self.sense_hat.set_pixels(icons.mute)
self.pushButton_mute.clicked.disconnect()
self.pushButton_mute.clicked.connect(self.media_unmute)
def media_unmute(self):
self.music_player.unmute()
self.pushButton_mute.setText('Mute')
if self.sense_hat is not None:
self.sense_hat.set_pixels(icons.play)
self.pushButton_mute.clicked.disconnect()
self.pushButton_mute.clicked.connect(self.media_mute)
def media_play(self):
self.music_player.play()
self.label_status.setText('Playing')
if self.sense_hat is not None:
self.sense_hat.stick.direction_middle = self.media_pause
self.sense_hat.set_pixels(icons.play)
def media_pause(self):
self.music_player.pause()
self.label_status.setText('Paused')
if self.sense_hat is not None:
self.sense_hat.stick.direction_middle = self.media_play
self.sense_hat.set_pixels(icons.pause)
def media_stop(self):
self.music_player.stop()
self.label_status.setText('Stopped')
def media_open(self, file):
self.music_player.open(file)
if not file.split(sep='.')[0] in self.playlist:
self.add_to_playlist(file.split(sep='.')[0])
def volume_up(self):
volume = self.horizontalSlider.value() + 1
self.horizontalSlider.setValue(volume + 10)
def volume_down(self):
volume = self.horizontalSlider.value() + 1
self.horizontalSlider.setValue(volume - 10)
def add_to_playlist(self, video_id):
self.playlist.append(video_id)
with open(self.playlist_file, 'a') as file:
file.write(video_id + '\n')
def media_forward(self):
if len(self.playlist) > 0 and self.current_video_id != '':
if self.sense_hat is not None:
self.sense_hat.set_pixels(icons.forward)
index = self.playlist.index(self.current_video_id)
target_index = index + 1
if target_index > len(self.playlist) - 1:
target_index = len(self.playlist) - 1
self.download_and_play(self.playlist[target_index])
def media_backward(self):
if len(self.playlist) > 0 and self.current_video_id != '':
if self.sense_hat is not None:
self.sense_hat.set_pixels(icons.backwards)
index = self.playlist.index(self.current_video_id)
target_index = index - 1
if target_index < 0:
target_index = 0
self.download_and_play(self.playlist[target_index])
@QtCore.pyqtSlot(str)
def download_and_play(self, video_id):
self.current_video_id = video_id
if not os.path.isfile(self.current_video_id + self.ext):
self.signal_start_downloading.emit(self.current_video_id)
else:
self.play_file()
@QtCore.pyqtSlot()
def play_file(self):
self.media_open(self.current_video_id + self.ext)
self.media_play()
def calculate_size(self):
total_size = 0
for filename in os.listdir(os.getcwd()):
if filename.endswith('.flac'):
total_size += os.path.getsize(filename)
return total_size
def oldest_file(self):
oldest_file = str()
oldest_time = 0.0
for filename in os.listdir(os.getcwd()):
if filename.endswith('.flac'):
if oldest_time == 0.0:
oldest_time = os.path.getmtime(filename)
oldest_file = filename
else:
if os.path.getmtime(filename) < oldest_time:
oldest_time = os.path.getmtime(filename)
oldest_file = filename
return oldest_file
class VoiceRecognition(QtCore.QObject):
signal_finished_transcribing = QtCore.pyqtSignal(str)
def __init__(self, main, parent=None):
super(VoiceRecognition, self).__init__(parent)
self.main = main
self.signal_finished_transcribing.connect(self.main.start_voice_2)
@QtCore.pyqtSlot()
def transcribe(self):
transcription = speech.transcribe()
self.signal_finished_transcribing.emit(transcription)
class YoutubeSearch(QtCore.QObject):
signal_finished_searching = QtCore.pyqtSignal(str)
def __init__(self, main, parent=None):
super(YoutubeSearch, self).__init__(parent)
self.main = main
self.signal_finished_searching.connect(self.main.download_and_play)
@QtCore.pyqtSlot(str)
def search(self, search_terms):
video_id = search.search(search_terms)
self.signal_finished_searching.emit(video_id)
class Downloader(QtCore.QObject):
signal_finished_downloading = QtCore.pyqtSignal()
def __init__(self, main, parent=None):
super(Downloader, self).__init__(parent)
self.main = main
self.signal_finished_downloading.connect(self.main.play_file)
@QtCore.pyqtSlot(str)
def download(self, video_id):
download.download(video_id)
self.signal_finished_downloading.emit()
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())