예제 #1
0
 def __init__(self):
     self.utilities = Utilities()
예제 #2
0
class SetNoiseProfile:
    def __init__(self):
        self.utilities = Utilities()

    # Service callback for detecting silence
    def setNoise_profile(self, user, noise_audio_file, audio_file_type):

        cleanup = []
        directory = os.path.expanduser(
            "~/rapp_platform_files/audio_processing/") + user
        if not os.path.isdir(directory):
            os.makedirs(directory)
            com_res = os.system("chmod 777 " + directory)
            if com_res != 0:
                return "Error: Server chmod malfunctioned"

        directory += "/noise_profile/"
        if not os.path.isdir(directory):
            os.makedirs(directory)
            com_res = os.system("chmod 777 " + directory)
            if com_res != 0:
                return "Error: Server chmod malfunctioned"

        noise_profile_file = directory
        new_audio = noise_audio_file

        if not os.path.isfile(new_audio):
            return "Error: The audio file does not exist"

        # Making audio compatible to sphinx4
        if audio_file_type == 'nao_ogg':
            if ".ogg" not in new_audio:
                return "Error: ogg type selected but file is of another type"
            new_audio += ".wav"
            com_res = os.system("sox " + noise_audio_file + " " + new_audio)
            if com_res != 0:
                return "Error: Server sox malfunctioned"
            cleanup.append(new_audio)

        elif audio_file_type == "nao_wav_1_ch":
            if ".wav" not in new_audio:
                return "Error: wav type 1 channel selected but file is of another type"
            samp_freq, signal = wavfile.read(new_audio)
            if len(signal.shape) != 1:
                return "Error: wav 1 ch declared but the audio file has " +\
                    str(signal.shape[1]) + ' channels'
            pass

        elif audio_file_type == "nao_wav_4_ch":
            if ".wav" not in new_audio:
                return "Error: wav type 4 channels selected but file is of another type"
            samp_freq, signal = wavfile.read(new_audio)
            if len(signal.shape) != 2 or signal.shape[1] != 4:
                return "Error: wav 4 ch declared but the audio file has not 4 channels"
            new_audio += "_1ch.wav"
            com_res = os.system("sox " + noise_audio_file + " -c 1 -r 16000 " + \
                new_audio)
            if com_res != 0:
                return "Error: Server sox malfunctioned"
            cleanup.append(new_audio)
        else:
            success = ''
            success = "Non valid noise audio type"
            status = self.utilities.cleanup(cleanup)
            if status != True:
                success += " " + status
            return success

        noise_profile_uri = directory + "/noise_profile_" + audio_file_type
        # Extract noise_profile
        com_res = os.system(\
                "sox " + new_audio + " -t null /dev/null trim 0.5 2.5 noiseprof "\
                + noise_profile_uri)
        if com_res != 0:
            return "Error: Server sox malfunctioned"

        com_res = os.system("chmod 777 " + noise_profile_uri)
        if com_res != 0:
            return "Error: Server chmod malfunctioned"

        status = self.utilities.cleanup(cleanup)
        if status != True:
            return status
        else:
            return "true"
예제 #3
0
class AudioProcessing:

  # Constructor performing initializations
  def __init__(self):

    self.detect_silence_module = DetectSilence()
    self.energy_denoise_module = EnergyDenoise()
    self.sox_denoise_module = SoxDenoise()
    self.utilities_module = Utilities()
    self.set_noise_profile_module = SetNoiseProfile()
    self.transform_audio_module= TransformAudio()

    # Parameters acquisition
    self.set_noise_profile_topic = \
        rospy.get_param("rapp_audio_processing_set_noise_profile_topic")
    self.denoise_topic = \
        rospy.get_param("rapp_audio_processing_denoise_topic")
    self.energy_denoise_topic = \
        rospy.get_param("rapp_audio_processing_energy_denoise_topic")
    self.detect_silence_topic = \
        rospy.get_param("rapp_audio_processing_detect_silence_topic")
    self.transform_audio_topic = \
        rospy.get_param("rapp_audio_processing_transform_audio_topic")

    if(not self.set_noise_profile_topic):
      rospy.logerror("Audio processing noise profiling topic param not found")
    if(not self.denoise_topic):
      rospy.logerror("Audio processing denoise topic param not found")
    if(not self.energy_denoise_topic):
      rospy.logerror("Audio processing energy denoise topic param not found")
    if(not self.detect_silence_topic):
      rospy.logerror("Audio processing detect silence topic param not found")
    if(not self.transform_audio_topic):
      rospy.logerror("Audio processing noise transform audio topic param not found")

    # Check for denoising debug mode. DO NOT make this true when in production
    self.energy_denoising_debug = False
    self.energy_denoising_debug = \
        rospy.get_param("rapp_audio_processing_energy_denoising_debug")
    if not self.energy_denoising_debug:
      self.energy_denoising_debug = False
    else:
      self.energy_denoising_debug = True

    # Create set noise profile services
    self.set_noise_profile_service = rospy.Service(self.set_noise_profile_topic, \
        AudioProcessingSetNoiseProfileSrv, self.setNoiseProfile)
      # Create sox denoise services
    self.denoise_service = rospy.Service( \
        self.denoise_topic, AudioProcessingDenoiseSrv, \
        self.denoise)
    # Create energy denoise services
    self.energy_denoise_service = rospy.Service( \
        self.energy_denoise_topic, AudioProcessingDenoiseSrv, \
        self.energy_denoise)
    # Create detect silence services
    self.detect_silence_service = rospy.Service( \
        self.detect_silence_topic, AudioProcessingDetectSilenceSrv, \
        self.detect_silence)
    # Create transform audio services
    self.transform_audio = rospy.Service( self.transform_audio_topic, \
        AudioProcessingTransformAudioSrv, self.transform_audio)

    self.serv_db_topic = rospy.get_param("rapp_mysql_wrapper_user_fetch_data_topic")

