Skip to content
This repository was archived by the owner on Nov 16, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 100 additions & 0 deletions powershell/AzureTTS-RestAPI-PowerShell.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
Function Play-MediaFile($Filename)
{
# PowerShell Function to play WAV file programatically without invoking Media Player
# Supplied file must be a standard Windows WAV

# Note, function has no error checking
$PlayMedia=New-object System.Media.Soundplayer
$PlayMedia.SoundLocation=($Filename)
$PlayMedia.playsync()
}

# Generate Access token to allow use of Cognitive Text to Speech API
# and store in $AccessToken

$Uri="https://api.cognitive.microsoft.com/sts/v1.0/issueToken"
$Method="POST"
$ContentType="application/json"
$APIKey='-----'
$Headers=@{'Ocp-Apim-Subscription-Key' = $APIKey }

# Call up Access Token API with Provided Subscription Key
$AccessToken=Invoke-RestMethod -Uri $URI `
-Method $METHOD -ContentType $ContentType `
–Headers $HEADERS

# Define Headers for REST API
# Type of Audio File
$AudioOutputType='riff-16khz-16bit-mono-pcm'

# SearchAppID Guid - Generate using (New-Guid).guid | Set-Clipboard and "Paste" in section between ''
$XSearchAppId=''

# SearchClientID Guid - Generate using (New-Guid).guid | Set-Clipboard and "Paste" in section between ''

$XSearchClientId=''

# Name of Calling application
$UserAgent='SampleApplicationName'

# Populate the header values
$Headers=@{ `
'Content-Type' = 'application/ssml+xml'; `
'X-Microsoft-OutputFormat' = $AudioOutputType; `
'X-Search-AppId' = $XSearchAppId; `
'X-Search-ClientId' = $XSearchClientId; `
'Authorization' = $AccessToken `
}

$Uri= 'https://speech.platform.bing.com/synthesize'
$Method='POST'
$ContentType='application/ssml+xml'
$Filename='output.wav'

#$Headers=@{'Content-Type' = 'application/ssml+xml';'X-Microsoft-OutputFormat' = $AudioOutputType;'X-Search-AppId' = $XSearchAppId; 'X-Search-ClientId' = $XSearchClientId; 'Authorization' = $AccessToken }
#^^^ ^^^ ^^^
# Or does THIS make more sense to you for the header?

# Define the Region for the Voice (You will note there *is* a unique region for each voice
# as the Spanish voice region (es-ES) will pronounce Spanish words far better than the
# English voice (en-US) as it is TUNED to that language. But you CAN mix if you're interested

$Region='en-US'
$Voice='"Microsoft Server Speech Text to Speech Voice (en-US, JessaRUS)"'
$Content="It's time for a little PowerShell fun"

$Body='<speak version="1.0" xmlns="http://www.w3.org/2001/10/synthesis" xmlns:mstts="http://www.w3.org/2001/mstts" xml:lang="'+$region+'"><voice xml:lang="' +$Region+'" name='+$Voice+'>'+$Content+'</voice></speak>'

Invoke-RestMethod -Uri $Uri -Method $Method `
-Headers $Headers -ContentType $ContentType `
-Body $Body -UserAgent $UserAgent `
-OutFile $Filename
Play-MediaFile -Filename $Filename
break

# Body in Spanish
$Region='es-ES'
$Voice='"Microsoft Server Speech Text to Speech Voice (es-ES, Pablo, Apollo)"'
$Content="Es hora de un poco de diversión PowerShell"

$Body='<speak version="1.0" xmlns="http://www.w3.org/2001/10/synthesis" xmlns:mstts="http://www.w3.org/2001/mstts" xml:lang="'+$region+'"><voice xml:lang="' +$Region+'" name='+$Voice+'>'+$Content+'</voice></speak>'

Invoke-RestMethod -Uri $Uri -Method $Method `
-Headers $Headers -ContentType $ContentType `
-Body $Body -UserAgent $UserAgent `
-OutFile $Filename
Play-MediaFile -Filename $Filename
break

# Body in French
$Region='fr-FR'
$Voice='"Microsoft Server Speech Text to Speech Voice (fr-FR, HortenseRUS)"'
$Content="Il est temps pour un peu de fun PowerShell"

$Body='<speak version="1.0" xmlns="http://www.w3.org/2001/10/synthesis" xmlns:mstts="http://www.w3.org/2001/mstts" xml:lang="'+$region+'"><voice xml:lang="' +$Region+'" name='+$Voice+'>'+$Content+'</voice></speak>'

