source: python/lib/aubio/cut.py @ f8c75aa

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5
Last change on this file since f8c75aa was f8c75aa, checked in by Paul Brossier <piem@piem.org>, 5 years ago

[py] [style] add space after , in cut.py

  • Property mode set to 100644
File size: 7.2 KB
RevLine 
[9582713]1#! /usr/bin/env python
2
3""" this file was written by Paul Brossier
4  it is released under the GNU/GPL license.
5"""
6
7import sys
[f8c75aa]8import warnings
[c81c3d2]9from aubio.cmd import AubioArgumentParser
[f8c75aa]10from aubio.slicing import slice_source_at_stamps
11
[9582713]12
[b02d52f]13def aubio_cut_parser():
[c81c3d2]14    parser = AubioArgumentParser()
15    parser.add_input()
[f8c75aa]16    parser.add_argument("-O", "--onset-method",
[9582713]17            action="store", dest="onset_method", default='default',
18            metavar = "<onset_method>",
19            help="onset detection method [default=default] \
20                    complexdomain|hfc|phase|specdiff|energy|kl|mkl")
21    # cutting methods
[f8c75aa]22    parser.add_argument("-b", "--beat",
[9582713]23            action="store_true", dest="beat", default=False,
[c81c3d2]24            help="slice at beat locations")
[7fc5ba2]25    """
[f8c75aa]26    parser.add_argument("-S", "--silencecut",
[9582713]27            action="store_true", dest="silencecut", default=False,
28            help="use silence locations")
[f8c75aa]29    parser.add_argument("-s", "--silence",
[9582713]30            metavar = "<value>",
31            action="store", dest="silence", default=-70,
32            help="silence threshold [default=-70]")
33            """
34    # algorithm parameters
[c81c3d2]35    parser.add_buf_hop_size()
[f8c75aa]36    parser.add_argument("-t", "--threshold", "--onset-threshold",
[c81c3d2]37            metavar = "<threshold>", type=float,
[9582713]38            action="store", dest="threshold", default=0.3,
39            help="onset peak picking threshold [default=0.3]")
[f8c75aa]40    parser.add_argument("-c", "--cut",
[9582713]41            action="store_true", dest="cut", default=False,
[c81c3d2]42            help="cut input sound file at detected labels")
43    parser.add_minioi()
[4c0a1db]44
[9582713]45    """
[f8c75aa]46    parser.add_argument("-D", "--delay",
[a5004903]47            action = "store", dest = "delay", type = float,
[9582713]48            metavar = "<seconds>", default=0,
49            help="number of seconds to take back [default=system]\
50                    default system delay is 3*hopsize/samplerate")
[f8c75aa]51    parser.add_argument("-C", "--dcthreshold",
[9582713]52            metavar = "<value>",
53            action="store", dest="dcthreshold", default=1.,
54            help="onset peak picking DC component [default=1.]")
[f8c75aa]55    parser.add_argument("-L", "--localmin",
[9582713]56            action="store_true", dest="localmin", default=False,
57            help="use local minima after peak detection")
[f8c75aa]58    parser.add_argument("-d", "--derivate",
[9582713]59            action="store_true", dest="derivate", default=False,
60            help="derivate onset detection function")
[f8c75aa]61    parser.add_argument("-z", "--zerocross",
[9582713]62            metavar = "<value>",
63            action="store", dest="zerothres", default=0.008,
64            help="zero-crossing threshold for slicing [default=0.00008]")
65    # plotting functions
[f8c75aa]66    parser.add_argument("-p", "--plot",
[9582713]67            action="store_true", dest="plot", default=False,
68            help="draw plot")
[f8c75aa]69    parser.add_argument("-x", "--xsize",
[9582713]70            metavar = "<size>",
71            action="store", dest="xsize", default=1.,
[a5004903]72            type=float, help="define xsize for plot")
[f8c75aa]73    parser.add_argument("-y", "--ysize",
[9582713]74            metavar = "<size>",
75            action="store", dest="ysize", default=1.,
[a5004903]76            type=float, help="define ysize for plot")
[f8c75aa]77    parser.add_argument("-f", "--function",
[9582713]78            action="store_true", dest="func", default=False,
79            help="print detection function")
[f8c75aa]80    parser.add_argument("-n", "--no-onsets",
[9582713]81            action="store_true", dest="nplot", default=False,
82            help="do not plot detected onsets")
[f8c75aa]83    parser.add_argument("-O", "--outplot",
[9582713]84            metavar = "<output_image>",
85            action="store", dest="outplot", default=None,
86            help="save plot to output.{ps,png}")
[f8c75aa]87    parser.add_argument("-F", "--spectrogram",
[9582713]88            action="store_true", dest="spectro", default=False,
89            help="add spectrogram to the plot")
90    """
[c81c3d2]91    parser.add_slicer_options()
92    parser.add_verbose_help()
[b02d52f]93    return parser
[9582713]94
95
[b02d52f]96def _cut_analyze(options):
[c81c3d2]97    hopsize = options.hop_size
98    bufsize = options.buf_size
[9582713]99    samplerate = options.samplerate
[c81c3d2]100    source_uri = options.source_uri
[9582713]101
[b02d52f]102    # analyze pass
[4077fa1]103    from aubio import onset, tempo, source
[9582713]104
[c81c3d2]105    s = source(source_uri, samplerate, hopsize)
[b02d52f]106    if samplerate == 0:
[adf09b2]107        samplerate = s.samplerate
[b02d52f]108        options.samplerate = samplerate
[9582713]109
[7fc5ba2]110    if options.beat:
[f8c75aa]111        o = tempo(options.onset_method, bufsize, hopsize,
112                samplerate=samplerate)
[7fc5ba2]113    else:
[f8c75aa]114        o = onset(options.onset_method, bufsize, hopsize,
115                samplerate=samplerate)
[4c0a1db]116        if options.minioi:
117            if options.minioi.endswith('ms'):
118                o.set_minioi_ms(int(options.minioi[:-2]))
119            elif options.minioi.endswith('s'):
120                o.set_minioi_s(int(options.minioi[:-1]))
121            else:
122                o.set_minioi(int(options.minioi))
[9582713]123    o.set_threshold(options.threshold)
124
125    timestamps = []
[dee4164]126    total_frames = 0
[9582713]127    while True:
128        samples, read = s()
[dee4164]129        if o(samples):
[f8c75aa]130            timestamps.append(o.get_last())
131            if options.verbose:
132                print("%.4f" % o.get_last_s())
[dee4164]133        total_frames += read
[f8c75aa]134        if read < hopsize:
135            break
[7fc5ba2]136    del s
[b02d52f]137    return timestamps, total_frames
[dee4164]138
[f8c75aa]139
[b02d52f]140def _cut_slice(options, timestamps):
[dee4164]141    # cutting pass
[b02d52f]142    nstamps = len(timestamps)
143    if nstamps > 0:
[94b16e89]144        # generate output files
145        timestamps_end = None
[e79acd9]146        if options.cut_every_nslices:
147            timestamps = timestamps[::options.cut_every_nslices]
148            nstamps = len(timestamps)
[94b16e89]149        if options.cut_until_nslices and options.cut_until_nsamples:
[f8c75aa]150            msg = "using cut_until_nslices, but cut_until_nsamples is set"
151            warnings.warn(msg)
[94b16e89]152        if options.cut_until_nsamples:
[f8c75aa]153            lag = options.cut_until_nsamples
154            timestamps_end = [t + lag for t in timestamps[1:]]
155            timestamps_end += [1e120]
[94b16e89]156        if options.cut_until_nslices:
[f8c75aa]157            slice_lag = options.cut_until_nslices
158            timestamps_end = [t for t in timestamps[1 + slice_lag:]]
159            timestamps_end += [1e120] * (options.cut_until_nslices + 1)
[c81c3d2]160        slice_source_at_stamps(options.source_uri,
[b02d52f]161                timestamps, timestamps_end = timestamps_end,
[a2ca72a]162                output_dir = options.output_directory,
[e126e65]163                samplerate = options.samplerate,
164                create_first = options.create_first)
[b02d52f]165
166def main():
167    parser = aubio_cut_parser()
168    options = parser.parse_args()
[c81c3d2]169    if not options.source_uri and not options.source_uri2:
[b02d52f]170        sys.stderr.write("Error: no file name given\n")
171        parser.print_help()
172        sys.exit(1)
[c81c3d2]173    elif options.source_uri2 is not None:
174        options.source_uri = options.source_uri2
[b02d52f]175
176    # analysis
177    timestamps, total_frames = _cut_analyze(options)
178
179    # print some info
[f8c75aa]180    duration = float(total_frames) / float(options.samplerate)
[c81c3d2]181    base_info = '%(source_uri)s' % {'source_uri': options.source_uri}
[b02d52f]182    base_info += ' (total %(duration).2fs at %(samplerate)dHz)\n' % \
183            {'duration': duration, 'samplerate': options.samplerate}
184
185    info = "found %d timestamps in " % len(timestamps)
186    info += base_info
187    sys.stderr.write(info)
[dee4164]188
[b02d52f]189    if options.cut:
190        _cut_slice(options, timestamps)
191        info = "created %d slices from " % len(timestamps)
192        info += base_info
[dee4164]193        sys.stderr.write(info)
Note: See TracBrowser for help on using the repository browser.