-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcontrol_functions.py
330 lines (192 loc) · 5.55 KB
/
control_functions.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
import os
from time import sleep
import threading
import wave
#import numpy as np
import struct
import pyaudio
from PyQt5.QtCore import QObject, pyqtSignal, pyqtSlot
from ffmpeg import Ffmpeg
class Control(QObject):
"""
"""
def __init__(self):
"""
"""
QObject.__init__(self)
self.file = ''
self.file_size = 0
self.app_running = True
self._not_paused = True
self._not_stopped = False
self.t_size = 0
self.tt_played = 0
self.volume_val = 1.4
self.ff = Ffmpeg()
print(threading.enumerate())
stillPlaying = pyqtSignal(str, arguments=['playing'])
completedPlaying = pyqtSignal(str, arguments=["complete"])
@pyqtSlot(str, str, str)
def play(self, file, f_for, size):
"""
"""
self._not_stopped = False
sleep(2)
self.file = file
self.file_size = int(size)
play_thread = threading.Thread(target=self._play, args=[f_for])
play_thread.start()
def _play(self, f_for):
"""
"""
splits = os.path.split(self.file)
filename = splits[1].replace(f_for, 'wav')
file = self.ff.sav_dir + '/' + filename
if self.app_running:
self.ff.convert(self.file, f_for)
else:
return 1
print('quick or ')
pyaud = pyaudio.PyAudio()
wf = wave.open(file, mode='rb')
stream = pyaud.open(format=pyaud.get_format_from_width(wf.getsampwidth()),
channels=wf.getnchannels(),
rate=wf.getframerate(),
output=True)
self.playing()
self._not_stopped = True
self._not_paused = True
a = wf.readframes(1)
print('\n\n inside here \n', self._not_paused, self._not_stopped )
while self.app_running and len(a) != 0:
if self._not_stopped:
if self._not_paused:
stream.write(a)
#a = wf.readframes(512)
a = (np.fromstring(wf.readframes(512), np.int16) )
self.t_played()
b = []
for x in a:
var = int(float(x) / self.volume_val)
b.append(var)
a = b
a = struct.pack('h'*len(a), *a)
else:
#pause
sleep(.1)
else:
break
wf.close()
stream.stop_stream()
stream.close()
pyaud.terminate()
self.complete()
@pyqtSlot()
def stop(self):
"""
"""
stop_thread = threading.Thread(target=self._stop)
stop_thread.start()
# implement a wait
sleep(1)
def _stop(self):
"""
"""
self._not_stopped = False
return
@pyqtSlot()
def pause(self):
"""
"""
pause_thread = threading.Thread(target=self._pause)
pause_thread.start()
sleep(1)
def _pause(self):
"""
"""
self._not_paused = False
return
@pyqtSlot()
def resume(self):
"""
"""
resume_thread = threading.Thread(target=self._resume)
resume_thread.start()
sleep(1)
def _resume(self):
"""
"""
self._not_paused = True
return
def playing(self):
"""
"""
self.stillPlaying.emit('playing')
def complete(self):
"""
"""
print('complete')
if self._not_paused:
self.completedPlaying.emit('')
elif self._not_stopped:
pass
else:
self.completedPlaying.emit('')
@pyqtSlot(str)
def controlVolume(self, deci):
"""
"""
cont = threading.Thread( target=self._controlVolume, args=[deci] )
cont.start()
def _controlVolume(self, deci):
"""
"""
vol = float(deci)
vol = format(100 / vol, '.1f')
r_vol = float(vol)
self.volume_val = r_vol
def t_played(self):
"""
"""
t_play = threading.Thread( target = self._t_played )
t_play.start()
def _t_played(self):
"""
"""
self.tt_played += 512
per = self.tt_played / self.file_size * 100
print(per)
print(self.file_size)
print(self.tt_played)
return
def propertyNotify(self, prop):
self.prop = prop
propNoti = threading.Thread(target = self._propertyNotify)
propNoti.start()
def propertyNotifier(self, result):
self.propertyChanged.emit(result)
def _propertyNotify(self):
while self.app_running and self._not_stopped:
sleep(.3)
count = self.prop
if count > self.filesPrevCount:
self.filesPrevCount = count
self.propertyNotifier([count, self.prop])
def endPropertyChange(self):
sleep(1)
count = len(self.prop)
result = [count, '']
# emit the end of property
self.endOfPropertyChange.emit(result)
def endProperty(self):
self.now_crawling = False
self.endPropertyChange()
endProp = threading.Thread( target = self._endProperty )
endProp.start()
def _endProperty(self):
sleep(15)
self.prop = 0
self.propertyEnded()
def propertyEnded(self):
result = []
self.propertyEnd.emit(result)