Changeset 264247e for python/demos


Ignore:
Timestamp:
Mar 8, 2013, 1:41:48 PM (12 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:
98c0f92
Parents:
1ee4033
Message:

demos/demo_slicing.py: slice exactly at region boundary

File:
1 edited

Legend:

Unmodified
Added
Removed
  • python/demos/demo_slicing.py

    r1ee4033 r264247e  
    1111    source_file = sys.argv[1]
    1212    duration = float(sys.argv[2])
    13     sink_base, sink_ext = os.path.splitext(os.path.basename(source_file))
    14     slice_n, total_frames, read = 1, 0, 256
    15     f = source(source_file, 0, 256)
    16     g = sink(sink_base + '-%02d' % slice_n + sink_ext, f.samplerate)
    17     while read:
     13    source_base_name, source_ext = os.path.splitext(os.path.basename(source_file))
     14
     15    hopsize = 256
     16    slice_n, total_frames_written, read = 0, 0, hopsize
     17
     18    def new_sink_name(source_base_name, slice_n, duration = duration):
     19        return source_base_name + '_%02.3f' % (slice_n*duration) + '.wav'
     20
     21    f = source(source_file, 0, hopsize)
     22    samplerate = f.samplerate
     23    g = sink(new_sink_name(source_base_name, slice_n), samplerate)
     24
     25    #print "new slice:", slice_n, 0, "+", 0, "=", 0
     26    while read == hopsize:
    1827        vec, read = f()
    19         g(vec, read)
    20         total_frames += read
    21         if total_frames / float(f.samplerate) >= duration * slice_n:
     28        start_of_next_region = int(duration * samplerate * (slice_n + 1))
     29        remaining = start_of_next_region - total_frames_written
     30        # number of samples remaining is less than what we got
     31        if remaining <= read:
     32            # write remaining samples from current region
     33            g(vec[0:remaining], remaining)
     34            # close this file
     35            del g
     36            #print "new slice", slice_n, total_frames_written, "+", remaining, "=", start_of_next_region
    2237            slice_n += 1
    23             del g
    24             g = sink(sink_base + '-%02d' % slice_n + sink_ext, f.samplerate)
    25     total_duration = total_frames / float(f.samplerate)
    26     print 'created %(slice_n)d slices from %(source_file)s' % locals(),
     38            # create a new file for the new region
     39            g = sink(new_sink_name(source_base_name, slice_n), samplerate)
     40            # write the remaining samples in the new file
     41            g(vec[remaining:read], read - remaining)
     42        else:
     43            g(vec[0:read], read)
     44        total_frames_written += read
     45    total_duration = total_frames_written / float(samplerate)
     46    slice_n += 1
     47    print 'created %(slice_n)s slices from %(source_base_name)s%(source_ext)s' % locals(),
    2748    print ' (total duration %(total_duration).2fs)' % locals()
     49    # close source and sink files
    2850    del f, g
Note: See TracChangeset for help on using the changeset viewer.