<# .SYNOPSIS 从语音服务一次性抓取播报种子,转换成 Android res/raw 可直接使用的 WAV。 .DESCRIPTION 设备上没有中文 TTS 引擎(只有 PicoTts,无中文数据),所以播报必须在本地做。 这个脚本把「0-9、A-Z、开始打包」逐条请求语音服务,下载 WAV 并统一转成 44.1kHz / 立体声 / 16-bit PCM(与设备 AudioFlinger 输出一致), 去掉首尾静音并提到 1.25x 语速,放到 app/src/main/res/raw/voice_*.wav。 运行时只读本地资源,断网也能播报。 .EXAMPLE powershell -NoProfile -ExecutionPolicy Bypass -File tools\fetch-voice-seeds.ps1 .EXAMPLE powershell -NoProfile -ExecutionPolicy Bypass -File tools\fetch-voice-seeds.ps1 -Force #> [CmdletBinding()] param( [string]$BaseUrl = 'http://tbdata.gztbyc.com', [string]$OutDir = '', [string]$Ffmpeg = 'D:\packService\desktop\native\ffmpeg\ffmpeg.exe', # 只重新抓某几个 key(例如 -Only welcome);不传则抓全部 [string[]]$Only = @(), [switch]$Force ) $ErrorActionPreference = 'Stop' if (-not $OutDir) { $scriptDir = if ($PSScriptRoot) { $PSScriptRoot } else { Split-Path -Parent $MyInvocation.MyCommand.Path } $OutDir = Join-Path $scriptDir '..\app\src\main\res\raw' } if (-not (Test-Path $Ffmpeg)) { throw "找不到 ffmpeg:$Ffmpeg(可用 -Ffmpeg 指定)" } New-Item -ItemType Directory -Force -Path $OutDir | Out-Null # key = 资源文件名后缀(必须是小写字母/数字/下划线),value = 送给语音服务的文本 $tokens = [ordered]@{ '0' = '零' '1' = '一' '2' = '二' '3' = '三' '4' = '四' '5' = '五' '6' = '六' '7' = '七' '8' = '八' '9' = '九' 'a' = 'A' 'b' = 'B' 'c' = 'C' 'd' = 'D' 'e' = 'E' 'f' = 'F' 'g' = 'G' 'h' = 'H' 'i' = 'I' 'j' = 'J' 'k' = 'K' 'l' = 'L' 'm' = 'M' 'n' = 'N' 'o' = 'O' 'p' = 'P' 'q' = 'Q' 'r' = 'R' 's' = 'S' 't' = 'T' 'u' = 'U' 'v' = 'V' 'w' = 'W' 'x' = 'X' 'y' = 'Y' 'z' = 'Z' 'start_pack' = '开始打包' 'welcome' = '欢迎使用溯源系统,初始化已经完成,扫码即可开始打包' } function Get-VoiceSeed { param([string]$Text) $encoded = [Uri]::EscapeDataString($Text) $uri = "$BaseUrl/voice/$encoded" $lastError = $null for ($attempt = 1; $attempt -le 3; $attempt++) { try { $json = Invoke-RestMethod -Uri $uri -TimeoutSec 30 if (-not $json.audio) { throw '接口没有返回 audio 路径' } $audioUri = $BaseUrl + $json.audio $bytes = (Invoke-WebRequest -UseBasicParsing -Uri $audioUri -TimeoutSec 60).Content if (-not $bytes -or $bytes.Length -lt 1024) { throw "音频太小($($bytes.Length) 字节)" } return $bytes } catch { $lastError = $_.Exception.Message Start-Sleep -Seconds (1 * $attempt) } } throw "抓取「$Text」失败:$lastError" } $total = $tokens.Count $index = 0 foreach ($key in $tokens.Keys) { $index++ if ($Only.Count -gt 0 -and $Only -notcontains $key) { continue } $text = $tokens[$key] $target = Join-Path $OutDir "voice_$key.wav" if ((Test-Path $target) -and -not $Force) { Write-Host ("[{0}/{1}] 跳过 {2}(已存在)" -f $index, $total, $target) -ForegroundColor DarkGray continue } Write-Host ("[{0}/{1}] {2} -> voice_{3}.wav" -f $index, $total, $text, $key) -ForegroundColor Cyan $bytes = Get-VoiceSeed -Text $text $temp = Join-Path $env:TEMP "voice-seed-$key-raw.wav" [IO.File]::WriteAllBytes($temp, $bytes) # 去掉首尾静音并把语速提到 1.25x(保持音调),播报更跟手 $filter = 'silenceremove=start_periods=1:start_duration=0.02:start_threshold=-45dB:stop_periods=-1:stop_duration=0.02:stop_threshold=-45dB,atempo=1.25' # 设备音频输出固定 44100Hz 立体声;种子也统一成这个格式,老 rk3288 的 AudioTrack 才认 & $Ffmpeg -y -loglevel error -i $temp -af $filter -ac 2 -ar 44100 -acodec pcm_s16le $target if ($LASTEXITCODE -ne 0 -or -not (Test-Path $target)) { throw "ffmpeg 转换失败:$text" } $size = (Get-Item $target).Length Write-Host (" {0} 字节" -f $size) -ForegroundColor DarkGray Remove-Item -Force $temp -ErrorAction SilentlyContinue } Write-Host '' Write-Host '语音种子已就绪:' -ForegroundColor Green Get-ChildItem $OutDir -Filter 'voice_*.wav' | Sort-Object Name | Select-Object Name, Length | Format-Table -AutoSize