summaryrefslogtreecommitdiff
path: root/dumb/dumb-kode54/src/helpers/clickrem.c
blob: 336b492def5f82a690aa842c5080362ec9d2c2bf (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*  _______         ____    __         ___    ___
 * \    _  \       \    /  \  /       \   \  /   /       '   '  '
 *  |  | \  \       |  |    ||         |   \/   |         .      .
 *  |  |  |  |      |  |    ||         ||\  /|  |
 *  |  |  |  |      |  |    ||         || \/ |  |         '  '  '
 *  |  |  |  |      |  |    ||         ||    |  |         .      .
 *  |  |_/  /        \  \__//          ||    |  |
 * /_______/ynamic    \____/niversal  /__\  /____\usic   /|  .  . ibliotheque
 *                                                      /  \
 *                                                     / .  \
 * clickrem.c - Click removal helpers.                / / \  \
 *                                                   | <  /   \_
 * By entheh.                                        |  \/ /\   /
 *                                                    \_  /  > /
 *                                                      | \ / /
 *                                                      |  ' /
 *                                                       \__/
 */

#include <stdlib.h>
#include <math.h>
#include "dumb.h"



typedef struct DUMB_CLICK DUMB_CLICK;


struct DUMB_CLICK_REMOVER
{
	DUMB_CLICK *click;
	int n_clicks;

	int offset;
};


struct DUMB_CLICK
{
	DUMB_CLICK *next;
	long pos;
	sample_t step;
};



DUMB_CLICK_REMOVER *dumb_create_click_remover(void)
{
	DUMB_CLICK_REMOVER *cr = malloc(sizeof(*cr));
	if (!cr) return NULL;

	cr->click = NULL;
	cr->n_clicks = 0;

	cr->offset = 0;

	return cr;
}



void dumb_record_click(DUMB_CLICK_REMOVER *cr, long pos, sample_t step)
{
	DUMB_CLICK *click;

	ASSERT(pos >= 0);

	if (!cr || !step) return;

	if (pos == 0) {
		cr->offset -= step;
		return;
	}

	click = malloc(sizeof(*click));
	if (!click) return;

	click->pos = pos;
	click->step = step;

	click->next = cr->click;
	cr->click = click;
	cr->n_clicks++;
}



static DUMB_CLICK *dumb_click_mergesort(DUMB_CLICK *click, int n_clicks)
{
	int i;
	DUMB_CLICK *c1, *c2, **cp;

	if (n_clicks <= 1) return click;

	/* Split the list into two */
	c1 = click;
	cp = &c1;
	for (i = 0; i < n_clicks; i += 2) cp = &(*cp)->next;
	c2 = *cp;
	*cp = NULL;

	/* Sort the sublists */
	c1 = dumb_click_mergesort(c1, (n_clicks + 1) >> 1);
	c2 = dumb_click_mergesort(c2, n_clicks >> 1);

	/* Merge them */
	cp = &click;
	while (c1 && c2) {
		if (c1->pos > c2->pos) {
			*cp = c2;
			c2 = c2->next;
		} else {
			*cp = c1;
			c1 = c1->next;
		}
		cp = &(*cp)->next;
	}
	if (c2)
		*cp = c2;
	else
		*cp = c1;

	return click;
}



void dumb_remove_clicks(DUMB_CLICK_REMOVER *cr, sample_t *samples, long length, int step, float halflife)
{
	DUMB_CLICK *click;
	long pos = 0;
	int offset;
	int factor;

	if (!cr) return;

	factor = (int)floor(pow(0.5, 1.0/halflife) * (1U << 31));

	click = dumb_click_mergesort(cr->click, cr->n_clicks);
	cr->click = NULL;
	cr->n_clicks = 0;

	length *= step;

	while (click) {
		DUMB_CLICK *next = click->next;
		int end = click->pos * step;
		ASSERT(end <= length);
		offset = cr->offset;
		if (offset < 0) {
			offset = -offset;
			while (pos < end) {
				samples[pos] -= offset;
				offset = (int)(((LONG_LONG)(offset << 1) * factor) >> 32);
				pos += step;
			}
			offset = -offset;
		} else {
			while (pos < end) {
				samples[pos] += offset;
				offset = (int)(((LONG_LONG)(offset << 1) * factor) >> 32);
				pos += step;
			}
		}
		cr->offset = offset - click->step;
		free(click);
		click = next;
	}

	offset = cr->offset;
	if (offset < 0) {
		offset = -offset;
		while (pos < length) {
			samples[pos] -= offset;
			offset = (int)((LONG_LONG)(offset << 1) * factor >> 32);
			pos += step;
		}
		offset = -offset;
	} else {
		while (pos < length) {
			samples[pos] += offset;
			offset = (int)((LONG_LONG)(offset << 1) * factor >> 32);
			pos += step;
		}
	}
	cr->offset = offset;
}



sample_t dumb_click_remover_get_offset(DUMB_CLICK_REMOVER *cr)
{
	return cr ? cr->offset : 0;
}



void dumb_destroy_click_remover(DUMB_CLICK_REMOVER *cr)
{
	if (cr) {
		DUMB_CLICK *click = cr->click;
		while (click) {
			DUMB_CLICK *next = click->next;
			free(click);
			click = next;
		}
		free(cr);
	}
}



DUMB_CLICK_REMOVER **dumb_create_click_remover_array(int n)
{
	int i;
	DUMB_CLICK_REMOVER **cr;
	if (n <= 0) return NULL;
	cr = malloc(n * sizeof(*cr));
	if (!cr) return NULL;
	for (i = 0; i < n; i++) cr[i] = dumb_create_click_remover();
	return cr;
}



void dumb_record_click_array(int n, DUMB_CLICK_REMOVER **cr, long pos, sample_t *step)
{
	if (cr) {
		int i;
		for (i = 0; i < n; i++)
			dumb_record_click(cr[i], pos, step[i]);
	}
}



void dumb_record_click_negative_array(int n, DUMB_CLICK_REMOVER **cr, long pos, sample_t *step)
{
	if (cr) {
		int i;
		for (i = 0; i < n; i++)
			dumb_record_click(cr[i], pos, -step[i]);
	}
}



void dumb_remove_clicks_array(int n, DUMB_CLICK_REMOVER **cr, sample_t **samples, long length, float halflife)
{
	if (cr) {
		int i;
		for (i = 0; i < n >> 1; i++) {
			dumb_remove_clicks(cr[i << 1], samples[i], length, 2, halflife);
			dumb_remove_clicks(cr[(i << 1) + 1], samples[i] + 1, length, 2, halflife);
		}
		if (n & 1)
			dumb_remove_clicks(cr[i << 1], samples[i], length, 1, halflife);
	}
}



void dumb_click_remover_get_offset_array(int n, DUMB_CLICK_REMOVER **cr, sample_t *offset)
{
	if (cr) {
		int i;
		for (i = 0; i < n; i++)
			if (cr[i]) offset[i] += cr[i]->offset;
	}
}



void dumb_destroy_click_remover_array(int n, DUMB_CLICK_REMOVER **cr)
{
	if (cr) {
		int i;
		for (i = 0; i < n; i++) dumb_destroy_click_remover(cr[i]);
		free(cr);
	}
}