Thread: Problem with some arithmetic!

  1. #1
    Matt Conway bobthebullet990's Avatar
    Join Date
    Nov 2005
    Location
    Cambridge
    Posts
    122

    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???
    Many junglists take pride in their belongin to what may be referred to as a globalised drum & bass subculture, as a subculture though, it is not nearly as distinct at gothic or punk!

  2. #2
    Registered User
    Join Date
    Dec 2005
    Location
    Australia - Melbourne
    Posts
    63
    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.

  3. #3
    Registered User rynoon's Avatar
    Join Date
    Dec 2006
    Location
    London, ON
    Posts
    26
    If you replace your printf function with the following, what happens?

    printf("flangerDelay: %f samples: %f\n", flangerDelay, tmp);

  4. #4
    Matt Conway bobthebullet990's Avatar
    Join Date
    Nov 2005
    Location
    Cambridge
    Posts
    122
    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() */
    Many junglists take pride in their belongin to what may be referred to as a globalised drum & bass subculture, as a subculture though, it is not nearly as distinct at gothic or punk!

  5. #5
    Matt Conway bobthebullet990's Avatar
    Join Date
    Nov 2005
    Location
    Cambridge
    Posts
    122
    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!!!
    Many junglists take pride in their belongin to what may be referred to as a globalised drum & bass subculture, as a subculture though, it is not nearly as distinct at gothic or punk!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  2. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  3. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  4. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  5. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM