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

feature/autosinkfeature/constantqfeature/pitchshiftfeature/pydocstringsfeature/timestretch
Last change on this file since eb8f7d5 was eb8f7d5, checked in by Paul Brossier <piem@piem.org>, 3 years ago

python/lib/aubio/cut.py: remove unused variable

  • Property mode set to 100644
File size: 9.2 KB
Line 
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
8import argparse
9
10def aubio_cut_parser():
11    parser = argparse.ArgumentParser()
12    parser.add_argument("source_file", default=None, nargs='?',
13            help="input sound file to analyse", metavar = "<source_file>")
14    parser.add_argument("-i", "--input", action = "store", dest = "source_file2",
15            help="input sound file to analyse", metavar = "<source_file>")
16    parser.add_argument("-O","--onset-method",
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
22    parser.add_argument("-b","--beat",
23            action="store_true", dest="beat", default=False,
24            help="use beat locations")
25    """
26    parser.add_argument("-S","--silencecut",
27            action="store_true", dest="silencecut", default=False,
28            help="use silence locations")
29    parser.add_argument("-s","--silence",
30            metavar = "<value>",
31            action="store", dest="silence", default=-70,
32            help="silence threshold [default=-70]")
33            """
34    # algorithm parameters
35    parser.add_argument("-r", "--samplerate",
36            metavar = "<freq>", type=int,
37            action="store", dest="samplerate", default=0,
38            help="samplerate at which the file should be represented")
39    parser.add_argument("-B","--bufsize",
40            action="store", dest="bufsize", default=512,
41            metavar = "<size>", type=int,
42            help="buffer size [default=512]")
43    parser.add_argument("-H","--hopsize",
44            metavar = "<size>", type=int,
45            action="store", dest="hopsize", default=256,
46            help="overlap size [default=256]")
47    parser.add_argument("-t","--onset-threshold",
48            metavar = "<value>", type=float,
49            action="store", dest="threshold", default=0.3,
50            help="onset peak picking threshold [default=0.3]")
51    parser.add_argument("-c","--cut",
52            action="store_true", dest="cut", default=False,
53            help="cut input sound file at detected labels \
54                    best used with option -L")
55
56    # minioi
57    parser.add_argument("-M","--minioi",
58            metavar = "<value>", type=str,
59            action="store", dest="minioi", default="12ms",
60            help="minimum inter onset interval [default=12ms]")
61
62    """
63    parser.add_argument("-D","--delay",
64            action = "store", dest = "delay", type = float,
65            metavar = "<seconds>", default=0,
66            help="number of seconds to take back [default=system]\
67                    default system delay is 3*hopsize/samplerate")
68    parser.add_argument("-C","--dcthreshold",
69            metavar = "<value>",
70            action="store", dest="dcthreshold", default=1.,
71            help="onset peak picking DC component [default=1.]")
72    parser.add_argument("-L","--localmin",
73            action="store_true", dest="localmin", default=False,
74            help="use local minima after peak detection")
75    parser.add_argument("-d","--derivate",
76            action="store_true", dest="derivate", default=False,
77            help="derivate onset detection function")
78    parser.add_argument("-z","--zerocross",
79            metavar = "<value>",
80            action="store", dest="zerothres", default=0.008,
81            help="zero-crossing threshold for slicing [default=0.00008]")
82            """
83    # plotting functions
84    """
85    parser.add_argument("-p","--plot",
86            action="store_true", dest="plot", default=False,
87            help="draw plot")
88    parser.add_argument("-x","--xsize",
89            metavar = "<size>",
90            action="store", dest="xsize", default=1.,
91            type=float, help="define xsize for plot")
92    parser.add_argument("-y","--ysize",
93            metavar = "<size>",
94            action="store", dest="ysize", default=1.,
95            type=float, help="define ysize for plot")
96    parser.add_argument("-f","--function",
97            action="store_true", dest="func", default=False,
98            help="print detection function")
99    parser.add_argument("-n","--no-onsets",
100            action="store_true", dest="nplot", default=False,
101            help="do not plot detected onsets")
102    parser.add_argument("-O","--outplot",
103            metavar = "<output_image>",
104            action="store", dest="outplot", default=None,
105            help="save plot to output.{ps,png}")
106    parser.add_argument("-F","--spectrogram",
107            action="store_true", dest="spectro", default=False,
108            help="add spectrogram to the plot")
109    """
110    parser.add_argument("-o","--output", type = str,
111            metavar = "<outputdir>",
112            action="store", dest="output_directory", default=None,
113            help="specify path where slices of the original file should be created")
114    parser.add_argument("--cut-until-nsamples", type = int,
115            metavar = "<samples>",
116            action = "store", dest = "cut_until_nsamples", default = None,
117            help="how many extra samples should be added at the end of each slice")
118    parser.add_argument("--cut-every-nslices", type = int,
119            metavar = "<samples>",
120            action = "store", dest = "cut_every_nslices", default = None,
121            help="how many slices should be groupped together at each cut")
122    parser.add_argument("--cut-until-nslices", type = int,
123            metavar = "<slices>",
124            action = "store", dest = "cut_until_nslices", default = None,
125            help="how many extra slices should be added at the end of each slice")
126
127    parser.add_argument("-v","--verbose",
128            action="store_true", dest="verbose", default=True,
129            help="make lots of noise [default]")
130    parser.add_argument("-q","--quiet",
131            action="store_false", dest="verbose", default=True,
132            help="be quiet")
133    return parser
134
135
136def _cut_analyze(options):
137    source_file = options.source_file
138    hopsize = options.hopsize
139    bufsize = options.bufsize
140    samplerate = options.samplerate
141    source_file = options.source_file
142
143    # analyze pass
144    from aubio import onset, tempo, source
145
146    s = source(source_file, samplerate, hopsize)
147    if samplerate == 0:
148        samplerate = s.get_samplerate()
149        options.samplerate = samplerate
150
151    if options.beat:
152        o = tempo(options.onset_method, bufsize, hopsize, samplerate=samplerate)
153    else:
154        o = onset(options.onset_method, bufsize, hopsize, samplerate=samplerate)
155        if options.minioi:
156            if options.minioi.endswith('ms'):
157                o.set_minioi_ms(int(options.minioi[:-2]))
158            elif options.minioi.endswith('s'):
159                o.set_minioi_s(int(options.minioi[:-1]))
160            else:
161                o.set_minioi(int(options.minioi))
162    o.set_threshold(options.threshold)
163
164    timestamps = []
165    total_frames = 0
166    while True:
167        samples, read = s()
168        if o(samples):
169            timestamps.append (o.get_last())
170            if options.verbose: print ("%.4f" % o.get_last_s())
171        total_frames += read
172        if read < hopsize: break
173    del s
174    return timestamps, total_frames
175
176def _cut_slice(options, timestamps):
177    # cutting pass
178    nstamps = len(timestamps)
179    if nstamps > 0:
180        # generate output files
181        from aubio.slicing import slice_source_at_stamps
182        timestamps_end = None
183        if options.cut_every_nslices:
184            timestamps = timestamps[::options.cut_every_nslices]
185            nstamps = len(timestamps)
186        if options.cut_until_nslices and options.cut_until_nsamples:
187            print ("warning: using cut_until_nslices, but cut_until_nsamples is set")
188        if options.cut_until_nsamples:
189            timestamps_end = [t + options.cut_until_nsamples for t in timestamps[1:]]
190            timestamps_end += [ 1e120 ]
191        if options.cut_until_nslices:
192            timestamps_end = [t for t in timestamps[1 + options.cut_until_nslices:]]
193            timestamps_end += [ 1e120 ] * (options.cut_until_nslices + 1)
194        slice_source_at_stamps(options.source_file,
195                timestamps, timestamps_end = timestamps_end,
196                output_dir = options.output_directory,
197                samplerate = options.samplerate)
198
199def main():
200    parser = aubio_cut_parser()
201    options = parser.parse_args()
202    if not options.source_file and not options.source_file2:
203        sys.stderr.write("Error: no file name given\n")
204        parser.print_help()
205        sys.exit(1)
206    elif options.source_file2 is not None:
207        options.source_file = options.source_file2
208
209    # analysis
210    timestamps, total_frames = _cut_analyze(options)
211
212    # print some info
213    duration = float (total_frames) / float(options.samplerate)
214    base_info = '%(source_file)s' % {'source_file': options.source_file}
215    base_info += ' (total %(duration).2fs at %(samplerate)dHz)\n' % \
216            {'duration': duration, 'samplerate': options.samplerate}
217
218    info = "found %d timestamps in " % len(timestamps)
219    info += base_info
220    sys.stderr.write(info)
221
222    if options.cut:
223        _cut_slice(options, timestamps)
224        info = "created %d slices from " % len(timestamps)
225        info += base_info
226        sys.stderr.write(info)
Note: See TracBrowser for help on using the repository browser.