Invoke-RestMethod -Uri $Uri -Method $Method `
-Headers $Headers -ContentType $ContentType `
-Body $Body -UserAgent $UserAgent `
-OutFile $Filename
Play-MediaFile -Filename $Filename
100 changes: 100 additions & 0 deletions powershell/PowerShell-TTS-RestApi-Sample.PS1
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
Function Play-MediaFile($Filename)
{
# PowerShell Function to play WAV file programatically without invoking Media Player
# Supplied file must be a standard Windows WAV

# Note, function has no error checking
$PlayMedia=New-object System.Media.Soundplayer
$PlayMedia.SoundLocation=($Filename)
$PlayMedia.playsync()
}

# Generate Access token to allow use of Cognitive Text to Speech API
# and store in $AccessToken

$Uri="https://api.cognitive.microsoft.com/sts/v1.0/issueToken"
$Method="POST"
$ContentType="application/json"
$APIKey='-----'
$Headers=@{'Ocp-Apim-Subscription-Key' = $APIKey }

# Call up Access Token API with Provided Subscription Key
$AccessToken=Invoke-RestMethod -Uri $URI `
-Method $METHOD -ContentType $ContentType `
–Headers $HEADERS

# Define Headers for REST API
# Type of Audio File
$AudioOutputType='riff-16khz-16bit-mono-pcm'

# SearchAppID Guid - Generate using (New-Guid).guid | Set-Clipboard and "Paste" in section between ''
$XSearchAppId=''

# SearchClientID Guid - Generate using (New-Guid).guid | Set-Clipboard and "Paste" in section between ''

$XSearchClientId=''

# Name of Calling application
$UserAgent='SampleApplicationName'

# Populate the header values
$Headers=@{ `
'Content-Type' = 'application/ssml+xml'; `
'X-Microsoft-OutputFormat' = $AudioOutputType; `
'X-Search-AppId' = $XSearchAppId; `
'X-Search-ClientId' = $XSearchClientId; `
'Authorization' = $AccessToken `
}

$Uri= 'https://speech.platform.bing.com/synthesize'
$Method='POST'
$ContentType='application/ssml+xml'
$Filename='output.wav'

#$Headers=@{'Content-Type' = 'application/ssml+xml';'X-Microsoft-OutputFormat' = $AudioOutputType;'X-Search-AppId' = $XSearchAppId; 'X-Search-ClientId' = $XSearchClientId; 'Authorization' = $AccessToken }
#^^^ ^^^ ^^^
# Or does THIS make more sense to you for the header?

# Define the Region for the Voice (You will note there *is* a unique region for each voice
# as the Spanish voice region (es-ES) will pronounce Spanish words far better than the
# English voice (en-US) as it is TUNED to that language. But you CAN mix if you're interested

$Region='en-US'
$Voice='"Microsoft Server Speech Text to Speech Voice (en-US, JessaRUS)"'
$Content="It's time for a little PowerShell fun"

$Body='<speak version="1.0" xmlns="http://www.w3.org/2001/10/synthesis" xmlns:mstts="http://www.w3.org/2001/mstts" xml:lang="'+$region+'"><voice xml:lang="' +$Region+'" name='+$Voice+'>'+$Content+'</voice></speak>'

Invoke-RestMethod -Uri $Uri -Method $Method `
-Headers $Headers -ContentType $ContentType `
-Body $Body -UserAgent $UserAgent `
-OutFile $Filename
Play-MediaFile -Filename $Filename
break

# Body in Spanish
$Region='es-ES'
$Voice='"Microsoft Server Speech Text to Speech Voice (es-ES, Pablo, Apollo)"'
$Content="Es hora de un poco de diversión PowerShell"

$Body='<speak version="1.0" xmlns="http://www.w3.org/2001/10/synthesis" xmlns:mstts="http://www.w3.org/2001/mstts" xml:lang="'+$region+'"><voice xml:lang="' +$Region+'" name='+$Voice+'>'+$Content+'</voice></speak>'

Invoke-RestMethod -Uri $Uri -Method $Method `
-Headers $Headers -ContentType $ContentType `
-Body $Body -UserAgent $UserAgent `
-OutFile $Filename
Play-MediaFile -Filename $Filename
break

# Body in French
$Region='fr-FR'
$Voice='"Microsoft Server Speech Text to Speech Voice (fr-FR, HortenseRUS)"'
$Content="Il est temps pour un peu de fun PowerShell"

$Body='<speak version="1.0" xmlns="http://www.w3.org/2001/10/synthesis" xmlns:mstts="http://www.w3.org/2001/mstts" xml:lang="'+$region+'"><voice xml:lang="' +$Region+'" name='+$Voice+'>'+$Content+'</voice></speak>'

Invoke-RestMethod -Uri $Uri -Method $Method `
-Headers $Headers -ContentType $ContentType `
-Body $Body -UserAgent $UserAgent `
-OutFile $Filename
Play-MediaFile -Filename $Filename