singlecomp.c 15.9 KB
Newer Older
1 2 3 4
/*
 *
 *  postfish
 *    
Monty Montgomery's avatar
Monty Montgomery committed
5
 *      Copyright (C) 2002-2005 Monty and Xiph.Org
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 *
 *  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 "feedback.h"
26
#include "window.h"
27
#include "follower.h"
28 29 30 31 32 33
#include "singlecomp.h"

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

34 35 36 37 38 39 40 41 42 43
typedef struct {
  sig_atomic_t u_thresh;
  sig_atomic_t u_ratio;

  sig_atomic_t o_thresh;
  sig_atomic_t o_ratio;
  
  sig_atomic_t b_ratio;
} atten_cache;

44 45 46 47
typedef struct{
  time_linkage out;
  feedback_generic_pool feedpool;

48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
  iir_state o_iir[MAX_INPUT_CHANNELS];
  iir_state u_iir[MAX_INPUT_CHANNELS];
  iir_state b_iir[MAX_INPUT_CHANNELS];
  int o_delay[MAX_INPUT_CHANNELS];
  int u_delay[MAX_INPUT_CHANNELS];
  int b_delay[MAX_INPUT_CHANNELS];

  peak_state o_peak[MAX_INPUT_CHANNELS];
  peak_state u_peak[MAX_INPUT_CHANNELS];
  peak_state b_peak[MAX_INPUT_CHANNELS];

  iir_filter o_attack[MAX_INPUT_CHANNELS];
  iir_filter u_attack[MAX_INPUT_CHANNELS];
  iir_filter b_attack[MAX_INPUT_CHANNELS];
  iir_filter o_decay[MAX_INPUT_CHANNELS];
  iir_filter u_decay[MAX_INPUT_CHANNELS];
  iir_filter b_decay[MAX_INPUT_CHANNELS];
65 66 67 68 69

  int fillstate;
  float **cache;
  int cache_samples;

70 71 72 73 74
  int *activeP;
  int *active0;

  int mutemaskP;
  int mutemask0;
75
  int ch;
76 77 78

  atten_cache *prevset;
  atten_cache *currset;
79 80
} singlecomp_state;

81
static float *window;
82 83 84 85 86 87 88 89 90

singlecomp_settings  singlecomp_master_set;
singlecomp_settings *singlecomp_channel_set;

static singlecomp_settings **master_set_bundle;
static singlecomp_settings **channel_set_bundle;

static singlecomp_state     master_state;
static singlecomp_state     channel_state;
91 92 93 94 95 96 97 98 99 100 101 102 103

/* feedback! */
typedef struct singlecomp_feedback{
  feedback_generic parent_class;
  float *peak;
  float *rms;
} singlecomp_feedback;

static feedback_generic *new_singlecomp_feedback(void){
  singlecomp_feedback *ret=calloc(1,sizeof(*ret));
  return (feedback_generic *)ret;
}

104 105
static int pull_singlecomp_feedback(singlecomp_state *scs, float *peak,float *rms){
  singlecomp_feedback *f=(singlecomp_feedback *)feedback_pull(&scs->feedpool);
106 107 108 109
  
  if(!f)return 0;
  
  if(peak)
110
    memcpy(peak,f->peak,sizeof(*peak)*scs->ch);
111
  if(rms)
112
    memcpy(rms,f->rms,sizeof(*rms)*scs->ch);
113
  feedback_old(&scs->feedpool,(feedback_generic *)f);
114 115 116
  return 1;
}

117 118 119
int pull_singlecomp_feedback_master(float *peak,float *rms){
  return pull_singlecomp_feedback(&master_state,peak,rms);
}
120

121 122 123
int pull_singlecomp_feedback_channel(float *peak,float *rms){
  return pull_singlecomp_feedback(&channel_state,peak,rms);
}
124

125
static void singlecomp_load_helper(singlecomp_state *scs,int ch){
126
  int i;
127
  memset(scs,0,sizeof(*scs));
128

129 130 131
  scs->ch=ch;
  scs->activeP=calloc(scs->ch,sizeof(*scs->activeP));
  scs->active0=calloc(scs->ch,sizeof(*scs->active0));
132

133 134 135
  scs->out.channels=scs->ch;
  scs->out.data=malloc(scs->ch*sizeof(*scs->out.data));
  for(i=0;i<scs->ch;i++)
136
    scs->out.data[i]=malloc(input_size*sizeof(**scs->out.data));
137

138
  scs->fillstate=0;
139 140
  scs->cache=malloc(scs->ch*sizeof(*scs->cache));
  for(i=0;i<scs->ch;i++)
141 142
    scs->cache[i]=malloc(input_size*sizeof(**scs->cache));

143 144
  scs->prevset=malloc(ch*sizeof(*scs->prevset));
  scs->currset=malloc(ch*sizeof(*scs->currset));
145
}
146