# Service callback for setting noise profile
  def setNoiseProfile(self, req):
    res = AudioProcessingSetNoiseProfileSrvResponse()

    #-------------------------Check with database-------------------------#
    authentication_service = rospy.ServiceProxy(self.serv_db_topic, fetchDataSrv)
    req_db = fetchDataSrv()
    req_db.req_cols=["username"]
    entry1=["username", req.user]
    req_db.where_data=[StringArrayMsg(s=entry1)]

    resp = authentication_service(req_db.req_cols, req_db.where_data)
    if resp.success.data != True or len(resp.res_data) == 0:
      res.success = "false"
      res.error = "Non authenticated user"
      return res

    #-------------------------set noise profile-------------------------#
    ret = self.set_noise_profile_module.setNoise_profile(\
            req.user,\
            req.noise_audio_file,\
            req.audio_file_type)
    if ret == 'true':
        res.success = ret
        res.error = ''
    else:
        res.success = 'false'
        res.error = ret
    return res

  # Service callback for handling denoising
  def denoise(self, req):
    res = AudioProcessingDenoiseSrvResponse()
    res.success = self.sox_denoise_module.soxDenoise(\
            req.user,\
            req.audio_type,\
            req.audio_file,\
            req.denoised_audio_file,\
            req.scale)
    return res

  # Service callback for detecting silence
  def detect_silence(self, req):
    res = AudioProcessingDetectSilenceSrvResponse()
    [res.level, res.silence] = self.detect_silence_module.detectSilence(\
            req.audio_file, req.threshold)
    if res.silence == True:
        res.silence = "true"
    else:
        res.silence = "false"
    return res

  # Service callback for energy denoising
  def energy_denoise(self, req):
    res = AudioProcessingDenoiseSrvResponse()
    output = self.energy_denoise_module.energyDenoise(\
          req.audio_file, req.scale, req.denoised_audio_file,\
          self.energy_denoising_debug)
    if output == True:
        res.success = "true"
    else:
        res.success = "false"
    return res

  # Service callback for audio transformation
  def transform_audio(self, req):
      res = AudioProcessingTransformAudioSrvResponse()

      [ res.error, res.fullpath ] = \
          self.transform_audio_module.transform_audio( \
              req.source_type, req.source_name, req.target_type, \
              req.target_name, req.target_channels, req.target_rate )

      return res


  # Cleanup method
  def cleanup(self, clean):
    return self.utilities_module.cleanup(clean)
