History

python/lib/aubio/slicing.py: clean up

Paul Brossier committed 10 years ago

Commit 8b56b181 parent f6892d41 file · +25 −22Browse files

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

Unified · Split

python/lib/aubio/slicing.py+25−22
@@ -1,15 +1,16 @@
1−from aubio import source, sink
1+"""utility routines to slice sound files at given timestamps"""
2+
23 import os
4+from aubio import source, sink
35
4−max_timestamp = 1e120
6+_max_timestamp = 1e120
57
6−def slice_source_at_stamps(source_file, timestamps, timestamps_end = None,
7− output_dir = None,
8− samplerate = 0,
9− hopsize = 256):
8+def slice_source_at_stamps(source_file, timestamps, timestamps_end=None,
9+ output_dir=None, samplerate=0, hopsize=256):
10+ """ slice a sound file at given timestamps """
1011
11− if timestamps == None or len(timestamps) == 0:
12− raise ValueError ("no timestamps given")
12+ if timestamps is None or len(timestamps) == 0:
13+ raise ValueError("no timestamps given")
1314
1415 if timestamps[0] != 0:
1516 timestamps = [0] + timestamps
@@ -18,33 +19,34 @@ def slice_source_at_stamps(source_file, timestamps, timestamps_end = None, · 2 unmodified lines
1819
1920 if timestamps_end != None:
2021 if len(timestamps_end) != len(timestamps):
21− raise ValueError ("len(timestamps_end) != len(timestamps)")
22+ raise ValueError("len(timestamps_end) != len(timestamps)")
2223 else:
23− timestamps_end = [t - 1 for t in timestamps[1:] ] + [ max_timestamp ]
24+ timestamps_end = [t - 1 for t in timestamps[1:]] + [_max_timestamp]
2425
2526 regions = list(zip(timestamps, timestamps_end))
2627 #print regions
2728
28− source_base_name, source_ext = os.path.splitext(os.path.basename(source_file))
29+ source_base_name, _ = os.path.splitext(os.path.basename(source_file))
2930 if output_dir != None:
3031 if not os.path.isdir(output_dir):
3132 os.makedirs(output_dir)
3233 source_base_name = os.path.join(output_dir, source_base_name)
3334
3435 def new_sink_name(source_base_name, timestamp, samplerate):
36+ """ create a sink based on a timestamp in samples, converted in seconds """
3537 timestamp_seconds = timestamp / float(samplerate)
3638 return source_base_name + "_%011.6f" % timestamp_seconds + '.wav'
3739
38− # reopen source file
39− s = source(source_file, samplerate, hopsize)
40− samplerate = s.get_samplerate()
40+ # open source file
41+ _source = source(source_file, samplerate, hopsize)
42+ samplerate = source.get_samplerate()
4143
4244 total_frames = 0
4345 slices = []
4446
4547 while True:
4648 # get hopsize new samples from source
47− vec, read = s.do_multi()
49+ vec, read = _source.do_multi()
4850 # if the total number of frames read will exceed the next region start
4951 if len(regions) and total_frames + read >= regions[0][0]:
5052 #print "getting", regions[0], "at", total_frames
@@ -53,16 +55,16 @@ def slice_source_at_stamps(source_file, timestamps, timestamps_end = None, · 2 unmodified lines
5355 # create a name for the sink
5456 new_sink_path = new_sink_name(source_base_name, start_stamp, samplerate)
5557 # create its sink
56− g = sink(new_sink_path, samplerate, s.channels)
58+ _sink = sink(new_sink_path, samplerate, _source.channels)
5759 # create a dictionary containing all this
58− new_slice = {'start_stamp': start_stamp, 'end_stamp': end_stamp, 'sink': g}
60+ new_slice = {'start_stamp': start_stamp, 'end_stamp': end_stamp, 'sink': _sink}
5961 # append the dictionary to the current list of slices
6062 slices.append(new_slice)
6163
6264 for current_slice in slices:
6365 start_stamp = current_slice['start_stamp']
6466 end_stamp = current_slice['end_stamp']
65− g = current_slice['sink']
67+ _sink = current_slice['sink']
6668 # sample index to start writing from new source vector
6769 start = max(start_stamp - total_frames, 0)
6870 # number of samples yet to written be until end of region
@@ -72,12 +74,13 @@ def slice_source_at_stamps(source_file, timestamps, timestamps_end = None, · 3 unmodified lines
7274 if remaining < read:
7375 if remaining > start:
7476 # write remaining samples from current region
75− g.do_multi(vec[:,start:remaining], remaining - start)
77+ _sink.do_multi(vec[:, start:remaining], remaining - start)
7678 #print "closing region", "remaining", remaining
7779 # close this file
78− g.close()
80+ _sink.close()
7981 elif read > start:
8082 # write all the samples
81− g.do_multi(vec[:,start:read], read - start)
83+ _sink.do_multi(vec[:, start:read], read - start)
8284 total_frames += read
83− if read < hopsize: break
85+ if read < hopsize:
86+ 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