[5e491b3b] | 1 | from aubioclass import * |
---|
| 2 | |
---|
| 3 | def check_onset_mode(option, opt, value, parser): |
---|
| 4 | """ utility function to convert a string to aubio_onsetdetection_type """ |
---|
| 5 | nvalues = parser.rargs[0].split(',') |
---|
| 6 | val = [] |
---|
| 7 | for nvalue in nvalues: |
---|
| 8 | if nvalue == 'complexdomain' or nvalue == 'complex' : |
---|
| 9 | val.append(aubio_onset_complex) |
---|
| 10 | elif nvalue == 'hfc' : |
---|
| 11 | val.append(aubio_onset_hfc) |
---|
| 12 | elif nvalue == 'phase' : |
---|
| 13 | val.append(aubio_onset_phase) |
---|
| 14 | elif nvalue == 'specdiff' : |
---|
| 15 | val.append(aubio_onset_specdiff) |
---|
| 16 | elif nvalue == 'energy' : |
---|
| 17 | val.append(aubio_onset_energy) |
---|
| 18 | elif nvalue == 'kl' : |
---|
| 19 | val.append(aubio_onset_kl) |
---|
| 20 | elif nvalue == 'mkl' : |
---|
| 21 | val.append(aubio_onset_mkl) |
---|
| 22 | elif nvalue == 'dual' : |
---|
| 23 | val.append('dual') |
---|
| 24 | else: |
---|
| 25 | import sys |
---|
| 26 | print "unknown onset detection function selected" |
---|
| 27 | sys.exit(1) |
---|
| 28 | setattr(parser.values, option.dest, val) |
---|
| 29 | |
---|
| 30 | def check_pitch_mode(option, opt, value, parser): |
---|
| 31 | """ utility function to convert a string to aubio_pitchdetection_type""" |
---|
| 32 | nvalues = parser.rargs[0].split(',') |
---|
| 33 | val = [] |
---|
| 34 | for nvalue in nvalues: |
---|
| 35 | if nvalue == 'mcomb' : |
---|
| 36 | val.append(aubio_pitch_mcomb) |
---|
| 37 | elif nvalue == 'yin' : |
---|
| 38 | val.append(aubio_pitch_yin) |
---|
| 39 | elif nvalue == 'fcomb' : |
---|
| 40 | val.append(aubio_pitch_fcomb) |
---|
| 41 | elif nvalue == 'schmitt': |
---|
| 42 | val.append(aubio_pitch_schmitt) |
---|
| 43 | else: |
---|
| 44 | import sys |
---|
| 45 | print "error: unknown pitch detection function selected" |
---|
| 46 | sys.exit(1) |
---|
| 47 | setattr(parser.values, option.dest, val) |
---|
| 48 | |
---|
| 49 | def check_pitchm_mode(option, opt, value, parser): |
---|
| 50 | """ utility function to convert a string to aubio_pitchdetection_mode """ |
---|
| 51 | nvalue = parser.rargs[0] |
---|
| 52 | if nvalue == 'freq' : |
---|
| 53 | setattr(parser.values, option.dest, aubio_pitchm_freq) |
---|
| 54 | elif nvalue == 'midi' : |
---|
| 55 | setattr(parser.values, option.dest, aubio_pitchm_midi) |
---|
| 56 | elif nvalue == 'cent' : |
---|
| 57 | setattr(parser.values, option.dest, aubio_pitchm_cent) |
---|
| 58 | elif nvalue == 'bin' : |
---|
| 59 | setattr(parser.values, option.dest, aubio_pitchm_bin) |
---|
| 60 | else: |
---|
| 61 | import sys |
---|
| 62 | print "error: unknown pitch detection output selected" |
---|
| 63 | sys.exit(1) |
---|
| 64 | |
---|
| 65 | |
---|
| 66 | def getonsets(filein,threshold=0.2,silence=-70.,bufsize=1024,hopsize=512, |
---|
| 67 | mode='dual',localmin=False,storefunc=False,derivate=False): |
---|
| 68 | frameread = 0 |
---|
| 69 | filei = sndfile(filein) |
---|
| 70 | channels = filei.channels() |
---|
| 71 | myvec = fvec(hopsize,channels) |
---|
| 72 | readsize = filei.read(hopsize,myvec) |
---|
| 73 | opick = onsetpick(bufsize,hopsize,channels,myvec,threshold, |
---|
| 74 | mode=mode,derivate=derivate) |
---|
| 75 | mylist = list() |
---|
| 76 | if localmin: |
---|
| 77 | ovalist = [0., 0., 0., 0., 0.] |
---|
| 78 | ofunclist = [] |
---|
| 79 | while(readsize): |
---|
| 80 | readsize = filei.read(hopsize,myvec) |
---|
| 81 | isonset,val = opick.do(myvec) |
---|
| 82 | if (aubio_silence_detection(myvec(),silence)): |
---|
| 83 | isonset=0 |
---|
| 84 | if localmin: |
---|
| 85 | if val > 0: ovalist.append(val) |
---|
| 86 | else: ovalist.append(0) |
---|
| 87 | ovalist.pop(0) |
---|
| 88 | if storefunc: |
---|
| 89 | ofunclist.append(val) |
---|
| 90 | if (isonset == 1): |
---|
| 91 | if localmin: |
---|
| 92 | i=len(ovalist)-1 |
---|
| 93 | # find local minima before peak |
---|
| 94 | while ovalist[i-1] < ovalist[i] and i > 0: |
---|
| 95 | i -= 1 |
---|
| 96 | now = (frameread+1-i) |
---|
| 97 | else: |
---|
| 98 | now = frameread |
---|
| 99 | if now > 0 : |
---|
| 100 | mylist.append(now) |
---|
| 101 | else: |
---|
| 102 | now = 0 |
---|
| 103 | mylist.append(now) |
---|
| 104 | frameread += 1 |
---|
| 105 | return mylist, ofunclist |
---|
| 106 | |
---|
| 107 | def cutfile(filein,slicetimes,zerothres=0.008,bufsize=1024,hopsize=512): |
---|
| 108 | frameread = 0 |
---|
| 109 | readsize = hopsize |
---|
| 110 | filei = sndfile(filein) |
---|
| 111 | framestep = hopsize/(filei.samplerate()+0.) |
---|
| 112 | channels = filei.channels() |
---|
| 113 | newname = "%s%s%09.5f%s%s" % (filein.split(".")[0].split("/")[-1],".", |
---|
| 114 | frameread*framestep,".",filein.split(".")[-1]) |
---|
| 115 | fileo = sndfile(newname,model=filei) |
---|
| 116 | myvec = fvec(hopsize,channels) |
---|
| 117 | mycopy = fvec(hopsize,channels) |
---|
| 118 | while(readsize==hopsize): |
---|
| 119 | readsize = filei.read(hopsize,myvec) |
---|
| 120 | # write to current file |
---|
| 121 | if len(slicetimes) and frameread >= slicetimes[0]: |
---|
| 122 | slicetimes.pop(0) |
---|
| 123 | # write up to 1st zero crossing |
---|
| 124 | zerocross = 0 |
---|
| 125 | while ( abs( myvec.get(zerocross,0) ) > zerothres ): |
---|
| 126 | zerocross += 1 |
---|
| 127 | writesize = fileo.write(zerocross,myvec) |
---|
| 128 | fromcross = 0 |
---|
| 129 | while (zerocross < readsize): |
---|
| 130 | for i in range(channels): |
---|
| 131 | mycopy.set(myvec.get(zerocross,i),fromcross,i) |
---|
| 132 | fromcross += 1 |
---|
| 133 | zerocross += 1 |
---|
| 134 | del fileo |
---|
| 135 | fileo = sndfile("%s%s%09.5f%s%s" % |
---|
| 136 | (filein.split(".")[0].split("/")[-1],".", |
---|
| 137 | frameread*framestep,".",filein.split(".")[-1]),model=filei) |
---|
| 138 | writesize = fileo.write(fromcross,mycopy) |
---|
| 139 | else: |
---|
| 140 | writesize = fileo.write(readsize,myvec) |
---|
| 141 | frameread += 1 |
---|
| 142 | del fileo |
---|
| 143 | |
---|
| 144 | |
---|
| 145 | def getsilences(filein,hopsize=512,silence=-70): |
---|
| 146 | frameread = 0 |
---|
| 147 | filei = sndfile(filein) |
---|
| 148 | srate = filei.samplerate() |
---|
| 149 | channels = filei.channels() |
---|
| 150 | myvec = fvec(hopsize,channels) |
---|
| 151 | readsize = filei.read(hopsize,myvec) |
---|
| 152 | mylist = [] |
---|
| 153 | wassilence = 0 |
---|
| 154 | while(readsize==hopsize): |
---|
| 155 | readsize = filei.read(hopsize,myvec) |
---|
| 156 | if (aubio_silence_detection(myvec(),silence)==1): |
---|
| 157 | if wassilence == 0: |
---|
| 158 | mylist.append(frameread) |
---|
| 159 | wassilence == 1 |
---|
| 160 | else: wassilence = 0 |
---|
| 161 | frameread += 1 |
---|
| 162 | return mylist |
---|
| 163 | |
---|
| 164 | def getpitch(filein,mode=aubio_pitch_mcomb,bufsize=1024,hopsize=512,omode=aubio_pitchm_freq, |
---|
| 165 | samplerate=44100.,silence=-70): |
---|
| 166 | frameread = 0 |
---|
| 167 | filei = sndfile(filein) |
---|
| 168 | srate = filei.samplerate() |
---|
| 169 | channels = filei.channels() |
---|
| 170 | myvec = fvec(hopsize,channels) |
---|
| 171 | readsize = filei.read(hopsize,myvec) |
---|
| 172 | pitchdet = pitchdetection(mode=mode,bufsize=bufsize,hopsize=hopsize, |
---|
| 173 | channels=channels,samplerate=srate,omode=omode) |
---|
| 174 | mylist = [] |
---|
| 175 | while(readsize==hopsize): |
---|
| 176 | readsize = filei.read(hopsize,myvec) |
---|
| 177 | freq = pitchdet(myvec) |
---|
| 178 | #print "%.3f %.2f" % (now,freq) |
---|
| 179 | if (aubio_silence_detection(myvec(),silence)!=1): |
---|
| 180 | mylist.append(freq) |
---|
| 181 | else: |
---|
| 182 | mylist.append(-1.) |
---|
| 183 | frameread += 1 |
---|
| 184 | return mylist |
---|
| 185 | |
---|