RAPP Platform  v0.6.0
RAPP Platform is a collection of ROS nodes and back-end processes that aim to deliver ready-to-use generic services to robots
 All Classes Namespaces Files Functions Variables Macros
text_to_speech_espeak.py
Go to the documentation of this file.
1 #!/usr/bin/env python2
2 # -*- coding: utf-8 -*-
3 
4 #Copyright 2015 RAPP
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 import rospy
19 import json
20 import sys
21 import os
22 
23 from pylab import *
24 from scipy.io import wavfile
25 
26 from rapp_platform_ros_communications.srv import (
27  TextToSpeechSrv,
28  TextToSpeechSrvResponse
29  )
30 
31 from rapp_exceptions import RappError
32 
34 
35  def __init__(self):
36  # Speech recognition service published
37  self.serv_topic = rospy.get_param("rapp_text_to_speech_espeak_topic")
38  if(not self.serv_topic):
39  rospy.logerror("Text to speech espeak topic param not found")
40 
41  self.serv = rospy.Service(self.serv_topic, \
42  TextToSpeechSrv, self.text_to_speech_callback)
43 
44  # The service callback
45  def text_to_speech_callback(self, req):
46 
47  res = TextToSpeechSrvResponse()
48  lang = req.language
49  # Check for language
50  if req.language == '':
51  res.error = 'Language not specified'
52  return res
53 
54  # Check for audio output
55  if req.audio_output == '':
56  res.error = 'Text2Speech: Audio output not specified'
57  return res
58 
59  speed = 130 # upper limit = 180
60  pitch = 50 # upper limit = 99
61  if lang == 'el':
62  lang = 'mb-gr2'
63  speed = 160
64  pitch = 99
65 
66  command = 'espeak -p ' + str(pitch) + ' -s ' + str(speed) + \
67  ' -v ' + lang + ' --stdout \'' + req.text + '\' -w ' +\
68  req.audio_output
69  output = os.system(command)
70  if output != 0:
71  res.error = "Error: Text to speech espeak malfunctioned. You have probably\
72  given wrong language settings"
73  return res
74 
75  # Transform the output to wav 16kHz mono
76  os.system('sox ' + req.audio_output + ' -c 1 -r 16000 ' + req.audio_output + '2.wav')
77  os.system('mv ' + req.audio_output + '2.wav ' + req.audio_output)
78  return res
79 
80 if __name__ == "__main__":
81  rospy.init_node('text_to_speech_espeak_ros_node')
82  text_to_speech_espeak_ros_node = TextToSpeechEspeak()
83  rospy.spin()
84 
85