equalizer.cpp 3.84 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
/*****************************************************************************
 * equalizer.cpp
 *****************************************************************************
 * Copyright (C) 2003 the VideoLAN team
 * $Id$
 *
 * Authors: Cyril Deguet     <asmax@via.ecp.fr>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
 *****************************************************************************/


#include "vlc/aout.h"
#include "aout_internal.h"
#include "equalizer.hpp"
#include "../utils/var_percent.hpp"
#include <ios>
#include <iomanip>
#include <sstream>


EqualizerBands::EqualizerBands( intf_thread_t *pIntf ): SkinObject( pIntf ),
    m_isUpdating( false )
{
    for( int i = 0; i < kNbBands; i++ )
    {
        // Create and observe the band variables
        VarPercent *pVar = new VarPercent( pIntf );
        m_cBands[i] = VariablePtr( pVar );
        pVar->set( 0.5f );
        pVar->addObserver( this );
    }
}


EqualizerBands::~EqualizerBands()
{
    for( int i = 0; i < kNbBands; i++ )
    {
        ((VarPercent*)m_cBands[i].get())->delObserver( this );
    }
}


void EqualizerBands::set( string bands )
{
    float val;
    stringstream ss( bands );

    m_isUpdating = true;
    // Parse the string
    for( int i = 0; i < kNbBands; i++ )
    {
        ss >> val;
        // Set the band value in percent
        ((VarPercent*)m_cBands[i].get())->set( (val + 20) / 40 );
    }
    m_isUpdating = false;
}


VariablePtr EqualizerBands::getBand( int band )
{
    return m_cBands[band];
}


void EqualizerBands::onUpdate( Subject<VarPercent> &rBand )
{
    // Make sure we are not called from set()
    if (!m_isUpdating)
    {
        float val;
        stringstream ss;
        // Write one digit after the floating point
        ss << setprecision( 1 ) << setiosflags( ios::fixed );

        // Convert the band values to a string
        val = 40 * ((VarPercent*)m_cBands[0].get())->get() - 20;
        ss << val;
        for( int i = 1; i < kNbBands; i++ )
        {
            val = 40 * ((VarPercent*)m_cBands[i].get())->get() - 20;
            ss << " " << val;
        }

99
        aout_instance_t *pAout = (aout_instance_t *)vlc_object_find( getIntf(),
100
                VLC_OBJECT_AOUT, FIND_ANYWHERE );
101 102
        char *bands = (char*)ss.str().c_str();
        config_PutPsz( getIntf(), "equalizer-bands", bands );
103 104 105
        if( pAout )
        {
            // Update the audio output
106
            var_SetString( pAout, "equalizer-bands", bands );
107 108 109 110 111
            vlc_object_release( pAout );
        }
    }
}

112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139

EqualizerPreamp::EqualizerPreamp( intf_thread_t *pIntf ): VarPercent( pIntf )
{
    // Initial value
    VarPercent::set( 0.8 );
}


void EqualizerPreamp::set( float percentage, bool updateVLC )
{
    VarPercent::set( percentage );

    // Avoid infinite loop
    if( updateVLC )
    {
        float val = 40 * percentage - 20;

        aout_instance_t *pAout = (aout_instance_t *)vlc_object_find( getIntf(),
                                VLC_OBJECT_AOUT, FIND_ANYWHERE );
        config_PutFloat( getIntf(), "equalizer-preamp", val );
        if( pAout )
        {
            // Update the audio output
            var_SetFloat( pAout, "equalizer-preamp", val );
            vlc_object_release( pAout );
        }
    }
}