aboutsummaryrefslogtreecommitdiffstats
path: root/src/pcm/dsd2pcm/noiseshape.c
blob: ecd2f251d968e28ec4ea856783547231cb46a230 (plain) (blame)
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
#include <stdlib.h>
#include <string.h>

#include "noiseshape.h"

extern int noise_shape_init(
	noise_shape_ctx *ctx,
	int sos_count,
	const float *coeffs)
{
	int i;
	ctx->sos_count = sos_count;
	ctx->bbaa = coeffs;
	ctx->t1 = (float*) malloc(sizeof(float)*sos_count);
	if (!ctx->t1) goto escape1;
	ctx->t2 = (float*) malloc(sizeof(float)*sos_count);
	if (!ctx->t2) goto escape2;
	for (i=0; i<sos_count; ++i) {
		ctx->t1[i] = 0.f;
		ctx->t2[i] = 0.f;
	}
	return 0;
escape2:
	free(ctx->t1);
escape1:
	return -1;
}

extern void noise_shape_destroy(
	noise_shape_ctx *ctx)
{
	free(ctx->t1);
	free(ctx->t2);
}

extern int noise_shape_clone(
	const noise_shape_ctx *from,
	noise_shape_ctx *to)
{
	to->sos_count = from->sos_count;
	to->bbaa = from->bbaa;
	to->t1 = (float*) malloc(sizeof(float)*to->sos_count);
	if (!to->t1) goto error1;
	to->t2 = (float*) malloc(sizeof(float)*to->sos_count);
	if (!to->t2) goto error2;
	memcpy(to->t1,from->t1,sizeof(float)*to->sos_count);
	memcpy(to->t2,from->t2,sizeof(float)*to->sos_count);
	return 0;
error2:
	free(to->t1);
error1:
	return -1;
}

extern float noise_shape_get(noise_shape_ctx *ctx)
{
	int i;
	float acc;
	const float *c;
	acc = 0.0;
	c = ctx->bbaa;
	for (i=0; i<ctx->sos_count; ++i) {
		float t1i = ctx->t1[i];
		float t2i = ctx->t2[i];
		ctx->t2[i] = acc -= t1i * c[2] + t2i * c[3];
		acc += t1i * c[0] + t2i * c[1];
		c += 4;
	}
	return acc;
}

extern void noise_shape_update(noise_shape_ctx *ctx, float qerror)
{
	float *p;
	int i;
	for (i=0; i<ctx->sos_count; ++i) {
		ctx->t2[i] += qerror;
	}
	p = ctx->t1;
	ctx->t1 = ctx->t2;
	ctx->t2 = p;
}