dcfilter: more correct algorithm. doesn't sound any different. speed unchanged.

This commit is contained in:
goyuken 2012-12-09 20:02:43 +00:00
parent 34a6248606
commit b9f37d7ed8
2 changed files with 30 additions and 19 deletions

View File

@ -738,7 +738,9 @@ namespace BizHawk.Emulation.Consoles.GB
void InitSound() void InitSound()
{ {
resampler = new Sound.Utilities.SpeexResampler(2, 2097152, 44100, 2097152, 44100, null, this); resampler = new Sound.Utilities.SpeexResampler(2, 2097152, 44100, 2097152, 44100, null, this);
dcfilter = Sound.Utilities.DCFilter.AsISyncSoundProvider(resampler, 65536); //dcfilter = Sound.Utilities.DCFilter.AsISyncSoundProvider(resampler, 65536);
// lowpass filtering on an actual GB was probably pretty aggressive?
dcfilter = Sound.Utilities.DCFilter.AsISyncSoundProvider(resampler, 2048);
} }
void DisposeSound() void DisposeSound()

View File

@ -20,10 +20,21 @@ namespace BizHawk.Emulation.Sound.Utilities
ISoundProvider input; ISoundProvider input;
ISyncSoundProvider syncinput; ISyncSoundProvider syncinput;
int sumL = 0; int latchL = 0;
int sumR = 0; int latchR = 0;
int accumL = 0;
int accumR = 0;
Queue<short> buffer; static int DepthFromFilterwidth(int filterwidth)
{
int ret = -2;
while (filterwidth > 0)
{
filterwidth >>= 1;
ret++;
}
return ret;
}
int depth; int depth;
@ -48,14 +59,11 @@ namespace BizHawk.Emulation.Sound.Utilities
DCFilter(ISoundProvider input, ISyncSoundProvider syncinput, int filterwidth) DCFilter(ISoundProvider input, ISyncSoundProvider syncinput, int filterwidth)
{ {
if (filterwidth < 1 || filterwidth > 65536) if (filterwidth < 8 || filterwidth > 65536)
throw new ArgumentOutOfRangeException(); throw new ArgumentOutOfRangeException();
this.input = input; this.input = input;
this.syncinput = syncinput; this.syncinput = syncinput;
depth = filterwidth; depth = DepthFromFilterwidth(filterwidth);
buffer = new Queue<short>(depth * 2);
for (int i = 0; i < depth * 2; i++)
buffer.Enqueue(0);
} }
/// <summary> /// <summary>
@ -72,16 +80,17 @@ namespace BizHawk.Emulation.Sound.Utilities
{ {
for (int i = 0; i < length; i += 2) for (int i = 0; i < length; i += 2)
{ {
sumL -= buffer.Dequeue(); int L = samplesin[i] << 12;
sumR -= buffer.Dequeue(); int R = samplesin[i + 1] << 12;
short L = samplesin[i]; accumL -= accumL >> depth;
short R = samplesin[i + 1]; accumR -= accumR >> depth;
sumL += L; accumL += L - latchL;
sumR += R; accumR += R - latchR;
buffer.Enqueue(L); latchL = L;
buffer.Enqueue(R); latchR = R;
int bigL = L - sumL / depth;
int bigR = R - sumR / depth; int bigL = accumL >> 12;
int bigR = accumR >> 12;
// check for clipping // check for clipping
if (bigL > 32767) if (bigL > 32767)
samplesout[i] = 32767; samplesout[i] = 32767;