History

python/lib/aubio/slicing.py: use start and end stamps, make sure read > 0, improve tests

Paul Brossier committed 12 years ago

Commit f36ecea1 parent d9459763 files · +51 −18Browse files

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

Unified · Split

3 files
  1. python/lib/aubio/slicing.py +27 −13
  2. python/tests/test_slicing.py +13 −5
  3. python/tests/utils.py +11 −0
python/lib/aubio/slicing.py+27−13
@@ -1,6 +1,8 @@
1from aubio import source, sink1from aubio import source, sink
2import os2import os
3 3
4max_timestamp = 1e120
5
4def slice_source_at_stamps(source_file, timestamps, timestamps_end = None,6def slice_source_at_stamps(source_file, timestamps, timestamps_end = None,
5 output_dir = None,7 output_dir = None,
6 samplerate = 0,8 samplerate = 0,
@@ -9,8 +11,13 @@ def slice_source_at_stamps(source_file, timestamps, timestamps_end = None, · 2 unmodified lines
9 if timestamps == None or len(timestamps) == 0:11 if timestamps == None or len(timestamps) == 0:
10 raise ValueError ("no timestamps given")12 raise ValueError ("no timestamps given")
11 13
14 if timestamps[0] != 0:
15 timestamps = [0] + timestamps
16
12 if timestamps_end != None and len(timestamps_end) != len(timestamps):17 if timestamps_end != None and len(timestamps_end) != len(timestamps):
13 raise ValueError ("len(timestamps_end) != len(timestamps)")18 raise ValueError ("len(timestamps_end) != len(timestamps)")
19 else:
20 timestamps_end = [t - 1 for t in timestamps[1:] ] + [ max_timestamp ]
14 21
15 source_base_name, source_ext = os.path.splitext(os.path.basename(source_file))22 source_base_name, source_ext = os.path.splitext(os.path.basename(source_file))
16 if output_dir != None:23 if output_dir != None:
@@ -18,22 +25,29 @@ def slice_source_at_stamps(source_file, timestamps, timestamps_end = None, · 1 unmodified line
18 os.makedirs(output_dir)25 os.makedirs(output_dir)
19 source_base_name = os.path.join(output_dir, source_base_name)26 source_base_name = os.path.join(output_dir, source_base_name)
20 27
21 def new_sink_name(source_base_name, timestamp):28 def new_sink_name(source_base_name, timestamp, samplerate):
22 return source_base_name + '_%02.3f' % (timestamp) + '.wav'29 timestamp_seconds = timestamp / float(samplerate)
30 #print source_base_name + '_%02.3f' % (timestamp_seconds) + '.wav'
31 return source_base_name + '_%02.3f' % (timestamp_seconds) + '.wav'
23 32
24 # reopen source file33 # reopen source file
25 s = source(source_file, samplerate, hopsize)34 s = source(source_file, samplerate, hopsize)
26 if samplerate == 0: samplerate = s.get_samplerate()35 if samplerate == 0: samplerate = s.get_samplerate()
27 # create first sink at 0
28 g = sink(new_sink_name(source_base_name, 0.), samplerate)
29 total_frames = 036 total_frames = 0
30 # get next region37 # get next region
31 next_stamp = int(timestamps.pop(0))38 start_stamp = int(timestamps.pop(0))
39 end_stamp = int(timestamps_end.pop(0))
40
41 # create first sink
42 new_sink_path = new_sink_name(source_base_name, start_stamp, samplerate)
43 #print "new slice", total_frames, "+", remaining, "=", end_stamp
44 g = sink(new_sink_path, samplerate)
32 45
33 while True:46 while True:
34 # get hopsize new samples from source47 # get hopsize new samples from source
35 vec, read = s()48 vec, read = s()
36 remaining = next_stamp - total_frames49 # number of samples until end of region
50 remaining = end_stamp - total_frames
37 # not enough frames remaining, time to split51 # not enough frames remaining, time to split
38 if remaining < read:52 if remaining < read:
39 if remaining != 0:53 if remaining != 0:
@@ -41,17 +55,17 @@ def slice_source_at_stamps(source_file, timestamps, timestamps_end = None, · 1 unmodified line
41 g(vec[0:remaining], remaining)55 g(vec[0:remaining], remaining)
42 # close this file56 # close this file
43 del g57 del g
58 # get the next region
59 start_stamp = int(timestamps.pop(0))
60 end_stamp = int(timestamps_end.pop(0))
44 # create a new file for the new region61 # create a new file for the new region
45 new_sink_path = new_sink_name(source_base_name, next_stamp / float(samplerate))62 new_sink_path = new_sink_name(source_base_name, start_stamp, samplerate)
46 #print "new slice", total_frames, "+", remaining, "=", next_stamp63 #print "new slice", total_frames, "+", remaining, "=", end_stamp
47 g = sink(new_sink_path, samplerate)64 g = sink(new_sink_path, samplerate)
48 # write the remaining samples in the new file65 # write the remaining samples in the new file
49 g(vec[remaining:read], read - remaining)66 g(vec[remaining:read], read - remaining)
50 if len(timestamps):67 elif read > 0:
51 next_stamp = int(timestamps.pop(0))68 # write all the samples
52 else:
53 next_stamp = 1e120
54 else:
55 g(vec[0:read], read)69 g(vec[0:read], read)
56 total_frames += read70 total_frames += read
57 if read < hopsize: break71 if read < hopsize: break
python/tests/test_slicing.py+13−5
@@ -4,8 +4,7 @@ from numpy.testing import TestCase, run_module_suite · 3 unmodified lines
4from numpy.testing import assert_equal, assert_almost_equal4from numpy.testing import assert_equal, assert_almost_equal
5 5
6from aubio import slice_source_at_stamps6from aubio import slice_source_at_stamps
7from utils import count_samples_in_file, count_samples_in_directory7from utils import *
8from utils import get_default_test_sound
9 8
10import tempfile9import tempfile
11import shutil10import shutil
@@ -28,13 +27,22 @@ class aubio_slicing_test_case(TestCase): · 16 unmodified lines
28 27
29 def test_slice_start_beyond_end(self):28 def test_slice_start_beyond_end(self):
30 regions_start = [i*1000 for i in range(1, n_slices)]29 regions_start = [i*1000 for i in range(1, n_slices)]
31 regions_start += [count_samples_in_file(self.source_file)]
32 regions_start += [count_samples_in_file(self.source_file) + 1000]30 regions_start += [count_samples_in_file(self.source_file) + 1000]
33 slice_source_at_stamps(self.source_file, regions_start, output_dir = self.output_dir)31 slice_source_at_stamps(self.source_file, regions_start, output_dir = self.output_dir)
34 32
33 def test_slice_start_every_blocksize(self):
34 hopsize = 200
35 regions_start = [i*hopsize for i in range(1, n_slices)]
36 regions_start += [count_samples_in_file(self.source_file) + 1000]
37 slice_source_at_stamps(self.source_file, regions_start, output_dir = self.output_dir,
38 hopsize = 200)
39
35 def tearDown(self):40 def tearDown(self):
36 original_samples = count_samples_in_file(self.source_file)41 original_samples = count_samples_in_file(self.source_file)
37 written_samples = count_samples_in_directory(self.output_dir)42 written_samples = count_samples_in_directory(self.output_dir)
43 total_files = count_files_in_directory(self.output_dir)
44 assert_equal(n_slices, total_files,
45 "number of slices created different from expected")
38 assert_equal(written_samples, original_samples,46 assert_equal(written_samples, original_samples,
39 "number of samples written different from number of original samples")47 "number of samples written different from number of original samples")
40 shutil.rmtree(self.output_dir)48 shutil.rmtree(self.output_dir)
@@ -67,14 +75,14 @@ class aubio_slicing_wrong_ends_test_case(TestCase): · 26 unmodified lines
67 self.output_dir = tempfile.mkdtemp(suffix = 'aubio_slicing_test_case')75 self.output_dir = tempfile.mkdtemp(suffix = 'aubio_slicing_test_case')
68 76
69 def test_slice_wrong_ends(self):77 def test_slice_wrong_ends(self):
70 regions_start = [i*1000 for i in range(1, 100)]78 regions_start = [i*1000 for i in range(1, n_slices)]
71 regions_end = []79 regions_end = []
72 self.assertRaises (ValueError,80 self.assertRaises (ValueError,
73 slice_source_at_stamps, self.source_file, regions_start, regions_end,81 slice_source_at_stamps, self.source_file, regions_start, regions_end,
74 output_dir = self.output_dir)82 output_dir = self.output_dir)
75 83
76 def test_slice_no_ends(self):84 def test_slice_no_ends(self):
77 regions_start = [i*1000 for i in range(1, 100)]85 regions_start = [i*1000 for i in range(1, n_slices)]
78 regions_end = None86 regions_end = None
79 slice_source_at_stamps (self.source_file, regions_start, regions_end,87 slice_source_at_stamps (self.source_file, regions_start, regions_end,
80 output_dir = self.output_dir)88 output_dir = self.output_dir)
python/tests/utils.py+11−0
@@ -47,3 +47,14 @@ def count_samples_in_directory(samples_dir): · 46 unmodified lines
47 if file_path:47 if file_path:
48 total_frames += count_samples_in_file(file_path)48 total_frames += count_samples_in_file(file_path)
49 return total_frames49 return total_frames
50
51def count_files_in_directory(samples_dir):
52 import os
53 total_files = 0
54 for f in os.walk(samples_dir):
55 if len(f[2]):
56 for each in f[2]:
57 file_path = os.path.join(f[0], each)
58 if file_path:
59 total_files += 1
60 return total_files

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