+/**********************************************************************
+ * Dynamic range compressor
+ **********************************************************************/
+
+typedef struct
+{
+ const char *psz_name;
+ const char *psz_descs;
+ const char *psz_units;
+ const float f_min; // min
+ const float f_max; // max
+ const float f_value; // value
+ const float f_resolution; // resolution
+} comp_controls_t;
+
+static const comp_controls_t comp_controls[] =
+{
+ { "compressor-rms-peak", _("RMS/peak"), "", 0.0f, 1.0f, 0.00f, 0.001f },
+ { "compressor-attack", _("Attack"), _(" ms"), 1.5f, 400.0f, 25.00f, 0.100f },
+ { "compressor-release", _("Release"), _(" ms"), 2.0f, 800.0f, 100.00f, 0.100f },
+ { "compressor-threshold", _("Threshold"), _(" dB"), -30.0f, 0.0f, -11.00f, 0.010f },
+ { "compressor-ratio", _("Ratio"), ":1", 1.0f, 20.0f, 8.00f, 0.010f },
+ { "compressor-knee", _("Knee\nradius"), _(" dB"), 1.0f, 10.0f, 2.50f, 0.010f },
+ { "compressor-makeup-gain", _("Makeup\ngain"), _(" dB"), 0.0f, 24.0f, 7.00f, 0.010f },
+};
+
+Compressor::Compressor( intf_thread_t *_p_intf, QWidget *_parent )
+ : QWidget( _parent ) , p_intf( _p_intf )
+{
+ QFont smallFont = QApplication::font();
+ smallFont.setPointSize( smallFont.pointSize() - 3 );
+
+ QGridLayout *layout = new QGridLayout( this );
+ layout->setMargin( 0 );
+
+ enableCheck = new QCheckBox( qtr( "Enable dynamic range compressor" ) );
+ layout->addWidget( enableCheck, 0, 0, 1, NUM_CP_CTRL );
+
+ for( int i = 0 ; i < NUM_CP_CTRL ; i++ )
+ {
+ const int i_min = (int)( comp_controls[i].f_min
+ / comp_controls[i].f_resolution );
+ const int i_max = (int)( comp_controls[i].f_max
+ / comp_controls[i].f_resolution );
+ const int i_val = (int)( comp_controls[i].f_value
+ / comp_controls[i].f_resolution );
+
+ compCtrl[i] = new QSlider( Qt::Vertical );
+ compCtrl[i]->setMinimum( i_min );
+ compCtrl[i]->setMaximum( i_max );
+ compCtrl[i]->setValue( i_val );
+
+ oldControlVars[i] = comp_controls[i].f_value;
+
+ CONNECT( compCtrl[i], valueChanged( int ), this, setInitValues() );
+
+ ctrl_texts[i] = new QLabel( qtr( comp_controls[i].psz_descs ) + "\n" );
+ ctrl_texts[i]->setFont( smallFont );
+ ctrl_texts[i]->setAlignment( Qt::AlignHCenter );
+
+ ctrl_readout[i] = new QLabel;
+ ctrl_readout[i]->setFont( smallFont );
+ ctrl_readout[i]->setAlignment( Qt::AlignHCenter );
+
+ layout->addWidget( compCtrl[i], 1, i, Qt::AlignHCenter );
+ layout->addWidget( ctrl_readout[i], 2, i, Qt::AlignHCenter );
+ layout->addWidget( ctrl_texts[i], 3, i, Qt::AlignHCenter );
+ }
+
+ BUTTONACT( enableCheck, enable() );
+
+ /* Write down initial values */
+ aout_instance_t *p_aout = THEMIM->getAout();
+ char *psz_af;
+
+ if( p_aout )
+ {
+ psz_af = var_GetNonEmptyString( p_aout, "audio-filter" );
+ for( int i = 0; i < NUM_CP_CTRL; i++ )
+ {
+ controlVars[i] = var_GetFloat( p_aout,
+ comp_controls[i].psz_name );
+ }
+ vlc_object_release( p_aout );
+ }
+ else
+ {
+ psz_af = config_GetPsz( p_intf, "audio-filter" );
+ for( int i = 0; i < NUM_CP_CTRL; i++ )
+ {
+ controlVars[i] = config_GetFloat( p_intf,
+ comp_controls[i].psz_name );
+ }
+ }
+ if( psz_af && strstr( psz_af, "compressor" ) != NULL )
+ {
+ enableCheck->setChecked( true );
+ }
+ free( psz_af );
+ enable( enableCheck->isChecked() );
+ updateSliders( controlVars );
+ setValues( controlVars );
+}
+
+void Compressor::enable()
+{
+ bool en = enableCheck->isChecked();
+ aout_EnableFilter( THEPL, "compressor", en );
+ enable( en );
+}
+
+void Compressor::enable( bool en )
+{
+ for( int i = 0 ; i < NUM_CP_CTRL ; i++ )
+ {
+ compCtrl[i]->setEnabled( en );
+ ctrl_texts[i]->setEnabled( en );
+ ctrl_readout[i]->setEnabled( en );
+ }
+}
+
+void Compressor::updateSliders( float * controlVars )
+{
+ for( int i = 0 ; i < NUM_CP_CTRL ; i++ )
+ {
+ if( oldControlVars[i] != controlVars[i] )
+ {
+ compCtrl[i]->setValue(
+ (int)( controlVars[i] / comp_controls[i].f_resolution ) );
+ }
+ }
+}
+
+void Compressor::setInitValues()
+{
+ setValues( controlVars );
+}
+
+void Compressor::setValues( float * controlVars )
+{
+ aout_instance_t *p_aout = THEMIM->getAout();
+
+ for( int i = 0 ; i < NUM_CP_CTRL ; i++ )
+ {
+ float f = (float)( compCtrl[i]->value() ) * ( comp_controls[i].f_resolution );
+ ctrl_readout[i]->setText( QString::number( f, 'f', 1 )
+ + qtr( comp_controls[i].psz_units ) );
+ if( oldControlVars[i] != f )
+ {
+ if( p_aout )
+ {
+ var_SetFloat( p_aout, comp_controls[i].psz_name, f );
+ }
+ config_PutFloat( p_intf, comp_controls[i].psz_name, f );
+ oldControlVars[i] = f;
+ }
+ }
+ if( p_aout )
+ {
+ vlc_object_release( p_aout );
+ }
+}
+