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. |
---|
21 | """ |
---|
22 | |
---|
23 | |
---|
24 | __notesheight = 0.25 |
---|
25 | |
---|
26 | |
---|
27 | def audio_to_array(filename): |
---|
28 | import aubio.aubioclass |
---|
29 | import numarray |
---|
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 |
---|
45 | time = numarray.arange(len(data))*framestep |
---|
46 | return time,data |
---|
47 | |
---|
48 | def plot_audio(filenames, g, start=0, end=None, noaxis=None,xsize=1.,ysize=1.): |
---|
49 | d = [] |
---|
50 | todraw = len(filenames) |
---|
51 | xorig = 0. |
---|
52 | xratio = 1./todraw |
---|
53 | #g('set rmargin 7') |
---|
54 | g('set multiplot;') |
---|
55 | while (len(filenames)): |
---|
56 | time,data = audio_to_array(filenames.pop(0)) |
---|
57 | if not noaxis and todraw==1: |
---|
58 | if max(time) < 1.: |
---|
59 | time = [t*1000. for t in time] |
---|
60 | g.xlabel('Time (ms)') |
---|
61 | else: |
---|
62 | g.xlabel('Time (s)') |
---|
63 | g.ylabel('Amplitude') |
---|
64 | d.append(make_audio_plot(time,data)) |
---|
65 | g('set size %f,%f;' % (xsize*xratio,ysize) ) |
---|
66 | g('set origin %f,0.;' % (xorig) ) |
---|
67 | g('set style data lines; \ |
---|
68 | set yrange [-1.:1.]; \ |
---|
69 | set xrange [0:%f]' % time[-1]) |
---|
70 | g.plot(d.pop(0)) |
---|
71 | xorig += xsize*xratio |
---|
72 | g('unset multiplot;') |
---|
73 | |
---|
74 | def audio_to_spec(filename,minf = 0, maxf = 0, lowthres = -20., |
---|
75 | bufsize= 8192, hopsize = 1024): |
---|
76 | from aubioclass import fvec,cvec,pvoc,sndfile |
---|
77 | from math import log10 |
---|
78 | filei = sndfile(filename) |
---|
79 | srate = float(filei.samplerate()) |
---|
80 | framestep = hopsize/srate |
---|
81 | freqstep = srate/bufsize |
---|
82 | channels = filei.channels() |
---|
83 | myvec = fvec(hopsize,channels) |
---|
84 | myfft = cvec(bufsize,channels) |
---|
85 | pv = pvoc(bufsize,hopsize,channels) |
---|
86 | data,time,freq = [],[],[] |
---|
87 | |
---|
88 | if maxf == 0.: maxf = bufsize/2 |
---|
89 | else: maxf = int(maxf/freqstep) |
---|
90 | if minf: minf = int(minf/freqstep) |
---|
91 | else: minf = 0 |
---|
92 | |
---|
93 | for f in range(minf,maxf): |
---|
94 | freq.append(f*freqstep) |
---|
95 | readsize = hopsize |
---|
96 | frameread = 0 |
---|
97 | while (readsize==hopsize): |
---|
98 | readsize = filei.read(hopsize,myvec) |
---|
99 | pv.do(myvec,myfft) |
---|
100 | frame = [] |
---|
101 | i = 0 #for i in range(channels): |
---|
102 | curpos = minf |
---|
103 | while (curpos < maxf): |
---|
104 | frame.append(max(lowthres,20.*log10(myfft.get(curpos,i)**2+0.00001))) |
---|
105 | curpos+=1 |
---|
106 | time.append(frameread*framestep) |
---|
107 | data.append(frame) |
---|
108 | frameread += 1 |
---|
109 | # crop data if unfinished frames |
---|
110 | if len(data[-1]) != len(data[0]): |
---|
111 | data = data[0:-2] |
---|
112 | time = time[0:-2] |
---|
113 | # verify size consistency |
---|
114 | assert len(data) == len(time) |
---|
115 | assert len(data[0]) == len(freq) |
---|
116 | return data,time,freq |
---|
117 | |
---|
118 | def plot_spec(filename, g, start=0, end=None, noaxis=None,log=1, minf=0, maxf= 0, xsize = 1., ysize = 1.,bufsize=8192, hopsize=1024): |
---|
119 | import Gnuplot |
---|
120 | data,time,freq = audio_to_spec(filename,minf=minf,maxf=maxf,bufsize=bufsize,hopsize=hopsize) |
---|
121 | xorig = 0. |
---|
122 | if not noaxis: |
---|
123 | if max(time) < 1.: |
---|
124 | time = [t*1000. for t in time] |
---|
125 | g.xlabel('Time (ms)') |
---|
126 | else: |
---|
127 | g.xlabel('Time (s)') |
---|
128 | if xsize < 0.5 and not log and max(time) > 1.: |
---|
129 | freq = [f/1000. for f in freq] |
---|
130 | minf /= 1000. |
---|
131 | maxf /= 1000. |
---|
132 | g.ylabel('Frequency (kHz)') |
---|
133 | else: |
---|
134 | g.ylabel('Frequency (Hz)') |
---|
135 | g('set pm3d map') |
---|
136 | g('set palette rgbformulae -25,-24,-32') |
---|
137 | #g('set lmargin 4') |
---|
138 | g('set cbtics 20') |
---|
139 | #g('set colorbox horizontal') |
---|
140 | g('set xrange [0.:%f]' % time[-1]) |
---|
141 | if log: |
---|
142 | g('set log y') |
---|
143 | g('set yrange [%f:%f]' % (max(10,minf),maxf)) |
---|
144 | else: |
---|
145 | g('set yrange [%f:%f]' % (minf,maxf)) |
---|
146 | g.splot(Gnuplot.GridData(data,time,freq, binary=1)) |
---|
147 | #xorig += 1./todraw |
---|
148 | |
---|
149 | def downsample_audio(time,data,maxpoints=10000): |
---|
150 | """ resample audio data to last only maxpoints """ |
---|
151 | import numarray |
---|
152 | length = len(time) |
---|
153 | downsample = length/maxpoints |
---|
154 | if downsample == 0: downsample = 1 |
---|
155 | x = numarray.array(time).resize(length)[0:-1:downsample] |
---|
156 | y = numarray.array(data).resize(length)[0:-1:downsample] |
---|
157 | return x,y |
---|
158 | |
---|
159 | def make_audio_plot(time,data,maxpoints=10000): |
---|
160 | """ create gnuplot plot from an audio file """ |
---|
161 | import Gnuplot, Gnuplot.funcutils |
---|
162 | x,y = downsample_audio(time,data,maxpoints=maxpoints) |
---|
163 | return Gnuplot.Data(x,y,with='lines') |
---|
164 | |
---|
165 | def gnuplot_addargs(parser): |
---|
166 | """ add common gnuplot argument to OptParser object """ |
---|
167 | parser.add_option("-x","--xsize", |
---|
168 | action="store", dest="xsize", default=1., |
---|
169 | type='float',help="define xsize for plot") |
---|
170 | parser.add_option("-y","--ysize", |
---|
171 | action="store", dest="ysize", default=1., |
---|
172 | type='float',help="define ysize for plot") |
---|
173 | parser.add_option("-d","--debug", |
---|
174 | action="store_true", dest="debug", default=False, |
---|
175 | help="use gnuplot debug mode") |
---|
176 | parser.add_option("-p","--persist", |
---|
177 | action="store_false", dest="persist", default=True, |
---|
178 | help="do not use gnuplot persistant mode") |
---|
179 | parser.add_option("-O","--outplot", |
---|
180 | action="store", dest="outplot", default=None, |
---|
181 | help="save plot to output.{ps,png}") |
---|
182 | |
---|
183 | def gnuplot_create(outplot='',extension='', options=None): |
---|
184 | import Gnuplot |
---|
185 | g = Gnuplot.Gnuplot(debug=options.debug, persist=options.persist) |
---|
186 | if not extension or not outplot: return g |
---|
187 | if extension == 'ps': ext, extension = '.ps' , 'postscript' |
---|
188 | elif extension == 'eps': ext, extension = '.eps' , 'postscript enhanced' |
---|
189 | elif extension == 'epsc': ext, extension = '.eps' , 'postscript enhanced color' |
---|
190 | elif extension == 'png': ext, extension = '.png', 'png' |
---|
191 | elif extension == 'svg': ext, extension = '.svg', 'svg' |
---|
192 | else: exit("ERR: unknown plot extension") |
---|
193 | g('set terminal %s' % extension) |
---|
194 | if outplot != "stdout": |
---|
195 | g('set output \'%s%s\'' % (outplot,ext)) |
---|
196 | g('set size %f,%f' % (options.xsize, options.ysize)) |
---|
197 | return g |
---|