Coverage Report

Created: 2026-02-14 06:59

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/fdk-aac/libPCMutils/src/limiter.cpp
Line
Count
Source
1
/* -----------------------------------------------------------------------------
2
Software License for The Fraunhofer FDK AAC Codec Library for Android
3
4
© Copyright  1995 - 2020 Fraunhofer-Gesellschaft zur Förderung der angewandten
5
Forschung e.V. All rights reserved.
6
7
 1.    INTRODUCTION
8
The Fraunhofer FDK AAC Codec Library for Android ("FDK AAC Codec") is software
9
that implements the MPEG Advanced Audio Coding ("AAC") encoding and decoding
10
scheme for digital audio. This FDK AAC Codec software is intended to be used on
11
a wide variety of Android devices.
12
13
AAC's HE-AAC and HE-AAC v2 versions are regarded as today's most efficient
14
general perceptual audio codecs. AAC-ELD is considered the best-performing
15
full-bandwidth communications codec by independent studies and is widely
16
deployed. AAC has been standardized by ISO and IEC as part of the MPEG
17
specifications.
18
19
Patent licenses for necessary patent claims for the FDK AAC Codec (including
20
those of Fraunhofer) may be obtained through Via Licensing
21
(www.vialicensing.com) or through the respective patent owners individually for
22
the purpose of encoding or decoding bit streams in products that are compliant
23
with the ISO/IEC MPEG audio standards. Please note that most manufacturers of
24
Android devices already license these patent claims through Via Licensing or
25
directly from the patent owners, and therefore FDK AAC Codec software may
26
already be covered under those patent licenses when it is used for those
27
licensed purposes only.
28
29
Commercially-licensed AAC software libraries, including floating-point versions
30
with enhanced sound quality, are also available from Fraunhofer. Users are
31
encouraged to check the Fraunhofer website for additional applications
32
information and documentation.
33
34
2.    COPYRIGHT LICENSE
35
36
Redistribution and use in source and binary forms, with or without modification,
37
are permitted without payment of copyright license fees provided that you
38
satisfy the following conditions:
39
40
You must retain the complete text of this software license in redistributions of
41
the FDK AAC Codec or your modifications thereto in source code form.
42
43
You must retain the complete text of this software license in the documentation
44
and/or other materials provided with redistributions of the FDK AAC Codec or
45
your modifications thereto in binary form. You must make available free of
46
charge copies of the complete source code of the FDK AAC Codec and your
47
modifications thereto to recipients of copies in binary form.
48
49
The name of Fraunhofer may not be used to endorse or promote products derived
50
from this library without prior written permission.
51
52
You may not charge copyright license fees for anyone to use, copy or distribute
53
the FDK AAC Codec software or your modifications thereto.
54
55
Your modified versions of the FDK AAC Codec must carry prominent notices stating
56
that you changed the software and the date of any change. For modified versions
57
of the FDK AAC Codec, the term "Fraunhofer FDK AAC Codec Library for Android"
58
must be replaced by the term "Third-Party Modified Version of the Fraunhofer FDK
59
AAC Codec Library for Android."
60
61
3.    NO PATENT LICENSE
62
63
NO EXPRESS OR IMPLIED LICENSES TO ANY PATENT CLAIMS, including without
64
limitation the patents of Fraunhofer, ARE GRANTED BY THIS SOFTWARE LICENSE.
65
Fraunhofer provides no warranty of patent non-infringement with respect to this
66
software.
67
68
You may use this FDK AAC Codec software or modifications thereto only for
69
purposes that are authorized by appropriate patent licenses.
70
71
4.    DISCLAIMER
72
73
This FDK AAC Codec software is provided by Fraunhofer on behalf of the copyright
74
holders and contributors "AS IS" and WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES,
75
including but not limited to the implied warranties of merchantability and
76
fitness for a particular purpose. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
77
CONTRIBUTORS BE LIABLE for any direct, indirect, incidental, special, exemplary,
78
or consequential damages, including but not limited to procurement of substitute
79
goods or services; loss of use, data, or profits, or business interruption,
80
however caused and on any theory of liability, whether in contract, strict
81
liability, or tort (including negligence), arising in any way out of the use of
82
this software, even if advised of the possibility of such damage.
83
84
5.    CONTACT INFORMATION
85
86
Fraunhofer Institute for Integrated Circuits IIS
87
Attention: Audio and Multimedia Departments - FDK AAC LL
88
Am Wolfsmantel 33
89
91058 Erlangen, Germany
90
91
www.iis.fraunhofer.de/amm
92
amm-info@iis.fraunhofer.de
93
----------------------------------------------------------------------------- */
94
95
/**************************** PCM utility library ******************************
96
97
   Author(s):   Matthias Neusinger
98
99
   Description: Hard limiter for clipping prevention
100
101
*******************************************************************************/
102
103
#include "limiter.h"
104
#include "FDK_core.h"
105
106
/* library version */
107
#include "version.h"
108
/* library title */
109
0
#define TDLIMIT_LIB_TITLE "TD Limiter Lib"
110
111
/* create limiter */
112
TDLimiterPtr pcmLimiter_Create(unsigned int maxAttackMs, unsigned int releaseMs,
113
                               FIXP_DBL threshold, unsigned int maxChannels,
114
13.6k
                               UINT maxSampleRate) {
115
13.6k
  TDLimiterPtr limiter = NULL;
116
13.6k
  unsigned int attack, release;
117
13.6k
  FIXP_DBL attackConst, releaseConst, exponent;
118
13.6k
  INT e_ans;
119
120
  /* calc attack and release time in samples */
121
13.6k
  attack = (unsigned int)(maxAttackMs * maxSampleRate / 1000);
122
13.6k
  release = (unsigned int)(releaseMs * maxSampleRate / 1000);
123
124
  /* alloc limiter struct */
125
13.6k
  limiter = (TDLimiterPtr)FDKcalloc(1, sizeof(struct TDLimiter));
126
13.6k
  if (!limiter) return NULL;
127
128
  /* alloc max and delay buffers */
129
13.6k
  limiter->maxBuf = (FIXP_DBL*)FDKcalloc(attack + 1, sizeof(FIXP_DBL));
130
13.6k
  limiter->delayBuf =
131
13.6k
      (FIXP_DBL*)FDKcalloc(attack * maxChannels, sizeof(FIXP_DBL));
132
133
13.6k
  if (!limiter->maxBuf || !limiter->delayBuf) {
134
0
    pcmLimiter_Destroy(limiter);
135
0
    return NULL;
136
0
  }
137
138
  /* attackConst = pow(0.1, 1.0 / (attack + 1)) */
139
13.6k
  exponent = invFixp(attack + 1);
140
13.6k
  attackConst = fPow(FL2FXCONST_DBL(0.1f), 0, exponent, 0, &e_ans);
141
13.6k
  attackConst = scaleValue(attackConst, e_ans);
142
143
  /* releaseConst  = (float)pow(0.1, 1.0 / (release + 1)) */
144
13.6k
  exponent = invFixp(release + 1);
145
13.6k
  releaseConst = fPow(FL2FXCONST_DBL(0.1f), 0, exponent, 0, &e_ans);
146
13.6k
  releaseConst = scaleValue(releaseConst, e_ans);
147
148
  /* init parameters */
149
13.6k
  limiter->attackMs = maxAttackMs;
150
13.6k
  limiter->maxAttackMs = maxAttackMs;
151
13.6k
  limiter->releaseMs = releaseMs;
152
13.6k
  limiter->attack = attack;
153
13.6k
  limiter->attackConst = attackConst;
154
13.6k
  limiter->releaseConst = releaseConst;
155
13.6k
  limiter->threshold = threshold;
156
13.6k
  limiter->channels = maxChannels;
157
13.6k
  limiter->maxChannels = maxChannels;
158
13.6k
  limiter->sampleRate = maxSampleRate;
159
13.6k
  limiter->maxSampleRate = maxSampleRate;
160
161
13.6k
  pcmLimiter_Reset(limiter);
162
163
13.6k
  return limiter;
164
13.6k
}
165
166
/* apply limiter */
167
TDLIMITER_ERROR pcmLimiter_Apply(TDLimiterPtr limiter, PCM_LIM* samplesIn,
168
                                 INT_PCM* samplesOut, FIXP_DBL* pGainPerSample,
169
1.45M
                                 const INT scaling, const UINT nSamples) {
170
1.45M
  unsigned int i, j;
171
1.45M
  FIXP_DBL tmp2;
172
1.45M
  FIXP_DBL tmp, old, gain, additionalGain = 0;
173
1.45M
  FIXP_DBL minGain = FL2FXCONST_DBL(1.0f / (1 << 1));
174
1.45M
  UINT additionalGainAvailable = 1;
175
176
1.45M
  if (limiter == NULL) return TDLIMIT_INVALID_HANDLE;
177
178
1.45M
  {
179
1.45M
    unsigned int channels = limiter->channels;
180
1.45M
    unsigned int attack = limiter->attack;
181
1.45M
    FIXP_DBL attackConst = limiter->attackConst;
182
1.45M
    FIXP_DBL releaseConst = limiter->releaseConst;
183
1.45M
    FIXP_DBL threshold = limiter->threshold >> scaling;
184
185
1.45M
    FIXP_DBL max = limiter->max;
186
1.45M
    FIXP_DBL* maxBuf = limiter->maxBuf;
187
1.45M
    unsigned int maxBufIdx = limiter->maxBufIdx;
188
1.45M
    FIXP_DBL cor = limiter->cor;
189
1.45M
    FIXP_DBL* delayBuf = limiter->delayBuf;
190
1.45M
    unsigned int delayBufIdx = limiter->delayBufIdx;
191
192
1.45M
    FIXP_DBL smoothState0 = limiter->smoothState0;
193
194
1.45M
    if (limiter->scaling != scaling) {
195
8.36k
      scaleValuesSaturate(delayBuf, attack * channels,
196
8.36k
                          limiter->scaling - scaling);
197
8.36k
      scaleValuesSaturate(maxBuf, attack + 1, limiter->scaling - scaling);
198
8.36k
      max = scaleValueSaturate(max, limiter->scaling - scaling);
199
8.36k
      limiter->scaling = scaling;
200
8.36k
    }
201
202
1.45M
    if (pGainPerSample == NULL) {
203
1.32M
      additionalGainAvailable = 0;
204
1.32M
    }
205
206
2.80G
    for (i = 0; i < nSamples; i++) {
207
      /* get maximum absolute sample value of all channels, including the
208
       * additional gain. */
209
2.80G
      tmp = (FIXP_DBL)0;
210
6.39G
      for (j = 0; j < channels; j++) {
211
3.59G
        tmp2 = PCM_LIM2FIXP_DBL(samplesIn[j]);
212
3.59G
        tmp2 =
213
3.59G
            (tmp2 == (FIXP_DBL)MINVAL_DBL) ? (FIXP_DBL)MAXVAL_DBL : fAbs(tmp2);
214
3.59G
        tmp = fMax(tmp, tmp2);
215
3.59G
      }
216
217
2.80G
      if (additionalGainAvailable) {
218
238M
        additionalGain = pGainPerSample[i];
219
238M
        tmp = fMult(tmp, additionalGain);
220
238M
      }
221
222
      /* set threshold as lower border to save calculations in running maximum
223
       * algorithm */
224
2.80G
      tmp = fMax(tmp, threshold);
225
226
      /* running maximum */
227
2.80G
      old = maxBuf[maxBufIdx];
228
2.80G
      maxBuf[maxBufIdx] = tmp;
229
230
2.80G
      if (tmp >= max) {
231
        /* new sample is greater than old maximum, so it is the new maximum */
232
2.70G
        max = tmp;
233
2.70G
      } else if (old < max) {
234
        /* maximum does not change, as the sample, which has left the window was
235
           not the maximum */
236
95.5M
      } else {
237
        /* the old maximum has left the window, we have to search the complete
238
           buffer for the new max */
239
2.84M
        max = maxBuf[0];
240
1.32G
        for (j = 1; j <= attack; j++) {
241
1.32G
          max = fMax(max, maxBuf[j]);
242
1.32G
        }
243
2.84M
      }
244
2.80G
      maxBufIdx++;
245
2.80G
      if (maxBufIdx >= attack + 1) maxBufIdx = 0;
246
247
      /* calc gain */
248
      /* gain is downscaled by one, so that gain = 1.0 can be represented */
249
2.80G
      if (max > threshold) {
250
100M
        gain = fDivNorm(threshold, max) >> 1;
251
2.70G
      } else {
252
2.70G
        gain = FL2FXCONST_DBL(1.0f / (1 << 1));
253
2.70G
      }
254
255
      /* gain smoothing, method: TDL_EXPONENTIAL */
256
      /* first order IIR filter with attack correction to avoid overshoots */
257
258
      /* correct the 'aiming' value of the exponential attack to avoid the
259
       * remaining overshoot */
260
2.80G
      if (gain < smoothState0) {
261
39.0M
        cor = fMin(cor,
262
39.0M
                   fMultDiv2((gain - fMultDiv2(FL2FXCONST_SGL(0.1f * (1 << 1)),
263
39.0M
                                               smoothState0)),
264
39.0M
                             FL2FXCONST_SGL(1.11111111f / (1 << 1)))
265
39.0M
                       << 2);
266
2.76G
      } else {
267
2.76G
        cor = gain;
268
2.76G
      }
269
270
      /* smoothing filter */
271
2.80G
      if (cor < smoothState0) {
272
39.0M
        smoothState0 =
273
39.0M
            fMult(attackConst, (smoothState0 - cor)) + cor; /* attack */
274
39.0M
        smoothState0 = fMax(smoothState0, gain); /* avoid overshooting target */
275
2.76G
      } else {
276
        /* sign inversion twice to round towards +infinity,
277
           so that gain can converge to 1.0 again,
278
           for bit-identical output when limiter is not active */
279
2.76G
        smoothState0 =
280
2.76G
            -fMult(releaseConst, -(smoothState0 - cor)) + cor; /* release */
281
2.76G
      }
282
283
2.80G
      gain = smoothState0;
284
285
2.80G
      FIXP_DBL* p_delayBuf = &delayBuf[delayBufIdx * channels + 0];
286
2.80G
      if (gain < FL2FXCONST_DBL(1.0f / (1 << 1))) {
287
360M
        gain <<= 1;
288
        /* lookahead delay, apply gain */
289
813M
        for (j = 0; j < channels; j++) {
290
453M
          tmp = p_delayBuf[j];
291
292
453M
          if (additionalGainAvailable) {
293
66.1M
            p_delayBuf[j] = fMult((FIXP_PCM_LIM)samplesIn[j], additionalGain);
294
387M
          } else {
295
387M
            p_delayBuf[j] = PCM_LIM2FIXP_DBL(samplesIn[j]);
296
387M
          }
297
298
          /* Apply gain to delayed signal */
299
453M
          tmp = fMultDiv2(tmp, gain);
300
#if (SAMPLE_BITS == DFRACT_BITS)
301
          samplesOut[j] = (INT_PCM)FX_DBL2FX_PCM(
302
              (FIXP_DBL)SATURATE_LEFT_SHIFT(tmp, scaling + 1, DFRACT_BITS));
303
#else
304
453M
          samplesOut[j] = (INT_PCM)FX_DBL2FX_PCM((FIXP_DBL)SATURATE_LEFT_SHIFT(
305
453M
              tmp + ((FIXP_DBL)0x8000 >> (scaling + 1)), scaling + 1,
306
453M
              DFRACT_BITS));
307
453M
#endif
308
453M
        }
309
360M
        gain >>= 1;
310
2.44G
      } else {
311
        /* lookahead delay, apply gain=1.0f */
312
5.57G
        for (j = 0; j < channels; j++) {
313
3.13G
          tmp = p_delayBuf[j];
314
3.13G
          if (additionalGainAvailable) {
315
427M
            p_delayBuf[j] = fMult((FIXP_PCM_LIM)samplesIn[j], additionalGain);
316
2.70G
          } else {
317
2.70G
            p_delayBuf[j] = PCM_LIM2FIXP_DBL(samplesIn[j]);
318
2.70G
          }
319
320
#if (SAMPLE_BITS == DFRACT_BITS)
321
          samplesOut[j] = (INT_PCM)FX_DBL2FX_PCM(
322
              (FIXP_DBL)SATURATE_LEFT_SHIFT(tmp, scaling, DFRACT_BITS));
323
#else
324
3.13G
          samplesOut[j] = (INT_PCM)FX_DBL2FX_PCM((FIXP_DBL)SATURATE_LEFT_SHIFT(
325
3.13G
              (tmp >> 1) + ((FIXP_DBL)0x8000 >> (scaling + 1)), scaling + 1,
326
3.13G
              DFRACT_BITS));
327
3.13G
#endif
328
3.13G
        }
329
2.44G
      }
330
331
2.80G
      delayBufIdx++;
332
2.80G
      if (delayBufIdx >= attack) {
333
14.0M
        delayBufIdx = 0;
334
14.0M
      }
335
336
      /* save minimum gain factor */
337
2.80G
      if (gain < minGain) {
338
30.7M
        minGain = gain;
339
30.7M
      }
340
341
      /* advance sample pointer by <channel> samples */
342
2.80G
      samplesIn += channels;
343
2.80G
      samplesOut += channels;
344
2.80G
    }
345
346
1.45M
    limiter->max = max;
347
1.45M
    limiter->maxBufIdx = maxBufIdx;
348
1.45M
    limiter->cor = cor;
349
1.45M
    limiter->delayBufIdx = delayBufIdx;
350
351
1.45M
    limiter->smoothState0 = smoothState0;
352
353
1.45M
    limiter->minGain = minGain;
354
355
1.45M
    return TDLIMIT_OK;
356
1.45M
  }
357
1.45M
}
358
359
/* set limiter threshold */
360
TDLIMITER_ERROR pcmLimiter_SetThreshold(TDLimiterPtr limiter,
361
4.83k
                                        FIXP_DBL threshold) {
362
4.83k
  if (limiter == NULL) return TDLIMIT_INVALID_HANDLE;
363
364
4.83k
  limiter->threshold = threshold;
365
366
4.83k
  return TDLIMIT_OK;
367
4.83k
}
368
369
/* reset limiter */
370
13.6k
TDLIMITER_ERROR pcmLimiter_Reset(TDLimiterPtr limiter) {
371
13.6k
  if (limiter != NULL) {
372
13.6k
    limiter->maxBufIdx = 0;
373
13.6k
    limiter->delayBufIdx = 0;
374
13.6k
    limiter->max = (FIXP_DBL)0;
375
13.6k
    limiter->cor = FL2FXCONST_DBL(1.0f / (1 << 1));
376
13.6k
    limiter->smoothState0 = FL2FXCONST_DBL(1.0f / (1 << 1));
377
13.6k
    limiter->minGain = FL2FXCONST_DBL(1.0f / (1 << 1));
378
13.6k
    limiter->scaling = 0;
379
380
13.6k
    FDKmemset(limiter->maxBuf, 0, (limiter->attack + 1) * sizeof(FIXP_DBL));
381
13.6k
    FDKmemset(limiter->delayBuf, 0,
382
13.6k
              limiter->attack * limiter->channels * sizeof(FIXP_DBL));
383
13.6k
  } else {
384
0
    return TDLIMIT_INVALID_HANDLE;
385
0
  }
386
387
13.6k
  return TDLIMIT_OK;
388
13.6k
}
389
390
/* destroy limiter */
391
13.6k
TDLIMITER_ERROR pcmLimiter_Destroy(TDLimiterPtr limiter) {
392
13.6k
  if (limiter != NULL) {
393
13.6k
    FDKfree(limiter->maxBuf);
394
13.6k
    FDKfree(limiter->delayBuf);
395
396
13.6k
    FDKfree(limiter);
397
13.6k
  } else {
398
0
    return TDLIMIT_INVALID_HANDLE;
399
0
  }
400
13.6k
  return TDLIMIT_OK;
401
13.6k
}
402
403
/* get delay in samples */
404
1.45M
unsigned int pcmLimiter_GetDelay(TDLimiterPtr limiter) {
405
1.45M
  FDK_ASSERT(limiter != NULL);
406
1.45M
  return limiter->attack;
407
1.45M
}
408
409
/* get maximum gain reduction of last processed block */
410
0
INT pcmLimiter_GetMaxGainReduction(TDLimiterPtr limiter) {
411
  /* maximum gain reduction in dB = -20 * log10(limiter->minGain)
412
     = -20 * log2(limiter->minGain)/log2(10) = -6.0206*log2(limiter->minGain) */
413
0
  int e_ans;
414
0
  FIXP_DBL loggain, maxGainReduction;
415
416
0
  FDK_ASSERT(limiter != NULL);
417
418
0
  loggain = fLog2(limiter->minGain, 1, &e_ans);
419
420
0
  maxGainReduction = fMult(loggain, FL2FXCONST_DBL(-6.0206f / (1 << 3)));
421
422
0
  return fixp_roundToInt(maxGainReduction, (e_ans + 3));
423
0
}
424
425
/* set number of channels */
426
TDLIMITER_ERROR pcmLimiter_SetNChannels(TDLimiterPtr limiter,
427
1.45M
                                        unsigned int nChannels) {
428
1.45M
  if (limiter == NULL) return TDLIMIT_INVALID_HANDLE;
429
430
1.45M
  if (nChannels > limiter->maxChannels) return TDLIMIT_INVALID_PARAMETER;
431
432
1.45M
  limiter->channels = nChannels;
433
  // pcmLimiter_Reset(limiter);
434
435
1.45M
  return TDLIMIT_OK;
436
1.45M
}
437
438
/* set sampling rate */
439
TDLIMITER_ERROR pcmLimiter_SetSampleRate(TDLimiterPtr limiter,
440
1.45M
                                         UINT sampleRate) {
441
1.45M
  unsigned int attack, release;
442
1.45M
  FIXP_DBL attackConst, releaseConst, exponent;
443
1.45M
  INT e_ans;
444
445
1.45M
  if (limiter == NULL) return TDLIMIT_INVALID_HANDLE;
446
447
1.45M
  if (sampleRate > limiter->maxSampleRate) return TDLIMIT_INVALID_PARAMETER;
448
449
  /* update attack and release time in samples */
450
1.34M
  attack = (unsigned int)(limiter->attackMs * sampleRate / 1000);
451
1.34M
  release = (unsigned int)(limiter->releaseMs * sampleRate / 1000);
452
453
  /* attackConst = pow(0.1, 1.0 / (attack + 1)) */
454
1.34M
  exponent = invFixp(attack + 1);
455
1.34M
  attackConst = fPow(FL2FXCONST_DBL(0.1f), 0, exponent, 0, &e_ans);
456
1.34M
  attackConst = scaleValue(attackConst, e_ans);
457
458
  /* releaseConst  = (float)pow(0.1, 1.0 / (release + 1)) */
459
1.34M
  exponent = invFixp(release + 1);
460
1.34M
  releaseConst = fPow(FL2FXCONST_DBL(0.1f), 0, exponent, 0, &e_ans);
461
1.34M
  releaseConst = scaleValue(releaseConst, e_ans);
462
463
1.34M
  limiter->attack = attack;
464
1.34M
  limiter->attackConst = attackConst;
465
1.34M
  limiter->releaseConst = releaseConst;
466
1.34M
  limiter->sampleRate = sampleRate;
467
468
  /* reset */
469
  // pcmLimiter_Reset(limiter);
470
471
1.34M
  return TDLIMIT_OK;
472
1.45M
}
473
474
/* set attack time */
475
TDLIMITER_ERROR pcmLimiter_SetAttack(TDLimiterPtr limiter,
476
4.83k
                                     unsigned int attackMs) {
477
4.83k
  unsigned int attack;
478
4.83k
  FIXP_DBL attackConst, exponent;
479
4.83k
  INT e_ans;
480
481
4.83k
  if (limiter == NULL) return TDLIMIT_INVALID_HANDLE;
482
483
4.83k
  if (attackMs > limiter->maxAttackMs) return TDLIMIT_INVALID_PARAMETER;
484
485
  /* calculate attack time in samples */
486
4.83k
  attack = (unsigned int)(attackMs * limiter->sampleRate / 1000);
487
488
  /* attackConst = pow(0.1, 1.0 / (attack + 1)) */
489
4.83k
  exponent = invFixp(attack + 1);
490
4.83k
  attackConst = fPow(FL2FXCONST_DBL(0.1f), 0, exponent, 0, &e_ans);
491
4.83k
  attackConst = scaleValue(attackConst, e_ans);
492
493
4.83k
  limiter->attack = attack;
494
4.83k
  limiter->attackConst = attackConst;
495
4.83k
  limiter->attackMs = attackMs;
496
497
4.83k
  return TDLIMIT_OK;
498
4.83k
}
499
500
/* set release time */
501
TDLIMITER_ERROR pcmLimiter_SetRelease(TDLimiterPtr limiter,
502
0
                                      unsigned int releaseMs) {
503
0
  unsigned int release;
504
0
  FIXP_DBL releaseConst, exponent;
505
0
  INT e_ans;
506
507
0
  if (limiter == NULL) return TDLIMIT_INVALID_HANDLE;
508
509
  /* calculate  release time in samples */
510
0
  release = (unsigned int)(releaseMs * limiter->sampleRate / 1000);
511
512
  /* releaseConst  = (float)pow(0.1, 1.0 / (release + 1)) */
513
0
  exponent = invFixp(release + 1);
514
0
  releaseConst = fPow(FL2FXCONST_DBL(0.1f), 0, exponent, 0, &e_ans);
515
0
  releaseConst = scaleValue(releaseConst, e_ans);
516
517
0
  limiter->releaseConst = releaseConst;
518
0
  limiter->releaseMs = releaseMs;
519
520
0
  return TDLIMIT_OK;
521
0
}
522
523
/* Get library info for this module. */
524
0
TDLIMITER_ERROR pcmLimiter_GetLibInfo(LIB_INFO* info) {
525
0
  int i;
526
527
0
  if (info == NULL) {
528
0
    return TDLIMIT_INVALID_PARAMETER;
529
0
  }
530
531
  /* Search for next free tab */
532
0
  for (i = 0; i < FDK_MODULE_LAST; i++) {
533
0
    if (info[i].module_id == FDK_NONE) break;
534
0
  }
535
0
  if (i == FDK_MODULE_LAST) {
536
0
    return TDLIMIT_UNKNOWN;
537
0
  }
538
539
  /* Add the library info */
540
0
  info[i].module_id = FDK_TDLIMIT;
541
0
  info[i].version =
542
0
      LIB_VERSION(PCMUTIL_LIB_VL0, PCMUTIL_LIB_VL1, PCMUTIL_LIB_VL2);
543
0
  LIB_VERSION_STRING(info + i);
544
0
  info[i].build_date = PCMUTIL_LIB_BUILD_DATE;
545
0
  info[i].build_time = PCMUTIL_LIB_BUILD_TIME;
546
0
  info[i].title = TDLIMIT_LIB_TITLE;
547
548
  /* Set flags */
549
0
  info[i].flags = CAPF_LIMITER;
550
551
  /* Add lib info for FDK tools (if not yet done). */
552
0
  FDK_toolsGetLibInfo(info);
553
554
0
  return TDLIMIT_OK;
555
0
}