ftp.nice.ch/pub/next/unix/audio/sms.N.bs.tar.gz#/sms/library/sinTab.c

This is sinTab.c in view mode; [Download] [Up]

#include "../sms.h"

static double fSineScale;
static int nSineTabSize;

/* clear sine table */
void ClearSine()
{
  if(pFSTab)
    free(pFSTab);
  pFSTab = 0;
}

/* prepares the sine table, returns 1 if allocations made, 0 on failure
 * int nTableSize;    size of table
 */
int PrepSine (int nTableSize)
{
  register int i;
  double fTheta;
  
  if((pFSTab = (double *)malloc(nTableSize*sizeof(double))) == 0)
    return (0);
  nSineTabSize = nTableSize;
  fSineScale =  (double)(TWO_PI) / (double)(nSineTabSize - 1);
  fTheta = 0.0;
  for(i = 0; i < nSineTabSize; i++) 
  {
    fTheta = fSineScale * (double)i;
    pFSTab[i] = sin(fTheta);
  }
  return (1);
}

/* function that returns approximately sin(fTheta)
 * double fTheta;    angle in radians
 */
double SinTab (double fTheta)
{
  double fSign = 1.0, fT;
  int i;
  
  fTheta = fTheta - floor(fTheta / TWO_PI) * TWO_PI;
  
  if(fTheta < 0)
  {
    fSign = -1;
    fTheta = -fTheta;
  }
  
  i = fTheta / fSineScale + .5;
  fT = pFSTab[i];
  
  if (fSign == 1)
    return(fT);
  else
    return(-fT);
}

These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Netfuture.ch.