[xiph-cvs] cvs commit: MTG postfish.c smallft.c soundboard.c

Monty xiphmont at xiph.org
Sat Nov 23 17:21:19 PST 2002



xiphmont    02/11/23 20:21:18

  Modified:    .        soundboard.c
  Added:       .        postfish.c smallft.c
  Log:
  add recording hooks to the sound board
  add postprocessor named 'postfish'.

Revision  Changes    Path
1.10      +625 -106  MTG/soundboard.c

Index: soundboard.c
===================================================================
RCS file: /usr/local/cvsroot/MTG/soundboard.c,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -r1.9 -r1.10
--- soundboard.c	7 Nov 2002 09:12:24 -0000	1.9
+++ soundboard.c	24 Nov 2002 01:21:18 -0000	1.10
@@ -40,6 +40,7 @@
 enum menutype {MENU_MAIN,MENU_KEYPRESS,MENU_ADD,MENU_EDIT,MENU_OUTPUT,MENU_QUIT};
 
 pthread_mutex_t master_mutex=PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
+pthread_mutex_t rec_mutex=PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
 static long main_master_volume=50;
 char *program;
 static int playback_buffer_minfill=0;
@@ -50,7 +51,8 @@
 static int firstsave=0;
 static int unsaved=0;
 
-static FILE *audiofd=NULL;
+static FILE *playfd=NULL;
+static FILE *recfd=NULL;
 int ttyfd;
 int ttypipe[2];
 
@@ -58,6 +60,8 @@
 
 pthread_t main_thread_id;
 pthread_t playback_thread_id;
+pthread_t record_thread_id;
+pthread_t record_disk_thread_id;
 pthread_t tty_thread_id;
 
 void main_update_tags(int y);
@@ -93,8 +97,8 @@
 
 /******** channel mappings.  All hardwired for now... ***********/
 // only OSS stereo builin for now
-#define MAX_OUPUT_CHANNELS 4
-#define MAX_INPUT_CHANNELS 2
+#define MAX_OUTPUT_CHANNELS 4
+#define MAX_FILECHANNELS 2
 #define CHANNEL_LABEL_LENGTH 50
 int playback_bufsize=0;
 
@@ -104,15 +108,19 @@
   /* real stuff not here yet */
 } outchannel;
   
-static outchannel channel_list[MAX_OUPUT_CHANNELS]={
-  {"one",0},
-  {"two",0},
-  {"three",0},
-  {"four",0},
+static outchannel channel_list[MAX_OUTPUT_CHANNELS]={
+  {"house front left",0},
+  {"house front right",0},
+  {"house rear left",0},
+  {"house rear right",0},
   {"five",0},
   {"six",0},
 };
-static int channel_count=MAX_OUPUT_CHANNELS;
+static outchannel rchannel_list[2]={
+  {"left",0},
+  {"right",0},
+};
+static int channel_count=MAX_OUTPUT_CHANNELS;
 
 /******** label abstraction code; use this for all alloced strings
           that need to be saved to config file (shared or not) */
@@ -248,9 +256,9 @@
   double master_vol_target;
   double master_vol_slew;
 
-  double outvol_current[MAX_INPUT_CHANNELS][MAX_OUPUT_CHANNELS];
-  double outvol_target[MAX_INPUT_CHANNELS][MAX_OUPUT_CHANNELS];
-  double outvol_slew[MAX_INPUT_CHANNELS][MAX_OUPUT_CHANNELS];
+  double outvol_current[MAX_FILECHANNELS][MAX_OUTPUT_CHANNELS];
+  double outvol_target[MAX_FILECHANNELS][MAX_OUTPUT_CHANNELS];
+  double outvol_slew[MAX_FILECHANNELS][MAX_OUTPUT_CHANNELS];
 
 } tag;
 
@@ -498,7 +506,7 @@
   int  vol_master;
   long vol_ms;
   
-  int  outvol[MAX_INPUT_CHANNELS][MAX_OUPUT_CHANNELS];
+  int  outvol[MAX_FILECHANNELS][MAX_OUTPUT_CHANNELS];
 } mix;
 
 typedef struct {
@@ -596,10 +604,10 @@
             c->tag,c->tag_create_p,
             c->mix.vol_master,
             c->mix.vol_ms,
-	    MAX_OUPUT_CHANNELS,
-	    MAX_INPUT_CHANNELS);
+	    MAX_OUTPUT_CHANNELS,
+	    MAX_FILECHANNELS);
     
-    for(i=0;i<MAX_OUPUT_CHANNELS;i++)
+    for(i=0;i<MAX_OUTPUT_CHANNELS;i++)
       fprintf(f,"<%d %d>",c->mix.outvol[0][i],c->mix.outvol[1][i]);
     count=fprintf(f,"\n");
     if(count<1)return(-1);
@@ -653,7 +661,7 @@
         addnlstr("LOAD ERROR (CUE): parse error looking for value.",80,' ');
         return(-1);
       }
-      if(j<MAX_INPUT_CHANNELS)
+      if(j<MAX_FILECHANNELS)
         c.mix.outvol[j][i]=v;
     }
     count=fscanf(f,"%c",&ch);
@@ -744,6 +752,265 @@
   return ret;
 }
 
