From 552f55f00aab7f0bd5c2d34cbd01acf6503e24cd Mon Sep 17 00:00:00 2001
From: smorlat <smorlat@3f6dc0c8-ddfe-455d-9043-3cd528dc4637>
Date: Fri, 24 Jul 2009 13:47:01 +0000
Subject: [PATCH] fix equalizer un-precision, and allow band width setting per
 frequency.

git-svn-id: svn+ssh://svn.savannah.nongnu.org/linphone/trunk@549 3f6dc0c8-ddfe-455d-9043-3cd528dc4637
---
 linphone/coreapi/linphonecore.c               |  6 ++---
 .../include/mediastreamer2/msequalizer.h      |  7 +----
 linphone/mediastreamer2/src/equalizer.c       | 27 +++++++++----------
 3 files changed, 16 insertions(+), 24 deletions(-)

diff --git a/linphone/coreapi/linphonecore.c b/linphone/coreapi/linphonecore.c
index 333a12a34c..96994ed2b5 100644
--- a/linphone/coreapi/linphonecore.c
+++ b/linphone/coreapi/linphonecore.c
@@ -1480,17 +1480,15 @@ static void parametrize_equalizer(LinphoneCore *lc, AudioStream *st){
 	if (st->equalizer){
 		MSFilter *f=st->equalizer;
 		int enabled=lp_config_get_int(lc->config,"sound","eq_active",0);
-		float width=lp_config_get_float(lc->config,"sound","eq_width_factor",-1);
 		const char *gains=lp_config_get_string(lc->config,"sound","eq_gains",NULL);
 		ms_filter_call_method(f,MS_EQUALIZER_SET_ACTIVE,&enabled);
 		if (enabled){
-			if (width!=-1) ms_filter_call_method(f,MS_EQUALIZER_SET_ACTIVE,&width);
 			if (gains){
 				do{
 					int bytes;
 					MSEqualizerGain g;
-					if (sscanf(gains,"%f:%f %n",&g.frequency,&g.gain,&bytes)==2){
-						ms_message("Read equalizer gains: %f --> %f",g.frequency,g.gain);
+					if (sscanf(gains,"%f:%f:%f %n",&g.frequency,&g.gain,&g.width,&bytes)==2){
+						ms_message("Read equalizer gains: %f(~%f) --> %f",g.frequency,g.width,g.gain);
 						ms_filter_call_method(f,MS_EQUALIZER_SET_GAIN,&g);
 						gains+=bytes;
 					}else break;
diff --git a/linphone/mediastreamer2/include/mediastreamer2/msequalizer.h b/linphone/mediastreamer2/include/mediastreamer2/msequalizer.h
index 1ebcae4379..4dbdfc8be9 100644
--- a/linphone/mediastreamer2/include/mediastreamer2/msequalizer.h
+++ b/linphone/mediastreamer2/include/mediastreamer2/msequalizer.h
@@ -25,17 +25,12 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 typedef struct _MSEqualizerGain{
 	float frequency; ///< In hz
 	float gain; ///< between 0-1.2
+	float width; ///< frequency band width around mid frequency for which the gain is applied, in Hz. Use 0 for the lowest frequency resolution.
 }MSEqualizerGain;
 
 #define MS_EQUALIZER_SET_GAIN	MS_FILTER_METHOD(MS_EQUALIZER_ID,0,MSEqualizerGain)
 #define MS_EQUALIZER_GET_GAIN	MS_FILTER_METHOD(MS_EQUALIZER_ID,1,MSEqualizerGain)
 #define MS_EQUALIZER_SET_ACTIVE	MS_FILTER_METHOD(MS_EQUALIZER_ID,2,int)
-/**
- * Sets the coeficient for computing how large is the frequency band around a gain setting.
- * For example, setting this value to 0.4 will result in a frequency range of 800-1200 when setting
- * a gain for frequency 1000 Hz.
-**/
-#define MS_EQUALIZER_SET_FREQ_WIDTH_COEF MS_FILTER_METHOD(MS_EQUALIZER_ID,3,float)
 
 #endif
 
diff --git a/linphone/mediastreamer2/src/equalizer.c b/linphone/mediastreamer2/src/equalizer.c
index 1c06f1d0f8..1fe5ff958f 100644
--- a/linphone/mediastreamer2/src/equalizer.c
+++ b/linphone/mediastreamer2/src/equalizer.c
@@ -76,6 +76,7 @@ static void equalizer_state_destroy(EqualizerState *s){
 
 static int equalizer_state_hz_to_index(EqualizerState *s, int hz){
 	int ret;
+	int bandres=s->rate/s->nfft;
 	if (hz<0){
 		ms_error("Bad frequency value %i",hz);
 		return -1;
@@ -84,7 +85,7 @@ static int equalizer_state_hz_to_index(EqualizerState *s, int hz){
 		hz=(s->rate/2);
 	}
 	/*round to nearest integer*/
-	ret=((hz*s->nfft)+(hz/2))/s->rate;
+	ret=((hz+(bandres/2))*s->nfft)/s->rate;
 	if (ret==s->nfft/2) ret=(s->nfft/2)-1;
 	return ret;
 }
@@ -106,19 +107,19 @@ static float equalizer_state_get(EqualizerState *s, int freqhz){
 }
 
 /* return the frequency band width we want to control around hz*/
-static void equalizer_state_get_band(EqualizerState *s, int hz, int *low_index, int *high_index){
-	int half_band=(int)((float)hz*s->width_coef*0.5);
+static void equalizer_state_get_band(EqualizerState *s, int hz, int freq_width,int *low_index, int *high_index){
+	int half_band=freq_width/2;
 	*low_index=equalizer_state_hz_to_index(s,hz-half_band);
 	*high_index=equalizer_state_hz_to_index(s,hz+half_band);
 }
 
-static void equalizer_state_set(EqualizerState *s, int freqhz, float gain){
+static void equalizer_state_set(EqualizerState *s, int freqhz, float gain, int freq_width){
 	int low,high;
 	int i;
-	equalizer_state_get_band(s,freqhz,&low,&high);
+	equalizer_state_get_band(s,freqhz,freq_width,&low,&high);
 	for(i=low;i<=high;++i){
 		ms_message("Setting gain %f for freq_index %i (freqhz=%i)",gain,i,freqhz);
-		s->fft_cpx[1+(i*2)]=gain_int16(gain)/s->nfft;
+		s->fft_cpx[1+((i-1)*2)]=gain_int16(gain)/s->nfft;
 	}
 	s->needs_update=TRUE;
 }
@@ -169,11 +170,15 @@ static void equalizer_state_compute_impulse_response(EqualizerState *s){
 	dump_table(s->fft_cpx,s->nfft);
 	ms_ifft(fft_handle,s->fft_cpx,s->fir);
 	ms_fft_destroy(fft_handle);
+	/*
 	ms_message("Inverse fft result:");
 	dump_table(s->fir,s->fir_len);
+	*/
 	time_shift(s->fir,s->fir_len);
+	/*
 	ms_message("Time shifted:");
 	dump_table(s->fir,s->fir_len);
+	*/
 	norm_and_apodize(s->fir,s->fir_len);
 	ms_message("Apodized impulse response:");
 	dump_table(s->fir,s->fir_len);
@@ -236,7 +241,7 @@ void equalizer_process(MSFilter *f){
 int equalizer_set_gain(MSFilter *f, void *data){
 	EqualizerState *s=(EqualizerState*)f->data;
 	MSEqualizerGain *d=(MSEqualizerGain*)data;
-	equalizer_state_set(s,d->frequency,d->gain);
+	equalizer_state_set(s,d->frequency,d->gain,d->width);
 	return 0;
 }
 
@@ -244,6 +249,7 @@ int equalizer_get_gain(MSFilter *f, void *data){
 	EqualizerState *s=(EqualizerState*)f->data;
 	MSEqualizerGain *d=(MSEqualizerGain*)data;
 	d->gain=equalizer_state_get(s,d->frequency);
+	d->width=0;
 	return 0;
 }
 
@@ -259,18 +265,11 @@ int equalizer_set_active(MSFilter *f, void *data){
 	return 0;
 }
 
-int equalizer_set_freq_width_coef(MSFilter *f, void *data){
-	EqualizerState *s=(EqualizerState*)f->data;
-	s->width_coef=*(float*)data;
-	return 0;
-}
-
 static MSFilterMethod equalizer_methods[]={
 	{	MS_EQUALIZER_SET_GAIN		,	equalizer_set_gain	},
 	{	MS_EQUALIZER_GET_GAIN		,	equalizer_get_gain	},
 	{	MS_EQUALIZER_SET_ACTIVE		,	equalizer_set_active	},
 	{	MS_FILTER_SET_SAMPLE_RATE	,	equalizer_set_rate	},
-	{	MS_EQUALIZER_SET_FREQ_WIDTH_COEF,	equalizer_set_freq_width_coef},
 	{	0				,	NULL			}
 };
 
-- 
GitLab