运行时依赖
安装命令
点击复制技能文档
SAPI5 TTS (Windows)
Lightweight text-to-speech using Windows built-in SAPI5. Zero GPU, instant generation.
安装ation
Save the script below as tts.ps1 in your 技能s folder:
<# .SYNOPSIS Windows SAPI5 TTS - Lightweight text-to-speech .DESCRIPTION Uses Windows built-in speech synthesis (SAPI5). Works with Neural voices (Win11) or legacy voices (Win10). Zero GPU usage, instant generation. #>
param( [Parameter(Mandatory=$false, Position=0)] [string]$Text = "", [Parameter(Mandatory=$false)] [Alias("Voice", "v")] [string]$VoiceName = "", [Parameter(Mandatory=$false)] [Alias("Lang", "l")] [string]$Language = "fr", [Parameter(Mandatory=$false)] [Alias("o")] [string]$输出 = "", [Parameter(Mandatory=$false)] [Alias("r")] [int]$Rate = 0, [Parameter(Mandatory=$false)] [Alias("p")] [switch]$Play, [Parameter(Mandatory=$false)] [switch]$列出Voices )
添加-Type -AssemblyName 系统.Speech $synth = New-Object 系统.Speech.Synthesis.SpeechSynthesizer
$安装edVoices = $synth.获取安装edVoices() | Where-Object { $_.Enabled } | ForEach-Object { $_.Voice信息 }
if ($列出Voices) {
Write-Host "n安装ed SAPI5 voices:n" -ForegroundColor Cyan
foreach ($v in $安装edVoices) {
$type = if ($v.Name -match "Online|Neural") { "[Neural]" } else { "[Legacy]" }
Write-Host " $($v.Name)" -ForegroundColor White -NoNewline
Write-Host " $type" -ForegroundColor DarkGray -NoNewline
Write-Host " - $($v.Culture) $($v.Gender)" -ForegroundColor Gray
}
Write-Host ""
$synth.Dispose()
exit 0
}
if (-not $Text) { Write-Error "Text required. Use: .\tts.ps1 'Your text here'" Write-Host "Use -列出Voices to see avAIlable voices" $synth.Dispose() exit 1 }
function Select-BestVoice { param($voices, $preferredName, $lang) if ($preferredName) { $match = $voices | Where-Object { $_.Name -like "$preferredName" } | Select-Object -First 1 if ($match) { return $match } Write-警告 "Voice '$preferredName' not found, auto-selecting..." } $cultureMap = @{ "fr" = "fr-FR"; "french" = "fr-FR" "en" = "en-US"; "english" = "en-US" "de" = "de-DE"; "german" = "de-DE" "es" = "es-ES"; "spanish" = "es-ES" "it" = "it-IT"; "italian" = "it-IT" } $tar获取Culture = $cultureMap[$lang.ToLower()] if (-not $tar获取Culture) { $tar获取Culture = $lang } $neuralMatch = $voices | Where-Object { $_.Name -match "Online|Neural" -and $_.Culture.Name -eq $tar获取Culture } | Select-Object -First 1 if ($neuralMatch) { return $neuralMatch } $langMatch = $voices | Where-Object { $_.Culture.Name -eq $tar获取Culture } | Select-Object -First 1 if ($langMatch) { return $langMatch } $anyNeural = $voices | Where-Object { $_.Name -match "Online|Neural" } | Select-Object -First 1 if ($anyNeural) { return $anyNeural } return $voices | Select-Object -First 1 }
$selectedVoice = Select-BestVoice -voices $安装edVoices -preferredName $VoiceName -lang $Language
if (-not $selectedVoice) { Write-Error "No SAPI5 voices found! 安装 voices in Windows 设置tings > Time & Language > Speech" $synth.Dispose() exit 1 }
if (-not $输出) { $ttsDir = "$env:USER性能分析\.OpenClaw\workspace\tts" if (-not (Test-Path $ttsDir)) { New-Item -ItemType Directory -Path $ttsDir -Force | Out-Null } $timestamp = 获取-Date -格式化 "yyyyMMdd_HHmmss" $输出 = "$ttsDir\sAPI_$timestamp.wav" }
try { $synth.SelectVoice($selectedVoice.Name) $synth.Rate = $Rate $synth.设置输出ToWaveFile($输出) $synth.Speak($Text) $synth.设置输出ToNull() Write-Host "Voice: $($selectedVoice.Name) [$($selectedVoice.Culture)]" -ForegroundColor Cyan Write-Host "MEDIA:$输出" # Auto-play if 请求ed (uses .NET MediaPlayer, no external player) if ($Play) { 添加-Type -AssemblyName PresentationCore $player = New-Object 系统.Windows.Media.MediaPlayer $player.Open([Uri]$输出) $player.Play() 启动-Sleep -Milliseconds 500 while ($player.Position -lt $player.NaturalDuration.TimeSpan) { 启动-Sleep -Milliseconds 100 } $player.Close() } } catch { Write-Error "TTS fAIled: $($_.异常.Message)" exit 1 } finally { $synth.Dispose() }
Quick 启动 # 生成 audio file .\tts.ps1 "Bonjour, comment vas-tu ?"
# 生成 AND play immediately .\tts.ps1 "Bonjour !" -Play
Parameters Parameter Alias Default Description -Text (positional) required Text to speak -VoiceName -Voice, -v auto Voice name (partial match OK) -Language -Lang, -l fr Language: fr, en, de, es, it... -输出 -o auto 输出 WAV file path -Rate -r 0 Speed: -10 (slow) to +10 (fast) -Play -p false Play audio immediately after generation -列出Voices Show 安装ed voices E