Changeset 8b56b18


Ignore:
Timestamp:
May 10, 2016, 10:42:55 PM (8 years ago)
Author:
Paul Brossier <piem@piem.org>
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
Message:

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

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
     3import os
    14from aubio import source, sink
    2 import os
    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):
     8def 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:
     
    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):
     
    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
     
    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]:
     
    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)
     
    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)
     
    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
Note: See TracChangeset for help on using the changeset viewer.