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