History

Merge branch 'fix/slicing' into feature/docstrings

Paul Brossier committed 8 years ago

Commit 92a6af02 parents 7165fc5 adf09b24 files · +41 −10Browse files

Showing the changes to python (4 of 4 files). Show all

Unified · Split

4 files
  1. python/lib/aubio/cmd.py +3 −0
  2. python/lib/aubio/cut.py +3 −2
  3. python/lib/aubio/slicing.py +11 −5
  4. python/tests/test_slicing.py +24 −3
python/lib/aubio/cmd.py+3−0
@@ -247,6 +247,9 @@ class AubioArgumentParser(argparse.ArgumentParser): · 246 unmodified lines
247247 metavar = "<slices>",
248248 action = "store", dest = "cut_until_nslices", default = None,
249249 help="how many extra slices should be added at the end of each slice")
250+ self.add_argument("--create-first",
251+ action = "store_true", dest = "create_first", default = False,
252+ help="always include first slice")
250253
251254 # some utilities
252255
python/lib/aubio/cut.py+3−2
@@ -101,7 +101,7 @@ def _cut_analyze(options): · 100 unmodified lines
101101
102102 s = source(source_uri, samplerate, hopsize)
103103 if samplerate == 0:
104− samplerate = s.get_samplerate()
104+ samplerate = s.samplerate
105105 options.samplerate = samplerate
106106
107107 if options.beat:
@@ -150,7 +150,8 @@ def _cut_slice(options, timestamps): · 42 unmodified lines
150150 slice_source_at_stamps(options.source_uri,
151151 timestamps, timestamps_end = timestamps_end,
152152 output_dir = options.output_directory,
153− samplerate = options.samplerate)
153+ samplerate = options.samplerate,
154+ create_first = options.create_first)
154155
155156 def main():
156157 parser = aubio_cut_parser()
python/lib/aubio/slicing.py+11−5
@@ -6,19 +6,22 @@ from aubio import source, sink · 5 unmodified lines
66 _max_timestamp = 1e120
77
88 def slice_source_at_stamps(source_file, timestamps, timestamps_end=None,
9− output_dir=None, samplerate=0, hopsize=256):
9+ output_dir=None, samplerate=0, hopsize=256,
10+ create_first=False):
1011 """ slice a sound file at given timestamps """
1112
1213 if timestamps is None or len(timestamps) == 0:
1314 raise ValueError("no timestamps given")
1415
15− if timestamps[0] != 0:
16+ if timestamps[0] != 0 and create_first:
1617 timestamps = [0] + timestamps
1718 if timestamps_end is not None:
1819 timestamps_end = [timestamps[1] - 1] + timestamps_end
1920
2021 if timestamps_end is not None:
21− if len(timestamps_end) != len(timestamps):
22+ if len(timestamps_end) == len(timestamps) - 1:
23+ timestamps_end = timestamps_end + [_max_timestamp]
24+ elif len(timestamps_end) != len(timestamps):
2225 raise ValueError("len(timestamps_end) != len(timestamps)")
2326 else:
2427 timestamps_end = [t - 1 for t in timestamps[1:]] + [_max_timestamp]
@@ -48,7 +51,7 @@ def slice_source_at_stamps(source_file, timestamps, timestamps_end=None, · 23 unmodified lines
4851 # get hopsize new samples from source
4952 vec, read = _source.do_multi()
5053 # if the total number of frames read will exceed the next region start
51− if len(regions) and total_frames + read >= regions[0][0]:
54+ while len(regions) and total_frames + read >= regions[0][0]:
5255 #print "getting", regions[0], "at", total_frames
5356 # get next region
5457 start_stamp, end_stamp = regions.pop(0)
@@ -75,12 +78,15 @@ def slice_source_at_stamps(source_file, timestamps, timestamps_end=None, · 20 unmodified lines
7578 if remaining > start:
7679 # write remaining samples from current region
7780 _sink.do_multi(vec[:, start:remaining], remaining - start)
78− #print "closing region", "remaining", remaining
81+ #print("closing region", "remaining", remaining)
7982 # close this file
8083 _sink.close()
8184 elif read > start:
8285 # write all the samples
8386 _sink.do_multi(vec[:, start:read], read - start)
8487 total_frames += read
88+ # remove old slices
89+ slices = list(filter(lambda s: s['end_stamp'] > total_frames,
90+ slices))
8591 if read < hopsize:
8692 break
python/tests/test_slicing.py+24−3
@@ -23,12 +23,14 @@ class aubio_slicing_test_case(TestCase): · 22 unmodified lines
2323
2424 def test_slice_start_only_no_zero(self):
2525 regions_start = [i*1000 for i in range(1, n_slices)]
26− slice_source_at_stamps(self.source_file, regions_start, output_dir = self.output_dir)
26+ slice_source_at_stamps(self.source_file, regions_start,
27+ output_dir = self.output_dir, create_first=True)
2728
2829 def test_slice_start_beyond_end(self):
2930 regions_start = [i*1000 for i in range(1, n_slices)]
3031 regions_start += [count_samples_in_file(self.source_file) + 1000]
31− slice_source_at_stamps(self.source_file, regions_start, output_dir = self.output_dir)
32+ slice_source_at_stamps(self.source_file, regions_start,
33+ output_dir = self.output_dir, create_first=True)
3234
3335 def test_slice_start_every_blocksize(self):
3436 hopsize = 200
@@ -36,6 +38,12 @@ class aubio_slicing_test_case(TestCase): · 1 unmodified line
3638 slice_source_at_stamps(self.source_file, regions_start, output_dir = self.output_dir,
3739 hopsize = 200)
3840
41+ def test_slice_start_every_half_blocksize(self):
42+ hopsize = 200
43+ regions_start = [i*hopsize//2 for i in range(1, n_slices)]
44+ slice_source_at_stamps(self.source_file, regions_start,
45+ output_dir = self.output_dir, hopsize = 200)
46+
3947 def tearDown(self):
4048 original_samples = count_samples_in_file(self.source_file)
4149 written_samples = count_samples_in_directory(self.output_dir)
@@ -91,6 +99,19 @@ class aubio_slicing_with_ends_test_case(TestCase): · 49 unmodified lines
9199 assert_equal(written_samples, expected_samples,
92100 "number of samples written different from number of original samples")
93101
102+ def test_slice_start_and_ends_with_missing_end(self):
103+ regions_start = [i*1000 for i in range(n_slices)]
104+ regions_ends = [r-1 for r in regions_start[1:]]
105+ slice_source_at_stamps(self.source_file, regions_start, regions_ends,
106+ output_dir = self.output_dir)
107+ written_samples = count_samples_in_directory(self.output_dir)
108+ original_samples = count_samples_in_file(self.source_file)
109+ total_files = count_files_in_directory(self.output_dir)
110+ assert_equal(n_slices, total_files,
111+ "number of slices created different from expected")
112+ assert_equal(written_samples, original_samples,
113+ "number of samples written different from number of original samples")
114+
94115 def tearDown(self):
95116 shutil.rmtree(self.output_dir)
96117
@@ -133,7 +154,7 @@ class aubio_slicing_wrong_ends_test_case(TestCase): · 36 unmodified lines
133154 regions_start = [i*1000 for i in range(1, n_slices)]
134155 regions_end = None
135156 slice_source_at_stamps (self.source_file, regions_start, regions_end,
136− output_dir = self.output_dir)
157+ output_dir = self.output_dir, create_first=True)
137158 total_files = count_files_in_directory(self.output_dir)
138159 assert_equal(n_slices, total_files,
139160 "number of slices created different from expected")

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