1 /*****************************************************************************
2 * equalizer.cpp : Equalizer
3 ****************************************************************************
4 * Copyright (C) 2006 the VideoLAN team
5 * $Id: preferences.cpp 16643 2006-09-13 12:45:46Z zorglub $
7 * Authors: Clément Stenac <zorglub@videolan.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
28 #include <QGridLayout>
30 #include "components/equalizer.hpp"
33 #include "../../audio_filter/equalizer_presets.h"
34 #include <audio_output.h>
35 #include <aout_internal.h>
36 #include <vlc_intf_strings.h>
39 static const QString band_frequencies[] =
41 " 60Hz ", " 170 Hz " , " 310 Hz ", " 600 Hz ", " 1 kHz ",
42 " 3 kHz " , " 6 kHz ", " 12 kHz ", " 14 kHz ", " 16 kHz "
45 Equalizer::Equalizer( intf_thread_t *_p_intf, QWidget *_parent ) :
46 QWidget( _parent ) , p_intf( _p_intf )
48 QFont smallFont = QApplication::font(0);
49 smallFont.setPointSize( smallFont.pointSize() - 3 );
53 ui.preampLabel->setFont( smallFont );
54 ui.preampSlider->setMaximum( 400 );
55 for( int i = 0 ; i < NB_PRESETS ; i ++ )
57 ui.presetsCombo->addItem( qfu( preset_list_text[i] ),
60 CONNECT( ui.presetsCombo, activated( int ), this, setPreset( int ) );
62 BUTTONACT( ui.enableCheck, enable() );
63 BUTTONACT( ui.eq2PassCheck, set2Pass() );
65 CONNECT( ui.preampSlider, valueChanged(int), this, setPreamp() );
67 QGridLayout *grid = new QGridLayout( ui.frame );
69 for( int i = 0 ; i < BANDS ; i++ )
71 bands[i] = new QSlider( Qt::Vertical );
72 bands[i]->setMaximum( 400 );
73 bands[i]->setValue( 200 );
74 CONNECT( bands[i], valueChanged(int), this, setBand() );
75 band_texts[i] = new QLabel( band_frequencies[i] + "\n0.0dB" );
76 band_texts[i]->setFont( smallFont );
77 grid->addWidget( bands[i], 0, i );
78 grid->addWidget( band_texts[i], 1, i );
81 /* Write down initial values */
82 aout_instance_t *p_aout = (aout_instance_t *)vlc_object_find(p_intf,
83 VLC_OBJECT_AOUT, FIND_ANYWHERE);
89 psz_af = var_GetString( p_aout, "audio-filter" );
90 if( var_GetBool( p_aout, "equalizer-2pass" ) )
91 ui.eq2PassCheck->setChecked( true );
92 psz_bands = var_GetString( p_aout, "equalizer-bands" );
93 f_preamp = var_GetFloat( p_aout, "equalizer-preamp" );
94 vlc_object_release( p_aout );
98 psz_af = config_GetPsz( p_intf, "audio-filter" );
99 if( config_GetInt( p_intf, "equalizer-2pass" ) )
100 ui.eq2PassCheck->setChecked( true );
101 psz_bands = config_GetPsz( p_intf, "equalizer-bands" );
102 f_preamp = config_GetFloat( p_intf, "equalizer-preamp" );
104 if( psz_af && strstr( psz_af, "equalizer" ) != NULL )
105 ui.enableCheck->setChecked( true );
106 enable( ui.enableCheck->isChecked() );
108 setValues( psz_bands, f_preamp );
111 Equalizer::~Equalizer()
115 void Equalizer::enable()
117 bool en = ui.enableCheck->isChecked();
118 aout_EnableFilter( VLC_OBJECT( p_intf ), "equalizer",
119 en ? VLC_TRUE : VLC_FALSE );
123 void Equalizer::enable( bool en )
125 ui.eq2PassCheck->setEnabled( en );
126 ui.preampLabel->setEnabled( en );
127 ui.preampSlider->setEnabled( en );
128 for( int i = 0 ; i< BANDS; i++ )
130 bands[i]->setEnabled( en ); band_texts[i]->setEnabled( en );
134 void Equalizer::set2Pass()
136 aout_instance_t *p_aout= (aout_instance_t *)vlc_object_find(p_intf,
137 VLC_OBJECT_AOUT, FIND_ANYWHERE);
138 vlc_bool_t b_2p = ui.eq2PassCheck->isChecked();
141 config_PutInt( p_intf, "equalizer-2pass", b_2p );
144 var_SetBool( p_aout, "equalizer-2pass", b_2p );
145 config_PutInt( p_intf, "equalizer-2pass", b_2p );
146 for( int i = 0; i < p_aout->i_nb_inputs; i++ )
148 p_aout->pp_inputs[i]->b_restart = VLC_TRUE;
150 vlc_object_release( p_aout );
154 void Equalizer::setPreamp()
156 float f= (float)( ui.preampSlider->value() ) /10 - 20;
158 aout_instance_t *p_aout= (aout_instance_t *)vlc_object_find(p_intf,
159 VLC_OBJECT_AOUT, FIND_ANYWHERE);
161 sprintf( psz_val, "%.1f", f );
162 ui.preampLabel->setText( qtr("Preamp\n") + psz_val + qtr("dB") );
165 delCallbacks( p_aout );
166 var_SetFloat( p_aout, "equalizer-preamp", f );
167 addCallbacks( p_aout );
168 vlc_object_release( p_aout );
170 config_PutFloat( p_intf, "equalizer-preamp", f );
173 void Equalizer::setBand()
175 char psz_values[102]; memset( psz_values, 0, 102 );
177 /**\todo smoothing */
179 for( int i = 0 ; i< BANDS ; i++ )
182 float f_val = (float)( bands[i]->value() ) / 10 - 20 ;
183 sprintf( psz_values, "%s %f", psz_values, f_val );
184 sprintf( psz_val, "% 5.1f", f_val );
185 band_texts[i]->setText( band_frequencies[i] + "\n" + psz_val + "dB" );
187 aout_instance_t *p_aout= (aout_instance_t *)vlc_object_find(p_intf,
188 VLC_OBJECT_AOUT, FIND_ANYWHERE);
191 delCallbacks( p_aout );
192 var_SetString( p_aout, "equalizer-bands", psz_values );
193 addCallbacks( p_aout );
194 vlc_object_release( p_aout );
197 void Equalizer::setValues( char *psz_bands, float f_preamp )
202 for( int i = 0; i < 10; i++ )
205 float f = strtof( p, &p );
206 int i_val= (int)( ( f + 20 ) * 10 );
207 bands[i]->setValue( i_val );
208 sprintf( psz_val, "% 5.1f", f );
209 band_texts[i]->setText( band_frequencies[i] + "\n" + psz_val +
211 if( p == NULL ) break;
217 int i_val = (int)( ( f_preamp + 20 ) * 10 );
218 sprintf( psz_val, "%.1f", f_preamp );
219 ui.preampSlider->setValue( i_val );
220 ui.preampLabel->setText( qtr("Preamp\n") + psz_val + qtr("dB") );
223 void Equalizer::setPreset( int preset )
225 aout_instance_t *p_aout= (aout_instance_t *)vlc_object_find(p_intf,
226 VLC_OBJECT_AOUT, FIND_ANYWHERE);
228 char psz_values[102]; memset( psz_values, 0, 102 );
229 for( int i = 0 ; i< 10 ;i++ )
230 sprintf( psz_values, "%s %.1f", psz_values,
231 eqz_preset_10b[preset]->f_amp[i] );
235 delCallbacks( p_aout );
236 var_SetString( p_aout, "equalizer-bands", psz_values );
237 var_SetFloat( p_aout, "equalizer-preamp",
238 eqz_preset_10b[preset]->f_preamp );
239 addCallbacks( p_aout );
240 vlc_object_release( p_aout );
242 config_PutPsz( p_intf, "equalizer-bands", psz_values );
243 config_PutFloat( p_intf, "equalizer-preamp",
244 eqz_preset_10b[preset]->f_preamp );
246 setValues( psz_values, eqz_preset_10b[preset]->f_preamp );
249 void Equalizer::delCallbacks( aout_instance_t *p_aout )
251 // var_DelCallback( p_aout, "equalizer-bands", EqzCallback, this );
252 // var_DelCallback( p_aout, "equalizer-preamp", EqzCallback, this );
255 void Equalizer::addCallbacks( aout_instance_t *p_aout )
257 // var_AddCallback( p_aout, "equalizer-bands", EqzCallback, this );
258 // var_AddCallback( p_aout, "equalizer-preamp", EqzCallback, this );