|
3 | 3 | import json |
4 | 4 | from os.path import join, dirname |
5 | 5 | from watson_developer_cloud import TextToSpeechV1 |
6 | | -from watson_developer_cloud.websocket import SynthesizeCallback |
7 | 6 |
|
8 | 7 | # If service instance provides API key authentication |
9 | 8 | # service = TextToSpeechV1( |
|
64 | 63 |
|
65 | 64 | # response = service.delete_voice_model('YOUR CUSTOMIZATION ID').get_result() |
66 | 65 | # print(response) |
67 | | - |
68 | | -# Synthesize using websocket. Note: The service accepts one request per connection |
69 | | -file_path = join(dirname(__file__), "../resources/dog.wav") |
70 | | -class MySynthesizeCallback(SynthesizeCallback): |
71 | | - def __init__(self): |
72 | | - SynthesizeCallback.__init__(self) |
73 | | - self.fd = open(file_path, 'ab') |
74 | | - |
75 | | - def on_connected(self): |
76 | | - print('Connection was successful') |
77 | | - |
78 | | - def on_error(self, error): |
79 | | - print('Error received: {}'.format(error)) |
80 | | - |
81 | | - def on_content_type(self, content_type): |
82 | | - print('Content type: {}'.format(content_type)) |
83 | | - |
84 | | - def on_timing_information(self, timing_information): |
85 | | - print(timing_information) |
86 | | - |
87 | | - def on_audio_stream(self, audio_stream): |
88 | | - self.fd.write(audio_stream) |
89 | | - |
90 | | - def on_close(self): |
91 | | - self.fd.close() |
92 | | - print('Done synthesizing. Closing the connection') |
93 | | - |
94 | | -my_callback = MySynthesizeCallback() |
95 | | -service.synthesize_using_websocket('I like to pet dogs', |
96 | | - my_callback, |
97 | | - accept='audio/wav', |
98 | | - voice='en-US_AllisonVoice' |
99 | | - ) |
0 commit comments