22 from scipy.io
import wavfile
45 target_type, target_name, target_channels, target_rate ):
48 self.
_assertArgs( source_type, source_name, target_type, \
49 target_name, target_channels, target_rate )
50 except Exception
as e:
55 except Exception
as e:
59 self.
_convertType( source_type, source_name, target_type, \
60 target_name, target_channels, target_rate )
61 except Exception
as e:
64 return [
'success', target_name ]
75 def _assertArgs(self, source_type, source_name, target_type, target_name, \
76 target_channels, target_rate ):
78 if not os.path.isfile( source_name ):
79 raise Exception(
"Error: file \'" + source_name +
'\' not found' )
81 raise Exception(
"Error: target filename not provided" )
83 raise Exception(
"Error: target type not provided" )
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" )
100 def _convertType(self, source_type, source_name, target_type, target_name, \
101 target_channels, target_rate ):
105 if target_type ==
'flac':
106 if target_channels != 0:
107 channels =
'--channels=' + str( target_channels )
109 rate =
'--sample-rate=' + str( target_rate )
111 command =
'flac -f ' + channels +
' ' + rate +
" " + source_name + \
112 ' -o ' + target_name +
" --totally-silent --channel-map=none"
113 flac_status = os.system( command )
116 if os.path.isfile( target_name ) !=
True or flac_status != 0 :
117 raise Exception(
"Error: flac command malfunctioned. File path was"\
120 if target_channels != 0:
121 channels =
'-c ' + str( target_channels )
123 rate =
'-r ' + str( target_rate )
124 command =
"sox " + source_name +
" " + channels +
" " + rate + \
127 sox_status = os.system( command )
129 if os.path.isfile( target_name ) !=
True or sox_status:
130 raise Exception(
"Error: SoX malfunctioned. File path was" + \
141 [ source_file_name, source_extention ] = os.path.splitext( name )
143 if source_type ==
'nao_ogg':
144 if source_extention !=
'.ogg':
145 raise Exception(
"Error: ogg type selected but file is of another type" )
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" )
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 )
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" )
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" )
166 raise Exception(
"Non valid noise audio type" )