annotate data/model/WaveformOversampler.cpp @ 1539:0b08bc8741c6 zoom

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