Changeset 8b56b18 for python/lib/aubio
- Timestamp:
- May 10, 2016, 10:42:55 PM (9 years ago)
- Branches:
- feature/autosink, feature/cnn, feature/cnn_org, feature/constantq, feature/crepe, feature/crepe_org, feature/pitchshift, feature/pydocstrings, feature/timestretch, fix/ffmpeg5, master, pitchshift, sampler, timestretch, yinfft+
- Children:
- 744190f
- Parents:
- f6892d4
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
python/lib/aubio/slicing.py
rf6892d4 r8b56b18 1 """utility routines to slice sound files at given timestamps""" 2 3 import os 1 4 from aubio import source, sink 2 import os3 5 4 max_timestamp = 1e1206 _max_timestamp = 1e120 5 7 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 """ 10 11 11 if timestamps ==None or len(timestamps) == 0:12 raise ValueError 12 if timestamps is None or len(timestamps) == 0: 13 raise ValueError("no timestamps given") 13 14 14 15 if timestamps[0] != 0: … … 19 20 if timestamps_end != None: 20 21 if len(timestamps_end) != len(timestamps): 21 raise ValueError 22 raise ValueError("len(timestamps_end) != len(timestamps)") 22 23 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] 24 25 25 26 regions = list(zip(timestamps, timestamps_end)) 26 27 #print regions 27 28 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)) 29 30 if output_dir != None: 30 31 if not os.path.isdir(output_dir): … … 33 34 34 35 def new_sink_name(source_base_name, timestamp, samplerate): 36 """ create a sink based on a timestamp in samples, converted in seconds """ 35 37 timestamp_seconds = timestamp / float(samplerate) 36 38 return source_base_name + "_%011.6f" % timestamp_seconds + '.wav' 37 39 38 # reopen source file39 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() 41 43 42 44 total_frames = 0 … … 45 47 while True: 46 48 # get hopsize new samples from source 47 vec, read = s.do_multi()49 vec, read = _source.do_multi() 48 50 # if the total number of frames read will exceed the next region start 49 51 if len(regions) and total_frames + read >= regions[0][0]: … … 54 56 new_sink_path = new_sink_name(source_base_name, start_stamp, samplerate) 55 57 # create its sink 56 g = sink(new_sink_path, samplerate, s.channels)58 _sink = sink(new_sink_path, samplerate, _source.channels) 57 59 # 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} 59 61 # append the dictionary to the current list of slices 60 62 slices.append(new_slice) … … 63 65 start_stamp = current_slice['start_stamp'] 64 66 end_stamp = current_slice['end_stamp'] 65 g= current_slice['sink']67 _sink = current_slice['sink'] 66 68 # sample index to start writing from new source vector 67 69 start = max(start_stamp - total_frames, 0) … … 73 75 if remaining > start: 74 76 # write remaining samples from current region 75 g.do_multi(vec[:,start:remaining], remaining - start)77 _sink.do_multi(vec[:, start:remaining], remaining - start) 76 78 #print "closing region", "remaining", remaining 77 79 # close this file 78 g.close()80 _sink.close() 79 81 elif read > start: 80 82 # write all the samples 81 g.do_multi(vec[:,start:read], read - start)83 _sink.do_multi(vec[:, start:read], read - start) 82 84 total_frames += read 83 if read < hopsize: break 85 if read < hopsize: 86 break
Note: See TracChangeset
for help on using the changeset viewer.