-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlimiter.c
31 lines (25 loc) · 879 Bytes
/
limiter.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/*
* Idea for algoritm from https://www.kvraudio.com/forum/viewtopic.php?t=195315
* Optimized for int32_t by Dan Green
*/
#include "globals.h"
#include "limiter.h"
float MAX_SAMPLEVAL_L;
float THRESHOLD_COMPILED;
float THRESHOLD_PERCENT;
int32_t THRESHOLD_VALUE;
void init_limiter(uint32_t max_sample_val, float threshold_percent)
{
MAX_SAMPLEVAL_L=max_sample_val;
THRESHOLD_PERCENT=threshold_percent;
float m = (float)max_sample_val;
THRESHOLD_COMPILED = m * m * threshold_percent * (1.0 - threshold_percent);
THRESHOLD_VALUE = threshold_percent*max_sample_val;
}
int32_t limiter(int32_t val)
{
float tv = THRESHOLD_COMPILED / ((float)val); // value to be subtracted for incoming signal going above theshold
if (val > THRESHOLD_VALUE) return (MAX_SAMPLEVAL_L - tv) ;
else if (val < -THRESHOLD_VALUE) return (-MAX_SAMPLEVAL_L - tv) ;
else return val;
}