forked from TASEmulators/fceux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilter.cpp
209 lines (173 loc) · 4.41 KB
/
filter.cpp
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
/// \file
/// \brief Sound filtering code
#include "types.h"
#include "sound.h"
#include "x6502.h"
#include "fceu.h"
8000
#include "filter.h"
#include "fcoeffs.h"
#include <cmath>
#include <cstdio>
static int32 sq2coeffs[SQ2NCOEFFS];
static int32 coeffs[NCOEFFS];
static uint32 mrindex;
static uint32 mrratio;
void SexyFilter2(int32 *in, int32 count)
{
#ifdef moo
static int64 acc=0;
double x,p;
int64 c;
x=2*M_PI*6000/FSettings.SndRate;
p=((double)2-cos(x)) - sqrt(pow((double)2-cos(x),2) -1 );
c=p*0x100000;
//printf("%f\n",(double)c/0x100000);
#endif
static int64 acc=0;
while(count--)
{
int64 dropcurrent;
dropcurrent=( int32(uint32(*in)<<16)-acc)>>3;
acc+=dropcurrent;
*in=acc>>16;
in++;
//acc=((int64)0x100000-c)* *in + ((c*acc)>>20);
//*in=acc>>20;
//in++;
}
}
void SexyFilter(int32 *in, int32 *out, int32 count)
{
static int64 acc1=0,acc2=0;
int32 mul1,mul2,vmul;
mul1=(94<<16)/FSettings.SndRate;
mul2=(24<<16)/FSettings.SndRate;
vmul=(FSettings.SoundVolume<<16)*3/4/100;
//FCEU_DispMessage("SoundVolume %d, vmul %d",0,FSettings.SoundVolume,vmul);
if(FSettings.soundq) vmul/=4;
else vmul*=2; /* TODO: Increase volume in low quality sound rendering code itself */
while(count)
{
int64 ino=(int64)*in*vmul;
acc1+=((ino-acc1)*mul1)>>16;
acc2+=((ino-acc1-acc2)*mul2)>>16;
//printf("%d ",*in);
*in=0;
{
int32 t=(acc1-ino+acc2)>>16;
//if(t>32767 || t<-32768) printf("Flow: %d\n",t);
if(t>32767) t=32767;
if(t<-32768) t=-32768;
*out=t;
}
in++;
out++;
count--;
}
}
/* Returns number of samples written to out. */
/* leftover is set to the number of samples that need to be copied
from the end of in to the beginning of in.
*/
//static uint32 mva=1000;
/* This filtering code assumes that almost all input values stay below 32767.
Do not adjust the volume in the wlookup tables and the expansion sound
code to be higher, or you *might* overflow the FIR code.
*/
int32 NeoFilterSound(int32 *in, int32 *out, uint32 inlen, int32 *leftover)
{
uint32 x;
uint32 max;
int32 *outsave=out;
int32 count=0;
// for(x=0;x<inlen;x++)
// {
// if(in[x]>mva){ mva=in[x]; printf("%ld\n",in[x]);}
// }
max=(inlen-1)<<16;
if(FSettings.soundq==2)
for(x=mrindex;x<max;x+=mrratio)
{
int32 acc=0,acc2=0;
unsigned int c;
int32 *S,*D;
for(c=SQ2NCOEFFS,S=&in[(x>>16)-SQ2NCOEFFS],D=sq2coeffs;c;c--,D++)
{
acc+=(S[c]**D)>>6;
acc2+=(S[1+c]**D)>>6;
}
acc=((int64)acc*(65536-(x&65535))+(int64)acc2*(x&65535))>>(16+11);
*out=acc;
out++;
count++;
}
else
for(x=mrindex;x<max;x+=mrratio)
{
int32 acc=0,acc2=0;
unsigned int c;
const int32 *S,*D;
for(c=NCOEFFS,S=&in[(x>>16)-NCOEFFS],D=coeffs;c;c--,D++)
{
acc+=(S[c]**D)>>6;
acc2+=(S[1+c]**D)>>6;
}
acc=((int64)acc*(65536-(x&65535))+(int64)acc2*(x&65535))>>(16+11);
*out=acc;
out++;
count++;
}
mrindex=x-max;
if(FSettings.soundq==2)
{
mrindex+=SQ2NCOEFFS*65536;
*leftover=SQ2NCOEFFS+1;
}
else
{
mrindex+=NCOEFFS*65536;
*leftover=NCOEFFS+1;
}
if(GameExpSound.NeoFill)
GameExpSound.NeoFill(outsave,count);
SexyFilter(outsave,outsave,count);
if(FSettings.lowpass)
SexyFilter2(outsave,count);
return(count);
}
void MakeFilters(int32 rate)
{
const int32 *tabs[8]={C44100NTSC,C44100PAL,C48000NTSC,C48000PAL,C96000NTSC,
C96000PAL, nullptr, nullptr};
const int32 *sq2tabs[8]={SQ2C44100NTSC,SQ2C44100PAL,SQ2C48000NTSC,SQ2C48000PAL,
SQ2C96000NTSC,SQ2C96000PAL, nullptr, nullptr};
const int32 *tmp;
int32 x;
uint32 nco;
if(FSettings.soundq==2)
nco=SQ2NCOEFFS;
else
nco=NCOEFFS;
mrindex=(nco+1)<<16;
mrratio=(PAL?(int64)(PAL_CPU*65536):(int64)(NTSC_CPU*65536))/rate;
if(FSettings.soundq==2)
tmp=sq2tabs[(PAL?1:0)|(rate==48000?2:0)|(rate==96000?4:0)];
else
tmp=tabs[(PAL?1:0)|(rate==48000?2:0)|(rate==96000?4:0)];
if(FSettings.soundq==2)
for(x=0;x<SQ2NCOEFFS>>1;x++)
sq2coeffs[x]=sq2coeffs[SQ2NCOEFFS-1-x]=tmp[x];
else
for(x=0;x<NCOEFFS>>1;x++)
coeffs[x]=coeffs[NCOEFFS-1-x]=tmp[x];
#ifdef MOO
/* Some tests involving precision and error. */
{
static int64 acc=0;
int x;
for(x=0;x<SQ2NCOEFFS;x++)
acc+=(int64)32767*sq2coeffs[x];
printf("Foo: %lld\n",acc);
}
#endif
}