subband.c 18.6 KB
Newer Older
Monty Montgomery's avatar
 
Monty Montgomery committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
/*
 *
 *  postfish
 *    
 *      Copyright (C) 2002-2004 Monty and Xiph.Org
 *
 *  Postfish is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2, or (at your option)
 *  any later version.
 *   
 *  Postfish is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *   
 *  You should have received a copy of the GNU General Public License
 *  along with Postfish; see the file COPYING.  If not, write to the
 *  Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * 
 */

#include "postfish.h"
#include <fftw3.h>
#include "subband.h"
#include "lpc.h"

extern int input_size;
extern int input_rate;
extern int input_ch;

/* called only by initial setup */
int subband_load(subband_state *f,int bands,int qblocksize){
  int i,j;
  memset(f,0,sizeof(*f));

  f->qblocksize=qblocksize;
  f->bands=bands;
  f->fillstate=0;
  f->lap_samples=0;
  f->lap=malloc(bands*sizeof(*f->lap));
43 44 45 46 47 48 49 50 51 52 53
  f->cache0=malloc(input_ch*sizeof(*f->cache0));
  f->cache1=malloc(input_ch*sizeof(*f->cache1));

  f->lap_activeP=malloc(input_ch*sizeof(*f->lap_activeP));
  f->lap_active1=malloc(input_ch*sizeof(*f->lap_active1));
  f->lap_active0=malloc(input_ch*sizeof(*f->lap_active0));
  f->lap_activeC=malloc(input_ch*sizeof(*f->lap_activeC));

  f->visible1=malloc(input_ch*sizeof(*f->visible1));
  f->visible0=malloc(input_ch*sizeof(*f->visible0));
  f->visibleC=malloc(input_ch*sizeof(*f->visibleC));
Monty Montgomery's avatar
 
Monty Montgomery committed
54
  
55 56 57 58 59
  f->effect_activeP=malloc(input_ch*sizeof(*f->effect_activeP));
  f->effect_active1=malloc(input_ch*sizeof(*f->effect_active1));
  f->effect_active0=malloc(input_ch*sizeof(*f->effect_active0));
  f->effect_activeC=malloc(input_ch*sizeof(*f->effect_activeC));

60 61 62 63 64
  f->wP=calloc(input_ch,sizeof(*f->wP));
  f->w1=calloc(input_ch,sizeof(*f->w1));
  f->w0=calloc(input_ch,sizeof(*f->w0));
  f->wC=calloc(input_ch,sizeof(*f->wC));

65 66
  for(i=0;i<input_ch;i++)
    f->cache0[i]=malloc(input_size*sizeof(**f->cache0));
Monty Montgomery's avatar
 
Monty Montgomery committed
67
  for(i=0;i<input_ch;i++)
68
    f->cache1[i]=malloc(input_size*sizeof(**f->cache1));
Monty Montgomery's avatar
 
Monty Montgomery committed
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111

  for(i=0;i<bands;i++){
    f->lap[i]=malloc(input_ch*sizeof(**f->lap));
    for(j=0;j<input_ch;j++)
      f->lap[i][j]=malloc(input_size*3*sizeof(***f->lap));
  }

  f->out.size=input_size;
  f->out.channels=input_ch;
  f->out.rate=input_rate;
  f->out.data=malloc(input_ch*sizeof(*f->out.data));
  for(i=0;i<input_ch;i++)
    f->out.data[i]=malloc(input_size*sizeof(**f->out.data));

  /* fill in time window */
  f->window=malloc(f->qblocksize*2*sizeof(*f->window)); 
  /* we need a smooth-edged, symmetric window */
  for(i=0;i<f->qblocksize*2;i++)f->window[i]=sin(M_PIl*i/(f->qblocksize*2));
  for(i=0;i<f->qblocksize*2;i++)f->window[i]*=f->window[i]*.25/f->qblocksize;

  f->fftwf_forward_out  = fftwf_malloc(sizeof(*f->fftwf_forward_out) * 
				       (f->qblocksize*4+2));
  f->fftwf_backward_in  = fftwf_malloc(sizeof(*f->fftwf_backward_in) * 
				       (f->qblocksize*4+2));
  f->fftwf_forward_in   = fftwf_malloc(sizeof(*f->fftwf_forward_in) * 
				       f->qblocksize*4);
  f->fftwf_backward_out = fftwf_malloc(sizeof(*f->fftwf_backward_out) * 
				       f->qblocksize*4);
  
  f->fftwf_forward=fftwf_plan_dft_r2c_1d(f->qblocksize*4, 
					 f->fftwf_forward_in, 
					 (fftwf_complex *)f->fftwf_forward_out, 
					 FFTW_MEASURE);
  f->fftwf_backward=fftwf_plan_dft_c2r_1d(f->qblocksize*4,
					  (fftwf_complex *)f->fftwf_backward_in, 
					  f->fftwf_backward_out, 
					  FFTW_MEASURE);

  return(0);
}

