26 from global_parameters
import GlobalParams
29 from rapp_utilities
import RappUtilities
30 from rapp_exceptions
import RappError
32 from rapp_platform_ros_communications.srv import(
33 AudioProcessingDenoiseSrv,
34 AudioProcessingDenoiseSrvRequest,
35 AudioProcessingDetectSilenceSrv,
36 AudioProcessingDetectSilenceSrvRequest,
37 AudioProcessingTransformAudioSrv,
38 AudioProcessingTransformAudioSrvResponse,
39 AudioProcessingTransformAudioSrvRequest
72 denoise_topic = rospy.get_param(
"rapp_audio_processing_denoise_topic")
74 energy_denoise_topic = \
75 rospy.get_param(
"rapp_audio_processing_energy_denoise_topic")
77 detect_silence_topic = \
78 rospy.get_param(
"rapp_audio_processing_detect_silence_topic")
81 rospy.get_param(
"rapp_audio_processing_transform_audio_topic")
83 if(
not denoise_topic):
84 rospy.logerror(
"Audio processing denoise topic not found")
85 if(
not energy_denoise_topic):
86 rospy.logerror(
"Audio processing energy denoise topic not found")
87 if(
not detect_silence_topic):
88 rospy.logerror(
"Audio processing detect silence topic not found")
89 if(
not audio_trans_topic):
90 rospy.logerror(
"Audio processing transform audio topic not found")
96 denoise_topic, AudioProcessingDenoiseSrv)
102 energy_denoise_topic, AudioProcessingDenoiseSrv)
108 detect_silence_topic, AudioProcessingDetectSilenceSrv)
114 audio_trans_topic, AudioProcessingTransformAudioSrv )
117 self.
_jar_path =
".:" + self._globalParams._sphinx_jar_files_url + \
118 "/" + self._globalParams._sphinx_jar_file +
":" \
119 + self._globalParams._sphinx_package_url +
"/src"
127 line = self.socket_connection.recv(1024)
128 if self._globalParams._allow_sphinx_output ==
True:
129 RappUtilities.rapp_print( line )
138 RappUtilities.rapp_print(
'Initializing Sphinx subprocess')
141 RappUtilities.rapp_print(
'Setting up socket IPC')
144 RappUtilities.rapp_print(
'Forking subprocess')
145 if self._globalParams._allow_sphinx_output ==
True:
147 [
"java",
"-cp", self.
_jar_path,
"Sphinx4", \
151 from subprocess
import DEVNULL
153 DEVNULL = open(os.devnull,
'wb')
156 [
"java",
"-cp", self.
_jar_path,
"Sphinx4", \
158 stdout = DEVNULL, stderr = DEVNULL )
160 RappUtilities.rapp_print(
'Awaiting socket connection')
161 self.socket_connection, addr = self._sphinx_socket.accept()
170 HOST = self._globalParams._socket_host
171 self.
_sphinx_socket = socket.socket( socket.AF_INET, socket.SOCK_STREAM )
172 self._sphinx_socket.setsockopt( socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
173 self._sphinx_socket.bind( (HOST, 0) )
175 self._sphinx_socket.listen( 1 )
176 RappUtilities.rapp_print(
'Socket created. PORT: ' + \
184 self.socket_connection.sendall(
"configurationPath#" + conf[
'configuration_path'] +
'\r\n')
186 self.socket_connection.sendall(
"acousticModel#" + conf[
'acoustic_model'] +
'\r\n')
188 self.socket_connection.sendall(
"grammarName#" + conf[
'grammar_name'] +
"#" + \
189 conf[
'grammar_folder'] +
'\r\n')
191 self.socket_connection.sendall(
"dictionary#" + conf[
'dictionary'] +
'\r\n')
193 self.socket_connection.sendall(
"languageModel#" + conf[
'language_model'] +
'\r\n')
195 if(conf[
'grammar_disabled']):
196 self.socket_connection.sendall(
"disableGrammar#\r\n")
198 self.socket_connection.sendall(
"enableGrammar#\r\n")
200 self.socket_connection.sendall(
"forceConfiguration#\r\n")
212 processingProfile = {}
213 processingProfile[
'sox_transform'] =
False
214 processingProfile[
'sox_channels_and_rate'] =
False
215 processingProfile[
'sox_denoising'] =
False
216 processingProfile[
'sox_denoising_scale'] = 0.0
217 processingProfile[
'detect_silence'] =
False
218 processingProfile[
'detect_silence_threshold'] = 0.0
219 processingProfile[
'energy_denoising'] =
False
220 processingProfile[
'energy_denoising_init_scale'] = 0.0
222 if audio_type ==
"headset":
224 elif audio_type ==
"nao_ogg":
225 processingProfile[
'sox_transform'] =
True
226 processingProfile[
'sox_denoising'] =
True
227 processingProfile[
'sox_denoising_scale'] = 0.15
228 processingProfile[
'detect_silence'] =
True
229 processingProfile[
'detect_silence_threshold'] = 3.0
230 processingProfile[
'energy_denoising'] =
True
231 processingProfile[
'energy_denoising_init_scale'] = 0.125
232 elif audio_type ==
"nao_wav_4_ch":
233 processingProfile[
'sox_channels_and_rate'] =
True
234 processingProfile[
'sox_denoising'] =
True
235 processingProfile[
'sox_denoising_scale'] = 0.15
236 processingProfile[
'detect_silence'] =
True
237 processingProfile[
'detect_silence_threshold'] = 3.0
238 processingProfile[
'energy_denoising'] =
True
239 processingProfile[
'energy_denoising_init_scale'] = 0.125
240 elif audio_type ==
"nao_wav_1_ch":
241 processingProfile[
'sox_denoising'] =
True
242 processingProfile[
'sox_denoising_scale'] = 0.15
243 processingProfile[
'detect_silence'] =
True
244 processingProfile[
'detect_silence_threshold'] = 3.0
245 processingProfile[
'energy_denoising'] =
True
246 processingProfile[
'energy_denoising_init_scale'] = 0.125
247 elif audio_type ==
"nao_wav_1_ch_denoised":
248 processingProfile[
'detect_silence'] =
True
249 processingProfile[
'detect_silence_threshold'] = 3.0
250 processingProfile[
'energy_denoising'] =
True
251 processingProfile[
'energy_denoising_init_scale'] = 0.125
252 elif audio_type ==
"nao_wav_1_ch_only_sox":
253 processingProfile[
'sox_denoising'] =
True
254 processingProfile[
'sox_denoising_scale'] = 0.15
255 processingProfile[
'detect_silence'] =
True
256 processingProfile[
'detect_silence_threshold'] = 3.0
257 elif audio_type ==
"nao_wav_1_ch_denoised_only_sox":
258 processingProfile[
'detect_silence'] =
True
259 processingProfile[
'detect_silence_threshold'] = 3.0
261 return processingProfile
273 if os.path.isfile(audio_file) ==
False:
274 return [
"Error: Something went wrong with the local audio storage\
275 Requested path: " + audio_file]
278 audio_to_be_erased = []
281 next_audio_file = audio_file
282 prev_audio_file = next_audio_file
284 audio_file_folder = os.path.dirname(audio_file)
285 if audio_file_folder[-1] !=
"/":
286 audio_file_folder +=
"/"
289 if audio_type
not in [\
294 "nao_wav_1_ch_denoised", \
295 "nao_wav_1_ch_only_sox", \
296 "nao_wav_1_ch_denoised_only_sox"\
298 return [
"Error: Audio source unrecognized"]
303 transform_req = AudioProcessingTransformAudioSrvRequest()
304 transform_req.source_type = audio_type
305 transform_req.source_name = prev_audio_file
306 transform_req.target_type =
'wav'
309 if profile[
'sox_transform'] ==
True:
310 next_audio_file +=
"_transformed.wav"
311 transform_req.target_name = next_audio_file
315 if trans_response.error !=
'success':
316 return [
'Audio transformation error: ' + trans_response.error ]
319 audio_to_be_erased.append(next_audio_file)
320 prev_audio_file = next_audio_file
321 if profile[
'sox_channels_and_rate'] ==
True:
322 next_audio_file +=
"_mono16k.wav"
323 transform_req.target_name = next_audio_file
324 transform_req.target_channels = 1
325 transform_req.target_rate = 16000
329 if trans_response.error !=
'success':
330 return [
'Audio transformation error: ' + trans_response.error ]
332 audio_to_be_erased.append(next_audio_file)
333 prev_audio_file = next_audio_file
334 if profile[
'sox_denoising'] ==
True:
335 next_audio_file = prev_audio_file +
"_denoised.wav"
336 den_request = AudioProcessingDenoiseSrvRequest()
337 den_request.audio_file = prev_audio_file
338 den_request.denoised_audio_file = next_audio_file
339 den_request.audio_type = audio_type
340 den_request.user = user
341 den_request.scale = profile[
'sox_denoising_scale']
343 if den_response.success !=
"true":
344 return [
"Error:" + den_response.success]
345 audio_to_be_erased.append(next_audio_file)
346 prev_audio_file = next_audio_file
347 if profile[
'detect_silence'] ==
True:
349 silence_req = AudioProcessingDetectSilenceSrvRequest()
350 silence_req.audio_file = prev_audio_file
351 silence_req.threshold = profile[
'detect_silence_threshold']
353 RappUtilities.rapp_print(
"Silence detection results: " + str(silence_res))
354 if silence_res.silence ==
"true":
355 return [
"Error: No speech detected. RSD = " + str(silence_res.level)]
360 if profile[
'energy_denoising'] ==
True:
361 next_audio_file = prev_audio_file +
"_energy_denoised.wav"
363 profile[
'energy_denoising_init_scale'] + tries * 0.125)
365 return [
"Error:" + dres]
366 audio_to_be_erased.append(next_audio_file)
367 prev_audio_file = next_audio_file
369 new_audio_file = next_audio_file
375 if len(words) == 0
or (len(words) == 1
and words[0] ==
""):
380 for f
in audio_to_be_erased:
384 return [
"Error: Server rm malfunctioned"]
398 energy_denoise_req = AudioProcessingDenoiseSrvRequest()
399 energy_denoise_req.audio_file = audio_file
400 energy_denoise_req.denoised_audio_file = next_audio_file
401 energy_denoise_req.scale = scale
403 return energy_denoise_res.success
411 self.socket_connection.sendall(
"start\r\n")
412 self.socket_connection.sendall(
"audioInput#" + audio_file +
"\r\n")
413 start_time = time.time()
417 self.socket_connection.sendall(
'Read line\r\n')
420 stripped_down_line = line[1:-1].
split(
" ")
421 for word
in stripped_down_line:
423 if(
"stopPython\n" in line):
425 if(
"CatchedException" in line):
431 if (time.time() - start_time > 10):
432 words.append(
"Error: Time out error")
439 self._sphinxSubprocess.kill()
_sphinxDied
Sphinx status flag.
_denoise_service
Denoise service client.
_sphinx_socket
The IPC socket.
def _readLine
Helper function for getting input from IPC with Sphinx subprocess.
_jar_path
Contains the absolute path for the Sphinx jar file.
_audio_transform_srv
Transform audio service client.
_energy_denoise_service
Energy denoise service client.
_conf
Sphinx configuration.
def __init__
Constructor Initiates service clients.
Contains the Sphinx subprocess and is responsible for configuring Sphinx and performing the recogniti...
def performSpeechRecognition
Performs the speech recognition and returns a list of words.
def _performEnergyDenoising
Perform energy denoise.
_globalParams
Contains global Sphinx parameters.
def configureSphinx
Perform Sphinx4 configuration.
def _createProcessingProfile
Creates audio profile based on the audio type for processing purposes.
_sphinx_socket_PORT
The IPC socket port.
_sphinxSubprocess
The Sphinx subprocess.
def _respawnSphinx
Respawns Sphinx subprocess, if it terminates abruptly.
def _createSocket
Creates socket IPC between self and Sphinx subprocess Creates the socket server with a system provide...
def _callSphinxJava
Communicate with Sphinx subprocess to initiate recognition and fetch results.
_detect_silence_service
Detect silence service client.
def _initializeSphinxProcess
Perform Sphinx4 initialization Initiates Sphinx subprocess, sets up socket IPC and configures Sphinx ...
std::vector< std::string > split(std::string str, std::string sep)
Splits string by delimiter.