Changeset 98df9f4 for python/aubiocut
- Timestamp:
- May 28, 2005, 11:06:33 PM (20 years ago)
- Branches:
- feature/autosink, feature/cnn, feature/cnn_org, feature/constantq, feature/crepe, feature/crepe_org, feature/pitchshift, feature/pydocstrings, feature/timestretch, fix/ffmpeg5, master, pitchshift, sampler, timestretch, yinfft+
- Children:
- 6b384f3
- Parents:
- ead2920
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
python/aubiocut
read2920 r98df9f4 1 #! /usr/bin/python1 #! /usr/bin/python 2 2 3 3 """ this file was written by Paul Brossier … … 5 5 """ 6 6 7 import sys 8 import numarray 7 9 from aubio.aubioclass import * 8 10 9 bufsize = 1024 10 hopsize = bufsize/2 11 usage = "usage: %s [options] -i soundfile" % sys.argv[0] 11 12 12 def cutfile(filein,slicetimes,zerothres=0.002): 13 frameread = 0 14 readsize = hopsize 15 filei = sndfile(filein) 16 framestep = hopsize/(filei.samplerate()+0.) 17 channels = filei.channels() 18 newname = "%s%f%s" % ("/tmp/",0.0000000,filein[-4:]) 19 fileo = sndfile(newname,model=filei) 20 myvec = fvec(hopsize,channels) 21 mycopy = fvec(hopsize,channels) 22 while(readsize==hopsize): 23 readsize = filei.read(hopsize,myvec) 24 # write to current file 25 if len(slicetimes) and frameread >= slicetimes[0]: 26 slicetimes.pop(0) 27 # write up to 1st zero crossing 28 zerocross = 0 29 while ( abs( myvec.get(zerocross,0) ) > zerothres ): 30 zerocross += 1 31 writesize = fileo.write(zerocross,myvec) 32 fromcross = 0 33 while (zerocross < readsize): 34 for i in range(channels): 35 mycopy.set(myvec.get(zerocross,i),fromcross,i) 36 fromcross += 1 37 zerocross += 1 38 del fileo 39 fileo = sndfile("%s%s%f%s%s" % 40 (filein.split(".")[0].split("/")[-1],".", 41 frameread*framestep,".",filein.split(".")[-1]),model=filei) 42 writesize = fileo.write(fromcross,mycopy) 43 else: 44 writesize = fileo.write(readsize,myvec) 45 frameread += 1 46 del fileo 13 def check_mode(option, opt, value, parser): 14 nvalue = parser.rargs[0] 15 if nvalue == 'complexdomain' : 16 setattr(parser.values, option.dest, complexdomain) 17 elif nvalue == 'hfc' : 18 setattr(parser.values, option.dest, hfc) 19 elif nvalue == 'phase' : 20 setattr(parser.values, option.dest, phase) 21 elif nvalue == 'specdiff' : 22 setattr(parser.values, option.dest, specdiff) 23 elif nvalue == 'energy' : 24 setattr(parser.values, option.dest, energy) 25 elif nvalue == 'dual' : 26 setattr(parser.values, option.dest, 'dual') 47 27 48 import sys 49 filename = sys.argv[1] 50 threshold = 0.2 51 if (len(sys.argv) > 2): threshold = sys.argv[2] 52 onsets = getonsets(filename,threshold) 53 cutfile(filename,onsets) 28 def parse_args(): 29 from optparse import OptionParser 30 parser = OptionParser(usage=usage) 31 parser.add_option("-i","--input", 32 action="store", dest="filename", 33 help="input sound file") 34 parser.add_option("-m","--mode", action="callback", 35 callback=check_mode, dest="mode", default='dual', 36 help="onsetdetection mode [default=dual] \ 37 complexdomain|hfc|phase|specdiff|energy|dual") 38 parser.add_option("-B","--bufsize", 39 action="store", dest="bufsize", default=1024, 40 help="buffer size [default=1024]") 41 parser.add_option("-H","--hopsize", 42 action="store", dest="hopsize", default=512, 43 help="overlap size [default=512]") 44 parser.add_option("-t","--threshold", 45 action="store", dest="threshold", default=0.3, 46 help="onset peak picking threshold [default=0.3]") 47 parser.add_option("-s","--silence", 48 action="store", dest="silence", default=-70, 49 help="silence threshold [default=-70]") 50 parser.add_option("-M","--mintol", 51 action="store", dest="mintol", default=0.048, 52 help="minimum inter onset interval [default=0.048]") 53 parser.add_option("-D","--delay", 54 action="store", dest="delay", default=0.022, 55 help="number of seconds to take back [default=0.022]") 56 parser.add_option("-L","--localmin", 57 action="store_true", dest="localmin", default=False, 58 help="use local minima after peak detection") 59 parser.add_option("-c","--cut", 60 action="store_true", dest="cut", default=False, 61 help="cut input sound file at detected labels \ 62 best used with option -L") 63 # to be implemented 64 # plotting functions 65 parser.add_option("-d","--derivative", 66 action="store_true", dest="derivative", default=False, 67 help="NOT IMPLEMENTED derivate onset detection function") 68 parser.add_option("-p","--plot", 69 action="store_true", dest="doplot", default=False, 70 help="NOT IMPLEMENTED draw plot") 71 parser.add_option("-O","--outplot", 72 action="store", dest="output-plot", default=None, 73 help="NOT IMPLEMENTED save plot to output.{ps,png}") 74 parser.add_option("-z","--zerocross", 75 action="store_true", dest="zerocross", default=False, 76 help="NOT IMPLEMENTED zero crossing matching") 77 parser.add_option("-b","--beat", 78 action="store_true", dest="beat", default=False, 79 help="NOT IMPLEMENTED output beat locations") 80 parser.add_option("-v","--verbose", 81 action="store_true", dest="verbose", default=False, 82 help="make lots of noise [default]") 83 parser.add_option("-q","--quiet", 84 action="store_false", dest="verbose", default=False, 85 help="be quiet") 86 (options, args) = parser.parse_args() 87 if not options.filename: 88 print "no file name given\n", usage 89 sys.exit(1) 90 return options, args 91 92 options, args = parse_args() 93 94 filename = options.filename 95 samplerate = float(sndfile(filename).samplerate()) 96 hopsize = options.hopsize 97 bufsize = options.bufsize 98 threshold = float(options.threshold) 99 silence = float(options.silence) 100 mintol = float(options.mintol)*samplerate/hopsize 101 delay = float(options.delay) 102 103 if options.beat: 104 #onsets = getbeats(filename,threshold,silence,mode=options.mode) 105 exit("not implemented yet") 106 else: 107 onsets = getonsets(filename,threshold,silence, 108 mode=options.mode,localmin=options.localmin, 109 bufsize=bufsize,hopsize=hopsize) 110 111 # take back system delay 112 if delay != 0: 113 for i in range(len(onsets)): 114 onsets[i] -= delay*samplerate/hopsize 115 116 # prune doubled 117 if mintol > 0: 118 last = -2*mintol 119 newonsets = [] 120 for new in onsets: 121 if (new - last > mintol): 122 newonsets.append(new) 123 last = new 124 onsets = newonsets 125 126 # print times in second 127 if options.verbose: 128 for i in onsets: print "%f" % (i*hopsize/samplerate) 129 130 if options.cut: 131 cutfile(filename,onsets,bufsize=bufsize,hopsize=hopsize)
Note: See TracChangeset
for help on using the changeset viewer.