/* called only by initial setup */
int subband_load_freqs(subband_state *f,subband_window *w,
112
		       const float *freq_list,int bands){
Monty Montgomery's avatar
 
Monty Montgomery committed
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
  int i,j;

  memset(w,0,sizeof(*w));

  w->freq_bands=bands;

  /* supersample the spectrum */
  w->ho_window=malloc(bands*sizeof(*w->ho_window));
  {
    float working[f->qblocksize*4+2];
    
    for(i=0;i<bands;i++){
      float lastf=(i>0?freq_list[i-1]:0);
      float thisf=freq_list[i];
      float nextf=freq_list[i+1];
      memset(working,0,sizeof(working));
      
      for(j=0;j<((f->qblocksize*2+1)<<5);j++){
        float localf= .5*j*input_rate/(f->qblocksize<<6);
        int localbin= j>>5;
        float localwin;

        if(localf>=lastf && localf<thisf){
          if(i==0)
            localwin=1.;
          else
            localwin= sin((localf-lastf)/(thisf-lastf)*M_PIl*.5);
          localwin*=localwin;
	  working[localbin]+=localwin*(1./32);

        }else if(localf>=thisf && localf<nextf){
          if(i+1==bands)
            localwin=1.;
          else
            localwin= sin((nextf-localf)/(nextf-thisf)*M_PIl*.5);
          
          localwin*=localwin;
          working[localbin]+=localwin*(1./32);
          
        }
      }

      /* window this desired response in the time domain so that our
         convolution is properly padded against being circular */
      memset(f->fftwf_backward_in,0,sizeof(*f->fftwf_backward_in)*
	     (f->qblocksize*4+2));
159
      for(j=0;j<f->qblocksize*2+2;j++)
Monty Montgomery's avatar
 
Monty Montgomery committed
160
	f->fftwf_backward_in[j*2]=working[j];
161

Monty Montgomery's avatar
 
Monty Montgomery committed
162
      fftwf_execute(f->fftwf_backward);
163

Monty Montgomery's avatar
 
Monty Montgomery committed
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
      /* window response in time */
      memcpy(f->fftwf_forward_in,f->fftwf_backward_out,
	     f->qblocksize*4*sizeof(*f->fftwf_forward_in));
      for(j=0;j<f->qblocksize;j++){
	float val=cos(j*M_PI/(f->qblocksize*2));
	val=sin(val*val*M_PIl*.5);
	f->fftwf_forward_in[j]*= sin(val*val*M_PIl*.5);
      }
      
      for(;j<f->qblocksize*3;j++)
	f->fftwf_forward_in[j]=0.;
      
      for(;j<f->qblocksize*4;j++){
	float val=sin((j-f->qblocksize*3)*M_PI/(f->qblocksize*2));
	val=sin(val*val*M_PIl*.5);
	f->fftwf_forward_in[j]*=sin(val*val*M_PIl*.5);
      }
      
      /* back to frequency; this is all-real data still */
      fftwf_execute(f->fftwf_forward);
      for(j=0;j<f->qblocksize*4+2;j++)
	f->fftwf_forward_out[j]/=f->qblocksize*4;
      
      /* now take what we learned and distill it a bit */
      w->ho_window[i]=calloc((f->qblocksize*2+1),sizeof(**w->ho_window));
      for(j=0;j<f->qblocksize*2+1;j++)
190
        w->ho_window[i][j]=f->fftwf_forward_out[j*2];
Monty Montgomery's avatar
 
Monty Montgomery committed
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
      
      lastf=thisf;
      
    }
  }
  
  return(0);
}

