From 8b56b1835e9b8f4858547b88c82e2cf2c977ea9f Mon Sep 17 00:00:00 2001 From: Paul Brossier Date: Tue, 10 May 2016 22:42:55 +0200 Subject: [PATCH] python/lib/aubio/slicing.py: clean up --- python/lib/aubio/slicing.py | 47 ++++++++++++++++++++++++--------------------- 1 file changed, 25 insertions(+), 22 deletions(-) diff --git a/python/lib/aubio/slicing.py b/python/lib/aubio/slicing.py index 93bd0c7c..3523dd10 100644 --- a/python/lib/aubio/slicing.py +++ b/python/lib/aubio/slicing.py @@ -1,15 +1,16 @@ -from aubio import source, sink +"""utility routines to slice sound files at given timestamps""" + import os +from aubio import source, sink -max_timestamp = 1e120 +_max_timestamp = 1e120 -def slice_source_at_stamps(source_file, timestamps, timestamps_end = None, - output_dir = None, - samplerate = 0, - hopsize = 256): +def slice_source_at_stamps(source_file, timestamps, timestamps_end=None, + output_dir=None, samplerate=0, hopsize=256): + """ slice a sound file at given timestamps """ - if timestamps == None or len(timestamps) == 0: - raise ValueError ("no timestamps given") + if timestamps is None or len(timestamps) == 0: + raise ValueError("no timestamps given") if timestamps[0] != 0: timestamps = [0] + timestamps @@ -18,33 +19,34 @@ def slice_source_at_stamps(source_file, timestamps, timestamps_end = None, if timestamps_end != None: if len(timestamps_end) != len(timestamps): - raise ValueError ("len(timestamps_end) != len(timestamps)") + raise ValueError("len(timestamps_end) != len(timestamps)") else: - timestamps_end = [t - 1 for t in timestamps[1:] ] + [ max_timestamp ] + timestamps_end = [t - 1 for t in timestamps[1:]] + [_max_timestamp] regions = list(zip(timestamps, timestamps_end)) #print regions - source_base_name, source_ext = os.path.splitext(os.path.basename(source_file)) + source_base_name, _ = os.path.splitext(os.path.basename(source_file)) if output_dir != None: if not os.path.isdir(output_dir): os.makedirs(output_dir) source_base_name = os.path.join(output_dir, source_base_name) def new_sink_name(source_base_name, timestamp, samplerate): + """ create a sink based on a timestamp in samples, converted in seconds """ timestamp_seconds = timestamp / float(samplerate) return source_base_name + "_%011.6f" % timestamp_seconds + '.wav' - # reopen source file - s = source(source_file, samplerate, hopsize) - samplerate = s.get_samplerate() + # open source file + _source = source(source_file, samplerate, hopsize) + samplerate = source.get_samplerate() total_frames = 0 slices = [] while True: # get hopsize new samples from source - vec, read = s.do_multi() + vec, read = _source.do_multi() # if the total number of frames read will exceed the next region start if len(regions) and total_frames + read >= regions[0][0]: #print "getting", regions[0], "at", total_frames @@ -53,16 +55,16 @@ def slice_source_at_stamps(source_file, timestamps, timestamps_end = None, # create a name for the sink new_sink_path = new_sink_name(source_base_name, start_stamp, samplerate) # create its sink - g = sink(new_sink_path, samplerate, s.channels) + _sink = sink(new_sink_path, samplerate, _source.channels) # create a dictionary containing all this - new_slice = {'start_stamp': start_stamp, 'end_stamp': end_stamp, 'sink': g} + new_slice = {'start_stamp': start_stamp, 'end_stamp': end_stamp, 'sink': _sink} # append the dictionary to the current list of slices slices.append(new_slice) for current_slice in slices: start_stamp = current_slice['start_stamp'] end_stamp = current_slice['end_stamp'] - g = current_slice['sink'] + _sink = current_slice['sink'] # sample index to start writing from new source vector start = max(start_stamp - total_frames, 0) # number of samples yet to written be until end of region @@ -72,12 +74,13 @@ def slice_source_at_stamps(source_file, timestamps, timestamps_end = None, if remaining < read: if remaining > start: # write remaining samples from current region - g.do_multi(vec[:,start:remaining], remaining - start) + _sink.do_multi(vec[:, start:remaining], remaining - start) #print "closing region", "remaining", remaining # close this file - g.close() + _sink.close() elif read > start: # write all the samples - g.do_multi(vec[:,start:read], read - start) + _sink.do_multi(vec[:, start:read], read - start) total_frames += read - if read < hopsize: break + if read < hopsize: + break -- 2.11.0