Elucidate a snippet of index.c?


    Aug 11 2008 | 4:06 pm
    Hi all-
    I'm staring at the perform routine of the MSP example file index.c, specifically these lines:
    temp = *++in; f = temp + 0.5; index = f; if( index < 0 )
    that's a quote from this larger snippet:
    double temp; double f; long index, chan, frames, nc;
    [...]
    while( --n ) { temp = *++in; f = temp + 0.5; index = f; if( index < 0 ) index = 0; else if( index >= frames ) index = frames - 1; if( nc > 1 ) index = index * nc + chan; *++out = tab[index]; }
    So is this just a fast way to round the floating point value to an integer value?
    That is, add 0.5 and truncate through the implicit conversion to a double, then an implicit conversion to a long?
    Or am I missing something?
    Thanks for the help!
    Charles

    • Aug 11 2008 | 4:26 pm
      On 11 Aug 2008, at 18:06, Charles Turner wrote: > > temp = *++in; > f = temp + 0.5; > index = f; > if( index < 0 )
      > So is this just a fast way to round the floating point value to an > integer value?
      yes, i would say that's what it is. index~ doesn't do interpolation. vb
    • Aug 11 2008 | 9:52 pm
      I would say it's a simple way rather than the fastest.
      The redundant 'temp' variable is probably optimised out by the compiler but there are faster algorithms than a standard cast to round a double to an int.
      See the section on "Double precision float to signed integer conversion". A similar algo is used in Juce although you won't get access to it via the Max 5 SDK as I understand!