147
/* called only by initial setup */
148
int singlecomp_load(int outch){
149
  int i;
150 151
  singlecomp_load_helper(&master_state,outch);
  singlecomp_load_helper(&channel_state,input_ch);
152

153
  window=window_get(1,input_size/2);
154 155

  singlecomp_channel_set=calloc(input_ch,sizeof(*singlecomp_channel_set));
156
  master_set_bundle=malloc(outch*sizeof(*master_set_bundle));
157
  channel_set_bundle=malloc(input_ch*sizeof(*channel_set_bundle));
158
  for(i=0;i<input_ch;i++)
159
    channel_set_bundle[i]=&singlecomp_channel_set[i];
160 161
  for(i=0;i<outch;i++)
    master_set_bundle[i]=&singlecomp_master_set;
162 163

  return 0;
164 165 166
}

static void filter_set(float msec,
167 168 169
		       int order,
		       int attackp,
                       iir_filter *filter){
170 171 172 173 174
  float alpha;
  float corner_freq= 500./msec;
  
  /* make sure the chosen frequency doesn't require a lookahead
     greater than what's available */
175
  if(step_freq(input_size)*1.01>corner_freq && attackp && order==2)
176 177 178
    corner_freq=step_freq(input_size);
  
  alpha=corner_freq/input_rate;
179
  filter->g=mkbessel(alpha,order,filter->c);
180 181 182 183 184
  filter->alpha=alpha;
  filter->Hz=alpha*input_rate;
  filter->ms=msec;
}

185 186 187 188
static void reset_onech_filter(singlecomp_state *scs,int i){
  memset(scs->o_peak+i,0,sizeof(*scs->o_peak));
  memset(scs->u_peak+i,0,sizeof(*scs->u_peak));
  memset(scs->b_peak+i,0,sizeof(*scs->b_peak));
189 190 191 192

  /* all filters are set to 0, even the ones that steady-state at one,
     because we know that our steepest attack will complete in the
     pre-charge time, but there's no such guarantee about decay */
193 194 195
  memset(scs->o_iir+i,0,sizeof(*scs->o_iir));
  memset(scs->u_iir+i,0,sizeof(*scs->u_iir));
  memset(scs->b_iir+i,0,sizeof(*scs->b_iir));
196 197 198 199 200 201

  /* delays are only used for soft-starting individual filters when we
     know things began at unity multiplier */
  scs->o_delay[i]=0;
  scs->u_delay[i]=0;
  scs->b_delay[i]=0;
202 203 204 205 206 207
}

static void reset_filter(singlecomp_state *scs){
  int i;
  for(i=0;i<scs->ch;i++)
    reset_onech_filter(scs,i);
208 209
}

210
/* called only in playback thread */
211
int singlecomp_reset(void){
212
  /* reset cached pipe state */
213 214 215 216 217 218 219
  master_state.fillstate=0;
  channel_state.fillstate=0;
  while(pull_singlecomp_feedback_master(NULL,NULL));
  while(pull_singlecomp_feedback_channel(NULL,NULL));

  reset_filter(&master_state);
  reset_filter(&channel_state);
220 221 222
  return 0;
}

