[96fb8ad] | 1 | """Copyright (C) 2004 Paul Brossier <piem@altern.org> |
---|
| 2 | print aubio.__LICENSE__ for the terms of use |
---|
| 3 | """ |
---|
| 4 | |
---|
| 5 | __LICENSE__ = """\ |
---|
[20d8266] | 6 | Copyright (C) 2004-2009 Paul Brossier <piem@aubio.org> |
---|
[96fb8ad] | 7 | |
---|
[20d8266] | 8 | This file is part of aubio. |
---|
[96fb8ad] | 9 | |
---|
[20d8266] | 10 | aubio is free software: you can redistribute it and/or modify |
---|
| 11 | it under the terms of the GNU General Public License as published by |
---|
| 12 | the Free Software Foundation, either version 3 of the License, or |
---|
| 13 | (at your option) any later version. |
---|
[96fb8ad] | 14 | |
---|
[20d8266] | 15 | aubio is distributed in the hope that it will be useful, |
---|
| 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 18 | GNU General Public License for more details. |
---|
| 19 | |
---|
| 20 | You should have received a copy of the GNU General Public License |
---|
| 21 | along with aubio. If not, see <http://www.gnu.org/licenses/>. |
---|
[65ebf30] | 22 | """ |
---|
[96fb8ad] | 23 | |
---|
| 24 | |
---|
[65ebf30] | 25 | def audio_to_array(filename): |
---|
| 26 | import aubio.aubioclass |
---|
[55ed67a] | 27 | from numpy import arange |
---|
[65ebf30] | 28 | hopsize = 2048 |
---|
| 29 | filei = aubio.aubioclass.sndfile(filename) |
---|
| 30 | framestep = 1/(filei.samplerate()+0.) |
---|
| 31 | channels = filei.channels() |
---|
| 32 | myvec = aubio.aubioclass.fvec(hopsize,channels) |
---|
| 33 | data = [] |
---|
| 34 | readsize = hopsize |
---|
| 35 | while (readsize==hopsize): |
---|
| 36 | readsize = filei.read(hopsize,myvec) |
---|
| 37 | #for i in range(channels): |
---|
| 38 | i = 0 |
---|
| 39 | curpos = 0 |
---|
| 40 | while (curpos < readsize): |
---|
| 41 | data.append(myvec.get(curpos,i)) |
---|
| 42 | curpos+=1 |
---|
[55ed67a] | 43 | time = arange(len(data))*framestep |
---|
[65ebf30] | 44 | return time,data |
---|
| 45 | |
---|
[33cf541] | 46 | def plot_audio(filenames, g, options): |
---|
[65ebf30] | 47 | todraw = len(filenames) |
---|
| 48 | xorig = 0. |
---|
[da6d9af] | 49 | xratio = 1./todraw |
---|
[e5cc1ea] | 50 | g('set multiplot;') |
---|
[65ebf30] | 51 | while (len(filenames)): |
---|
[431e9d8] | 52 | time,data = audio_to_array(filenames.pop(0)) |
---|
[33cf541] | 53 | if todraw==1: |
---|
[da6d9af] | 54 | if max(time) < 1.: |
---|
| 55 | time = [t*1000. for t in time] |
---|
| 56 | g.xlabel('Time (ms)') |
---|
| 57 | else: |
---|
| 58 | g.xlabel('Time (s)') |
---|
[65ebf30] | 59 | g.ylabel('Amplitude') |
---|
[e81c045] | 60 | curplot = make_audio_plot(time,data) |
---|
[33cf541] | 61 | g('set size %f,%f;' % (options.xsize*xratio,options.ysize) ) |
---|
[e5cc1ea] | 62 | g('set origin %f,0.;' % (xorig) ) |
---|
| 63 | g('set style data lines; \ |
---|
[65ebf30] | 64 | set yrange [-1.:1.]; \ |
---|
[733c2f8] | 65 | set xrange [0:%f]' % time[-1]) |
---|
[e81c045] | 66 | g.plot(curplot) |
---|
[33cf541] | 67 | xorig += options.xsize*xratio |
---|
[e5cc1ea] | 68 | g('unset multiplot;') |
---|
[96fb8ad] | 69 | |
---|
[e638bcf] | 70 | def audio_to_spec(filename,minf = 0, maxf = 0, lowthres = -20., |
---|
| 71 | bufsize= 8192, hopsize = 1024): |
---|
[431e9d8] | 72 | from aubioclass import fvec,cvec,pvoc,sndfile |
---|
[da6d9af] | 73 | from math import log10 |
---|
[431e9d8] | 74 | filei = sndfile(filename) |
---|
| 75 | srate = float(filei.samplerate()) |
---|
| 76 | framestep = hopsize/srate |
---|
| 77 | freqstep = srate/bufsize |
---|
| 78 | channels = filei.channels() |
---|
| 79 | myvec = fvec(hopsize,channels) |
---|
[da6d9af] | 80 | myfft = cvec(bufsize,channels) |
---|
| 81 | pv = pvoc(bufsize,hopsize,channels) |
---|
[431e9d8] | 82 | data,time,freq = [],[],[] |
---|
[da6d9af] | 83 | |
---|
| 84 | if maxf == 0.: maxf = bufsize/2 |
---|
| 85 | else: maxf = int(maxf/freqstep) |
---|
| 86 | if minf: minf = int(minf/freqstep) |
---|
| 87 | else: minf = 0 |
---|
| 88 | |
---|
| 89 | for f in range(minf,maxf): |
---|
[431e9d8] | 90 | freq.append(f*freqstep) |
---|
| 91 | readsize = hopsize |
---|
| 92 | frameread = 0 |
---|
| 93 | while (readsize==hopsize): |
---|
| 94 | readsize = filei.read(hopsize,myvec) |
---|
| 95 | pv.do(myvec,myfft) |
---|
| 96 | frame = [] |
---|
| 97 | i = 0 #for i in range(channels): |
---|
[da6d9af] | 98 | curpos = minf |
---|
| 99 | while (curpos < maxf): |
---|
| 100 | frame.append(max(lowthres,20.*log10(myfft.get(curpos,i)**2+0.00001))) |
---|
[431e9d8] | 101 | curpos+=1 |
---|
| 102 | time.append(frameread*framestep) |
---|
| 103 | data.append(frame) |
---|
| 104 | frameread += 1 |
---|
| 105 | # crop data if unfinished frames |
---|
| 106 | if len(data[-1]) != len(data[0]): |
---|
| 107 | data = data[0:-2] |
---|
| 108 | time = time[0:-2] |
---|
| 109 | # verify size consistency |
---|
| 110 | assert len(data) == len(time) |
---|
| 111 | assert len(data[0]) == len(freq) |
---|
| 112 | return data,time,freq |
---|
| 113 | |
---|
[33cf541] | 114 | def plot_spec(filename, g, options): |
---|
[431e9d8] | 115 | import Gnuplot |
---|
[33cf541] | 116 | data,time,freq = audio_to_spec(filename, |
---|
| 117 | minf=options.minf,maxf=options.maxf, |
---|
| 118 | bufsize=options.bufsize,hopsize=options.hopsize) |
---|
[431e9d8] | 119 | xorig = 0. |
---|
[33cf541] | 120 | if max(time) < 1.: |
---|
| 121 | time = [t*1000. for t in time] |
---|
| 122 | g.xlabel('Time (ms)') |
---|
| 123 | else: |
---|
| 124 | g.xlabel('Time (s)') |
---|
| 125 | if options.xsize < 0.5 and not options.log and max(time) > 1.: |
---|
| 126 | freq = [f/1000. for f in freq] |
---|
| 127 | options.minf /= 1000. |
---|
| 128 | options.maxf /= 1000. |
---|
| 129 | g.ylabel('Frequency (kHz)') |
---|
| 130 | else: |
---|
| 131 | g.ylabel('Frequency (Hz)') |
---|
[da6d9af] | 132 | g('set pm3d map') |
---|
| 133 | g('set palette rgbformulae -25,-24,-32') |
---|
[e5cc1ea] | 134 | g('set cbtics 20') |
---|
[da6d9af] | 135 | #g('set colorbox horizontal') |
---|
| 136 | g('set xrange [0.:%f]' % time[-1]) |
---|
[33cf541] | 137 | if options.log: |
---|
[da6d9af] | 138 | g('set log y') |
---|
[33cf541] | 139 | g('set yrange [%f:%f]' % (max(10,options.minf),options.maxf)) |
---|
[e5cc1ea] | 140 | else: |
---|
[33cf541] | 141 | g('set yrange [%f:%f]' % (options.minf,options.maxf)) |
---|
[9b138a8] | 142 | g.splot(Gnuplot.GridData(data,time,freq, binary=1)) |
---|
[7538ac5] | 143 | #xorig += 1./todraw |
---|
[431e9d8] | 144 | |
---|
[2bd1a2a] | 145 | def downsample_audio(time,data,maxpoints=10000): |
---|
[32fa8e8] | 146 | """ resample audio data to last only maxpoints """ |
---|
[55ed67a] | 147 | from numpy import array, resize |
---|
[32fa8e8] | 148 | length = len(time) |
---|
| 149 | downsample = length/maxpoints |
---|
| 150 | if downsample == 0: downsample = 1 |
---|
[55ed67a] | 151 | x = resize(array(time),length)[0:-1:downsample] |
---|
| 152 | y = resize(array(data),length)[0:-1:downsample] |
---|
[32fa8e8] | 153 | return x,y |
---|
[2bd1a2a] | 154 | |
---|
[a0fd4e4] | 155 | def make_audio_plot(time,data,maxpoints=10000): |
---|
[32fa8e8] | 156 | """ create gnuplot plot from an audio file """ |
---|
| 157 | import Gnuplot, Gnuplot.funcutils |
---|
| 158 | x,y = downsample_audio(time,data,maxpoints=maxpoints) |
---|
[55ed67a] | 159 | return Gnuplot.Data(x,y,with_='lines') |
---|
[80c0417] | 160 | |
---|
[e81c045] | 161 | def make_audio_envelope(time,data,maxpoints=10000): |
---|
| 162 | """ create gnuplot plot from an audio file """ |
---|
[55ed67a] | 163 | from numpy import array |
---|
[e81c045] | 164 | import Gnuplot, Gnuplot.funcutils |
---|
| 165 | bufsize = 500 |
---|
[55ed67a] | 166 | x = [i.mean() for i in resize(array(time), (len(time)/bufsize,bufsize))] |
---|
| 167 | y = [i.mean() for i in resize(array(data), (len(time)/bufsize,bufsize))] |
---|
[e81c045] | 168 | x,y = downsample_audio(x,y,maxpoints=maxpoints) |
---|
[55ed67a] | 169 | return Gnuplot.Data(x,y,with_='lines') |
---|
[e81c045] | 170 | |
---|
[0ab19df] | 171 | def gnuplot_addargs(parser): |
---|
| 172 | """ add common gnuplot argument to OptParser object """ |
---|
| 173 | parser.add_option("-x","--xsize", |
---|
| 174 | action="store", dest="xsize", default=1., |
---|
| 175 | type='float',help="define xsize for plot") |
---|
| 176 | parser.add_option("-y","--ysize", |
---|
| 177 | action="store", dest="ysize", default=1., |
---|
| 178 | type='float',help="define ysize for plot") |
---|
[33cf541] | 179 | parser.add_option("--debug", |
---|
[0ab19df] | 180 | action="store_true", dest="debug", default=False, |
---|
| 181 | help="use gnuplot debug mode") |
---|
[33cf541] | 182 | parser.add_option("--persist", |
---|
[0ab19df] | 183 | action="store_false", dest="persist", default=True, |
---|
| 184 | help="do not use gnuplot persistant mode") |
---|
[33cf541] | 185 | parser.add_option("--lmargin", |
---|
| 186 | action="store", dest="lmargin", default=None, |
---|
| 187 | type='int',help="define left margin for plot") |
---|
| 188 | parser.add_option("--rmargin", |
---|
| 189 | action="store", dest="rmargin", default=None, |
---|
| 190 | type='int',help="define right margin for plot") |
---|
| 191 | parser.add_option("--bmargin", |
---|
| 192 | action="store", dest="bmargin", default=None, |
---|
| 193 | type='int',help="define bottom margin for plot") |
---|
| 194 | parser.add_option("--tmargin", |
---|
| 195 | action="store", dest="tmargin", default=None, |
---|
| 196 | type='int',help="define top margin for plot") |
---|
[0ab19df] | 197 | parser.add_option("-O","--outplot", |
---|
| 198 | action="store", dest="outplot", default=None, |
---|
| 199 | help="save plot to output.{ps,png}") |
---|
| 200 | |
---|
| 201 | def gnuplot_create(outplot='',extension='', options=None): |
---|
| 202 | import Gnuplot |
---|
[e81c045] | 203 | if options: |
---|
| 204 | g = Gnuplot.Gnuplot(debug=options.debug, persist=options.persist) |
---|
| 205 | else: |
---|
| 206 | g = Gnuplot.Gnuplot(persist=1) |
---|
[0ab19df] | 207 | if not extension or not outplot: return g |
---|
| 208 | if extension == 'ps': ext, extension = '.ps' , 'postscript' |
---|
| 209 | elif extension == 'eps': ext, extension = '.eps' , 'postscript enhanced' |
---|
| 210 | elif extension == 'epsc': ext, extension = '.eps' , 'postscript enhanced color' |
---|
| 211 | elif extension == 'png': ext, extension = '.png', 'png' |
---|
| 212 | elif extension == 'svg': ext, extension = '.svg', 'svg' |
---|
| 213 | else: exit("ERR: unknown plot extension") |
---|
| 214 | g('set terminal %s' % extension) |
---|
[e81c045] | 215 | if options and options.lmargin: g('set lmargin %i' % options.lmargin) |
---|
| 216 | if options and options.rmargin: g('set rmargin %i' % options.rmargin) |
---|
| 217 | if options and options.bmargin: g('set bmargin %i' % options.bmargin) |
---|
| 218 | if options and options.tmargin: g('set tmargin %i' % options.tmargin) |
---|
[0ab19df] | 219 | if outplot != "stdout": |
---|
| 220 | g('set output \'%s%s\'' % (outplot,ext)) |
---|
[e81c045] | 221 | if options: g('set size %f,%f' % (options.xsize, options.ysize)) |
---|
[0ab19df] | 222 | return g |
---|