[96fb8ad] | 1 | """Copyright (C) 2004 Paul Brossier <piem@altern.org> |
---|
| 2 | print aubio.__LICENSE__ for the terms of use |
---|
| 3 | """ |
---|
| 4 | |
---|
| 5 | __LICENSE__ = """\ |
---|
| 6 | Copyright (C) 2004 Paul Brossier <piem@altern.org> |
---|
| 7 | |
---|
| 8 | This program is free software; you can redistribute it and/or modify |
---|
| 9 | it under the terms of the GNU General Public License as published by |
---|
| 10 | the Free Software Foundation; either version 2 of the License, or |
---|
| 11 | (at your option) any later version. |
---|
| 12 | |
---|
| 13 | This program is distributed in the hope that it will be useful, |
---|
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 16 | GNU General Public License for more details. |
---|
| 17 | |
---|
| 18 | You should have received a copy of the GNU General Public License |
---|
| 19 | along with this program; if not, write to the Free Software |
---|
| 20 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
[65ebf30] | 21 | """ |
---|
[96fb8ad] | 22 | |
---|
| 23 | |
---|
[733c2f8] | 24 | __notesheight = 0.25 |
---|
[96fb8ad] | 25 | |
---|
| 26 | |
---|
[65ebf30] | 27 | def audio_to_array(filename): |
---|
| 28 | import aubio.aubioclass |
---|
[733c2f8] | 29 | import numarray |
---|
[65ebf30] | 30 | hopsize = 2048 |
---|
| 31 | filei = aubio.aubioclass.sndfile(filename) |
---|
| 32 | framestep = 1/(filei.samplerate()+0.) |
---|
| 33 | channels = filei.channels() |
---|
| 34 | myvec = aubio.aubioclass.fvec(hopsize,channels) |
---|
| 35 | data = [] |
---|
| 36 | readsize = hopsize |
---|
| 37 | while (readsize==hopsize): |
---|
| 38 | readsize = filei.read(hopsize,myvec) |
---|
| 39 | #for i in range(channels): |
---|
| 40 | i = 0 |
---|
| 41 | curpos = 0 |
---|
| 42 | while (curpos < readsize): |
---|
| 43 | data.append(myvec.get(curpos,i)) |
---|
| 44 | curpos+=1 |
---|
[733c2f8] | 45 | time = numarray.arange(len(data))*framestep |
---|
[65ebf30] | 46 | return time,data |
---|
| 47 | |
---|
[da6d9af] | 48 | def plot_audio(filenames, fileout=None, start=0, end=None, noaxis=None,xsize=1.,ysize=1.): |
---|
[733c2f8] | 49 | g = gnuplot_init(fileout) |
---|
[65ebf30] | 50 | d = [] |
---|
| 51 | todraw = len(filenames) |
---|
| 52 | xorig = 0. |
---|
[da6d9af] | 53 | xratio = 1./todraw |
---|
| 54 | g.gnuplot('set size %f,%f;' % (xsize,ysize) ) |
---|
[65ebf30] | 55 | g.gnuplot('set multiplot;') |
---|
| 56 | while (len(filenames)): |
---|
[431e9d8] | 57 | time,data = audio_to_array(filenames.pop(0)) |
---|
[65ebf30] | 58 | if not noaxis and todraw==1: |
---|
[da6d9af] | 59 | if max(time) < 1.: |
---|
| 60 | time = [t*1000. for t in time] |
---|
| 61 | g.xlabel('Time (ms)') |
---|
| 62 | else: |
---|
| 63 | g.xlabel('Time (s)') |
---|
[65ebf30] | 64 | g.ylabel('Amplitude') |
---|
[da6d9af] | 65 | d.append(make_audio_plot(time,data)) |
---|
| 66 | g.gnuplot('set size %f,%f;' % (xsize*xratio,ysize) ) |
---|
[65ebf30] | 67 | g.gnuplot('set origin %f,0.;' % (xorig) ) |
---|
| 68 | g.gnuplot('set style data lines; \ |
---|
| 69 | set yrange [-1.:1.]; \ |
---|
[733c2f8] | 70 | set xrange [0:%f]' % time[-1]) |
---|
[65ebf30] | 71 | g.plot(d.pop(0)) |
---|
[da6d9af] | 72 | xorig += xsize*xratio |
---|
[65ebf30] | 73 | g.gnuplot('unset multiplot;') |
---|
[96fb8ad] | 74 | |
---|
[da6d9af] | 75 | def audio_to_spec(filename,minf = 0, maxf = 0, lowthres = 0.): |
---|
[431e9d8] | 76 | from aubioclass import fvec,cvec,pvoc,sndfile |
---|
[da6d9af] | 77 | from math import log10 |
---|
| 78 | bufsize = 8192 |
---|
| 79 | hopsize = bufsize/8 # could depend on filelength |
---|
[431e9d8] | 80 | filei = sndfile(filename) |
---|
| 81 | srate = float(filei.samplerate()) |
---|
| 82 | framestep = hopsize/srate |
---|
| 83 | freqstep = srate/bufsize |
---|
| 84 | channels = filei.channels() |
---|
| 85 | myvec = fvec(hopsize,channels) |
---|
[da6d9af] | 86 | myfft = cvec(bufsize,channels) |
---|
| 87 | pv = pvoc(bufsize,hopsize,channels) |
---|
[431e9d8] | 88 | data,time,freq = [],[],[] |
---|
[da6d9af] | 89 | |
---|
| 90 | if maxf == 0.: maxf = bufsize/2 |
---|
| 91 | else: maxf = int(maxf/freqstep) |
---|
| 92 | if minf: minf = int(minf/freqstep) |
---|
| 93 | else: minf = 0 |
---|
| 94 | |
---|
| 95 | for f in range(minf,maxf): |
---|
[431e9d8] | 96 | freq.append(f*freqstep) |
---|
| 97 | readsize = hopsize |
---|
| 98 | frameread = 0 |
---|
| 99 | while (readsize==hopsize): |
---|
| 100 | readsize = filei.read(hopsize,myvec) |
---|
| 101 | pv.do(myvec,myfft) |
---|
| 102 | frame = [] |
---|
| 103 | i = 0 #for i in range(channels): |
---|
[da6d9af] | 104 | curpos = minf |
---|
| 105 | while (curpos < maxf): |
---|
| 106 | frame.append(max(lowthres,20.*log10(myfft.get(curpos,i)**2+0.00001))) |
---|
[431e9d8] | 107 | curpos+=1 |
---|
| 108 | time.append(frameread*framestep) |
---|
| 109 | data.append(frame) |
---|
| 110 | frameread += 1 |
---|
| 111 | # crop data if unfinished frames |
---|
| 112 | if len(data[-1]) != len(data[0]): |
---|
| 113 | data = data[0:-2] |
---|
| 114 | time = time[0:-2] |
---|
| 115 | # verify size consistency |
---|
| 116 | assert len(data) == len(time) |
---|
| 117 | assert len(data[0]) == len(freq) |
---|
| 118 | return data,time,freq |
---|
| 119 | |
---|
[da6d9af] | 120 | def plot_spec(filename, outplot='',extension='', fileout=None, start=0, end=None, noaxis=None,log=1, minf=0, maxf= 0, xsize = 1., ysize = 1.): |
---|
[431e9d8] | 121 | import Gnuplot |
---|
| 122 | g = gnuplot_create(outplot,extension) |
---|
[da6d9af] | 123 | data,time,freq = audio_to_spec(filename,minf=minf,maxf=maxf) |
---|
[431e9d8] | 124 | xorig = 0. |
---|
[7538ac5] | 125 | if not noaxis: |
---|
[431e9d8] | 126 | g.xlabel('Time (s)') |
---|
| 127 | g.ylabel('Frequency (Hz)') |
---|
[da6d9af] | 128 | g('set size %f,%f' % (xsize, ysize)) |
---|
| 129 | g('set pm3d map') |
---|
| 130 | g('set palette rgbformulae -25,-24,-32') |
---|
| 131 | #g('set colorbox horizontal') |
---|
| 132 | g('set xrange [0.:%f]' % time[-1]) |
---|
| 133 | g('set yrange [%f:%f]' % (minf,maxf)) |
---|
[431e9d8] | 134 | if log: |
---|
[da6d9af] | 135 | g('set yrange [%f:%f]' % (max(10,minf),maxf)) |
---|
| 136 | g('set log y') |
---|
| 137 | g.splot(Gnuplot.GridData(data,time,freq, binary=1, title='mag. (dB)')) |
---|
[7538ac5] | 138 | #xorig += 1./todraw |
---|
[431e9d8] | 139 | |
---|
[2bd1a2a] | 140 | def downsample_audio(time,data,maxpoints=10000): |
---|
[431e9d8] | 141 | """ resample audio data to last only maxpoints """ |
---|
[2bd1a2a] | 142 | import numarray |
---|
| 143 | length = len(time) |
---|
| 144 | downsample = length/maxpoints |
---|
| 145 | if downsample == 0: downsample = 1 |
---|
| 146 | x = numarray.array(time).resize(length)[0:-1:downsample] |
---|
| 147 | y = numarray.array(data).resize(length)[0:-1:downsample] |
---|
| 148 | return x,y |
---|
| 149 | |
---|
[a0fd4e4] | 150 | def make_audio_plot(time,data,maxpoints=10000): |
---|
| 151 | """ create gnuplot plot from an audio file """ |
---|
[733c2f8] | 152 | import numarray |
---|
| 153 | import Gnuplot, Gnuplot.funcutils |
---|
[a0fd4e4] | 154 | length = len(time) |
---|
| 155 | downsample = length/maxpoints |
---|
| 156 | if downsample == 0: downsample = 1 |
---|
[733c2f8] | 157 | x = numarray.array(time).resize(length)[0:-1:downsample] |
---|
| 158 | y = numarray.array(data).resize(length)[0:-1:downsample] |
---|
[a0fd4e4] | 159 | return Gnuplot.Data(x,y,with='lines') |
---|
[80c0417] | 160 | |
---|
[733c2f8] | 161 | def gnuplot_init(outplot,debug=0,persist=1): |
---|
| 162 | # prepare the plot |
---|
[0baafcf] | 163 | import Gnuplot |
---|
[733c2f8] | 164 | g = Gnuplot.Gnuplot(debug=debug, persist=persist) |
---|
| 165 | if outplot == 'stdout': |
---|
| 166 | g("set terminal png fontfile 'p052023l.pfb'") |
---|
| 167 | #g('set output \'%s\'' % outplot) |
---|
| 168 | elif outplot: |
---|
| 169 | extension = outplot.split('.')[-1] |
---|
| 170 | if extension == 'ps': extension = 'postscript' |
---|
| 171 | g('set terminal %s' % extension) |
---|
| 172 | g('set output \'%s\'' % outplot) |
---|
| 173 | return g |
---|
[0baafcf] | 174 | |
---|
| 175 | def gnuplot_create(outplot='',extension='',debug=0,persist=1): |
---|
| 176 | import Gnuplot |
---|
| 177 | g = Gnuplot.Gnuplot(debug=debug, persist=persist) |
---|
| 178 | if not extension or not outplot: return g |
---|
| 179 | if extension == 'ps': ext, extension = '.ps' , 'postscript' |
---|
[da6d9af] | 180 | elif extension == 'eps': ext, extension = '.eps' , 'postscript enhanced' |
---|
| 181 | elif extension == 'epsc': ext, extension = '.eps' , 'postscript enhanced color' |
---|
[0baafcf] | 182 | elif extension == 'png': ext, extension = '.png', 'png' |
---|
| 183 | elif extension == 'svg': ext, extension = '.svg', 'svg' |
---|
| 184 | else: exit("ERR: unknown plot extension") |
---|
| 185 | g('set terminal %s' % extension) |
---|
| 186 | if outplot != "stdout": |
---|
[7538ac5] | 187 | g('set output \'%s%s\'' % (outplot,ext)) |
---|
[0baafcf] | 188 | return g |
---|