| 1 | /* |
| 2 | Copyright (C) 2007-2010 Christian Kothe |
| 3 | |
| 4 | This program is free software; you can redistribute it and/or |
| 5 | modify it under the terms of the GNU General Public License |
| 6 | as published by the Free Software Foundation; either version 2 |
| 7 | of the License, or (at your option) any later version. |
| 8 | |
| 9 | This program is distributed in the hope that it will be useful, |
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | GNU General Public License for more details. |
| 13 | |
| 14 | You should have received a copy of the GNU General Public License |
| 15 | along with this program; if not, write to the Free Software |
| 16 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | */ |
| 18 | |
| 19 | #include "FreeSurround/FreeSurroundDecoder.h" |
| 20 | #include "FreeSurround/ChannelMaps.h" |
| 21 | #include <cmath> |
| 22 | |
| 23 | #undef min |
| 24 | #undef max |
| 25 | |
| 26 | // FreeSurround implementation |
| 27 | // DPL2FSDecoder::Init() must be called before using the decoder. |
| 28 | DPL2FSDecoder::DPL2FSDecoder() { |
| 29 | initialized = false; |
| 30 | buffer_empty = true; |
| 31 | } |
| 32 | |
| 33 | DPL2FSDecoder::~DPL2FSDecoder() { |
| 34 | kiss_fftr_free(forward); |
| 35 | kiss_fftr_free(inverse); |
| 36 | } |
| 37 | |
| 38 | void DPL2FSDecoder::Init(channel_setup chsetup, unsigned int blsize, |
| 39 | unsigned int sample_rate) { |
| 40 | if (!initialized) { |
| 41 | setup = chsetup; |
| 42 | N = blsize; |
| 43 | samplerate = sample_rate; |
| 44 | |
| 45 | // Initialize the parameters |
| 46 | wnd = std::vector<double>(N); |
| 47 | inbuf = std::vector<float>(3 * N); |
| 48 | lt = std::vector<double>(N); |
| 49 | rt = std::vector<double>(N); |
| 50 | dst = std::vector<double>(N); |
| 51 | lf = std::vector<cplx>(N / 2 + 1); |
| 52 | rf = std::vector<cplx>(N / 2 + 1); |
| 53 | forward = kiss_fftr_alloc(N, 0, 0, 0); |
| 54 | inverse = kiss_fftr_alloc(N, 1, 0, 0); |
| 55 | C = static_cast<unsigned int>(chn_alloc[setup].size()); |
| 56 | |
| 57 | // Allocate per-channel buffers |
| 58 | outbuf.resize((N + N / 2) * C); |
| 59 | signal.resize(C, std::vector<cplx>(N)); |
| 60 | |
| 61 | // Init the window function |
| 62 | for (unsigned int k = 0; k < N; k++) |
| 63 | wnd[k] = sqrt(0.5 * (1 - cos(2 * pi * k / N)) / N); |
| 64 | |
| 65 | // set default parameters |
| 66 | set_circular_wrap(90); |
| 67 | set_shift(0); |
| 68 | set_depth(1); |
| 69 | set_focus(0); |
| 70 | set_center_image(1); |
| 71 | set_front_separation(1); |
| 72 | set_rear_separation(1); |
| 73 | set_low_cutoff(40.0f / samplerate * 2); |
| 74 | set_high_cutoff(90.0f / samplerate * 2); |
| 75 | set_bass_redirection(false); |
| 76 | |
| 77 | initialized = true; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | // decode a stereo chunk, produces a multichannel chunk of the same size |
| 82 | // (lagged) |
| 83 | float *DPL2FSDecoder::decode(float *input) { |
| 84 | if (initialized) { |
| 85 | // append incoming data to the end of the input buffer |
| 86 | memcpy(&inbuf[N], &input[0], 8 * N); |
| 87 | // process first and second half, overlapped |
| 88 | buffered_decode(&inbuf[0]); |
| 89 | buffered_decode(&inbuf[N]); |
| 90 | // shift last half of the input to the beginning (for overlapping with a |
| 91 | // future block) |
| 92 | memcpy(&inbuf[0], &inbuf[2 * N], 4 * N); |
| 93 | buffer_empty = false; |
| 94 | return &outbuf[0]; |
| 95 | } |
| 96 | return 0; |
| 97 | } |
| 98 | |
| 99 | // flush the internal buffers |
| 100 | void DPL2FSDecoder::flush() { |
| 101 | memset(&outbuf[0], 0, outbuf.size() * 4); |
| 102 | memset(&inbuf[0], 0, inbuf.size() * 4); |
| 103 | buffer_empty = true; |
| 104 | } |
| 105 | |
| 106 | // number of samples currently held in the buffer |
| 107 | unsigned int DPL2FSDecoder::buffered() { return buffer_empty ? 0 : N / 2; } |
| 108 | |
| 109 | // set soundfield & rendering parameters |
| 110 | void DPL2FSDecoder::set_circular_wrap(float v) { circular_wrap = v; } |
| 111 | void DPL2FSDecoder::set_shift(float v) { shift = v; } |
| 112 | void DPL2FSDecoder::set_depth(float v) { depth = v; } |
| 113 | void DPL2FSDecoder::set_focus(float v) { focus = v; } |
| 114 | void DPL2FSDecoder::set_center_image(float v) { center_image = v; } |
| 115 | void DPL2FSDecoder::set_front_separation(float v) { front_separation = v; } |
| 116 | void DPL2FSDecoder::set_rear_separation(float v) { rear_separation = v; } |
| 117 | void DPL2FSDecoder::set_low_cutoff(float v) { lo_cut = v * (N / 2); } |
| 118 | void DPL2FSDecoder::set_high_cutoff(float v) { hi_cut = v * (N / 2); } |
| 119 | void DPL2FSDecoder::set_bass_redirection(bool v) { use_lfe = v; } |
| 120 | |
| 121 | // helper functions |
| 122 | inline float DPL2FSDecoder::sqr(double x) { return static_cast<float>(x * x); } |
| 123 | inline double DPL2FSDecoder::amplitude(const cplx &x) { |
| 124 | return sqrt(sqr(x.real()) + sqr(x.imag())); |
| 125 | } |
| 126 | inline double DPL2FSDecoder::phase(const cplx &x) { |
| 127 | return atan2(x.imag(), x.real()); |
| 128 | } |
| 129 | inline cplx DPL2FSDecoder::polar(double a, double p) { |
| 130 | return cplx(a * cos(p), a * sin(p)); |
| 131 | } |
| 132 | inline float DPL2FSDecoder::min(double a, double b) { |
| 133 | return static_cast<float>(a < b ? a : b); |
| 134 | } |
| 135 | inline float DPL2FSDecoder::max(double a, double b) { |
| 136 | return static_cast<float>(a > b ? a : b); |
| 137 | } |
| 138 | inline float DPL2FSDecoder::clamp(double x) { return max(-1, min(1, x)); } |
| 139 | inline float DPL2FSDecoder::sign(double x) { |
| 140 | return static_cast<float>(x < 0 ? -1 : (x > 0 ? 1 : 0)); |
| 141 | } |
| 142 | // get the distance of the soundfield edge, along a given angle |
| 143 | inline double DPL2FSDecoder::edgedistance(double a) { |
| 144 | return min(sqrt(1 + sqr(tan(a))), sqrt(1 + sqr(1 / tan(a)))); |
| 145 | } |
| 146 | // get the index (and fractional offset!) in a piecewise-linear channel |
| 147 | // allocation grid |
| 148 | int DPL2FSDecoder::map_to_grid(double &x) { |
| 149 | double gp = ((x + 1) * 0.5) * (grid_res - 1), |
| 150 | i = min(grid_res - 2, floor(gp)); |
| 151 | x = gp - i; |
| 152 | return static_cast<int>(i); |
| 153 | } |
| 154 | |
| 155 | // decode a block of data and overlap-add it into outbuf |
| 156 | void DPL2FSDecoder::buffered_decode(float *input) { |
| 157 | // demultiplex and apply window function |
| 158 | for (unsigned int k = 0; k < N; k++) { |
| 159 | lt[k] = wnd[k] * input[k * 2 + 0]; |
| 160 | rt[k] = wnd[k] * input[k * 2 + 1]; |
| 161 | } |
| 162 | |
| 163 | // map into spectral domain |
| 164 | kiss_fftr(forward, <[0], (kiss_fft_cpx *)&lf[0]); |
| 165 | kiss_fftr(forward, &rt[0], (kiss_fft_cpx *)&rf[0]); |
| 166 | |
| 167 | // compute multichannel output signal in the spectral domain |
| 168 | for (unsigned int f = 1; f < N / 2; f++) { |
| 169 | // get Lt/Rt amplitudes & phases |
| 170 | double ampL = amplitude(lf[f]), ampR = amplitude(rf[f]); |
| 171 | double phaseL = phase(lf[f]), phaseR = phase(rf[f]); |
| 172 | // calculate the amplitude & phase differences |
| 173 | double ampDiff = |
| 174 | clamp((ampL + ampR < epsilon) ? 0 : (ampR - ampL) / (ampR + ampL)); |
| 175 | double phaseDiff = abs(phaseL - phaseR); |
| 176 | if (phaseDiff > pi) |
| 177 | phaseDiff = 2 * pi - phaseDiff; |
| 178 | |
| 179 | // decode into x/y soundfield position |
| 180 | double x, y; |
| 181 | transform_decode(ampDiff, phaseDiff, x, y); |
| 182 | // add wrap control |
| 183 | transform_circular_wrap(x, y, circular_wrap); |
| 184 | // add shift control |
| 185 | y = clamp(y - shift); |
| 186 | // add depth control |
| 187 | y = clamp(1 - (1 - y) * depth); |
| 188 | // add focus control |
| 189 | transform_focus(x, y, focus); |
| 190 | // add crossfeed control |
| 191 | x = clamp(x * |
| 192 | (front_separation * (1 + y) / 2 + rear_separation * (1 - y) / 2)); |
| 193 | |
| 194 | // get total signal amplitude |
| 195 | double amp_total = sqrt(ampL * ampL + ampR * ampR); |
| 196 | // and total L/C/R signal phases |
| 197 | double phase_of[] = { |
| 198 | phaseL, atan2(lf[f].imag() + rf[f].imag(), lf[f].real() + rf[f].real()), |
| 199 | phaseR}; |
| 200 | // compute 2d channel map indexes p/q and update x/y to fractional offsets |
| 201 | // in the map grid |
| 202 | int p = map_to_grid(x), q = map_to_grid(y); |
| 203 | // map position to channel volumes |
| 204 | for (unsigned int c = 0; c < C - 1; c++) { |
| 205 | // look up channel map at respective position (with bilinear |
| 206 | // interpolation) and build the |
| 207 | // signal |
| 208 | std::vector<float *> &a = chn_alloc[setup][c]; |
| 209 | signal[c][f] = polar( |
| 210 | amp_total * ((1 - x) * (1 - y) * a[q][p] + x * (1 - y) * a[q][p + 1] + |
| 211 | (1 - x) * y * a[q + 1][p] + x * y * a[q + 1][p + 1]), |
| 212 | phase_of[1 + static_cast<int>(sign(chn_xsf[setup][c]))]); |
| 213 | } |
| 214 | |
| 215 | // optionally redirect bass |
| 216 | if (use_lfe && f < hi_cut) { |
| 217 | // level of LFE channel according to normalized frequency |
| 218 | double lfe_level = |
| 219 | f < lo_cut ? 1 |
| 220 | : 0.5 * (1 + cos(pi * (f - lo_cut) / (hi_cut - lo_cut))); |
| 221 | // assign LFE channel |
| 222 | signal[C - 1][f] = lfe_level * polar(amp_total, phase_of[1]); |
| 223 | // subtract the signal from the other channels |
| 224 | for (unsigned int c = 0; c < C - 1; c++) |
| 225 | signal[c][f] *= (1 - lfe_level); |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | // shift the last 2/3 to the first 2/3 of the output buffer |
| 230 | memcpy(&outbuf[0], &outbuf[C * N / 2], N * C * 4); |
| 231 | // and clear the rest |
| 232 | memset(&outbuf[C * N], 0, C * 4 * N / 2); |
| 233 | // backtransform each channel and overlap-add |
| 234 | for (unsigned int c = 0; c < C; c++) { |
| 235 | // back-transform into time domain |
| 236 | kiss_fftri(inverse, (kiss_fft_cpx *)&signal[c][0], &dst[0]); |
| 237 | // add the result to the last 2/3 of the output buffer, windowed (and |
| 238 | // remultiplex) |
| 239 | for (unsigned int k = 0; k < N; k++) |
| 240 | outbuf[C * (k + N / 2) + c] += static_cast<float>(wnd[k] * dst[k]); |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | // transform amp/phase difference space into x/y soundfield space |
| 245 | void DPL2FSDecoder::transform_decode(double a, double p, double &x, double &y) { |
| 246 | x = clamp(1.0047 * a + 0.46804 * a * p * p * p - 0.2042 * a * p * p * p * p + |
| 247 | 0.0080586 * a * p * p * p * p * p * p * p - |
| 248 | 0.0001526 * a * p * p * p * p * p * p * p * p * p * p - |
| 249 | 0.073512 * a * a * a * p - 0.2499 * a * a * a * p * p * p * p + |
| 250 | 0.016932 * a * a * a * p * p * p * p * p * p * p - |
| 251 | 0.00027707 * a * a * a * p * p * p * p * p * p * p * p * p * p + |
| 252 | 0.048105 * a * a * a * a * a * p * p * p * p * p * p * p - |
| 253 | 0.0065947 * a * a * a * a * a * p * p * p * p * p * p * p * p * p * |
| 254 | p + |
| 255 | 0.0016006 * a * a * a * a * a * p * p * p * p * p * p * p * p * p * |
| 256 | p * p - |
| 257 | 0.0071132 * a * a * a * a * a * a * a * p * p * p * p * p * p * p * |
| 258 | p * p + |
| 259 | 0.0022336 * a * a * a * a * a * a * a * p * p * p * p * p * p * p * |
| 260 | p * p * p * p - |
| 261 | 0.0004804 * a * a * a * a * a * a * a * p * p * p * p * p * p * p * |
| 262 | p * p * p * p * p); |
| 263 | y = clamp( |
| 264 | 0.98592 - 0.62237 * p + 0.077875 * p * p - 0.0026929 * p * p * p * p * p + |
| 265 | 0.4971 * a * a * p - 0.00032124 * a * a * p * p * p * p * p * p + |
| 266 | 9.2491e-006 * a * a * a * a * p * p * p * p * p * p * p * p * p * p + |
| 267 | 0.051549 * a * a * a * a * a * a * a * a + |
| 268 | 1.0727e-014 * a * a * a * a * a * a * a * a * a * a); |
| 269 | } |
| 270 | |
| 271 | // apply a circular_wrap transformation to some position |
| 272 | void DPL2FSDecoder::transform_circular_wrap(double &x, double &y, |
| 273 | double refangle) { |
| 274 | if (refangle == 90) |
| 275 | return; |
| 276 | refangle = refangle * pi / 180; |
| 277 | double baseangle = 90 * pi / 180; |
| 278 | // translate into edge-normalized polar coordinates |
| 279 | double ang = atan2(x, y), len = sqrt(x * x + y * y); |
| 280 | len = len / edgedistance(ang); |
| 281 | // apply circular_wrap transform |
| 282 | if (abs(ang) < baseangle / 2) |
| 283 | // angle falls within the front region (to be enlarged) |
| 284 | ang *= refangle / baseangle; |
| 285 | else |
| 286 | // angle falls within the rear region (to be shrunken) |
| 287 | ang = pi - (-(((refangle - 2 * pi) * (pi - abs(ang)) * sign(ang)) / |
| 288 | (2 * pi - baseangle))); |
| 289 | // translate back into soundfield position |
| 290 | len = len * edgedistance(ang); |
| 291 | x = clamp(sin(ang) * len); |
| 292 | y = clamp(cos(ang) * len); |
| 293 | } |
| 294 | |
| 295 | // apply a focus transformation to some position |
| 296 | void DPL2FSDecoder::transform_focus(double &x, double &y, double focus) { |
| 297 | if (focus == 0) |
| 298 | return; |
| 299 | // translate into edge-normalized polar coordinates |
| 300 | double ang = atan2(x, y), |
| 301 | len = clamp(sqrt(x * x + y * y) / edgedistance(ang)); |
| 302 | // apply focus |
| 303 | len = focus > 0 ? 1 - pow(1 - len, 1 + focus * 20) : pow(len, 1 - focus * 20); |
| 304 | // back-transform into euclidian soundfield position |
| 305 | len = len * edgedistance(ang); |
| 306 | x = clamp(sin(ang) * len); |
| 307 | y = clamp(cos(ang) * len); |
| 308 | } |