Sapi Tts技能使用说明
2026-04-01
新闻来源:网淘吧
围观:9
电脑广告
手机广告
SAPI5 文本转语音 (Windows)
使用 Windows 内置的 SAPI5 实现轻量级文本转语音。无需 GPU,即时生成。
安装
将下面的脚本保存为tts.ps1到你的技能文件夹中:

<#
.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]$Output = "",
[Parameter(Mandatory=$false)]
[Alias("r")]
[int]$Rate = 0,
[Parameter(Mandatory=$false)]
[Alias("p")]
[switch]$Play,
[Parameter(Mandatory=$false)]
[switch]$ListVoices
)
Add-Type -AssemblyName System.Speech
$synth = New-Object System.Speech.Synthesis.SpeechSynthesizer
$installedVoices = $synth.GetInstalledVoices() | Where-Object { $_.Enabled } | ForEach-Object { $_.VoiceInfo }
if ($ListVoices) {
Write-Host "`nInstalled SAPI5 voices:`n" -ForegroundColor Cyan
foreach ($v in $installedVoices) {
$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 -ListVoices 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-Warning "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"
}
$targetCulture = $cultureMap[$lang.ToLower()]
if (-not $targetCulture) { $targetCulture = $lang }
$neuralMatch = $voices | Where-Object {
$_.Name -match "Online|Neural" -and $_.Culture.Name -eq $targetCulture
} | Select-Object -First 1
if ($neuralMatch) { return $neuralMatch }
$langMatch = $voices | Where-Object { $_.Culture.Name -eq $targetCulture } | 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 $installedVoices -preferredName $VoiceName -lang $Language
if (-not $selectedVoice) {
Write-Error "No SAPI5 voices found! Install voices in Windows Settings > Time & Language > Speech"
$synth.Dispose()
exit 1
}
if (-not $Output) {
$ttsDir = "$env:USERPROFILE\.openclaw\workspace\tts"
if (-not (Test-Path $ttsDir)) { New-Item -ItemType Directory -Path $ttsDir -Force | Out-Null }
$timestamp = Get-Date -Format "yyyyMMdd_HHmmss"
$Output = "$ttsDir\sapi_$timestamp.wav"
}
try {
$synth.SelectVoice($selectedVoice.Name)
$synth.Rate = $Rate
$synth.SetOutputToWaveFile($Output)
$synth.Speak($Text)
$synth.SetOutputToNull()
Write-Host "Voice: $($selectedVoice.Name) [$($selectedVoice.Culture)]" -ForegroundColor Cyan
Write-Host "MEDIA:$Output"
# Auto-play if requested (uses .NET MediaPlayer, no external player)
if ($Play) {
Add-Type -AssemblyName PresentationCore
$player = New-Object System.Windows.Media.MediaPlayer
$player.Open([Uri]$Output)
$player.Play()
Start-Sleep -Milliseconds 500
while ($player.Position -lt $player.NaturalDuration.TimeSpan) {
Start-Sleep -Milliseconds 100
}
$player.Close()
}
} catch {
Write-Error "TTS failed: $($_.Exception.Message)"
exit 1
} finally {
$synth.Dispose()
}
快速开始
# Generate audio file
.\tts.ps1 "Bonjour, comment vas-tu ?"
# Generate AND play immediately
.\tts.ps1 "Bonjour !" -Play
参数
| 参数 | 别名 | 默认值 | 描述 |
|---|---|---|---|
-Text | (位置参数) | 必需 | 要朗读的文本 |
-VoiceName | -Voice,-v | 自动 | 语音名称 (支持部分匹配) |
-Language | -Lang,-l | fr | 语言:法语、英语、德语、西班牙语、意大利语... |
-输出 | -o | 自动 | 输出WAV文件路径 |
-速率 | -r | 0 | 速度:-10(慢)到 +10(快) |
-播放 | -p | 假 | 生成后立即播放音频 |
-列出语音 | 显示已安装的语音 |
示例
# French with auto-play
.\tts.ps1 "Bonjour !" -Lang fr -Play
# English, faster
.\tts.ps1 "Hello there!" -Lang en -Rate 2 -Play
# Specific voice
.\tts.ps1 "Salut !" -Voice "Denise" -Play
# List available voices
.\tts.ps1 -ListVoices
安装神经语音(推荐)
神经语音听起来比传统的桌面语音好得多。
Windows 11
神经语音已内置。请前往:设置 → 时间与语言 → 语音 → 管理语音
Windows 10/11(更多语音)
如需更多神经语音(例如法语Denise):
- 安装NaturalVoiceSAPIAdapter
- 下载语音包至设置 → 时间和语言 → 语音
- 运行
-ListVoices以验证
性能
- 生成速度:即时(< 1 秒)
- GPU:无需
- CPU:极低
- 音质:优质(神经语音)/ 基础(传统语音)
致谢
由 Pocus 🎩(AI 助手)与 Olive(@Korddie)共同制作。
文章底部电脑广告
手机广告位-内容正文底部
上一篇:ResearchMonitor技能使用说明
下一篇:Qmd技能使用说明


微信扫一扫,打赏作者吧~