Changeset 4320679 for python/lib


Ignore:
Timestamp:
Jan 12, 2014, 7:58:06 AM (10 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:
6fbee46
Parents:
f36ecea
Message:

lib/aubio/slicing.py: allow any regions, overlaping or not, add more tests

File:
1 edited

Legend:

Unmodified
Added
Removed
  • python/lib/aubio/slicing.py

    rf36ecea r4320679  
    1515        timestamps = [0] + timestamps
    1616
    17     if timestamps_end != None and len(timestamps_end) != len(timestamps):
    18         raise ValueError ("len(timestamps_end) != len(timestamps)")
     17    if timestamps_end != None:
     18        if len(timestamps_end) != len(timestamps):
     19            raise ValueError ("len(timestamps_end) != len(timestamps)")
    1920    else:
    2021        timestamps_end = [t - 1 for t in timestamps[1:] ] + [ max_timestamp ]
     22
     23    regions = zip(timestamps, timestamps_end)
     24    #print regions
    2125
    2226    source_base_name, source_ext = os.path.splitext(os.path.basename(source_file))
     
    3337    # reopen source file
    3438    s = source(source_file, samplerate, hopsize)
    35     if samplerate == 0: samplerate = s.get_samplerate()
     39    samplerate = s.get_samplerate()
     40
    3641    total_frames = 0
    37     # get next region
    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)
     42    slices = []
    4543
    4644    while True:
    4745        # get hopsize new samples from source
    4846        vec, read = s()
    49         # number of samples until end of region
    50         remaining = end_stamp - total_frames
    51         # not enough frames remaining, time to split
    52         if remaining < read:
    53             if remaining != 0:
    54                 # write remaining samples from current region
    55                 g(vec[0:remaining], remaining)
    56             # close this file
    57             del g
    58             # get the next region
    59             start_stamp = int(timestamps.pop(0))
    60             end_stamp = int(timestamps_end.pop(0))
    61             # create a new file for the new region
     47        # if the total number of frames read will exceed the next region start
     48        if len(regions) and total_frames + read >= regions[0][0]:
     49            #print "getting", regions[0], "at", total_frames
     50            # get next region
     51            start_stamp, end_stamp = regions.pop(0)
     52            # create a name for the sink
    6253            new_sink_path = new_sink_name(source_base_name, start_stamp, samplerate)
    63             #print "new slice", total_frames, "+", remaining, "=", end_stamp
     54            # create its sink
    6455            g = sink(new_sink_path, samplerate)
    65             # write the remaining samples in the new file
    66             g(vec[remaining:read], read - remaining)
    67         elif read > 0:
    68             # write all the samples
    69             g(vec[0:read], read)
     56            # create a dictionary containing all this
     57            new_slice = {'start_stamp': start_stamp, 'end_stamp': end_stamp, 'sink': g}
     58            # append the dictionary to the current list of slices
     59            slices.append(new_slice)
     60
     61        for current_slice in slices:
     62            start_stamp = current_slice['start_stamp']
     63            end_stamp = current_slice['end_stamp']
     64            g = current_slice['sink']
     65            # sample index to start writing from new source vector
     66            start = max(start_stamp - total_frames, 0)
     67            # number of samples yet to written be until end of region
     68            remaining = end_stamp - total_frames + 1
     69            #print current_slice, remaining, start
     70            # not enough frames remaining, time to split
     71            if remaining < read:
     72                if remaining > start:
     73                    # write remaining samples from current region
     74                    g(vec[start:remaining], remaining - start)
     75                    #print "closing region", "remaining", remaining
     76                    # close this file
     77                    del g
     78            elif read > start:
     79                # write all the samples
     80                g(vec[start:read], read - start)
    7081        total_frames += read
    7182        if read < hopsize: break
Note: See TracChangeset for help on using the changeset viewer.