|
| 1 | +# You need to install pyaudio to run this example |
| 2 | +# pip install pyaudio |
| 3 | + |
| 4 | +# In this example, the websocket connection is opened with a text |
| 5 | +# passed in the request. When the service responds with the synthesized |
| 6 | +# audio, the pyaudio would play it in a blocking mode |
| 7 | + |
| 8 | +from watson_developer_cloud import TextToSpeechV1 |
| 9 | +from watson_developer_cloud.websocket import SynthesizeCallback |
| 10 | +import pyaudio |
| 11 | + |
| 12 | +# If service instance provides API key authentication |
| 13 | +service = TextToSpeechV1( |
| 14 | + ## url is optional, and defaults to the URL below. Use the correct URL for your region. |
| 15 | + url='https://stream.watsonplatform.net/text-to-speech/api', |
| 16 | + iam_apikey='your_apikey') |
| 17 | + |
| 18 | +# service = TextToSpeechV1( |
| 19 | +# ## url is optional, and defaults to the URL below. Use the correct URL for your region. |
| 20 | +# # url='https://stream.watsonplatform.net/text-to-speech/api, |
| 21 | +# username='YOUR SERVICE USERNAME', |
| 22 | +# password='YOUR SERVICE PASSWORD') |
| 23 | + |
| 24 | +class Play(object): |
| 25 | + """ |
| 26 | + Wrapper to play the audio in a blocking mode |
| 27 | + """ |
| 28 | + def __init__(self): |
| 29 | + self.format = pyaudio.paInt16 |
| 30 | + self.channels = 1 |
| 31 | + self.rate = 22050 |
| 32 | + self.chunk = 1024 |
| 33 | + self.pyaudio = None |
| 34 | + self.stream = None |
| 35 | + |
| 36 | + def start_streaming(self): |
| 37 | + self.pyaudio = pyaudio.PyAudio() |
| 38 | + self.stream = self._open_stream() |
| 39 | + self._start_stream() |
| 40 | + |
| 41 | + def _open_stream(self): |
| 42 | + stream = self.pyaudio.open( |
| 43 | + format=self.format, |
| 44 | + channels=self.channels, |
| 45 | + rate=self.rate, |
| 46 | + output=True, |
| 47 | + frames_per_buffer=self.chunk, |
| 48 | + start=False |
| 49 | + ) |
| 50 | + return stream |
| 51 | + |
| 52 | + def _start_stream(self): |
| 53 | + self.stream.start_stream() |
| 54 | + |
| 55 | + def write_stream(self, audio_stream): |
| 56 | + self.stream.write(audio_stream) |
| 57 | + |
| 58 | + def complete_playing(self): |
| 59 | + self.stream.stop_stream() |
| 60 | + self.stream.close() |
| 61 | + self.pyaudio.terminate() |
| 62 | + |
| 63 | +class MySynthesizeCallback(SynthesizeCallback): |
| 64 | + def __init__(self): |
| 65 | + SynthesizeCallback.__init__(self) |
| 66 | + self.play = Play() |
| 67 | + |
| 68 | + def on_connected(self): |
| 69 | + print 'Opening stream to play' |
| 70 | + self.play.start_streaming() |
| 71 | + |
| 72 | + def on_error(self, error): |
| 73 | + print 'Error received: {}'.format(error) |
| 74 | + |
| 75 | + def on_timing_information(self, timing_information): |
| 76 | + print timing_information |
| 77 | + |
| 78 | + def on_audio_stream(self, audio_stream): |
| 79 | + self.play.write_stream(audio_stream) |
| 80 | + |
| 81 | + def on_close(self): |
| 82 | + print 'Completed synthesizing' |
| 83 | + self.play.complete_playing() |
| 84 | + |
| 85 | +test_callback = MySynthesizeCallback() |
| 86 | + |
| 87 | +# An example SSML text |
| 88 | +SSML_sorry_text = """<speak version=\"1.0\"> |
| 89 | + <emphasis> I am sorry, I know how it feels.</emphasis> |
| 90 | + </speak>""" |
| 91 | + |
| 92 | +# Another example of SSML text |
| 93 | +SSML_text = """ |
| 94 | + <speak> |
| 95 | + I have been assigned to handle your order status request. |
| 96 | + <express-as type=\"Apology\"> |
| 97 | + I am sorry to inform you that the items you requested are backordered. |
| 98 | + We apologize for the inconvenience. |
| 99 | + </express-as> |
| 100 | + <express-as type=\"Uncertainty\"> |
| 101 | + We don't know when the items will become available. Maybe next week, |
| 102 | + but we are not sure at this time. |
| 103 | + </express-as> |
| 104 | + <express-as type=\"GoodNews\"> |
| 105 | + But because we want you to be a satisfied customer, we are giving you |
| 106 | + a 50% discount on your order! |
| 107 | + </express-as> |
| 108 | + </speak>""" |
| 109 | + |
| 110 | +service.synthesize_using_websocket(SSML_text, |
| 111 | + test_callback, |
| 112 | + accept='audio/wav', |
| 113 | + voice="en-US_AllisonVoice" |
| 114 | + ) |
0 commit comments