diff --git a/powershell/AzureTTS-RestAPI-PowerShell.ps1 b/powershell/AzureTTS-RestAPI-PowerShell.ps1 new file mode 100644 index 0000000..c20093a --- /dev/null +++ b/powershell/AzureTTS-RestAPI-PowerShell.ps1 @@ -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=''+$Content+'' + +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=''+$Content+'' + +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=''+$Content+'' + +Invoke-RestMethod -Uri $Uri -Method $Method ` +-Headers $Headers -ContentType $ContentType ` +-Body $Body -UserAgent $UserAgent ` +-OutFile $Filename +Play-MediaFile -Filename $Filename diff --git a/powershell/PowerShell-TTS-RestApi-Sample.PS1 b/powershell/PowerShell-TTS-RestApi-Sample.PS1 new file mode 100644 index 0000000..0d4d999 --- /dev/null +++ b/powershell/PowerShell-TTS-RestApi-Sample.PS1 @@ -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=''+$Content+'' + +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=''+$Content+'' + +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=''+$Content+'' + +Invoke-RestMethod -Uri $Uri -Method $Method ` +-Headers $Headers -ContentType $ContentType ` +-Body $Body -UserAgent $UserAgent ` +-OutFile $Filename +Play-MediaFile -Filename $Filename