+/*************** threaded record ****************************/
+
+#define REC_SAMPLE_BYTES 2
+#define REC_SAMPLE_FMT AFMT_S16_LE
+#define REC_SAMPLE_CH 2
+#define REC_BLOCK (REC_SAMPLE_CH * REC_SAMPLE_BYTES * 512) 
+unsigned char recordbuffer[REC_BLOCK*512];
+
+pthread_mutex_t rec_buffer_mutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
+pthread_cond_t rec_buffer_cond   = PTHREAD_COND_INITIALIZER;
+
+int record_head=0;
+int record_count=0;
+int record_tail=0;
+int rec_active1=0;
+int rec_active2=0;
+int rec_exit=0;
+int rec_flush_req=0;
+int rec_flush_ok=0;
+
+int rec_buffer_disk_min=100;
+int rec_buffer_dma_min=100;
+
+/* writes a wav header without the length set. */
+void PutNumLE(long num,FILE *f,int bytes){
+  int i=0;
+  while(bytes--){
+    fputc((num>>(i<<3))&0xff,f);
+    i++;
+  }
+}
+void WriteWav(FILE *f,long channels,long rate,long bits,long duration){
+  fseek(f,0,SEEK_SET);
+  fprintf(f,"RIFF");
+  PutNumLE(duration+44-8,f,4);
+  fprintf(f,"WAVEfmt ");
+  PutNumLE(16,f,4);
+  PutNumLE(1,f,2);
+  PutNumLE(channels,f,2);
+  PutNumLE(rate,f,4);
+  PutNumLE(rate*channels*((bits-1)/8+1),f,4);
+  PutNumLE(((bits-1)/8+1)*channels,f,2);
+  PutNumLE(bits,f,2);
+  fprintf(f,"data");
+  PutNumLE(duration,f,4);
+}
+
+void *record_disk_thread(void *dummy){
+  FILE *recdiskfd=NULL;
+  long filesize=0;
+
+  while(1){
+    /* open a file; name the capture by time/date */
+    struct stat buf;
+    int ret;
+
+    while(1){
+
+      pthread_mutex_lock(&rec_mutex);
+      if(!rec_flush_req && recdiskfd){
+	WriteWav(recdiskfd,2,44100,REC_SAMPLE_BYTES*8,filesize);
+	fclose(recdiskfd);
+	recdiskfd=NULL;
+	rec_flush_ok=0;
+      }
+      pthread_mutex_unlock(&rec_mutex);
+
+      while(1){
+	/* lock and check count; wait if none to flush */
+	pthread_mutex_lock(&rec_buffer_mutex);
+	if(record_count<=0){
+	  /* wait for it */
+	  pthread_cond_wait(&rec_buffer_cond,&rec_buffer_mutex);
+	  pthread_mutex_unlock(&rec_buffer_mutex);
+	  pthread_mutex_lock(&rec_mutex);
+	}else{
+	  pthread_mutex_unlock(&rec_buffer_mutex);
+	  pthread_mutex_lock(&rec_mutex);
+	  break;
+	}
+
+	if(rec_exit)break;
+	pthread_mutex_unlock(&rec_mutex);
+
+      }
+      /* rec mutex lock fell through */
+      {
+	int percent=rint(100.-record_count*100./sizeof(recordbuffer));
+	if(rec_buffer_disk_min>percent)rec_buffer_disk_min=percent;
+      }
+      pthread_mutex_unlock(&rec_mutex);
+
+      /* flush to disk */
+
+      if(recdiskfd)ret=fwrite(recordbuffer+record_tail,1,REC_BLOCK,recdiskfd);
+
+      /* update counters, alert dma that we have space in ring buffer */
+      pthread_mutex_lock(&rec_buffer_mutex);
+      record_tail+=REC_BLOCK;
+      if(record_tail>=sizeof(recordbuffer))record_tail=0;
+      record_count-=REC_BLOCK;
+      pthread_cond_signal(&rec_buffer_cond);
+      pthread_mutex_unlock(&rec_buffer_mutex);
+      filesize+=REC_BLOCK;
+
+      pthread_mutex_lock(&rec_mutex);
+      if(filesize>10*60*44100*2*3)break;
+      if(rec_flush_req && !recdiskfd)break;
+      if(rec_exit)break;
+      pthread_mutex_unlock(&rec_mutex);
+
+    }
+    
+    if(rec_exit)break;
+    pthread_mutex_unlock(&rec_mutex);
+
+    if(recdiskfd){
+      WriteWav(recdiskfd,2,44100,REC_SAMPLE_BYTES*8,filesize);
+      fclose(recdiskfd);
+      filesize=0;
+    }
+
+    ret=stat("record",&buf);
+    if(ret){
+      mkdir("record",0700);
+      ret=stat("record",&buf);
+    }
+    if(!ret && S_ISDIR(buf.st_mode)){
+      /* construct a new filename */
+      struct tm *now;
+      char buf2[4096];
+      char buf1[256];
+      time_t nows;
+      nows=time(NULL);
+      now=localtime(&nows);
+      strftime(buf1,256,"%Y%m%d_%H:%M:%S",now);
+      sprintf(buf2,"record/%s.wav",buf1);
+      recdiskfd=fopen(buf2,"wb");
+      if(recdiskfd){
+	WriteWav(recdiskfd,2,44100,REC_SAMPLE_BYTES*8,-1);
+	pthread_mutex_lock(&rec_mutex);
+	rec_flush_ok=1;
+	filesize=0;
+      }else{
+	pthread_mutex_lock(&rec_mutex);
+	rec_flush_ok=0;
+      }
+      pthread_mutex_unlock(&rec_mutex);
+    }
+  }
+
+  if(recdiskfd){
+    WriteWav(recdiskfd,2,44100,REC_SAMPLE_BYTES*8,filesize);
+    fclose(recdiskfd);
+  }
+
+  rec_active2=0;
+  fprintf(stderr,"Record flush thread exit...\n");
+  pthread_mutex_unlock(&rec_mutex);
+  return(NULL);
+}
+
+void *record_thread(void *dummy){
+  /* sound device startup */
+  int fd=fileno(recfd),i,j;
+  int format=REC_SAMPLE_FMT;
+  int channels=2;
+  int rate=44100;
+  long last=0;
+  long totalsize;
+  int count=0,ret;
+  audio_buf_info info;
+
+  ret=ioctl(fd,SNDCTL_DSP_SETFMT,&format);
+  if(ret || format!=REC_SAMPLE_FMT){
+    fprintf(stderr,"Could not set recording format\n");
+    exit(1);
+  }
+  ret=ioctl(fd,SNDCTL_DSP_CHANNELS,&channels);
+  if(ret || channels!=2){
+    fprintf(stderr,"Could not set %d channel recording\n",2);
+    exit(1);
+  }
+  ret=ioctl(fd,SNDCTL_DSP_SPEED,&rate);
+  if(ret || rate!=44100){
+    fprintf(stderr,"Could not set %dHz recording\n",44100);
+    exit(1);
+  }
+
+  ioctl(fd,SNDCTL_DSP_GETISPACE,&info);
+  totalsize=info.fragstotal*info.fragsize;
+
+
+  pthread_create(&record_disk_thread_id,NULL,record_disk_thread,NULL);
+
+  while(1){
+    int ret;
+
+    /* lock the buffer and check tail; block on space to record */
+    while(1){
+      pthread_mutex_lock(&rec_buffer_mutex);
+      if(sizeof(recordbuffer)-record_count<REC_BLOCK)
+	/* wait for it */
+	pthread_cond_wait(&rec_buffer_cond,&rec_buffer_mutex);
+      else
+	break;
+      pthread_mutex_unlock(&rec_buffer_mutex);
+
+      pthread_mutex_lock(&rec_mutex);
+      if(rec_exit)break;
+      pthread_mutex_unlock(&rec_mutex);
+
+    }
+    pthread_mutex_unlock(&rec_buffer_mutex);
+
+    /* update ISPACE min */
+    ioctl(fd,SNDCTL_DSP_GETISPACE,&info);
+    {
+      int percent=rint((totalsize-info.bytes)*100./totalsize);
+      pthread_mutex_lock(&rec_mutex);
+      if(rec_buffer_dma_min>percent)rec_buffer_dma_min=percent;
+      pthread_mutex_unlock(&rec_mutex);
+    }
+
+    ret=fread(recordbuffer+record_head,1,REC_BLOCK,recfd);
+
+    pthread_mutex_lock(&rec_mutex);
+    for(i=record_head;i<record_head+REC_BLOCK;)
+      for(j=0;j<REC_SAMPLE_CH;j++){
+	//int val=((recordbuffer[i]<<8)|(recordbuffer[i+1]<<16)|(recordbuffer[i+2]<<24))>>8;
+	int val=((recordbuffer[i]<<16)|
+		 (recordbuffer[i+1]<<24))>>8;
+	if(labs(val)>rchannel_list[j].peak)
+	  rchannel_list[j].peak=labs(val);
+	i+=REC_SAMPLE_BYTES;
+      }
+    if(rec_exit)break;
+
+    if(rec_flush_req){
+      pthread_mutex_unlock(&rec_mutex);
+
+      pthread_mutex_lock(&rec_buffer_mutex);
+      record_head+=REC_BLOCK;
+      if(record_head>=sizeof(recordbuffer))record_head=0;
+      record_count+=REC_BLOCK;
+      pthread_cond_signal(&rec_buffer_cond);
+      pthread_mutex_unlock(&rec_buffer_mutex);
+    }else{
+      pthread_mutex_unlock(&rec_mutex);
+    }
+  }
+
+  rec_active1=0;
+  fprintf(stderr,"Record thread exit...\n");
+  pthread_mutex_unlock(&rec_mutex);
+  
+  return(NULL);
+}
+
 /*************** threaded playback ****************************/
 
 static inline double _slew_ms(long ms,double now,double target){
@@ -794,7 +1061,7 @@
   t->master_vol_target=c->mix.vol_master;
   t->master_vol_slew=_slew_ms(c->mix.vol_ms,0,c->mix.vol_master);
                              
-  for(j=0;j<MAX_OUPUT_CHANNELS;j++)
+  for(j=0;j<MAX_OUTPUT_CHANNELS;j++)
     for(k=0;k<t->channels;k++){
       t->outvol_current[k][j]=0;
       t->outvol_target[k][j]=c->mix.outvol[k][j];
@@ -815,7 +1082,7 @@
   t->master_vol_slew=_slew_ms(c->mix.vol_ms,t->master_vol_current,
                            c->mix.vol_master);
                              
-  for(j=0;j<MAX_OUPUT_CHANNELS;j++)
+  for(j=0;j<MAX_OUTPUT_CHANNELS;j++)
     for(k=0;k<t->channels;k++){
       t->outvol_target[k][j]=c->mix.outvol[k][j];
       t->outvol_slew[k][j]=_slew_ms(c->mix.vol_ms,t->outvol_current[k][j],
@@ -825,7 +1092,7 @@
 
 static inline void _next_sample(int16 *out){
   int i,j,k;
-  int staging[MAX_OUPUT_CHANNELS];
+  int staging[MAX_OUTPUT_CHANNELS];
 
   memset(staging,0,sizeof(staging));
 
@@ -851,7 +1118,7 @@
       }
 
       /* output split and mix */
-      for(k=0;k<MAX_OUPUT_CHANNELS;k++){
+      for(k=0;k<MAX_OUTPUT_CHANNELS;k++){
         staging[k]+=value*t->outvol_current[j][k]*main_master_volume*.0001;
         
         t->outvol_current[j][k]+=t->outvol_slew[j][k];
@@ -879,7 +1146,7 @@
       t->master_vol_slew=0;
       t->master_vol_current=t->master_vol_target;
     }
-    if(t->master_vol_current==0)
+    if(t->master_vol_current<0)
       _playback_remove(i);
     
     /* determine if fade out has begun */
@@ -903,7 +1170,7 @@
   }
 
   /* declipping, conversion */
-  for(i=0;i<MAX_OUPUT_CHANNELS;i++){
+  for(i=0;i<MAX_OUTPUT_CHANNELS;i++){
     if(channel_list[i].peak<fabs(staging[i]))
       channel_list[i].peak=fabs(staging[i]);
     if(staging[i]>32767.){
@@ -921,33 +1188,39 @@
 void *playback_thread(void *dummy){
   /* sound device startup */
   audio_buf_info info;
-  int fd=fileno(audiofd),i;
+  int fd=fileno(playfd),i;
   int format=AFMT_S16_NE;
-  int channels=MAX_OUPUT_CHANNELS;
+  int channels=MAX_OUTPUT_CHANNELS;
   int rate=44100;
   long last=0;
   long delay=10;
   long totalsize;
   int fragment=0x7fff000d;
-  int16 audiobuf[256*MAX_OUPUT_CHANNELS];
+  int16 audiobuf[256*MAX_OUTPUT_CHANNELS];
   int count=0,ret;
 
   ioctl(fd,SNDCTL_DSP_SETFRAGMENT,&fragment);
   ret=ioctl(fd,SNDCTL_DSP_SETFMT,&format);
   if(ret || format!=AFMT_S16_NE){
-    fprintf(stderr,"Could not set AFMT_S16_NE\n");
+    fprintf(stderr,"Could not set AFMT_S16_NE playback\n");
     exit(1);
   }
   ret=ioctl(fd,SNDCTL_DSP_CHANNELS,&channels);
-  if(ret || channels!=MAX_OUPUT_CHANNELS){
-    fprintf(stderr,"Could not set %d channels\n",channels);
+  if(ret || channels!=MAX_OUTPUT_CHANNELS){
+    fprintf(stderr,"Could not set %d channel playback\n",MAX_OUTPUT_CHANNELS);
     exit(1);
   }
 
-  ioctl(fd,SNDCTL_DSP_SPEED,&rate);
+  ret=ioctl(fd,SNDCTL_DSP_SPEED,&rate);
+  if(ret || rate!=44100){
+    fprintf(stderr,"Could not set %dHz playback\n",44100);
+    exit(1);
+  }
 
   ioctl(fd,SNDCTL_DSP_GETOSPACE,&info);
-  totalsize=info.fragstotal*info.fragsize;
+  pthread_mutex_lock(&master_mutex);
+  playback_buffer_minfill=totalsize=info.fragstotal*info.fragsize;
+  pthread_mutex_unlock(&master_mutex);
 
   while(!playback_exit){
     int samples;
@@ -970,10 +1243,10 @@
 
     pthread_mutex_lock(&master_mutex);
     for(i=0;i<256;i++)
-      _next_sample(audiobuf+i*MAX_OUPUT_CHANNELS);
+      _next_sample(audiobuf+i*MAX_OUTPUT_CHANNELS);
     pthread_mutex_unlock(&master_mutex);
 
-    ret=fwrite(audiobuf,2*MAX_OUPUT_CHANNELS,256,audiofd);
+    ret=fwrite(audiobuf,2*MAX_OUTPUT_CHANNELS,256,playfd);
     
     {
       struct timeval tv;
@@ -987,12 +1260,16 @@
 
   }
   
+  pthread_mutex_lock(&master_mutex);
   playback_active=0;
   
   /* sound device shutdown */
   
   ioctl(fd,SNDCTL_DSP_RESET);
   //pthread_mutex_unlock(&master_mutex);
+  fprintf(stderr,"Playback thread exit...\n");
+  pthread_mutex_unlock(&master_mutex);
+
   return(NULL);
 }
 
@@ -1037,7 +1314,7 @@
 
     if(t->activep){
 
-      t->master_vol_target=0;
+      t->master_vol_target=-1;
       t->master_vol_slew=_slew_ms(100,t->master_vol_current,0);
       
     }
@@ -1049,7 +1326,8 @@
 
 /***************** simple form entry fields *******************/
 
-enum field_type { FORM_YESNO, FORM_PERCENTAGE, FORM_NUMBER, FORM_GENERIC,
+enum field_type { FORM_YESNO, FORM_PERCENTAGE, FORM_PERCENTAGE_R, 
+		  FORM_NUMBER, FORM_GENERIC,
                   FORM_BUTTON } ;
 typedef struct {
   enum field_type type;
@@ -1129,6 +1407,19 @@
         addstr(buf);
       }
       break;
+    case FORM_PERCENTAGE_R:
+      {
+	int var=*(int *)(f->var);
+	char buf[80];
+	if(var<-1)var=-1;
+	if(var>100)var=100;
+	if(var==-1)
+	  snprintf(buf,80,"%*s",f->width-2,"DEL");
+	else
+	  snprintf(buf,80,"%*d",f->width-2,var);
+	addstr(buf);
+      }
+      break;
     case FORM_NUMBER:
       {
         long var=*(long *)(f->var);
@@ -1284,14 +1575,48 @@
         {
           int *val=(int *)ff->var;
           switch(c){
-	  case '+':case '=':case KEY_RIGHT:
+	  case '=':case KEY_RIGHT:
             (*val)++;
             if(*val>100)*val=100;
             break;
-	  case '-':case '_':case KEY_LEFT:
+	  case '+':
+	    (*val)+=10;
+	    if(*val>100)*val=100;
+	    break;
+	  case '-':case KEY_LEFT:
             (*val)--;
             if(*val<0)*val=0;
             break;
+	  case '_':
+	    (*val)-=10;
+	    if(*val<0)*val=0;
+	    break;
+	  default:
+	    ret=c;
+	    break;
+	  }
+	}
+	break;
+      case FORM_PERCENTAGE_R:
+	{
+	  int *val=(int *)ff->var;
+	  switch(c){
+	  case '=':case KEY_RIGHT:
+	    (*val)++;
+	    if(*val>100)*val=100;
+	    break;
+	  case '+':
+	    (*val)+=10;
+	    if(*val>100)*val=100;
+	    break;
+	  case '-':case KEY_LEFT:
+	    (*val)--;
+	    if(*val<-1)*val=-1;
+	    break;
+	  case '_':
+	    (*val)-=10;
+	    if(*val<-1)*val=-1;
+	    break;
           default:
             ret=c;
             break;
@@ -1414,8 +1739,10 @@
 void main_update_playbuffer(int y){
   if(menu==MENU_MAIN){
     char buf[20];
-    int  n;
+    int  n,nr;
     static int starve=0;
+    static int starver1=0;
+    static int starver2=0;
 
     pthread_mutex_lock(&master_mutex);
     n=playback_buffer_minfill;
@@ -1441,15 +1768,86 @@
     addstr("playbuffer: ");
     sprintf(buf,"%3d%% %s",n,starve?"***STARVE***":"            ");
     addstr(buf);
+
+
+    move(y+1,63);
+    {
+      int state=0;
+      pthread_mutex_lock(&rec_mutex);
+      if(rec_flush_req)
+	if(rec_flush_ok)
+	  state=2;
+	else
+	  state=1;
+      pthread_mutex_unlock(&rec_mutex);
+    
+      switch(state){
+      case 0:
+	addstr(" OFF         ");
+	break;
+      case 1:
+	addstr(" STARTING    ");
+	break;
+      case 2:
+	addstr(" RECORDING   ");
+	break;
+      }
+    }
+
+    nr=rec_buffer_dma_min;
+    rec_buffer_dma_min=100;
+    pthread_mutex_unlock(&rec_mutex);
+    
+    if(nr==0){
+      starver1=15;
+    }
+    starver1--;
+    if(starver1<0){
+      starver1=0; /* reset */
+    }
+    
+    move(y,42);
+    addstr("recbuffer (DMA):");
+    sprintf(buf," %3d%% %s",nr,starver1?"***OVERRUN***":"             ");
+    addstr(buf);
+
+    pthread_mutex_lock(&rec_mutex);
+    nr=rec_buffer_disk_min;
+    rec_buffer_disk_min=100;
+    pthread_mutex_unlock(&rec_mutex);
+    
+    if(nr==0){
+      starver2=15;
+    }
+    starver2--;
+    if(starver2<0){
+      starver2=0; /* reset */
+    }
+    
+    move(y+1,42);
+    addstr("recbuffer(disk):");
+    sprintf(buf," %3d%% %s",nr,starver2?"***OVERRUN***":"");
+    addstr(buf);
   }
 }
 
 #define todB_nn(x)   ((x)==0.f?-400.f:log((x))*8.6858896f)
-static int clip[MAX_OUPUT_CHANNELS];
+static int dBmap[100]={0,0,1,1,1,1,1,1,1,1,
+		    1,1,1,1,1,1,1,1,1,1,
+		    1,1,1,1,1,1,1,1,1,1,
+		    1,1,1,1,1,2,2,2,2,2,
+		    2,2,2,2,2,2,2,2,2,2,
+		    2,2,2,2,2,2,2,2,2,2,
+		    3,3,3,3,3,3,3,3,3,3,
+		    3,3,3,3,3,3,3,3,3,3,
+		    4,4,4,4,4,5,5,5,5,5,
+		    6,6,6,6,8,8,8,9,9,10};
+
+static int clip[MAX_OUTPUT_CHANNELS];
 void main_update_outchannel_levels(int y){
   int i,j;
   if(menu==MENU_MAIN){
-    for(i=0;i<MAX_OUPUT_CHANNELS;i++){
+    for(i=0;i<MAX_OUTPUT_CHANNELS;i++){
       int val;
       char buf[11];
       pthread_mutex_lock(&master_mutex);
@@ -1457,7 +1855,7 @@
       channel_list[i].peak=0;
       pthread_mutex_unlock(&master_mutex);
       
-      move(y+i+1,24);
+      move(y+i+1,17);
       if(val>=32767){
         clip[i]=15;
         val=32767;
@@ -1472,10 +1870,53 @@
       }
       attron(A_BOLD);
 
-      move(y+i+1,13);
-      val=rint(10.+(todB_nn(val/32768.)/10));
+      move(y+i+1,6);
+      val=rint(todB_nn(val/32768.)+100.);
+      if(val<0)val=0;
+      if(val>99)val=99;
+      val=dBmap[val];
+      for(j=0;j<val;j++)buf[j]='=';
+      for(;j<10;j++)buf[j]=' ';
+      buf[j]='\0';
+      addstr(buf);
+      attroff(A_BOLD);
+    }
+  }
+}
+
+static int rclip[2];
+void main_update_inchannel_levels(int y){
+  int i,j;
+  if(menu==MENU_MAIN){
+    for(i=0;i<2;i++){
+      int val;
+      char buf[11];
+      pthread_mutex_lock(&rec_mutex);
+      val=rchannel_list[i].peak;
+      rchannel_list[i].peak=0;
+      pthread_mutex_unlock(&rec_mutex);
+      
+      move(y+i+1,55);
+      if(val>=0x7fffff){
+	rclip[i]=15;
+	val=0x7fffff;
+      }
+      rclip[i]--;
+      if(rclip[i]<0)rclip[i]=0;
+      if(rclip[i]){
+	attron(A_BOLD);
+	addstr("CLIP");
+      }else{
+	addstr("+0dB");
+      }
+      attron(A_BOLD);
+
+      move(y+i+1,44);
+      val=rint(todB_nn(val/8388607.)+100.);
+      if(val<0)val=0;
+      if(val>99)val=99;
+      val=dBmap[val];
       for(j=0;j<val;j++)buf[j]='=';
-      buf[j]='|';
       for(;j<10;j++)buf[j]=' ';
       buf[j]='\0';
       addstr(buf);
@@ -1484,21 +1925,30 @@
   }
 }
 
-void main_update_outchannel_labels(int y){
+void main_update_channel_labels(int y){
   int i;
   char buf[80];
   if(menu==MENU_MAIN){
-    for(i=0;i<MAX_OUPUT_CHANNELS;i++){
+    for(i=0;i<MAX_OUTPUT_CHANNELS;i++){
       move(y+i+1,4);
-      sprintf(buf,"%2d: -Inf[          ]+0dB ",i);
+      sprintf(buf,"-[          ]+0dB ",i);
       addstr(buf);
       pthread_mutex_lock(&master_mutex);
       addstr(channel_list[i].label);
       pthread_mutex_unlock(&master_mutex);
     }
+    for(i=0;i<2;i++){
+      move(y+i+1,42);
+      sprintf(buf,"-[          ]+0dB ",i);
+      addstr(buf);
+      pthread_mutex_lock(&rec_mutex);
+      addstr(rchannel_list[i].label);
+      pthread_mutex_unlock(&rec_mutex);
+    }
   }
 
   main_update_outchannel_levels(y);
+  main_update_inchannel_levels(y);
 }
 
 void main_update_cues(int y){
@@ -1617,7 +2067,7 @@
       if(cue_list[cue_list_number].tag==-1)break;
     cue_list_position++;
   }
-  main_update_cues(10+MAX_OUPUT_CHANNELS);
+  main_update_cues(10+MAX_OUTPUT_CHANNELS);
 }
 
 void move_prev_cue(){
@@ -1626,7 +2076,7 @@
       if(cue_list[cue_list_number].tag==-1)break;
     cue_list_position--;
   }
-  main_update_cues(10+MAX_OUPUT_CHANNELS);
+  main_update_cues(10+MAX_OUTPUT_CHANNELS);
 }
 
 int save_top_level(char *fn){
@@ -1674,37 +2124,45 @@
   attroff(A_BOLD);
   update_editable();
 
-  mvvline(3,2,0,MAX_OUPUT_CHANNELS+5);
-  mvvline(3,77,0,MAX_OUPUT_CHANNELS+5);
+  mvvline(3,2,0,MAX_OUTPUT_CHANNELS+5);
+  mvvline(3,77,0,MAX_OUTPUT_CHANNELS+5);
+  mvvline(3,40,0,MAX_OUTPUT_CHANNELS+5);
   mvhline(2,2,0,76);
   mvhline(7,2,0,76);
-  mvhline(8+MAX_OUPUT_CHANNELS,2,0,76);
+  mvhline(8+MAX_OUTPUT_CHANNELS,2,0,76);
   mvaddch(2,2,ACS_ULCORNER);
   mvaddch(2,77,ACS_URCORNER);
-  mvaddch(8+MAX_OUPUT_CHANNELS,2,ACS_LLCORNER);
-  mvaddch(8+MAX_OUPUT_CHANNELS,77,ACS_LRCORNER);
+  mvaddch(2,40,ACS_TTEE);
+  mvaddch(8+MAX_OUTPUT_CHANNELS,2,ACS_LLCORNER);
+  mvaddch(8+MAX_OUTPUT_CHANNELS,40,ACS_BTEE);
+  mvaddch(8+MAX_OUTPUT_CHANNELS,77,ACS_LRCORNER);
 
   mvaddch(7,2,ACS_LTEE);
+  mvaddch(7,40,ACS_PLUS);
   mvaddch(7,77,ACS_RTEE);
 
   move(2,7);
   addstr(" output ");
 
-  mvhline(9+MAX_OUPUT_CHANNELS,0,0,80);
-  mvhline(18+MAX_OUPUT_CHANNELS,0,0,80);
+  move(2,45);
+  addstr(" input ");
+
+  mvhline(9+MAX_OUTPUT_CHANNELS,0,0,80);
+  mvhline(18+MAX_OUTPUT_CHANNELS,0,0,80);
 
   main_update_master(main_master_volume,5);
   main_update_playbuffer(4);
-  main_update_outchannel_labels(7);
+  main_update_channel_labels(7);
 
-  main_update_cues(10+MAX_OUPUT_CHANNELS);
-  main_update_tags(19+MAX_OUPUT_CHANNELS);
+  main_update_cues(10+MAX_OUTPUT_CHANNELS);
+  main_update_tags(19+MAX_OUTPUT_CHANNELS);
   curs_set(0);
 
   refresh();
 
   while(1){
     int ch=getch();
+    const char *ctrl=unctrl(ch);
     switch(ch){
     case '?':
       return(MENU_KEYPRESS);
@@ -1751,7 +2209,7 @@
         if(ch=='y'){
           unsaved=1;
           delete_cue_bank(cue_list_number);
-	  main_update_cues(10+MAX_OUPUT_CHANNELS);
+	  main_update_cues(10+MAX_OUTPUT_CHANNELS);
         }
         move(0,0);
         addstr("MTG Beaverphonic build "VERSION": ");
@@ -1790,11 +2248,6 @@
     case 'H':
       halt_playback();
       break;
-    case 'R':
-      halt_playback();
-      cue_list_position=0;
-      cue_list_number=0;
-      return(MENU_MAIN);
     case 'l':
       if(editable)
         editable=0;
@@ -1802,10 +2255,26 @@
         editable=1;
       update_editable();
     case 0:
-      main_update_tags(19+MAX_OUPUT_CHANNELS);
+      main_update_tags(19+MAX_OUTPUT_CHANNELS);
       main_update_playbuffer(4);
-      main_update_outchannel_labels(7);
+      main_update_outchannel_levels(7);
+      main_update_inchannel_levels(7);
       break;
+      //    default:
+      //     if(ctrl[0]=='^'){
+      //	switch(ctrl[1]){
+	case 'r':
+	  pthread_mutex_lock(&rec_mutex);
+	  rec_flush_req=1;
+	  pthread_mutex_unlock(&rec_mutex);
+	  break;
+	case 'R':
+	  pthread_mutex_lock(&rec_mutex);
+	  rec_flush_req=0;
+	  pthread_mutex_unlock(&rec_mutex);
+	  break;
+	  //	}
+	  //      }
     }
   }
 }
@@ -1843,7 +2312,7 @@
   mvaddstr(14,12,"save program");
   mvaddstr(15,12,"master volume up/down");
   mvaddstr(16,12,"halt playback");
-  mvaddstr(17,12,"reset board to beginning");
+  mvaddstr(17,12,"record to disk");
 
   mvaddstr(19,12,"any key to return to main menu");
   refresh();
@@ -1962,20 +2431,20 @@
   box(stdscr,0,0);
   mvaddstr(0,2," Add/Edit sample cue ");
   
-  mvaddstr(10,2,"loop                      crosslap       ms");
-  mvaddstr(11,2,"volume master        % fade       /      ms");
+  mvaddstr(10,2,"loop                       crosslap        ms");
+  mvaddstr(11,2,"volume master        % fade        /       ms");
   
-  form_init(&f,7+MAX_OUPUT_CHANNELS*t->channels);
+  form_init(&f,7+MAX_OUTPUT_CHANNELS*t->channels);
   strcpy(tdesc,label_text(t->sample_desc));
 
   field_add(&f,FORM_GENERIC,18,7,59,tdesc);
 
   field_add(&f,FORM_YESNO,18,10,5,&t->loop_p);
-  field_add(&f,FORM_NUMBER,37,10,6,&t->loop_lapping);
+  field_add(&f,FORM_NUMBER,38,10,7,&t->loop_lapping);
 
   field_add(&f,FORM_PERCENTAGE,18,11,5,&c->mix.vol_master);
-  field_add(&f,FORM_NUMBER,30,11,6,&c->mix.vol_ms);
-  field_add(&f,FORM_NUMBER,37,11,6,&t->fade_out);
+  field_add(&f,FORM_NUMBER,30,11,7,&c->mix.vol_ms);
+  field_add(&f,FORM_NUMBER,38,11,7,&t->fade_out);
   
   mvaddstr(2,2,"cue label        ");
   addnlstr(label_text(c->label),59,' ');
@@ -1993,13 +2462,13 @@
   addstr(buf);
 
   mvhline(13,3,0,74);
-  mvhline(14+MAX_OUPUT_CHANNELS,3,0,74);
-  mvvline(14,2,0,MAX_OUPUT_CHANNELS);
-  mvvline(14,77,0,MAX_OUPUT_CHANNELS);
+  mvhline(14+MAX_OUTPUT_CHANNELS,3,0,74);
+  mvvline(14,2,0,MAX_OUTPUT_CHANNELS);
+  mvvline(14,77,0,MAX_OUTPUT_CHANNELS);
   mvaddch(13,2,ACS_ULCORNER);
   mvaddch(13,77,ACS_URCORNER);
-  mvaddch(14+MAX_OUPUT_CHANNELS,2,ACS_LLCORNER);
-  mvaddch(14+MAX_OUPUT_CHANNELS,77,ACS_LRCORNER);
+  mvaddch(14+MAX_OUTPUT_CHANNELS,2,ACS_LLCORNER);
+  mvaddch(14+MAX_OUTPUT_CHANNELS,77,ACS_LRCORNER);
   
   if(t->channels==2){
     mvaddstr(13,5," L ");
@@ -2012,15 +2481,15 @@
     }
   }
 
-  for(i=0;i<MAX_OUPUT_CHANNELS;i++){
-    for(j=0;j<t->channels && j<MAX_INPUT_CHANNELS;j++)
+  for(i=0;i<MAX_OUTPUT_CHANNELS;i++){
+    for(j=0;j<t->channels && j<MAX_FILECHANNELS;j++)
       field_add(&f,FORM_PERCENTAGE,4+j*5,14+i,5,&c->mix.outvol[j][i]);
     sprintf(buf,"%% ->%2d ",i);
     mvaddstr(14+i,4+t->channels*5,buf);
     addnlstr(channel_list[i].label,76-11-t->channels*5,' ');
   }
 
-  field_add(&f,FORM_BUTTON,61,17+MAX_OUPUT_CHANNELS,16,"ACCEPT CHANGES");
+  field_add(&f,FORM_BUTTON,61,17+MAX_OUTPUT_CHANNELS,16,"ACCEPT CHANGES");
 
 
 #if 0
@@ -2084,13 +2553,13 @@
   box(stdscr,0,0);
   mvaddstr(0,2," Add/Edit mix cue ");
   
-  mvaddstr(10,2,"volume master        % fade       ms");
+  mvaddstr(10,2,"volume master        % fade        ms");
   
-  form_init(&f,3+MAX_OUPUT_CHANNELS*t->channels);
+  form_init(&f,3+MAX_OUTPUT_CHANNELS*t->channels);
   strcpy(tdesc,label_text(t->sample_desc));
 
-  field_add(&f,FORM_PERCENTAGE,18,10,5,&c->mix.vol_master);
-  field_add(&f,FORM_NUMBER,30,10,6,&c->mix.vol_ms);
+  field_add(&f,FORM_PERCENTAGE_R,18,10,5,&c->mix.vol_master);
+  field_add(&f,FORM_NUMBER,30,10,7,&c->mix.vol_ms);
   
   mvaddstr(2,2,"cue label        ");
   addnlstr(label_text(c->label),59,' ');
@@ -2109,13 +2578,13 @@
   addstr(buf);
 
   mvhline(12,3,0,74);
-  mvhline(13+MAX_OUPUT_CHANNELS,3,0,74);
-  mvvline(13,2,0,MAX_OUPUT_CHANNELS);
-  mvvline(13,77,0,MAX_OUPUT_CHANNELS);
+  mvhline(13+MAX_OUTPUT_CHANNELS,3,0,74);
+  mvvline(13,2,0,MAX_OUTPUT_CHANNELS);
+  mvvline(13,77,0,MAX_OUTPUT_CHANNELS);
   mvaddch(12,2,ACS_ULCORNER);
   mvaddch(12,77,ACS_URCORNER);
-  mvaddch(13+MAX_OUPUT_CHANNELS,2,ACS_LLCORNER);
-  mvaddch(13+MAX_OUPUT_CHANNELS,77,ACS_LRCORNER);
+  mvaddch(13+MAX_OUTPUT_CHANNELS,2,ACS_LLCORNER);
+  mvaddch(13+MAX_OUTPUT_CHANNELS,77,ACS_LRCORNER);
   
   if(t->channels==2){
     mvaddstr(12,5," L ");
@@ -2128,15 +2597,15 @@
     }
   }
 
-  for(i=0;i<MAX_OUPUT_CHANNELS;i++){
-    for(j=0;j<t->channels && j<MAX_INPUT_CHANNELS;j++)
+  for(i=0;i<MAX_OUTPUT_CHANNELS;i++){
+    for(j=0;j<t->channels && j<MAX_FILECHANNELS;j++)
       field_add(&f,FORM_PERCENTAGE,4+j*5,13+i,5,&c->mix.outvol[j][i]);
     sprintf(buf,"%% ->%2d ",i);
     mvaddstr(13+i,4+t->channels*5,buf);
     addnlstr(channel_list[i].label,76-11-t->channels*5,' ');
   }
 
-  field_add(&f,FORM_BUTTON,61,16+MAX_OUPUT_CHANNELS,16,"ACCEPT CHANGES");
+  field_add(&f,FORM_BUTTON,61,16+MAX_OUTPUT_CHANNELS,16,"ACCEPT CHANGES");
 
 
 #if 0
@@ -2270,6 +2739,15 @@
   return(MENU_EDIT);
 }
 
+int tagno_to_cueno(int tagno){
+  int i;
+
+  for(i=0;i<cue_count;i++)
+    if(cue_list[i].tag==tagno)return(i);
+
+  return(-1);
+} 
+
 int add_mix_menu(){
   tag_number tagno=0;
 
@@ -2316,8 +2794,11 @@
   }
 
   /* add the new cue */
+  /* prefill with original mix data from sample */
   {
     cue c;
+    int i,j;
+    int createno=tagno_to_cueno(tagno);
     unsaved=1;
 
     aquire_tag(tagno);
@@ -2327,11 +2808,13 @@
     c.tag=tagno;
     c.tag_create_p=0;
     memset(&c.mix,0,sizeof(mix));
-    c.mix.vol_master=100;
-    c.mix.vol_ms=10;
-    c.mix.outvol[0][0]=100;
-    c.mix.outvol[1][1]=100;
-
+    if(createno>-1){
+      c.mix.vol_master=cue_list[createno].mix.vol_master;
+      c.mix.vol_ms=cue_list[createno].mix.vol_ms;
+      for(j=0;j<MAX_FILECHANNELS;j++)
+	for(i=0;i<MAX_OUTPUT_CHANNELS;i++)
+	  c.mix.outvol[j][i]=cue_list[createno].mix.outvol[j][i];
+    }
     add_cue(cue_list_number+1,c);
   }
 
@@ -2495,12 +2978,18 @@
 void *tty_thread(void *dummy){
   char buf;
 
-  while(!playback_exit){
+  while(1){
     int ret=read(ttyfd,&buf,1);
     if(ret==1){
       write(ttypipe[1],&buf,1);
     }
+    
+    pthread_mutex_lock(&master_mutex);
+    if(playback_exit)break;
+    pthread_mutex_unlock(&master_mutex);
   }
+  pthread_mutex_unlock(&master_mutex);
+
 }
 
 int main(int gratuitously,char *different[]){
@@ -2527,9 +3016,16 @@
     exit(1);
   }
 
-  audiofd=fopen("/dev/dsp1","wb");
-  if(!audiofd){
-    fprintf(stderr,"unable to open audio device: %s.\n",strerror(errno));
+  playfd=fopen("/dev/dsp1","wb");
+  if(!playfd){
+    fprintf(stderr,"unable to open audio device for playback: %s.\n",strerror(errno));
+    fprintf(stderr,"\nPress enter to continue\n");
+    getc(stdin);
+  }
+
+  recfd=fopen("/dev/dsp1","rb");
+  if(!recfd){
+    fprintf(stderr,"unable to open audio device fo record: %s.\n",strerror(errno));
     fprintf(stderr,"\nPress enter to continue\n");
     getc(stdin);
   }
@@ -2557,8 +3053,14 @@
   
   {
     pthread_t dummy;
-    pthread_create(&playback_thread_id,NULL,playback_thread,NULL);
     playback_active=1;
+    pthread_create(&playback_thread_id,NULL,playback_thread,NULL);
+  }
+  {
+    pthread_t dummy;
+    rec_active1=1;
+    rec_active2=1;
+    pthread_create(&record_thread_id,NULL,record_thread,NULL);
   }
 
 
@@ -2594,15 +3096,32 @@
   pthread_mutex_lock(&master_mutex);
   playback_exit=1;
   pthread_mutex_unlock(&master_mutex);
+  pthread_mutex_lock(&rec_mutex);
+  rec_exit=1;
+  pthread_mutex_unlock(&rec_mutex);
+
+  /* wake the record producer/consumer if either is waiting */
+  pthread_mutex_lock(&rec_buffer_mutex);
+  pthread_cond_broadcast(&rec_buffer_cond);
+  pthread_mutex_unlock(&rec_buffer_mutex);
 
   while(1){
     pthread_mutex_lock(&master_mutex);
     if(!playback_active)break;
-    sched_yield();
     pthread_mutex_unlock(&master_mutex);
+    sched_yield();
   }
   pthread_mutex_unlock(&master_mutex);
-  fclose(audiofd);
+
+  while(1){
+    pthread_mutex_lock(&rec_mutex);
+    if(!rec_active1 && !rec_active2)break;
+    pthread_mutex_unlock(&rec_mutex);
+    sched_yield();
+  }
+  pthread_mutex_unlock(&rec_mutex);
+  fclose(playfd);
+  fclose(recfd);
 
   unlink(lockfile);
 }  

<p><p>1.1                  MTG/postfish.c

Index: postfish.c
===================================================================
#define _GNU_SOURCE
#define _LARGEFILE_SOURCE 
#define _LARGEFILE64_SOURCE
#define _FILE_OFFSET_BITS 64
#define _REENTRANT 1
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/time.h>
#define __USE_GNU 1
#include <pthread.h>
#include <string.h>
#include <math.h>
#include <signal.h>
#include <curses.h>
#include <fcntl.h>

#include <sys/soundcard.h>
#include <sys/ioctl.h>

#include <smallft.c>

#define todB(x)   ((x)==0?-400.f:log((x)*(x))*4.34294480f)
#define fromdB(x) (exp((x)*.11512925f))  
#define toOC(n)     (log(n)*1.442695f-5.965784f)

#define MAX_BLOCKSIZE 32768
#define BANDS 35

tatic int inbytes=0;
static int outbytes=2;
static int rate=0;
static int ch=0;
static int signp=0;

#define MAX_DECAY_MS 2000

/* saved, but not part of a config profile */
static long TX[10]={-1,-1,-1,-1,-1,-1,-1,-1,-1,-1}; 
static long A=0;
static long B=-1;

typedef struct {
  long block_a;
  
  long dyn_p;
  long dynms;
  long dynt;
  long masteratt;
  long masteratt_p;
  long dynamicatt;
  long dynamicatt_p;
  long eq_p;
  long eqt[BANDS];
  long noise_p;
  long noiset[BANDS];

  long used;
  char name[20];

} configprofile;

#define CONFIG_MAX 5
static long configactive=0;
static configprofile configlist[CONFIG_MAX]={
  {
    8192,1,300,-60,0,1,0,1,1,
    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
    1,
    {-120,-120,-120,-120,-120,-120,-120,-120,-120,
     -120,-120,-120,-120,-120,-120,-120,-120,-120,
     -120,-120,-120,-120,-120,-120,-120,-120,-120,
     -120,-120,-120,-120,-120,-120,-120,-120},
    1,"default"
  }
};
static configprofile configdefault={
  8192,1,300,-60,0,1,0,1,1,
  {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
   0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  1,
  {-120,-120,-120,-120,-120,-120,-120,-120,-120,
   -120,-120,-120,-120,-120,-120,-120,-120,-120,
   -120,-120,-120,-120,-120,-120,-120,-120,-120,
   -120,-120,-120,-120,-120,-120,-120,-120},
  0,"default"
};
static configprofile wc;

/* working space */

tatic long block=8192;

tatic double eqt_feedbackmax[BANDS]={
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
};
static double eqt_feedbackav[BANDS]={
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
};
static long eqt_feedbackcount[BANDS]={
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
};

tatic int eq_dirty=1;
static double eq_tfull[(MAX_BLOCKSIZE>>1)+1];

tatic double noiset_feedbackmax[BANDS]={
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
};
static double noiset_feedbackav[BANDS]={
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
};

tatic long noiset_feedbackcount[BANDS]={
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
};

tatic int noise_dirty=1; /* mark on blocksize or noise filter setting changes */
static double noise_tfull[(MAX_BLOCKSIZE>>1)+1];
static int oc[(MAX_BLOCKSIZE>>1)+1];

tatic double *dyn_decay=NULL;
static long dyn_decaysize=0;

tatic double maxtimepre=0;
static double maxtimepost=0;
static double maxtimeprehold=0;
static double maxtimeposthold=0;

tatic off_t Acursor=0;
static off_t Bcursor=-1;
static long  T=-1;
static off_t cursor=0;
static long  primed=0;

pthread_mutex_t master_mutex=PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;

int playback_active=0;
int playback_exit=0;
drft_lookup fft;
pthread_t playback_thread_id;

<p>int ttyfd;
int ttypipe[2];
pthread_t tty_thread_id;

typedef struct {
  char *name;
  FILE *f;
  
  off_t begin;
  off_t end;
  off_t data;

} file_entry;

file_entry *file_list=NULL;
int file_entries=0;
int current_file_entry_number=-1;
file_entry *current_file_entry=NULL;

#include "form.c"

/* simple dB-scale volume scaling */
void master_att(double *b){
  int i;
  pthread_mutex_lock(&master_mutex);
  if(wc.masteratt_p && wc.masteratt!=0.){
    double val=fromdB(wc.masteratt*.1);
    pthread_mutex_unlock(&master_mutex);

    for(i=0;i<block;i++)
      b[i]*=val;

  }else
    pthread_mutex_unlock(&master_mutex);
}  

/* return a scaled amplitude */
inline double atan_amplitude(double amplitude,double thresh){
  if(fabs(amplitude)>thresh){
    /* use a scaled atan() rolloff, which will begin with a 1:1 slope
       but asymptotically approach our limit */
    double scale=(1./(M_PI*.5))*(1.-thresh);
    if(amplitude>0)
      return(atan((amplitude-thresh)/scale)*scale + thresh );
    else
      return(atan((amplitude+thresh)/scale)*scale - thresh );
  }else
    return(amplitude);
}

inline double atan_scale(double amplitude,double thresh){
  if(amplitude==0.)return 1.;
  return(atan_amplitude(amplitude,thresh)/amplitude);
}

/* limit output by block; atan-scale according to max amplitude */
void dynamic_limit(double **b){
  int i,j;
  pthread_mutex_lock(&master_mutex);
  {
    double thresh=fromdB(wc.dynt*.1);
    double y=0;
    double d=0;
    long ms=wc.dynms;
    pthread_mutex_unlock(&master_mutex);
    
    for(j=0;j<ch;j++)
      for(i=0;i<block;i++)
        if(fabs(b[j][i])>y)y=fabs(b[j][i]);

    memmove(dyn_decay+1,dyn_decay,sizeof(*dyn_decay)*(dyn_decaysize-1));
    dyn_decay[0]=y;
    j=rint(rate*.002*ms/block);

    if(j>dyn_decaysize)j=dyn_decaysize; /* be certain */
    for(i=1;i<j;i++)
      if(y<dyn_decay[i])y=dyn_decay[i];

    d=atan_scale(y,thresh);
    y=dyn_decay[0];

    pthread_mutex_lock(&master_mutex);
    if(maxtimepre<y)maxtimepre=y;
    if(maxtimepost<y*d)maxtimepost=y*d;
    if(maxtimeprehold<y)maxtimeprehold=y;
    if(maxtimeposthold<y*d)maxtimeposthold=y*d;
    if(wc.dyn_p && d!=1.){
      pthread_mutex_unlock(&master_mutex);

      for(j=0;j<ch;j++)
        for(i=0;i<block;i++)
          b[j][i]*=d;
    }else{
      pthread_mutex_unlock(&master_mutex);
    }
  }
}  

/* stretch/shrink dynamic range of output.  argument is dB added to a
   nominal 120dB range */
void dynamic_att(double *b){
  int i;
  pthread_mutex_lock(&master_mutex);
  if(wc.dynamicatt_p && wc.dynamicatt!=0.){
    double dBper120=wc.dynamicatt/1200.;
    pthread_mutex_unlock(&master_mutex);

    for(i=0;i<block;i++)
      b[i]*= fromdB(todB(b[i])*dBper120);

  }else
    pthread_mutex_unlock(&master_mutex);
}  

void refresh_thresh_array(double *t,long *set){
  int i;
  for(i=0;i<=block/2;i++){
    double hoc=toOC(i/(block*.5)*22050.)*4.;
    int ihoc=hoc;
    double dhoc=hoc-ihoc;
    double dB;

    if(ihoc>=BANDS-1){
      hoc=BANDS-1;
      ihoc=BANDS-2;
      dhoc=1.;
    }
    if(hoc<0.){
      hoc=0;
      ihoc=0;
      dhoc=0.;
    }

    oc[i]=rint(hoc);

    dhoc=sin(dhoc*M_PI/2)*sin(dhoc*M_PI/2);
    dB=set[ihoc]*(1.-dhoc)+set[ihoc+1]*dhoc;

    t[i]=fromdB(dB);
  }

}

void noise_filter(double *b){
  int i;
  double dv;
  pthread_mutex_lock(&master_mutex);
  if(wc.noise_p){
    if(noise_dirty){
      refresh_thresh_array(noise_tfull,wc.noiset);
      noise_dirty=0;
    }
      
    pthread_mutex_unlock(&master_mutex);
    
    dv=fabs(b[0])/noise_tfull[0];
    if(noiset_feedbackmax[oc[0]]<dv)noiset_feedbackmax[oc[0]]=dv;
    noiset_feedbackav[oc[0]]+=dv;
    noiset_feedbackcount[oc[0]]++;

    if(b[0]<0){
      b[0]+=noise_tfull[0];
      if(b[0]>0)b[0]=0;
    }else{
      b[0]-=noise_tfull[0];
      if(b[0]<0)b[0]=0;
    }

    for(i=1;i<block/2;i++){
      double M=sqrt(b[i*2]*b[i*2]+b[i*2-1]*b[i*2-1]);
      
      dv=M/noise_tfull[i];
      if(noiset_feedbackmax[oc[i]]<dv)noiset_feedbackmax[oc[i]]=dv;
      noiset_feedbackav[oc[i]]+=dv;
      noiset_feedbackcount[oc[i]]++;

      if(M-noise_tfull[i]>0){
        double div=(M-noise_tfull[i])/M;
        b[i*2]*=div;
        b[i*2-1]*=div;
      }else{
        b[i*2]=0;
        b[i*2-1]=0;
      }
    }

    dv=fabs(b[block-1])/noise_tfull[block/2];
    if(noiset_feedbackmax[oc[block/2]]<dv)noiset_feedbackmax[oc[block/2]]=dv;
    noiset_feedbackav[oc[block/2]]+=dv;
    noiset_feedbackcount[oc[block/2]]++;

    if(b[i*2-1]<0){
      b[i*2-1]+=noise_tfull[block/2];
      if(b[i*2-1]>0)b[i*2-1]=0;
    }else{
      b[i*2-1]-=noise_tfull[block/2];
      if(b[i*2-1]<0)b[i*2-1]=0;
    }

  }else{
    pthread_mutex_unlock(&master_mutex);
  }
}

void eq_filter(double *b){
  int i;
  double dv;
  pthread_mutex_lock(&master_mutex);
  if(wc.eq_p){

    if(eq_dirty){
      refresh_thresh_array(eq_tfull,wc.eqt);
      eq_dirty=0;
    }
      
    pthread_mutex_unlock(&master_mutex);
    
    b[0]*=eq_tfull[0];
    dv=fabs(b[0]);
    if(eqt_feedbackmax[oc[0]]<dv)eqt_feedbackmax[oc[0]]=dv;
    eqt_feedbackav[oc[0]]+=dv;
    eqt_feedbackcount[oc[0]]++;
    
    for(i=1;i<block/2;i++){
      b[i*2]*=eq_tfull[i];
      b[i*2-1]*=eq_tfull[i];
      
      dv=sqrt(b[i*2]*b[i*2]+b[i*2-1]*b[i*2-1]);
      if(eqt_feedbackmax[oc[i]]<dv)eqt_feedbackmax[oc[i]]=dv;
      eqt_feedbackav[oc[i]]+=dv;
      eqt_feedbackcount[oc[i]]++;

    }

    b[block-1]*=eq_tfull[block/2-1];
    dv=fabs(b[block-1]);
    if(eqt_feedbackmax[oc[i]]<dv)eqt_feedbackmax[oc[i]]=dv;
    eqt_feedbackav[oc[i]]+=dv;
    eqt_feedbackcount[oc[i]]++;

  }else{
    pthread_mutex_unlock(&master_mutex);
  }
}

int aseek(off_t pos){
  int i;

  if(pos<0)pos=0;

  pthread_mutex_lock(&master_mutex);
  for(i=0;i<file_entries;i++){
    current_file_entry=file_list+i;
    current_file_entry_number=i;
    if(current_file_entry->begin<=pos && current_file_entry->end>pos){
      fseek(current_file_entry->f,pos-current_file_entry->begin+current_file_entry->data,SEEK_SET);
      cursor=pos;
      //primed=0;
      pthread_mutex_unlock(&master_mutex);
      return 0;
    }
  }
  i--;

  pos=current_file_entry->end;
  fseek(current_file_entry->f,pos-current_file_entry->begin+current_file_entry->data,SEEK_SET);
  cursor=pos;
  pthread_mutex_unlock(&master_mutex);
  return(0);
}

/* shift data if necessary, read across input file segment boundaries if necessary */
int aread(double **buf){
  int read_b=0,i,j,k;
  int toread_b=block*ch*inbytes;
  unsigned char *readbuf;
  double M,S;
  
  pthread_mutex_lock(&master_mutex);
  if(Bcursor<0 && cursor>=current_file_entry->end){
    pthread_mutex_unlock(&master_mutex);
    return -1; /* EOF */
  }

  if(primed){
    for(j=0;j<ch;j++)
      memmove(buf[j],buf[j]+block/2,sizeof(**buf)*block/2);
    toread_b/=2;
  }

  readbuf=alloca(toread_b);

  while(toread_b){
    off_t ret;
    off_t read_this_loop=current_file_entry->end-cursor;
    if(read_this_loop>toread_b)read_this_loop=toread_b;

    ret=fread(readbuf+read_b,1,read_this_loop,current_file_entry->f);

    if(ret>=0){
      read_b+=ret;
      toread_b-=ret;
      cursor+=ret;
    }

    if(Bcursor!=-1 && cursor>=Bcursor){
      aseek(Acursor);
    }else if(cursor>=current_file_entry->end){
      if(current_file_entry_number+1<file_entries){
        current_file_entry_number++;
        current_file_entry++;
        fseek(current_file_entry->f,current_file_entry->data,SEEK_SET);
      }else{
        memset(readbuf+read_b,0,toread_b);
        read_b+=toread_b;
        toread_b=0;
      }
    }
  }
  
  k=0;
  for(i=(primed?block/2:0);i<block;i++){
    for(j=0;j<ch;j++){
      int val=0;
      switch(inbytes){
      case 1:
        val=(readbuf[k]<<24);
        break;
      case 2:
        val=(readbuf[k]<<16)|(readbuf[k+1]<<24);
        break;
      case 3:
        val=(readbuf[k]<<8)|(readbuf[k+1]<<16)|(readbuf[k+2]<<24);
        break;
      case 4:
        val=(readbuf[k])|(readbuf[k+1]<<8)|(readbuf[k+2]<<16)|(readbuf[k+3]<<24);
        break;
      }
      if(signp)
        buf[j][i]=val*4.6566128730e-10;
      else
        buf[j][i]=(val^0x80000000UL)*4.6566128730e-10;

      k+=inbytes;
    }
    if(ch==2){
      M=(buf[0][i]+buf[1][i])*.5;
      S=(buf[0][i]-buf[1][i])*.5;
      buf[0][i]=M;
      buf[1][i]=S;
    }
  }    
  primed=1;
  pthread_mutex_unlock(&master_mutex);
  return 0;
}

void PutNumLE(long num,FILE *f,int bytes){
  int i=0;
  while(bytes--){
    fputc((num>>(i<<3))&0xff,f);
    i++;
  }
}
void WriteWav(FILE *f,long channels,long rate,long bits,long duration){
  fseek(f,0,SEEK_SET);
  fprintf(f,"RIFF");
  PutNumLE(duration+44-8,f,4);
  fprintf(f,"WAVEfmt ");
  PutNumLE(16,f,4);
  PutNumLE(1,f,2);
  PutNumLE(channels,f,2);
  PutNumLE(rate,f,4);
  PutNumLE(rate*channels*((bits-1)/8+1),f,4);
  PutNumLE(((bits-1)/8+1)*channels,f,2);
  PutNumLE(bits,f,2);
  fprintf(f,"data");
  PutNumLE(duration,f,4);
}

/* playback must be halted to change blocksize. */
void *playback_thread(void *vfile){
  FILE *playback_fd=NULL;
  char *file=(char *)vfile;
  int i,j,k;
  int format=AFMT_S16_NE;
  int channels=ch;
  int irate=rate;
  unsigned char *audiobuf;
  int audiobufsize;
  int ret,fd;
  double **buf;
  double **work;
  double **save;
  double *window;
  int bigendianp=0;
  double scale;
  int last=0;

  pthread_mutex_lock(&master_mutex);
  block=wc.block_a;
  scale=2./block;
  eq_dirty=1;
  noise_dirty=1;

  maxtimepost=0.;
  maxtimepre=0.;
  maxtimeposthold=0.;
  maxtimeprehold=0.;
  pthread_mutex_unlock(&master_mutex);

  dyn_decaysize=MAX_DECAY_MS/(block*500./rate);
  dyn_decay=alloca(sizeof(*dyn_decay)*dyn_decaysize);
  memset(dyn_decay,0,sizeof(*dyn_decay)*dyn_decaysize);

  playback_fd=fopen(file,"wb");
  if(!playback_fd){
    pthread_mutex_lock(&master_mutex);
    playback_active=0;
    playback_exit=0;
    pthread_mutex_unlock(&master_mutex);
    return NULL;
  }

  if(!strncmp(file,"/dev/",5)){
    
    fd=fileno(playback_fd);
    ret=ioctl(fd,SNDCTL_DSP_SETFMT,&format);
    if(ret || format!=AFMT_S16_NE){
      fprintf(stderr,"Could not set AFMT_S16_NE playback\n");
      exit(1);
    }
    ret=ioctl(fd,SNDCTL_DSP_CHANNELS,&channels);
    if(ret || channels!=ch){
      fprintf(stderr,"Could not set %d channel playback\n",ch);
      exit(1);
    }
    ret=ioctl(fd,SNDCTL_DSP_SPEED,&irate);
    if(ret || irate!=rate){
      fprintf(stderr,"Could not set %dHz playback\n",44100);
      exit(1);
    }

    if(AFMT_S16_NE==AFMT_S16_BE)bigendianp=1;

  }else{
    WriteWav(playback_fd,ch,rate,outbytes*8,file_list[file_entries-1].end);
  }

  buf=alloca(sizeof(*buf)*ch);
  work=alloca(sizeof(*work)*ch);
  save=alloca(sizeof(*save)*ch);
  for(i=0;i<ch;i++){
    buf[i]=alloca(sizeof(**buf)*block);
    work[i]=alloca(sizeof(**work)*block);
    save[i]=alloca(sizeof(**save)*block/2);
    memset(save[i],0,sizeof(**save)*block/2);
  }
  audiobufsize=block/2*ch*outbytes;
  audiobuf=alloca(audiobufsize);
  window=alloca(block*sizeof(*window));

  drft_init(&fft,block);
  for(i=0;i<block;i++)
    window[i]=sin((i+.5)/block*M_PI);

  aseek(cursor);
  pthread_mutex_lock(&master_mutex);
  primed=0;
  pthread_mutex_unlock(&master_mutex);

  while(1){
    pthread_mutex_lock(&master_mutex);
    if(playback_exit){
      pthread_mutex_unlock(&master_mutex);
      break;
    }
    pthread_mutex_unlock(&master_mutex);

    /* get data */
    if(aread(buf))break;

    for(j=0;j<ch;j++){
      double *v=buf[j];
      double *w=work[j];
 
      /* window */
      for(i=0;i<block;i++)
        w[i]=v[i]*window[i]*scale;

      /* forward FFT */
      drft_forward(&fft,w);

      /* noise thresh */
      noise_filter(w);

      /* EQ */
      eq_filter(w);

      /* dynamic range att */
      dynamic_att(w);
      
      /* inverse FFT */
      drft_backward(&fft,w);

      /* master att */
      master_att(w);

    }

    /* Back to L/R if stereo */
    
    if(ch==2){
      for(i=0;i<block;i++){
        double L=(work[0][i]+work[1][i])*.5;
        double R=(work[0][i]-work[1][i])*.5;
        work[0][i]=L;
        work[1][i]=R;
      }
    }

    dynamic_limit(work);
      
    for(j=0;j<ch;j++){
      double *w=work[j];

      /* window */
      for(i=0;i<block;i++)
        w[i]*=window[i];

      /* add */
      for(i=0;i<block/2;i++)
        w[i]+=save[j][i];
      
      /* save */
      for(i=0;i<block/2;i++)
        save[j][i]=w[i+block/2];
      
    }

    k=0;
    for(i=0;i<block/2;i++){
      for(j=0;j<ch;j++){
        int val=rint(work[j][i]*32767.);
        if(val>32767)val=32767;
        if(val<-32768)val=-32768;
        if(bigendianp){
          audiobuf[k++]=val>>8;
          audiobuf[k++]=val;
        }else{
          audiobuf[k++]=val;
          audiobuf[k++]=val>>8;
        }
      }
    }
    {
      struct timeval tv;
      long foo;
      gettimeofday(&tv,NULL);
      foo=tv.tv_sec*10+tv.tv_usec/100000;
      if(last!=foo)
        write(ttypipe[1],"",1);
      last=foo;
    }

    fwrite(audiobuf,1,audiobufsize,playback_fd);

  }

  pthread_mutex_lock(&master_mutex);
  fclose(playback_fd);
  playback_active=0;
  playback_exit=0;
  drft_clear(&fft);
  pthread_mutex_unlock(&master_mutex);
  write(ttypipe[1],"",1);
  return(NULL);
}

void _analysis(char *base,int i,double *v,int n,int dB){
  int j;
  FILE *of;
  char buffer[80];

  sprintf(buffer,"%s_%d.m",base,i);
  of=fopen(buffer,"w");
  
  if(!of)perror("failed to open data dump file");
  
  for(j=0;j<n;j++){
    fprintf(of,"%f ",(double)toOC((j+1)*22050./n)*2);
    
    if(dB){
      float val;
      if(v[j]==0.)
        val=-140.;
      else
        val=todB(v[j]);
      fprintf(of,"%f\n",val);
    }else{
      fprintf(of,"%f\n",v[j]);
    }
  }
  fclose(of);
}

void save_settings(int fd){
  FILE *f;
  int i,j;

  if(fd>=0){
    lseek(fd,0,SEEK_SET);
    ftruncate(fd,0);
    f=fdopen(fd,"r+");
    
    memcpy(&configlist[configactive],&wc,sizeof(wc));

    for(i=0;i<CONFIG_MAX;i++){
      if(configlist[i].used){
        
        fprintf(f,"PROFILE:%d\n",i);
        
        fprintf(f,"BLOCK:%ld\n",configlist[i].block_a);
        fprintf(f,"NOISE_P:%ld\n",configlist[i].noise_p);
        fprintf(f,"NOISE_T:");
        for(j=0;j<BANDS;j++)fprintf(f,"%ld:",configlist[i].noiset[j]);
        fprintf(f,"\n");
        fprintf(f,"EQ_P:%ld\n",configlist[i].eq_p);
        fprintf(f,"EQ_T:");
        for(j=0;j<BANDS;j++)fprintf(f,"%ld:",configlist[i].eqt[j]);
        fprintf(f,"\n");
        
        fprintf(f,"DYN_P:%ld\n",configlist[i].dyn_p);
        fprintf(f,"DYN_T:%ld\n",configlist[i].dynt);
        fprintf(f,"DYN_MS:%ld\n",configlist[i].dynms);
        
        fprintf(f,"MASTERATT:%ld\n",configlist[i].masteratt);
        fprintf(f,"DYNAMICATT:%ld\n",configlist[i].dynamicatt);
        
        fprintf(f,"MASTERATT_P:%ld\n",configlist[i].masteratt_p);
        fprintf(f,"DYNAMICATT_P:%ld\n",configlist[i].dynamicatt_p);
        
        fprintf(f,"NAME:%s\n",configlist[i].name);
      }
    }
    
    fprintf(f,"CONFIG_ACTIVE:%ld\n",configactive);
    
    fprintf(f,"A:%ld\n",A);
    fprintf(f,"B:%ld\n",B);
    for(i=0;i<10;i++)
      fprintf(f,"TIME_%d:%ld\n",i,TX[i]);
  
    fclose(f);
  }
}

void confparse_ld(char *line,char *match,long *assign){
  char buffer[160];
  snprintf(buffer,160,"%s:%%ld",match);
  sscanf(line,buffer,assign);
}
void confparse_f(char *line,char *match,double *assign){
  char buffer[160];
  snprintf(buffer,160,"%s:%%f",match);
  sscanf(line,buffer,assign);
}
void confparse_s(char *line,char *match,char *assign,int len){
  char buffer[160];
  snprintf(buffer,160,"%s:%%s",match);
  if(strlen(buffer)-strlen(match)-3<(unsigned)len)
    sscanf(line,buffer,assign);
}
void confparse_bands(char *line,char *match,long *assign){
  char buffer[320];
  snprintf(buffer,320,
           "%s:%%ld:%%ld:%%ld:%%ld:%%ld:%%ld:%%ld:%%ld:%%ld:"
              "%%ld:%%ld:%%ld:%%ld:%%ld:%%ld:%%ld:%%ld:%%ld:"
              "%%ld:%%ld:%%ld:%%ld:%%ld:%%ld:%%ld:%%ld:%%ld:"
              "%%ld:%%ld:%%ld:%%ld:%%ld:%%ld:%%ld:%%ld:",match);
  sscanf(line,buffer,
         assign+0,assign+1,assign+2,assign+3,
         assign+4,assign+5,assign+6,assign+7,
         assign+8,assign+9,assign+10,assign+11,
         assign+12,assign+13,assign+14,assign+15,
         assign+16,assign+17,

         assign+18,assign+19,assign+20,assign+21,
         assign+22,assign+23,assign+24,assign+25,
         assign+26,assign+27,assign+28,assign+29,
         assign+30,assign+31,assign+32,assign+33,
         assign+34);

}

void load_settings(int fd){
  char buffer[1024];
  long i=0;
  FILE *f;
  lseek(fd,0,SEEK_SET);
  f=fdopen(fd,"r");
  
  while(fgets(buffer,1024,f)){

    confparse_ld(buffer,"PROFILE",&i);

    if(i>=0 && i< CONFIG_MAX){

      configlist[i].used=1;

      confparse_ld(buffer,"BLOCK",&configlist[i].block_a);
      confparse_ld(buffer,"NOISE_P",&configlist[i].noise_p);
      confparse_bands(buffer,"NOISE_T",configlist[i].noiset);
      confparse_ld(buffer,"EQ_P",&configlist[i].eq_p);
      confparse_bands(buffer,"EQ_T",configlist[i].eqt);
      
      confparse_ld(buffer,"DYN_P",&configlist[i].dyn_p);
      confparse_ld(buffer,"DYN_T",&configlist[i].dynt);
      confparse_ld(buffer,"DYN_MS",&configlist[i].dynms);
    
      confparse_ld(buffer,"MASTERATT",&configlist[i].masteratt);
      confparse_ld(buffer,"DYNAMICATT",&configlist[i].dynamicatt);
      confparse_ld(buffer,"MASTERATT_P",&configlist[i].masteratt_p);
      confparse_ld(buffer,"DYNAMICATT_P",&configlist[i].dynamicatt_p);
      
      confparse_s(buffer,"NAME",configlist[i].name,sizeof(configlist[i].name));
    }

    confparse_ld(buffer,"A",&A);
    confparse_ld(buffer,"B",&B);

    confparse_ld(buffer,"TIME_0",&TX[0]);
    confparse_ld(buffer,"TIME_1",&TX[1]);
    confparse_ld(buffer,"TIME_2",&TX[2]);
    confparse_ld(buffer,"TIME_3",&TX[3]);
    confparse_ld(buffer,"TIME_4",&TX[4]);
    confparse_ld(buffer,"TIME_5",&TX[5]);
    confparse_ld(buffer,"TIME_6",&TX[6]);
    confparse_ld(buffer,"TIME_7",&TX[7]);
    confparse_ld(buffer,"TIME_8",&TX[8]);
    confparse_ld(buffer,"TIME_9",&TX[9]);
    confparse_ld(buffer,"CONFIG_ACTIVE",&configactive);

  }

  Acursor=time_to_cursor(A);
  Bcursor=time_to_cursor(B);
  if(Acursor<0)Acursor=0;
  if(Acursor>file_list[file_entries-1].end)Acursor=file_list[file_entries-1].end;
  if(Bcursor>file_list[file_entries-1].end)Bcursor=file_list[file_entries-1].end;
  
  memcpy(&wc,&configlist[configactive],sizeof(wc));

}

void *tty_thread(void *dummy){
  char buf;

  while(1){
    int ret=read(ttyfd,&buf,1);
    if(ret==1){
      write(ttypipe[1],&buf,1);
    }
  }

}

int main(int argc, char **argv){
  off_t total=0;
  int i,j;
  int configfd;

  /* parse command line and open all the input files */
  file_list=calloc(argc-1,sizeof(file_entry));
  file_entries=argc-1;
  for(i=1;i<argc;i++){
    FILE *f=fopen(argv[i],"rb");
    if(f){
      unsigned char buffer[81];
      off_t filelength;
      int datap=0;
      int fmtp=0;
      file_list[i-1].name=strdup(argv[i]);
      file_list[i-1].f=f;
      
      /* parse header (well, sort of) and get file size */
      fseek(f,0,SEEK_END);
      filelength=ftell(f);
      fseek(f,0,SEEK_SET);

      fread(buffer,1,12,f);
      if(strncmp(buffer,"RIFF",4) || strncmp(buffer+8,"WAVE",4)){
        fprintf(stderr,"%s: Not a WAVE file.\n",argv[i]);
        exit(1);
      }

      while(fread(buffer,1,8,f)==8){
        int chunklen=buffer[4]|(buffer[5]<<8)|(buffer[6]<<16)|(buffer[7]<<24);

        if(!strncmp(buffer,"fmt ",4)){
          int ltype;
          int lch;
          int lrate;
          int lbits;

          if(chunklen>80){
            fprintf(stderr,"%s: WAVE file fmt chunk too large to parse.\n",argv[i]);
            exit(1);
          }
          fread(buffer,1,chunklen,f);
          
          ltype=buffer[0] |(buffer[1]<<8);
          lch=  buffer[2] |(buffer[3]<<8);
          lrate=buffer[4] |(buffer[5]<<8)|(buffer[6]<<16)|(buffer[7]<<24);
          lbits=buffer[14]|(buffer[15]<<8);

          if(ltype!=1){
            fprintf(stderr,"%s: WAVE file not PCM.\n",argv[i]);
            exit(1);
          }

          if(i==1){
            ch=lch;
            rate=lrate;
            inbytes=lbits/8;
            if(inbytes>1)signp=1;
          }else{
            if(ch!=lch){
              fprintf(stderr,"%s: WAVE files must all have same number of channels.\n",argv[i]);
              exit(1);
            }
            if(rate!=lrate){
              fprintf(stderr,"%s: WAVE files must all be same sampling rate.\n",argv[i]);
              exit(1);
            }
            if(inbytes!=lbits/8){
              fprintf(stderr,"%s: WAVE files must all be same sample width.\n",argv[i]);
              exit(1);
            }
          }
          fmtp=1;
        } else if(!strncmp(buffer,"data",4)){
          int pos=ftell(f);
          if(!fmtp){
            fprintf(stderr,"%s: WAVE fmt chunk must preceed data chunk.\n",argv[i]);
            exit(1);
          }
          datap=1;
          
          filelength=(filelength-pos)/(ch*inbytes)*(ch*inbytes)+pos;
          
          if(chunklen+pos<=filelength){
            file_list[i-1].begin=total;
            total=file_list[i-1].end=total+chunklen;
            fprintf(stderr,"%s: Using declared file size.\n",argv[i]);
          }else{
            file_list[i-1].begin=total;
            total=file_list[i-1].end=total+filelength-pos;
            fprintf(stderr,"%s: Using actual file size.\n",argv[i]);
          }
          file_list[i-1].data=ftell(f);
          break;
        } else {
          fprintf(stderr,"%s: Unknown chunk type %c%c%c%c; skipping.\n",argv[i],
                  buffer[0],buffer[1],buffer[2],buffer[3]);
          for(j=0;j<chunklen;j++)
            if(fgetc(f)==EOF)break;
        }
      }

      if(!datap){
        fprintf(stderr,"%s: WAVE file has no data chunk.\n",argv[i]);
        exit(1);
      }
      
    }else{
      fprintf(stderr,"%s: Unable to open file.\n",argv[i]);
      exit(1);
    }
  }

  /* load config */
  {
    configfd=open(".postfishrc",O_RDWR|O_CREAT,0666);
    if(configfd>=0)load_settings(configfd);
  }

  /* set up the hack for interthread ncurses event triggering through
   input subversion */
  ttyfd=open("/dev/tty",O_RDONLY);

  if(ttyfd<0){
    fprintf(stderr,"Unable to open /dev/tty:\n"
            "  %s\n",strerror(errno));
    
    exit(1);
  }
  if(pipe(ttypipe)){
    fprintf(stderr,"Unable to open tty pipe:\n"
            "  %s\n",strerror(errno));
    
    exit(1);
  }
  dup2(ttypipe[0],0);

  pthread_create(&tty_thread_id,NULL,tty_thread,NULL);

  aseek(0);

  {
    int ysize=10+BANDS;
    int xsize=104;

    initscr(); cbreak(); noecho();
    nonl();
    intrflush(stdscr, FALSE);
    keypad(stdscr, TRUE);
    use_default_colors();
    clear();
    def_prog_mode();           /* save current tty modes */
    refresh();
    clear();

    if(LINES<ysize || COLS<xsize){
      endwin();
      fprintf(stderr,"Minimum required terminal size: %d cols x %d lines\n",
                  xsize, ysize);
      exit(1);
    }

    form_init(&editf,120,1);
    form_init(&noneditf,50,0);
    box(stdscr,0,0);
    mvaddstr(0, 2, " MTG Postprocess Filter build 20021120.0 ");
    mvaddstr(LINES-1, 2, 
             " [<]<<   [,]<   [Spc] Play/Pause   [Bksp] Stop   [.]>   [>]>>   [p] Process ");

    mvaddstr(LINES-1, COLS-12, " [q] Quit ");

    update_static_N();
    update_N();
    
    update_static_E();
    update_E();

    update_static_D();
    update_D();

    update_a();
    update_b();
    update_C();
    update_play();
    update_static_0();

    refresh();
    
    signal(SIGINT,SIG_IGN);

    while(1){
      int c=form_handle_char(&editf,getch());
      if(c=='q')break;
      switch(c){
      case '<':
        pthread_mutex_lock(&master_mutex);
        aseek(cursor-rate*ch*inbytes*60);
        pthread_mutex_unlock(&master_mutex);
        update_ui();
        break;
      case ',':
        pthread_mutex_lock(&master_mutex);
        aseek(cursor-rate*ch*inbytes*5);
        pthread_mutex_unlock(&master_mutex);
        update_ui();
        break;
      case '>':
        pthread_mutex_lock(&master_mutex);
        aseek(cursor+rate*ch*inbytes*60);
        pthread_mutex_unlock(&master_mutex);
        update_ui();
        break;
      case '.':
        pthread_mutex_lock(&master_mutex);
        aseek(cursor+rate*ch*inbytes*5);
        pthread_mutex_unlock(&master_mutex);
        update_ui();
        break;
      case 'n':
        pthread_mutex_lock(&master_mutex);
        wc.noise_p=!wc.noise_p;
        pthread_mutex_unlock(&master_mutex);
        update_N();
        break;
      case 'e':
        pthread_mutex_lock(&master_mutex);
        wc.eq_p=!wc.eq_p;
        pthread_mutex_unlock(&master_mutex);
        update_E();
        break;
      case 'l':
        pthread_mutex_lock(&master_mutex);
        wc.dyn_p=!wc.dyn_p;
        pthread_mutex_unlock(&master_mutex);
        update_D();
        break;
      case 'm':
        pthread_mutex_lock(&master_mutex);
        wc.masteratt_p=!wc.masteratt_p;
        pthread_mutex_unlock(&master_mutex);
        update_D();
        break;
      case 'r':
        pthread_mutex_lock(&master_mutex);
        wc.dynamicatt_p=!wc.dynamicatt_p;
        pthread_mutex_unlock(&master_mutex);
        update_D();
        break;
      case '0':case '1':case '2':case '3':case '4':
      case '5':case '6':case '7':case '8':case '9':
        {
          int num=c-48;
          if(TX[num]!=-1){
            pthread_mutex_lock(&master_mutex);
            aseek(time_to_cursor(TX[num]));
            pthread_mutex_unlock(&master_mutex);
            update_play();
          }else{
            pthread_mutex_lock(&master_mutex);
            TX[num]=cursor_to_time(cursor);
            pthread_mutex_unlock(&master_mutex);
            update_0();
          }
        }
        update_ui();
        break;
      case ')':
        TX[0]=-1;update_0();break;
      case '!':
        TX[1]=-1;update_0();break;
      case '@':
        TX[2]=-1;update_0();break;
      case '#':
        TX[3]=-1;update_0();break;
      case '$':
        TX[4]=-1;update_0();break;
      case '%':
        TX[5]=-1;update_0();break;
      case '^':
        TX[6]=-1;update_0();break;
      case '&':
        TX[7]=-1;update_0();break;
      case '*':
        TX[8]=-1;update_0();break;
      case '(':
        TX[9]=-1;update_0();break;
      case 'a':
        pthread_mutex_lock(&master_mutex);
        if(Acursor==0){
          Acursor=cursor;
          pthread_mutex_unlock(&master_mutex);
          A=cursor_to_time(cursor);
          if(B<A){
            pthread_mutex_lock(&master_mutex);
            B=-1;
            Bcursor=-1;
            pthread_mutex_unlock(&master_mutex);
          }
          update_a();
        }else{
          aseek(Acursor);
          pthread_mutex_unlock(&master_mutex);
        }
        break;	    
      case 'b':
        pthread_mutex_lock(&master_mutex);
        if(Bcursor==-1){
          Bcursor=cursor;
          pthread_mutex_unlock(&master_mutex);
          B=cursor_to_time(cursor);
          if(B<A){
            pthread_mutex_lock(&master_mutex);
            B=-1;
            Bcursor=-1;
            pthread_mutex_unlock(&master_mutex);
          }
          update_b();
        }else
          pthread_mutex_unlock(&master_mutex);
        break;
      case 'A':
        pthread_mutex_lock(&master_mutex);
        Acursor=0;
        pthread_mutex_unlock(&master_mutex);
        A=0;
        update_a();
        break;
      case 'B':
        pthread_mutex_lock(&master_mutex);
        Bcursor=-1;
        pthread_mutex_unlock(&master_mutex);
        B=-1;
        update_b();
        break;
      case '\b':case KEY_BACKSPACE:case KEY_DC:
        pthread_mutex_lock(&master_mutex);
        if(playback_active){
          playback_exit=1;
          pthread_mutex_unlock(&master_mutex);
          sched_yield();
          while(1){
            pthread_mutex_lock(&master_mutex);
            if(playback_active){
              pthread_mutex_unlock(&master_mutex);
              sched_yield();
            }else
              break;
          }
        }
        cursor=Acursor;
        pthread_mutex_unlock(&master_mutex);
        update_play();
        break;
      case ' ':
        pthread_mutex_lock(&master_mutex);
        if(!playback_active){
          playback_active=1;
          pthread_mutex_unlock(&master_mutex);
          update_play();
          pthread_create(&playback_thread_id,NULL,&playback_thread,"/dev/dsp");
        }else{
          playback_exit=1;
          pthread_mutex_unlock(&master_mutex);
          sched_yield();
          while(1){
            pthread_mutex_lock(&master_mutex);
            if(playback_active){
              pthread_mutex_unlock(&master_mutex);
              sched_yield();
            }else
              break;
          }
          pthread_mutex_unlock(&master_mutex);
          update_play();
        }
        break;
      case 'v':case 'w':case 'x':case 'y':case 'z':
        {
          int num=c-118;
          memcpy(configlist+configactive,&wc,sizeof(wc));
          if(!configlist[num].used){
            memcpy(configlist+num,&wc,sizeof(wc));
            configlist[num].used=1;
          }
          pthread_mutex_lock(&master_mutex);
          memcpy(&wc,configlist+num,sizeof(wc));
          eq_dirty=1;
          noise_dirty=1;
          configactive=num;
          pthread_mutex_unlock(&master_mutex);
          update_C();
          update_N();
          update_E();
          update_ui();
          form_redraw(&editf);
          form_redraw(&noneditf);
          update_D();
        }
        break;
      case 'V':case 'W':case 'X':case 'Y':case 'Z':
        {
          int num=c-86;
          memcpy(configlist+num,&configdefault,sizeof(configdefault));
          configlist[configactive].used=1;
          if(configactive==num){
            pthread_mutex_lock(&master_mutex);
            memcpy(&wc,configlist+configactive,sizeof(wc));
            eq_dirty=1;
            noise_dirty=1;
            configactive=num;
            pthread_mutex_unlock(&master_mutex);
          }
          update_C();
          update_N();
          update_E();
          update_ui();
          form_redraw(&editf);
          form_redraw(&noneditf);
          update_D();
        }
        break;
      case 0:
        update_play();
        update_ui();
        break;
      default:
        update_ui();
        break;
      }
    }
  }

  pthread_mutex_lock(&master_mutex);
  playback_exit=1;
  pthread_mutex_unlock(&master_mutex);

  while(1){
    pthread_mutex_lock(&master_mutex);
    if(playback_active){
      pthread_mutex_unlock(&master_mutex);
      sched_yield();
    }else
      break;
  }
  endwin();
  save_settings(configfd);
  if(configfd>=0)close(configfd);
  return(0);
}

<p><p>1.1                  MTG/smallft.c

Index: smallft.c
===================================================================
/********************************************************************
 *                                                                  *
 * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE.   *
 * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS     *
 * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
 * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING.       *
 *                                                                  *
 * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2001             *
 * by the XIPHOPHORUS Company http://www.xiph.org/                  *
 *                                                                  *
 ********************************************************************

 function: *unnormalized* fft transform
 last mod: $Id: smallft.c,v 1.1 2002/11/24 01:21:18 xiphmont Exp $

 ********************************************************************/

/* FFT implementation from OggSquish, minus cosine transforms,
 * minus all but radix 2/4 case.  In Vorbis we only need this
 * cut-down version.
 *
 * To do more than just power-of-two sized vectors, see the full
 * version I wrote for NetLib.
 *
 * Note that the packing is a little strange; rather than the FFT r/i
 * packing following R_0, I_n, R_1, I_1, R_2, I_2 ... R_n-1, I_n-1,
 * it follows R_0, R_1, I_1, R_2, I_2 ... R_n-1, I_n-1, I_n like the
 * FORTRAN version
 */

#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "smallft.h"

tatic void drfti1(int n, double *wa, int *ifac){
  static int ntryh[4] = { 4,2,3,5 };
  static double tpi = 6.28318530717958648f;
  double arg,argh,argld,fi;
  int ntry=0,i,j=-1;
  int k1, l1, l2, ib;
  int ld, ii, ip, is, nq, nr;
  int ido, ipm, nfm1;
  int nl=n;
  int nf=0;

 L101:
  j++;
  if (j < 4)
    ntry=ntryh[j];
  else
    ntry+=2;

 L104:
  nq=nl/ntry;
  nr=nl-ntry*nq;
  if (nr!=0) goto L101;

  nf++;
  ifac[nf+1]=ntry;
  nl=nq;
  if(ntry!=2)goto L107;
  if(nf==1)goto L107;

  for (i=1;i<nf;i++){
    ib=nf-i+1;
    ifac[ib+1]=ifac[ib];
  }
  ifac[2] = 2;

 L107:
  if(nl!=1)goto L104;
  ifac[0]=n;
  ifac[1]=nf;
  argh=tpi/n;
  is=0;
  nfm1=nf-1;
  l1=1;

  if(nfm1==0)return;

  for (k1=0;k1<nfm1;k1++){
    ip=ifac[k1+2];
    ld=0;
    l2=l1*ip;
    ido=n/l2;
    ipm=ip-1;

    for (j=0;j<ipm;j++){
      ld+=l1;
      i=is;
      argld=(double)ld*argh;
      fi=0.f;
      for (ii=2;ii<ido;ii+=2){
        fi+=1.f;
        arg=fi*argld;
        wa[i++]=cos(arg);
        wa[i++]=sin(arg);
      }
      is+=ido;
    }
    l1=l2;
  }
}

tatic void fdrffti(int n, double *wsave, int *ifac){

  if (n == 1) return;
  drfti1(n, wsave+n, ifac);
}

tatic void dradf2(int ido,int l1,double *cc,double *ch,double *wa1){
  int i,k;
  double ti2,tr2;
  int t0,t1,t2,t3,t4,t5,t6;

  t1=0;
  t0=(t2=l1*ido);
  t3=ido<<1;
  for(k=0;k<l1;k++){
    ch[t1<<1]=cc[t1]+cc[t2];
    ch[(t1<<1)+t3-1]=cc[t1]-cc[t2];
    t1+=ido;
    t2+=ido;
  }
    
  if(ido<2)return;
  if(ido==2)goto L105;

  t1=0;
  t2=t0;
  for(k=0;k<l1;k++){
    t3=t2;
    t4=(t1<<1)+(ido<<1);
    t5=t1;
    t6=t1+t1;
    for(i=2;i<ido;i+=2){
      t3+=2;
      t4-=2;
      t5+=2;
      t6+=2;
      tr2=wa1[i-2]*cc[t3-1]+wa1[i-1]*cc[t3];
      ti2=wa1[i-2]*cc[t3]-wa1[i-1]*cc[t3-1];
      ch[t6]=cc[t5]+ti2;
      ch[t4]=ti2-cc[t5];
      ch[t6-1]=cc[t5-1]+tr2;
      ch[t4-1]=cc[t5-1]-tr2;
    }
    t1+=ido;
    t2+=ido;
  }

  if(ido%2==1)return;

 L105:
  t3=(t2=(t1=ido)-1);
  t2+=t0;
  for(k=0;k<l1;k++){
    ch[t1]=-cc[t2];
    ch[t1-1]=cc[t3];
    t1+=ido<<1;
    t2+=ido;
    t3+=ido;
  }
}

tatic void dradf4(int ido,int l1,double *cc,double *ch,double *wa1,
            double *wa2,double *wa3){
  static double hsqt2 = .70710678118654752f;
  int i,k,t0,t1,t2,t3,t4,t5,t6;
  double ci2,ci3,ci4,cr2,cr3,cr4,ti1,ti2,ti3,ti4,tr1,tr2,tr3,tr4;
  t0=l1*ido;
  
  t1=t0;
  t4=t1<<1;
  t2=t1+(t1<<1);
  t3=0;

  for(k=0;k<l1;k++){
    tr1=cc[t1]+cc[t2];
    tr2=cc[t3]+cc[t4];

    ch[t5=t3<<2]=tr1+tr2;
    ch[(ido<<2)+t5-1]=tr2-tr1;
    ch[(t5+=(ido<<1))-1]=cc[t3]-cc[t4];
    ch[t5]=cc[t2]-cc[t1];

    t1+=ido;
    t2+=ido;
    t3+=ido;
    t4+=ido;
  }

  if(ido<2)return;
  if(ido==2)goto L105;

<p>  t1=0;
  for(k=0;k<l1;k++){
    t2=t1;
    t4=t1<<2;
    t5=(t6=ido<<1)+t4;
    for(i=2;i<ido;i+=2){
      t3=(t2+=2);
      t4+=2;
      t5-=2;

      t3+=t0;
      cr2=wa1[i-2]*cc[t3-1]+wa1[i-1]*cc[t3];
      ci2=wa1[i-2]*cc[t3]-wa1[i-1]*cc[t3-1];
      t3+=t0;
      cr3=wa2[i-2]*cc[t3-1]+wa2[i-1]*cc[t3];
      ci3=wa2[i-2]*cc[t3]-wa2[i-1]*cc[t3-1];
      t3+=t0;
      cr4=wa3[i-2]*cc[t3-1]+wa3[i-1]*cc[t3];
      ci4=wa3[i-2]*cc[t3]-wa3[i-1]*cc[t3-1];

      tr1=cr2+cr4;
      tr4=cr4-cr2;
      ti1=ci2+ci4;
      ti4=ci2-ci4;

      ti2=cc[t2]+ci3;
      ti3=cc[t2]-ci3;
      tr2=cc[t2-1]+cr3;
      tr3=cc[t2-1]-cr3;

      ch[t4-1]=tr1+tr2;
      ch[t4]=ti1+ti2;

      ch[t5-1]=tr3-ti4;
      ch[t5]=tr4-ti3;

      ch[t4+t6-1]=ti4+tr3;
      ch[t4+t6]=tr4+ti3;

      ch[t5+t6-1]=tr2-tr1;
      ch[t5+t6]=ti1-ti2;
    }
    t1+=ido;
  }
  if(ido&1)return;

 L105:
  
  t2=(t1=t0+ido-1)+(t0<<1);
  t3=ido<<2;
  t4=ido;
  t5=ido<<1;
  t6=ido;

  for(k=0;k<l1;k++){
    ti1=-hsqt2*(cc[t1]+cc[t2]);
    tr1=hsqt2*(cc[t1]-cc[t2]);

    ch[t4-1]=tr1+cc[t6-1];
    ch[t4+t5-1]=cc[t6-1]-tr1;

    ch[t4]=ti1-cc[t1+t0];
    ch[t4+t5]=ti1+cc[t1+t0];

    t1+=ido;
    t2+=ido;
    t4+=t3;
    t6+=ido;
  }
}

tatic void dradfg(int ido,int ip,int l1,int idl1,double *cc,double *c1,
                          double *c2,double *ch,double *ch2,double *wa){

  static double tpi=6.283185307179586f;
  int idij,ipph,i,j,k,l,ic,ik,is;
  int t0,t1,t2,t3,t4,t5,t6,t7,t8,t9,t10;
  double dc2,ai1,ai2,ar1,ar2,ds2;
  int nbd;
  double dcp,arg,dsp,ar1h,ar2h;
  int idp2,ipp2;
  
  arg=tpi/(double)ip;
  dcp=cos(arg);
  dsp=sin(arg);
  ipph=(ip+1)>>1;
  ipp2=ip;
  idp2=ido;
  nbd=(ido-1)>>1;
  t0=l1*ido;
  t10=ip*ido;

  if(ido==1)goto L119;
  for(ik=0;ik<idl1;ik++)ch2[ik]=c2[ik];

  t1=0;
  for(j=1;j<ip;j++){
    t1+=t0;
    t2=t1;
    for(k=0;k<l1;k++){
      ch[t2]=c1[t2];
      t2+=ido;
    }
  }

  is=-ido;
  t1=0;
  if(nbd>l1){
    for(j=1;j<ip;j++){
      t1+=t0;
      is+=ido;
      t2= -ido+t1;
      for(k=0;k<l1;k++){
        idij=is-1;
        t2+=ido;
        t3=t2;
        for(i=2;i<ido;i+=2){
          idij+=2;
          t3+=2;
          ch[t3-1]=wa[idij-1]*c1[t3-1]+wa[idij]*c1[t3];
          ch[t3]=wa[idij-1]*c1[t3]-wa[idij]*c1[t3-1];
        }
      }
    }
  }else{

    for(j=1;j<ip;j++){
      is+=ido;
      idij=is-1;
      t1+=t0;
      t2=t1;
      for(i=2;i<ido;i+=2){
        idij+=2;
        t2+=2;
        t3=t2;
        for(k=0;k<l1;k++){
          ch[t3-1]=wa[idij-1]*c1[t3-1]+wa[idij]*c1[t3];
          ch[t3]=wa[idij-1]*c1[t3]-wa[idij]*c1[t3-1];
          t3+=ido;
        }
      }
    }
  }

  t1=0;
  t2=ipp2*t0;
  if(nbd<l1){
    for(j=1;j<ipph;j++){
      t1+=t0;
      t2-=t0;
      t3=t1;
      t4=t2;
      for(i=2;i<ido;i+=2){
        t3+=2;
        t4+=2;
        t5=t3-ido;
        t6=t4-ido;
        for(k=0;k<l1;k++){
          t5+=ido;
          t6+=ido;
          c1[t5-1]=ch[t5-1]+ch[t6-1];
          c1[t6-1]=ch[t5]-ch[t6];
          c1[t5]=ch[t5]+ch[t6];
          c1[t6]=ch[t6-1]-ch[t5-1];
        }
      }
    }
  }else{
    for(j=1;j<ipph;j++){
      t1+=t0;
      t2-=t0;
      t3=t1;
      t4=t2;
      for(k=0;k<l1;k++){
        t5=t3;
        t6=t4;
        for(i=2;i<ido;i+=2){
          t5+=2;
          t6+=2;
          c1[t5-1]=ch[t5-1]+ch[t6-1];
          c1[t6-1]=ch[t5]-ch[t6];
          c1[t5]=ch[t5]+ch[t6];
          c1[t6]=ch[t6-1]-ch[t5-1];
        }
        t3+=ido;
        t4+=ido;
      }
    }
  }

L119:
  for(ik=0;ik<idl1;ik++)c2[ik]=ch2[ik];

  t1=0;
  t2=ipp2*idl1;
  for(j=1;j<ipph;j++){
    t1+=t0;
    t2-=t0;
    t3=t1-ido;
    t4=t2-ido;
    for(k=0;k<l1;k++){
      t3+=ido;
      t4+=ido;
      c1[t3]=ch[t3]+ch[t4];
      c1[t4]=ch[t4]-ch[t3];
    }
  }

  ar1=1.f;
  ai1=0.f;
  t1=0;
  t2=ipp2*idl1;
  t3=(ip-1)*idl1;
  for(l=1;l<ipph;l++){
    t1+=idl1;
    t2-=idl1;
    ar1h=dcp*ar1-dsp*ai1;
    ai1=dcp*ai1+dsp*ar1;
    ar1=ar1h;
    t4=t1;
    t5=t2;
    t6=t3;
    t7=idl1;

    for(ik=0;ik<idl1;ik++){
      ch2[t4++]=c2[ik]+ar1*c2[t7++];
      ch2[t5++]=ai1*c2[t6++];
    }

    dc2=ar1;
    ds2=ai1;
    ar2=ar1;
    ai2=ai1;

    t4=idl1;
    t5=(ipp2-1)*idl1;
    for(j=2;j<ipph;j++){
      t4+=idl1;
      t5-=idl1;

      ar2h=dc2*ar2-ds2*ai2;
      ai2=dc2*ai2+ds2*ar2;
      ar2=ar2h;

      t6=t1;
      t7=t2;
      t8=t4;
      t9=t5;
      for(ik=0;ik<idl1;ik++){
        ch2[t6++]+=ar2*c2[t8++];
        ch2[t7++]+=ai2*c2[t9++];
      }
    }
  }

  t1=0;
  for(j=1;j<ipph;j++){
    t1+=idl1;
    t2=t1;
    for(ik=0;ik<idl1;ik++)ch2[ik]+=c2[t2++];
  }

  if(ido<l1)goto L132;

  t1=0;
  t2=0;
  for(k=0;k<l1;k++){
    t3=t1;
    t4=t2;
    for(i=0;i<ido;i++)cc[t4++]=ch[t3++];
    t1+=ido;
    t2+=t10;
  }

  goto L135;

 L132:
  for(i=0;i<ido;i++){
    t1=i;
    t2=i;
    for(k=0;k<l1;k++){
      cc[t2]=ch[t1];
      t1+=ido;
      t2+=t10;
    }
  }

 L135:
  t1=0;
  t2=ido<<1;
  t3=0;
  t4=ipp2*t0;
  for(j=1;j<ipph;j++){

    t1+=t2;
    t3+=t0;
    t4-=t0;

    t5=t1;
    t6=t3;
    t7=t4;

    for(k=0;k<l1;k++){
      cc[t5-1]=ch[t6];
      cc[t5]=ch[t7];
      t5+=t10;
      t6+=ido;
      t7+=ido;
    }
  }

  if(ido==1)return;
  if(nbd<l1)goto L141;

  t1=-ido;
  t3=0;
  t4=0;
  t5=ipp2*t0;
  for(j=1;j<ipph;j++){
    t1+=t2;
    t3+=t2;
    t4+=t0;
    t5-=t0;
    t6=t1;
    t7=t3;
    t8=t4;
    t9=t5;
    for(k=0;k<l1;k++){
      for(i=2;i<ido;i+=2){
        ic=idp2-i;
        cc[i+t7-1]=ch[i+t8-1]+ch[i+t9-1];
        cc[ic+t6-1]=ch[i+t8-1]-ch[i+t9-1];
        cc[i+t7]=ch[i+t8]+ch[i+t9];
        cc[ic+t6]=ch[i+t9]-ch[i+t8];
      }
      t6+=t10;
      t7+=t10;
      t8+=ido;
      t9+=ido;
    }
  }
  return;

 L141:

  t1=-ido;
  t3=0;
  t4=0;
  t5=ipp2*t0;
  for(j=1;j<ipph;j++){
    t1+=t2;
    t3+=t2;
    t4+=t0;
    t5-=t0;
    for(i=2;i<ido;i+=2){
      t6=idp2+t1-i;
      t7=i+t3;
      t8=i+t4;
      t9=i+t5;
      for(k=0;k<l1;k++){
        cc[t7-1]=ch[t8-1]+ch[t9-1];
        cc[t6-1]=ch[t8-1]-ch[t9-1];
        cc[t7]=ch[t8]+ch[t9];
        cc[t6]=ch[t9]-ch[t8];
        t6+=t10;
        t7+=t10;
        t8+=ido;
        t9+=ido;
      }
    }
  }
}

tatic void drftf1(int n,double *c,double *ch,double *wa,int *ifac){
  int i,k1,l1,l2;
  int na,kh,nf;
  int ip,iw,ido,idl1,ix2,ix3;

  nf=ifac[1];
  na=1;
  l2=n;
  iw=n;

  for(k1=0;k1<nf;k1++){
    kh=nf-k1;
    ip=ifac[kh+1];
    l1=l2/ip;
    ido=n/l2;
    idl1=ido*l1;
    iw-=(ip-1)*ido;
    na=1-na;

    if(ip!=4)goto L102;

    ix2=iw+ido;
    ix3=ix2+ido;
    if(na!=0)
      dradf4(ido,l1,ch,c,wa+iw-1,wa+ix2-1,wa+ix3-1);
    else
      dradf4(ido,l1,c,ch,wa+iw-1,wa+ix2-1,wa+ix3-1);
    goto L110;

 L102:
    if(ip!=2)goto L104;
    if(na!=0)goto L103;

    dradf2(ido,l1,c,ch,wa+iw-1);
    goto L110;

  L103:
    dradf2(ido,l1,ch,c,wa+iw-1);
    goto L110;

  L104:
    if(ido==1)na=1-na;
    if(na!=0)goto L109;

    dradfg(ido,ip,l1,idl1,c,c,c,ch,ch,wa+iw-1);
    na=1;
    goto L110;

  L109:
    dradfg(ido,ip,l1,idl1,ch,ch,ch,c,c,wa+iw-1);
    na=0;

  L110:
    l2=l1;
  }

  if(na==1)return;

  for(i=0;i<n;i++)c[i]=ch[i];
}

tatic void dradb2(int ido,int l1,double *cc,double *ch,double *wa1){
  int i,k,t0,t1,t2,t3,t4,t5,t6;
  double ti2,tr2;

  t0=l1*ido;
  
  t1=0;
  t2=0;
  t3=(ido<<1)-1;
  for(k=0;k<l1;k++){
    ch[t1]=cc[t2]+cc[t3+t2];
    ch[t1+t0]=cc[t2]-cc[t3+t2];
    t2=(t1+=ido)<<1;
  }

  if(ido<2)return;
  if(ido==2)goto L105;

  t1=0;
  t2=0;
  for(k=0;k<l1;k++){
    t3=t1;
    t5=(t4=t2)+(ido<<1);
    t6=t0+t1;
    for(i=2;i<ido;i+=2){
      t3+=2;
      t4+=2;
      t5-=2;
      t6+=2;
      ch[t3-1]=cc[t4-1]+cc[t5-1];
      tr2=cc[t4-1]-cc[t5-1];
      ch[t3]=cc[t4]-cc[t5];
      ti2=cc[t4]+cc[t5];
      ch[t6-1]=wa1[i-2]*tr2-wa1[i-1]*ti2;
      ch[t6]=wa1[i-2]*ti2+wa1[i-1]*tr2;
    }
    t2=(t1+=ido)<<1;
  }

  if(ido%2==1)return;

L105:
  t1=ido-1;
  t2=ido-1;
  for(k=0;k<l1;k++){
    ch[t1]=cc[t2]+cc[t2];
    ch[t1+t0]=-(cc[t2+1]+cc[t2+1]);
    t1+=ido;
    t2+=ido<<1;
  }
}

tatic void dradb3(int ido,int l1,double *cc,double *ch,double *wa1,
                          double *wa2){
  static double taur = -.5f;
  static double taui = .8660254037844386f;
  int i,k,t0,t1,t2,t3,t4,t5,t6,t7,t8,t9,t10;
  double ci2,ci3,di2,di3,cr2,cr3,dr2,dr3,ti2,tr2;
  t0=l1*ido;

  t1=0;
  t2=t0<<1;
  t3=ido<<1;
  t4=ido+(ido<<1);
  t5=0;
  for(k=0;k<l1;k++){
    tr2=cc[t3-1]+cc[t3-1];
    cr2=cc[t5]+(taur*tr2);
    ch[t1]=cc[t5]+tr2;
    ci3=taui*(cc[t3]+cc[t3]);
    ch[t1+t0]=cr2-ci3;
    ch[t1+t2]=cr2+ci3;
    t1+=ido;
    t3+=t4;
    t5+=t4;
  }

  if(ido==1)return;

  t1=0;
  t3=ido<<1;
  for(k=0;k<l1;k++){
    t7=t1+(t1<<1);
    t6=(t5=t7+t3);
    t8=t1;
    t10=(t9=t1+t0)+t0;

    for(i=2;i<ido;i+=2){
      t5+=2;
      t6-=2;
      t7+=2;
      t8+=2;
      t9+=2;
      t10+=2;
      tr2=cc[t5-1]+cc[t6-1];
      cr2=cc[t7-1]+(taur*tr2);
      ch[t8-1]=cc[t7-1]+tr2;
      ti2=cc[t5]-cc[t6];
      ci2=cc[t7]+(taur*ti2);
      ch[t8]=cc[t7]+ti2;
      cr3=taui*(cc[t5-1]-cc[t6-1]);
      ci3=taui*(cc[t5]+cc[t6]);
      dr2=cr2-ci3;
      dr3=cr2+ci3;
      di2=ci2+cr3;
      di3=ci2-cr3;
      ch[t9-1]=wa1[i-2]*dr2-wa1[i-1]*di2;
      ch[t9]=wa1[i-2]*di2+wa1[i-1]*dr2;
      ch[t10-1]=wa2[i-2]*dr3-wa2[i-1]*di3;
      ch[t10]=wa2[i-2]*di3+wa2[i-1]*dr3;
    }
    t1+=ido;
  }
}

tatic void dradb4(int ido,int l1,double *cc,double *ch,double *wa1,
                          double *wa2,double *wa3){
  static double sqrt2=1.414213562373095f;
  int i,k,t0,t1,t2,t3,t4,t5,t6,t7,t8;
  double ci2,ci3,ci4,cr2,cr3,cr4,ti1,ti2,ti3,ti4,tr1,tr2,tr3,tr4;
  t0=l1*ido;
  
  t1=0;
  t2=ido<<2;
  t3=0;
  t6=ido<<1;
  for(k=0;k<l1;k++){
    t4=t3+t6;
    t5=t1;
    tr3=cc[t4-1]+cc[t4-1];
    tr4=cc[t4]+cc[t4]; 
    tr1=cc[t3]-cc[(t4+=t6)-1];
    tr2=cc[t3]+cc[t4-1];
    ch[t5]=tr2+tr3;
    ch[t5+=t0]=tr1-tr4;
    ch[t5+=t0]=tr2-tr3;
    ch[t5+=t0]=tr1+tr4;
    t1+=ido;
    t3+=t2;
  }

  if(ido<2)return;
  if(ido==2)goto L105;

  t1=0;
  for(k=0;k<l1;k++){
    t5=(t4=(t3=(t2=t1<<2)+t6))+t6;
    t7=t1;
    for(i=2;i<ido;i+=2){
      t2+=2;
      t3+=2;
      t4-=2;
      t5-=2;
      t7+=2;
      ti1=cc[t2]+cc[t5];
      ti2=cc[t2]-cc[t5];
      ti3=cc[t3]-cc[t4];
      tr4=cc[t3]+cc[t4];
      tr1=cc[t2-1]-cc[t5-1];
      tr2=cc[t2-1]+cc[t5-1];
      ti4=cc[t3-1]-cc[t4-1];
      tr3=cc[t3-1]+cc[t4-1];
      ch[t7-1]=tr2+tr3;
      cr3=tr2-tr3;
      ch[t7]=ti2+ti3;
      ci3=ti2-ti3;
      cr2=tr1-tr4;
      cr4=tr1+tr4;
      ci2=ti1+ti4;
      ci4=ti1-ti4;

      ch[(t8=t7+t0)-1]=wa1[i-2]*cr2-wa1[i-1]*ci2;
      ch[t8]=wa1[i-2]*ci2+wa1[i-1]*cr2;
      ch[(t8+=t0)-1]=wa2[i-2]*cr3-wa2[i-1]*ci3;
      ch[t8]=wa2[i-2]*ci3+wa2[i-1]*cr3;
      ch[(t8+=t0)-1]=wa3[i-2]*cr4-wa3[i-1]*ci4;
      ch[t8]=wa3[i-2]*ci4+wa3[i-1]*cr4;
    }
    t1+=ido;
  }

  if(ido%2 == 1)return;

 L105:

  t1=ido;
  t2=ido<<2;
  t3=ido-1;
  t4=ido+(ido<<1);
  for(k=0;k<l1;k++){
    t5=t3;
    ti1=cc[t1]+cc[t4];
    ti2=cc[t4]-cc[t1];
    tr1=cc[t1-1]-cc[t4-1];
    tr2=cc[t1-1]+cc[t4-1];
    ch[t5]=tr2+tr2;
    ch[t5+=t0]=sqrt2*(tr1-ti1);
    ch[t5+=t0]=ti2+ti2;
    ch[t5+=t0]=-sqrt2*(tr1+ti1);

    t3+=ido;
    t1+=t2;
    t4+=t2;
  }
}

tatic void dradbg(int ido,int ip,int l1,int idl1,double *cc,double *c1,
            double *c2,double *ch,double *ch2,double *wa){
  static double tpi=6.283185307179586f;
  int idij,ipph,i,j,k,l,ik,is,t0,t1,t2,t3,t4,t5,t6,t7,t8,t9,t10,
      t11,t12;
  double dc2,ai1,ai2,ar1,ar2,ds2;
  int nbd;
  double dcp,arg,dsp,ar1h,ar2h;
  int ipp2;

  t10=ip*ido;
  t0=l1*ido;
  arg=tpi/(double)ip;
  dcp=cos(arg);
  dsp=sin(arg);
  nbd=(ido-1)>>1;
  ipp2=ip;
  ipph=(ip+1)>>1;
  if(ido<l1)goto L103;
  
  t1=0;
  t2=0;
  for(k=0;k<l1;k++){
    t3=t1;
    t4=t2;
    for(i=0;i<ido;i++){
      ch[t3]=cc[t4];
      t3++;
      t4++;
    }
    t1+=ido;
    t2+=t10;
  }
  goto L106;

 L103:
  t1=0;
  for(i=0;i<ido;i++){
    t2=t1;
    t3=t1;
    for(k=0;k<l1;k++){
      ch[t2]=cc[t3];
      t2+=ido;
      t3+=t10;
    }
    t1++;
  }

 L106:
  t1=0;
  t2=ipp2*t0;
  t7=(t5=ido<<1);
  for(j=1;j<ipph;j++){
    t1+=t0;
    t2-=t0;
    t3=t1;
    t4=t2;
    t6=t5;
    for(k=0;k<l1;k++){
      ch[t3]=cc[t6-1]+cc[t6-1];
      ch[t4]=cc[t6]+cc[t6];
      t3+=ido;
      t4+=ido;
      t6+=t10;
    }
    t5+=t7;
  }

  if (ido == 1)goto L116;
  if(nbd<l1)goto L112;

  t1=0;
  t2=ipp2*t0;
  t7=0;
  for(j=1;j<ipph;j++){
    t1+=t0;
    t2-=t0;
    t3=t1;
    t4=t2;

    t7+=(ido<<1);
    t8=t7;
    for(k=0;k<l1;k++){
      t5=t3;
      t6=t4;
      t9=t8;
      t11=t8;
      for(i=2;i<ido;i+=2){
        t5+=2;
        t6+=2;
        t9+=2;
        t11-=2;
        ch[t5-1]=cc[t9-1]+cc[t11-1];
        ch[t6-1]=cc[t9-1]-cc[t11-1];
        ch[t5]=cc[t9]-cc[t11];
        ch[t6]=cc[t9]+cc[t11];
      }
      t3+=ido;
      t4+=ido;
      t8+=t10;
    }
  }
  goto L116;

 L112:
  t1=0;
  t2=ipp2*t0;
  t7=0;
  for(j=1;j<ipph;j++){
    t1+=t0;
    t2-=t0;
    t3=t1;
    t4=t2;
    t7+=(ido<<1);
    t8=t7;
    t9=t7;
    for(i=2;i<ido;i+=2){
      t3+=2;
      t4+=2;
      t8+=2;
      t9-=2;
      t5=t3;
      t6=t4;
      t11=t8;
      t12=t9;
      for(k=0;k<l1;k++){
        ch[t5-1]=cc[t11-1]+cc[t12-1];
        ch[t6-1]=cc[t11-1]-cc[t12-1];
        ch[t5]=cc[t11]-cc[t12];
        ch[t6]=cc[t11]+cc[t12];
        t5+=ido;
        t6+=ido;
        t11+=t10;
        t12+=t10;
      }
    }
  }

L116:
  ar1=1.f;
  ai1=0.f;
  t1=0;
  t9=(t2=ipp2*idl1);
  t3=(ip-1)*idl1;
  for(l=1;l<ipph;l++){
    t1+=idl1;
    t2-=idl1;

    ar1h=dcp*ar1-dsp*ai1;
    ai1=dcp*ai1+dsp*ar1;
    ar1=ar1h;
    t4=t1;
    t5=t2;
    t6=0;
    t7=idl1;
    t8=t3;
    for(ik=0;ik<idl1;ik++){
      c2[t4++]=ch2[t6++]+ar1*ch2[t7++];
      c2[t5++]=ai1*ch2[t8++];
    }
    dc2=ar1;
    ds2=ai1;
    ar2=ar1;
    ai2=ai1;

    t6=idl1;
    t7=t9-idl1;
    for(j=2;j<ipph;j++){
      t6+=idl1;
      t7-=idl1;
      ar2h=dc2*ar2-ds2*ai2;
      ai2=dc2*ai2+ds2*ar2;
      ar2=ar2h;
      t4=t1;
      t5=t2;
      t11=t6;
      t12=t7;
      for(ik=0;ik<idl1;ik++){
        c2[t4++]+=ar2*ch2[t11++];
        c2[t5++]+=ai2*ch2[t12++];
      }
    }
  }

  t1=0;
  for(j=1;j<ipph;j++){
    t1+=idl1;
    t2=t1;
    for(ik=0;ik<idl1;ik++)ch2[ik]+=ch2[t2++];
  }

  t1=0;
  t2=ipp2*t0;
  for(j=1;j<ipph;j++){
    t1+=t0;
    t2-=t0;
    t3=t1;
    t4=t2;
    for(k=0;k<l1;k++){
      ch[t3]=c1[t3]-c1[t4];
      ch[t4]=c1[t3]+c1[t4];
      t3+=ido;
      t4+=ido;
    }
  }

  if(ido==1)goto L132;
  if(nbd<l1)goto L128;

  t1=0;
  t2=ipp2*t0;
  for(j=1;j<ipph;j++){
    t1+=t0;
    t2-=t0;
    t3=t1;
    t4=t2;
    for(k=0;k<l1;k++){
      t5=t3;
      t6=t4;
      for(i=2;i<ido;i+=2){
        t5+=2;
        t6+=2;
        ch[t5-1]=c1[t5-1]-c1[t6];
        ch[t6-1]=c1[t5-1]+c1[t6];
        ch[t5]=c1[t5]+c1[t6-1];
        ch[t6]=c1[t5]-c1[t6-1];
      }
      t3+=ido;
      t4+=ido;
    }
  }
  goto L132;

 L128:
  t1=0;
  t2=ipp2*t0;
  for(j=1;j<ipph;j++){
    t1+=t0;
    t2-=t0;
    t3=t1;
    t4=t2;
    for(i=2;i<ido;i+=2){
      t3+=2;
      t4+=2;
      t5=t3;
      t6=t4;
      for(k=0;k<l1;k++){
        ch[t5-1]=c1[t5-1]-c1[t6];
        ch[t6-1]=c1[t5-1]+c1[t6];
        ch[t5]=c1[t5]+c1[t6-1];
        ch[t6]=c1[t5]-c1[t6-1];
        t5+=ido;
        t6+=ido;
      }
    }
  }

L132:
  if(ido==1)return;

  for(ik=0;ik<idl1;ik++)c2[ik]=ch2[ik];

  t1=0;
  for(j=1;j<ip;j++){
    t2=(t1+=t0);
    for(k=0;k<l1;k++){
      c1[t2]=ch[t2];
      t2+=ido;
    }
  }

  if(nbd>l1)goto L139;

  is= -ido-1;
  t1=0;
  for(j=1;j<ip;j++){
    is+=ido;
    t1+=t0;
    idij=is;
    t2=t1;
    for(i=2;i<ido;i+=2){
      t2+=2;
      idij+=2;
      t3=t2;
      for(k=0;k<l1;k++){
        c1[t3-1]=wa[idij-1]*ch[t3-1]-wa[idij]*ch[t3];
        c1[t3]=wa[idij-1]*ch[t3]+wa[idij]*ch[t3-1];
        t3+=ido;
      }
    }
  }
  return;

 L139:
  is= -ido-1;
  t1=0;
  for(j=1;j<ip;j++){
    is+=ido;
    t1+=t0;
    t2=t1;
    for(k=0;k<l1;k++){
      idij=is;
      t3=t2;
      for(i=2;i<ido;i+=2){
        idij+=2;
        t3+=2;
        c1[t3-1]=wa[idij-1]*ch[t3-1]-wa[idij]*ch[t3];
        c1[t3]=wa[idij-1]*ch[t3]+wa[idij]*ch[t3-1];
      }
      t2+=ido;
    }
  }
}

tatic void drftb1(int n, double *c, double *ch, double *wa, int *ifac){
  int i,k1,l1,l2;
  int na;
  int nf,ip,iw,ix2,ix3,ido,idl1;

  nf=ifac[1];
  na=0;
  l1=1;
  iw=1;

  for(k1=0;k1<nf;k1++){
    ip=ifac[k1 + 2];
    l2=ip*l1;
    ido=n/l2;
    idl1=ido*l1;
    if(ip!=4)goto L103;
    ix2=iw+ido;
    ix3=ix2+ido;

    if(na!=0)
      dradb4(ido,l1,ch,c,wa+iw-1,wa+ix2-1,wa+ix3-1);
    else
      dradb4(ido,l1,c,ch,wa+iw-1,wa+ix2-1,wa+ix3-1);
    na=1-na;
    goto L115;

  L103:
    if(ip!=2)goto L106;

    if(na!=0)
      dradb2(ido,l1,ch,c,wa+iw-1);
    else
      dradb2(ido,l1,c,ch,wa+iw-1);
    na=1-na;
    goto L115;

  L106:
    if(ip!=3)goto L109;

    ix2=iw+ido;
    if(na!=0)
      dradb3(ido,l1,ch,c,wa+iw-1,wa+ix2-1);
    else
      dradb3(ido,l1,c,ch,wa+iw-1,wa+ix2-1);
    na=1-na;
    goto L115;

  L109:
/*    The radix five case can be translated later..... */
/*    if(ip!=5)goto L112;

    ix2=iw+ido;
    ix3=ix2+ido;
    ix4=ix3+ido;
    if(na!=0)
      dradb5(ido,l1,ch,c,wa+iw-1,wa+ix2-1,wa+ix3-1,wa+ix4-1);
    else
      dradb5(ido,l1,c,ch,wa+iw-1,wa+ix2-1,wa+ix3-1,wa+ix4-1);
    na=1-na;
    goto L115;

  L112:*/
    if(na!=0)
      dradbg(ido,ip,l1,idl1,ch,ch,ch,c,c,wa+iw-1);
    else
      dradbg(ido,ip,l1,idl1,c,c,c,ch,ch,wa+iw-1);
    if(ido==1)na=1-na;

  L115:
    l1=l2;
    iw+=(ip-1)*ido;
  }

  if(na==0)return;

  for(i=0;i<n;i++)c[i]=ch[i];
}

void drft_forward(drft_lookup *l,double *data){
  if(l->n==1)return;
  drftf1(l->n,data,l->trigcache,l->trigcache+l->n,l->splitcache);
}

void drft_backward(drft_lookup *l,double *data){
  if (l->n==1)return;
  drftb1(l->n,data,l->trigcache,l->trigcache+l->n,l->splitcache);
}

void drft_init(drft_lookup *l,int n){
  l->n=n;
  l->trigcache=calloc(3*n,sizeof(*l->trigcache));
  l->splitcache=calloc(32,sizeof(*l->splitcache));
  fdrffti(n, l->trigcache, l->splitcache);
}

void drft_clear(drft_lookup *l){
  if(l){
    if(l->trigcache)free(l->trigcache);
    if(l->splitcache)free(l->splitcache);
    memset(l,0,sizeof(*l));
  }
}

<p><p>--- >8 ----
List archives:  http://www.xiph.org/archives/
Ogg project homepage: http://www.xiph.org/ogg/
To unsubscribe from this list, send a message to 'cvs-request at xiph.org'
containing only the word 'unsubscribe' in the body.  No subject is needed.
Unsubscribe messages sent to the list will be ignored/filtered.



More information about the commits mailing list