/* called only in playback thread */
int subband_reset(subband_state *f){
  /* reset cached pipe state */
  f->fillstate=0;
  return 0;
}

/* Brute force: subband the time data input by doing the equivalent of
   linear time-convolution using the response filters created earlier
   and padded FFTs with 75% overlap. */

211 212
static void subband_work(subband_state *f,
			 time_linkage *in,
213
			 subband_window **w,
214 215 216 217
			 int      *visible,
			 int      *active){


218
  int i,j,k,l,off;
Monty Montgomery's avatar
 
Monty Montgomery committed
219
  float *workoff=f->fftwf_forward_in+f->qblocksize;
220 221 222
  u_int32_t mutemask=in->active;

  f->mutemaskC=mutemask;
Monty Montgomery's avatar
 
Monty Montgomery committed
223 224 225

  for(i=0;i<input_ch;i++){

226 227 228
    int content_p=  f->lap_activeC[i]= (visible[i]||active[i]) && !mute_channel_muted(mutemask,i);
    int content_p0= f->lap_active0[i];
    int content_p1= f->lap_active1[i];
Monty Montgomery's avatar
 
Monty Montgomery committed
229

230 231 232 233
    int maxbands=w[i]->freq_bands;
    if(maxbands<f->w0[i]->freq_bands)maxbands=f->w0[i]->freq_bands;
    if(maxbands<f->w1[i]->freq_bands)maxbands=f->w1[i]->freq_bands;
    f->wC[i]=w[i];
Monty Montgomery's avatar
 
Monty Montgomery committed
234

235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
    f->effect_activeC[i] = active[i] && !mute_channel_muted(mutemask,i);
    f->visibleC[i] = visible[i];

    if(!content_p1 && !content_p0 && !content_p){
      /* all disabled, lap is already zeroed.  Nothing to do */
      continue;

    }else if(!content_p0 && content_p){
      /* from inactive to active; first block to be processed must be
         entirely within new data so prepare the lap */

      for(k=0;k<maxbands;k++)
	memset(f->lap[k][i]+input_size*2,0,f->qblocksize*3*sizeof(***f->lap));

    }else if (content_p0 && !content_p){
      /* from active to inactive; the previous lap is done, just zero out this frame */
      for(k=0;k<maxbands;k++)
	memset(f->lap[k][i]+input_size*2,0,input_size*sizeof(*f->lap[k][i]));
    } 

    if(content_p){ /* there is something to do */

      if(content_p0){ /* was lap active last block? */
	j=0; /* yes; span the gap */
	off=0;
      }else{
	j=3; /* no; start entirely within new data */
	off=f->qblocksize;
      }
Monty Montgomery's avatar
 
Monty Montgomery committed
264

265
      for(;j<(input_size/f->qblocksize);j++){
Monty Montgomery's avatar
 
Monty Montgomery committed
266
	
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
	switch(j){
	case 0:
	  memcpy(workoff,f->cache0[i]+input_size-f->qblocksize*2,
		 sizeof(*workoff)*f->qblocksize*2);
	  break;
	case 1:	
	  memcpy(workoff,f->cache0[i]+input_size-f->qblocksize,
		 sizeof(*workoff)*f->qblocksize);
	  memcpy(workoff+f->qblocksize,in->data[i],
		 sizeof(*workoff)*f->qblocksize);
	  break;
	default:
	  memcpy(workoff,in->data[i]+off,
		 sizeof(*workoff)*f->qblocksize*2);
	  off+=f->qblocksize;
	  break;
Monty Montgomery's avatar
 
Monty Montgomery committed
283 284
	}
	
285 286
	/* window; assume the edges are already zeroed */
	for(k=0;k<f->qblocksize*2;k++)workoff[k]*=f->window[k];
Monty Montgomery's avatar
 
Monty Montgomery committed
287
	
288 289 290
	fftwf_execute(f->fftwf_forward);
	
	/* repeatedly filter and transform back */
291
	for(k=0;k<w[i]->freq_bands;k++){
Monty Montgomery's avatar
 
Monty Montgomery committed
292
	  float *lapcb=f->lap[k][i]+input_size*2+(j-3)*f->qblocksize;
293 294
	  float *hw=w[i]->ho_window[k];

295 296
	  for(l=0;l<f->qblocksize*2+1;l++){
	    f->fftwf_backward_in[2*l]= 
297
	      f->fftwf_forward_out[2*l]*hw[l];
298
	    f->fftwf_backward_in[2*l+1]= 
299
	      f->fftwf_forward_out[2*l+1]*hw[l];
300 301 302 303 304 305 306 307 308 309 310
	  }
	  
	  fftwf_execute(f->fftwf_backward);
	  
	  /* lap back into the subband; the convolution is already
	     balanced so no further windowing needed */
	  for(l=0;l<f->qblocksize*3;l++)
	    lapcb[l]+=f->fftwf_backward_out[l];
	  for(;l<f->qblocksize*4;l++)
	    lapcb[l]=f->fftwf_backward_out[l];
	  
Monty Montgomery's avatar
 
Monty Montgomery committed
311 312
	}
      }
313 314
      /* if we're suddenly processing fewer bands than we were, we
	 have to trail out zeroes until the band lap is emptied */
315
      for(k=w[i]->freq_bands;k<maxbands;k++)
316 317
	memset(f->lap[k][i]+input_size*2,0,sizeof(*f->lap[k][i])*input_size);
	  
Monty Montgomery's avatar
 
Monty Montgomery committed
318 319 320 321
    }
  }
}

