Text to speech - Virtual IoT device
In this part of the lesson, you will write code to convert text to speech using the speech service.
Convert text to speech
The speech services SDK that you used in the last lesson to convert speech to text can be used to convert text back to speech. When requesting speech, you need to provide the voice to use as speech can be generated using a variety of different voices.
Each language supports a range of different voices, and you can get the list of supported voices for each language from the speech services SDK.
Task - convert text to speech
-
Open the
smart-timer
project in VS Code, and ensure the virtual environment is loaded in the terminal. -
Import the
SpeechSynthesizer
from theazure.cognitiveservices.speech
package by adding it to the existing imports:from azure.cognitiveservices.speech import SpeechConfig, SpeechRecognizer, SpeechSynthesizer
-
Above the
say
function, create a speech configuration to use with the speech synthesizer:speech_config = SpeechConfig(subscription=speech_api_key,
region=location)
speech_config.speech_synthesis_language = language
speech_synthesizer = SpeechSynthesizer(speech_config=speech_config)This uses the same API key, location and language that was used by the recognizer.
-
Below this, add the following code to get a voice and set it on the speech config:
voices = speech_synthesizer.get_voices_async().get().voices
first_voice = next(x for x in voices if x.locale.lower() == language.lower())
speech_config.speech_synthesis_voice_name = first_voice.short_name