RAPP Platform API
 All Classes Namespaces Files Functions Variables Typedefs
TextToSpeech.py
Go to the documentation of this file.
1 from RappCloud.Objects import (
2  File,
3  Payload)
4 
5 from Cloud import (
6  CloudMsg,
7  CloudRequest,
8  CloudResponse)
9 
10 import base64
11 from os import path
12 
13 
14 class TextToSpeech(CloudMsg):
15  """ Text-To-Speech (TTS) Cloud Message object"""
16 
17  class Request(CloudRequest):
18  """ Text-To-Speech (TTS) Cloud Request object.
19 
20  TextToSpeech.Request
21  """
22  def __init__(self, **kwargs):
23  """!
24  Constructor
25 
26  @param **kwargs - Keyword arguments. Apply values to the request attributes.
27  - @ref text
28  - @ref language
29  """
30 
31  ## Input text to translate to audio data
32  self.text = ''
33  ## Language to use for translation.
34  # Valid values are currently:
35  # - 'el'
36  # - 'en'
37  self.language = ''
38  super(TextToSpeech.Request, self).__init__(**kwargs)
39 
40 
41  def make_payload(self):
42  """ Create and return the Payload of the Request. """
43  return Payload(
44  text=self.text,
45  language=self.language
46  )
47 
48 
49 
50  def make_files(self):
51  """ Create and return Array of File objects of the Request. """
52  return []
53 
54 
55  class Response(CloudResponse):
56  """ Text-To-Speech (TTS) Cloud Response object.
57 
58  TextToSpeech.Response
59  """
60  def __init__(self, **kwargs):
61  """!
62  Constructor
63 
64  @param **kwargs - Keyword arguments. Apply values to the request attributes.
65  - @ref error
66  - @ref payload
67  - @ref encoding
68  - @ref basename
69  """
70 
71  ## Error message
72  self.error = ''
73  ## The audio data payload. Payload encoding is defined by the
74  # 'encoding' json field.
75  self.payload = ''
76  ## Codec used to encode the audio data payload.
77  self.encoding = ''
78  ## A static basename for the audio data file, returned from the
79  # platform service. Ignore this field.
80  # May be usefull in future implementations.
81  self.basename = ''
82  super(TextToSpeech.Response, self).__init__(**kwargs)
83 
84 
85  def get_audio_raw(self):
86  """! Get audio raw data from response """
87  b64Data = self.payload
88  rawData = base64.b64decode(b64Data)
89  return rawData
90 
91 
92  def store_audio(self, destfile):
93  """! Store returned audio data to an audio file given by path
94 
95  @param destfile - Destination file path.
96  """
97 
98  if destfile == '' or destfile == u'':
99  raise ValueError('Empty destination file path {destfile} given')
100 
101  destAbs = path.expanduser(path.realpath(destfile))
102  rawData = self.get_audio_raw()
103  with open(destAbs, 'wb') as f:
104  f.write(rawData)
105 
106 
107 
108  def __init__(self, **kwargs):
109  """!
110  Constructor
111 
112  @param **kwargs - Keyword arguments. Apply values to the request attributes.
113  - @ref Request.text
114  - @ref Request.language
115  """
116 
117  # Create and hold the Request object for this CloudMsg
119  # Create and hold the Response object for this CloudMsg
121  super(TextToSpeech, self).__init__(
122  svcname='text_to_speech', **kwargs)
123 
basename
A static basename for the audio data file, returned from the platform service.
Definition: TextToSpeech.py:81
text
Input text to translate to audio data.
Definition: TextToSpeech.py:32
def store_audio
Store returned audio data to an audio file given by path.
Definition: TextToSpeech.py:92
def get_audio_raw
Get audio raw data from response.
Definition: TextToSpeech.py:85
encoding
Codec used to encode the audio data payload.
Definition: TextToSpeech.py:77