# Problem with some arithmetic!

• 01-05-2007
bobthebullet990
Problem with some arithmetic!
Hi again!!! ...I'm a little stuck here! I dont' really understand what this is doing...

Code:

```    #define SAMPLE_RATE 22400   float flangerDelay;   static float sweepValue; /* delay changes depeding on current waveform value */     /* Calculate the total current delay (in ms, not samples!) */   flangerDelay = sweepValue + delay;     /* calculate delay in samples rather than in time */   tmp = flangerDelay * (float)(SAMPLE_RATE/1000);     printf("flangerDelay: %f samples: %d\n", flangerDelay, tmp);```
and the output...

Code:

```... ... flangerDelay: 0.500000 samples: 0 flangerDelay: 0.500000 samples: 0 flangerDelay: 0.500000 samples: 0 flangerDelay: 0.501000 samples: -1073741824 flangerDelay: 0.501000 samples: -1073741824 flangerDelay: 0.501000 samples: -1073741824 flangerDelay: 0.501000 samples: -1073741824 flangerDelay: 0.501000 samples: -1073741824 flangerDelay: 0.501000 samples: -1073741824 flangerDelay: 0.501000 samples: -1073741824 flangerDelay: 0.501000 samples: -1073741824 flangerDelay: 0.501000 samples: -1073741824 flangerDelay: 0.501000 samples: -1073741824 flangerDelay: 0.501000 samples: -1073741824 flangerDelay: 0.502000 samples: 1610612736 flangerDelay: 0.502000 samples: 1610612736 flangerDelay: 0.502000 samples: 1610612736 flangerDelay: 0.502000 samples: 1610612736 ... ...```
why if tmp = flangerDelay*(float)(SAMPLE_RATE/1000); is it printing out total crap? For the first samples it should be:

0.5 * (22400 / 1000) = 11.2

I'm very confused!!! anyone know whats going on???
• 01-05-2007
peterchen
you need to get sweepValue a value before using it in an expression.

e.g float sweepValue = 0;

also delay doesn't seem to have been defined.
• 01-05-2007
rynoon
If you replace your printf function with the following, what happens?

printf("flangerDelay: %f samples: %f\n", flangerDelay, tmp);
• 01-05-2007
bobthebullet990
Full listing for function flanger()...

Code:

```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 */     /* 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(calculateAverage(delayArray[readPtr],delayArray[0]));         else           return(calculateAverage(delayArray[readPtr],delayArray[readPtr+1]));   }   else {     /* is a whole number! therefore, can take straight from buffer! */     return delayArray[readPtr];   }   } /* flanger() */```
• 01-05-2007
bobthebullet990
OK! Its fixed!!! I was being a dumbass!!! ...yes you were right! it was %f, but my flanger function.... if you see where the line:

if (readPtr == ((SAMPLE_RATE/1000) * MAX_DELAY))

This is obviously wrong because delayBuffer is defined as length [MAX_DELAY] - So I forgot to take one away!!!! ...by not doing this was giving a police siren effect!!!!

Thanks for everyones help!!!