File indexing completed on 2024-05-05 04:48:31

0001 /****************************************************************************************
0002  * Copyright (c) 2009 Artur Szymiec <artur.szymiec@gmail.com>                           *
0003  * Copyright (c) 2011 Kevin Funk <krf@electrostorm.net>                                 *
0004  *                                                                                      *
0005  * This program is free software; you can redistribute it and/or modify it under        *
0006  * the terms of the GNU General Public License as published by the Free Software        *
0007  * Foundation; either version 2 of the License, or (at your option) any later           *
0008  * version.                                                                             *
0009  *                                                                                      *
0010  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
0011  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
0012  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
0013  *                                                                                      *
0014  * You should have received a copy of the GNU General Public License along with         *
0015  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
0016  ****************************************************************************************/
0017 
0018 #include "EqualizerPresets.h"
0019 
0020 #include "amarokconfig.h"
0021 #include "core/support/Debug.h"
0022 
0023 #include <KLocalizedString>
0024 
0025 #define NUM_EQ_VALUES 11
0026 
0027 static int DEFAULT_PRESET_VALUES[][NUM_EQ_VALUES] =
0028 {
0029     {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, // Manual
0030     {0, 0, 0, 0, 0, 0, 0, -40, -40, -40, -50}, // Classical
0031     {0, 0, 0, 20, 30, 30, 30, 20, 0, 0, 0}, // Club
0032     {-10, 50, 35, 10, 0, 0, -30, -40, -40, 0, 0}, // Dance
0033     {0, 29, 40, 23, 15, 0, 0, 0, 0, 0, 0}, // Full Bass
0034     {-83, -50, -50, -50, -25, 15, 55, 80, 80, 80, 85}, // Full Treble
0035     {-41, 35, 30, 0, -40, -25, 10, 45, 55, 60, 60}, // Full Bass + Treble
0036     {-16, 25, 50, 25, -20, 0, -30, -40, -40, 0, 0}, // Laptop/Headphones
0037     {-25, 50, 50, 30, 30, 0, -25, -25, -25, 0, 0}, // Large Hall
0038     {0, -25, 0, 20, 25, 30, 30, 20, 15, 15, 10}, // Live
0039     {0, 35, 35, 0, 0, 0, 0, 0, 0, 35, 35}, // Party
0040     {-15, -10, 25, 35, 40, 25, -5, -15, -15, -10, -10}, // Pop
0041     {0, 0, 0, -5, -30, 0, -35, -35, 0, 0, 0}, // Reggae
0042     {-28, 40, 25, -30, -40, -20, 20, 45, 55, 55, 55}, // Rock
0043     {-33, 25, 10, -5, -15, -5, 20, 45, 50, 55, 60}, // Soft
0044     {-29, -15, -25, -25, -5, 20, 30, 45, 50, 55, 50}, // Ska
0045     {0, 20, 20, 10, -5, -25, -30, -20, -5, 15, 45}, // Soft Rock
0046     {-26, 40, 30, 0, -30, -25, 0, 40, 50, 50, 45}, // Techno
0047     {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} // Zero
0048 };
0049 
0050 QStringList
0051 EqualizerPresets::eqDefaultPresetsList()
0052 {
0053     QStringList presets;
0054     presets << "Manual"
0055             << "Classical"
0056             << "Club"
0057             << "Dance"
0058             << "Full Bass"
0059             << "Full Treble"
0060             << "Full Bass + Treble"
0061             << "Laptop/Headphones"
0062             << "Large Hall"
0063             << "Live"
0064             << "Party"
0065             << "Pop"
0066             << "Reggae"
0067             << "Rock"
0068             << "Soft"
0069             << "Ska"
0070             << "Soft Rock"
0071             << "Techno"
0072             << "Zero";
0073     return presets;
0074 }
0075 
0076 QStringList
0077 EqualizerPresets::eqDefaultTranslatedPresetsList()
0078 {
0079     QStringList strings;
0080     strings << i18n( "Manual" );
0081     strings << i18n( "Classical" );
0082     strings << i18n( "Club" );
0083     strings << i18n( "Dance" );
0084     strings << i18n( "Full Bass" );
0085     strings << i18n( "Full Treble" );
0086     strings << i18n( "Full Bass + Treble" );
0087     strings << i18n( "Laptop/Headphones" );
0088     strings << i18n( "Large Hall" );
0089     strings << i18n( "Live" );
0090     strings << i18n( "Party" );
0091     strings << i18n( "Pop" );
0092     strings << i18n( "Reggae" );
0093     strings << i18n( "Rock" );
0094     strings << i18n( "Soft" );
0095     strings << i18n( "Ska" );
0096     strings << i18n( "Soft Rock" );
0097     strings << i18n( "Techno");
0098     strings << i18n( "Zero" );
0099     return strings;
0100 }
0101 
0102 QStringList
0103 EqualizerPresets::eqGlobalTranslatedList()
0104 {
0105     QStringList globalTranslatedList = eqDefaultTranslatedPresetsList();
0106     globalTranslatedList += eqUserList();
0107     return globalTranslatedList;
0108 }
0109 
0110 QStringList
0111 EqualizerPresets::eqGlobalList()
0112 {
0113     // This function will build up a global list
0114     // first a default preset will comes
0115     // then user list is filtered to omit duplicates from default preset list
0116     QStringList mGlobalList;
0117     mGlobalList += eqDefaultPresetsList();
0118     mGlobalList += eqUserList();
0119     return mGlobalList;
0120 }
0121 
0122 QStringList
0123 EqualizerPresets::eqUserList()
0124 {
0125     const QStringList defaultList = eqDefaultPresetsList();
0126 
0127     QStringList userList;
0128     foreach( const QString &mUsrName, AmarokConfig::equalizerPresetsNames() )
0129     {
0130         if( !defaultList.contains( mUsrName ) )
0131             userList.append( mUsrName );
0132     }
0133     return userList;
0134 }
0135 
0136 // Equalizer preset management helper functions
0137 bool
0138 EqualizerPresets::eqCfgDeletePreset( const QString &presetName )
0139 {
0140       // Idea is to delete the preset only if it is user preset:
0141       // present on user list & absent on default list
0142       const int idUsr = AmarokConfig::equalizerPresetsNames().indexOf( presetName );
0143       const int idDef = eqDefaultPresetsList().indexOf( presetName );
0144 
0145       if( idUsr >= 0 && idDef < 0 )
0146       {
0147           QStringList mNewNames = AmarokConfig::equalizerPresetsNames();
0148           QList<int> mNewValues = AmarokConfig::equalizerPresestValues();
0149           mNewNames.removeAt( idUsr );
0150 
0151           for( int it = 0; it < NUM_EQ_VALUES; it++ )
0152               mNewValues.removeAt( NUM_EQ_VALUES*idUsr );
0153 
0154           AmarokConfig::setEqualizerPresetsNames( mNewNames );
0155           AmarokConfig::setEqualizerPresestValues( mNewValues );
0156           return true;
0157       }
0158 
0159       return false;
0160 }
0161 
0162 bool
0163 EqualizerPresets::eqCfgRestorePreset( const QString &presetName )
0164 {
0165       // Idea is to delete the preset if it found on both
0166       // user list and default list - delete from the latter if so
0167       const int idUsr = AmarokConfig::equalizerPresetsNames().indexOf( presetName );
0168       const int idDef = eqDefaultPresetsList().indexOf( presetName );
0169 
0170       if( idUsr >= 0 && idDef >= 0 )
0171       {
0172           QStringList mNewNames = AmarokConfig::equalizerPresetsNames();
0173           QList<int> mNewValues = AmarokConfig::equalizerPresestValues();
0174           mNewNames.removeAt( idUsr );
0175 
0176           for( int it = 0; it < NUM_EQ_VALUES; it++ )
0177               mNewValues.removeAt( NUM_EQ_VALUES*idUsr );
0178 
0179           AmarokConfig::setEqualizerPresetsNames( mNewNames );
0180           AmarokConfig::setEqualizerPresestValues( mNewValues );
0181           return true;
0182       }
0183 
0184       return false;
0185 }
0186 
0187 bool
0188 EqualizerPresets::eqCfgCanRestorePreset( const QString &presetName )
0189 {
0190       // Idea is to delete the preset if it found on both
0191       // user list and default list - delete from the latter if so
0192       const int idUsr = AmarokConfig::equalizerPresetsNames().indexOf( presetName );
0193       const int idDef = eqDefaultPresetsList().indexOf( presetName );
0194 
0195       return ( idUsr >= 0 && idDef >= 0 );
0196 }
0197 
0198 void
0199 EqualizerPresets::eqCfgSetPresetVal( const QString &presetName, const QList<int> &presetValues)
0200 {
0201     DEBUG_BLOCK
0202 
0203     debug() << "Preset:" << presetName << presetValues;
0204 
0205     // Idea is to insert new values into user list
0206     // if preset exist on the list - replace it values
0207     const int idUsr = AmarokConfig::equalizerPresetsNames().indexOf( presetName );
0208     QStringList mNewNames = AmarokConfig::equalizerPresetsNames();
0209     QList<int> mNewValues = AmarokConfig::equalizerPresestValues();
0210     debug() << "Old preset found:" << (idUsr >= 0);
0211 
0212     if( idUsr < 0 )
0213     {
0214         mNewNames.append( presetName );
0215         mNewValues += presetValues;
0216     }
0217     else
0218     {
0219         for( int it = 0; it < NUM_EQ_VALUES; it++ )
0220             mNewValues.replace( idUsr * NUM_EQ_VALUES + it, presetValues.value(it) );
0221     }
0222     AmarokConfig::setEqualizerPresetsNames( mNewNames );
0223     AmarokConfig::setEqualizerPresestValues( mNewValues );
0224 }
0225 
0226 QList<int>
0227 EqualizerPresets::eqCfgGetPresetVal( const QString &presetName )
0228 {
0229       // Idea is to return user preset with request name first
0230       // if not look into the default preset names
0231       const int idUsr = AmarokConfig::equalizerPresetsNames().indexOf( presetName );
0232       const int idDef = eqDefaultPresetsList().indexOf( presetName );
0233 
0234       QList<int> mPresetVal;
0235       if( idUsr >= 0 )
0236           mPresetVal = AmarokConfig::equalizerPresestValues().mid( idUsr * NUM_EQ_VALUES, NUM_EQ_VALUES );
0237       else if( idDef >= 0) {
0238           for (int i = 0; i < NUM_EQ_VALUES; ++i)
0239               mPresetVal << DEFAULT_PRESET_VALUES[idDef][i];
0240       }
0241 
0242       return mPresetVal;
0243 }