Changeset b02d52f for python


Ignore:
Timestamp:
Sep 16, 2017, 12:36:42 PM (7 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
Children:
c95062b
Parents:
9a52962
Message:

python/lib/aubio/cut.py: clean-up, split in functions

File:
1 edited

Legend:

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

    r9a52962 rb02d52f  
    88import argparse
    99
    10 def parse_args():
     10def aubio_cut_parser():
    1111    usage = "usage: %s [options] -i soundfile" % sys.argv[0]
    1212    usage += "\n help: %s -h" % sys.argv[0]
     
    133133            action="store_false", dest="verbose", default=True,
    134134            help="be quiet")
    135     args = parser.parse_args()
    136     if not args.source_file and not args.source_file2:
    137         sys.stderr.write("Error: no file name given\n")
    138         parser.print_help()
    139         sys.exit(1)
    140     elif args.source_file2 is not None:
    141         args.source_file = args.source_file2
    142     return args
    143 
    144 def main():
    145     options = parse_args()
    146 
     135    return parser
     136
     137
     138def _cut_analyze(options):
    147139    source_file = options.source_file
    148140    hopsize = options.hopsize
     
    151143    source_file = options.source_file
    152144
     145    # analyze pass
    153146    from aubio import onset, tempo, source
    154147
    155148    s = source(source_file, samplerate, hopsize)
    156     if samplerate == 0: samplerate = s.get_samplerate()
     149    if samplerate == 0:
     150        samplerate = s.get_samplerate()
     151        options.samplerate = samplerate
    157152
    158153    if options.beat:
     
    171166    timestamps = []
    172167    total_frames = 0
    173     # analyze pass
    174168    while True:
    175169        samples, read = s()
     
    180174        if read < hopsize: break
    181175    del s
    182     # print some info
     176    return timestamps, total_frames
     177
     178def _cut_slice(options, timestamps):
     179    # cutting pass
    183180    nstamps = len(timestamps)
    184     duration = float (total_frames) / float(samplerate)
    185     info = 'found %(nstamps)d timestamps in %(source_file)s' % locals()
    186     info += ' (total %(duration).2fs at %(samplerate)dHz)\n' % locals()
    187     sys.stderr.write(info)
    188 
    189     # cutting pass
    190     if options.cut and nstamps > 0:
     181    if nstamps > 0:
    191182        # generate output files
    192183        from aubio.slicing import slice_source_at_stamps
     
    203194            timestamps_end = [t for t in timestamps[1 + options.cut_until_nslices:]]
    204195            timestamps_end += [ 1e120 ] * (options.cut_until_nslices + 1)
    205         slice_source_at_stamps(source_file, timestamps, timestamps_end = timestamps_end,
     196        slice_source_at_stamps(options.source_file,
     197                timestamps, timestamps_end = timestamps_end,
    206198                output_dir = options.output_directory,
    207                 samplerate = samplerate)
    208 
    209         # print some info
    210         duration = float (total_frames) / float(samplerate)
    211         info = 'created %(nstamps)d slices from %(source_file)s' % locals()
    212         info += ' (total %(duration).2fs at %(samplerate)dHz)\n' % locals()
     199                samplerate = options.samplerate)
     200
     201def main():
     202    parser = aubio_cut_parser()
     203    options = parser.parse_args()
     204    if not options.source_file and not options.source_file2:
     205        sys.stderr.write("Error: no file name given\n")
     206        parser.print_help()
     207        sys.exit(1)
     208    elif options.source_file2 is not None:
     209        options.source_file = options.source_file2
     210
     211    # analysis
     212    timestamps, total_frames = _cut_analyze(options)
     213
     214    # print some info
     215    duration = float (total_frames) / float(options.samplerate)
     216    base_info = '%(source_file)s' % {'source_file': options.source_file}
     217    base_info += ' (total %(duration).2fs at %(samplerate)dHz)\n' % \
     218            {'duration': duration, 'samplerate': options.samplerate}
     219
     220    info = "found %d timestamps in " % len(timestamps)
     221    info += base_info
     222    sys.stderr.write(info)
     223
     224    if options.cut:
     225        _cut_slice(options, timestamps)
     226        info = "created %d slices from " % len(timestamps)
     227        info += base_info
    213228        sys.stderr.write(info)
Note: See TracChangeset for help on using the changeset viewer.