Delay outputting 0's
Can someone please tell me what is wrong here. delay has a buffer (del) with a pointer *p, but is outputting zero (0) every sample.
(Fixed Delay with 3 secs)
float delay(float *sig, float dtime, float *del, int *p, int vecsize, float sr) {
int dt;
float out;
dt = (int) (dtime*sr);// dtime = 3开发者_如何学编程secs, sr=44100.0
for(int i=0; i<vecsize; i++){
out = del[*p];
del[*p] = sig[i];
sig[i] = out;
*p = (*p != dt-1 ? *p+1 : 0);
}
return *sig;
}
Your delay()
function appears to work fine, so the error must be in how you called it. See the following example program:
#include <stdio.h>
float delay(float *sig, float dtime, float *del, int *p, int vecsize, float sr)
{
int dt;
float out;
dt = (int) (dtime*sr);// dtime = 3secs, sr=44100.0
for(int i=0; i<vecsize; i++){
out = del[*p];
del[*p] = sig[i];
sig[i] = out;
*p = (*p != dt-1 ? *p+1 : 0);
}
return *sig;
}
#define DTIME 3
#define SAMPLERATE 44100
#define VECSIZE (10 * SAMPLERATE)
int main()
{
float del[DTIME * SAMPLERATE] = { 0.0 };
int del_p = 0;
float sig[VECSIZE] = { -5.0, -4.0, -3.0, -2.0, -1.0, 0.0, 1.0, 2.0, 3.0, 4.0, 5.0 };
int i, j;
delay(sig, DTIME, del, &del_p, VECSIZE, SAMPLERATE);
for (i = 0; i < VECSIZE && sig[i] == 0.0; i++)
;
for (j = i; j < VECSIZE && j < i + 10; j++)
printf("sig[%d] = %f\n", j, sig[j]);
return 0;
}
The output produced is as expected:
sig[132300] = -5.000000
sig[132301] = -4.000000
sig[132302] = -3.000000
sig[132303] = -2.000000
sig[132304] = -1.000000
sig[132305] = 0.000000
sig[132306] = 1.000000
sig[132307] = 2.000000
sig[132308] = 3.000000
sig[132309] = 4.000000
Note that you should really be passing dt
directly to the delay()
function, rather than calculating it from sr
and dtime
- as far as the delay()
function is concerned, it's the delay in number of samples (and hence the assumed size of the del[]
delay line array) that matters. You'd have to have calculated that value in the caller anyway, to appropriately size the del[]
array.
You're returning *sig
always. That's always going to be the first element of the sig
array. sig[0]
will be equal to del[*p]
because you're the first time through the loop (when i=0
) you're doing:
out = del[*p];
del[*p] = sig[0]; // doesn't affect sig
sig[0] = out;
That's the only time sig[0]
is affected. And then when you return *sig
you're returning sig[0]
.
So I would expect this function would always return whatever del[*p]
is.
精彩评论