-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspeech2speech.py
41 lines (30 loc) · 1.56 KB
/
speech2speech.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import os
import azure.cognitiveservices.speech as speechsdk
speech_key, service_region = os.environ['SPEECH_KEY'], os.environ['SPEECH_REGION']
from_language, to_languages = 'en-US', [ 'de', 'fr']
def translate_speech_to_text():
translation_config = speechsdk.translation.SpeechTranslationConfig(
subscription=speech_key, region=service_region)
translation_config.speech_recognition_language = from_language
for lang in to_languages:
translation_config.add_target_language(lang)
translation_recognizer = speechsdk.translation.TranslationRecognizer(
translation_config=translation_config)
print('Say something...')
result = translation_recognizer.recognize_once()
synthesize_translations(result=result)
def synthesize_translations(result):
language_to_voice_map = {
"de": "de-DE-KatjaNeural",
"fr": "en-US-AvaMultilingualNeural",
}
print(f'Recognized: "{result.text}"')
audio_config = speechsdk.audio.AudioOutputConfig(use_default_speaker=True)
for language in result.translations:
translation = result.translations[language]
print(f'Translated into "{language}": {translation}')
speech_config = speechsdk.SpeechConfig(subscription=speech_key, region=service_region)
speech_config.speech_synthesis_voice_name = language_to_voice_map.get(language)
speech_synthesizer = speechsdk.SpeechSynthesizer(speech_config=speech_config, audio_config=audio_config)
speech_synthesizer.speak_text_async(translation).get()
translate_speech_to_text()