223 224 225 226
static void work_and_lapping(singlecomp_state *scs,
			     singlecomp_settings **scset,
			     time_linkage *in,
			     time_linkage *out,
227 228 229
			     int *active,
                             int link){
  int i,j;
230 231 232 233
  int have_feedback=0;
  u_int32_t mutemaskC=in->active;
  u_int32_t mutemask0=scs->mutemask0;
  u_int32_t mutemaskP=scs->mutemaskP;
234 235
  int iterations = (link?1:scs->ch);
  int chper = (link?scs->ch:1);
236

237 238
  float peakfeed[scs->ch];
  float rmsfeed[scs->ch];
239 240 241
  memset(peakfeed,0,sizeof(peakfeed));
  memset(rmsfeed,0,sizeof(rmsfeed));
  
242
  for(i=0;i<iterations;i++){
243

244 245
    int o_active=0,u_active=0,b_active=0;

246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
    int activeC= active[i] && !mute_channel_muted(mutemaskC,i);
    int active0= scs->active0[i];
    int activeP= scs->activeP[i];

    int mutedC=mute_channel_muted(mutemaskC,i);
    int muted0=mute_channel_muted(mutemask0,i);
    int mutedP=mute_channel_muted(mutemaskP,i);

    float o_attackms=scset[i]->o_attack*.1;
    float o_decayms=scset[i]->o_decay*.1;
    float u_attackms=scset[i]->u_attack*.1;
    float u_decayms=scset[i]->u_decay*.1;
    float b_attackms=scset[i]->b_attack*.1;
    float b_decayms=scset[i]->b_decay*.1;

261 262 263 264 265 266
    if(o_attackms!=scs->o_attack[i].ms)filter_set(o_attackms,2,1,&scs->o_attack[i]);
    if(o_decayms!=scs->o_decay[i].ms)filter_set(o_decayms,1,0,&scs->o_decay[i]);
    if(u_attackms!=scs->u_attack[i].ms)filter_set(u_attackms,2,1,&scs->u_attack[i]);
    if(u_decayms!=scs->u_decay[i].ms)filter_set(u_decayms,1,0,&scs->u_decay[i]);
    if(b_attackms!=scs->b_attack[i].ms)filter_set(b_attackms,2,1,&scs->b_attack[i]);
    if(b_decayms!=scs->b_decay[i].ms)filter_set(b_decayms,1,0,&scs->b_decay[i]);
267
    
268
    if(!active0 && !activeC){
269
      
270 271 272
      if(activeP) reset_onech_filter(scs,i); /* just became inactive;
                                                reset all filters for
                                                this channel */
273
      /* feedback */
274
      if(scset[i]->panel_visible){
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
        for(j=i;j<i+chper;j++){
          int k;
          float rms=0.;
          float peak=0.;
          float *x=scs->cache[j];
          have_feedback=1;

          if(!muted0){
            for(k=0;k<input_size;k++){
              float val=x[k]*x[k];
              rms+= val;
              if(peak<val)peak=val;
            }
          }
          peakfeed[j]=todB_a(peak)*.5;
          rms/=input_size;
          rmsfeed[j]=todB_a(rms)*.5;
        }
293
      }
294

295 296
      /* rotate data vectors */
      if(out){
297 298 299 300 301 302
        for(j=i;j<i+chper;j++){
          float *temp=out->data[j];
          out->data[j]=scs->cache[j];
          scs->cache[j]=temp;
        }
      }	
303

304
    }else if(active0 || activeC){
305
      float adj[input_size]; // under will set it
306 307 308 309 310 311 312 313
      atten_cache *prevset=scs->prevset+i;
      atten_cache *currset=scs->currset+i;

      currset->u_thresh=scset[i]->u_thresh;
      currset->o_thresh=scset[i]->o_thresh;
      currset->u_ratio=scset[i]->u_ratio;
      currset->o_ratio=scset[i]->o_ratio;
      currset->b_ratio=scset[i]->b_ratio;
314 315 316
      
      /* don't slew from an unknown value */
      if(!activeP || !scs->fillstate) 
317
	memcpy(prevset,currset,sizeof(*currset));
318
      
319 320
      /* don't run filters that will be applied at unity */
      if(prevset->u_ratio==1000 && currset->u_ratio==1000){
321
	scs->u_delay[i]=1;
322 323 324 325 326 327 328 329 330
	memset(scs->u_peak+i,0,sizeof(peak_state));
	memset(scs->u_iir+i,0,sizeof(iir_state));
      }else{
	if(scs->u_delay[i]-->0)currset->u_ratio=1000;
	if(scs->u_delay[i]<0)scs->u_delay[i]=0;
	u_active=1;
      }

      if(prevset->o_ratio==1000 && currset->o_ratio==1000){
331
	scs->o_delay[i]=1;
332 333 334 335 336 337 338 339 340
	memset(scs->o_peak+i,0,sizeof(peak_state));
	memset(scs->o_iir+i,0,sizeof(iir_state));
      }else{
	if(scs->o_delay[i]-->0)currset->o_ratio=1000;
	if(scs->o_delay[i]<0)scs->o_delay[i]=0;
	o_active=1;
      }

      if(prevset->b_ratio==1000 && currset->b_ratio==1000){
341
	scs->b_delay[i]=1;
342 343 344 345 346 347 348
	memset(scs->b_peak+i,0,sizeof(peak_state));
	memset(scs->b_iir+i,0,sizeof(iir_state));
      }else{
	if(scs->b_delay[i]-->0)currset->b_ratio=1000;
	if(scs->b_delay[i]<0)scs->b_delay[i]=0;
	b_active=1;
      }
349

350
      /* run the filters */
351 352 353
      memset(adj,0,sizeof(*adj)*input_size);

      if(u_active)
354
	bi_compand(scs->cache+i,in->data+i,chper,adj,
355 356 357 358 359 360 361 362 363 364
		   //scs->prevset[i].u_thresh,
		   scs->currset[i].u_thresh,
		   1.f-1000./scs->prevset[i].u_ratio,
		   1.f-1000./scs->currset[i].u_ratio,
		   scset[i]->u_lookahead/1000.f,
		   scset[i]->u_mode,
		   scset[i]->u_softknee,
		   scs->u_attack+i,scs->u_decay+i,
		   scs->u_iir+i,scs->u_peak+i,
		   active0,0);
365
      
366
      if(o_active)
367
	bi_compand(scs->cache+i,in->data+i,chper,adj,
368 369 370 371 372 373 374 375 376 377
		   //scs->prevset[i].o_thresh,
		   scs->currset[i].o_thresh,
		   1.f-1000.f/scs->prevset[i].o_ratio,
		   1.f-1000.f/scs->currset[i].o_ratio,
		   scset[i]->o_lookahead/1000.f,
		   scset[i]->o_mode,
		   scset[i]->o_softknee,
		   scs->o_attack+i,scs->o_decay+i,
		   scs->o_iir+i,scs->o_peak+i,
		   active0,1);
378
      
379

380
      /* feedback before base */
381
      if(scset[i]->panel_visible){
382 383 384 385 386 387 388 389 390 391 392
        for(j=i;j<i+chper;j++){
          int k;
          float rms=0.;
          float peak=0.;
          float *x=scs->cache[j];
          have_feedback=1;

          if(!muted0){
            for(k=0;k<input_size;k++){
              float mul=fromdB_a(adj[k]);
              float val=x[k]*mul;
393
	  
394 395 396 397 398 399 400 401 402 403
              val*=val;
              rms+= val;
              if(peak<val)peak=val;
              
            }
          }
          peakfeed[j]=todB_a(peak)*.5;
          rms/=input_size;
          rmsfeed[j]=todB_a(rms)*.5;
        }
404
      }
405
       
406
      if(b_active)
407
	full_compand(scs->cache+i,in->data+i,chper,adj,
408 409
		     1.-1000./scs->prevset[i].b_ratio,
		     1.-1000./scs->currset[i].b_ratio,
410
		     scset[i]->b_mode,
411 412 413
		     scs->b_attack+i,scs->b_decay+i,
		     scs->b_iir+i,scs->b_peak+i,
		     active0);
414 415

      if(active0 && out){
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
        for(j=i;j<i+chper;j++){
          /* current frame should be manipulated; render into out,
             handle transitioning after */
          int k;
          float *ix=scs->cache[j];
          float *ox=out->data[j];

          for(k=0;k<input_size;k++)
            ox[k]=ix[k]*fromdB_a(adj[k]);

          /* is this frame preceeded/followed by an 'inactive' frame?
             If so, smooth the transition */
          if(!activeP){
            if(!mutedP){
              for(k=0;k<input_size/2;k++){
                float w=window[k];
                ox[k]= ox[k]*w + ix[k]*(1.-w);
              }
            }
          }
          if(!activeC){
            if(!mutedC){
              float *cox=ox+input_size/2;
              float *cix=ix+input_size/2;
              for(k=0;k<input_size/2;k++){
                float w=window[k];
                cox[k]= cox[k]*(1.-w) + cix[k]*w;
              }
            }
          }
        }
447
      }else if(out){
448 449 450 451 452
        for(j=i;j<i+chper;j++){
          float *temp=out->data[j];
          out->data[j]=scs->cache[j];
          scs->cache[j]=temp;
        }
453 454
      }
    }
455

456 457 458 459 460 461 462
    for(j=i;j<i+chper;j++){
      float *temp=scs->cache[j];
      scs->cache[j]=in->data[j];
      in->data[j]=temp;
      scs->activeP[j]=active0;
      scs->active0[j]=activeC;
    }
463
  }
464 465 466

  if(out){
    /* feedback is also triggered off of output */
467
    singlecomp_feedback *ff=
468
      (singlecomp_feedback *)feedback_new(&scs->feedpool,new_singlecomp_feedback);
469 470
    
    if(!ff->peak)
471
      ff->peak=malloc(scs->ch*sizeof(*ff->peak));
472 473
    
    if(!ff->rms)
474
      ff->rms=malloc(scs->ch*sizeof(*ff->rms));
475
    
476 477
    memcpy(ff->peak,peakfeed,sizeof(peakfeed));
    memcpy(ff->rms,rmsfeed,sizeof(rmsfeed));
478 479
    
    feedback_push(&scs->feedpool,(feedback_generic *)ff);
480

481 482
    out->active=mutemask0;
    out->samples=scs->cache_samples;
483
  }
484 485 486 487 488 489

  {
    atten_cache *temp=scs->prevset;
    scs->prevset=scs->currset;
    scs->currset=temp;
  }
490

491 492 493 494 495 496 497 498
  scs->cache_samples=in->samples;
  scs->mutemaskP=mutemask0;
  scs->mutemask0=mutemaskC;
}

