From 2c8ada64b37bb68c5f5ce274b9ecf90a779aaef0 Mon Sep 17 00:00:00 2001 From: Paul Brossier Date: Sun, 4 Nov 2018 20:01:08 +0100 Subject: [PATCH] [py] improve code style of slicing.py --- python/lib/aubio/slicing.py | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/python/lib/aubio/slicing.py b/python/lib/aubio/slicing.py index e85ac034..4d9964c1 100644 --- a/python/lib/aubio/slicing.py +++ b/python/lib/aubio/slicing.py @@ -5,6 +5,7 @@ from aubio import source, sink _max_timestamp = 1e120 + def slice_source_at_stamps(source_file, timestamps, timestamps_end=None, output_dir=None, samplerate=0, hopsize=256, create_first=False): @@ -72,7 +73,7 @@ def slice_source_at_stamps(source_file, timestamps, timestamps_end=None, the file. """ - if timestamps is None or len(timestamps) == 0: + if not timestamps: raise ValueError("no timestamps given") if timestamps[0] != 0 and create_first: @@ -89,7 +90,6 @@ def slice_source_at_stamps(source_file, timestamps, timestamps_end=None, timestamps_end = [t - 1 for t in timestamps[1:]] + [_max_timestamp] regions = list(zip(timestamps, timestamps_end)) - #print regions source_base_name, _ = os.path.splitext(os.path.basename(source_file)) if output_dir is not None: @@ -97,8 +97,8 @@ def slice_source_at_stamps(source_file, timestamps, timestamps_end=None, 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 """ + def _new_sink_name(source_base_name, timestamp, samplerate): + # create name based on a timestamp in samples, converted in seconds timestamp_seconds = timestamp / float(samplerate) return source_base_name + "_%011.6f" % timestamp_seconds + '.wav' @@ -113,16 +113,17 @@ def slice_source_at_stamps(source_file, timestamps, timestamps_end=None, # get hopsize new samples from source vec, read = _source.do_multi() # if the total number of frames read will exceed the next region start - while len(regions) and total_frames + read >= regions[0][0]: - #print "getting", regions[0], "at", total_frames + while regions and total_frames + read >= regions[0][0]: # get next region start_stamp, end_stamp = regions.pop(0) # create a name for the sink - new_sink_path = new_sink_name(source_base_name, start_stamp, samplerate) + new_sink_path = _new_sink_name(source_base_name, start_stamp, + samplerate) # create its sink _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': _sink} + 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) @@ -134,13 +135,11 @@ def slice_source_at_stamps(source_file, timestamps, timestamps_end=None, start = max(start_stamp - total_frames, 0) # number of samples yet to written be until end of region remaining = end_stamp - total_frames + 1 - #print current_slice, remaining, start # not enough frames remaining, time to split if remaining < read: if remaining > start: # write remaining samples from current region _sink.do_multi(vec[:, start:remaining], remaining - start) - #print("closing region", "remaining", remaining) # close this file _sink.close() elif read > start: @@ -149,6 +148,6 @@ def slice_source_at_stamps(source_file, timestamps, timestamps_end=None, total_frames += read # remove old slices slices = list(filter(lambda s: s['end_stamp'] > total_frames, - slices)) + slices)) if read < hopsize: break -- 2.11.0