forked from kode54/libupse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
upse_ps1_spu_lowpass_filter.c
159 lines (128 loc) · 4.8 KB
/
upse_ps1_spu_lowpass_filter.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
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
/*
* UPSE: the unix playstation sound emulator.
*
* Filename: upse_ps1_spu_lowpass_filter.c
* Purpose: libupse: PS1 SPU band-limiting filter.
*
* Copyright (c) 2010 William Pitcock <[email protected]>
*
* UPSE is free software, released under the GNU General Public License,
* version 2.
*
* A copy of the GNU General Public License, version 2, is included in
* the UPSE source kit as COPYING.
*
* UPSE is offered without any warranty of any kind, explicit or implicit.
*/
#define _IN_SPU
#include "upse-types.h"
#include "upse-internal.h"
#include "upse-ps1-spu-base.h"
#include "upse-spu-internal.h"
#include "upse-ps1-spu-register-io.h"
#include "upse.h"
#include "upse-ps1-memory-manager.h"
#include <math.h>
void
upse_spu_lowpass_filter_reset(upse_spu_state_t *spu)
{
spu->lowpass.lx1 = 0;
spu->lowpass.lx2 = 0;
spu->lowpass.ly1 = 0;
spu->lowpass.ly2 = 0;
spu->lowpass.hx1[0] = 0;
spu->lowpass.hx2[0] = 0;
spu->lowpass.hy1[0] = 0;
spu->lowpass.hy2[0] = 0;
spu->lowpass.hx1[1] = 0;
spu->lowpass.hx2[1] = 0;
spu->lowpass.hy1[1] = 0;
spu->lowpass.hy2[1] = 0;
}
void
upse_spu_lowpass_filter_redesign(upse_spu_state_t *spu, int samplerate)
{
switch (samplerate)
{
case 48000:
spu->lowpass.la0 = 1.00320890889339290000;
spu->lowpass.la1 = -1.97516434134506300000;
spu->lowpass.la2 = 0.97243484967313087000;
spu->lowpass.lb1 = -1.97525280404731810000;
spu->lowpass.lb2 = 0.97555529586426892000;
spu->lowpass.ha0 = 1.52690772687271160000;
spu->lowpass.ha1 = -1.62653918974914990000;
spu->lowpass.ha2 = 0.57997976029249387000;
spu->lowpass.hb1 = -0.80955590379048203000;
spu->lowpass.hb2 = 0.28990420120653748000;
break;
default:
spu->lowpass.la0 = 1.00349314378906680000;
spu->lowpass.la1 = -1.97295980267143170000;
spu->lowpass.la2 = 0.97003400595243994000;
spu->lowpass.lb1 = -1.97306449030610280000;
spu->lowpass.lb2 = 0.97342246210683581000;
spu->lowpass.ha0 = 1.50796284998687450000;
spu->lowpass.ha1 = -1.48628361940858910000;
spu->lowpass.ha2 = 0.52606706092412581000;
spu->lowpass.hb1 = -0.71593574211151134000;
spu->lowpass.hb2 = 0.26368203361392234000;
break;
}
upse_spu_lowpass_filter_reset(spu);
}
#define EPSILON (1e-10f)
#define OVERALL_SCALE (0.87f)
#define OVERFLOW_CHECK(x) if(fabsf((x)) < EPSILON) (x) = 0
#define CLIP(x) { if(x > 32767) x = 32767; if(x < -32767) x = -32767; }
void
upse_spu_lowpass_filter_process(upse_spu_state_t *spu, s16 *samplebuf, int samplecount)
{
int i;
OVERFLOW_CHECK(spu->lowpass.lx1);
OVERFLOW_CHECK(spu->lowpass.lx2);
OVERFLOW_CHECK(spu->lowpass.ly1);
OVERFLOW_CHECK(spu->lowpass.ly2);
OVERFLOW_CHECK(spu->lowpass.hx1[0]);
OVERFLOW_CHECK(spu->lowpass.hx2[0]);
OVERFLOW_CHECK(spu->lowpass.hy1[0]);
OVERFLOW_CHECK(spu->lowpass.hy2[0]);
OVERFLOW_CHECK(spu->lowpass.hx1[1]);
OVERFLOW_CHECK(spu->lowpass.hx2[1]);
OVERFLOW_CHECK(spu->lowpass.hy1[1]);
OVERFLOW_CHECK(spu->lowpass.hy2[1]);
for (i = 0; i < samplecount; i++)
{
s32 in, out;
s32 l, r;
s32 mid, side;
l = samplebuf[0];
r = samplebuf[1];
mid = l + r;
side = l - r;
in = mid;
out = spu->lowpass.la0 * in + spu->lowpass.la1 * spu->lowpass.lx1 + spu->lowpass.la2 * spu->lowpass.lx2 - spu->lowpass.lb1 * spu->lowpass.ly1 - spu->lowpass.lb2 * spu->lowpass.ly2;
spu->lowpass.lx2 = spu->lowpass.lx1;
spu->lowpass.lx1 = in;
spu->lowpass.ly2 = spu->lowpass.ly1;
spu->lowpass.ly1 = out;
mid = out;
l = ((0.5) * (OVERALL_SCALE)) * (mid + side);
r = ((0.5) * (OVERALL_SCALE)) * (mid - side);
in = l;
out = spu->lowpass.ha0 * in + spu->lowpass.ha1 * spu->lowpass.hx1[0] + spu->lowpass.ha2 * spu->lowpass.hx2[0] - spu->lowpass.hb1 * spu->lowpass.hy1[0] - spu->lowpass.hb2 * spu->lowpass.hy2[0];
spu->lowpass.hx2[0] = spu->lowpass.hx1[0]; spu->lowpass.hx1[0] = in;
spu->lowpass.hy2[0] = spu->lowpass.hy1[0]; spu->lowpass.hy1[0] = out;
l = out;
in = r;
out = spu->lowpass.ha0 * in + spu->lowpass.ha1 * spu->lowpass.hx1[1] + spu->lowpass.ha2 * spu->lowpass.hx2[1] - spu->lowpass.hb1 * spu->lowpass.hy1[1] - spu->lowpass.hb2 * spu->lowpass.hy2[1];
spu->lowpass.hx2[1] = spu->lowpass.hx1[1]; spu->lowpass.hx1[1] = in;
spu->lowpass.hy2[1] = spu->lowpass.hy1[1]; spu->lowpass.hy1[1] = out;
r = out;
CLIP(l);
CLIP(r);
samplebuf[0] = l;
samplebuf[1] = r;
samplebuf += 2;
}
}