-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsort_recordings.py
More file actions
96 lines (78 loc) · 2.32 KB
/
sort_recordings.py
File metadata and controls
96 lines (78 loc) · 2.32 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
"""
Sort recorded hotword audio into training data.
Plays each file from recordings/, single keypress to classify:
[p] positive (wake word) → train_data/wake<N>_16k.wav
[n] negative (not wake word) → train_data/neg<N>_16k.wav
[r] replay
[s] skip
[d] delete
[q] quit
Usage: python sort_recordings.py
"""
import shutil
import subprocess
import sys
import termios
import tty
from pathlib import Path
_DIR = Path(__file__).resolve().parent
DATA_DIR = "train_data_new"
REC_DIR = _DIR / "recordings"
POS_DIR = _DIR / DATA_DIR / "positive"
NEG_DIR = _DIR / DATA_DIR / "negative"
def getch():
"""Read a single keypress."""
fd = sys.stdin.fileno()
old = termios.tcgetattr(fd)
try:
tty.setraw(fd)
return sys.stdin.read(1).lower()
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old)
def play(path):
"""Play a WAV file using afplay (macOS) or aplay (Linux)."""
if sys.platform == "darwin":
subprocess.run(["afplay", str(path)], check=False)
else:
subprocess.run(["aplay", "-q", str(path)], check=False)
def main():
if not REC_DIR.exists():
print("No recordings/ directory found.")
return
files = sorted(REC_DIR.glob("*.wav"))
if not files:
print("No recordings to sort.")
return
POS_DIR.mkdir(parents=True, exist_ok=True)
NEG_DIR.mkdir(parents=True, exist_ok=True)
print(f"\n{len(files)} recordings. [p]ositive [n]egative [r]eplay [s]kip [d]elete [q]uit\n")
for f in files:
print(f" {f.name} ", end="", flush=True)
play(f)
print("→ ", end="", flush=True)
while True:
ch = getch()
if ch == "r":
play(f)
continue
if ch == "p":
shutil.move(str(f), str(POS_DIR / f.name))
print("positive")
break
elif ch == "n":
shutil.move(str(f), str(NEG_DIR / f.name))
print("negative")
break
elif ch == "s":
print("skipped")
break
elif ch == "d":
f.unlink()
print("deleted")
break
elif ch == "q":
print("\nQuit.")
return
print("\nDone.")
if __name__ == "__main__":
main()