Ignore:
Timestamp:
Sep 30, 2017, 8:22:13 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:
fee0094
Parents:
9fabad5 (diff), 5b679ba (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge branch 'master' into PR/simplify_emscripten

File:
1 edited

Legend:

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

    r9fabad5 r23facac  
    66
    77import sys
    8 import argparse
     8from aubio.cmd import AubioArgumentParser
    99
    10 def parse_args():
    11     usage = "usage: %s [options] -i soundfile" % sys.argv[0]
    12     usage += "\n help: %s -h" % sys.argv[0]
    13     parser = argparse.ArgumentParser()
    14     parser.add_argument("source_file", default=None, nargs='?',
    15             help="input sound file to analyse", metavar = "<source_file>")
    16     parser.add_argument("-i", "--input", action = "store", dest = "source_file2",
    17             help="input sound file to analyse", metavar = "<source_file>")
     10def aubio_cut_parser():
     11    parser = AubioArgumentParser()
     12    parser.add_input()
    1813    parser.add_argument("-O","--onset-method",
    1914            action="store", dest="onset_method", default='default',
     
    2419    parser.add_argument("-b","--beat",
    2520            action="store_true", dest="beat", default=False,
    26             help="use beat locations")
     21            help="slice at beat locations")
    2722    """
    2823    parser.add_argument("-S","--silencecut",
     
    3530            """
    3631    # algorithm parameters
    37     parser.add_argument("-r", "--samplerate",
    38             metavar = "<freq>", type=int,
    39             action="store", dest="samplerate", default=0,
    40             help="samplerate at which the file should be represented")
    41     parser.add_argument("-B","--bufsize",
    42             action="store", dest="bufsize", default=512,
    43             metavar = "<size>", type=int,
    44             help="buffer size [default=512]")
    45     parser.add_argument("-H","--hopsize",
    46             metavar = "<size>", type=int,
    47             action="store", dest="hopsize", default=256,
    48             help="overlap size [default=256]")
    49     parser.add_argument("-t","--onset-threshold",
    50             metavar = "<value>", type=float,
     32    parser.add_buf_hop_size()
     33    parser.add_argument("-t","--threshold", "--onset-threshold",
     34            metavar = "<threshold>", type=float,
    5135            action="store", dest="threshold", default=0.3,
    5236            help="onset peak picking threshold [default=0.3]")
    5337    parser.add_argument("-c","--cut",
    5438            action="store_true", dest="cut", default=False,
    55             help="cut input sound file at detected labels \
    56                     best used with option -L")
    57 
    58     # minioi
    59     parser.add_argument("-M","--minioi",
    60             metavar = "<value>", type=str,
    61             action="store", dest="minioi", default="12ms",
    62             help="minimum inter onset interval [default=12ms]")
     39            help="cut input sound file at detected labels")
     40    parser.add_minioi()
    6341
    6442    """
     
    8260            action="store", dest="zerothres", default=0.008,
    8361            help="zero-crossing threshold for slicing [default=0.00008]")
    84             """
    8562    # plotting functions
    86     """
    8763    parser.add_argument("-p","--plot",
    8864            action="store_true", dest="plot", default=False,
     
    11086            help="add spectrogram to the plot")
    11187    """
    112     parser.add_argument("-o","--output", type = str,
    113             metavar = "<outputdir>",
    114             action="store", dest="output_directory", default=None,
    115             help="specify path where slices of the original file should be created")
    116     parser.add_argument("--cut-until-nsamples", type = int,
    117             metavar = "<samples>",
    118             action = "store", dest = "cut_until_nsamples", default = None,
    119             help="how many extra samples should be added at the end of each slice")
    120     parser.add_argument("--cut-every-nslices", type = int,
    121             metavar = "<samples>",
    122             action = "store", dest = "cut_every_nslices", default = None,
    123             help="how many slices should be groupped together at each cut")
    124     parser.add_argument("--cut-until-nslices", type = int,
    125             metavar = "<slices>",
    126             action = "store", dest = "cut_until_nslices", default = None,
    127             help="how many extra slices should be added at the end of each slice")
     88    parser.add_slicer_options()
     89    parser.add_verbose_help()
     90    return parser
    12891
    129     parser.add_argument("-v","--verbose",
    130             action="store_true", dest="verbose", default=True,
    131             help="make lots of noise [default]")
    132     parser.add_argument("-q","--quiet",
    133             action="store_false", dest="verbose", default=True,
    134             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
    14392
    144 def main():
    145     options = parse_args()
     93def _cut_analyze(options):
     94    hopsize = options.hop_size
     95    bufsize = options.buf_size
     96    samplerate = options.samplerate
     97    source_uri = options.source_uri
    14698
    147     source_file = options.source_file
    148     hopsize = options.hopsize
    149     bufsize = options.bufsize
    150     samplerate = options.samplerate
    151     source_file = options.source_file
    152 
     99    # analyze pass
    153100    from aubio import onset, tempo, source
    154101
    155     s = source(source_file, samplerate, hopsize)
    156     if samplerate == 0: samplerate = s.get_samplerate()
     102    s = source(source_uri, samplerate, hopsize)
     103    if samplerate == 0:
     104        samplerate = s.get_samplerate()
     105        options.samplerate = samplerate
    157106
    158107    if options.beat:
     
    171120    timestamps = []
    172121    total_frames = 0
    173     # analyze pass
    174122    while True:
    175123        samples, read = s()
     
    180128        if read < hopsize: break
    181129    del s
    182     # print some info
     130    return timestamps, total_frames
     131
     132def _cut_slice(options, timestamps):
     133    # cutting pass
    183134    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:
     135    if nstamps > 0:
    191136        # generate output files
    192137        from aubio.slicing import slice_source_at_stamps
     
    203148            timestamps_end = [t for t in timestamps[1 + options.cut_until_nslices:]]
    204149            timestamps_end += [ 1e120 ] * (options.cut_until_nslices + 1)
    205         slice_source_at_stamps(source_file, timestamps, timestamps_end = timestamps_end,
     150        slice_source_at_stamps(options.source_uri,
     151                timestamps, timestamps_end = timestamps_end,
    206152                output_dir = options.output_directory,
    207                 samplerate = samplerate)
     153                samplerate = options.samplerate)
    208154
    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()
     155def main():
     156    parser = aubio_cut_parser()
     157    options = parser.parse_args()
     158    if not options.source_uri and not options.source_uri2:
     159        sys.stderr.write("Error: no file name given\n")
     160        parser.print_help()
     161        sys.exit(1)
     162    elif options.source_uri2 is not None:
     163        options.source_uri = options.source_uri2
     164
     165    # analysis
     166    timestamps, total_frames = _cut_analyze(options)
     167
     168    # print some info
     169    duration = float (total_frames) / float(options.samplerate)
     170    base_info = '%(source_uri)s' % {'source_uri': options.source_uri}
     171    base_info += ' (total %(duration).2fs at %(samplerate)dHz)\n' % \
     172            {'duration': duration, 'samplerate': options.samplerate}
     173
     174    info = "found %d timestamps in " % len(timestamps)
     175    info += base_info
     176    sys.stderr.write(info)
     177
     178    if options.cut:
     179        _cut_slice(options, timestamps)
     180        info = "created %d slices from " % len(timestamps)
     181        info += base_info
    213182        sys.stderr.write(info)
Note: See TracChangeset for help on using the changeset viewer.