eq.c 2.35 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
/*
 *
 *  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.
 *
 * 
 */

Monty Montgomery's avatar
 
Monty Montgomery committed
24
#include <sys/types.h>
Monty Montgomery's avatar
 
Monty Montgomery committed
25 26 27 28 29 30 31 32
#include "postfish.h"
#include "feedback.h"
#include "freq.h"
#include "eq.h"

extern int input_size;

sig_atomic_t eq_active;
Monty Montgomery's avatar
 
Monty Montgomery committed
33
sig_atomic_t eq_visible;
Monty Montgomery's avatar
 
Monty Montgomery committed
34

Monty Montgomery's avatar
 
Monty Montgomery committed
35 36
sig_atomic_t curve_dirty=1;

Monty Montgomery's avatar
 
Monty Montgomery committed
37 38
freq_state eq;

Monty Montgomery's avatar
 
Monty Montgomery committed
39 40
/* accessed only in playback thread/setup */
int pull_eq_feedback(float **peak,float **rms){
Monty Montgomery's avatar
 
Monty Montgomery committed
41 42 43 44 45
  return pull_freq_feedback(&eq,peak,rms);
}

/* called only by initial setup */
int eq_load(void){
Monty Montgomery's avatar
 
Monty Montgomery committed
46
  return freq_load(&eq,eq_freq_list,eq_freqs);
Monty Montgomery's avatar
 
Monty Montgomery committed
47 48 49 50 51 52 53
}

/* called only in playback thread */
int eq_reset(){
  return freq_reset(&eq);
}

Monty Montgomery's avatar
 
Monty Montgomery committed
54
static sig_atomic_t settings[eq_freqs];
Monty Montgomery's avatar
 
Monty Montgomery committed
55

Monty Montgomery's avatar
 
Monty Montgomery committed
56
void eq_set(int freq, float value){
Monty Montgomery's avatar
 
Monty Montgomery committed
57
  settings[freq]=rint(value*10.);
Monty Montgomery's avatar
 
Monty Montgomery committed
58
  curve_dirty=1;
Monty Montgomery's avatar
 
Monty Montgomery committed
59
}
Monty Montgomery's avatar
 
Monty Montgomery committed
60 61 62 63
 
static float *curve_cache=0;
 
static void workfunc(freq_state *f,float **data,float **peak, float **rms){
64
  int h,i,j;
Monty Montgomery's avatar
 
Monty Montgomery committed
65
  float sq_mags[f->qblocksize*2+1];
Monty Montgomery's avatar
 
Monty Montgomery committed
66
  
Monty Montgomery's avatar
 
Monty Montgomery committed
67 68
  if(curve_dirty || !curve_cache){
    curve_dirty=0;
Monty Montgomery's avatar
 
Monty Montgomery committed
69
    
Monty Montgomery's avatar
 
Monty Montgomery committed
70 71 72 73 74 75 76
    if(!curve_cache)curve_cache=malloc((f->qblocksize*2+1)*sizeof(*curve_cache));
    memset(curve_cache,0,(f->qblocksize*2+1)*sizeof(*curve_cache));
    
    for(i=0;i<eq_freqs;i++){
      float set=fromdB(settings[i]*.1);
      for(j=0;j<f->qblocksize*2+1;j++)
	curve_cache[j]+=f->ho_window[i][j]*set;
Monty Montgomery's avatar
 
Monty Montgomery committed
77
    }
Monty Montgomery's avatar
 
Monty Montgomery committed
78 79 80
  }
  
  for(h=0;h<input_ch;h++){
Monty Montgomery's avatar
 
Monty Montgomery committed
81
    
Monty Montgomery's avatar
 
Monty Montgomery committed
82 83 84 85 86 87
    if(eq_active){
      
      for(i=0;i<f->qblocksize*2+1;i++){
	data[h][i*2]*=curve_cache[i];
	data[h][i*2+1]*=curve_cache[i];
      }
Monty Montgomery's avatar
 
Monty Montgomery committed
88
    }
Monty Montgomery's avatar
 
Monty Montgomery committed
89 90
    
    freq_metric_work(data[h],f,sq_mags,peak[h],rms[h]);
Monty Montgomery's avatar
 
Monty Montgomery committed
91 92
  }

Monty Montgomery's avatar
 
Monty Montgomery committed
93 94 95 96 97
  return;
}

/* called only by playback thread */
time_linkage *eq_read(time_linkage *in){
Monty Montgomery's avatar
 
Monty Montgomery committed
98
  return freq_read(in,&eq,workfunc,!(eq_visible||eq_active));
Monty Montgomery's avatar
 
Monty Montgomery committed
99
}