322 323
static void unsubband_work(subband_state *f,time_linkage *in, time_linkage *out){
  int i,j,k;
Monty Montgomery's avatar
 
Monty Montgomery committed
324

325
  f->lap_samples+=in->samples;
Monty Montgomery's avatar
 
Monty Montgomery committed
326 327
  for(i=0;i<input_ch;i++){
    
328 329 330 331
    int content_pP= f->lap_activeP[i];
    int content_p1= f->lap_active1[i];
    int content_p0= f->lap_active0[i];
    int content_pC= f->lap_activeC[i];
Monty Montgomery's avatar
 
Monty Montgomery committed
332

333 334 335
    int active_pP= f->effect_activeP[i];
    int active_p1= f->effect_active1[i];
    int active_p0= f->effect_active0[i];
Monty Montgomery's avatar
 
Monty Montgomery committed
336

337
    int muted_p1= mute_channel_muted(f->mutemask1,i);
Monty Montgomery's avatar
 
Monty Montgomery committed
338

339 340 341
    int maxbands=f->wC[i]->freq_bands;
    if(maxbands<f->w0[i]->freq_bands)maxbands=f->w0[i]->freq_bands;
    if(maxbands<f->w1[i]->freq_bands)maxbands=f->w1[i]->freq_bands;
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
  
    /* even if the lapping for a channel is active, we will draw
       output from the cache is the effect is inactive; it saves
       performing the summation across bands */

    /*   OUTPUT PROCESSING

	 if muted, lap is inactive
	 if visible or active (and not muted) lap is active
	 if inactive and invisible, lap is inactive

            lap  effect mute 
	 1. (I)   (I)    A  : muted; do nothing, the mask will take care of it
	 2.  X     I     I  : full bypass, audible data; rotate the cache into output
	 3.  I     A     I  : not possible
	 4.  A     A     I  : nominal
	 
	 we transition from effect active/inactive/active:

	 a) transitions always happen in the shared active portion
	 b) sum frame from active lap
	 c) window beginning/end of resulting summation if necessary
	 d) window/add beginning/end of final frame from cache data if necessary

    */

    if(out){
      if(muted_p1 || !active_p1){
	/* case 1,2; they're similar enough  */
	
	float *temp=f->cache1[i];
	f->cache1[i]=out->data[i];
	out->data[i]=temp;
	/* mutemask will be propogated later */
	
      }else{
	/* 'other' */
	
	/* b) sum the lap */
	float *o=out->data[i];
	memcpy(o,f->lap[0][i],input_size*sizeof(*out->data[i]));
	for(j=1;j<maxbands;j++){
	  float *x=f->lap[j][i];
	  for(k=0;k<input_size;k++)
	    o[k]+=x[k];
	}
	
	/* c) window beginning/end of summation if needed */
	if(!active_pP && content_pP){

 	  /* transitioning to active effect, but the lap was already
	     previously active; window the transition */
	  float *lo=o+f->qblocksize;
	  float scale=f->qblocksize*4;
	  memset(o,0,sizeof(*o)*f->qblocksize);
	  for(j=0;j<f->qblocksize;j++)
	    lo[j]*=f->window[j]*scale;
	  
	}else if (!active_p0 && content_p0){

	  /* transitioning from active effect, but the lap will continue
	     to be active; window the transition */
	  float *lo=o+input_size-f->qblocksize*2;
	  float *lw=f->window+f->qblocksize;
	  float scale=f->qblocksize*4;
	  memset(o+input_size-f->qblocksize,0,sizeof(*o)*f->qblocksize);
	  for(j=0;j<f->qblocksize;j++)
	    lo[j]*=lw[j]*scale;

	} /* otherwise any transitions we need are already windowed as
	     above by the lap handling code in subband_work */
	
	/* d) window/add cache bits if needed */
	if(!active_pP){
	  /* beginning transition */
	  float *lo=o+f->qblocksize;
	  float *lc=f->cache1[i]+f->qblocksize;
	  float *lw=f->window+f->qblocksize;
	  float scale=f->qblocksize*4;

	  memcpy(o,f->cache1[i],sizeof(*o)*f->qblocksize);
	  for(j=0;j<f->qblocksize;j++)
	    lo[j]+=lc[j]*lw[j]*scale;
	  
	}else if (!active_p0){
	  /* end transition */
	  float *lo=o+input_size-f->qblocksize*2;
	  float *lc=f->cache1[i]+input_size-f->qblocksize*2;
	  float scale=f->qblocksize*4;

	  memcpy(o+input_size-f->qblocksize,
		 f->cache1[i]+input_size-f->qblocksize,
		 sizeof(*o)*f->qblocksize);
	  for(j=0;j<f->qblocksize;j++)
	    lo[j]+=lc[j]*f->window[j]*scale;
	  
	}
      }
Monty Montgomery's avatar
 
Monty Montgomery committed
440

441 442 443 444 445 446 447 448
    }
    
    /* out is done for this channel */
    /* rotate lap */
    if(content_p1 || content_p0 || content_pC){
      for(j=0;j<maxbands;j++)
	memmove(f->lap[j][i],f->lap[j][i]+input_size,
		sizeof(***f->lap)*input_size*2);
Monty Montgomery's avatar
 
Monty Montgomery committed
449
      
450 451 452 453 454 455 456 457 458 459
      f->lap_activeP[i]=f->lap_active1[i];
      f->lap_active1[i]=f->lap_active0[i];
      f->lap_active0[i]=f->lap_activeC[i];
    }
    /* rotate cache data vectors */
    {
      float *temp=f->cache1[i];
      f->cache1[i]=f->cache0[i];
      f->cache0[i]=in->data[i];
      in->data[i]=temp;
Monty Montgomery's avatar
 
Monty Montgomery committed
460 461
    }
  }
462 463 464 465
  if(out){
    out->active=f->mutemask1;
    f->out.samples=(f->lap_samples>input_size?input_size:f->lap_samples);
  }
Monty Montgomery's avatar
 
Monty Montgomery committed
466

467
  /* rotate full-frame data for next frame */
Monty Montgomery's avatar
 
Monty Montgomery committed
468

469 470 471
  memcpy(f->effect_activeP,f->effect_active1,input_ch*sizeof(*f->effect_active1));
  memcpy(f->effect_active1,f->effect_active0,input_ch*sizeof(*f->effect_active0));
  memcpy(f->effect_active0,f->effect_activeC,input_ch*sizeof(*f->effect_activeC));
Monty Montgomery's avatar
 
Monty Montgomery committed
472

473 474
  memcpy(f->visible1,f->visible0,input_ch*sizeof(*f->visible0));
  memcpy(f->visible0,f->visibleC,input_ch*sizeof(*f->visibleC));
Monty Montgomery's avatar
 
Monty Montgomery committed
475

476 477 478 479
  f->mutemaskP=f->mutemask1;
  f->mutemask1=f->mutemask0;
  f->mutemask0=f->mutemaskC;

480 481 482
  memcpy(f->wP,f->w1,input_ch*sizeof(*f->w1));
  memcpy(f->w1,f->w0,input_ch*sizeof(*f->w0));
  memcpy(f->w0,f->wC,input_ch*sizeof(*f->wC));
483 484

  f->lap_samples-=(out?f->out.samples:0);
Monty Montgomery's avatar
 
Monty Montgomery committed
485 486 487 488 489

}

