[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 | |
---|
| 7 | import sys |
---|
[c81c3d2] | 8 | from aubio.cmd import AubioArgumentParser |
---|
[9582713] | 9 | |
---|
[b02d52f] | 10 | def aubio_cut_parser(): |
---|
[c81c3d2] | 11 | parser = AubioArgumentParser() |
---|
| 12 | parser.add_input() |
---|
[a5004903] | 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 |
---|
[a5004903] | 19 | parser.add_argument("-b","--beat", |
---|
[9582713] | 20 | action="store_true", dest="beat", default=False, |
---|
[c81c3d2] | 21 | help="slice at beat locations") |
---|
[7fc5ba2] | 22 | """ |
---|
[a5004903] | 23 | parser.add_argument("-S","--silencecut", |
---|
[9582713] | 24 | action="store_true", dest="silencecut", default=False, |
---|
| 25 | help="use silence locations") |
---|
[a5004903] | 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() |
---|
| 33 | parser.add_argument("-t","--threshold", "--onset-threshold", |
---|
| 34 | metavar = "<threshold>", type=float, |
---|
[9582713] | 35 | action="store", dest="threshold", default=0.3, |
---|
| 36 | help="onset peak picking threshold [default=0.3]") |
---|
[a5004903] | 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 | """ |
---|
[a5004903] | 43 | parser.add_argument("-D","--delay", |
---|
| 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") |
---|
[a5004903] | 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.]") |
---|
[a5004903] | 52 | parser.add_argument("-L","--localmin", |
---|
[9582713] | 53 | action="store_true", dest="localmin", default=False, |
---|
| 54 | help="use local minima after peak detection") |
---|
[a5004903] | 55 | parser.add_argument("-d","--derivate", |
---|
[9582713] | 56 | action="store_true", dest="derivate", default=False, |
---|
| 57 | help="derivate onset detection function") |
---|
[a5004903] | 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 |
---|
[a5004903] | 63 | parser.add_argument("-p","--plot", |
---|
[9582713] | 64 | action="store_true", dest="plot", default=False, |
---|
| 65 | help="draw plot") |
---|
[a5004903] | 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") |
---|
| 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") |
---|
| 74 | parser.add_argument("-f","--function", |
---|
[9582713] | 75 | action="store_true", dest="func", default=False, |
---|
| 76 | help="print detection function") |
---|
[a5004903] | 77 | parser.add_argument("-n","--no-onsets", |
---|
[9582713] | 78 | action="store_true", dest="nplot", default=False, |
---|
| 79 | help="do not plot detected onsets") |
---|
[a5004903] | 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}") |
---|
[a5004903] | 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] | 93 | def _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: |
---|
[2bf530e0] | 108 | o = tempo(options.onset_method, bufsize, hopsize, samplerate=samplerate) |
---|
[7fc5ba2] | 109 | else: |
---|
[2bf530e0] | 110 | o = onset(options.onset_method, bufsize, hopsize, samplerate=samplerate) |
---|
[4c0a1db] | 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)) |
---|
[9582713] | 118 | o.set_threshold(options.threshold) |
---|
| 119 | |
---|
| 120 | timestamps = [] |
---|
[dee4164] | 121 | total_frames = 0 |
---|
[9582713] | 122 | while True: |
---|
| 123 | samples, read = s() |
---|
[dee4164] | 124 | if o(samples): |
---|
[7fc5ba2] | 125 | timestamps.append (o.get_last()) |
---|
[1c6fe64] | 126 | if options.verbose: print ("%.4f" % o.get_last_s()) |
---|
[dee4164] | 127 | total_frames += read |
---|
[9582713] | 128 | if read < hopsize: break |
---|
[7fc5ba2] | 129 | del s |
---|
[b02d52f] | 130 | return timestamps, total_frames |
---|
[dee4164] | 131 | |
---|
[b02d52f] | 132 | def _cut_slice(options, timestamps): |
---|
[dee4164] | 133 | # cutting pass |
---|
[b02d52f] | 134 | nstamps = len(timestamps) |
---|
| 135 | if nstamps > 0: |
---|
[94b16e89] | 136 | # generate output files |
---|
[bc24e9c] | 137 | from aubio.slicing import slice_source_at_stamps |
---|
[94b16e89] | 138 | timestamps_end = None |
---|
[e79acd9] | 139 | if options.cut_every_nslices: |
---|
| 140 | timestamps = timestamps[::options.cut_every_nslices] |
---|
| 141 | nstamps = len(timestamps) |
---|
[94b16e89] | 142 | if options.cut_until_nslices and options.cut_until_nsamples: |
---|
[1c6fe64] | 143 | print ("warning: using cut_until_nslices, but cut_until_nsamples is set") |
---|
[94b16e89] | 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) |
---|
[c81c3d2] | 150 | slice_source_at_stamps(options.source_uri, |
---|
[b02d52f] | 151 | timestamps, timestamps_end = timestamps_end, |
---|
[a2ca72a] | 152 | output_dir = options.output_directory, |
---|
[e126e65] | 153 | samplerate = options.samplerate, |
---|
| 154 | create_first = options.create_first) |
---|
[b02d52f] | 155 | |
---|
| 156 | def main(): |
---|
| 157 | parser = aubio_cut_parser() |
---|
| 158 | options = parser.parse_args() |
---|
[c81c3d2] | 159 | if not options.source_uri and not options.source_uri2: |
---|
[b02d52f] | 160 | sys.stderr.write("Error: no file name given\n") |
---|
| 161 | parser.print_help() |
---|
| 162 | sys.exit(1) |
---|
[c81c3d2] | 163 | elif options.source_uri2 is not None: |
---|
| 164 | options.source_uri = options.source_uri2 |
---|
[b02d52f] | 165 | |
---|
| 166 | # analysis |
---|
| 167 | timestamps, total_frames = _cut_analyze(options) |
---|
| 168 | |
---|
| 169 | # print some info |
---|
| 170 | duration = float (total_frames) / float(options.samplerate) |
---|
[c81c3d2] | 171 | base_info = '%(source_uri)s' % {'source_uri': options.source_uri} |
---|
[b02d52f] | 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) |
---|
[dee4164] | 178 | |
---|
[b02d52f] | 179 | if options.cut: |
---|
| 180 | _cut_slice(options, timestamps) |
---|
| 181 | info = "created %d slices from " % len(timestamps) |
---|
| 182 | info += base_info |
---|
[dee4164] | 183 | sys.stderr.write(info) |
---|