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_set_noise_profile.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 sys
22 import time
23 import os
24 
25 from scipy.io import wavfile
26 
27 from rapp_utilities import Utilities
28 
29 ## @class SetNoiseProfile
30 # Evaluates the noise profile for an audio file
32 
33  ## Performs initializations
34  def __init__(self):
35  self.utilities = Utilities()
36 
37  ## Evaluates the audio profile
38  #
39  # Handles service callback
40  # rapp_audio_processing.AudioProcessing#setNoiseProfileCallback
41  #
42  # @param user [string] The system user, for environmental variable access
43  # @param noise_audio_file [string] Noise audio file path
44  # @param audio_type [string] Noise audio file's type
45  def setNoise_profile(self, user, noise_audio_file, audio_file_type):
46 
47  cleanup = []
48  directory = os.path.expanduser("~/rapp_platform_files/audio_processing/") + user
49  if not os.path.isdir(directory):
50  os.makedirs(directory)
51  com_res = os.system("chmod 777 " + directory)
52  if com_res != 0:
53  return "Error: Server chmod malfunctioned"
54 
55  directory += "/noise_profile/"
56  if not os.path.isdir(directory):
57  os.makedirs(directory)
58  com_res = os.system("chmod 777 " + directory)
59  if com_res != 0:
60  return "Error: Server chmod malfunctioned"
61 
62  noise_profile_file = directory
63  new_audio = noise_audio_file
64 
65  if not os.path.isfile(new_audio):
66  return "Error: The audio file does not exist"
67 
68  # Making audio compatible to sphinx4
69  if audio_file_type == 'nao_ogg':
70  if ".ogg" not in new_audio:
71  return "Error: ogg type selected but file is of another type"
72  new_audio += ".wav"
73  com_res = os.system("sox " + noise_audio_file + " " + new_audio)
74  if com_res != 0:
75  return "Error: Server sox malfunctioned"
76  cleanup.append(new_audio)
77 
78  elif audio_file_type == "nao_wav_1_ch":
79  if ".wav" not in new_audio:
80  return "Error: wav type 1 channel selected but file is of another type"
81  samp_freq, signal = wavfile.read(new_audio)
82  if len(signal.shape) != 1:
83  return "Error: wav 1 ch declared but the audio file has " +\
84  str(signal.shape[1]) + ' channels'
85  pass
86 
87  elif audio_file_type == "nao_wav_4_ch":
88  if ".wav" not in new_audio:
89  return "Error: wav type 4 channels selected but file is of another type"
90  samp_freq, signal = wavfile.read(new_audio)
91  if len(signal.shape) != 2 or signal.shape[1] != 4:
92  return "Error: wav 4 ch declared but the audio file has not 4 channels"
93  new_audio += "_1ch.wav"
94  com_res = os.system("sox " + noise_audio_file + " -c 1 -r 16000 " + \
95  new_audio)
96  if com_res != 0:
97  return "Error: Server sox malfunctioned"
98  cleanup.append(new_audio)
99  else:
100  success = ''
101  success = "Non valid noise audio type"
102  status = self.utilities.cleanup(cleanup)
103  if status != True:
104  success += " " + status
105  return success
106 
107  noise_profile_uri = directory + "/noise_profile_" + audio_file_type
108  # Extract noise_profile
109  com_res = os.system(\
110  "sox " + new_audio + " -t null /dev/null trim 0.5 2.5 noiseprof "\
111  + noise_profile_uri)
112  if com_res != 0:
113  return "Error: Server sox malfunctioned"
114 
115  com_res = os.system("chmod 777 " + noise_profile_uri)
116  if com_res != 0:
117  return "Error: Server chmod malfunctioned"
118 
119  status = self.utilities.cleanup(cleanup)
120  if status != True:
121  return status
122  else:
123  return "true"
124 
Evaluates the noise profile for an audio file.