Showing the changes to python/scripts (1 of 2 files). Show all
Unified · Split
| @@ -0,0 +1,163 @@ | |||
| 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.task import * | ||
| 9 | |||
| 10 | usage = "usage: %s [options] -i soundfile" % sys.argv[0] | ||
| 11 | |||
| 12 | def parse_args(): | ||
| 13 | from optparse import OptionParser | ||
| 14 | parser = OptionParser(usage=usage) | ||
| 15 | parser.add_option("-i", "--input", action = "store", dest = "source_file", | ||
| 16 | help="input sound file to analyse", metavar = "<source_file>") | ||
| 17 | parser.add_option("-m","--method", | ||
| 18 | action="store", dest="onset_method", default='default', | ||
| 19 | metavar = "<onset_method>", | ||
| 20 | help="onset detection method [default=default] \ | ||
| 21 | complexdomain|hfc|phase|specdiff|energy|kl|mkl") | ||
| 22 | # cutting methods | ||
| 23 | """ | ||
| 24 | parser.add_option("-b","--beat", | ||
| 25 | action="store_true", dest="beat", default=False, | ||
| 26 | help="use beat locations") | ||
| 27 | parser.add_option("-S","--silencecut", | ||
| 28 | action="store_true", dest="silencecut", default=False, | ||
| 29 | help="use silence locations") | ||
| 30 | parser.add_option("-s","--silence", | ||
| 31 | metavar = "<value>", | ||
| 32 | action="store", dest="silence", default=-70, | ||
| 33 | help="silence threshold [default=-70]") | ||
| 34 | """ | ||
| 35 | # algorithm parameters | ||
| 36 | parser.add_option("--samplerate", | ||
| 37 | metavar = "<freq>", type='int', | ||
| 38 | action="store", dest="samplerate", default=0, | ||
| 39 | help="samplerate at which the file should be represented") | ||
| 40 | parser.add_option("-B","--bufsize", | ||
| 41 | action="store", dest="bufsize", default=512, | ||
| 42 | metavar = "<size>", | ||
| 43 | help="buffer size [default=512]") | ||
| 44 | parser.add_option("-H","--hopsize", | ||
| 45 | metavar = "<size>", | ||
| 46 | action="store", dest="hopsize", default=256, | ||
| 47 | help="overlap size [default=256]") | ||
| 48 | parser.add_option("-t","--threshold", | ||
| 49 | metavar = "<value>", type="float", | ||
| 50 | action="store", dest="threshold", default=0.3, | ||
| 51 | help="onset peak picking threshold [default=0.3]") | ||
| 52 | parser.add_option("-c","--cut", | ||
| 53 | action="store_true", dest="cut", default=False, | ||
| 54 | help="cut input sound file at detected labels \ | ||
| 55 | best used with option -L") | ||
| 56 | """ | ||
| 57 | parser.add_option("-D","--delay", | ||
| 58 | action = "store", dest = "delay", type = "float", | ||
| 59 | metavar = "<seconds>", default=0, | ||
| 60 | help="number of seconds to take back [default=system]\ | ||
| 61 | default system delay is 3*hopsize/samplerate") | ||
| 62 | parser.add_option("-C","--dcthreshold", | ||
| 63 | metavar = "<value>", | ||
| 64 | action="store", dest="dcthreshold", default=1., | ||
| 65 | help="onset peak picking DC component [default=1.]") | ||
| 66 | parser.add_option("-M","--mintol", | ||
| 67 | metavar = "<value>", | ||
| 68 | action="store", dest="mintol", default=0.048, | ||
| 69 | help="minimum inter onset interval [default=0.048]") | ||
| 70 | parser.add_option("-L","--localmin", | ||
| 71 | action="store_true", dest="localmin", default=False, | ||
| 72 | help="use local minima after peak detection") | ||
| 73 | parser.add_option("-d","--derivate", | ||
| 74 | action="store_true", dest="derivate", default=False, | ||
| 75 | help="derivate onset detection function") | ||
| 76 | parser.add_option("-z","--zerocross", | ||
| 77 | metavar = "<value>", | ||
| 78 | action="store", dest="zerothres", default=0.008, | ||
| 79 | help="zero-crossing threshold for slicing [default=0.00008]") | ||
| 80 | """ | ||
| 81 | # plotting functions | ||
| 82 | """ | ||
| 83 | parser.add_option("-p","--plot", | ||
| 84 | action="store_true", dest="plot", default=False, | ||
| 85 | help="draw plot") | ||
| 86 | parser.add_option("-x","--xsize", | ||
| 87 | metavar = "<size>", | ||
| 88 | action="store", dest="xsize", default=1., | ||
| 89 | type='float', help="define xsize for plot") | ||
| 90 | parser.add_option("-y","--ysize", | ||
| 91 | metavar = "<size>", | ||
| 92 | action="store", dest="ysize", default=1., | ||
| 93 | type='float', help="define ysize for plot") | ||
| 94 | parser.add_option("-f","--function", | ||
| 95 | action="store_true", dest="func", default=False, | ||
| 96 | help="print detection function") | ||
| 97 | parser.add_option("-n","--no-onsets", | ||
| 98 | action="store_true", dest="nplot", default=False, | ||
| 99 | help="do not plot detected onsets") | ||
| 100 | parser.add_option("-O","--outplot", | ||
| 101 | metavar = "<output_image>", | ||
| 102 | action="store", dest="outplot", default=None, | ||
| 103 | help="save plot to output.{ps,png}") | ||
| 104 | parser.add_option("-F","--spectrogram", | ||
| 105 | action="store_true", dest="spectro", default=False, | ||
| 106 | help="add spectrogram to the plot") | ||
| 107 | """ | ||
| 108 | parser.add_option("-v","--verbose", | ||
| 109 | action="store_true", dest="verbose", default=True, | ||
| 110 | help="make lots of noise [default]") | ||
| 111 | parser.add_option("-q","--quiet", | ||
| 112 | action="store_false", dest="verbose", default=True, | ||
| 113 | help="be quiet") | ||
| 114 | (options, args) = parser.parse_args() | ||
| 115 | if not options.source_file: | ||
| 116 | print "no file name given\n", usage | ||
| 117 | sys.exit(1) | ||
| 118 | return options, args | ||
| 119 | |||
| 120 | if __name__ == '__main__': | ||
| 121 | options, args = parse_args() | ||
| 122 | |||
| 123 | hopsize = options.hopsize | ||
| 124 | bufsize = options.bufsize | ||
| 125 | samplerate = options.samplerate | ||
| 126 | source_file = options.source_file | ||
| 127 | |||
| 128 | from aubio import onset, source, sink | ||
| 129 | |||
| 130 | s = source(source_file, samplerate, hopsize) | ||
| 131 | if samplerate == 0: samplerate = s.get_samplerate() | ||
| 132 | |||
| 133 | o = onset(options.onset_method, bufsize, hopsize) | ||
| 134 | o.set_threshold(options.threshold) | ||
| 135 | |||
| 136 | slice_number = 0 | ||
| 137 | #if options.cut: | ||
| 138 | this_slice = sink('/tmp/t-%02d.wav' % slice_number, samplerate) | ||
| 139 | |||
| 140 | timestamps = [] | ||
| 141 | block_read = 0 | ||
| 142 | while True: | ||
| 143 | samples, read = s() | ||
| 144 | #ring_buffer = hstack([ring_buffer, samples]) | ||
| 145 | is_onset = o(samples) | ||
| 146 | if is_onset: | ||
| 147 | this_onset = (block_read - 4. + is_onset[0]) * hopsize | ||
| 148 | if options.verbose: | ||
| 149 | print "%.4f" % ( this_onset / samplerate ) | ||
| 150 | timestamps.append (this_onset) | ||
| 151 | del this_slice | ||
| 152 | slice_number += 1 | ||
| 153 | this_slice = sink('/tmp/t-%02d.wav' % slice_number, samplerate) | ||
| 154 | this_slice(samples, read) | ||
| 155 | block_read += 1 | ||
| 156 | if read < hopsize: break | ||
| 157 | |||
| 158 | # print some info | ||
| 159 | duration = float ( block_read * hopsize + read ) / samplerate | ||
| 160 | nstamps = len(timestamps) | ||
| 161 | info = 'found %(nstamps)d timestamps in %(source_file)s' % locals() | ||
| 162 | info += ' (read %(duration).2fs at %(samplerate)dHz)\n' % locals() | ||
| 163 | sys.stderr.write(info) | ||