History

[py] improve code style of slicing.py

Paul Brossier committed 8 years ago

Commit 2c8ada61 parent 78ebc271 file · +10 −11Browse files

Showing the changes to python (1 of 1 file). Show all

Unified · Split

python/lib/aubio/slicing.py+10−11
@@ -5,6 +5,7 @@ from aubio import source, sink · 4 unmodified lines
5 5
6_max_timestamp = 1e1206_max_timestamp = 1e120
7 7
8
8def slice_source_at_stamps(source_file, timestamps, timestamps_end=None,9def slice_source_at_stamps(source_file, timestamps, timestamps_end=None,
9 output_dir=None, samplerate=0, hopsize=256,10 output_dir=None, samplerate=0, hopsize=256,
10 create_first=False):11 create_first=False):
@@ -72,7 +73,7 @@ def slice_source_at_stamps(source_file, timestamps, timestamps_end=None, · 61 unmodified lines
72 the file.73 the file.
73 """74 """
74 75
75 if timestamps is None or len(timestamps) == 0:76 if not timestamps:
76 raise ValueError("no timestamps given")77 raise ValueError("no timestamps given")
77 78
78 if timestamps[0] != 0 and create_first:79 if timestamps[0] != 0 and create_first:
@@ -89,7 +90,6 @@ def slice_source_at_stamps(source_file, timestamps, timestamps_end=None, · 10 unmodified lines
89 timestamps_end = [t - 1 for t in timestamps[1:]] + [_max_timestamp]90 timestamps_end = [t - 1 for t in timestamps[1:]] + [_max_timestamp]
90 91
91 regions = list(zip(timestamps, timestamps_end))92 regions = list(zip(timestamps, timestamps_end))
92 #print regions
93 93
94 source_base_name, _ = os.path.splitext(os.path.basename(source_file))94 source_base_name, _ = os.path.splitext(os.path.basename(source_file))
95 if output_dir is not None:95 if output_dir is not None:
@@ -97,8 +97,8 @@ def slice_source_at_stamps(source_file, timestamps, timestamps_end=None, · 1 unmodified line
97 os.makedirs(output_dir)97 os.makedirs(output_dir)
98 source_base_name = os.path.join(output_dir, source_base_name)98 source_base_name = os.path.join(output_dir, source_base_name)
99 99
100 def new_sink_name(source_base_name, timestamp, samplerate):100 def _new_sink_name(source_base_name, timestamp, samplerate):
101 """ create a sink based on a timestamp in samples, converted in seconds """101 # create name based on a timestamp in samples, converted in seconds
102 timestamp_seconds = timestamp / float(samplerate)102 timestamp_seconds = timestamp / float(samplerate)
103 return source_base_name + "_%011.6f" % timestamp_seconds + '.wav'103 return source_base_name + "_%011.6f" % timestamp_seconds + '.wav'
104 104
@@ -113,16 +113,17 @@ def slice_source_at_stamps(source_file, timestamps, timestamps_end=None, · 8 unmodified lines
113 # get hopsize new samples from source113 # get hopsize new samples from source
114 vec, read = _source.do_multi()114 vec, read = _source.do_multi()
115 # if the total number of frames read will exceed the next region start115 # if the total number of frames read will exceed the next region start
116 while len(regions) and total_frames + read >= regions[0][0]:116 while regions and total_frames + read >= regions[0][0]:
117 #print "getting", regions[0], "at", total_frames
118 # get next region117 # get next region
119 start_stamp, end_stamp = regions.pop(0)118 start_stamp, end_stamp = regions.pop(0)
120 # create a name for the sink119 # create a name for the sink
121 new_sink_path = new_sink_name(source_base_name, start_stamp, samplerate)120 new_sink_path = _new_sink_name(source_base_name, start_stamp,
121 samplerate)
122 # create its sink122 # create its sink
123 _sink = sink(new_sink_path, samplerate, _source.channels)123 _sink = sink(new_sink_path, samplerate, _source.channels)
124 # create a dictionary containing all this124 # create a dictionary containing all this
125 new_slice = {'start_stamp': start_stamp, 'end_stamp': end_stamp, 'sink': _sink}125 new_slice = {'start_stamp': start_stamp, 'end_stamp': end_stamp,
126 'sink': _sink}
126 # append the dictionary to the current list of slices127 # append the dictionary to the current list of slices
127 slices.append(new_slice)128 slices.append(new_slice)
128 129
@@ -134,13 +135,11 @@ def slice_source_at_stamps(source_file, timestamps, timestamps_end=None, · 5 unmodified lines
134 start = max(start_stamp - total_frames, 0)135 start = max(start_stamp - total_frames, 0)
135 # number of samples yet to written be until end of region136 # number of samples yet to written be until end of region
136 remaining = end_stamp - total_frames + 1137 remaining = end_stamp - total_frames + 1
137 #print current_slice, remaining, start
138 # not enough frames remaining, time to split138 # not enough frames remaining, time to split
139 if remaining < read:139 if remaining < read:
140 if remaining > start:140 if remaining > start:
141 # write remaining samples from current region141 # write remaining samples from current region
142 _sink.do_multi(vec[:, start:remaining], remaining - start)142 _sink.do_multi(vec[:, start:remaining], remaining - start)
143 #print("closing region", "remaining", remaining)
144 # close this file143 # close this file
145 _sink.close()144 _sink.close()
146 elif read > start:145 elif read > start:
@@ -149,6 +148,6 @@ def slice_source_at_stamps(source_file, timestamps, timestamps_end=None, · 2 unmodified lines
149 total_frames += read148 total_frames += read
150 # remove old slices149 # remove old slices
151 slices = list(filter(lambda s: s['end_stamp'] > total_frames,150 slices = list(filter(lambda s: s['end_stamp'] > total_frames,
152 slices))151 slices))
153 if read < hopsize:152 if read < hopsize:
154 break153 break

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t