Thread: if statement has no effect

  1. #1
    Registered User samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Newport
    Posts
    382

    if statement has no effect

    Here is the algorithm for an audio delay. I want a slight amount of noise to be added to fedback signals.

    Code:
    void samDelay::processReplacing (float **inputs, float **outputs, long sampleFrames)
    {
        float *inLeft = inputs [chLeft];
        float *inRight = inputs [chRight];
    
        float *outLeft = outputs [chLeft];
        float *outRight = outputs [chRight];
    
        float fedBack [nChannels];
    
        while(--sampleFrames >= 0)
        {	
    		fedBack [chLeft] = xFade (bufRetrieve (chRight), bufRetrieve (chLeft), crossover) * feedback;
    		fedBack [chRight] = xFade (bufRetrieve (chLeft), bufRetrieve (chRight), crossover) * feedback;
    
    		if (fedBack [chLeft])
    			fedBack [chLeft] += noise (0.2f);
    
    		if (fedBack [chRight])
    			fedBack [chRight] += noise (0.2f);
    
    		// Output = input + output from delay lines * feedback:
    		*outLeft = (*inLeft++ + fedBack [chLeft]);
    		*outRight = (*inRight++ + fedBack [chRight]);
    
    		// Feed it back into the delay line:
    		bufInsert (chLeft, *outLeft++);
    		bufInsert (chRight, *outRight++);
    
    		bufMove ();		// Next indices.
    	}
    }
    So I use an if statement to make sure to only add noise if there is a signal, or the plugin would just constantly generate noise. But that's just what it does. It seems to completely ignore the if statement and just indiscriminately add noise.

    The delay lines are initialised to 0 at the start, so there's no possibility of any non-zero values causing noise to be added when not wanted.

    What could be the cause?

  2. #2
    C/C++ homeyg's Avatar
    Join Date
    Nov 2004
    Location
    Louisiana, USA
    Posts
    209
    Well, I don't see 'chRight' or 'chLeft' declared anywhere in there, but it wouldn't compiled if that was the case.

    And another problem: can a float value be converted to a bool value? You are trying to make this conversion with 'fedBack[chRight]' and 'fedBack[chLeft]' in the if statements and I don't even think that is possible.

  3. #3
    Registered User samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Newport
    Posts
    382
    It is possible - I tested it in a console application beforehand. Even if I explicitly state: if (fedBack [chLeft] != 0.0f); it still does the same thing.

    And this is just an excerpt - chLeft and chRight are enums of 0 and 1.

  4. #4
    C/C++ homeyg's Avatar
    Join Date
    Nov 2004
    Location
    Louisiana, USA
    Posts
    209
    Another problem could be that the variable 'sampleFrames' is causing the array to go past its limits in the if test. It may be converting null characters to float (0), and in effect, causing the if statements to fail.

  5. #5
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    You should not check to see if a float is equal to zero it's not going to work. You are going to want to check if it's greater than or check if it's less than.
    Woop?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    A recent post on floating point comparisons.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Newport
    Posts
    382
    Quote Originally Posted by prog-bman
    You should not check to see if a float is equal to zero it's not going to work. You are going to want to check if it's greater than or check if it's less than.
    Tried it. I even gave it a bit of leeway with:

    Code:
    if ((fedBack [chLeft] > 0.01f) || (fedBack [chLeft] < -0.01f))
        fedBack [chLeft] += noise (0.2f);
    
    if ((fedBack [chRight] > 0.01f) || (fedBack [chRight] < -0.01f))
        fedBack [chRight] += noise (0.2f);
    It seems to work now, but that was strange. Cheers!

    Does anyone recommend making it more elegant with:

    Code:
    if (fabs (fedback [chLeft]) > 0.01f)
        fedBack [chLeft] += noise (0.2f);
    
    if (fabs (fedback [chRight]) > 0.01f)
        fedBack [chRight] += noise (0.2f);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Usefulness of the "else if" statement
    By gn17 in forum C Programming
    Replies: 7
    Last Post: 08-12-2007, 05:19 AM
  2. If Else statement problem
    By doofusboy in forum C Programming
    Replies: 2
    Last Post: 11-09-2005, 07:18 AM
  3. if/break statement
    By Apropos in forum C++ Programming
    Replies: 7
    Last Post: 02-22-2005, 02:33 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Uh-oh! I am having a major switch problem!
    By goodn in forum C Programming
    Replies: 4
    Last Post: 11-01-2001, 04:49 PM