-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplayeraser.py
More file actions
104 lines (80 loc) · 2.21 KB
/
playeraser.py
File metadata and controls
104 lines (80 loc) · 2.21 KB
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
#!/usr/bin/python3
import sounddevice
import soundfile
import curses
import glob
import os
import shutil
import time
DO_ERASE = True
IDLERATE = 100
REPLIQUES_DIRNAME = '_repliques_'
SPEAKER_FILENAME = 'speaker.txt'
SPEECH_FILENAME = 'speech.flac'
TEXT_FILENAME = 'text.txt'
def run(scr):
scr.nodelay(True)
if curses.can_change_color():
curses.init_color(0, 0, 0, 0)
curses.curs_set(0)
scr.hline(curses.LINES - 2, 0, curses.ACS_HLINE, curses.COLS)
scr.refresh()
wnd_main = curses.newwin(curses.LINES - 2, curses.COLS, 0, 0)
wnd_main.idlok(True) # seems to work even without this...
wnd_main.scrollok(True)
wnd_help = curses.newwin(1, curses.COLS, curses.LINES - 1, 0)
wnd_help.idlok(True)
wnd_help.scrollok(True)
wnd_help.addstr('Q: quit | SPACE: next replique')
wnd_help.refresh()
os.chdir(REPLIQUES_DIRNAME)
dirnames = glob.glob('*')
os.chdir('..')
i = None
for dirname in dirnames:
try:
n = int(dirname)
i = n if (i is None) else min(i, n)
except ValueError:
pass
if i is None:
i = 1
dirpath = f"{REPLIQUES_DIRNAME}/{i}"
quit = False
while not quit:
if os.path.isdir(dirpath):
with open(f"{dirpath}/{SPEAKER_FILENAME}", 'r') as file:
speaker = file.read()
with open(f"{dirpath}/{TEXT_FILENAME}", 'r') as file:
text = file.read()
speech, samplerate = soundfile.read(f"{dirpath}/{SPEECH_FILENAME}")
wnd_main.addstr(f"[{i}] \"{speaker}\":\n\r{text}\n\r\n\r")
wnd_main.refresh()
sounddevice.play(speech, samplerate) # non-blocking
stream = sounddevice.get_stream()
status = 'PLAYING'
wnd_help.addstr(0, curses.COLS - 1 - len(status), status)
wnd_help.refresh()
while stream.active:
ch = scr.getch()
if ch in {ord('q'), ord('Q')}:
stream.stop()
quit = True
break
elif ch == ord(' '):
stream.stop()
break
time.sleep(1.0 / IDLERATE) # prevents CPU overusage
if DO_ERASE:
shutil.rmtree(dirpath)
i += 1
dirpath = f"{REPLIQUES_DIRNAME}/{i}"
else:
status = 'WAITING'
wnd_help.addstr(0, curses.COLS - 1 - len(status), status)
wnd_help.refresh()
if scr.getch() in {ord('q'), ord('Q')}:
quit = True
time.sleep(1.0 / IDLERATE) # prevents CPU overusage
if __name__ == '__main__':
curses.wrapper(run)