我试图对一组数据执行一个函数,但我不太确定如何去做,
这是我的代码
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#define BUFFER_LEN 10
#define SAMPLE_RATE 48000
#define MAX_DELAY 0.25
int buffer_in[]= {0,1,2,3,4,5,6,7,8,9};
int buffer_out[10];
short int flanger(float , float , int , short int );
int main(void)
{
int j,k,l;
for (j = 0; j <=BUFFER_LEN; j++){
buffer_out[j] = flanger(buffer_in[j]); //this is causing the error
printf("buffer out value = %d",buffer_out[j]);
}
return 0;
}
// Flanger function
short int flanger(float range, float delay, int rate, short int inData){
float flangerDelay; /* stores current delay required for flange effect */
static int i=0; /* keeps track of time for creating sweep waveform */
static float sweepValue=0; /* keeps track of current sweep delay in ms */
static int sweepFlag=1; /* keeps track of waveform movement */
static int writePtr=0; /* pointer to newest audio sample in buffer */
static int readPtr=0; /* pointer to oldest audio sample in buffer */
float tmp; /* tmp value to see if dly will point to a position in buff */
float delayArray[50];
/* convert rate from Hz to Hz according to current sample rate */
/* NOTE: If it does not divise exact, take the integer part only! */
/* is it time to change waveform? if not, increment counter */
if (i >= rate) {
/* has the maximum possible delay for sweep been reached? */
if (sweepValue >= range)
sweepFlag = 0; /* start the \ of triangular waveform */
else if (sweepValue <= 0)
sweepFlag = 1; /* start the / of triangular waveform */
/* Is the waveform rising or falling? */
if (sweepFlag==1)
sweepValue += 0.001; /* increase sweep delay by .001 ms */
else
sweepValue -= 0.001; /* decrease sweep delay by .001 ms */
/* reset i, to start count before waveform changes shape again */
i=0;
}
else i++;
/* Calculate the total current to delay (in ms, not samples!) */
flangerDelay = sweepValue + delay;
/* calculate delay in samples rather than in time */
tmp = flangerDelay * 22.4f; //(float)(SAMPLE_RATE/1000);
//printf("flangerDelay: %f samples: %d\n", flangerDelay, tmp);
/* Calculate position of the read & write pointers */
if (writePtr < (int)tmp )
readPtr = (((SAMPLE_RATE/1000)*MAX_DELAY) - ((int)tmp - writePtr));
else
readPtr = writePtr - (int)tmp;
/* has the write pointer reached end of delay buffer? */
if (writePtr > ((SAMPLE_RATE/1000)*MAX_DELAY))
writePtr=0;
else
writePtr++;
/* now add current audio sample to array and return oldest sample */
delayArray[writePtr] = inData;
/* is tmp a whole value? i.e. will it point to a sample in the buffer? */
if (tmp > (int)tmp){
/* not a whole number! therefore, interpolation is required! */
if (readPtr == ((SAMPLE_RATE/1000) * MAX_DELAY))
return((delayArray[readPtr] + delayArray[0])/2);
else
return((delayArray[readPtr] + delayArray[readPtr+1])/2);
}
else {
/* is a whole number! therefore, can take straight from buffer! */
return delayArray[readPtr];
}
}
我在这部分得到了错误
for (j = 0; j <=BUFFER_LEN; j++){
buffer_out[j] = flanger(buffer_in[j]); //this is causing the error
printf("buffer out value = %d",buffer_out[j]);
}
基本上我想对输入的数据执行功能buffer_in
并将结果放入buffer_out
其中是否设置错误?非常感谢您的帮助!