예제 #4
0
  def __init__(self):

    self.detect_silence_module = DetectSilence()
    self.energy_denoise_module = EnergyDenoise()
    self.sox_denoise_module = SoxDenoise()
    self.utilities_module = Utilities()
    self.set_noise_profile_module = SetNoiseProfile()
    self.transform_audio_module= TransformAudio()

    # Parameters acquisition
    self.set_noise_profile_topic = \
        rospy.get_param("rapp_audio_processing_set_noise_profile_topic")
    self.denoise_topic = \
        rospy.get_param("rapp_audio_processing_denoise_topic")
    self.energy_denoise_topic = \
        rospy.get_param("rapp_audio_processing_energy_denoise_topic")
    self.detect_silence_topic = \
        rospy.get_param("rapp_audio_processing_detect_silence_topic")
    self.transform_audio_topic = \
        rospy.get_param("rapp_audio_processing_transform_audio_topic")

    if(not self.set_noise_profile_topic):
      rospy.logerror("Audio processing noise profiling topic param not found")
    if(not self.denoise_topic):
      rospy.logerror("Audio processing denoise topic param not found")
    if(not self.energy_denoise_topic):
      rospy.logerror("Audio processing energy denoise topic param not found")
    if(not self.detect_silence_topic):
      rospy.logerror("Audio processing detect silence topic param not found")
    if(not self.transform_audio_topic):
      rospy.logerror("Audio processing noise transform audio topic param not found")

    # Check for denoising debug mode. DO NOT make this true when in production
    self.energy_denoising_debug = False
    self.energy_denoising_debug = \
        rospy.get_param("rapp_audio_processing_energy_denoising_debug")
    if not self.energy_denoising_debug:
      self.energy_denoising_debug = False
    else:
      self.energy_denoising_debug = True

    # Create set noise profile services
    self.set_noise_profile_service = rospy.Service(self.set_noise_profile_topic, \
        AudioProcessingSetNoiseProfileSrv, self.setNoiseProfile)
      # Create sox denoise services
    self.denoise_service = rospy.Service( \
        self.denoise_topic, AudioProcessingDenoiseSrv, \
        self.denoise)
    # Create energy denoise services
    self.energy_denoise_service = rospy.Service( \
        self.energy_denoise_topic, AudioProcessingDenoiseSrv, \
        self.energy_denoise)
    # Create detect silence services
    self.detect_silence_service = rospy.Service( \
        self.detect_silence_topic, AudioProcessingDetectSilenceSrv, \
        self.detect_silence)
    # Create transform audio services
    self.transform_audio = rospy.Service( self.transform_audio_topic, \
        AudioProcessingTransformAudioSrv, self.transform_audio)

    self.serv_db_topic = rospy.get_param("rapp_mysql_wrapper_user_fetch_data_topic")
  def __init__(self):

    ## Instantiates rapp_detect_silence.DetectSilence
    self._detect_silence_module = DetectSilence()
    ## Instantiates rapp_energy_denoise.EnergyDenoise
    self._energy_denoise_module = EnergyDenoise()
    ## Instantiates rapp_sox_denoise.SoxDenoise
    self._sox_denoise_module = SoxDenoise()
    ## Instantiates rapp_utilities.Utilities
    self._utilities_module = Utilities()
    ## Instantiates rapp_set_noise_profile.SetNoiseProfile
    self._set_noise_profile_module = SetNoiseProfile()
    ## Instantiates rapp_transform_audio.TransformAudio
    self._transform_audio_module= TransformAudio()

    # Parameters acquisition
    set_noise_profile_topic = \
        rospy.get_param("rapp_audio_processing_set_noise_profile_topic")
    denoise_topic = \
        rospy.get_param("rapp_audio_processing_denoise_topic")
    energy_denoise_topic = \
        rospy.get_param("rapp_audio_processing_energy_denoise_topic")
    detect_silence_topic = \
        rospy.get_param("rapp_audio_processing_detect_silence_topic")
    transform_audio_topic = \
        rospy.get_param("rapp_audio_processing_transform_audio_topic")

    if(not set_noise_profile_topic):
      rospy.logerror("Audio processing noise profiling topic param not found")
    if(not denoise_topic):
      rospy.logerror("Audio processing denoise topic param not found")
    if(not energy_denoise_topic):
      rospy.logerror("Audio processing energy denoise topic param not found")
    if(not detect_silence_topic):
      rospy.logerror("Audio processing detect silence topic param not found")
    if(not transform_audio_topic):
      rospy.logerror("Audio processing noise transform audio topic param not found")

    # Check for denoising debug mode. DO NOT make this true when in production
    ## Energy denoising degug flag
    self._energy_denoising_debug = False
    self._energy_denoising_debug = \
        rospy.get_param("rapp_audio_processing_energy_denoising_debug")
    if not self._energy_denoising_debug:
      self._energy_denoising_debug = False
    else:
      self._energy_denoising_debug = True

    # Create set noise profile services
    set_noise_profile_service = rospy.Service(set_noise_profile_topic, \
        AudioProcessingSetNoiseProfileSrv, self.setNoiseProfileCallback)
      # Create sox denoise services
    denoise_service = rospy.Service( \
        denoise_topic, AudioProcessingDenoiseSrv, \
        self.denoiseCallback)
    # Create energy denoise services
    energy_denoise_service = rospy.Service( \
        energy_denoise_topic, AudioProcessingDenoiseSrv, \
        self.energyDenoiseCallback)
    # Create detect silence services
    detect_silence_service = rospy.Service( \
        detect_silence_topic, AudioProcessingDetectSilenceSrv, \
        self.detectSilenceCallback)
    # Create transform audio services
    transform_audio = rospy.Service( transform_audio_topic, \
        AudioProcessingTransformAudioSrv, self.transformAudioCallback)