time_linkage *singlecomp_read_helper(time_linkage *in,
				     singlecomp_state *scs, 
				     singlecomp_settings **scset,
499 500
				     int *active,
                                     int link){
501 502 503 504 505 506 507 508 509
  int i;

  switch(scs->fillstate){
  case 0: /* prime the cache */
    if(in->samples==0){
      scs->out.samples=0;
      return &scs->out;
    }
    
510
    for(i=0;i<scs->ch;i++){
511 512 513 514 515 516 517 518 519 520 521
      memset(scs->o_iir+i,0,sizeof(*scs->o_iir));
      memset(scs->u_iir+i,0,sizeof(*scs->u_iir));
      memset(scs->b_iir+i,0,sizeof(*scs->b_iir));
      memset(scs->o_peak+i,0,sizeof(*scs->o_peak));
      memset(scs->u_peak+i,0,sizeof(*scs->u_peak));
      memset(scs->b_peak+i,0,sizeof(*scs->b_peak));
      memset(scs->cache[i],0,sizeof(**scs->cache)*input_size);
      scs->activeP[i]=scs->active0[i]=active[i];
    }
    scs->mutemaskP=scs->mutemask0=in->active;
    
522
    work_and_lapping(scs,scset,in,0,active,link);
523 524 525

    scs->fillstate=1;
    scs->out.samples=0;
526
    if(in->samples==input_size)goto tidy_up;
527
    
528 529
    for(i=0;i<scs->ch;i++)
      memset(in->data[i],0,sizeof(**in->data)*input_size);
530 531 532 533
    in->samples=0;
    /* fall through */
  case 1: /* nominal processing */

534
    work_and_lapping(scs,scset,in,&scs->out,active,link);
535

536
    if(scs->out.samples<input_size)scs->fillstate=2;
537 538 539 540 541
    break;
  case 2: /* we've pushed out EOF already */
    scs->out.samples=0;
  }
  
542 543
 tidy_up:
  {
544
    int tozero=input_size-scs->out.samples;
545
    if(tozero)
546 547
      for(i=0;i<scs->out.channels;i++)
        memset(scs->out.data[i]+scs->out.samples,0,sizeof(**scs->out.data)*tozero);
548 549
  }

550
  return &scs->out;
551 552
}

553
time_linkage *singlecomp_read_master(time_linkage *in){
554
  int active[master_state.ch],i;
555 556

  /* local copy required to avoid concurrency problems */
557
  for(i=0;i<master_state.ch;i++)
558 559
    active[i]=singlecomp_master_set.panel_active;

560
  return singlecomp_read_helper(in, &master_state, master_set_bundle,active,1);
561 562 563
}

time_linkage *singlecomp_read_channel(time_linkage *in){
564
  int active[channel_state.ch],i;
565 566

  /* local copy required to avoid concurrency problems */
567
  for(i=0;i<channel_state.ch;i++)
568
    active[i]=singlecomp_channel_set[i].panel_active;
569

570
  return singlecomp_read_helper(in, &channel_state, channel_set_bundle,active,0);
571
}