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