I try to program a little audio unit plug-in that convolves the audio track with an impulse response given by the plug-in. i know that i need an fft algorithm that transfers the audio file's real signals to their spectra,multiplies the two spectra of the audio files and then returns them to their real signals. studying the "Audio Programming Book" i found an fft-based convolution algorithm that covers this operation.
unfortunately this algorithm does not cover REAL-TIME convolution, which is of course absolutely necessary for a plug-in (is it?). nevertheless the book says:
"This function will not serve for real-time purposes. However, it can be modified for this kind of application, by processing one FFT block at a time and leaving the overlapp-add to be performed by the invoking code."
(i'll post the code at the end of my post!)
alright..concerning this i have a couple of questions that i just can't figure out myself, since i'm pretty new to this programming-audio-units-thing.
1. what exactly do i need to change within the code in order to make this function work within a real-time plug-in? maybe somebody can rewrite it or add the stuff needed if it isn't much work, because now i'm not making any progress. if not, maybe someone can show me the place in the code that needs to be changed and tell me how it needs to be changed.
2. how and at which location within the audio unit code do i load the impulse response? i couldn't figure that out, because i didn't find any examples that showed how to load a sound and where to load it within the code.
3. is there someone that knows audio units well or at least has some basic knowledge of them that can answer me further questions concerning that topic? that would help me A LOT! i read all i could find about them but some little things still aren't clear to me and i don't want to post a list of 20 lil questions in here. (maybe via email or personal message? would be just AWESOME)
since this is concerning my bachelor thesis, any help would be marvellous
tl;dr
anyone familiar with real-time convolution and/or audio units? holla at me
thank you guys very much in advance and greetings from germany!
patrick
Code: Select all
void convol(float* impulse, float* input, float* output,
int impulse_size, int input_size){
float *impspec, *inspec, *outspec; // spectral vectors
float *insig, *outsig, *overlap; // time-domain vectors
int fftsize=1, convsize; // transform and convolution sizes
int overlap_size; // overlap size
int count, i, j; // counter and loop variables
overlap_size= impulse_size - 1;
convsize = impulse_size + overlap_size;
while(fftsize < convsize) fftsize *= 2;
impspec = new float[fftsize]; // allocate memory for
inspec = new float[fftsize]; // spectral vectors
outspec = new float[fftsize];
insig = new float[fftsize];
outsig = new float[fftsize];
overlap = new float[overlap_size];
// get the impulse into the FFT input vector
// pad with zeros
for(i = 0; i < fftsize; i++){
if(i < impulse_size) insig[i] = impulse[i];
else insig[i] = 0.f;
}
// Take the DFT of impulse
fft(insig, impspec, fftsize);
// processing loop
for(i = count = 0; i < input_size+convsize; i++, count++){
// if an input block is ready
if(count == impulse_size && i < (input_size+impulse_size)){
// copy overlapping block
for(j = 0; j < overlap_size ; j++)
overlap[j] = outsig[j+impulse_size];
// pad input signal with zeros
for(j = impulse_size; j < fftsize; j++)
insig[j] = 0.f;
// Take the DFT of input signal block
fft(insig, inspec, fftsize);
// complex multiplication
// first pair is re[0Hz] and re[Nyquist]
outspec[0] = inspec[0]*impspec[0];
outspec[1] = inspec[1]*impspec[1];
// (a+ib)*(c+id) = (ac - bd) + (ad + bc)i
for(j = 2; j < fftsize; j+=2){
outspec[j] = inspec[j]*impspec[j]
- inspec[j+1]*impspec[j+1];
outspec[j+1] = inspec[j]*impspec[j+1]
+ inspec[j+1]*impspec[j];
}
// IDFT of the spectral product
ifft(outspec, outsig, fftsize);
// zero the sample counter
count = 0;
}
// get the input signal
// stop when the input is finished
if(i < input_size)
insig[count] = input[i];
else insig[count] = 0.f;
// overlap-add output starts only
// after the first convolution operation
if(i >= impulse_size)
output[i-impulse_size] = outsig[count] +
(count < overlap_size ? overlap[count] : 0.f);
}
// de-allocate memory
delete[] overlap;
delete[] outsig;
delete[] insig;
delete[] outspec;
delete[] inspec;
delete[] impspec;
}
