-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspeak.ps1
More file actions
42 lines (33 loc) · 1.01 KB
/
speak.ps1
File metadata and controls
42 lines (33 loc) · 1.01 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
param(
[Parameter(Mandatory=$true)][string]$Text,
[Parameter(Mandatory=$true)][string]$OutputPath,
[string]$Voice = "",
[int]$Rate = -1,
[int]$Volume = 100
)
Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'
Add-Type -AssemblyName System.Speech
$synth = New-Object System.Speech.Synthesis.SpeechSynthesizer
if ($Voice -ne "") {
try {
$synth.SelectVoice($Voice)
} catch {
Write-Warning "Voice '$Voice' not found, using system default."
}
}
$synth.Rate = [Math]::Max(-10, [Math]::Min(10, $Rate))
$synth.Volume = [Math]::Max(0, [Math]::Min(100, $Volume))
$dir = Split-Path -Parent $OutputPath
if ($dir -and -not (Test-Path $dir)) {
New-Item -ItemType Directory -Path $dir -Force | Out-Null
}
# Save to WAV file
$synth.SetOutputToWaveFile($OutputPath)
$synth.Speak($Text)
# Play through speakers so narration is heard live during the test run
$synth.SetOutputToDefaultAudioDevice()
$synth.Speak($Text)
$synth.Dispose()
Write-Host "Audio saved: $OutputPath"
exit 0