mpserviceplugins/audioeffects/src/mpxaudioeffectengine.cpp
branchRCL_3
changeset 25 14979e23cb5e
equal deleted inserted replaced
24:26a1709b9fec 25:14979e23cb5e
       
     1 /*
       
     2 * Copyright (c) 2005 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  Engine class that uses the Effects API and saves changes.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include <centralrepository.h>
       
    21 #include <CustomCommandUtility.h>
       
    22 #include <mpxplaybackutility.h>
       
    23 #include <mpxplaybackmessage.h>
       
    24 
       
    25 #include <EnvironmentalReverbUtility.h>
       
    26 #include <EnvironmentalReverbUtilityData.h>
       
    27 #include <StereoWideningBase.h>
       
    28 #include <StereoWideningData.h>
       
    29 #include <BassBoostBase.h>
       
    30 #include <BassBoostData.h>
       
    31 #include <LoudnessBase.h>
       
    32 #include <LoudnessData.h>
       
    33 #include <AudioEqualizerUtility.h>
       
    34 #include <EqualizerConstants.h>
       
    35 #include <mpxlog.h>
       
    36 
       
    37 #include "mpxaudioeffectengine.h"
       
    38 
       
    39 // Music setting
       
    40 const TUid KMPCenRepSettingsFeature = {0x10207C92};
       
    41 const TUint32 KMPCenRepSettingPresetIdKey = 0x00000004;
       
    42 
       
    43 // ================= MEMBER FUNCTIONS =======================
       
    44 
       
    45 // -----------------------------------------------------------------------------
       
    46 // CMPXAudioEffectEngine::NewL()
       
    47 // Standard 2-Phased Constructor
       
    48 // -----------------------------------------------------------------------------
       
    49 // 
       
    50 EXPORT_C CMPXAudioEffectEngine* CMPXAudioEffectEngine::NewL(
       
    51     CMdaAudioPlayerUtility* aPlayer )
       
    52     {
       
    53     CMPXAudioEffectEngine* self = new( ELeave ) CMPXAudioEffectEngine( aPlayer );
       
    54     CleanupStack::PushL( self );
       
    55     self->ConstructL();
       
    56     CleanupStack::Pop( self );
       
    57     return self;
       
    58     }
       
    59  
       
    60 // -----------------------------------------------------------------------------
       
    61 // CMPXAudioEffectEngine::CMPXAudioEffectEngine()
       
    62 // Constructor
       
    63 // -----------------------------------------------------------------------------
       
    64 // 
       
    65 CMPXAudioEffectEngine::CMPXAudioEffectEngine(CMdaAudioPlayerUtility* aPlayer)
       
    66                                     :iMdaPlayer(aPlayer)   
       
    67     {
       
    68     }
       
    69 
       
    70 // -----------------------------------------------------------------------------
       
    71 // CMPXAudioEffectEngine::~CMPXAudioEffectEngine()
       
    72 // Virtual destructor
       
    73 // -----------------------------------------------------------------------------
       
    74 // 
       
    75 EXPORT_C CMPXAudioEffectEngine::~CMPXAudioEffectEngine()
       
    76     {    
       
    77     if (iProp)
       
    78         {
       
    79         delete iProp;
       
    80         }
       
    81     DestroyAudioEffect();
       
    82     }
       
    83 
       
    84 // -----------------------------------------------------------------------------
       
    85 // CMPXAudioEffectEngine::ConstructL()
       
    86 // Standard Second Phased Constructor
       
    87 // -----------------------------------------------------------------------------
       
    88 // 
       
    89 void CMPXAudioEffectEngine::ConstructL()
       
    90     {
       
    91     iProp = new(ELeave) CMPXAudioEffectProperties();   
       
    92     MPX_TRAPD( err, iProp->LoadFromFileL() ); //Loads saved settings if available
       
    93     if( err != KErrNone )
       
    94         {
       
    95         MPX_DEBUG1("CMPXAudioEffectEngine::ConstructL -- reset save to file");
       
    96         iProp->Reset();
       
    97         iProp->SaveToFileL();        
       
    98         }
       
    99     }
       
   100 
       
   101 
       
   102 // -----------------------------------------------------------------------------
       
   103 // CMPXAudioEffectEngine::SetBalanceL()
       
   104 // (other items were commented in a header).
       
   105 // -----------------------------------------------------------------------------
       
   106 // 
       
   107 EXPORT_C void CMPXAudioEffectEngine::SetBalanceL()
       
   108     {
       
   109     iProp->LoadFromFileL();
       
   110     if(iMdaPlayer)
       
   111         {
       
   112         TInt currentBalance;    
       
   113         TInt err = iMdaPlayer->GetBalance(currentBalance);
       
   114         if( err !=KErrNone )
       
   115             {   
       
   116             User::Leave(KErrNotSupported);    
       
   117             }        
       
   118         // don't set non-changed values:   
       
   119         if( iProp->Balance() != currentBalance )
       
   120             {
       
   121             err = iMdaPlayer->SetBalance(iProp->Balance());
       
   122             if( err !=KErrNone )
       
   123                 {
       
   124                 User::Leave(KErrNotSupported);    
       
   125                 }            
       
   126             }
       
   127         } 
       
   128     }
       
   129   
       
   130   
       
   131 // -----------------------------------------------------------------------------
       
   132 // CMPXAudioEffectEngine::SetReverberationL()
       
   133 // (other items were commented in a header).
       
   134 // -----------------------------------------------------------------------------
       
   135 // 
       
   136 EXPORT_C void CMPXAudioEffectEngine::SetReverberationL()
       
   137     {
       
   138 #ifdef _REVERB
       
   139     if(!iReverbEffect)
       
   140         {
       
   141          //Effect enabled immediately, cmdUtil ownership passed into new object
       
   142         MPX_TRAPD(error, iReverbEffect = 
       
   143                             CEnvironmentalReverbUtility::NewL(*iMdaPlayer));
       
   144         if(!error)
       
   145             {
       
   146             SetReverberationL();
       
   147             }
       
   148         }
       
   149 
       
   150     iProp->LoadFromFileL();
       
   151 
       
   152     TBuf16<32> reverbation;
       
   153     TBuf16<32> reverbation2;
       
   154   
       
   155     switch( iProp->Reverb() ) //aIndex ranges always from 0 to 7
       
   156         {
       
   157         case 1:   _LIT( KRevName, "Alley" );
       
   158               reverbation.Append( KRevName );
       
   159               break;
       
   160         case 2:   _LIT( KRevName2, "Bathroom" );
       
   161               reverbation.Append( KRevName2 );
       
   162               break;
       
   163         case 3:   _LIT( KRevName3, "Underwater" );
       
   164               reverbation.Append( KRevName3 );
       
   165               break;
       
   166         case 4:   _LIT( KRevName4, "Small room" );
       
   167               reverbation.Append( KRevName4 );
       
   168               break;
       
   169         case 5:   _LIT( KRevName5, "Medium room" );
       
   170               reverbation.Append( KRevName5 );
       
   171               break;
       
   172         case 6:   _LIT( KRevName6, "Large room" );
       
   173               reverbation.Append( KRevName6 );
       
   174               break;
       
   175         case 7:   _LIT( KRevName7, "Large hall" );
       
   176               reverbation.Append( KRevName7 );
       
   177               break;
       
   178         default:  break;
       
   179         }
       
   180     TUint32 count;
       
   181     count = iReverbEffect->NumberOfPreDefinedPresets();
       
   182     TArray<TEfEnvironmentalReverbUtilityPreset> array = iReverbEffect->Presets();
       
   183     
       
   184     if( 0 != iProp->Reverb() )
       
   185         {
       
   186         TInt i = 0;
       
   187         
       
   188         do    //compare descriptors and apply preset if descriptors match
       
   189           {
       
   190           reverbation2.Copy( array[i].iPresetName );
       
   191           if( reverbation2.Compare(reverbation) == 0 )
       
   192             {
       
   193             iReverbEffect->ApplyPresetL( i );
       
   194             i = count;
       
   195             }
       
   196           i++;
       
   197           }while( i < count );
       
   198         }
       
   199     else
       
   200         {
       
   201         iReverbEffect->DisableEnvironmentalReverbL();
       
   202         }
       
   203 #endif
       
   204     }
       
   205   
       
   206 
       
   207 // -----------------------------------------------------------------------------
       
   208 // CMPXAudioEffectEngine::SetStereoWideningL()
       
   209 // (other items were commented in a header).
       
   210 // -----------------------------------------------------------------------------
       
   211 // 
       
   212 EXPORT_C void CMPXAudioEffectEngine::SetStereoWideningL()
       
   213     {
       
   214 #ifdef _STEREO
       
   215     iProp->LoadFromFileL();
       
   216     
       
   217     if(iProp->Stereo())
       
   218         {
       
   219         if(!iStereoEffect)  // If stereo widening is ON and not constructed
       
   220             {
       
   221             TUint stereoLevel = 30;
       
   222             // cmdUtil ownership passed into new object           
       
   223             MPX_TRAPD(error, 
       
   224                   iStereoEffect = CStereoWidening::NewL(*iMdaPlayer, 
       
   225                                                         EFalse, stereoLevel));
       
   226             if(error)
       
   227                 {
       
   228                 iStereoEffect = NULL;
       
   229                 User::Leave(KErrNotSupported);
       
   230                 }
       
   231             }
       
   232 
       
   233         if ( !iStereoEffect->IsEnabled() )
       
   234             {
       
   235             iStereoEffect->EnableL();
       
   236             TUint8 level = 30;
       
   237             iStereoEffect->SetStereoWideningLevelL( level );
       
   238             iStereoEffect->ApplyL();                
       
   239             }
       
   240 
       
   241         }
       
   242     else
       
   243         {
       
   244         if (iStereoEffect && iStereoEffect->IsEnabled() )   // If audio effects was not on, then no need to disable
       
   245             { 
       
   246             iStereoEffect->DisableL();
       
   247             }
       
   248         }
       
   249 #endif
       
   250     }
       
   251 
       
   252 // -----------------------------------------------------------------------------
       
   253 // CMPXAudioEffectEngine::SetBassBoostL()
       
   254 // (other items were commented in a header).
       
   255 // -----------------------------------------------------------------------------
       
   256 // 
       
   257 EXPORT_C void CMPXAudioEffectEngine::SetBassBoostL()
       
   258   {
       
   259 #ifdef _BASSBOOST
       
   260     iProp->LoadFromFileL();
       
   261     if(iProp->BassBoost())
       
   262         {
       
   263         if (!iBassBoostEffect)
       
   264             {
       
   265             // cmdUtil ownership passed into new object           
       
   266             MPX_TRAPD(error, 
       
   267                   iBassBoostEffect = CBassBoost::NewL(*iMdaPlayer, 
       
   268                                                       iProp->BassBoost()));
       
   269             if (error)
       
   270                 {
       
   271                 User::Leave(KErrNotSupported);
       
   272                 }
       
   273             }
       
   274         iBassBoostEffect->EnableL();
       
   275         }
       
   276     else
       
   277         {
       
   278         if (iBassBoostEffect)
       
   279             {
       
   280             iBassBoostEffect->DisableL();    
       
   281             }
       
   282         }
       
   283 #endif
       
   284   }
       
   285 
       
   286 // -----------------------------------------------------------------------------
       
   287 // CMPXAudioEffectEngine::SetLoudnessL()
       
   288 // (other items were commented in a header).
       
   289 // -----------------------------------------------------------------------------
       
   290 // 
       
   291 EXPORT_C void CMPXAudioEffectEngine::SetLoudnessL()
       
   292     {
       
   293 #ifdef _LOUDNESS
       
   294     iProp->LoadFromFileL();
       
   295     if(iProp->Loudness())
       
   296         {
       
   297         if(!iLoudnessEffect)
       
   298             {
       
   299             // cmdUtil ownership passed into new object           
       
   300             MPX_TRAPD(error, 
       
   301                   iLoudnessEffect = CLoudness::NewL(*iMdaPlayer, 
       
   302                                                     iProp->Loudness()));
       
   303             if(error)
       
   304                 {
       
   305                 iLoudnessEffect = NULL;
       
   306                 User::Leave(KErrNotSupported);
       
   307                 }
       
   308             else
       
   309                 {
       
   310                 }
       
   311             } 
       
   312             
       
   313         if( !iLoudnessEffect->IsEnabled() )
       
   314             {   
       
   315             iLoudnessEffect->EnableL();    
       
   316             }
       
   317         }
       
   318     else
       
   319         { 
       
   320         if( iLoudnessEffect && iLoudnessEffect->IsEnabled() )
       
   321             {       
       
   322             iLoudnessEffect->DisableL();
       
   323             }
       
   324         }
       
   325 #endif
       
   326     }
       
   327 
       
   328 // -----------------------------------------------------------------------------
       
   329 // Sets the equalizer
       
   330 // (other items were commented in a header).
       
   331 // -----------------------------------------------------------------------------
       
   332 // 
       
   333 EXPORT_C void CMPXAudioEffectEngine::SetEqualizerL()
       
   334     {
       
   335     MPX_DEBUG1("CMPXAudioEffectEngine::SetEqualizerL <--");
       
   336     
       
   337     // Create the effect
       
   338     //
       
   339     if( !iEqualizerEffect )
       
   340         {
       
   341         iEqualizerEffect = CAudioEqualizerUtility::NewL(*iMdaPlayer);
       
   342         }
       
   343         
       
   344     // Get equalizer preset id from cenrep
       
   345     //
       
   346     TInt presetId( KEqualizerPresetNone );
       
   347     TRAP_IGNORE(
       
   348         {
       
   349         CRepository* repository = CRepository::NewL( KMPCenRepSettingsFeature  );
       
   350         repository->Get( KMPCenRepSettingPresetIdKey, presetId );
       
   351         delete repository;
       
   352         repository = NULL;
       
   353         } );
       
   354 
       
   355     // Translate preset ID to index
       
   356     //
       
   357     MPX_DEBUG1("CMPXAudioEffectEngine::SetEqualizerL = finding preset index");
       
   358     
       
   359     TInt index( KErrNotFound );
       
   360     if ( iEqualizerEffect && presetId != KEqualizerPresetNone )
       
   361         {
       
   362         TArray<TEfAudioEqualizerUtilityPreset> presetArray = 
       
   363             iEqualizerEffect->Presets();
       
   364 
       
   365         TBool found( EFalse );
       
   366         for ( TInt i = 0; i < presetArray.Count() && found == EFalse; i++ )
       
   367             {
       
   368             if ( presetArray[i].iPresetNameKey == 
       
   369                 static_cast<TUint32>( presetId ) )
       
   370                 {
       
   371                 found = ETrue;
       
   372                 index = i;
       
   373                 }
       
   374             }
       
   375         }
       
   376     MPX_DEBUG2("CMPXAudioEffectEngine::SetEqualizerL effect index: %i", index);
       
   377     
       
   378     // Apply preset index index or disable if not found
       
   379     //
       
   380     if ( KErrNotFound != index )
       
   381         {
       
   382         iEqualizerEffect->ApplyPresetL( index );
       
   383         }
       
   384     else
       
   385         {
       
   386         iEqualizerEffect->DisableEqualizerL();    
       
   387         }
       
   388         
       
   389     MPX_DEBUG1("CMPXAudioEffectEngine::SetEqualizerL -->");
       
   390     }
       
   391     
       
   392 // ---------------------------------------------------------------------------
       
   393 // CreateAudioEffectsL
       
   394 // ---------------------------------------------------------------------------
       
   395 //
       
   396 EXPORT_C void CMPXAudioEffectEngine::CreateAudioEffectsL()
       
   397     {
       
   398     SetEqualizerL();
       
   399     SetStereoWideningL();
       
   400     SetBalanceL();
       
   401     SetLoudnessL();
       
   402     }
       
   403     
       
   404 // -----------------------------------------------------------------------------
       
   405 // Destroy audio effect
       
   406 // -----------------------------------------------------------------------------
       
   407 // 
       
   408 EXPORT_C void CMPXAudioEffectEngine::DestroyAudioEffect()
       
   409     {
       
   410     if (iReverbEffect)
       
   411         {
       
   412         delete iReverbEffect;     //effect automatically disabled
       
   413         iReverbEffect = NULL;
       
   414         }
       
   415         
       
   416     if (iStereoEffect)
       
   417         {
       
   418         delete iStereoEffect;     //effect automatically disabled
       
   419         iStereoEffect = NULL;
       
   420         }
       
   421     if (iBassBoostEffect)
       
   422         {
       
   423         delete iBassBoostEffect;  //effect automatically disabled
       
   424         iBassBoostEffect = NULL;
       
   425         }
       
   426     if (iLoudnessEffect)
       
   427         {
       
   428         delete iLoudnessEffect;   //effect automatically disabled
       
   429         iLoudnessEffect = NULL;
       
   430         }
       
   431     if (iEqualizerEffect)
       
   432         {
       
   433         delete iEqualizerEffect;  //effect automatically disabled
       
   434         iEqualizerEffect = NULL;
       
   435         }
       
   436     }
       
   437 
       
   438 //End of File