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_transform_audio.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: Aris Thallas
19 # contact: aris.thallas@{iti.gr, gmail.com}
20 
21 import os
22 from scipy.io import wavfile
23 
24 ## @class TransformAudio
25 # @brief Provides audio type tranformation functionalities
26 #
27 # Allows the transformation of an audio file to a different format. Supports
28 # the alteration of the type (i.e. headset, nao_wav_1_ch etc.), the sample
29 # rate, the audio channel number, the audio format and the audio name.
30 # Handles transform audio service callback
31 # (rapp_audio_processing.rapp_audio_processing.AudioProcessing::transform_audio)
33 
34  ## @brief Performs the audio transformation
35  # @param source_type [string] The source audio file's type
36  # @param source_name [string] The source audio file's name
37  # @param target_type [string] The target audio file's type
38  # @param target_name [string] The target audio file's name
39  # @param target_channels [string] The target audio's channel number
40  # @param target_rate [string] The target audio's sample rate
41  #
42  # @return status [string] The status of the transformation prcedure
43  # @return filename [string] The name of the produced file
44  def transform_audio(self, source_type, source_name, \
45  target_type, target_name, target_channels, target_rate ):
46 
47  try:
48  self._assertArgs( source_type, source_name, target_type, \
49  target_name, target_channels, target_rate )
50  except Exception as e:
51  return [ str(e), '' ]
52 
53  try:
54  self._validateSourceType( source_type, source_name )
55  except Exception as e:
56  return [ str(e), '' ]
57 
58  try:
59  self._convertType( source_type, source_name, target_type, \
60  target_name, target_channels, target_rate )
61  except Exception as e:
62  return [ str(e), '' ]
63 
64  return [ 'success', target_name ]
65 
66  ## @brief Verifies the existence of the required arguments
67  # @param source_type [string] The source audio file's type
68  # @param source_name [string] The source audio file's name
69  # @param target_type [string] The target audio file's type
70  # @param target_name [string] The target audio file's name
71  # @param target_channels [string] The target audio's channel number
72  # @param target_rate [string] The target audio's sample rate
73  #
74  # @exception Exception Arguments are invalid
75  def _assertArgs(self, source_type, source_name, target_type, target_name, \
76  target_channels, target_rate ):
77 
78  if not os.path.isfile( source_name ):
79  raise Exception( "Error: file \'" + source_name + '\' not found' )
80  if target_name == '':
81  raise Exception( "Error: target filename not provided" )
82  if target_type == '':
83  raise Exception( "Error: target type not provided" )
84  if target_rate < 0:
85  raise Exception( "Error: target_rate can not be negative" )
86  if target_channels < 0:
87  raise Exception( "Error: target_channels can not be negative" )
88  if target_channels > 8:
89  raise Exception( "Error: target_channels can not be greater than 8" )
90 
91  ## @brief Performs audio conversion
92  # @param source_type [string] The source audio file's type
93  # @param source_name [string] The source audio file's name
94  # @param target_type [string] The target audio file's type
95  # @param target_name [string] The target audio file's name
96  # @param target_channels [string] The target audio's channel number
97  # @param target_rate [string] The target audio's sample rate
98  #
99  # @exception Exception Conversion malfunction
100  def _convertType(self, source_type, source_name, target_type, target_name, \
101  target_channels, target_rate ):
102 
103  channels = ''
104  rate = ''
105  if target_type == 'flac':
106  if target_channels != 0:
107  channels = '--channels=' + str( target_channels )
108  if target_rate != 0:
109  rate = '--sample-rate=' + str( target_rate )
110 
111  command = 'flac -f ' + channels + ' ' + rate + " " + source_name + \
112  ' -o ' + target_name + " --totally-silent --channel-map=none"
113  flac_status = os.system( command )
114 
115 
116  if os.path.isfile( target_name ) != True or flac_status != 0 :
117  raise Exception( "Error: flac command malfunctioned. File path was"\
118  + source_name )
119  else:
120  if target_channels != 0:
121  channels = '-c ' + str( target_channels )
122  if target_rate != 0:
123  rate = '-r ' + str( target_rate )
124  command = "sox " + source_name + " " + channels + " " + rate + \
125  " " + target_name
126 
127  sox_status = os.system( command )
128 
129  if os.path.isfile( target_name ) != True or sox_status:
130  raise Exception( "Error: SoX malfunctioned. File path was" + \
131  source_name )
132 
133 
134  ## @brief Validates that the provided audio type match the file extension
135  # @param source_type [string] The source audio file's type
136  # @param name [string] The source audio file's name
137  #
138  # @exception Exception Audio type/file type mismatch
139  def _validateSourceType( self, source_type, name ):
140 
141  [ source_file_name, source_extention ] = os.path.splitext( name )
142 
143  if source_type == 'nao_ogg':
144  if source_extention != '.ogg':
145  raise Exception( "Error: ogg type selected but file is of another type" )
146 
147  elif source_type == "nao_wav_1_ch" or source_type == 'headset':
148  if source_extention != ".wav":
149  raise Exception( "Error: wav type 1 channel selected but file is of another type" )
150 
151  samp_freq, signal = wavfile.read( name )
152  if len( signal.shape ) != 1:
153  error = ("Error: wav 1 ch declared but the audio file has " +\
154  str(signal.shape[1]) + ' channels')
155  raise Exception( error )
156 
157  elif source_type == "nao_wav_4_ch":
158  if source_extention != ".wav":
159  raise Exception( "Error: wav type 4 channels selected but file is of another type" )
160 
161  samp_freq, signal = wavfile.read( name )
162  if len(signal.shape) != 2 or signal.shape[1] != 4:
163  raise Exception( "Error: wav 4 ch declared but the audio file has not 4 channels" )
164 
165  else:
166  raise Exception( "Non valid noise audio type" )
def _assertArgs
Verifies the existence of the required arguments.
def _validateSourceType
Validates that the provided audio type match the file extension.
Provides audio type tranformation functionalities.
def transform_audio
Performs the audio transformation.