/* called only by playback thread */
time_linkage *subband_read(time_linkage *in, subband_state *f,
490
			   subband_window **w,int *visible, int *active,
491
			   void (*workfunc)(void *),void *arg){
Monty Montgomery's avatar
 
Monty Montgomery committed
492 493 494 495 496 497 498 499 500
  int i,j;
  
  switch(f->fillstate){
  case 0: /* begin priming the lapping and cache */
    if(in->samples==0){
      f->out.samples=0;
      return &f->out;
    }
    
501
    /* initially zero the lapping and cache */
Monty Montgomery's avatar
 
Monty Montgomery committed
502 503 504
    for(i=0;i<input_ch;i++){    
      for(j=0;j<f->bands;j++)    
	memset(f->lap[j][i],0,sizeof(***f->lap)*input_size*2);
505 506 507 508 509 510 511 512 513 514 515 516 517 518
      memset(f->cache1[i],0,sizeof(**f->cache1)*input_size);
      memset(f->cache0[i],0,sizeof(**f->cache0)*input_size);
    }

    /* and set up state variables */
    /* set the vars to 'active' so that if the first frame is an
       active frame, we don't transition window into it (the window
       would have been in the previous frame */
    for(i=0;i<input_ch;i++){
      int set=(visible[i]||active[i]) && !mute_channel_muted(in->active,i);
      memset(f->lap_activeP,set,sizeof(*f->lap_activeP)*input_ch);
      memset(f->lap_active1,set,sizeof(*f->lap_active1)*input_ch);
      memset(f->lap_active0,set,sizeof(*f->lap_active0)*input_ch);
      //memset(f->lap_activeC,1,sizeof(*f->lap_activeC)*input_ch);
519 520 521 522 523

      f->wP[i]=w[i];
      f->w1[i]=w[i];
      f->w0[i]=w[i];

Monty Montgomery's avatar
 
Monty Montgomery committed
524
    }
525 526 527 528 529 530 531 532 533 534 535 536 537
    
    memcpy(f->effect_activeP,active,sizeof(*f->effect_activeP)*input_ch);
    memcpy(f->effect_active1,active,sizeof(*f->effect_active1)*input_ch);
    memcpy(f->effect_active0,active,sizeof(*f->effect_active0)*input_ch);
    //memset(f->effect_activeC,1,sizeof(*f->effect_activeC)*input_ch);

    memset(f->visible1,0,sizeof(*f->visible1)*input_ch);
    memset(f->visible0,0,sizeof(*f->visible0)*input_ch);
    
    f->mutemaskP=in->active;
    f->mutemask1=in->active;
    f->mutemask0=in->active;

Monty Montgomery's avatar
 
Monty Montgomery committed
538 539 540
    /* initially zero the padding of the input working array */
    memset(f->fftwf_forward_in,0,f->qblocksize*4*sizeof(*f->fftwf_forward_in));

541 542 543 544
    /* extrapolation mechanism; avoid harsh transients at edges */
    for(i=0;i<input_ch;i++){
      
      if(f->lap_active0[i]){
545
	preextrapolate_helper(in->data[i],input_size,
546
			    f->cache0[i],input_size);
Monty Montgomery's avatar
 
Monty Montgomery committed
547
      
548 549 550 551 552
	if(in->samples<in->size)
	  postextrapolate_helper(f->cache0[i],input_size,
				 in->data[i],in->samples,
				 in->data[i]+in->samples,
				 in->size-in->samples);
Monty Montgomery's avatar
 
Monty Montgomery committed
553 554
      }
    }
555 556 557 558
      
    subband_work(f,in,w,visible,active);
    workfunc(arg);
    unsubband_work(f,in,NULL);
Monty Montgomery's avatar
 
Monty Montgomery committed
559 560 561 562 563 564 565 566 567 568 569 570

    f->fillstate=1;
    f->out.samples=0;
    if(in->samples==in->size)goto tidy_up;
    
    for(i=0;i<input_ch;i++)
      memset(in->data[i],0,sizeof(**in->data)*in->size);
    in->samples=0;
    /* fall through */

  case 1: /* finish priming the lapping and cache */

571 572 573
    /* extrapolation mechanism; avoid harsh transients at edges */

    if(in->samples<in->size)
Monty Montgomery's avatar
 
Monty Montgomery committed
574
      for(i=0;i<input_ch;i++){
575 576
	if((active[i] || visible[i]) && !mute_channel_muted(in->active,i))
	  postextrapolate_helper(f->cache0[i],input_size,
Monty Montgomery's avatar
 
Monty Montgomery committed
577 578 579 580 581
				 in->data[i],in->samples,
				 in->data[i]+in->samples,
				 in->size-in->samples);
      }
      
582 583 584
    subband_work(f,in,w,visible,active);
    workfunc(arg);
    unsubband_work(f,in,NULL);
Monty Montgomery's avatar
 
Monty Montgomery committed
585 586 587 588 589 590 591 592 593 594 595 596

    f->fillstate=2;
    f->out.samples=0;
    if(in->samples==in->size)goto tidy_up;
    
    for(i=0;i<input_ch;i++)
      memset(in->data[i],0,sizeof(**in->data)*in->size);
    in->samples=0;
    /* fall through */

  case 2: /* nominal processing */
    
597
    if(in->samples<in->size)
Monty Montgomery's avatar
 
Monty Montgomery committed
598
      for(i=0;i<input_ch;i++){
599 600
	if((active[i] || visible[i]) && !mute_channel_muted(in->active,i))
	  postextrapolate_helper(f->cache0[i],input_size,
Monty Montgomery's avatar
 
Monty Montgomery committed
601 602 603 604 605
				 in->data[i],in->samples,
				 in->data[i]+in->samples,
				 in->size-in->samples);
      }
      
606 607 608 609 610
    subband_work(f,in,w,visible,active);
    workfunc(arg);
    unsubband_work(f,in,&f->out);

    if(f->out.samples<f->out.size)f->fillstate=3;
Monty Montgomery's avatar
 
Monty Montgomery committed
611 612 613 614 615 616 617 618 619 620 621 622 623 624
    break;

  case 3: /* we've pushed out EOF already */
    f->out.samples=0;
    break;
  }

 tidy_up:
  {
    int tozero=f->out.size-f->out.samples;
    if(tozero)
      for(i=0;i<f->out.channels;i++)
        memset(f->out.data[i]+f->out.samples,0,sizeof(**f->out.data)*tozero);
  }
625
  
Monty Montgomery's avatar
 
Monty Montgomery committed
626 627
  return &f->out;
}