<s> import sys <EOL> import subprocess <EOL> import socket <EOL> import time <EOL> import os <EOL> from global_parameters import GlobalParams <EOL> import rospy <EOL> from rapp_tools import * <EOL> from rapp_platform_ros_communications . srv import ( <EOL> AudioProcessingDenoiseSrv , <EOL> AudioProcessingDenoiseSrvRequest , <EOL> AudioProcessingDetectSilenceSrv , <EOL> AudioProcessingDetectSilenceSrvRequest , <EOL> AudioProcessingTransformAudioSrv , <EOL> AudioProcessingTransformAudioSrvResponse , <EOL> AudioProcessingTransformAudioSrvRequest <EOL> ) <EOL> class Sphinx4Wrapper ( GlobalParams ) : <EOL> def __init__ ( self ) : <EOL> GlobalParams . __init__ ( self ) <EOL> self . _conf = '<STR_LIT>' <EOL> self . _sphinxDied = False <EOL> self . _sphinx_socket = None <EOL> self . _sphinx_socket_PORT = None <EOL> self . _sphinxSubprocess = None <EOL> denoise_topic = rospy . get_param ( "<STR_LIT>" ) <EOL> energy_denoise_topic = rospy . get_param ( "<STR_LIT>" ) <EOL> detect_silence_topic = rospy . get_param ( "<STR_LIT>" ) <EOL> audio_trans_topic = rospy . get_param ( "<STR_LIT>" ) <EOL> if ( not denoise_topic ) : <EOL> rospy . logerror ( "<STR_LIT>" ) <EOL> if ( not energy_denoise_topic ) : <EOL> rospy . logerror ( "<STR_LIT>" ) <EOL> if ( not detect_silence_topic ) : <EOL> rospy . logerror ( "<STR_LIT>" ) <EOL> if ( not audio_trans_topic ) : <EOL> rospy . logerror ( "<STR_LIT>" ) <EOL> self . _denoise_service = rospy . ServiceProxy ( denoise_topic , AudioProcessingDenoiseSrv ) <EOL> self . _energy_denoise_service = rospy . ServiceProxy ( energy_denoise_topic , AudioProcessingDenoiseSrv ) <EOL> self . _detect_silence_service = rospy . ServiceProxy ( detect_silence_topic , AudioProcessingDetectSilenceSrv ) <EOL> self . _audio_transform_srv = rospy . ServiceProxy ( audio_trans_topic , AudioProcessingTransformAudioSrv ) <EOL> def _readLine ( self ) : <EOL> line = self . socket_connection . recv ( <NUM_LIT> ) <EOL> if self . _allow_sphinx_output == True : <EOL> rapp_print ( line ) <EOL> return line <EOL> def initializeSphinx ( self , conf ) : <EOL> rapp_print ( str ( conf [ '<STR_LIT>' ] ) ) <EOL> self . _createSocket ( ) <EOL> if self . _allow_sphinx_output == True : <EOL> self . _sphinxSubprocess = subprocess . Popen ( [ "<STR_LIT>" , "<STR_LIT>" , conf [ '<STR_LIT>' ] , "<STR_LIT>" , str ( self . _sphinx_socket_PORT ) ] ) <EOL> else : <EOL> try : <EOL> from subprocess import DEVNULL <EOL> except ImportError : <EOL> DEVNULL = open ( os . devnull , '<STR_LIT:wb>' ) <EOL> self . _sphinxSubprocess = subprocess . Popen ( [ "<STR_LIT>" , "<STR_LIT>" , conf [ '<STR_LIT>' ] , "<STR_LIT>" , str ( self . _sphinx_socket_PORT ) ] , stdout = DEVNULL , stderr = DEVNULL ) <EOL> self . socket_connection , addr = self . _sphinx_socket . accept ( ) <EOL> self . configureSphinx ( conf ) <EOL> def _createSocket ( self ) : <EOL> HOST = self . _socket_host <EOL> self . _sphinx_socket = socket . socket ( socket . AF_INET , socket . SOCK_STREAM ) <EOL> self . _sphinx_socket . setsockopt ( socket . SOL_SOCKET , socket . SO_REUSEADDR , <NUM_LIT:1> ) <EOL> self . _sphinx_socket . bind ( ( HOST , <NUM_LIT:0> ) ) <EOL> self . _sphinx_socket_PORT = self . _sphinx_socket . getsockname ( ) [ <NUM_LIT:1> ] <EOL> self . _sphinx_socket . listen ( <NUM_LIT:1> ) <EOL> def configureSphinx ( self , conf ) : <EOL> self . _conf = conf <EOL> self . socket_connection . sendall ( "<STR_LIT>" + conf [ '<STR_LIT>' ] + '<STR_LIT:\r\n>' ) <EOL> self . _readLine ( ) <EOL> self . socket_connection . sendall ( "<STR_LIT>" + conf [ '<STR_LIT>' ] + '<STR_LIT:\r\n>' ) <EOL> self . _readLine ( ) <EOL> self . socket_connection . sendall ( "<STR_LIT>" + conf [ '<STR_LIT>' ] + "<STR_LIT:#>" + conf [ '<STR_LIT>' ] + '<STR_LIT:\r\n>' ) <EOL> self . _readLine ( ) <EOL> self . socket_connection . sendall ( "<STR_LIT>" + conf [ '<STR_LIT>' ] + '<STR_LIT:\r\n>' ) <EOL> self . _readLine ( ) <EOL> self . socket_connection . sendall ( "<STR_LIT>" + conf [ '<STR_LIT>' ] + '<STR_LIT:\r\n>' ) <EOL> self . _readLine ( ) <EOL> if ( conf [ '<STR_LIT>' ] ) : <EOL> self . socket_connection . sendall ( "<STR_LIT>" ) <EOL> else : <EOL> self . socket_connection . sendall ( "<STR_LIT>" ) <EOL> self . _readLine ( ) <EOL> self . socket_connection . sendall ( "<STR_LIT>" ) <EOL> self . _readLine ( ) <EOL> def _createProcessingProfile ( self , audio_type ) : <EOL> processingProfile = { } <EOL> processingProfile [ '<STR_LIT>' ] = False <EOL> processingProfile [ '<STR_LIT>' ] = False <EOL> processingProfile [ '<STR_LIT>' ] = False <EOL> processingProfile [ '<STR_LIT>' ] = <NUM_LIT:0.0> <EOL> processingProfile [ '<STR_LIT>' ] = False <EOL> processingProfile [ '<STR_LIT>' ] = <NUM_LIT:0.0> <EOL> processingProfile [ '<STR_LIT>' ] = False <EOL> processingProfile [ '<STR_LIT>' ] = <NUM_LIT:0.0> <EOL> if audio_type == "<STR_LIT>" : <EOL> pass <EOL> elif audio_type == "<STR_LIT>" : <EOL> processingProfile [ '<STR_LIT>' ] = True <EOL> processingProfile [ '<STR_LIT>' ] = True <EOL> processingProfile [ '<STR_LIT>' ] = <NUM_LIT> <EOL> processingProfile [ '<STR_LIT>' ] = True <EOL> processingProfile [ '<STR_LIT>' ] = <NUM_LIT> <EOL> processingProfile [ '<STR_LIT>' ] = True <EOL> processingProfile [ '<STR_LIT>' ] = <NUM_LIT> <EOL> elif audio_type == "<STR_LIT>" : <EOL> processingProfile [ '<STR_LIT>' ] = True <EOL> processingProfile [ '<STR_LIT>' ] = True <EOL> processingProfile [ '<STR_LIT>' ] = <NUM_LIT> <EOL> processingProfile [ '<STR_LIT>' ] = True <EOL> processingProfile [ '<STR_LIT>' ] = <NUM_LIT> <EOL> processingProfile [ '<STR_LIT>' ] = True <EOL> processingProfile [ '<STR_LIT>' ] = <NUM_LIT> <EOL> elif audio_type == "<STR_LIT>" : <EOL> processingProfile [ '<STR_LIT>' ] = True <EOL> processingProfile [ '<STR_LIT>' ] = <NUM_LIT> <EOL> processingProfile [ '<STR_LIT>' ] = True <EOL> processingProfile [ '<STR_LIT>' ] = <NUM_LIT> <EOL> processingProfile [ '<STR_LIT>' ] = True <EOL> processingProfile [ '<STR_LIT>' ] = <NUM_LIT> <EOL> elif audio_type == "<STR_LIT>" : <EOL> processingProfile [ '<STR_LIT>' ] = True <EOL> processingProfile [ '<STR_LIT>' ] = <NUM_LIT> <EOL> processingProfile [ '<STR_LIT>' ] = True <EOL> processingProfile [ '<STR_LIT>' ] = <NUM_LIT> <EOL> elif audio_type == "<STR_LIT>" : <EOL> processingProfile [ '<STR_LIT>' ] = True <EOL> processingProfile [ '<STR_LIT>' ] = <NUM_LIT> <EOL> processingProfile [ '<STR_LIT>' ] = True <EOL> processingProfile [ '<STR_LIT>' ] = <NUM_LIT> <EOL> elif audio_type == "<STR_LIT>" : <EOL> processingProfile [ '<STR_LIT>' ] = True <EOL> processingProfile [ '<STR_LIT>' ] = <NUM_LIT> <EOL> return processingProfile <EOL> def performSpeechRecognition ( self , audio_file , audio_type , user ) : <EOL> if os . path . isfile ( audio_file ) == False : <EOL> return [ "<STR_LIT>" + audio_file ] <EOL> audio_to_be_erased = [ ] <EOL> next_audio_file = audio_file <EOL> prev_audio_file = next_audio_file <EOL> audio_file_folder = os . path . dirname ( audio_file ) <EOL> if audio_file_folder [ - <NUM_LIT:1> ] != "<STR_LIT:/>" : <EOL> audio_file_folder += "<STR_LIT:/>" <EOL> if audio_type not in [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] : <EOL> return [ "<STR_LIT>" ] <EOL> profile = self . _createProcessingProfile ( audio_type ) <EOL> transform_req = AudioProcessingTransformAudioSrvRequest ( ) <EOL> transform_req . source_type = audio_type <EOL> transform_req . source_name = prev_audio_file <EOL> transform_req . target_type = '<STR_LIT>' <EOL> if profile [ '<STR_LIT>' ] == True : <EOL> next_audio_file += "<STR_LIT>" <EOL> transform_req . target_name = next_audio_file <EOL> trans_response = self . _audio_transform_srv ( transform_req ) <EOL> if trans_response . error != '<STR_LIT:success>' : <EOL> return [ '<STR_LIT>' + error ] <EOL> audio_to_be_erased . append ( next_audio_file ) <EOL> prev_audio_file = next_audio_file <EOL> if profile [ '<STR_LIT>' ] == True : <EOL> next_audio_file += "<STR_LIT>" <EOL> transform_req . target_name = next_audio_file <EOL> transform_req . target_channels = <NUM_LIT:1> <EOL> transform_req . target_rate = <NUM_LIT> <EOL> trans_response = self . _audio_transform_srv ( transform_req ) <EOL> if trans_response . error != '<STR_LIT:success>' : <EOL> return [ '<STR_LIT>' + error ] <EOL> audio_to_be_erased . append ( next_audio_file ) <EOL> prev_audio_file = next_audio_file <EOL> if profile [ '<STR_LIT>' ] == True : <EOL> next_audio_file = prev_audio_file + "<STR_LIT>" <EOL> den_request = AudioProcessingDenoiseSrvRequest ( ) <EOL> den_request . audio_file = prev_audio_file <EOL> den_request . denoised_audio_file = next_audio_file <EOL> den_request . audio_type = audio_type <EOL> den_request . user = user <EOL> den_request . scale = profile [ '<STR_LIT>' ] <EOL> den_response = self . _denoise_service ( den_request ) <EOL> if den_response . success != "<STR_LIT:true>" : <EOL> return [ "<STR_LIT>" + den_response . success ] <EOL> audio_to_be_erased . append ( next_audio_file ) <EOL> prev_audio_file = next_audio_file <EOL> if profile [ '<STR_LIT>' ] == True : <EOL> silence_req = AudioProcessingDetectSilenceSrvRequest ( ) <EOL> silence_req . audio_file = prev_audio_file <EOL> silence_req . threshold = profile [ '<STR_LIT>' ] <EOL> silence_res = self . _detect_silence_service ( silence_req ) <EOL> rapp_print ( "<STR_LIT>" + str ( silence_res ) ) <EOL> if silence_res . silence == "<STR_LIT:true>" : <EOL> return [ "<STR_LIT>" + str ( silence_res . level ) ] <EOL> tries = <NUM_LIT:0> <EOL> while tries < <NUM_LIT:2> : <EOL> if profile [ '<STR_LIT>' ] == True : <EOL> next_audio_file = prev_audio_file + "<STR_LIT>" <EOL> dres = self . _performEnergyDenoising ( next_audio_file , prev_audio_file , profile [ '<STR_LIT>' ] + tries * <NUM_LIT> ) <EOL> if dres != "<STR_LIT:true>" : <EOL> return [ "<STR_LIT>" + dres ] <EOL> audio_to_be_erased . append ( next_audio_file ) <EOL> prev_audio_file = next_audio_file <EOL> new_audio_file = next_audio_file <EOL> words = self . _callSphinxJava ( new_audio_file ) <EOL> if self . _sphinxDied == True : <EOL> self . _sphinxDied = False <EOL> break <EOL> if len ( words ) == <NUM_LIT:0> or ( len ( words ) == <NUM_LIT:1> and words [ <NUM_LIT:0> ] == "<STR_LIT>" ) : <EOL> tries += <NUM_LIT:1> <EOL> else : <EOL> break <EOL> backup_directory = os . path . expanduser ( "<STR_LIT>" ) + user <EOL> if not os . path . isdir ( backup_directory ) : <EOL> os . makedirs ( backup_directory ) <EOL> command = "<STR_LIT>" + audio_file + "<STR_LIT:U+0020>" + backup_directory + "<STR_LIT:/>" + audio_file . split ( "<STR_LIT:/>" ) [ - <NUM_LIT:1> ] <EOL> com_res = os . system ( command ) <EOL> if com_res != <NUM_LIT:0> : <EOL> return [ "<STR_LIT>" ] <EOL> for f in audio_to_be_erased : <EOL> clean_file = f . split ( "<STR_LIT:/>" ) [ - <NUM_LIT:1> ] <EOL> command = "<STR_LIT>" + f + "<STR_LIT:U+0020>" + backup_directory + "<STR_LIT:/>" + clean_file <EOL> os . system ( command ) <EOL> if com_res != <NUM_LIT:0> : <EOL> return [ "<STR_LIT>" ] <EOL> for f in audio_to_be_erased : <EOL> command = "<STR_LIT>" + f <EOL> os . system ( command ) <EOL> if com_res != <NUM_LIT:0> : <EOL> return [ "<STR_LIT>" ] <EOL> return words <EOL> def _performEnergyDenoising ( self , next_audio_file , audio_file , scale ) : <EOL> energy_denoise_req = AudioProcessingDenoiseSrvRequest ( ) <EOL> energy_denoise_req . audio_file = audio_file <EOL> energy_denoise_req . denoised_audio_file = next_audio_file <EOL> energy_denoise_req . scale = scale <EOL> energy_denoise_res = self . _energy_denoise_service ( energy_denoise_req ) <EOL> return energy_denoise_res . success <EOL> def _callSphinxJava ( self , audio_file ) : <EOL> self . socket_connection . sendall ( "<STR_LIT>" ) <EOL> self . socket_connection . sendall ( "<STR_LIT>" + audio_file + "<STR_LIT:\r\n>" ) <EOL> start_time = time . time ( ) <EOL> words = [ ] <EOL> while ( True ) : <EOL> line = self . _readLine ( ) <EOL> self . socket_connection . sendall ( '<STR_LIT>' ) <EOL> if ( len ( line ) > <NUM_LIT:0> ) : <EOL> if ( line [ <NUM_LIT:0> ] == "<STR_LIT:#>" ) : <EOL> stripped_down_line = line [ <NUM_LIT:1> : - <NUM_LIT:1> ] . split ( "<STR_LIT:U+0020>" ) <EOL> for word in stripped_down_line : <EOL> words . append ( word ) <EOL> if ( "<STR_LIT>" in line ) : <EOL> break <EOL> if ( "<STR_LIT>" in line ) : <EOL> rospy . logerr ( line ) <EOL> self . _respawnSphinx ( ) <EOL> self . _sphinxDied = True <EOL> return words <EOL> if ( time . time ( ) - start_time > <NUM_LIT:10> ) : <EOL> words . append ( "<STR_LIT>" ) <EOL> break <EOL> return words <EOL> def _respawnSphinx ( self ) : <EOL> self . _sphinxSubprocess . kill ( ) <EOL> time . sleep ( <NUM_LIT:2> ) <EOL> self . initializeSphinx ( self . _conf ) </s>
