Skip to content

Commit 34e2e43

Browse files
committed
SNES: Make S9xMixSamplesLowPass call S9xMixSamples
This is functionally identical, as the old code did the clipping before applying the filter anyway. It takes 150us more to go through the buffer a second time, to filter it. Which seems like a reasonable compromise for so much removed duplicated code.
1 parent 5c38d8a commit 34e2e43

File tree

2 files changed

+11
-91
lines changed

2 files changed

+11
-91
lines changed

retro-core/components/snes9x/src/soundux.c

Lines changed: 10 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ void S9xFixSoundAfterSnapshotLoad()
287287
}
288288
}
289289

290-
void S9xSetFilterCoefficient(int32_t tap, int32_t value)
290+
void S9xSetFilterCoefficient(int32_t tap, int8_t value)
291291
{
292292
FilterTaps [tap & 7] = value;
293293
if (value == 0 || (tap == 0 && value == 127))
@@ -308,7 +308,6 @@ void S9xSetSoundADSR(int32_t channel, int32_t attack_ind, int32_t decay_ind, int
308308
if(attack_rate == 1)
309309
attack_rate = 0;
310310

311-
312311
ch = &SoundData.channels[channel];
313312
ch->env_ind_attack = attack_ind;
314313
ch->env_ind_decay = decay_ind;
@@ -365,7 +364,7 @@ void S9xSetSoundType(int32_t channel, int32_t type_of_sound)
365364
SoundData.channels[channel].type = type_of_sound;
366365
}
367366

368-
void DecodeBlock(Channel* ch)
367+
static void DecodeBlock(Channel* ch)
369368
{
370369
int32_t out;
371370
uint8_t filter;
@@ -827,97 +826,18 @@ void S9xMixSamples(int16_t* buffer, int32_t sample_count)
827826

828827
void S9xMixSamplesLowPass(int16_t* buffer, int32_t sample_count, int32_t low_pass_range)
829828
{
830-
int32_t J;
831-
int32_t I;
832-
829+
S9xMixSamples(buffer, sample_count);
833830
/* Single-pole low-pass filter (6 dB/octave) */
834831
int32_t low_pass_factor_a = low_pass_range;
835832
int32_t low_pass_factor_b = 0x10000 - low_pass_factor_a;
836-
837-
if (SoundData.echo_enable)
838-
memset(EchoBuffer, 0, sample_count * sizeof(EchoBuffer [0]));
839-
memset(MixBuffer, 0, sample_count * sizeof(MixBuffer [0]));
840-
MixStereo(sample_count);
841-
842-
/* Mix and convert waveforms */
843-
if (SoundData.echo_enable && SoundData.echo_buffer_size)
833+
for (int32_t J = 0; J < sample_count; ++J)
844834
{
845-
/* 16-bit stereo sound with echo enabled ... */
846-
if (FilterTapDefinitionBitfield == 0)
847-
{
848-
/* ... but no filter defined. */
849-
for (J = 0; J < sample_count; J++)
850-
{
851-
int32_t *low_pass_sample = &MixOutputPrev[J & 0x1];
852-
int32_t E = Echo [SoundData.echo_ptr];
853-
Echo[SoundData.echo_ptr++] = (E * SoundData.echo_feedback) / 128 + EchoBuffer [J];
854-
855-
if (SoundData.echo_ptr >= SoundData.echo_buffer_size)
856-
SoundData.echo_ptr = 0;
857-
858-
I = (MixBuffer[J] * SoundData.master_volume [J & 1] + E * SoundData.echo_volume [J & 1]) / VOL_DIV16;
859-
CLIP16(I);
860-
861-
/* Apply low-pass filter */
862-
(*low_pass_sample) = ((*low_pass_sample) * low_pass_factor_a) + (I * low_pass_factor_b);
863-
/* 16.16 fixed point */
864-
(*low_pass_sample) >>= 16;
865-
866-
buffer[J] = (int16_t)(*low_pass_sample);
867-
}
868-
}
869-
else
870-
{
871-
/* ... with filter defined. */
872-
for (J = 0; J < sample_count; J++)
873-
{
874-
int32_t *low_pass_sample = &MixOutputPrev[J & 0x1];
875-
int32_t E;
876-
Loop [(Z - 0) & 15] = Echo [SoundData.echo_ptr];
877-
E = Loop [(Z - 0) & 15] * FilterTaps [0];
878-
if (FilterTapDefinitionBitfield & 0x02) E += Loop [(Z - 2) & 15] * FilterTaps [1];
879-
if (FilterTapDefinitionBitfield & 0x04) E += Loop [(Z - 4) & 15] * FilterTaps [2];
880-
if (FilterTapDefinitionBitfield & 0x08) E += Loop [(Z - 6) & 15] * FilterTaps [3];
881-
if (FilterTapDefinitionBitfield & 0x10) E += Loop [(Z - 8) & 15] * FilterTaps [4];
882-
if (FilterTapDefinitionBitfield & 0x20) E += Loop [(Z - 10) & 15] * FilterTaps [5];
883-
if (FilterTapDefinitionBitfield & 0x40) E += Loop [(Z - 12) & 15] * FilterTaps [6];
884-
if (FilterTapDefinitionBitfield & 0x80) E += Loop [(Z - 14) & 15] * FilterTaps [7];
885-
E /= 128;
886-
Z++;
887-
888-
Echo[SoundData.echo_ptr++] = (E * SoundData.echo_feedback) / 128 + EchoBuffer[J];
889-
890-
if (SoundData.echo_ptr >= SoundData.echo_buffer_size)
891-
SoundData.echo_ptr = 0;
892-
893-
I = (MixBuffer[J] * SoundData.master_volume [J & 1] + E * SoundData.echo_volume [J & 1]) / VOL_DIV16;
894-
CLIP16(I);
895-
896-
/* Apply low-pass filter */
897-
(*low_pass_sample) = ((*low_pass_sample) * low_pass_factor_a) + (I * low_pass_factor_b);
898-
/* 16.16 fixed point */
899-
(*low_pass_sample) >>= 16;
900-
901-
buffer[J] = (int16_t)(*low_pass_sample);
902-
}
903-
}
904-
}
905-
else
906-
{
907-
/* 16-bit mono or stereo sound, no echo */
908-
for (J = 0; J < sample_count; J++)
909-
{
910-
int32_t *low_pass_sample = &MixOutputPrev[J & 0x1];
911-
I = (MixBuffer[J] * SoundData.master_volume [J & 1]) / VOL_DIV16;
912-
CLIP16(I);
913-
914-
/* Apply low-pass filter */
915-
(*low_pass_sample) = ((*low_pass_sample) * low_pass_factor_a) + (I * low_pass_factor_b);
916-
/* 16.16 fixed point */
917-
(*low_pass_sample) >>= 16;
918-
919-
buffer[J] = (int16_t)(*low_pass_sample);
920-
}
835+
int32_t *low_pass_sample = &MixOutputPrev[J & 1];
836+
/* Apply low-pass filter */
837+
(*low_pass_sample) = ((*low_pass_sample) * low_pass_factor_a) + (buffer[J] * low_pass_factor_b);
838+
/* 16.16 fixed point */
839+
(*low_pass_sample) >>= 16;
840+
buffer[J] = (int16_t)(*low_pass_sample);
921841
}
922842
}
923843

retro-core/components/snes9x/src/soundux.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ void S9xSetEchoFeedback(int32_t echo_feedback);
123123
void S9xSetEchoEnable(uint8_t byte);
124124
void S9xSetEchoDelay(int32_t byte);
125125
void S9xSetEchoWriteEnable(uint8_t byte);
126-
void S9xSetFilterCoefficient(int32_t tap, int32_t value);
126+
void S9xSetFilterCoefficient(int32_t tap, int8_t value);
127127
void S9xSetFrequencyModulationEnable(uint8_t byte);
128128
void S9xSetEnvelopeRate(int32_t channel, uint32_t rate, int32_t direction, int32_t target, uint32_t mode);
129129
bool S9xSetSoundMode(int32_t channel, int32_t mode);

0 commit comments

Comments
 (0)