Powershell - Text-to-Speech (Text zu Sprache)
Für alle die in Powershell ein Text-To-Speech benötigen, hier ein Code-Beispiel wie dies damit realisiert werden kann:
oder als einfache Funktion umgesetzt
mit der dann so z.B. ein Text vorgelesen werden kann:
Wenn Ihr wollt das die Sprachausgabe Asynchron erfolgt, also im Script nicht auf das Ende der Ausgabe gewartet werden soll, könnt Ihr das mit der SpeakAsync() Methode im obigen Code machen:
Weitere verfügbare Eigenschaften und Methoden die das SpeechSynthesizer-Objekt bietet, könnt Ihr hier nachlesen.
Grüße @colinardo
Beispiel mit Auswahlmöglichkeit des Sprachsynthesizers (Stimme)
Add-Type -AssemblyName "System.Speech"
$speaker = new-object System.Speech.Synthesis.SpeechSynthesizer
$speaker.SetOutputToDefaultAudioDevice()
write-host "Folgende Sprachsynthesizer sind auf dem System installiert:`r`n" -ForegroundColor Yellow -BackgroundColor Black
$cntr = 0;
$voices = $speaker.GetInstalledVoices() | ?{$_.Enabled} | %{$_.VoiceInfo}
$voices | %{$cntr++;write-host "[$cntr] $($_.Name) ($($_.Culture))" -ForegroundColor Green}
$choice = Read-Host "`r`nWählen sie einen Namen für die Sprachausgabe aus [1-9]"
if ($choice -gt 0 -and $choice -le $voices.length){
$voice = $voices[$choice -1].Name
$speaker.SelectVoice($voice)
$text = Read-Host "`r`nGeben Sie den Text ein der vorgelesen werden soll"
write-host "`r`nAusgabe erfolgt jetzt !" -BackgroundColor DarkCyan -ForegroundColor White
$speaker.Speak($text)
}else{
write-host "Keine gültige Auswahl" -ForegroundColor Red
}
function speak([string]$text,[string]$voice = ""){
Add-Type -AssemblyName "System.Speech"
$speaker = new-object System.Speech.Synthesis.SpeechSynthesizer
$speaker.SetOutputToDefaultAudioDevice()
if ($voice -ne ""){
$speaker.SelectVoice($voice)
}
$speaker.Speak($text)
}
speak "Das ist ein Test"
$speaker.SpeakAsync("Euer Text")
Grüße @colinardo
Please also mark the comments that contributed to the solution of the article
Content-Key: 231466
Url: https://administrator.de/contentid/231466
Printed on: April 23, 2024 at 21:04 o'clock
3 Comments
Latest comment