WaveformOversampler.cpp
Go to the documentation of this file.
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2 
3 /*
4  Sonic Visualiser
5  An audio file viewer and annotation editor.
6  Centre for Digital Music, Queen Mary, University of London.
7 
8  This program is free software; you can redistribute it and/or
9  modify it under the terms of the GNU General Public License as
10  published by the Free Software Foundation; either version 2 of the
11  License, or (at your option) any later version. See the file
12  COPYING included with this distribution for more information.
13 */
14 
15 #include "WaveformOversampler.h"
16 
17 #include "base/Profiler.h"
18 
20 
23  int channel,
24  sv_frame_t sourceStartFrame,
25  sv_frame_t sourceFrameCount,
26  int oversampleBy)
27 {
28  Profiler profiler("WaveformOversampler::getOversampledData");
29 
30  // Oversampled at a fixed ratio of m_filterRatio
31  floatvec_t fixedRatio = getFixedRatioData(source, channel,
32  sourceStartFrame,
33  sourceFrameCount);
34  sv_frame_t fixedCount = fixedRatio.size();
35  sv_frame_t targetCount = (fixedCount / m_filterRatio) * oversampleBy;
36 
37  // And apply linear interpolation to the desired factor
38 
39  floatvec_t result(targetCount, 0.f);
40 
41  for (int i = 0; i < targetCount; ++i) {
42  double pos = (double(i) / oversampleBy) * m_filterRatio;
43  double diff = pos - floor(pos);
44  int ix = int(floor(pos));
45  double interpolated = (1.0 - diff) * fixedRatio[ix];
46  if (in_range_for(fixedRatio, ix + 1)) {
47  interpolated += diff * fixedRatio[ix + 1];
48  }
49  result[i] = float(interpolated);
50  }
51 
52  return result;
53 }
54 
57  int channel,
58  sv_frame_t sourceStartFrame,
59  sv_frame_t sourceFrameCount)
60 {
61  Profiler profiler("WaveformOversampler::getFixedRatioData");
62 
63  sv_frame_t sourceLength = source.getEndFrame();
64 
65  if (sourceStartFrame + sourceFrameCount > sourceLength) {
66  sourceFrameCount = sourceLength - sourceStartFrame;
67  if (sourceFrameCount <= 0) return {};
68  }
69 
70  sv_frame_t targetFrameCount = sourceFrameCount * m_filterRatio;
71 
72  sv_frame_t filterLength = m_filter.size(); // NB this is known to be odd
73  sv_frame_t filterTailOut = (filterLength - 1) / 2;
74  sv_frame_t filterTailIn = filterTailOut / m_filterRatio;
75 
76  floatvec_t oversampled(targetFrameCount, 0.f);
77 
78  sv_frame_t i0 = sourceStartFrame - filterTailIn;
79  if (i0 < 0) {
80  i0 = 0;
81  }
82  sv_frame_t i1 = sourceStartFrame + sourceFrameCount + filterTailIn;
83  if (i1 > sourceLength) {
84  i1 = sourceLength;
85  }
86 
87  floatvec_t sourceData = source.getData(channel, i0, i1 - i0);
88 
89  for (sv_frame_t i = i0; i < i1; ++i) {
90  float v = sourceData[i - i0];
91  sv_frame_t outOffset =
92  (i - sourceStartFrame) * m_filterRatio - filterTailOut;
93  for (sv_frame_t j = 0; j < filterLength; ++j) {
94  sv_frame_t outIndex = outOffset + j;
95  if (outIndex < 0 || outIndex >= targetFrameCount) {
96  continue;
97  }
98  oversampled[outIndex] += v * m_filter[j];
99  }
100  }
101 
102  return oversampled;
103 }
104 
105 int
107 
111  2.0171043153063023E-4, 2.887198196326776E-4,
112  3.410439309101285E-4, 3.4267123819805857E-4,
113  2.843462511901066E-4, 1.6636986363946504E-4,
114  -4.5940658605786285E-18, -1.9299665002484582E-4,
115  -3.8279951732549946E-4, -5.357990649609105E-4,
116  -6.201170748425957E-4, -6.11531555444137E-4,
117  -4.987822892899791E-4, -2.872272251922189E-4,
118  -7.822991648518709E-19, 3.2382854144162815E-4,
119  6.341027017666046E-4, 8.769331519465396E-4,
120  0.001003535186382615, 9.791732608026272E-4,
121  7.906678783612421E-4, 4.5101220009813206E-4,
122  -3.039648514978356E-18, -4.996532051508215E-4,
123  -9.704877518513666E-4, -0.0013318083550190923,
124  -0.0015128911871247466, -0.0014658111457301016,
125  -0.0011756800671747431, -6.663214707558645E-4,
126  1.0713650598357415E-17, 7.292959363289514E-4,
127  0.0014084768982220279, 0.0019222969998680237,
128  0.0021721723797956524, 0.0020938999751673173,
129  0.0016712330766289326, 9.427050283841188E-4,
130  -5.656965938821965E-18, -0.0010225643654040554,
131  -0.001966437013513757, -0.002672722670880038,
132  -0.00300806671037164, -0.00288843624179131,
133  -0.0022967244980623574, -0.0012908081494665458,
134  -5.1499690577098E-18, 0.0013904094522721,
135  0.0026648961861419334, 0.0036103002009868065,
136  0.004050469159316014, 0.0038774554290217484,
137  0.0030739396559265075, 0.001722603817632299,
138  -9.130030250503607E-18, -0.0018451873718735516,
139  -0.0035270571169279162, -0.004765847116110058,
140  -0.0053332982334767624, -0.005092831604550132,
141  -0.00402770012894082, -0.0022517645624319594,
142  3.2752446397299053E-17, 0.0024010765839506923,
143  0.004579613038976446, 0.006174912111845945,
144  0.00689578873526276, 0.006571541332393174,
145  0.005186887306036285, 0.002894248521447605,
146  -1.336645565990815E-17, -0.0030747336558684963,
147  -0.0058540294958507235, -0.007879546416595632,
148  -0.008784519668338507, -0.008357645279493864,
149  -0.006586046547485615, -0.003669217725935383,
150  -1.9348975378752276E-17, 0.0038863208094135626,
151  0.007388553022623823, 0.009931080628244226,
152  0.011056594746806033, 0.010505398026651453,
153  0.008267906564613223, 0.00460048159140493,
154  -1.816145184081109E-17, -0.004861124757802925,
155  -0.009231379891669668, -0.012394511669674028,
156  -0.01378467517229709, -0.013084177592430083,
157  -0.010287380585427207, -0.00571879407588959,
158  7.520535431851951E-17, 0.006032144534828161,
159  0.011445734103106982, 0.015355551390625357,
160  0.017065088242935025, 0.016186450815185452,
161  0.012718051439340603, 0.0070655888687995785,
162  -2.3209664144996714E-17, -0.007444328311482942,
163  -0.01411821163125819, -0.018932253281654043,
164  -0.02103125585328301, -0.019941019333653123,
165  -0.015663002335303704, -0.008699245445932525,
166  2.5712475624993567E-17, 0.009161748270723635,
167  0.0173729814451255, 0.023294901939595228,
168  0.02587678878709242, 0.02453592568963366,
169  0.01927365323131565, 0.010706050935569809,
170  -2.8133472199037193E-17, -0.011280308241551094,
171  -0.02139710071477064, -0.02870170641615764,
172  -0.031897218249350504, -0.030260140480986304,
173  -0.023784294156618507, -0.013220449772289724,
174  3.042099156841831E-17, 0.013951594368737923,
175  0.0264884258371512, 0.03556693609945249,
176  0.03957036852169639, 0.0375845888664677,
177  0.029579845398822833, 0.016465167405787955,
178  -3.2524514488155654E-17, -0.017431115375410273,
179  -0.03315356952091943, -0.04460179422099746,
180  -0.0497244634100025, -0.04733366619358394,
181  -0.037341081614037944, -0.020838316594689998,
182  3.439626695384943E-17, 0.022185914535618936,
183  0.04232958202159685, 0.05713801867687856,
184  0.06393033000280622, 0.06109191933191721,
185  0.04839482380906132, 0.027127167584840003,
186  -3.5992766927138734E-17, -0.029168755716052385,
187  -0.055960213335110184, -0.07598693477350407,
188  -0.08556575102599769, -0.08233350786406181,
189  -0.06571046000158454, -0.03713224848702707,
190  3.727625511036616E-17, 0.04066438975848791,
191  0.07882920057770397, 0.10826166115536123,
192  0.12343378955977465, 0.12040455825217859,
193  0.09755344650130694, 0.056053367635801106,
194  -3.8215953158245473E-17, -0.0638435745677513,
195  -0.12667849902789644, -0.17861887575594584,
196  -0.20985333136704623, -0.21188193950868073,
197  -0.17867464086818077, -0.10760048593620072,
198  3.8789099095340224E-17, 0.13868670259490817,
199  0.29927055936918734, 0.46961864377510765,
200  0.6358321371992203, 0.7836674214332147,
201  0.9000377382311825, 0.9744199685311685,
202  1.0000000000000004, 0.9744199685311685,
203  0.9000377382311825, 0.7836674214332147,
204  0.6358321371992203, 0.46961864377510765,
205  0.29927055936918734, 0.13868670259490817,
206  3.8789099095340224E-17, -0.10760048593620072,
207  -0.17867464086818077, -0.21188193950868073,
208  -0.20985333136704623, -0.17861887575594584,
209  -0.12667849902789644, -0.0638435745677513,
210  -3.8215953158245473E-17, 0.056053367635801106,
211  0.09755344650130694, 0.12040455825217859,
212  0.12343378955977465, 0.10826166115536123,
213  0.07882920057770397, 0.04066438975848791,
214  3.727625511036616E-17, -0.03713224848702707,
215  -0.06571046000158454, -0.08233350786406181,
216  -0.08556575102599769, -0.07598693477350407,
217  -0.055960213335110184, -0.029168755716052385,
218  -3.5992766927138734E-17, 0.027127167584840003,
219  0.04839482380906132, 0.06109191933191721,
220  0.06393033000280622, 0.05713801867687856,
221  0.04232958202159685, 0.022185914535618936,
222  3.439626695384943E-17, -0.020838316594689998,
223  -0.037341081614037944, -0.04733366619358394,
224  -0.0497244634100025, -0.04460179422099746,
225  -0.03315356952091943, -0.017431115375410273,
226  -3.2524514488155654E-17, 0.016465167405787955,
227  0.029579845398822833, 0.0375845888664677,
228  0.03957036852169639, 0.03556693609945249,
229  0.0264884258371512, 0.013951594368737923,
230  3.042099156841831E-17, -0.013220449772289724,
231  -0.023784294156618507, -0.030260140480986304,
232  -0.031897218249350504, -0.02870170641615764,
233  -0.02139710071477064, -0.011280308241551094,
234  -2.8133472199037193E-17, 0.010706050935569809,
235  0.01927365323131565, 0.02453592568963366,
236  0.02587678878709242, 0.023294901939595228,
237  0.0173729814451255, 0.009161748270723635,
238  2.5712475624993567E-17, -0.008699245445932525,
239  -0.015663002335303704, -0.019941019333653123,
240  -0.02103125585328301, -0.018932253281654043,
241  -0.01411821163125819, -0.007444328311482942,
242  -2.3209664144996714E-17, 0.0070655888687995785,
243  0.012718051439340603, 0.016186450815185452,
244  0.017065088242935025, 0.015355551390625357,
245  0.011445734103106982, 0.006032144534828161,
246  7.520535431851951E-17, -0.00571879407588959,
247  -0.010287380585427207, -0.013084177592430083,
248  -0.01378467517229709, -0.012394511669674028,
249  -0.009231379891669668, -0.004861124757802925,
250  -1.816145184081109E-17, 0.00460048159140493,
251  0.008267906564613223, 0.010505398026651453,
252  0.011056594746806033, 0.009931080628244226,
253  0.007388553022623823, 0.0038863208094135626,
254  -1.9348975378752276E-17, -0.003669217725935383,
255  -0.006586046547485615, -0.008357645279493864,
256  -0.008784519668338507, -0.007879546416595632,
257  -0.0058540294958507235, -0.0030747336558684963,
258  -1.336645565990815E-17, 0.002894248521447605,
259  0.005186887306036285, 0.006571541332393174,
260  0.00689578873526276, 0.006174912111845945,
261  0.004579613038976446, 0.0024010765839506923,
262  3.2752446397299053E-17, -0.0022517645624319594,
263  -0.00402770012894082, -0.005092831604550132,
264  -0.0053332982334767624, -0.004765847116110058,
265  -0.0035270571169279162, -0.0018451873718735516,
266  -9.130030250503607E-18, 0.001722603817632299,
267  0.0030739396559265075, 0.0038774554290217484,
268  0.004050469159316014, 0.0036103002009868065,
269  0.0026648961861419334, 0.0013904094522721,
270  -5.1499690577098E-18, -0.0012908081494665458,
271  -0.0022967244980623574, -0.00288843624179131,
272  -0.00300806671037164, -0.002672722670880038,
273  -0.001966437013513757, -0.0010225643654040554,
274  -5.656965938821965E-18, 9.427050283841188E-4,
275  0.0016712330766289326, 0.0020938999751673173,
276  0.0021721723797956524, 0.0019222969998680237,
277  0.0014084768982220279, 7.292959363289514E-4,
278  1.0713650598357415E-17, -6.663214707558645E-4,
279  -0.0011756800671747431, -0.0014658111457301016,
280  -0.0015128911871247466, -0.0013318083550190923,
281  -9.704877518513666E-4, -4.996532051508215E-4,
282  -3.039648514978356E-18, 4.5101220009813206E-4,
283  7.906678783612421E-4, 9.791732608026272E-4,
284  0.001003535186382615, 8.769331519465396E-4,
285  6.341027017666046E-4, 3.2382854144162815E-4,
286  -7.822991648518709E-19, -2.872272251922189E-4,
287  -4.987822892899791E-4, -6.11531555444137E-4,
288  -6.201170748425957E-4, -5.357990649609105E-4,
289  -3.8279951732549946E-4, -1.9299665002484582E-4,
290  -4.5940658605786285E-18, 1.6636986363946504E-4,
291  2.843462511901066E-4, 3.4267123819805857E-4,
292  3.410439309101285E-4, 2.887198196326776E-4,
293  2.0171043153063023E-4
294 };
295 
int64_t sv_frame_t
Frame index, the unit of our time axis.
Definition: BaseTypes.h:31
virtual floatvec_t getData(int channel, sv_frame_t start, sv_frame_t count) const =0
Get the specified set of samples from the given channel of the model in single-precision floating-poi...
sv_frame_t getEndFrame() const
Return the audio frame at the end of the model, i.e.
Definition: Model.h:87
std::vector< float, breakfastquay::StlAllocator< float > > floatvec_t
Definition: BaseTypes.h:53
static floatvec_t getOversampledData(const DenseTimeValueModel &source, int channel, sv_frame_t sourceStartFrame, sv_frame_t sourceFrameCount, int oversampleBy)
Return an oversampled version of the audio data from the given source sample range.
static floatvec_t m_filter
Precalculated windowed sinc FIR filter for oversampling ratio of 8.
bool in_range_for(const C &container, T i)
Check whether an integer index is in range for a container, avoiding overflows and signed/unsigned co...
Definition: BaseTypes.h:37
Base class for models containing dense two-dimensional data (value against time). ...
static floatvec_t getFixedRatioData(const DenseTimeValueModel &source, int channel, sv_frame_t sourceStartFrame, sv_frame_t sourceFrameCount)
Profile point instance class.
Definition: Profiler.h:93