|
| 1 | + |
| 2 | +# coding: utf-8 |
| 3 | + |
| 4 | +# Copyright 2018 IBM All Rights Reserved. |
| 5 | +# |
| 6 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +# you may not use this file except in compliance with the License. |
| 8 | +# You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, software |
| 13 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +# See the License for the specific language governing permissions and |
| 16 | +# limitations under the License. |
| 17 | + |
| 18 | +from watson_developer_cloud.websocket import SynthesizeCallback, SynthesizeListener |
| 19 | +import base64 |
| 20 | +from .text_to_speech_v1 import TextToSpeechV1 |
| 21 | +from .watson_service import _remove_null_values |
| 22 | +try: |
| 23 | + from urllib.parse import urlencode |
| 24 | +except ImportError: |
| 25 | + from urllib import urlencode |
| 26 | + |
| 27 | +BEARER = 'Bearer' |
| 28 | + |
| 29 | +class TextToSpeechV1Adapter(TextToSpeechV1): |
| 30 | + def synthesize_using_websocket(self, |
| 31 | + text, |
| 32 | + synthesize_callback, |
| 33 | + accept=None, |
| 34 | + voice=None, |
| 35 | + timings=None, |
| 36 | + customization_id=None, |
| 37 | + http_proxy_host=None, |
| 38 | + http_proxy_port=None, |
| 39 | + **kwargs): |
| 40 | + """ |
| 41 | + Synthesizes text to spoken audio using web sockets. It supports the use of |
| 42 | + the SSML <mark> element to identify the location of user-specified markers in the audio. |
| 43 | + It can also return timing information for all strings of the input text. |
| 44 | + Note:The service processes one request per connection. |
| 45 | +
|
| 46 | + :param str text: Provides the text that is to be synthesized. The client can pass plain |
| 47 | + text or text that is annotated with the Speech Synthesis Markup Language (SSML). For more |
| 48 | + information, see [Specifying input text](https://console.bluemix.net/docs/services/text-to-speech/http.html#input). |
| 49 | + SSML input can also include the <mark> element; |
| 50 | + see [Specifying an SSML mark](https://console.bluemix.net/docs/services/text-to-speech/word-timing.html#mark). |
| 51 | + The client can pass a maximum of 5 KB of text with the request. |
| 52 | + :param SynthesizeCallback synthesize_callback: The callback method for the websocket. |
| 53 | + :param str accept: Specifies the requested format (MIME type) of the audio. For more information, see [Specifying |
| 54 | + an audio format](https://console.bluemix.net/docs/services/text-to-speech/http.html#format). In addition to the |
| 55 | + supported specifications, you can use */* to specify the default audio format, audio/ogg;codecs=opus. |
| 56 | + :param str voice: The voice to use for synthesis. |
| 57 | + :param list[str] timings: Specifies that the service is to return word timing information for all strings of the |
| 58 | + input text. The service returns the start and end time of each string of the input. Specify words as the lone element |
| 59 | + of the array to request word timings. Specify an empty array or omit the parameter to receive no word timings. For |
| 60 | + more information, see [Obtaining word timings](https://console.bluemix.net/docs/services/text-to-speech/word-timing.html#timing). |
| 61 | + Not supported for Japanese input text. |
| 62 | + :param str customization_id: Specifies the globally unique identifier (GUID) for a custom voice model that is to be used for the |
| 63 | + synthesis. A custom voice model is guaranteed to work only if it matches the language of the voice that is used for the synthesis. |
| 64 | + If you include a customization ID, you must call the method with the service credentials of the custom model's owner. Omit the |
| 65 | + parameter to use the specified voice with no customization. For more information, see [Understanding customization] |
| 66 | + (https://console.bluemix.net/docs/services/text-to-speech/custom-intro.html#customIntro). |
| 67 | + :param str http_proxy_host: http proxy host name. |
| 68 | + :param str http_proxy_port: http proxy port. If not set, set to 80. |
| 69 | + :param dict headers: A `dict` containing the request headers |
| 70 | + :return: A `dict` containing the `SpeechRecognitionResults` response. |
| 71 | + :rtype: dict |
| 72 | + """ |
| 73 | + if text is None: |
| 74 | + raise ValueError('text must be provided') |
| 75 | + if synthesize_callback is None: |
| 76 | + raise ValueError('synthesize_callback must be provided') |
| 77 | + if not isinstance(synthesize_callback, SynthesizeCallback): |
| 78 | + raise Exception( |
| 79 | + 'Callback is not a derived class of SynthesizeCallback') |
| 80 | + |
| 81 | + headers = {} |
| 82 | + if self.default_headers is not None: |
| 83 | + headers = self.default_headers.copy() |
| 84 | + if 'headers' in kwargs: |
| 85 | + headers.update(kwargs.get('headers')) |
| 86 | + |
| 87 | + if self.token_manager: |
| 88 | + access_token = self.token_manager.get_token() |
| 89 | + headers['Authorization'] = '{0} {1}'.format(BEARER, access_token) |
| 90 | + else: |
| 91 | + authstring = "{0}:{1}".format(self.username, self.password) |
| 92 | + base64_authorization = base64.b64encode(authstring.encode('utf-8')).decode('utf-8') |
| 93 | + headers['Authorization'] = 'Basic {0}'.format(base64_authorization) |
| 94 | + |
| 95 | + url = self.url.replace('https:', 'wss:') |
| 96 | + params = { |
| 97 | + 'voice': voice, |
| 98 | + 'customization_id': customization_id, |
| 99 | + } |
| 100 | + params = _remove_null_values(params) |
| 101 | + url += '/v1/synthesize?{0}'.format(urlencode(params)) |
| 102 | + |
| 103 | + options = { |
| 104 | + 'text': text, |
| 105 | + 'accept': accept, |
| 106 | + 'timings': timings |
| 107 | + } |
| 108 | + options = _remove_null_values(options) |
| 109 | + |
| 110 | + SynthesizeListener(options, |
| 111 | + synthesize_callback, |
| 112 | + url, |
| 113 | + headers, |
| 114 | + http_proxy_host, |
| 115 | + http_proxy_port, |
| 116 | + self.verify) |
0 commit comments