-
Notifications
You must be signed in to change notification settings - Fork 0
/
ttsIBM.py
40 lines (31 loc) · 973 Bytes
/
ttsIBM.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
from watson_developer_cloud import TextToSpeechV1
import pyaudio
import wave
from ibmkeys import *
text_to_speech = TextToSpeechV1(
username=IBM_TTS_un,
password=IBM_TTS_pw,
x_watson_learning_opt_out=True) # Optional flag
def tts(message):
try:
with open('temp//output.wav',
'wb') as audio_file:
audio_file.write(
text_to_speech.synthesize(message, accept='audio/wav',
voice="en-US_AllisonVoice"))
chunk = 1024
f = wave.open(r"temp//output.wav","rb")
p = pyaudio.PyAudio()
stream = p.open(format = p.get_format_from_width(f.getsampwidth()),
channels = f.getnchannels(),
rate = f.getframerate(),
output = True)
data = f.readframes(chunk)
while data:
stream.write(data)
data = f.readframes(chunk)
stream.stop_stream()
stream.close()
p.terminate()
except:
pass