test_integration.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 # Copyright (c) 2018, Amazon.com, Inc. or its affiliates. All Rights Reserved.
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License").
6 # You may not use this file except in compliance with the License.
7 # A copy of the License is located at
8 #
9 # http://aws.amazon.com/apache2.0
10 #
11 # or in the "license" file accompanying this file. This file is distributed
12 # on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
13 # express or implied. See the License for the specific language governing
14 # permissions and limitations under the License.
15 
16 from __future__ import print_function
17 
18 import sys
19 import json
20 import unittest
21 
22 import rospy
23 import rostest
24 
25 from tts.srv import Polly
26 from tts.srv import PollyResponse
27 from tts.srv import Synthesizer
28 from tts.srv import SynthesizerResponse
29 
30 # import tts which is a relay package, otherwise things don't work
31 #
32 # devel/lib/python2.7/dist-packages/
33 # +-- tts
34 # | +-- __init__.py
35 # +-- ...
36 #
37 # per http://docs.ros.org/api/catkin/html/user_guide/setup_dot_py.html:
38 #
39 # A relay package is a folder with an __init__.py folder and nothing else.
40 # Importing this folder in python will execute the contents of __init__.py,
41 # which will in turn import the original python modules in the folder in
42 # the sourcespace using the python exec() function.
43 
44 
45 PKG = 'tts'
46 NAME = 'amazonpolly'
47 
48 
49 class TestPlainText(unittest.TestCase):
50 
52  rospy.wait_for_service('polly')
53  polly = rospy.ServiceProxy('polly', Polly)
54 
55  test_text = 'Mary has a little lamb, little lamb, little lamb.'
56  res = polly(polly_action='SynthesizeSpeech', text=test_text)
57  self.assertIsNotNone(res)
58  self.assertTrue(type(res) is PollyResponse)
59 
60  r = json.loads(res.result)
61  self.assertIn('Audio Type', r, 'result should contain audio type')
62  self.assertIn('Audio File', r, 'result should contain file path')
63  self.assertIn('Amazon Polly Response Metadata', r, 'result should contain metadata')
64 
65  audio_type = r['Audio Type']
66  audio_file = r['Audio File']
67  md = r['Amazon Polly Response Metadata']
68  self.assertTrue("'HTTPStatusCode': 200," in md)
69  self.assertEqual('audio/ogg', audio_type)
70  self.assertTrue(audio_file.endswith('.ogg'))
71 
72  import subprocess
73  o = subprocess.check_output(['file', audio_file], stderr=subprocess.STDOUT)
74  import re
75  m = re.search(r'.*Ogg data, Vorbis audi.*', o, flags=re.MULTILINE)
76  self.assertIsNotNone(m)
77 
79  from tts.amazonpolly import AmazonPolly
80  polly = AmazonPolly()
81  test_text = 'Mary has a little lamb, little lamb, little lamb.'
82  res = polly.synthesize(text=test_text)
83  self.assertIsNotNone(res)
84  self.assertTrue(type(res) is PollyResponse)
85 
86  r = json.loads(res.result)
87  self.assertIn('Audio Type', r, 'result should contain audio type')
88  self.assertIn('Audio File', r, 'result should contain file path')
89  self.assertIn('Amazon Polly Response Metadata', r, 'result should contain metadata')
90 
91  audio_type = r['Audio Type']
92  audio_file = r['Audio File']
93  md = r['Amazon Polly Response Metadata']
94  self.assertTrue("'HTTPStatusCode': 200," in md)
95  self.assertEqual('audio/ogg', audio_type)
96  self.assertTrue(audio_file.endswith('.ogg'))
97 
98  import subprocess
99  o = subprocess.check_output(['file', audio_file], stderr=subprocess.STDOUT)
100  import re
101  m = re.search(r'.*Ogg data, Vorbis audi.*', o, flags=re.MULTILINE)
102  self.assertIsNotNone(m)
103 
105  rospy.wait_for_service('synthesizer')
106  speech_synthesizer = rospy.ServiceProxy('synthesizer', Synthesizer)
107 
108  text = 'Mary has a little lamb, little lamb, little lamb.'
109  res = speech_synthesizer(text=text)
110  self.assertIsNotNone(res)
111  self.assertTrue(type(res) is SynthesizerResponse)
112 
113  r = json.loads(res.result)
114  self.assertIn('Audio Type', r, 'result should contain audio type')
115  self.assertIn('Audio File', r, 'result should contain file path')
116  self.assertIn('Amazon Polly Response Metadata', r, 'result should contain metadata')
117 
118  audio_type = r['Audio Type']
119  audio_file = r['Audio File']
120  md = r['Amazon Polly Response Metadata']
121  self.assertTrue("'HTTPStatusCode': 200," in md)
122  self.assertEqual('audio/ogg', audio_type)
123  self.assertTrue(audio_file.endswith('.ogg'))
124 
125  import subprocess
126  o = subprocess.check_output(['file', audio_file], stderr=subprocess.STDOUT)
127  import re
128  m = re.search(r'.*Ogg data, Vorbis audi.*', o, flags=re.MULTILINE)
129  self.assertIsNotNone(m)
130 
132  rospy.wait_for_service('polly')
133  polly = rospy.ServiceProxy('polly', Polly)
134 
135  test_text = 'Mary has a little lamb, little lamb, little lamb.'
136  res = polly(polly_action='SynthesizeSpeech', text=test_text, output_format='mp3')
137  self.assertIsNotNone(res)
138  self.assertTrue(type(res) is PollyResponse)
139 
140  r = json.loads(res.result)
141  self.assertIn('Audio Type', r, 'result should contain audio type')
142  self.assertIn('Audio File', r, 'result should contain file path')
143  self.assertIn('Amazon Polly Response Metadata', r, 'result should contain metadata')
144 
145  audio_type = r['Audio Type']
146  audio_file = r['Audio File']
147  md = r['Amazon Polly Response Metadata']
148  self.assertTrue("'HTTPStatusCode': 200," in md)
149  self.assertEqual('audio/mpeg', audio_type)
150  self.assertTrue(audio_file.endswith('.mp3'))
151 
152  import subprocess
153  o = subprocess.check_output(['file', audio_file], stderr=subprocess.STDOUT)
154  import re
155  m = re.search(r'.*MPEG.*layer III.*', o, flags=re.MULTILINE)
156  self.assertIsNotNone(m)
157 
159  rospy.wait_for_service('polly')
160  polly = rospy.ServiceProxy('polly', Polly)
161 
162  text = '<speak>Mary has a little lamb, little lamb, little lamb.</speak>'
163  res = polly(polly_action='SynthesizeSpeech', text=text, text_type='ssml')
164  self.assertIsNotNone(res)
165  self.assertTrue(type(res) is PollyResponse)
166 
167  r = json.loads(res.result)
168  self.assertIn('Audio Type', r, 'result should contain audio type')
169  self.assertIn('Audio File', r, 'result should contain file path')
170  self.assertIn('Amazon Polly Response Metadata', r, 'result should contain metadata')
171 
172  audio_type = r['Audio Type']
173  audio_file = r['Audio File']
174  md = r['Amazon Polly Response Metadata']
175  self.assertTrue("'HTTPStatusCode': 200," in md)
176  self.assertEqual('audio/ogg', audio_type)
177  self.assertTrue(audio_file.endswith('.ogg'))
178 
179  import subprocess
180  o = subprocess.check_output(['file', audio_file], stderr=subprocess.STDOUT)
181  import re
182  m = re.search(r'.*Ogg data, Vorbis audi.*', o, flags=re.MULTILINE)
183  self.assertIsNotNone(m)
184 
186  rospy.wait_for_service('synthesizer')
187  speech_synthesizer = rospy.ServiceProxy('synthesizer', Synthesizer)
188 
189  text = '<speak>Mary has a little lamb, little lamb, little lamb.</speak>'
190  res = speech_synthesizer(text=text, metadata='''{"text_type":"ssml"}''')
191  self.assertIsNotNone(res)
192  self.assertTrue(type(res) is SynthesizerResponse)
193 
194  r = json.loads(res.result)
195  self.assertIn('Audio Type', r, 'result should contain audio type')
196  self.assertIn('Audio File', r, 'result should contain file path')
197  self.assertIn('Amazon Polly Response Metadata', r, 'result should contain metadata')
198 
199  audio_type = r['Audio Type']
200  audio_file = r['Audio File']
201  md = r['Amazon Polly Response Metadata']
202  self.assertTrue("'HTTPStatusCode': 200," in md)
203  self.assertEqual('audio/ogg', audio_type)
204  self.assertTrue(audio_file.endswith('.ogg'))
205 
206  import subprocess
207  o = subprocess.check_output(['file', audio_file], stderr=subprocess.STDOUT)
208  import re
209  m = re.search(r'.*Ogg data, Vorbis audi.*', o, flags=re.MULTILINE)
210  self.assertIsNotNone(m)
211 
212 
213 if __name__ == '__main__':
214  rostest.rosrun(PKG, NAME, TestPlainText, sys.argv)


tts
Author(s): AWS RoboMaker
autogenerated on Fri Mar 5 2021 03:06:38