Instantly share code, notes, and snippets.

What would you like to do?
Convert Text to Speech using Azure Cognitive Services
# Text to Speech w/ Azure Cognitive Services - Trial keys will be for the 'westus' location
$location = "westus"
$txt2SpeechTokenURI = "https://$($location)"
$key1 = "your api key"
# Output Settings
Add-Type -AssemblyName presentationCore
$mediaPlayer = New-Object
# Output Path
$audioPath = "C:\temp\"
# Output File
$audiofile = "audiooutexample.mp3"
# Generate Request Auth Headers
$TokenHeaders = @{"Ocp-Apim-Subscription-Key" = $key1;
"Content-Length"= "0";
"Content-type" = "application/x-www-form-urlencoded"
# Get OAuth Token
$OAuthToken = Invoke-RestMethod -Method POST -Uri $txt2SpeechTokenURI -Headers $TokenHeaders
# Text to Speech Endpoint
$URI = "https://$($location)"
# Output formats
$headers = @{"Ocp-Apim-Subscription-Key" = $key1;
"Content-Type" = "application/ssml+xml";
"X-Microsoft-OutputFormat" = "audio-16khz-32kbitrate-mono-mp3";
"User-Agent" = "MIMText2Speech";
"Authorization" = $OAuthToken
# Voices
#Microsoft Server Speech Text to Speech Voice (en-US, JessaRUS)
#Microsoft Server Speech Text to Speech Voice (en-GB, Susan, Apollo)
#Microsoft Server Speech Text to Speech Voice (en-AU, HayleyRUS)
[xml]$Voice = @'
<speak version='1.0' xmlns="" xml:lang='en-US'>
<voice name='Microsoft Server Speech Text to Speech Voice (en-AU, HayleyRUS)'>
# Inject text to convert
$Voice.speak.voice.'#text' = "I just converted this string to speech using Azure"
# Send request for conversion
Invoke-RestMethod -Method POST -Uri $URI -Headers $headers -Body $voice -ContentType "application/ssml+xml" -OutFile "$($audioPath)$($audiofile)"
# small delay for file to be written, open the file and play
start-sleep -Seconds 1
$$audioPath + $audiofile)
Start-Sleep -Seconds 1
$responseDuration = $mediaPlayer.NaturalDuration.TimeSpan.TotalSeconds
Start-Sleep -Seconds $responseDuration
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment