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