ref: 2c8ada64b37bb68c5f5ce274b9ecf90a779aaef0
parent: 78ebc2788c95e677cc473027b20416cbd85cc9e0
author: Paul Brossier <piem@piem.org>
date: Sun Nov 4 15:01:08 EST 2018
[py] improve code style of slicing.py
--- a/python/lib/aubio/slicing.py
+++ b/python/lib/aubio/slicing.py
@@ -5,6 +5,7 @@
_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 @@
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 @@
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 @@
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 @@
# 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 @@
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 @@
total_frames += read
# remove old slices
slices = list(filter(lambda s: s['end_stamp'] > total_frames,
- slices))
+ slices))
if read < hopsize:
break