def read_audio(read_dir, sub_dirs, T_total, padding_dir, labels): for k, sub_dir in enumerate(sub_dirs): #print("label: %s" % (label)) print("sub_dir: %s" % (sub_dir)) for i, f in enumerate( glob.glob(read_dir + os.sep + sub_dir + os.sep + '*.wav')): # for each WAV file wavFile = f print wavFile waveFile_name = (os.path.splitext(wavFile)[0]).split(os.sep)[2] print waveFile_name quality = util.splitext(waveFile_name)[2] print quality # Read the wav file in_data, fs = librosa.load(wavFile) #print("fs is ",fs) #print("data is ", in_data) #print os.path.splitext(wavFile)[0] cmd = """ffmpeg -i """ + wavFile + """ 2>&1 | grep "Duration"| cut -d ' ' -f 4 | sed s/,// | awk '{ split($1, A, ":"); print 3600*A[1] + 60*A[2] + A[3] }'""" rs = sh.run(cmd, True) duration_in_wavFile = rs.stdout() print("the duration of wavfile is ", duration_in_wavFile) print type(duration_in_wavFile) if float(T_total) >= float(duration_in_wavFile): out_data = padding_audio(in_data, fs, T_total) # Save the output file for l in labels: if quality == l: out_wav = padding_dir + "/" + l + "/" + waveFile_name + ".wav" # if quality == "Eg": # out_wav = result_padding_dir +"/"+ eg_padding_dir +"/" +waveFile_name+".wav" librosa.output.write_wav(out_wav, out_data, fs) else: int_num = int(float(duration_in_wavFile) / T_total) + 1 number = float(duration_in_wavFile) % T_total print number, int_num if number != 0: T = T_total * int_num out_data = padding_audio(in_data, fs, T) for l in labels: if quality == l: out_wav = padding_dir + "/" + l + "/" + waveFile_name + ".wav" # if quality == "Eg": # out_wav = result_padding_dir +"/"+ eg_padding_dir +"/" +waveFile_name+".wav" librosa.output.write_wav(out_wav, out_data, fs)
def handle_request(self): ''' The main request handler for Pecan applications. ''' # get a sorted list of hooks, by priority (no controller hooks yet) state.hooks = self.determine_hooks() # store the routing path to allow hooks to modify it request.pecan['routing_path'] = request.path # handle "on_route" hooks self.handle_hooks('on_route', state) # lookup the controller, respecting content-type as requested # by the file extension on the URI path = request.pecan['routing_path'] if not request.pecan['content_type'] and '.' in path.split('/')[-1]: path, extension = splitext(path) request.pecan['extension'] = extension # preface with a letter to ensure compat for 2.5 request.pecan['content_type'] = guess_type('x' + extension)[0] controller, remainder = self.route(self.root, path) cfg = _cfg(controller) if cfg.get('generic_handler'): raise exc.HTTPNotFound # handle generic controllers im_self = None if cfg.get('generic'): im_self = controller.im_self handlers = cfg['generic_handlers'] controller = handlers.get(request.method, handlers['DEFAULT']) cfg = _cfg(controller) # add the controller to the state so that hooks can use it state.controller = controller # if unsure ask the controller for the default content type if not request.pecan['content_type']: request.pecan['content_type'] = cfg.get('content_type', 'text/html') elif cfg.get('content_type') is not None and \ request.pecan['content_type'] not in cfg.get('content_types', {}): print "Controller '%s' defined does not support content_type '%s'. Supported type(s): %s" % ( controller.__name__, request.pecan['content_type'], cfg.get('content_types', {}).keys() ) raise exc.HTTPNotFound # get a sorted list of hooks, by priority state.hooks = self.determine_hooks(controller) # handle "before" hooks self.handle_hooks('before', state) # fetch and validate any parameters params = dict(request.str_params) if 'schema' in cfg: params = self.validate( cfg['schema'], params, json=cfg['validate_json'], error_handler=cfg.get('error_handler'), htmlfill=cfg.get('htmlfill'), variable_decode=cfg.get('variable_decode') ) elif 'pecan.validation_errors' in request.environ: request.pecan['validation_errors'] = request.environ.pop('pecan.validation_errors') # fetch the arguments for the controller args, kwargs = self.get_args( params, remainder, cfg['argspec'], im_self ) # get the result from the controller result = controller(*args, **kwargs) # a controller can return the response object which means they've taken # care of filling it out if result == response: return raw_namespace = result # pull the template out based upon content type and handle overrides template = cfg.get('content_types', {}).get(request.pecan['content_type']) # check if for controller override of template template = request.pecan.get('override_template', template) request.pecan['content_type'] = request.pecan.get('override_content_type', request.pecan['content_type']) # if there is a template, render it if template: if template == 'json': request.pecan['content_type'] = 'application/json' result = self.render(template, result) # pass the response through htmlfill (items are popped out of the # environment even if htmlfill won't run for proper cleanup) _htmlfill = cfg.get('htmlfill') if _htmlfill is None and 'pecan.htmlfill' in request.environ: _htmlfill = request.environ.pop('pecan.htmlfill') if 'pecan.params' in request.environ: params = request.environ.pop('pecan.params') if request.pecan['validation_errors'] and _htmlfill is not None and request.pecan['content_type'] == 'text/html': errors = request.pecan['validation_errors'] result = htmlfill.render(result, defaults=params, errors=errors, text_as_default=True, **_htmlfill) # If we are in a test request put the namespace where it can be # accessed directly if request.environ.get('paste.testing'): testing_variables = request.environ['paste.testing_variables'] testing_variables['namespace'] = raw_namespace testing_variables['template_name'] = template testing_variables['controller_output'] = result # set the body content if isinstance(result, unicode): response.unicode_body = result else: response.body = result # set the content type if request.pecan['content_type']: response.content_type = request.pecan['content_type']
def splitext(self): return splitext(posixpath.basename(self.path.rstrip('/')))