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
rapp_audio_processing.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # -*- encode: 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 # Authors: Manos Tsardoulias
19 # contact: etsardou@iti.gr
20 
21 import rospy
22 import sys
23 import time
24 import os
25 from pylab import *
26 from scipy.io import wavfile
27 
28 from rapp_platform_ros_communications.srv import (
29  AudioProcessingDenoiseSrv,
30  AudioProcessingDenoiseSrvResponse,
31 
32  AudioProcessingSetNoiseProfileSrv,
33  AudioProcessingSetNoiseProfileSrvResponse,
34 
35  AudioProcessingDetectSilenceSrv,
36  AudioProcessingDetectSilenceSrvResponse,
37 
38  AudioProcessingTransformAudioSrv,
39  AudioProcessingTransformAudioSrvResponse
40  )
41 
42 from rapp_platform_ros_communications.msg import (
43  StringArrayMsg
44  )
45 
46 from std_msgs.msg import (
47  String
48  )
49 
50 from rapp_detect_silence import DetectSilence
51 from rapp_energy_denoise import EnergyDenoise
52 from rapp_sox_denoise import SoxDenoise
53 from rapp_utilities import Utilities
54 from rapp_set_noise_profile import SetNoiseProfile
55 from rapp_transform_audio import TransformAudio
56 
57 ## @class AudioProcessing
58 # Provides audio processing utilities
59 #
60 # Implements service servers to perform denoising, audio transformation etc.
62 
63  ## Constructor performing initializations
64  def __init__(self):
65 
66  ## Instantiates rapp_detect_silence.DetectSilence
67  self._detect_silence_module = DetectSilence()
68  ## Instantiates rapp_energy_denoise.EnergyDenoise
69  self._energy_denoise_module = EnergyDenoise()
70  ## Instantiates rapp_sox_denoise.SoxDenoise
71  self._sox_denoise_module = SoxDenoise()
72  ## Instantiates rapp_utilities.Utilities
73  self._utilities_module = Utilities()
74  ## Instantiates rapp_set_noise_profile.SetNoiseProfile
75  self._set_noise_profile_module = SetNoiseProfile()
76  ## Instantiates rapp_transform_audio.TransformAudio
77  self._transform_audio_module= TransformAudio()
78 
79  # Parameters acquisition
80  set_noise_profile_topic = \
81  rospy.get_param("rapp_audio_processing_set_noise_profile_topic")
82  denoise_topic = \
83  rospy.get_param("rapp_audio_processing_denoise_topic")
84  energy_denoise_topic = \
85  rospy.get_param("rapp_audio_processing_energy_denoise_topic")
86  detect_silence_topic = \
87  rospy.get_param("rapp_audio_processing_detect_silence_topic")
88  transform_audio_topic = \
89  rospy.get_param("rapp_audio_processing_transform_audio_topic")
90 
91  if(not set_noise_profile_topic):
92  rospy.logerror("Audio processing noise profiling topic param not found")
93  if(not denoise_topic):
94  rospy.logerror("Audio processing denoise topic param not found")
95  if(not energy_denoise_topic):
96  rospy.logerror("Audio processing energy denoise topic param not found")
97  if(not detect_silence_topic):
98  rospy.logerror("Audio processing detect silence topic param not found")
99  if(not transform_audio_topic):
100  rospy.logerror("Audio processing noise transform audio topic param not found")
101 
102  # Check for denoising debug mode. DO NOT make this true when in production
103  ## Energy denoising degug flag
105  self._energy_denoising_debug = \
106  rospy.get_param("rapp_audio_processing_energy_denoising_debug")
107  if not self._energy_denoising_debug:
108  self._energy_denoising_debug = False
109  else:
110  self._energy_denoising_debug = True
111 
112  # Create set noise profile services
113  set_noise_profile_service = rospy.Service(set_noise_profile_topic, \
114  AudioProcessingSetNoiseProfileSrv, self.setNoiseProfileCallback)
115  # Create sox denoise services
116  denoise_service = rospy.Service( \
117  denoise_topic, AudioProcessingDenoiseSrv, \
118  self.denoiseCallback)
119  # Create energy denoise services
120  energy_denoise_service = rospy.Service( \
121  energy_denoise_topic, AudioProcessingDenoiseSrv, \
123  # Create detect silence services
124  detect_silence_service = rospy.Service( \
125  detect_silence_topic, AudioProcessingDetectSilenceSrv, \
127  # Create transform audio services
128  transform_audio = rospy.Service( transform_audio_topic, \
129  AudioProcessingTransformAudioSrv, self.transformAudioCallback)
130 
131 
132  ## Service callback for setting noise profile
133  #
134  # @param req [rapp_platform_ros_comminications::AudioProcessing::AudioProcessingSetNoiseProfileSrv] The set noise profile request
135  #
136  # @return res [rapp_platform_ros_comminications::AudioProcessing::AudioProcessingSetNoiseProfileSrvResponse] The set noise profile response
137  def setNoiseProfileCallback(self, req):
138  res = AudioProcessingSetNoiseProfileSrvResponse()
139 
140  #-------------------------set noise profile-------------------------#
141  ret = self._set_noise_profile_module.setNoise_profile(\
142  req.user,\
143  req.noise_audio_file,\
144  req.audio_file_type)
145  if ret == 'true':
146  res.success = ret
147  res.error = ''
148  else:
149  res.success = 'false'
150  res.error = ret
151  return res
152 
153  ## Service callback for denoising
154  #
155  # @param req [rapp_platform_ros_comminications::AudioProcessing::AudioProcessingDenoiseSrv] The denoise request
156  #
157  # @return res [rapp_platform_ros_comminications::AudioProcessing::AudioProcessingDenoiseSrvResponse] The denoise response
158  def denoiseCallback(self, req):
159  res = AudioProcessingDenoiseSrvResponse()
160  res.success = self._sox_denoise_module.soxDenoise(\
161  req.user,\
162  req.audio_type,\
163  req.audio_file,\
164  req.denoised_audio_file,\
165  req.scale)
166  return res
167 
168  ## Service callback for Detecting silence
169  #
170  # @param req [rapp_platform_ros_comminications::AudioProcessing::AudioProcessingDetectSilence] The detect silence request
171  #
172  # @return res [rapp_platform_ros_comminications::AudioProcessing::AudioProcessingDetectSilenceSrvResponse] The detect silence response
173  def detectSilenceCallback(self, req):
174  res = AudioProcessingDetectSilenceSrvResponse()
175  [res.level, res.silence] = self._detect_silence_module.detectSilence(\
176  req.audio_file, req.threshold)
177  if res.silence == True:
178  res.silence = "true"
179  else:
180  res.silence = "false"
181  return res
182 
183  ## Service callback for Energy denoising
184  #
185  # @param req [rapp_platform_ros_comminications::AudioProcessing::AudioProcessingDenoiseSrv] The denoise request
186  #
187  # @return res [rapp_platform_ros_comminications::AudioProcessing::AudioProcessingDenoiseSrvResponse] The energy denoise response
188  def energyDenoiseCallback(self, req):
189  res = AudioProcessingDenoiseSrvResponse()
190  output = self._energy_denoise_module.energyDenoise(\
191  req.audio_file, req.scale, req.denoised_audio_file,\
193  if output == True:
194  res.success = "true"
195  else:
196  res.success = "false"
197  return res
198 
199  ## Service callback for Audio transformation
200  #
201  # @param req [rapp_platform_ros_comminications::AudioProcessing::AudioProcessingTransformAudioSrv] The transform audio request
202  #
203  # @return res [rapp_platform_ros_comminications::AudioProcessing::AudioProcessingTransformAudioSrvResponse] The transform audio response
204  def transformAudioCallback(self, req):
205  res = AudioProcessingTransformAudioSrvResponse()
206 
207  [ res.error, res.fullpath ] = \
208  self._transform_audio_module.transform_audio( \
209  req.source_type, req.source_name, req.target_type, \
210  req.target_name, req.target_channels, req.target_rate )
211 
212  return res
213 
214 # Main function
215 if __name__ == "__main__":
216  rospy.init_node('AudioProcessing')
217  AudioProcessingNode = AudioProcessing()
218  rospy.spin()
_set_noise_profile_module
Instantiates rapp_set_noise_profile.SetNoiseProfile.
_transform_audio_module
Instantiates rapp_transform_audio.TransformAudio.
def transformAudioCallback
Service callback for Audio transformation.
def __init__
Constructor performing initializations.
_detect_silence_module
Instantiates rapp_detect_silence.DetectSilence.
def detectSilenceCallback
Service callback for Detecting silence.
def energyDenoiseCallback
Service callback for Energy denoising.
_energy_denoise_module
Instantiates rapp_energy_denoise.EnergyDenoise.
def setNoiseProfileCallback
Service callback for setting noise profile.
_sox_denoise_module
Instantiates rapp_sox_denoise.SoxDenoise.