<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Tom Torggler</title>
    <description>The latest articles on Forem by Tom Torggler (@torggler).</description>
    <link>https://forem.com/torggler</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F319446%2F3fe90e00-4d12-4f5f-ae32-b4426ddd68b1.jpg</url>
      <title>Forem: Tom Torggler</title>
      <link>https://forem.com/torggler</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/torggler"/>
    <language>en</language>
    <item>
      <title>Using PowerShell and Azure Cognitive Services to convert text to speech</title>
      <dc:creator>Tom Torggler</dc:creator>
      <pubDate>Sat, 18 Jan 2020 16:54:24 +0000</pubDate>
      <link>https://forem.com/expertsinside/using-powershell-and-azure-cognitive-services-to-convert-text-to-speech-3cc</link>
      <guid>https://forem.com/expertsinside/using-powershell-and-azure-cognitive-services-to-convert-text-to-speech-3cc</guid>
      <description>&lt;p&gt;A few days ago I had to record some voice prompts for a customer service call queue that I was configuring in one of our Microsoft Teams enterprise voice projects. Something like "Thank you for calling X, please hold..." I figured it would be nice to have Azure's artificial-intelligence-powered speech service convert my text input to an audio file. Turns out it's easier than I thought it would be.&lt;/p&gt;

&lt;h2&gt;
  
  
  Azure Cognitive Speech Service
&lt;/h2&gt;

&lt;p&gt;First of all we need an Azure Subscription where we can deploy our Speech Services instance. If you don't have an Azure subscription, you can sign up for a trial account using the links below. If you already have a subscription, you can easily create a free Speech Services account using the following commands from Azure Cloud Shell:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;az group create -n devto-speech -l WestEurope
az cognitiveservices account create -n devto-speech -g devto-speech --kind SpeechServices --sku F0 -l WestEurope --yes
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now the account was created and we can start using it right away. To authenticate our calls from PowerShell, we need an API key, again we can use Azure Cloud Shell to retrieve the key:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;az cognitiveservices account keys list -n devto-speech -g devto-speech
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  PowerShell and REST API
&lt;/h2&gt;

&lt;p&gt;The speech service provides a very well documented API that can easily be called using PowerShell's native &lt;code&gt;Invoke-RestMethod&lt;/code&gt; command. The code is already documented on Microsoft Docs (see link below), I wrapped it into a module and uploaded it to the PowerShell gallery.  &lt;/p&gt;

&lt;p&gt;You can install the module using the following command, it works on Windows PowerShell and PowerShell 7.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Install-Module PSSpeech
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Before we can call any of the speech service's API endpoints, we have to use the API key to get a token and store it in a variable for later use. The function in the following example calls the &lt;code&gt;/issueToken&lt;/code&gt; endpoint:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Get-SpeechToken -Key &amp;lt;yourapikey&amp;gt; | Save-SpeechToken
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now we should have a token and be able to get a list of available voices using &lt;code&gt;Get-SpeechVoicesList | Format-Table&lt;/code&gt;. Note that the function has a parameter &lt;code&gt;-Token&lt;/code&gt; that accepts a token as retrieved by &lt;code&gt;Get-SpeechToken&lt;/code&gt;. If that parameter is omitted, it checks the value of the variable that's created by &lt;code&gt;Save-SpeechToken&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;And finally we can convert some input text to speech using one of the voices from the list:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Convert-TextToSpeech -Voice en-US-JessaNeural -Text "Hi Tom, I'm Jessa from Azure!" -Path jessa.mp3
Convert-TextToSpeech -Voice en-GB-HarryNeural -Text "Hi Tom, I'm Harry from Azure!" -Path harry.mp3
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;You can find a lot of information about the speech service in the links below, be sure to check out the SSML structure to see how you can customize the voices, introduce pauses to the audio file, and many other things.&lt;/p&gt;

&lt;p&gt;You can find the code for the module in my GitHub, please let me know if you find it useful and feel free to submit a pull request with your optimizations :)&lt;/p&gt;

&lt;p&gt;Tom&lt;/p&gt;

&lt;h2&gt;
  
  
  Links
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://azure.microsoft.com/en-us/services/cognitive-services/speech-services/"&gt;Speech Services&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.microsoft.com/en-us/azure/cognitive-services/speech-service/speech-synthesis-markup"&gt;Speech Synthesis Markup Language (SSML)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.microsoft.com/en-us/azure/cognitive-services/speech-service/rest-text-to-speech"&gt;Text to Speech REST API&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://ntsystems.it/PowerShell/PSSpeech/"&gt;PSSpeech Module Help&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/tomtorggler/PSSpeech"&gt;GitHub&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>teams</category>
      <category>azure</category>
      <category>powershell</category>
    </item>
  </channel>
</rss>
