-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathspeech_generator.py
55 lines (48 loc) · 1.73 KB
/
speech_generator.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
try:
import azure.cognitiveservices.speech as speechsdk
except ImportError:
print("""
Importing the Speech SDK for Python failed.
Refer to
https://docs.microsoft.com/azure/cognitive-services/speech-service/quickstart-text-to-speech-python for
installation instructions.
""")
import sys
sys.exit(1)
import glob
import random
import pygame.mixer
import os
from dotenv import load_dotenv
pygame.mixer.init()
load_dotenv()
AZURE_KEY = os.getenv('AZURE_KEY')
print(AZURE_KEY)
soundfiles = glob.glob("sounds/*.wav")
speech_key, service_region = AZURE_KEY, "westeurope"
isPlaying = False
def generate_output_speech(name):
global isPlaying
if isPlaying is False:
isPlaying = True
if name != "Unbekannt":
output = name.split()
name = output[0]
speech_config = speechsdk.SpeechConfig(subscription=speech_key, region=service_region)
synthesizer = speechsdk.SpeechSynthesizer(speech_config=speech_config)
ssml_string = "<speak version=\"1.0\""
ssml_string += " xmlns=\"http://www.w3.org/2001/10/synthesis\""
ssml_string += " xml:lang=\"de-DE\">"
ssml_string += "<voice name=\"de-CH-LeniNeural\">"
ssml_string += name + ", zieh deine Maske an!"
ssml_string += "</voice> </speak>"
result = synthesizer.speak_ssml_async(ssml_string).get()
if result.reason == speechsdk.ResultReason.Canceled:
print("Error:" + str(result.cancellation_details))
else:
random_output()
isPlaying = False
def random_output():
channel = pygame.mixer.Sound(random.choice(soundfiles)).play()
while channel.get_busy():
pygame.time.wait(100)