Ignore:
Timestamp:
Apr 9, 2013, 6:09:46 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:
8b884ef
Parents:
69440b8
Message:

python/scripts/aubiocut: add slicing

File:
1 edited

Legend:

Unmodified
Added
Removed
  • python/scripts/aubiocut

    r69440b8 rdee4164  
    99
    1010usage = "usage: %s [options] -i soundfile" % sys.argv[0]
     11usage += "\nhelp: %s -h" % sys.argv[0]
    1112
    1213def parse_args():
     
    134135    o.set_threshold(options.threshold)
    135136
    136     slice_number = 0
    137     #if options.cut:
    138     this_slice = sink('/tmp/t-%02d.wav' % slice_number, samplerate)
    139 
    140137    timestamps = []
    141     block_read = 0
     138    total_frames = 0
     139    # analyze pass
    142140    while True:
    143141        samples, read = s()
    144         #ring_buffer = hstack([ring_buffer, samples])
    145         is_onset = o(samples)
    146         if is_onset:
    147             this_onset = (block_read - 4. + is_onset[0]) * hopsize
    148             if options.verbose:
    149                 print "%.4f" % ( this_onset / samplerate )
     142        if o(samples):
     143            this_onset = o.get_last_onset()
     144            if options.verbose: print "%.4f" % o.get_last_onset_s()
    150145            timestamps.append (this_onset)
    151             del this_slice
    152             slice_number += 1
    153             this_slice = sink('/tmp/t-%02d.wav' % slice_number, samplerate)
    154         this_slice(samples, read)
    155         block_read += 1
     146        total_frames += read
    156147        if read < hopsize: break
    157148
    158149    # print some info
    159     duration = float ( block_read * hopsize + read ) / samplerate
    160150    nstamps = len(timestamps)
     151    duration = float (total_frames) / float(samplerate)
    161152    info = 'found %(nstamps)d timestamps in %(source_file)s' % locals()
    162     info += ' (read %(duration).2fs at %(samplerate)dHz)\n' % locals()
     153    info += ' (total %(duration).2fs at %(samplerate)dHz)\n' % locals()
    163154    sys.stderr.write(info)
     155
     156    # cutting pass
     157    if options.cut and nstamps > 0:
     158        # generate output filenames
     159        import os
     160        source_base_name, source_ext = os.path.splitext(os.path.basename(source_file))
     161        def new_sink_name(source_base_name, timestamp):
     162            return source_base_name + '_%02.3f' % (timestamp) + '.wav'
     163        # reopen source file
     164        del s
     165        s = source(source_file, samplerate, hopsize)
     166        # create first sink at 0
     167        g = sink(new_sink_name(source_base_name, 0.), samplerate)
     168        total_frames = 0
     169        # get next region
     170        next_onset = int(timestamps.pop(0))
     171        while True:
     172            vec, read = s()
     173            remaining = next_onset - total_frames
     174            if remaining <= read:
     175                # write remaining samples from current region
     176                g(vec[0:remaining], remaining)
     177                # close this file
     178                del g
     179                # create a new file for the new region
     180                g = sink(new_sink_name(source_base_name, next_onset / float(samplerate)), samplerate)
     181                # write the remaining samples in the new file
     182                g(vec[remaining:read], read - remaining)
     183                #print "new slice", total_frames_written, "+", remaining, "=", start_of_next_region
     184                if len(timestamps):
     185                    next_onset = int(timestamps.pop(0))
     186                else:
     187                    next_onset = 1e120
     188            else:
     189                g(vec[0:read], read)
     190            total_frames += read
     191            if read < hopsize: break
     192
     193        # print some info
     194        duration = float (total_frames) / float(samplerate)
     195        info = 'created %(nstamps)d slices from %(source_file)s' % locals()
     196        info += ' (total %(duration).2fs at %(samplerate)dHz)\n' % locals()
     197        sys.stderr.write(info)
Note: See TracChangeset for help on using the changeset viewer.