Changeset 4320679


Ignore:
Timestamp:
Jan 12, 2014, 7:58:06 AM (11 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

Location:
python
Files:
2 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
  • python/tests/test_slicing.py

    rf36ecea r4320679  
    1010import shutil
    1111
    12 n_slices = 8
     12n_slices = 4
    1313
    1414class aubio_slicing_test_case(TestCase):
     
    3434        hopsize = 200
    3535        regions_start = [i*hopsize for i in range(1, n_slices)]
    36         regions_start += [count_samples_in_file(self.source_file) + 1000]
    3736        slice_source_at_stamps(self.source_file, regions_start, output_dir = self.output_dir,
    3837                hopsize = 200)
     
    4746            "number of samples written different from number of original samples")
    4847        shutil.rmtree(self.output_dir)
     48
     49class aubio_slicing_with_ends_test_case(TestCase):
     50
     51    def setUp(self):
     52        self.source_file = get_default_test_sound(self)
     53        self.output_dir = tempfile.mkdtemp(suffix = 'aubio_slicing_test_case')
     54
     55    def test_slice_start_and_ends_no_gap(self):
     56        regions_start = [i*1000 for i in range(n_slices)]
     57        regions_ends = [start - 1 for start in regions_start[1:]] + [1e120]
     58        slice_source_at_stamps(self.source_file, regions_start, regions_ends,
     59                output_dir = self.output_dir)
     60        original_samples = count_samples_in_file(self.source_file)
     61        written_samples = count_samples_in_directory(self.output_dir)
     62        total_files = count_files_in_directory(self.output_dir)
     63        assert_equal(n_slices, total_files,
     64            "number of slices created different from expected")
     65        assert_equal(written_samples, original_samples,
     66            "number of samples written different from number of original samples")
     67
     68    def test_slice_start_and_ends_200_gap(self):
     69        regions_start = [i*1000 for i in range(n_slices)]
     70        regions_ends = [start + 199 for start in regions_start]
     71        slice_source_at_stamps(self.source_file, regions_start, regions_ends,
     72                output_dir = self.output_dir)
     73        expected_samples = 200 * n_slices
     74        written_samples = count_samples_in_directory(self.output_dir)
     75        total_files = count_files_in_directory(self.output_dir)
     76        assert_equal(n_slices, total_files,
     77            "number of slices created different from expected")
     78        assert_equal(written_samples, expected_samples,
     79            "number of samples written different from number of original samples")
     80
     81    def test_slice_start_and_ends_overlaping(self):
     82        regions_start = [i*1000 for i in range(n_slices)]
     83        regions_ends = [start + 1199 for start in regions_start]
     84        slice_source_at_stamps(self.source_file, regions_start, regions_ends,
     85                output_dir = self.output_dir)
     86        expected_samples = 1200 * n_slices
     87        written_samples = count_samples_in_directory(self.output_dir)
     88        total_files = count_files_in_directory(self.output_dir)
     89        assert_equal(n_slices, total_files,
     90            "number of slices created different from expected")
     91        assert_equal(written_samples, expected_samples,
     92            "number of samples written different from number of original samples")
     93
     94    def tearDown(self):
     95        shutil.rmtree(self.output_dir)
     96
    4997
    5098class aubio_slicing_wrong_starts_test_case(TestCase):
     
    87135        slice_source_at_stamps (self.source_file, regions_start, regions_end,
    88136                output_dir = self.output_dir)
     137        total_files = count_files_in_directory(self.output_dir)
     138        assert_equal(n_slices, total_files,
     139            "number of slices created different from expected")
    89140        original_samples = count_samples_in_file(self.source_file)
    90141        written_samples = count_samples_in_directory(self.output_dir)
Note: See TracChangeset for help on using the changeset viewer.