Tue, 06 Jan 2015 21:39:09 +0100
Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.
michael@0 | 1 | //////////////////////////////////////////////////////////////////////////////// |
michael@0 | 2 | /// |
michael@0 | 3 | /// Cubic interpolation routine. |
michael@0 | 4 | /// |
michael@0 | 5 | /// Author : Copyright (c) Olli Parviainen |
michael@0 | 6 | /// Author e-mail : oparviai 'at' iki.fi |
michael@0 | 7 | /// SoundTouch WWW: http://www.surina.net/soundtouch |
michael@0 | 8 | /// |
michael@0 | 9 | //////////////////////////////////////////////////////////////////////////////// |
michael@0 | 10 | // |
michael@0 | 11 | // $Id: InterpolateCubic.cpp 179 2014-01-06 18:41:42Z oparviai $ |
michael@0 | 12 | // |
michael@0 | 13 | //////////////////////////////////////////////////////////////////////////////// |
michael@0 | 14 | // |
michael@0 | 15 | // License : |
michael@0 | 16 | // |
michael@0 | 17 | // SoundTouch audio processing library |
michael@0 | 18 | // Copyright (c) Olli Parviainen |
michael@0 | 19 | // |
michael@0 | 20 | // This library is free software; you can redistribute it and/or |
michael@0 | 21 | // modify it under the terms of the GNU Lesser General Public |
michael@0 | 22 | // License as published by the Free Software Foundation; either |
michael@0 | 23 | // version 2.1 of the License, or (at your option) any later version. |
michael@0 | 24 | // |
michael@0 | 25 | // This library is distributed in the hope that it will be useful, |
michael@0 | 26 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
michael@0 | 27 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
michael@0 | 28 | // Lesser General Public License for more details. |
michael@0 | 29 | // |
michael@0 | 30 | // You should have received a copy of the GNU Lesser General Public |
michael@0 | 31 | // License along with this library; if not, write to the Free Software |
michael@0 | 32 | // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
michael@0 | 33 | // |
michael@0 | 34 | //////////////////////////////////////////////////////////////////////////////// |
michael@0 | 35 | |
michael@0 | 36 | #include <stddef.h> |
michael@0 | 37 | #include <math.h> |
michael@0 | 38 | #include "InterpolateCubic.h" |
michael@0 | 39 | #include "STTypes.h" |
michael@0 | 40 | |
michael@0 | 41 | using namespace soundtouch; |
michael@0 | 42 | |
michael@0 | 43 | // cubic interpolation coefficients |
michael@0 | 44 | static const float _coeffs[]= |
michael@0 | 45 | { -0.5f, 1.0f, -0.5f, 0.0f, |
michael@0 | 46 | 1.5f, -2.5f, 0.0f, 1.0f, |
michael@0 | 47 | -1.5f, 2.0f, 0.5f, 0.0f, |
michael@0 | 48 | 0.5f, -0.5f, 0.0f, 0.0f}; |
michael@0 | 49 | |
michael@0 | 50 | |
michael@0 | 51 | InterpolateCubic::InterpolateCubic() |
michael@0 | 52 | { |
michael@0 | 53 | fract = 0; |
michael@0 | 54 | } |
michael@0 | 55 | |
michael@0 | 56 | |
michael@0 | 57 | void InterpolateCubic::resetRegisters() |
michael@0 | 58 | { |
michael@0 | 59 | fract = 0; |
michael@0 | 60 | } |
michael@0 | 61 | |
michael@0 | 62 | |
michael@0 | 63 | /// Transpose mono audio. Returns number of produced output samples, and |
michael@0 | 64 | /// updates "srcSamples" to amount of consumed source samples |
michael@0 | 65 | int InterpolateCubic::transposeMono(SAMPLETYPE *pdest, |
michael@0 | 66 | const SAMPLETYPE *psrc, |
michael@0 | 67 | int &srcSamples) |
michael@0 | 68 | { |
michael@0 | 69 | int i; |
michael@0 | 70 | int srcSampleEnd = srcSamples - 4; |
michael@0 | 71 | int srcCount = 0; |
michael@0 | 72 | |
michael@0 | 73 | i = 0; |
michael@0 | 74 | while (srcCount < srcSampleEnd) |
michael@0 | 75 | { |
michael@0 | 76 | float out; |
michael@0 | 77 | const float x3 = 1.0f; |
michael@0 | 78 | const float x2 = (float)fract; // x |
michael@0 | 79 | const float x1 = x2*x2; // x^2 |
michael@0 | 80 | const float x0 = x1*x2; // x^3 |
michael@0 | 81 | float y0, y1, y2, y3; |
michael@0 | 82 | |
michael@0 | 83 | assert(fract < 1.0); |
michael@0 | 84 | |
michael@0 | 85 | y0 = _coeffs[0] * x0 + _coeffs[1] * x1 + _coeffs[2] * x2 + _coeffs[3] * x3; |
michael@0 | 86 | y1 = _coeffs[4] * x0 + _coeffs[5] * x1 + _coeffs[6] * x2 + _coeffs[7] * x3; |
michael@0 | 87 | y2 = _coeffs[8] * x0 + _coeffs[9] * x1 + _coeffs[10] * x2 + _coeffs[11] * x3; |
michael@0 | 88 | y3 = _coeffs[12] * x0 + _coeffs[13] * x1 + _coeffs[14] * x2 + _coeffs[15] * x3; |
michael@0 | 89 | |
michael@0 | 90 | out = y0 * psrc[0] + y1 * psrc[1] + y2 * psrc[2] + y3 * psrc[3]; |
michael@0 | 91 | |
michael@0 | 92 | pdest[i] = (SAMPLETYPE)out; |
michael@0 | 93 | i ++; |
michael@0 | 94 | |
michael@0 | 95 | // update position fraction |
michael@0 | 96 | fract += rate; |
michael@0 | 97 | // update whole positions |
michael@0 | 98 | int whole = (int)fract; |
michael@0 | 99 | fract -= whole; |
michael@0 | 100 | psrc += whole; |
michael@0 | 101 | srcCount += whole; |
michael@0 | 102 | } |
michael@0 | 103 | srcSamples = srcCount; |
michael@0 | 104 | return i; |
michael@0 | 105 | } |
michael@0 | 106 | |
michael@0 | 107 | |
michael@0 | 108 | /// Transpose stereo audio. Returns number of produced output samples, and |
michael@0 | 109 | /// updates "srcSamples" to amount of consumed source samples |
michael@0 | 110 | int InterpolateCubic::transposeStereo(SAMPLETYPE *pdest, |
michael@0 | 111 | const SAMPLETYPE *psrc, |
michael@0 | 112 | int &srcSamples) |
michael@0 | 113 | { |
michael@0 | 114 | int i; |
michael@0 | 115 | int srcSampleEnd = srcSamples - 4; |
michael@0 | 116 | int srcCount = 0; |
michael@0 | 117 | |
michael@0 | 118 | i = 0; |
michael@0 | 119 | while (srcCount < srcSampleEnd) |
michael@0 | 120 | { |
michael@0 | 121 | const float x3 = 1.0f; |
michael@0 | 122 | const float x2 = (float)fract; // x |
michael@0 | 123 | const float x1 = x2*x2; // x^2 |
michael@0 | 124 | const float x0 = x1*x2; // x^3 |
michael@0 | 125 | float y0, y1, y2, y3; |
michael@0 | 126 | float out0, out1; |
michael@0 | 127 | |
michael@0 | 128 | assert(fract < 1.0); |
michael@0 | 129 | |
michael@0 | 130 | y0 = _coeffs[0] * x0 + _coeffs[1] * x1 + _coeffs[2] * x2 + _coeffs[3] * x3; |
michael@0 | 131 | y1 = _coeffs[4] * x0 + _coeffs[5] * x1 + _coeffs[6] * x2 + _coeffs[7] * x3; |
michael@0 | 132 | y2 = _coeffs[8] * x0 + _coeffs[9] * x1 + _coeffs[10] * x2 + _coeffs[11] * x3; |
michael@0 | 133 | y3 = _coeffs[12] * x0 + _coeffs[13] * x1 + _coeffs[14] * x2 + _coeffs[15] * x3; |
michael@0 | 134 | |
michael@0 | 135 | out0 = y0 * psrc[0] + y1 * psrc[2] + y2 * psrc[4] + y3 * psrc[6]; |
michael@0 | 136 | out1 = y0 * psrc[1] + y1 * psrc[3] + y2 * psrc[5] + y3 * psrc[7]; |
michael@0 | 137 | |
michael@0 | 138 | pdest[2*i] = (SAMPLETYPE)out0; |
michael@0 | 139 | pdest[2*i+1] = (SAMPLETYPE)out1; |
michael@0 | 140 | i ++; |
michael@0 | 141 | |
michael@0 | 142 | // update position fraction |
michael@0 | 143 | fract += rate; |
michael@0 | 144 | // update whole positions |
michael@0 | 145 | int whole = (int)fract; |
michael@0 | 146 | fract -= whole; |
michael@0 | 147 | psrc += 2*whole; |
michael@0 | 148 | srcCount += whole; |
michael@0 | 149 | } |
michael@0 | 150 | srcSamples = srcCount; |
michael@0 | 151 | return i; |
michael@0 | 152 | } |
michael@0 | 153 | |
michael@0 | 154 | |
michael@0 | 155 | /// Transpose multi-channel audio. Returns number of produced output samples, and |
michael@0 | 156 | /// updates "srcSamples" to amount of consumed source samples |
michael@0 | 157 | int InterpolateCubic::transposeMulti(SAMPLETYPE *pdest, |
michael@0 | 158 | const SAMPLETYPE *psrc, |
michael@0 | 159 | int &srcSamples) |
michael@0 | 160 | { |
michael@0 | 161 | int i; |
michael@0 | 162 | int srcSampleEnd = srcSamples - 4; |
michael@0 | 163 | int srcCount = 0; |
michael@0 | 164 | |
michael@0 | 165 | i = 0; |
michael@0 | 166 | while (srcCount < srcSampleEnd) |
michael@0 | 167 | { |
michael@0 | 168 | const float x3 = 1.0f; |
michael@0 | 169 | const float x2 = (float)fract; // x |
michael@0 | 170 | const float x1 = x2*x2; // x^2 |
michael@0 | 171 | const float x0 = x1*x2; // x^3 |
michael@0 | 172 | float y0, y1, y2, y3; |
michael@0 | 173 | |
michael@0 | 174 | assert(fract < 1.0); |
michael@0 | 175 | |
michael@0 | 176 | y0 = _coeffs[0] * x0 + _coeffs[1] * x1 + _coeffs[2] * x2 + _coeffs[3] * x3; |
michael@0 | 177 | y1 = _coeffs[4] * x0 + _coeffs[5] * x1 + _coeffs[6] * x2 + _coeffs[7] * x3; |
michael@0 | 178 | y2 = _coeffs[8] * x0 + _coeffs[9] * x1 + _coeffs[10] * x2 + _coeffs[11] * x3; |
michael@0 | 179 | y3 = _coeffs[12] * x0 + _coeffs[13] * x1 + _coeffs[14] * x2 + _coeffs[15] * x3; |
michael@0 | 180 | |
michael@0 | 181 | for (int c = 0; c < numChannels; c ++) |
michael@0 | 182 | { |
michael@0 | 183 | float out; |
michael@0 | 184 | out = y0 * psrc[c] + y1 * psrc[c + numChannels] + y2 * psrc[c + 2 * numChannels] + y3 * psrc[c + 3 * numChannels]; |
michael@0 | 185 | pdest[0] = (SAMPLETYPE)out; |
michael@0 | 186 | pdest ++; |
michael@0 | 187 | } |
michael@0 | 188 | i ++; |
michael@0 | 189 | |
michael@0 | 190 | // update position fraction |
michael@0 | 191 | fract += rate; |
michael@0 | 192 | // update whole positions |
michael@0 | 193 | int whole = (int)fract; |
michael@0 | 194 | fract -= whole; |
michael@0 | 195 | psrc += numChannels*whole; |
michael@0 | 196 | srcCount += whole; |
michael@0 | 197 | } |
michael@0 | 198 | srcSamples = srcCount; |
michael@0 | 199 | return i; |
michael@0 | 200 | } |