]> git.sesse.net Git - vlc/blob - modules/gui/skins2/vars/equalizer.cpp
Qt4 - Typo correction.
[vlc] / modules / gui / skins2 / vars / equalizer.cpp
1 /*****************************************************************************
2  * equalizer.cpp
3  *****************************************************************************
4  * Copyright (C) 2003 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Cyril Deguet     <asmax@via.ecp.fr>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 #include <vlc/vlc.h>
25 #include <vlc_aout.h>
26 #include "equalizer.hpp"
27 #include "../utils/var_percent.hpp"
28 #include <ios>
29 #include <iomanip>
30 #include <sstream>
31
32 EqualizerBands::EqualizerBands( intf_thread_t *pIntf ): SkinObject( pIntf ),
33     m_isUpdating( false )
34 {
35     for( int i = 0; i < kNbBands; i++ )
36     {
37         // Create and observe the band variables
38         VarPercent *pVar = new VarPercent( pIntf );
39         m_cBands[i] = VariablePtr( pVar );
40         pVar->set( 0.5f );
41         pVar->addObserver( this );
42     }
43 }
44
45
46 EqualizerBands::~EqualizerBands()
47 {
48     for( int i = 0; i < kNbBands; i++ )
49     {
50         ((VarPercent*)m_cBands[i].get())->delObserver( this );
51     }
52 }
53
54
55 void EqualizerBands::set( string bands )
56 {
57     float val;
58     stringstream ss( bands );
59
60     m_isUpdating = true;
61     // Parse the string
62     for( int i = 0; i < kNbBands; i++ )
63     {
64         ss >> val;
65         // Set the band value in percent
66         ((VarPercent*)m_cBands[i].get())->set( (val + 20) / 40 );
67     }
68     m_isUpdating = false;
69 }
70
71
72 VariablePtr EqualizerBands::getBand( int band )
73 {
74     return m_cBands[band];
75 }
76
77
78 void EqualizerBands::onUpdate( Subject<VarPercent> &rBand, void *arg )
79 {
80     // Make sure we are not called from set()
81     if (!m_isUpdating)
82     {
83         float val;
84         stringstream ss;
85         // Write one digit after the floating point
86         ss << setprecision( 1 ) << setiosflags( ios::fixed );
87
88         // Convert the band values to a string
89         val = 40 * ((VarPercent*)m_cBands[0].get())->get() - 20;
90         ss << val;
91         for( int i = 1; i < kNbBands; i++ )
92         {
93             val = 40 * ((VarPercent*)m_cBands[i].get())->get() - 20;
94             ss << " " << val;
95         }
96
97
98         string bands = ss.str();
99         aout_instance_t *pAout = (aout_instance_t *)vlc_object_find( getIntf(),
100                 VLC_OBJECT_AOUT, FIND_ANYWHERE );
101         config_PutPsz( getIntf(), "equalizer-bands", bands.c_str() );
102         if( pAout )
103         {
104             // Update the audio output
105             var_SetString( pAout, "equalizer-bands", (char*)bands.c_str() );
106             vlc_object_release( pAout );
107         }
108     }
109 }
110
111
112 EqualizerPreamp::EqualizerPreamp( intf_thread_t *pIntf ): VarPercent( pIntf )
113 {
114     // Initial value
115     VarPercent::set( 0.8 );
116 }
117
118
119 void EqualizerPreamp::set( float percentage, bool updateVLC )
120 {
121     VarPercent::set( percentage );
122
123     // Avoid infinite loop
124     if( updateVLC )
125     {
126         float val = 40 * percentage - 20;
127
128         aout_instance_t *pAout = (aout_instance_t *)vlc_object_find( getIntf(),
129                                 VLC_OBJECT_AOUT, FIND_ANYWHERE );
130         config_PutFloat( getIntf(), "equalizer-preamp", val );
131         if( pAout )
132         {
133             // Update the audio output
134             var_SetFloat( pAout, "equalizer-preamp", val );
135             vlc_object_release( pAout );
136         }
137     }
138 }