aboutsummaryrefslogtreecommitdiffstats
path: root/src/libclipper/clipper.c
blob: 2201051bdbb6cea424fe5a85338c5183ca096d08 (plain)
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/* Clipper implementation, based on code by Jonathan Olds
 *
 * (C) 2017 by Andreas Eversberg <jolly@eversberg.eu>
 * All Rights Reserved
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdio.h>
#include <math.h>
#include <stdint.h>
#include <stdlib.h>
#include "../libsample/sample.h"
#include "clipper.h"

static double clipper_lut[6000];

static double clipper_point = NAN;

void clipper_init(double point)
{
	double a;
	int i;

	if (point > 0.99)
		point = 0.99;
	if (point < 0.01)
		point = 0.01;
	clipper_point = point;

	a = M_PI / (2.0 * (1.0 - clipper_point));

	for (i = 0; i < 6000; i++)
		clipper_lut[i] = clipper_point + atan(a * i / 1000.0) / a;
}

void clipper_process(sample_t *samples, int length)
{
	int i;
	double val, inv, shiftmultval;
	int n, q;

	if (isnan(clipper_point)) {
		fprintf(stderr, "Clipper not initialized, aborting!\n");
		abort();
	}

	for (i = 0; i < length; i++) {
		val = samples[i];
		if (val < 0) {
			inv = -1.0;
			val = -val;
		} else
			inv = 1.0;
		shiftmultval = (val - clipper_point) * 1000.0;
		/* no clipping up to clipping point */
		if (shiftmultval <= 0.0)
			continue;
		n = (int)shiftmultval;
		q = n + 1;
		if (q >= 6000) {
			samples[i] = inv;
			continue;
		}
		/* get clipped value from lut, interpolate between table entries */
		val = clipper_lut[n] + (shiftmultval - (double)n) * (clipper_lut[q] - clipper_lut[n]);
		samples[i] = val * inv;
	}
}