-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpeaker Test.py
More file actions
289 lines (202 loc) · 6.28 KB
/
Copy pathSpeaker Test.py
File metadata and controls
289 lines (202 loc) · 6.28 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
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
"""
Nexaura Speaker + Voice Tester
Features:
- Lists all audio output devices
- Lists all SAPI AI voices
- Lets you select AI voice
- Speaks test lines
- Adjustable volume and speed
Required Libraries:
pip install pywin32 pycaw
Run:
python speaker_test.py
"""
from __future__ import annotations
import sys
import time
import argparse
from pathlib import Path
from typing import Iterable, List
# ------------------------------------------#
# CHECK WINDOWS #
# ------------------------------------------#
if sys.platform != "win32":
raise RuntimeError("This script only works on Windows.")
# ------------------------------------------#
# IMPORTS #
# ------------------------------------------#
try:
import win32com.client
except:
print("Install pywin32:")
print("pip install pywin32")
sys.exit()
try:
from pycaw.pycaw import AudioUtilities
except:
print("Install pycaw:")
print("pip install pycaw")
sys.exit()
# ------------------------------------------#
# GET SPEAKER #
# ------------------------------------------#
def get_speaker():
return win32com.client.Dispatch("SAPI.SpVoice")
# -------------------------------------------#
# LIST AUDIO DEVICES #
# --------------------------------- ---------#
def list_audio_devices():
print("\n------------------------------------------#")
print("AUDIO OUTPUT DEVICES #")
print("------------------------------------------#\n")
devices = AudioUtilities.GetAllDevices()
found = False
for i, device in enumerate(devices):
try:
name = device.FriendlyName
if name:
print(f"[{i}] {name}")
found = True
except:
pass
if not found:
print("No audio devices found.")
# ------------------------------------------#
# LIST AI VOICES #
# ------------------------------------------#
def list_voices(speaker):
print("\n------------------------------------------#")
print("NEXAURA AI VOICES #")
print("------------------------------------------#\n")
voices = speaker.GetVoices()
for i in range(voices.Count):
voice = voices.Item(i)
print(f"[{i}] {voice.GetDescription()}")
return voices
# ------------------------------------------#
# SPEAK LINES #
# ------------------------------------------#
def speak_lines(
speaker,
lines: Iterable[str],
volume: int = 100,
rate: int = 0,
delay: float = 1.0
):
volume = max(0, min(100, int(volume)))
rate = max(-10, min(10, int(rate)))
speaker.Volume = volume
speaker.Rate = rate
print("\n------------------------------------------#")
print("SPEAKING TEST #")
print("------------------------------------------#\n")
for line in lines:
line = line.strip()
if not line:
continue
print(f"Speaking: {line}")
speaker.Speak(line)
time.sleep(delay)
# ------------------------------------------#
# LOAD TEXT FILE #
# ------------------------------------------#
def load_lines_from_file(path: Path) -> List[str]:
return [
l.rstrip("\n")
for l in path.read_text(encoding="utf-8").splitlines()
]
# ------------------------------------------#
# ARGUMENTS #
# ------------------------------------------#
def parse_args():
parser = argparse.ArgumentParser(
description="Nexaura Speaker + Voice Tester"
)
parser.add_argument(
"--volume",
type=int,
default=100,
help="Volume 0-100"
)
parser.add_argument(
"--rate",
type=int,
default=0,
help="Voice speed -10 to 10"
)
parser.add_argument(
"--delay",
type=float,
default=1.0,
help="Delay between lines"
)
parser.add_argument(
"--voice",
type=int,
default=0,
help="Voice index"
)
parser.add_argument(
"--file",
type=Path,
help="Optional text file"
)
return parser.parse_args()
# ------------------------------------------#
# MAIN #
# ------------------------------------------#
def main():
args = parse_args()
print("\nInitializing Nexaura Audio System...\n")
# Create speaker
speaker = get_speaker()
# Show audio devices
list_audio_devices()
# Show voices
voices = list_voices(speaker)
# ------------------------------------------#
# SELECT VOICE #
# ------------------------------------------#
try:
speaker.Voice = voices.Item(args.voice)
selected_voice = voices.Item(args.voice).GetDescription()
print(f"\nSelected Voice: {selected_voice}")
except:
print("\nInvalid voice index.")
print("Using default voice.\n")
# ------------------------------------------#
# DEFAULT TEST LINES #
# ------------------------------------------#
default_lines = [
"Hello Sam.",
"I am Nexaura AI.",
"Speaker test successful.",
"All systems are working perfectly."
]
# Load custom file if provided
if args.file:
try:
lines = load_lines_from_file(args.file)
except Exception as e:
print(f"Error reading file: {e}")
lines = default_lines
else:
lines = default_lines
# ------------------------------------------#
# START SPEAKING #
# ------------------------------------------#
speak_lines(
speaker=speaker,
lines=lines,
volume=args.volume,
rate=args.rate,
delay=args.delay
)
print("\n------------------------------------------#")
print("TEST COMPLETED #")
print("------------------------------------------#\n")
# ------------------------------------------#
# START PROGRAM #
# ------------------------------------------#
if __name__ == "__main__":
main()