First post, by superfury
I want to prevent the emulator from sending direct current (of any voltage) to the output(SDL). If it does, it might be harmful to the speakers(DC kills the coil), depending on it's duration.
Strangely enough, when I enable the high pass filter (frequency can be anything), it generates hard noise in the background (rendering audio unhearable). The low pass filter works without problems (after the high pass filter, when enabled).
OPTINLINE static float calcSoundHighpassFilter(float cutoff_freq, float samplerate, float currentsample, float previoussample, float previousresult)
{
float RC = 1.0 / (cutoff_freq * 2 * 3.14);
float dt = 1.0 / samplerate;
float alpha = RC / (RC + dt);
return alpha * (previousresult + currentsample - previoussample);
}
OPTINLINE static float calcSoundLowpassFilter(float cutoff_freq, float samplerate, float currentsample, float previousresult)
{
float RC = (float)1.0f / (cutoff_freq * (float)2 * (float)3.14);
float dt = (float)1.0f / samplerate;
float alpha = dt / (RC + dt);
return previousresult + (alpha*(currentsample - previousresult));
}
OPTINLINE static void applySoundHighpassFilter(sword *currentsample, sword *sound_last_result, sword *sound_last_sample)
{
#ifdef SOUND_HIGHPASS
//We're using a high pass filter!
*sound_last_result = (sword)calcSoundHighpassFilter(SOUND_HIGHPASS, SW_SAMPLERATE, (float)*currentsample, *sound_last_sample, *sound_last_result); //High pass filter!
*sound_last_sample = *currentsample; //The last sample that was processed!
*currentsample = *sound_last_result; //Give the new result!
#endif
}
OPTINLINE static void applySoundLowpassFilter(sword *currentsample, sword *sound_last_result, sword *sound_last_sample)
{
#ifdef SOUND_LOWPASS
//We're using a high pass filter!
*sound_last_result = (sword)calcSoundLowpassFilter(SOUND_LOWPASS, SW_SAMPLERATE, (float)*currentsample, *sound_last_result); //Low pass filter!
*sound_last_sample = *currentsample; //The last sample that was processed!
*currentsample = *sound_last_result; //Give the new result!
#endif
}
OPTINLINE static void applySoundFilters(sword *leftsample, sword *rightsample)
{
//Our information for filtering!
static sword soundhigh_last_result_l = 0, soundhigh_last_sample_l = 0, soundhigh_last_result_r = 0, soundhigh_last_sample_r = 0; //High pass
static sword soundlow_last_result_l = 0, soundlow_last_sample_l = 0, soundlow_last_result_r = 0, soundlow_last_sample_r = 0;
//First, as high pass to filter anything too low!
applySoundHighpassFilter(leftsample,&soundhigh_last_result_l,&soundhigh_last_sample_l);
applySoundHighpassFilter(rightsample,&soundhigh_last_result_r,&soundhigh_last_sample_r);
//Finally, a low pass to filter anything too high (or createn during the high pass filtering)
applySoundLowpassFilter(leftsample,&soundlow_last_result_l,&soundlow_last_sample_l);
applySoundLowpassFilter(rightsample,&soundlow_last_result_r,&soundlow_last_sample_r);
}
The low pass fiter works. What's going wrong with the high pass filter? (based on http://stackoverflow.com/questions/13882038/i … ss-filters-in-c )
Author of the UniPCemu emulator.
UniPCemu Git repository
UniPCemu for Android, Windows, PSP, Vita and Switch on itch.io