| 1 | /*(LGPL) |
| 2 | --------------------------------------------------------------------------- |
| 3 | a_filters.c - Various Audio Signal Filters |
| 4 | --------------------------------------------------------------------------- |
| 5 | * Copyright (C) 2001, 2002, David Olofson |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify it |
| 8 | * under the terms of the GNU Lesser General Public License as published by |
| 9 | * the Free Software Foundation; either version 2.1 of the License, or (at |
| 10 | * your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, but |
| 13 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * Lesser General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU Lesser General Public License |
| 18 | * along with this program; if not, write to the Free Software Foundation, |
| 19 | * Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 20 | */ |
| 21 | |
| 22 | #include <stdlib.h> |
| 23 | |
| 24 | #include "a_math.h"cxx . . $ `<file>/sound/a_math.h`/ |
| 25 | #include "a_tools.h"cxx . . $ `<file>/sound/a_tools.h`/ |
| 26 | #include "a_filters.h"cxx . . $ `<file>/sound/a_filters.h`/ |
| 27 | |
| 28 | /*-------------------------------------------------------------------- |
| 29 | f6_t - Simple mono 6 dB/oct state variable filter |
| 30 | --------------------------------------------------------------------*/ |
| 31 | void f6_process_lpcxx . . $ f6_process_lp(59dd92d23c693c1).(f6_tcxx . . $ f6_t# *fil, int *buf, unsigned frames) |
| 32 | { |
| 33 | int f = fil->fcxx . . $ f6_t#f.; |
| 34 | int d = fil->dcxx . . $ f6_t#d.; |
| 35 | unsigned s; |
| 36 | for(s = 0; s < frames; ++s) |
| 37 | { |
| 38 | d += (buf[s] - (d>>12)) * f; |
| 39 | buf[s] = d>>12; |
| 40 | } |
| 41 | fil->dcxx . . $ f6_t#d. = d; |
| 42 | } |
| 43 | |
| 44 | void f6_process_hpcxx . . $ f6_process_hp(59dd92d23c693c1).(f6_tcxx . . $ f6_t# *fil, int *buf, unsigned frames) |
| 45 | { |
| 46 | int f = fil->fcxx . . $ f6_t#f.; |
| 47 | int d = fil->dcxx . . $ f6_t#d.; |
| 48 | unsigned s; |
| 49 | for(s = 0; s < frames; ++s) |
| 50 | { |
| 51 | d += (buf[s] - (d>>12)) * f; |
| 52 | buf[s] -= d>>12; |
| 53 | } |
| 54 | fil->dcxx . . $ f6_t#d. = d; |
| 55 | } |
| 56 | |
| 57 | void f6_set_fcxx . . $ f6_set_f(354a7bd645272110).(f6_tcxx . . $ f6_t# *fil, int f) |
| 58 | { |
| 59 | fil->fcxx . . $ f6_t#f. = (f<<13) / fil->ratecxx . . $ f6_t#rate.; |
| 60 | if(fil->fcxx . . $ f6_t#f. > 1<<13) |
| 61 | fil->fcxx . . $ f6_t#f. = 1<<13; |
| 62 | } |
| 63 | |
| 64 | void f6_initcxx . . $ f6_init(354a7bd645272110).(f6_tcxx . . $ f6_t# *fil, int fs) |
| 65 | { |
| 66 | fil->ratecxx . . $ f6_t#rate. = fs; |
| 67 | fil->dcxx . . $ f6_t#d. = 0; |
| 68 | f6_set_fcxx . . $ f6_set_f(354a7bd645272110).(fil, 400); |
| 69 | } |
| 70 | |
| 71 | |
| 72 | /*-------------------------------------------------------------------- |
| 73 | f6s_t - Simple stereo 6 dB/oct state variable filter |
| 74 | --------------------------------------------------------------------*/ |
| 75 | void f6s_process_lpcxx . . $ f6s_process_lp(3672d499a6e7aae7).(f6s_tcxx . . $ f6s_t# *fil, int *buf, unsigned frames) |
| 76 | { |
| 77 | int f = fil->fcxx . . $ f6s_t#f.; |
| 78 | int dl = fil->dlcxx . . $ f6s_t#dl.; |
| 79 | int dr = fil->drcxx . . $ f6s_t#dr.; |
| 80 | unsigned s; |
| 81 | frames <<= 1; |
| 82 | for(s = 0; s < frames; s += 2) |
| 83 | { |
| 84 | dl += (buf[s] - (dl>>12)) * f; |
| 85 | dr += (buf[s+1] - (dr>>12)) * f; |
| 86 | buf[s] = dl>>12; |
| 87 | buf[s+1] = dr>>12; |
| 88 | } |
| 89 | fil->dlcxx . . $ f6s_t#dl. = dl; |
| 90 | fil->drcxx . . $ f6s_t#dr. = dr; |
| 91 | } |
| 92 | |
| 93 | void f6s_process_hpcxx . . $ f6s_process_hp(3672d499a6e7aae7).(f6s_tcxx . . $ f6s_t# *fil, int *buf, unsigned frames) |
| 94 | { |
| 95 | int f = fil->fcxx . . $ f6s_t#f.; |
| 96 | int dl = fil->dlcxx . . $ f6s_t#dl.; |
| 97 | int dr = fil->drcxx . . $ f6s_t#dr.; |
| 98 | unsigned s; |
| 99 | frames <<= 1; |
| 100 | for(s = 0; s < frames; s += 2) |
| 101 | { |
| 102 | dl += (buf[s] - (dl>>12)) * f; |
| 103 | dr += (buf[s+1] - (dr>>12)) * f; |
| 104 | buf[s] -= dl>>12; |
| 105 | buf[s+1] -= dr>>12; |
| 106 | } |
| 107 | fil->dlcxx . . $ f6s_t#dl. = dl; |
| 108 | fil->drcxx . . $ f6s_t#dr. = dr; |
| 109 | } |
| 110 | |
| 111 | void f6s_set_fcxx . . $ f6s_set_f(235123d64d12511c).(f6s_tcxx . . $ f6s_t# *fil, int f) |
| 112 | { |
| 113 | fil->fcxx . . $ f6s_t#f. = (f<<13) / fil->ratecxx . . $ f6s_t#rate.; |
| 114 | if(fil->fcxx . . $ f6s_t#f. > 1<<13) |
| 115 | fil->fcxx . . $ f6s_t#f. = 1<<13; |
| 116 | } |
| 117 | |
| 118 | void f6s_initcxx . . $ f6s_init(235123d64d12511c).(f6s_tcxx . . $ f6s_t# *fil, int fs) |
| 119 | { |
| 120 | fil->ratecxx . . $ f6s_t#rate. = fs; |
| 121 | fil->dlcxx . . $ f6s_t#dl. = fil->drcxx . . $ f6s_t#dr. = 0; |
| 122 | f6s_set_fcxx . . $ f6s_set_f(235123d64d12511c).(fil, 400); |
| 123 | } |
| 124 | |
| 125 | |
| 126 | /*-------------------------------------------------------------------- |
| 127 | dcf6s_t - Fast stereo 6 dB/oct HPF for DC suppression |
| 128 | --------------------------------------------------------------------*/ |
| 129 | void dcf6s_processcxx . . $ dcf6s_process(8c20f7f566a783f3).(dcf6s_tcxx . . $ dcf6s_t# *fil, int *buf, unsigned frames) |
| 130 | { |
| 131 | int f = fil->fcxx . . $ dcf6s_t#f.; |
| 132 | int dl = fil->dlcxx . . $ dcf6s_t#dl.; |
| 133 | int dr = fil->drcxx . . $ dcf6s_t#dr.; |
| 134 | unsigned s; |
| 135 | frames <<= 1; |
| 136 | for(s = 0; s < frames; s += 2) |
| 137 | { |
| 138 | dl += (buf[s] - (dl>>12)) >> f; |
| 139 | dr += (buf[s+1] - (dr>>12)) >> f; |
| 140 | buf[s] -= dl>>12; |
| 141 | buf[s+1] -= dr>>12; |
| 142 | } |
| 143 | fil->dlcxx . . $ dcf6s_t#dl. = dl; |
| 144 | fil->drcxx . . $ dcf6s_t#dr. = dr; |
| 145 | } |
| 146 | |
| 147 | int dcf6s_silentcxx . . $ dcf6s_silent(d9519cbb80e53e0e).(dcf6s_tcxx . . $ dcf6s_t# *fil) |
| 148 | { |
| 149 | if(labscxx . . $ labs(8ce0d1b217db7fd1).(fil->dlcxx . . $ dcf6s_t#dl.) > 4096) |
| 150 | return 0; |
| 151 | if(labscxx . . $ labs(8ce0d1b217db7fd1).(fil->drcxx . . $ dcf6s_t#dr.) > 4096) |
| 152 | return 0; |
| 153 | return 1; |
| 154 | } |
| 155 | |
| 156 | void dcf6s_set_fcxx . . $ dcf6s_set_f(8a8d5a713cc63619).(dcf6s_tcxx . . $ dcf6s_t# *fil, int f) |
| 157 | { |
| 158 | fil->fcxx . . $ dcf6s_t#f. = int2shiftcxx . . $ int2shift(7864480464b09eea).(fil->ratecxx . . $ dcf6s_t#rate. / f) - 13; |
| 159 | if(fil->fcxx . . $ dcf6s_t#f. < 0) |
| 160 | fil->fcxx . . $ dcf6s_t#f. = 0; |
| 161 | } |
| 162 | |
| 163 | void dcf6s_initcxx . . $ dcf6s_init(8a8d5a713cc63619).(dcf6s_tcxx . . $ dcf6s_t# *fil, int fs) |
| 164 | { |
| 165 | fil->ratecxx . . $ dcf6s_t#rate. = fs; |
| 166 | fil->dlcxx . . $ dcf6s_t#dl. = fil->drcxx . . $ dcf6s_t#dr. = 0; |
| 167 | dcf6s_set_fcxx . . $ dcf6s_set_f(8a8d5a713cc63619).(fil, 10); |
| 168 | } |
| 169 | |
| 170 | |
| 171 | |
| 172 | /*-------------------------------------------------------------------- |
| 173 | resof12_t - Resonant mono 12 dB/oct state variable filter |
| 174 | --------------------------------------------------------------------*/ |
| 175 | /* In-place LP */ |
| 176 | void resof12_process_lpcxx . . $ resof12_process_lp(b1789ad83566847a).(resof12_tcxx . . $ resof12_t# *rf, int *buf, unsigned frames) |
| 177 | { |
| 178 | int d1 = rf->d1cxx . . $ resof12_t#d1.; |
| 179 | int d2 = rf->d2cxx . . $ resof12_t#d2.; |
| 180 | int f = rf->fcxx . . $ resof12_t#f.; |
| 181 | int q = rf->qcxx . . $ resof12_t#q.; |
| 182 | int l, b, h; |
| 183 | unsigned s; |
| 184 | for(s = 0; s < frames; ++s) |
| 185 | { |
| 186 | l = d2 + (f*d1 >> 8); |
| 187 | h = (buf[s] << 4) - l - (q*d1 >> 8); |
| 188 | b = (f*h >> 8) + d1; |
| 189 | buf[s] = l >> 4; |
| 190 | d1 = b; |
| 191 | d2 = l; |
| 192 | } |
| 193 | rf->d1cxx . . $ resof12_t#d1. = d1; |
| 194 | rf->d2cxx . . $ resof12_t#d2. = d2; |
| 195 | } |
| 196 | |
| 197 | /* In-place HP */ |
| 198 | void resof12_process_hpcxx . . $ resof12_process_hp(b1789ad83566847a).(resof12_tcxx . . $ resof12_t# *rf, int *buf, unsigned frames) |
| 199 | { |
| 200 | int d1 = rf->d1cxx . . $ resof12_t#d1.; |
| 201 | int d2 = rf->d2cxx . . $ resof12_t#d2.; |
| 202 | int f = rf->fcxx . . $ resof12_t#f.; |
| 203 | int q = rf->qcxx . . $ resof12_t#q.; |
| 204 | int l, b, h; |
| 205 | unsigned s; |
| 206 | for(s = 0; s < frames; ++s) |
| 207 | { |
| 208 | l = d2 + (f*d1 >> 8); |
| 209 | h = (buf[s] << 4) - l - (q*d1 >> 8); |
| 210 | b = (f*h >> 8) + d1; |
| 211 | buf[s] = h >> 4; |
| 212 | d1 = b; |
| 213 | d2 = l; |
| 214 | } |
| 215 | rf->d1cxx . . $ resof12_t#d1. = d1; |
| 216 | rf->d2cxx . . $ resof12_t#d2. = d2; |
| 217 | } |
| 218 | |
| 219 | /* In-place BP */ |
| 220 | void resof12_process_bpcxx . . $ resof12_process_bp(b1789ad83566847a).(resof12_tcxx . . $ resof12_t# *rf, int *buf, unsigned frames) |
| 221 | { |
| 222 | int d1 = rf->d1cxx . . $ resof12_t#d1.; |
| 223 | int d2 = rf->d2cxx . . $ resof12_t#d2.; |
| 224 | int f = rf->fcxx . . $ resof12_t#f.; |
| 225 | int q = rf->qcxx . . $ resof12_t#q.; |
| 226 | int l, b, h; |
| 227 | unsigned s; |
| 228 | for(s = 0; s < frames; ++s) |
| 229 | { |
| 230 | l = d2 + (f*d1 >> 8); |
| 231 | h = (buf[s] << 4) - l - (q*d1 >> 8); |
| 232 | b = (f*h >> 8) + d1; |
| 233 | buf[s] = b >> 4; |
| 234 | d1 = b; |
| 235 | d2 = l; |
| 236 | } |
| 237 | rf->d1cxx . . $ resof12_t#d1. = d1; |
| 238 | rf->d2cxx . . $ resof12_t#d2. = d2; |
| 239 | } |
| 240 | |
| 241 | /* |
| 242 | * LP filter the 'buf' buffer, while separating off |
| 243 | * the HP part into buffer 'out'. |
| 244 | */ |
| 245 | void resof12_process_splitcxx . . $ resof12_process_split(73390d1bf4feb1a8).(resof12_tcxx . . $ resof12_t# *rf, int *buf, int *out, unsigned frames) |
| 246 | { |
| 247 | int d1 = rf->d1cxx . . $ resof12_t#d1.; |
| 248 | int d2 = rf->d2cxx . . $ resof12_t#d2.; |
| 249 | int f = rf->fcxx . . $ resof12_t#f.; |
| 250 | int q = rf->qcxx . . $ resof12_t#q.; |
| 251 | int l, b, h; |
| 252 | unsigned s; |
| 253 | for(s = 0; s < frames; ++s) |
| 254 | { |
| 255 | l = d2 + (f*d1 >> 8); |
| 256 | h = (buf[s] << 4) - l - (q*d1 >> 8); |
| 257 | b = (f*h >> 8) + d1; |
| 258 | buf[s] = l >> 4; |
| 259 | out[s] = h >> 4; |
| 260 | d1 = b; |
| 261 | d2 = l; |
| 262 | } |
| 263 | rf->d1cxx . . $ resof12_t#d1. = d1; |
| 264 | rf->d2cxx . . $ resof12_t#d2. = d2; |
| 265 | } |
| 266 | |
| 267 | void resof12_set_fcxx . . $ resof12_set_f(2abd089afca10685).(resof12_tcxx . . $ resof12_t# *rf, int f) |
| 268 | { |
| 269 | rf->fcxx . . $ resof12_t#f. = (int)(512.0 * sin(M_PIcxx . . $ `<external>/11263917310783833665/math.h:1121:10`! * (float)f / rf->ratecxx . . $ resof12_t#rate.)); |
| 270 | if(rf->fcxx . . $ resof12_t#f. > 256) |
| 271 | rf->fcxx . . $ resof12_t#f. = 256; |
| 272 | } |
| 273 | |
| 274 | void resof12_set_qcxx . . $ resof12_set_q(2abd089afca10685).(resof12_tcxx . . $ resof12_t# *rf, int q) |
| 275 | { |
| 276 | if(q < 1) |
| 277 | rf->qcxx . . $ resof12_t#q. = 256; |
| 278 | else |
| 279 | rf->qcxx . . $ resof12_t#q. = 256 / q; |
| 280 | } |
| 281 | |
| 282 | void resof12_initcxx . . $ resof12_init(2abd089afca10685).(resof12_tcxx . . $ resof12_t# *rf, int fs) |
| 283 | { |
| 284 | rf->ratecxx . . $ resof12_t#rate. = fs; |
| 285 | rf->d1cxx . . $ resof12_t#d1. = rf->d2cxx . . $ resof12_t#d2. = 0; |
| 286 | resof12_set_fcxx . . $ resof12_set_f(2abd089afca10685).(rf, 400); |
| 287 | resof12_set_qcxx . . $ resof12_set_q(2abd089afca10685).(rf, 10); |
| 288 | } |
| 289 | |
| 290 | |
| 291 | |
| 292 | /*-------------------------------------------------------------------- |
| 293 | resof12s_t - Resonant stereo 12 dB/oct state variable filter |
| 294 | --------------------------------------------------------------------*/ |
| 295 | |
| 296 | /* In-place LP */ |
| 297 | void resof12s_process_lpcxx . . $ resof12s_process_lp(53e4e8e6d1ed10f1).(resof12s_tcxx . . $ resof12s_t# *rf, int *buf, unsigned frames) |
| 298 | { |
| 299 | int d1l = rf->d1lcxx . . $ resof12s_t#d1l.; |
| 300 | int d1r = rf->d1rcxx . . $ resof12s_t#d1r.; |
| 301 | int d2l = rf->d2lcxx . . $ resof12s_t#d2l.; |
| 302 | int d2r = rf->d2rcxx . . $ resof12s_t#d2r.; |
| 303 | int f = rf->fcxx . . $ resof12s_t#f.; |
| 304 | int q = rf->qcxx . . $ resof12s_t#q.; |
| 305 | int ll, bl, hl; |
| 306 | int lr, br, hr; |
| 307 | unsigned s; |
| 308 | frames <<= 1; |
| 309 | for(s = 0; s < frames; s += 2) |
| 310 | { |
| 311 | ll = d2l + (f*d1l >> 8); |
| 312 | lr = d2r + (f*d1r >> 8); |
| 313 | hl = (buf[s] << 4) - ll - (q*d1l >> 8); |
| 314 | hr = (buf[s+1] << 4) - lr - (q*d1r >> 8); |
| 315 | bl = (f*hl >> 8) + d1l; |
| 316 | br = (f*hr >> 8) + d1r; |
| 317 | buf[s] = ll >> 4; |
| 318 | buf[s+1] = lr >> 4; |
| 319 | d1l = bl; |
| 320 | d1r = br; |
| 321 | d2l = ll; |
| 322 | d2r = lr; |
| 323 | } |
| 324 | rf->d1lcxx . . $ resof12s_t#d1l. = d1l; |
| 325 | rf->d1rcxx . . $ resof12s_t#d1r. = d1r; |
| 326 | rf->d2lcxx . . $ resof12s_t#d2l. = d2l; |
| 327 | rf->d2rcxx . . $ resof12s_t#d2r. = d2r; |
| 328 | } |
| 329 | |
| 330 | void resof12s_set_fcxx . . $ resof12s_set_f(2df552924bcf9158).(resof12s_tcxx . . $ resof12s_t# *rf, int f) |
| 331 | { |
| 332 | rf->fcxx . . $ resof12s_t#f. = (int)(512.0 * sin(M_PIcxx . . $ `<external>/11263917310783833665/math.h:1121:10`! * (float)f / rf->ratecxx . . $ resof12s_t#rate.)); |
| 333 | if(rf->fcxx . . $ resof12s_t#f. > 256) |
| 334 | rf->fcxx . . $ resof12s_t#f. = 256; |
| 335 | } |
| 336 | |
| 337 | void resof12s_set_qcxx . . $ resof12s_set_q(2df552924bcf9158).(resof12s_tcxx . . $ resof12s_t# *rf, int q) |
| 338 | { |
| 339 | if(q < 1) |
| 340 | rf->qcxx . . $ resof12s_t#q. = 256; |
| 341 | else |
| 342 | rf->qcxx . . $ resof12s_t#q. = 256 / q; |
| 343 | } |
| 344 | |
| 345 | void resof12s_initcxx . . $ resof12s_init(2df552924bcf9158).(resof12s_tcxx . . $ resof12s_t# *rf, int fs) |
| 346 | { |
| 347 | rf->ratecxx . . $ resof12s_t#rate. = fs; |
| 348 | rf->d1lcxx . . $ resof12s_t#d1l. = rf->d2lcxx . . $ resof12s_t#d2l. = 0; |
| 349 | rf->d1rcxx . . $ resof12s_t#d1r. = rf->d2rcxx . . $ resof12s_t#d2r. = 0; |
| 350 | resof12s_set_fcxx . . $ resof12s_set_f(2df552924bcf9158).(rf, 400); |
| 351 | resof12s_set_qcxx . . $ resof12s_set_q(2df552924bcf9158).(rf, 10); |
| 352 | } |
| 353 |