-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
70 lines (59 loc) · 2.22 KB
/
Copy pathmain.py
File metadata and controls
70 lines (59 loc) · 2.22 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import logging
from queue import Queue
from threading import Thread
import azure.cognitiveservices.speech as speechsdk
try:
from config import config
subscription = config['speech_key']
region = config.get('service_region', 'southeastasia')
voice = config.get('voice_name', 'en-US-GuyNeural')
log_file = config.get('log_file_name', 'tts.log')
except ImportError:
print("Missing config.py file")
import sys
sys.exit(1)
logging.basicConfig(
format='%(asctime)-15s: %(levelname)-8s %(message)s',
filename=log_file,
level=logging.DEBUG,
datefmt='%Y-%m-%d %H:%M:%S'
)
logging.info('Started Text to Speech CLI')
print(">>> Starting text-to-speech service enabled by Azure Cognitive Services")
print(">>> Enter the text you want to speak or enter quit to terminate")
speech_config = speechsdk.SpeechConfig(subscription=subscription, region=region)
speech_config.speech_synthesis_voice_name = voice
speech_synthesizer = speechsdk.SpeechSynthesizer(speech_config=speech_config)
speech_synthesizer.synthesis_started.connect(lambda evt: logging.info(f'{evt.result.result_id}: Started'))
speech_synthesizer.synthesis_completed.connect(lambda evt: logging.info(f'{evt.result.result_id}: Completed'))
speech_synthesizer.synthesis_canceled.connect(lambda evt: logging.info(f'{evt.result.result_id}: Cancelled'))
speech_synthesizer.synthesis_canceled.connect(lambda evt: logging.info(f'Error: {evt.result.cancellation_details}'))
texts = Queue()
hasError = False
def speak():
global hasError
while (True):
text = texts.get()
if text == 'quit':
break
logging.info(f'Synthesizing "{text}"')
result = speech_synthesizer.speak_text(text)
if result.reason == speechsdk.ResultReason.Canceled:
hasError = True
break
logging.info(f'{result.result_id}: "{text}"')
thread = Thread(target=speak)
thread.start()
while (True):
print('>', end=' ')
text = input()
if hasError:
print(">>> An error occurred. Check the logs.")
break
if len(text.strip()) > 0:
texts.put(text)
if text == 'quit':
break
thread.join()
print(">>> Stopped text-to-speech service")
logging.info('Stopped Text to Speech CLI')