-
Notifications
You must be signed in to change notification settings - Fork 1
/
YmFilters.cpp
189 lines (158 loc) · 3.4 KB
/
YmFilters.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
/**
* \file YmFilters.cpp
* \brief libstsound Audio Filter implementation file
* This file implement some audio filter used for improve audio output quality
*/
/*
* libstsound is an AY-3-8192, YM2149 and clone PSG emulator
* Copyright (C) 2008-2011 CPCSDK crew ( http://github.com/cpcsdk/ )
* Based on ST-Sound
* Copyright (C) 1995-1999 Arnaud Carre ( http://leonard.oxg.free.fr )
*/
#include <cstring>
#include "YmFilters.h"
Filter::Filter()
{
}
DCRemover::DCRemover()
{
Reset();
}
void DCRemover::Reset()
{
memset(buffer, 0, DC_ADJUST_BUFFERLEN*sizeof(ymint));
pos = DC_ADJUST_BUFFERLEN - 1;
sum = 0;
full = 0; // Calculate DC Remove only on the number of sample in buffer, can limits start clicks
}
inline void DCRemover::AddSample(ymint sample)
{
pos = (pos+1)&(DC_ADJUST_BUFFERLEN-1);
#ifndef YM_SIMPLIFIED_FILTER
if(!full && (pos == (DC_ADJUST_BUFFERLEN - 1)))
{
full = 1;
}
#endif
sum -= buffer[pos];
sum += sample;
buffer[pos] = sample;
}
inline ymint DCRemover::GetResult()
{
#ifdef YM_SIMPLIFIED_FILTER
return buffer[pos] - (sum / DC_ADJUST_BUFFERLEN);
#else
if(full)
{
return buffer[pos] - (sum / DC_ADJUST_BUFFERLEN);
}
else
{
return buffer[pos] - (sum / (pos + 1));
}
#endif
}
SimpleLowPassFilter::SimpleLowPassFilter()
{
Reset();
}
void SimpleLowPassFilter::Reset()
{
delay_line[0] = 0;
delay_line[1] = 0;
delay_line[2] = 0;
}
inline void SimpleLowPassFilter::AddSample(ymint sample)
{
delay_line[0] = delay_line[1];
delay_line[1] = delay_line[2];
delay_line[2] = sample;
}
inline ymint SimpleLowPassFilter::GetResult()
{
return (delay_line[0] >> 2)
+ (delay_line[1] >> 1)
+ (delay_line[2] >> 2);
}
SimpleStereoEffectReducer::SimpleStereoEffectReducer()
{
Reset();
}
void SimpleStereoEffectReducer::Reset()
{
buffer.r = buffer.l = 0;
}
inline void SimpleStereoEffectReducer::AddSample(stereoSample sample)
{
buffer = sample;
}
inline stereoSample SimpleStereoEffectReducer::GetResult()
{
// Simple stereo reducer based on mid-side
ymint m, s;
stereoSample out;
m = buffer.r + buffer.l;
s = buffer.r - buffer.l;
out.r = (3*m + s) >> 2;
out.l = (3*m - s) >> 2;
return out;
}
FilterStereo::FilterStereo()
{
}
#if 0
FIRFilter::FIRFilter()
: N(512), delay(0), buffer(NULL), delay_line(NULL)
{
Reset();
}
FIRFilter::FIRFilter(ymint N, ymint delay)
: N(N), delay(delay), buffer(NULL), delay_line(NULL)
{
Reset();
}
FIRFilter::~FIRFilter()
{
free(buffer);
buffer = NULL;
free(delay_line);
delay_line = NULL
}
void FIRFilter::Reset()
{
free(buffer);
buffer = (ymint*)malloc(N*size(ymint));
if(buffer == NULL)
{
// TODO
}
memset(buffer, 0, N*sizeof(ymint));
pos = N - 1;
free(delay_line);
delay_line = (ymint*)malloc(delay*size(ymint));
if(buffer == NULL)
{
// TODO
}
memset(delay_line, 0, N*sizeof(ymint));
delay_pos = delay - 1;
}
void FIRFilter::AddSample(ymint sample)
{
delay_pos = (++delay_pos) % delay;
pos = (++pos) % N;
buffer[pos] = sample;
}
ymint FIRFilter::GetResult()
{
double sum = 0;
for(ymint i = 0; i < N; i++)
{
const ymint j = (pos + N - i) % N
sum += coef[i] * buffer[j];
}
delay_line[delay_pos] = sum/coef_sum;
return delay[(delay_pos + 1) % delay];
}
#endif // Unfinish