I'm currently exploring writing a vibrato program/plug-in and I have it mostly working pretty well except for what sounds like sample noise in the output. I've parsed the code and stepped through breakpoints several times, but the problem is eluding me. Here is a sample of the output with the undesired noise:
http://www.christianfloisand.com/vibtest.wav
Here is the code (un-optimized for easier reading):
Code: Select all
SR = 44100
VDEL_T = 0.0005 // variable delay time
PHASE = 0 // phase for sine wave modifier
MDEL_S = (int) VDEL_T * SR // maximum delay in samples
buffer = new float[1024] // soundfile buffer
delayBuf = new float[MDEL_S]
memset (delayBuf, 0, sizeof(float)*MDEL_S)
writeIndex = 0 // write position for delayBuf
// the vibrato function, using variable delay and a sine wave LFO
// to modulate the delay time
void vibrato (float *input, float *delayBuf, int *writeIndex, int frames)
{
int readIndex;
float out, delSamples, rawPos, fract, next;
for (int i = 0; i < frames; i++) { // frames = 1024
// modulate delay time by sine wave LFO
delSamples = (VDEL_T * sin(PHASE) * SR;
if (delSamples > MDEL_S)
delSamples = (float) MDEL_S;
// calculate read pos in buffer relative to write pos
rawPos = *writeIndex - delSamples;
// and check bounds within circular buffer, delayBuf
rawPos = (rawPos >= 0 ? (rawPos < MDEL_S ? rawPos : rawPos - MDEL_S) : rawPos + MDEL_S;
readIndex = (int) rawPos; // get read pos in buffer
frac = rawPos - readIndex; // for interpolation
if (readIndex != MDEL_S-1)
next = delayBuf[readIndex+1];
else
next = delayBuf[0];
out = delayBuf[readIndex] + frac * (next - delayBuf[readIndex]);
delayBuf[*writeIndex] = input[i];
input[i] = out;
// wrap writeIndex if needed
if (*writeIndex != MDEL_S)
*writeIndex++;
else
*writeIndex = 0;
PHASE += TWOPI * freq / SR; // increment phase, freq = 6.4Hz
if (PHASE >= TWOPI) PHASE -= TWOPI;
if (PHASE < 0) PHASE += TWOPI;
}
}
// in the main loop, read from soundfile (in) into buffer
// (vector size of 1024), process vibrato on buffer,
// then write to output file (out)
do {
readCount = sf_readf_float(in, buffer, VECTORSIZE);
vibrato(buffer, delayBuf, &writeIndex, readCount);
writeCount = sf_writef_float(out, buffer, readCount);
} while (readCount);
Many thanks for any feedback.
Chris
