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/lib (1 of 3 files). Show all

Unified · Split

python/lib/aubio/slicing.py+27−13
@@ -1,6 +1,8 @@
11 from aubio import source, sink
22 import os
33
4+max_timestamp = 1e120
5+
46 def slice_source_at_stamps(source_file, timestamps, timestamps_end = None,
57 output_dir = None,
68 samplerate = 0,
@@ -9,8 +11,13 @@ def slice_source_at_stamps(source_file, timestamps, timestamps_end = None, · 2 unmodified lines
911 if timestamps == None or len(timestamps) == 0:
1012 raise ValueError ("no timestamps given")
1113
14+ if timestamps[0] != 0:
15+ timestamps = [0] + timestamps
16+
1217 if timestamps_end != None and len(timestamps_end) != len(timestamps):
1318 raise ValueError ("len(timestamps_end) != len(timestamps)")
19+ else:
20+ timestamps_end = [t - 1 for t in timestamps[1:] ] + [ max_timestamp ]
1421
1522 source_base_name, source_ext = os.path.splitext(os.path.basename(source_file))
1623 if output_dir != None:
@@ -18,22 +25,29 @@ def slice_source_at_stamps(source_file, timestamps, timestamps_end = None, · 1 unmodified line
1825 os.makedirs(output_dir)
1926 source_base_name = os.path.join(output_dir, source_base_name)
2027
21− def new_sink_name(source_base_name, timestamp):
22− return source_base_name + '_%02.3f' % (timestamp) + '.wav'
28+ def new_sink_name(source_base_name, timestamp, samplerate):
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'
2332
2433 # reopen source file
2534 s = source(source_file, samplerate, hopsize)
2635 if samplerate == 0: samplerate = s.get_samplerate()
27− # create first sink at 0
28− g = sink(new_sink_name(source_base_name, 0.), samplerate)
2936 total_frames = 0
3037 # 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)
3245
3346 while True:
3447 # get hopsize new samples from source
3548 vec, read = s()
36− remaining = next_stamp - total_frames
49+ # number of samples until end of region
50+ remaining = end_stamp - total_frames
3751 # not enough frames remaining, time to split
3852 if remaining < read:
3953 if remaining != 0:
@@ -41,17 +55,17 @@ def slice_source_at_stamps(source_file, timestamps, timestamps_end = None, · 1 unmodified line
4155 g(vec[0:remaining], remaining)
4256 # close this file
4357 del g
58+ # get the next region
59+ start_stamp = int(timestamps.pop(0))
60+ end_stamp = int(timestamps_end.pop(0))
4461 # create a new file for the new region
45− new_sink_path = new_sink_name(source_base_name, next_stamp / float(samplerate))
46− #print "new slice", total_frames, "+", remaining, "=", next_stamp
62+ new_sink_path = new_sink_name(source_base_name, start_stamp, samplerate)
63+ #print "new slice", total_frames, "+", remaining, "=", end_stamp
4764 g = sink(new_sink_path, samplerate)
4865 # write the remaining samples in the new file
4966 g(vec[remaining:read], read - remaining)
50− if len(timestamps):
51− next_stamp = int(timestamps.pop(0))
52− else:
53− next_stamp = 1e120
54− else:
67+ elif read > 0:
68+ # write all the samples
5569 g(vec[0:read], read)
5670 total_frames += read
5771 if read < hopsize: 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