Changeset f36ecea for python


Ignore:
Timestamp:
Jan 12, 2014, 5:54:42 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:
4320679
Parents:
d945976
Message:

python/lib/aubio/slicing.py: use start and end stamps, make sure read > 0, improve tests

Location:
python
Files:
3 edited

Legend:

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

    rd945976 rf36ecea  
    11from aubio import source, sink
    22import os
     3
     4max_timestamp = 1e120
    35
    46def slice_source_at_stamps(source_file, timestamps, timestamps_end = None,
     
    1012        raise ValueError ("no timestamps given")
    1113
     14    if timestamps[0] != 0:
     15        timestamps = [0] + timestamps
     16
    1217    if timestamps_end != None and len(timestamps_end) != len(timestamps):
    1318        raise ValueError ("len(timestamps_end) != len(timestamps)")
     19    else:
     20        timestamps_end = [t - 1 for t in timestamps[1:] ] + [ max_timestamp ]
    1421
    1522    source_base_name, source_ext = os.path.splitext(os.path.basename(source_file))
     
    1926        source_base_name = os.path.join(output_dir, source_base_name)
    2027
    21     def new_sink_name(source_base_name, timestamp):
    22         return source_base_name + '_%02.3f' % (timestamp) + '.wav'
     28    def new_sink_name(source_base_name, timestamp, samplerate):
     29        timestamp_seconds = timestamp / float(samplerate)
     30        #print source_base_name + '_%02.3f' % (timestamp_seconds) + '.wav'
     31        return source_base_name + '_%02.3f' % (timestamp_seconds) + '.wav'
    2332
    2433    # reopen source file
    2534    s = source(source_file, samplerate, hopsize)
    2635    if samplerate == 0: samplerate = s.get_samplerate()
    27     # create first sink at 0
    28     g = sink(new_sink_name(source_base_name, 0.), samplerate)
    2936    total_frames = 0
    3037    # get next region
    31     next_stamp = int(timestamps.pop(0))
     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)
    3245
    3346    while True:
    3447        # get hopsize new samples from source
    3548        vec, read = s()
    36         remaining = next_stamp - total_frames
     49        # number of samples until end of region
     50        remaining = end_stamp - total_frames
    3751        # not enough frames remaining, time to split
    3852        if remaining < read:
     
    4256            # close this file
    4357            del g
     58            # get the next region
     59            start_stamp = int(timestamps.pop(0))
     60            end_stamp = int(timestamps_end.pop(0))
    4461            # create a new file for the new region
    45             new_sink_path = new_sink_name(source_base_name, next_stamp / float(samplerate))
    46             #print "new slice", total_frames, "+", remaining, "=", next_stamp
     62            new_sink_path = new_sink_name(source_base_name, start_stamp, samplerate)
     63            #print "new slice", total_frames, "+", remaining, "=", end_stamp
    4764            g = sink(new_sink_path, samplerate)
    4865            # write the remaining samples in the new file
    4966            g(vec[remaining:read], read - remaining)
    50             if len(timestamps):
    51                 next_stamp = int(timestamps.pop(0))
    52             else:
    53                 next_stamp = 1e120
    54         else:
     67        elif read > 0:
     68            # write all the samples
    5569            g(vec[0:read], read)
    5670        total_frames += read
  • python/tests/test_slicing.py

    rd945976 rf36ecea  
    55
    66from aubio import slice_source_at_stamps
    7 from utils import count_samples_in_file, count_samples_in_directory
    8 from utils import get_default_test_sound
     7from utils import *
    98
    109import tempfile
     
    2928    def test_slice_start_beyond_end(self):
    3029        regions_start = [i*1000 for i in range(1, n_slices)]
    31         regions_start += [count_samples_in_file(self.source_file)]
    3230        regions_start += [count_samples_in_file(self.source_file) + 1000]
    3331        slice_source_at_stamps(self.source_file, regions_start, output_dir = self.output_dir)
     32
     33    def test_slice_start_every_blocksize(self):
     34        hopsize = 200
     35        regions_start = [i*hopsize for i in range(1, n_slices)]
     36        regions_start += [count_samples_in_file(self.source_file) + 1000]
     37        slice_source_at_stamps(self.source_file, regions_start, output_dir = self.output_dir,
     38                hopsize = 200)
    3439
    3540    def tearDown(self):
    3641        original_samples = count_samples_in_file(self.source_file)
    3742        written_samples = count_samples_in_directory(self.output_dir)
     43        total_files = count_files_in_directory(self.output_dir)
     44        assert_equal(n_slices, total_files,
     45            "number of slices created different from expected")
    3846        assert_equal(written_samples, original_samples,
    3947            "number of samples written different from number of original samples")
     
    6876
    6977    def test_slice_wrong_ends(self):
    70         regions_start = [i*1000 for i in range(1, 100)]
     78        regions_start = [i*1000 for i in range(1, n_slices)]
    7179        regions_end = []
    7280        self.assertRaises (ValueError,
     
    7583
    7684    def test_slice_no_ends(self):
    77         regions_start = [i*1000 for i in range(1, 100)]
     85        regions_start = [i*1000 for i in range(1, n_slices)]
    7886        regions_end = None
    7987        slice_source_at_stamps (self.source_file, regions_start, regions_end,
  • python/tests/utils.py

    rd945976 rf36ecea  
    4848                    total_frames += count_samples_in_file(file_path)
    4949    return total_frames
     50
     51def count_files_in_directory(samples_dir):
     52    import os
     53    total_files = 0
     54    for f in os.walk(samples_dir):
     55        if len(f[2]):
     56            for each in f[2]:
     57                file_path = os.path.join(f[0], each)
     58                if file_path:
     59                    total_files += 1
     60    return total_files
Note: See TracChangeset for help on using the changeset viewer.