41 lines
2.4 KiB
PowerShell
41 lines
2.4 KiB
PowerShell
# Run with Windows PowerShell 5.1. Russian strings come from UTF-8 JSON.
|
|
param([string]$Only = '')
|
|
$ErrorActionPreference = 'Stop'
|
|
Add-Type -AssemblyName System.Runtime.WindowsRuntime
|
|
$null = [Windows.Media.SpeechSynthesis.SpeechSynthesizer, Windows.Media.SpeechSynthesis, ContentType=WindowsRuntime]
|
|
$synth = New-Object Windows.Media.SpeechSynthesis.SpeechSynthesizer
|
|
$voice = [Windows.Media.SpeechSynthesis.SpeechSynthesizer]::AllVoices | Where-Object { $_.Language -eq 'ru-RU' -and $_.Gender -eq 1 } | Select-Object -First 1
|
|
if (-not $voice) { throw 'A Russian female Windows speech voice is required.' }
|
|
$synth.Voice = $voice
|
|
$method = [System.WindowsRuntimeSystemExtensions].GetMethods() | Where-Object { $_.Name -eq 'AsTask' -and $_.IsGenericMethod -and $_.GetParameters().Count -eq 1 -and $_.GetParameters()[0].ParameterType.Name -eq 'IAsyncOperation`1' } | Select-Object -First 1
|
|
$await = $method.MakeGenericMethod([Windows.Media.SpeechSynthesis.SpeechSynthesisStream])
|
|
$project = Join-Path $PSScriptRoot '../do-otboy'
|
|
$story = Get-Content -LiteralPath (Join-Path $project 'data/phone_story.json') -Raw -Encoding UTF8 | ConvertFrom-Json
|
|
$output = Join-Path $project 'assets/voice'
|
|
New-Item -ItemType Directory -Force -Path $output | Out-Null
|
|
$metadata = @{}
|
|
foreach ($property in $story.lines.PSObject.Properties) {
|
|
$id = $property.Name
|
|
if ($Only -and $id -ne $Only) { continue }
|
|
$entry = $property.Value
|
|
$daughter = $entry.speaker -eq $story.lines.intro_01.speaker
|
|
$synth.Options.SpeakingRate = $(if ($daughter) { 1.16 } else { 1.05 })
|
|
$synth.Options.AudioPitch = $(if ($daughter) { 1.08 } else { 0.96 })
|
|
$operation = $synth.SynthesizeTextToStreamAsync($entry.text)
|
|
$task = $await.Invoke($null, @($operation))
|
|
$task.Wait()
|
|
$stream = [System.IO.WindowsRuntimeStreamExtensions]::AsStreamForRead($task.Result)
|
|
$path = Join-Path $output ($id + '.wav')
|
|
$file = [System.IO.File]::Create($path)
|
|
$stream.CopyTo($file)
|
|
$file.Dispose()
|
|
$stream.Dispose()
|
|
$metadata[$id] = @{ voice = $voice.DisplayName; speaker = $entry.speaker; text = $entry.text; provider = 'Windows Speech / draft'; file = 'assets/voice/' + $id + '.wav' }
|
|
}
|
|
$synth.Dispose()
|
|
if (-not $Only) {
|
|
$json = $metadata | ConvertTo-Json -Depth 5
|
|
[IO.File]::WriteAllText((Join-Path $project 'data/voice_manifest.json'), $json, [Text.UTF8Encoding]::new($false))
|
|
}
|
|
Write-Output ('Generated ' + $metadata.Count + ' draft voice clips with ' + $voice.DisplayName)
|