-
Notifications
You must be signed in to change notification settings - Fork 5
/
verify.py
executable file
·292 lines (225 loc) · 9.2 KB
/
verify.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
#!/usr/bin/env python3
"""
Simple tkinter application for verifying voice samples with text prompts.
Shows plot of WAV data. Left click to add trim start, right click to add trim
end. Play and Verify will respect trimmings.
Change prompt in text box to have different text written with Verify.
"""
import argparse
import logging
import subprocess
import threading
import tkinter as tk
import tkinter.messagebox
import typing
from pathlib import Path
from tkinter import ttk
import matplotlib
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
from scipy.io.wavfile import read as wav_read
matplotlib.use("TkAgg")
# Directory of this script
_DIR = Path(__file__).parent
_LOGGER = logging.getLogger("verify")
# -----------------------------------------------------------------------------
def main():
"""Main entry point."""
parser = argparse.ArgumentParser()
parser.add_argument(
"input_dir", help="Directory with input WAV files and text prompts"
)
parser.add_argument(
"output_dir",
help="Directory to write verified/trimmed WAV files and text prompts to",
)
args = parser.parse_args()
logging.basicConfig(level=logging.DEBUG)
_LOGGER.debug(args)
input_dir = Path(args.input_dir)
output_dir = Path(args.output_dir)
output_dir.mkdir(parents=True, exist_ok=True)
# -------------------------------------------------------------------------
# Load WAV files to be verified
# -------------------------------------------------------------------------
_LOGGER.debug("Looking for WAV files in %s", input_dir)
todo_prompts: typing.Dict[Path, str] = {}
for wav_path in input_dir.glob("*.wav"):
text_path = wav_path.with_suffix(".txt")
if not text_path.is_file():
_LOGGER.warning("Missing %s", text_path)
continue
rel_wav_path = wav_path.relative_to(input_dir)
done_wav_path = output_dir / rel_wav_path.name
if done_wav_path.is_file():
# Skip already completed WAV files
continue
todo_prompts[rel_wav_path] = text_path.read_text().strip()
todo_paths = list(sorted(todo_prompts.keys()))
total_paths = len(todo_paths)
current_path: typing.Optional[Path] = None
_LOGGER.debug("Found %s WAV files to verify", len(todo_paths))
# -------------------------------------------------------------------------
# Verify Samples
# -------------------------------------------------------------------------
window = tk.Tk()
window.title("Sample Verifier")
# Text box with prompt text
textbox = tk.Text(window, height=3, wrap=tk.WORD)
textbox.config(font=("Courier", 20))
textbox.pack(fill=tk.X, padx=10, pady=10)
# Progress bar for how many prompts are done
progress = ttk.Progressbar(
window, orient=tk.HORIZONTAL, length=100, mode="determinate"
)
progress.pack(fill="x", padx=10, pady=10)
# Label with current WAV file
path_label = tk.Label(window)
path_label.pack(fill=tk.X, padx=10, pady=10)
# Plot of WAV data
figure = Figure(figsize=(8, 3), dpi=100)
plot = figure.add_subplot(1, 1, 1)
figure.subplots_adjust(left=0, right=1, top=1, bottom=0, wspace=0, hspace=0)
canvas = FigureCanvasTkAgg(figure, window)
canvas.get_tk_widget().pack(fill=tk.BOTH, padx=10, pady=10)
# Start of trimming
left_cut = None
# End of trimming
right_cut = None
# Sample rate (Hz) of current WAV file
sample_rate = None
def redraw():
"""Clears and re-draws WAV plot with trim lines."""
nonlocal sample_rate
plot.cla()
if current_path:
wav_path = input_dir / current_path
_LOGGER.debug("Loading %s", wav_path)
wav_sample_rate, wav_data = wav_read(str(wav_path))
sample_rate = wav_sample_rate
audio = wav_data[:, 0]
plot.plot(audio, color="blue")
plot.set_xlim(0, len(audio))
# Trim lines
if left_cut is not None:
plot.axvline(linewidth=2, x=left_cut, color="red")
if right_cut is not None:
plot.axvline(linewidth=2, x=right_cut, color="green")
canvas.draw()
def onclick(event):
"""Handles mouse clicks on plot."""
nonlocal left_cut, right_cut
if event.button == 1:
# Left click
left_cut = event.xdata
redraw()
elif (event.button == 2) and current_path:
# Middle click
wav_path = input_dir / current_path
if wav_path and wav_path.is_file():
from_sec = event.xdata / sample_rate
play_command = [
"play",
"--ignore-length",
str(wav_path),
"trim",
str(from_sec),
]
if right_cut is not None:
to_sec = right_cut / sample_rate
play_command.append(f"={to_sec}")
threading.Thread(
target=lambda: subprocess.check_call(play_command)
).start()
elif event.button == 3:
# Right click
right_cut = event.xdata
redraw()
canvas.mpl_connect("button_press_event", onclick)
skip_button = None
play_button = None
verify_button = None
def do_next(*_args):
"""Get the next prompt and show text."""
nonlocal current_path, left_cut, right_cut
left_cut = None
right_cut = None
current_path = None
if todo_paths:
current_path = todo_paths.pop()
if current_path:
# Update prompt and WAV plot
wav_path = input_dir / current_path
textbox.delete(1.0, tk.END)
textbox.insert(1.0, todo_prompts[current_path])
path_label["text"] = str(wav_path)
redraw()
else:
tkinter.messagebox.showinfo(message="All done :)")
path_label["text"] = ""
# Update progress bar
if total_paths > 0:
progress["value"] = 100 * ((total_paths - len(todo_paths)) / total_paths)
verify_button["text"] = f"Verify ({len(todo_paths)})"
verify_button.config(bg="#F0F0F0")
play_button.config(bg="yellow")
def do_play(*_args):
"""Play current WAV file"""
wav_path = input_dir / current_path
_LOGGER.debug("Playing %s", wav_path)
if wav_path and wav_path.is_file():
play_command = ["play", "--ignore-length", str(wav_path)]
if (left_cut is not None) or (right_cut is not None):
# Play clipped WAV file
from_sec = 0 if left_cut is None else (left_cut / sample_rate)
play_command.extend(["trim", str(from_sec)])
if right_cut is not None:
to_sec = right_cut / sample_rate
play_command.append(f"={to_sec}")
_LOGGER.debug(play_command)
threading.Thread(target=lambda: subprocess.check_call(play_command)).start()
play_button.config(bg="green")
verify_button.config(bg="yellow")
def do_verify(*_args):
"""Verify recording."""
if current_path:
input_path = input_dir / current_path
output_path = output_dir / current_path
sox_command = ["sox", "--ignore-length", str(input_path), str(output_path)]
if (left_cut is not None) or (right_cut is not None):
# Write clipped WAV file
from_sec = 0 if left_cut is None else (left_cut / sample_rate)
sox_command.extend(["trim", str(from_sec)])
if right_cut is not None:
to_sec = right_cut / sample_rate
sox_command.append(f"={to_sec}")
_LOGGER.debug(sox_command)
subprocess.check_call(sox_command)
# Write prompt
prompt_path = output_dir / current_path.with_suffix(".txt")
prompt_path.write_text(textbox.get(1.0, tk.END).strip())
do_next()
else:
tkinter.messagebox.showinfo(message="No prompt")
# -------------------------------------------------------------------------
bottom_frame = tk.Frame(window)
bottom_frame.pack(fill=tk.BOTH, padx=10, pady=10)
# Button to skip WAV file
skip_button = tk.Button(bottom_frame, text="Skip", command=do_next)
skip_button.config(bg="white", activebackground="red", font=("Courier", 20))
skip_button.pack(side="left", padx=10, pady=10)
# Button to confirm WAV file
verify_button = tk.Button(bottom_frame, text="Verify", command=do_verify)
verify_button.config(
activebackground="green", activeforeground="white", font=("Courier", 20)
)
verify_button.pack(side="right", padx=10, pady=10)
# Button to play back WAV file
play_button = tk.Button(bottom_frame, text="Play", command=do_play)
play_button.config(font=("Courier", 20))
play_button.pack(side="right", padx=10, pady=10)
do_next()
window.mainloop()
# -----------------------------------------------------------------------------
if __name__ == "__main__":
main()