jeudi 22 janvier 2015

more efficient linear interpolation?

I'm writing a granular synthesis engine with linear interpolation. The problem is that interpolation as currently implemented is roughly 10% heavier on the CPU than the processing without interpolation.


I am posting a simplified sample program showcasing the processing I apply to an interleaved sample buffer. Is there any way I could make the calculation inside the interpolation function more efficient and thus less heavy on the CPU? The function works perfectly and results in very smooth sounding audio. It's just the CPU consumption that should be optimized.


I write code in my free time and I'm therefore not a very experienced programmer. I can't seem to wrap my head around this. Been struggling with this for weeks now.



int main(void) {
long i, dist;
long oldlength = 5000;
long newlength = 10000;
double oldbuffer[oldlength]; // assume the buffer contains values
double newbuffer[newlength];

// method with linear interpolation
for (i = 0; i < newlength; i++) {
dist = (i / newlength) * oldlength;
newbuffer[i] = interpolation(dist, oldbuffer, 1, 0);
}

// method without linear interpolation
for (i = 0; i < newlength; i++) {
dist = (i / newlength) * oldlength;
newbuffer[i] = oldbuffer[dist];
}

return 0;
}

double interpolation(double dist, float *buffer, short chcount, short ch) {
// dist = current read position in buffer
// buffer = interleaved sample buffer
// chcount = channels contained in sample buffer
// ch = channel to be read from buffer

long i = (long)dist; // get truncated index
dist -= (long)dist; // calculate fraction value for interpolation

return buffer[i * chcount + ch] + dist * (buffer[(i + 1) * chcount + ch] - buffer[i * chcount + ch]);
}

Aucun commentaire:

Enregistrer un commentaire