source: python/lib/aubio/cut.py @ 2cf905f

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

[py] move cut_slice from cut to cmd to avoid cyclic import

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