123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- #include "stdafx.h"
- #include "Sndfile.h"
- #include "Tables.h"
- #include "../common/misc_util.h"
- #include "mpt/base/numbers.hpp"
- OPENMPT_NAMESPACE_BEGIN
- uint8 CSoundFile::FrequencyToCutOff(double frequency) const
- {
-
-
-
-
-
- double cutoff = (std::log(frequency) - 4.8737671609324025) * (m_SongFlags[SONG_EXFILTERRANGE] ? (20.0 / mpt::numbers::ln2) : (24.0 / mpt::numbers::ln2));
- Limit(cutoff, 0.0, 127.0);
- return mpt::saturate_round<uint8>(cutoff);
- }
- uint32 CSoundFile::CutOffToFrequency(uint32 nCutOff, int envModifier) const
- {
- MPT_ASSERT(nCutOff < 128);
- float computedCutoff = static_cast<float>(nCutOff * (envModifier + 256));
- float Fc;
- if(GetType() != MOD_TYPE_IMF)
- {
- Fc = 110.0f * std::pow(2.0f, 0.25f + computedCutoff / (m_SongFlags[SONG_EXFILTERRANGE] ? 20.0f * 512.0f : 24.0f * 512.0f));
- } else
- {
-
-
- Fc = 125.0f * std::pow(2.0f, computedCutoff * 6.0f / (127.0f * 512.0f));
- }
- int freq = mpt::saturate_round<int>(Fc);
- Limit(freq, 120, 20000);
- if(freq * 2 > (int)m_MixerSettings.gdwMixingFreq) freq = m_MixerSettings.gdwMixingFreq / 2;
- return static_cast<uint32>(freq);
- }
- int CSoundFile::SetupChannelFilter(ModChannel &chn, bool bReset, int envModifier) const
- {
- int cutoff = static_cast<int>(chn.nCutOff) + chn.nCutSwing;
- int resonance = static_cast<int>(chn.nResonance & 0x7F) + chn.nResSwing;
- Limit(cutoff, 0, 127);
- Limit(resonance, 0, 127);
- if(!m_playBehaviour[kMPTOldSwingBehaviour])
- {
- chn.nCutOff = (uint8)cutoff;
- chn.nCutSwing = 0;
- chn.nResonance = (uint8)resonance;
- chn.nResSwing = 0;
- }
-
- const int computedCutoff = cutoff * (envModifier + 256) / 256;
-
- if(m_playBehaviour[kITFilterBehaviour] && resonance == 0 && computedCutoff >= 254)
- {
- if(chn.rowCommand.IsNote() && !chn.rowCommand.IsPortamento() && !chn.nMasterChn && chn.triggerNote)
- {
-
-
- chn.dwFlags.reset(CHN_FILTER);
- }
- return -1;
- }
- chn.dwFlags.set(CHN_FILTER);
-
- const float dmpfac = std::pow(10.0f, -resonance * ((24.0f / 128.0f) / 20.0f));
- const float fc = CutOffToFrequency(cutoff, envModifier) * (2.0f * mpt::numbers::pi_v<float>);
- float d, e;
- if(m_playBehaviour[kITFilterBehaviour] && !m_SongFlags[SONG_EXFILTERRANGE])
- {
- const float r = m_MixerSettings.gdwMixingFreq / fc;
- d = dmpfac * r + dmpfac - 1.0f;
- e = r * r;
- } else
- {
- const float r = fc / m_MixerSettings.gdwMixingFreq;
- d = (1.0f - 2.0f * dmpfac) * r;
- LimitMax(d, 2.0f);
- d = (2.0f * dmpfac - d) / r;
- e = 1.0f / (r * r);
- }
- float fg = 1.0f / (1.0f + d + e);
- float fb0 = (d + e + e) / (1 + d + e);
- float fb1 = -e / (1.0f + d + e);
- #if defined(MPT_INTMIXER)
- #define MPT_FILTER_CONVERT(x) mpt::saturate_round<mixsample_t>((x) * (1 << MIXING_FILTER_PRECISION))
- #else
- #define MPT_FILTER_CONVERT(x) (x)
- #endif
- switch(chn.nFilterMode)
- {
- case FilterMode::HighPass:
- chn.nFilter_A0 = MPT_FILTER_CONVERT(1.0f - fg);
- chn.nFilter_B0 = MPT_FILTER_CONVERT(fb0);
- chn.nFilter_B1 = MPT_FILTER_CONVERT(fb1);
- #ifdef MPT_INTMIXER
- chn.nFilter_HP = -1;
- #else
- chn.nFilter_HP = 1.0f;
- #endif
- break;
- default:
- chn.nFilter_A0 = MPT_FILTER_CONVERT(fg);
- chn.nFilter_B0 = MPT_FILTER_CONVERT(fb0);
- chn.nFilter_B1 = MPT_FILTER_CONVERT(fb1);
- #ifdef MPT_INTMIXER
- if(chn.nFilter_A0 == 0)
- chn.nFilter_A0 = 1;
- chn.nFilter_HP = 0;
- #else
- chn.nFilter_HP = 0;
- #endif
- break;
- }
- #undef MPT_FILTER_CONVERT
- if (bReset)
- {
- chn.nFilter_Y[0][0] = chn.nFilter_Y[0][1] = 0;
- chn.nFilter_Y[1][0] = chn.nFilter_Y[1][1] = 0;
- }
- return computedCutoff;
- }
- OPENMPT_NAMESPACE_END
|