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-2009 Paul Brossier <piem@aubio.org> |
---|
7 | |
---|
8 | This file is part of aubio. |
---|
9 | |
---|
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. |
---|
14 | |
---|
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/>. |
---|
22 | """ |
---|
23 | |
---|
24 | |
---|
25 | def audio_to_array(filename): |
---|
26 | import aubio.aubioclass |
---|
27 | from numpy import arange |
---|
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 |
---|
43 | time = arange(len(data))*framestep |
---|
44 | return time,data |
---|
45 | |
---|
46 | def plot_audio(filenames, g, options): |
---|
47 | todraw = len(filenames) |
---|
48 | xorig = 0. |
---|
49 | xratio = 1./todraw |
---|
50 | g('set multiplot;') |
---|
51 | while (len(filenames)): |
---|
52 | time,data = audio_to_array(filenames.pop(0)) |
---|
53 | if todraw==1: |
---|
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)') |
---|
59 | g.ylabel('Amplitude') |
---|
60 | curplot = make_audio_plot(time,data) |
---|
61 | g('set size %f,%f;' % (options.xsize*xratio,options.ysize) ) |
---|
62 | g('set origin %f,0.;' % (xorig) ) |
---|
63 | g('set style data lines; \ |
---|
64 | set yrange [-1.:1.]; \ |
---|
65 | set xrange [0:%f]' % time[-1]) |
---|
66 | g.plot(curplot) |
---|
67 | xorig += options.xsize*xratio |
---|
68 | g('unset multiplot;') |
---|
69 | |
---|
70 | def audio_to_spec(filename,minf = 0, maxf = 0, lowthres = -20., |
---|
71 | bufsize= 8192, hopsize = 1024): |
---|
72 | from aubioclass import fvec,cvec,pvoc,sndfile |
---|
73 | from math import log10 |
---|
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) |
---|
80 | myfft = cvec(bufsize,channels) |
---|
81 | pv = pvoc(bufsize,hopsize,channels) |
---|
82 | data,time,freq = [],[],[] |
---|
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): |
---|
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): |
---|
98 | curpos = minf |
---|
99 | while (curpos < maxf): |
---|
100 | frame.append(max(lowthres,20.*log10(myfft.get(curpos,i)**2+0.00001))) |
---|
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 | |
---|
114 | def plot_spec(filename, g, options): |
---|
115 | import Gnuplot |
---|
116 | data,time,freq = audio_to_spec(filename, |
---|
117 | minf=options.minf,maxf=options.maxf, |
---|
118 | bufsize=options.bufsize,hopsize=options.hopsize) |
---|
119 | xorig = 0. |
---|
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)') |
---|
132 | g('set pm3d map') |
---|
133 | g('set palette rgbformulae -25,-24,-32') |
---|
134 | g('set cbtics 20') |
---|
135 | #g('set colorbox horizontal') |
---|
136 | g('set xrange [0.:%f]' % time[-1]) |
---|
137 | if options.log: |
---|
138 | g('set log y') |
---|
139 | g('set yrange [%f:%f]' % (max(10,options.minf),options.maxf)) |
---|
140 | else: |
---|
141 | g('set yrange [%f:%f]' % (options.minf,options.maxf)) |
---|
142 | g.splot(Gnuplot.GridData(data,time,freq, binary=1)) |
---|
143 | #xorig += 1./todraw |
---|
144 | |
---|
145 | def downsample_audio(time,data,maxpoints=10000): |
---|
146 | """ resample audio data to last only maxpoints """ |
---|
147 | from numpy import array, resize |
---|
148 | length = len(time) |
---|
149 | downsample = length/maxpoints |
---|
150 | if downsample == 0: downsample = 1 |
---|
151 | x = resize(array(time),length)[0:-1:downsample] |
---|
152 | y = resize(array(data),length)[0:-1:downsample] |
---|
153 | return x,y |
---|
154 | |
---|
155 | def make_audio_plot(time,data,maxpoints=10000): |
---|
156 | """ create gnuplot plot from an audio file """ |
---|
157 | import Gnuplot, Gnuplot.funcutils |
---|
158 | x,y = downsample_audio(time,data,maxpoints=maxpoints) |
---|
159 | return Gnuplot.Data(x,y,with_='lines') |
---|
160 | |
---|
161 | def make_audio_envelope(time,data,maxpoints=10000): |
---|
162 | """ create gnuplot plot from an audio file """ |
---|
163 | from numpy import array |
---|
164 | import Gnuplot, Gnuplot.funcutils |
---|
165 | bufsize = 500 |
---|
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))] |
---|
168 | x,y = downsample_audio(x,y,maxpoints=maxpoints) |
---|
169 | return Gnuplot.Data(x,y,with_='lines') |
---|
170 | |
---|
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") |
---|
179 | parser.add_option("--debug", |
---|
180 | action="store_true", dest="debug", default=False, |
---|
181 | help="use gnuplot debug mode") |
---|
182 | parser.add_option("--persist", |
---|
183 | action="store_false", dest="persist", default=True, |
---|
184 | help="do not use gnuplot persistant mode") |
---|
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") |
---|
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 |
---|
203 | if options: |
---|
204 | g = Gnuplot.Gnuplot(debug=options.debug, persist=options.persist) |
---|
205 | else: |
---|
206 | g = Gnuplot.Gnuplot(persist=1) |
---|
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) |
---|
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) |
---|
219 | if outplot != "stdout": |
---|
220 | g('set output \'%s%s\'' % (outplot,ext)) |
---|
221 | if options: g('set size %f,%f' % (options.xsize, options.ysize)) |
---|
222 | return g |
---|