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