Wednesday, April 23, 2014

Powershell function to create Wav file from text

Function Convert-TextToWavFile{
    [CmdletBinding()]
    Param(  [Parameter(Mandatory=$True)] [string]$OutputLocation,
            [Parameter(Mandatory=$True)] [string]$Text ,
            [ValidateSet("David","Hazel","Zira")]
            [String] $Voice = "David")

    Add-Type -AssemblyName System.speech
    $speak = New-Object System.Speech.Synthesis.SpeechSynthesizer
    $speak.SetOutputToWaveFile($OutputLocation)
    $voiceInfo = $speak.GetInstalledVoices() | ?{$_.VoiceInfo.Name.Contains($Voice)} #Get the specified voice
    $speak.SelectVoice($voiceInfo.VoiceInfo.Name)  #load the voice we want to use.
    $speak.speak($text)
    $speak.Dispose()
}

No comments:

Post a Comment