Code: Select all
float f;
float whole;
unsigned int pos = (phase * wavevector.size()) - 0.5;
f = modf(phase,&whole); //calulate the fraction
result = (wavevector[pos]*(1 - f)) + (wavevector[pos+1] * f);
Code: Select all
float f;
float whole;
unsigned int pos = (phase * wavevector.size()) - 0.5;
f = modf(phase,&whole); //calulate the fraction
result = (wavevector[pos]*(1 - f)) + (wavevector[pos+1] * f);
Code: Select all
// scale for tablesize
float pos = phase * tablesize;
// convert to integer (fast with sensible compiler options)
// casting to unsigned might or might not be marginally slower
int posInt = (int) pos;
// convert the integer back to float (also pretty fast)
// then subtract to get fractional part of position only
pos -= posInt;
// now the lerp can be done something like:
result = (1-pos) * wave[posInt] + pos * wave[posInt+1];
Code: Select all
for(int i = 0; i < nSamples; ++i)
{
x = (int)phase;
fraction = phase - x;
output[i] = waveTable[x] * (1.0f - fraction) + waveTable[x + 1] * fraction;
phase += rotation;
phaseWrap = (int)phase;
phase = phase - phaseWrap + (phaseWrap & (waveSize - 1));
}
This worked great. It sounds better and does not use much extra CPU. Thanks!mystran wrote:Using modf() is typically slow, converting floating point to integer+fractional parts is usually best done with something like the following:
No idea about the "sounds worse" part, maybe you are not handling the wrap around case properly or something?Code: Select all
// scale for tablesize float pos = phase * tablesize; // convert to integer (fast with sensible compiler options) // casting to unsigned might or might not be marginally slower int posInt = (int) pos; // convert the integer back to float (also pretty fast) // then subtract to get fractional part of position only pos -= posInt; // now the lerp can be done something like: result = (1-pos) * wave[posInt] + pos * wave[posInt+1];
© KVR Audio, Inc. 2000-2024
Submit: News, Plugins, Hosts & Apps | Advertise @ KVR | Developer Account | About KVR / Contact Us | Privacy Statement