Showing the changes to python (4 of 4 files). Show all
Unified · Split
| @@ -247,6 +247,9 @@ class AubioArgumentParser(argparse.ArgumentParser): · 246 unmodified lines | |||
| 247 | metavar = "<slices>", | 247 | metavar = "<slices>", |
| 248 | action = "store", dest = "cut_until_nslices", default = None, | 248 | action = "store", dest = "cut_until_nslices", default = None, |
| 249 | help="how many extra slices should be added at the end of each slice") | 249 | 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") | ||
| 250 | 253 | ||
| 251 | # some utilities | 254 | # some utilities |
| 252 | 255 | ||
| @@ -101,7 +101,7 @@ def _cut_analyze(options): · 100 unmodified lines | |||
| 101 | 101 | ||
| 102 | s = source(source_uri, samplerate, hopsize) | 102 | s = source(source_uri, samplerate, hopsize) |
| 103 | if samplerate == 0: | 103 | if samplerate == 0: |
| 104 | samplerate = s.get_samplerate() | 104 | samplerate = s.samplerate |
| 105 | options.samplerate = samplerate | 105 | options.samplerate = samplerate |
| 106 | 106 | ||
| 107 | if options.beat: | 107 | if options.beat: |
| @@ -150,7 +150,8 @@ def _cut_slice(options, timestamps): · 42 unmodified lines | |||
| 150 | slice_source_at_stamps(options.source_uri, | 150 | slice_source_at_stamps(options.source_uri, |
| 151 | timestamps, timestamps_end = timestamps_end, | 151 | timestamps, timestamps_end = timestamps_end, |
| 152 | output_dir = options.output_directory, | 152 | output_dir = options.output_directory, |
| 153 | samplerate = options.samplerate) | 153 | samplerate = options.samplerate, |
| 154 | create_first = options.create_first) | ||
| 154 | 155 | ||
| 155 | def main(): | 156 | def main(): |
| 156 | parser = aubio_cut_parser() | 157 | parser = aubio_cut_parser() |
| @@ -6,19 +6,22 @@ from aubio import source, sink · 5 unmodified lines | |||
| 6 | _max_timestamp = 1e120 | 6 | _max_timestamp = 1e120 |
| 7 | 7 | ||
| 8 | def slice_source_at_stamps(source_file, timestamps, timestamps_end=None, | 8 | 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): | ||
| 10 | """ slice a sound file at given timestamps """ | 11 | """ slice a sound file at given timestamps """ |
| 11 | 12 | ||
| 12 | if timestamps is None or len(timestamps) == 0: | 13 | if timestamps is None or len(timestamps) == 0: |
| 13 | raise ValueError("no timestamps given") | 14 | raise ValueError("no timestamps given") |
| 14 | 15 | ||
| 15 | if timestamps[0] != 0: | 16 | if timestamps[0] != 0 and create_first: |
| 16 | timestamps = [0] + timestamps | 17 | timestamps = [0] + timestamps |
| 17 | if timestamps_end is not None: | 18 | if timestamps_end is not None: |
| 18 | timestamps_end = [timestamps[1] - 1] + timestamps_end | 19 | timestamps_end = [timestamps[1] - 1] + timestamps_end |
| 19 | 20 | ||
| 20 | if timestamps_end is not None: | 21 | 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): | ||
| 22 | raise ValueError("len(timestamps_end) != len(timestamps)") | 25 | raise ValueError("len(timestamps_end) != len(timestamps)") |
| 23 | else: | 26 | else: |
| 24 | timestamps_end = [t - 1 for t in timestamps[1:]] + [_max_timestamp] | 27 | 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 | |||
| 48 | # get hopsize new samples from source | 51 | # get hopsize new samples from source |
| 49 | vec, read = _source.do_multi() | 52 | vec, read = _source.do_multi() |
| 50 | # if the total number of frames read will exceed the next region start | 53 | # 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]: |
| 52 | #print "getting", regions[0], "at", total_frames | 55 | #print "getting", regions[0], "at", total_frames |
| 53 | # get next region | 56 | # get next region |
| 54 | start_stamp, end_stamp = regions.pop(0) | 57 | 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 | |||
| 75 | if remaining > start: | 78 | if remaining > start: |
| 76 | # write remaining samples from current region | 79 | # write remaining samples from current region |
| 77 | _sink.do_multi(vec[:, start:remaining], remaining - start) | 80 | _sink.do_multi(vec[:, start:remaining], remaining - start) |
| 78 | #print "closing region", "remaining", remaining | 81 | #print("closing region", "remaining", remaining) |
| 79 | # close this file | 82 | # close this file |
| 80 | _sink.close() | 83 | _sink.close() |
| 81 | elif read > start: | 84 | elif read > start: |
| 82 | # write all the samples | 85 | # write all the samples |
| 83 | _sink.do_multi(vec[:, start:read], read - start) | 86 | _sink.do_multi(vec[:, start:read], read - start) |
| 84 | total_frames += read | 87 | total_frames += read |
| 88 | # remove old slices | ||
| 89 | slices = list(filter(lambda s: s['end_stamp'] > total_frames, | ||
| 90 | slices)) | ||
| 85 | if read < hopsize: | 91 | if read < hopsize: |
| 86 | break | 92 | break |
| @@ -23,12 +23,14 @@ class aubio_slicing_test_case(TestCase): · 22 unmodified lines | |||
| 23 | 23 | ||
| 24 | def test_slice_start_only_no_zero(self): | 24 | def test_slice_start_only_no_zero(self): |
| 25 | regions_start = [i*1000 for i in range(1, n_slices)] | 25 | 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) | ||
| 27 | 28 | ||
| 28 | def test_slice_start_beyond_end(self): | 29 | def test_slice_start_beyond_end(self): |
| 29 | regions_start = [i*1000 for i in range(1, n_slices)] | 30 | regions_start = [i*1000 for i in range(1, n_slices)] |
| 30 | regions_start += [count_samples_in_file(self.source_file) + 1000] | 31 | 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) | ||
| 32 | 34 | ||
| 33 | def test_slice_start_every_blocksize(self): | 35 | def test_slice_start_every_blocksize(self): |
| 34 | hopsize = 200 | 36 | hopsize = 200 |
| @@ -36,6 +38,12 @@ class aubio_slicing_test_case(TestCase): · 1 unmodified line | |||
| 36 | slice_source_at_stamps(self.source_file, regions_start, output_dir = self.output_dir, | 38 | slice_source_at_stamps(self.source_file, regions_start, output_dir = self.output_dir, |
| 37 | hopsize = 200) | 39 | hopsize = 200) |
| 38 | 40 | ||
| 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 | |||
| 39 | def tearDown(self): | 47 | def tearDown(self): |
| 40 | original_samples = count_samples_in_file(self.source_file) | 48 | original_samples = count_samples_in_file(self.source_file) |
| 41 | written_samples = count_samples_in_directory(self.output_dir) | 49 | written_samples = count_samples_in_directory(self.output_dir) |
| @@ -91,6 +99,19 @@ class aubio_slicing_with_ends_test_case(TestCase): · 49 unmodified lines | |||
| 91 | assert_equal(written_samples, expected_samples, | 99 | assert_equal(written_samples, expected_samples, |
| 92 | "number of samples written different from number of original samples") | 100 | "number of samples written different from number of original samples") |
| 93 | 101 | ||
| 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 | |||
| 94 | def tearDown(self): | 115 | def tearDown(self): |
| 95 | shutil.rmtree(self.output_dir) | 116 | shutil.rmtree(self.output_dir) |
| 96 | 117 | ||
| @@ -133,7 +154,7 @@ class aubio_slicing_wrong_ends_test_case(TestCase): · 36 unmodified lines | |||
| 133 | regions_start = [i*1000 for i in range(1, n_slices)] | 154 | regions_start = [i*1000 for i in range(1, n_slices)] |
| 134 | regions_end = None | 155 | regions_end = None |
| 135 | slice_source_at_stamps (self.source_file, regions_start, regions_end, | 156 | 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) |
| 137 | total_files = count_files_in_directory(self.output_dir) | 158 | total_files = count_files_in_directory(self.output_dir) |
| 138 | assert_equal(n_slices, total_files, | 159 | assert_equal(n_slices, total_files, |
| 139 | "number of slices created different from expected") | 160 | "number of slices created different from expected") |