phoneapp/phoneringingtoneplayer/src/cphonettsplayer.cpp
changeset 78 baacf668fe89
parent 76 cfea66083b62
equal deleted inserted replaced
76:cfea66083b62 78:baacf668fe89
     1 /*
       
     2 * Copyright (c) 2009 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:
       
    15 *
       
    16 */
       
    17 
       
    18 #include "cphonettsplayer.h"
       
    19 #include "cphoneringingtone.h"
       
    20 #include "cphoneringingtoneplayer.h"
       
    21 #include "phonelogger.h"
       
    22 #include <AudioPreference.h>
       
    23 
       
    24 CPhoneTTSPlayer::CPhoneTTSPlayer(CPhoneRingingtonePlayer* aRingingtonePlayer):
       
    25 iRingingtonePlayer (aRingingtonePlayer)
       
    26     {
       
    27     
       
    28 
       
    29     }
       
    30 
       
    31 CPhoneTTSPlayer::~CPhoneTTSPlayer()
       
    32     {
       
    33     delete iTtsDelayTimer;
       
    34     delete iTtsPlayer;
       
    35     }
       
    36 
       
    37 // -----------------------------------------------------------------------------
       
    38 // 
       
    39 // -----------------------------------------------------------------------------
       
    40 //
       
    41 CPhoneTTSPlayer* CPhoneTTSPlayer::NewL(CPhoneRingingtonePlayer* aRingingtonePlayer)
       
    42     {
       
    43     CPhoneTTSPlayer* self = 
       
    44         new ( ELeave ) CPhoneTTSPlayer(aRingingtonePlayer);
       
    45     
       
    46     CleanupStack::PushL( self );
       
    47     self->ConstructL();
       
    48     CleanupStack::Pop( self );
       
    49 
       
    50     return self;
       
    51     }
       
    52 
       
    53 // -----------------------------------------------------------------------------
       
    54 // 
       
    55 // -----------------------------------------------------------------------------
       
    56 //
       
    57 void CPhoneTTSPlayer::ConstructL()
       
    58     {
       
    59     iTtsDelayTimer = CPhoneTimer::NewL();
       
    60     }
       
    61       
       
    62 // -----------------------------------------------------------------------------
       
    63 // 
       
    64 // -----------------------------------------------------------------------------
       
    65 //
       
    66 void CPhoneTTSPlayer::PlayTtsTone( 
       
    67     const TDesC& aTextToSay, 
       
    68     CPhoneRingingTone* aAudioVideoRingingTone )
       
    69     {
       
    70     __LOGMETHODSTARTEND( EPhoneControl, "CPhoneTTSPlayer::PlayTtsTone()" );
       
    71     //__PHONELOG2( EBasic, EPhoneControl, "CPhoneTTSPlayer::PlayTtsTone - aVolume(%d), aRingingType(%d)",
       
    72     //    aVolume, aRingingType );
       
    73 
       
    74     if ( iTtsPlayer )
       
    75         {
       
    76         delete iTtsPlayer;
       
    77         iTtsPlayer = NULL;
       
    78         }
       
    79 
       
    80     //Set ringingtype for Tts.
       
    81     iTtsRingingType = aAudioVideoRingingTone->RingingType();
       
    82     iTtsVolume = aAudioVideoRingingTone->Volume();
       
    83 
       
    84     // TTS should be a bit louder, than main component.
       
    85     // No need to check for overflows, they are handled in SET_PHONE_VOLUME macro.
       
    86     
       
    87     TPtrC ttsHeader( KPhoneTtsHeader );
       
    88     HBufC* preparedString = 
       
    89         HBufC::New( aTextToSay.Length() + ttsHeader.Length() );
       
    90 
       
    91     if ( preparedString )
       
    92         {
       
    93         TPtr pPreparedString = preparedString->Des();
       
    94         pPreparedString.Append( ttsHeader );
       
    95         pPreparedString.Append( aTextToSay );
       
    96            
       
    97         __PHONELOG1( 
       
    98             EBasic, 
       
    99             EPhoneControl, 
       
   100             "CPhoneTTSPlayer::PlayTtsTone - about to say %S",
       
   101             &pPreparedString );
       
   102 
       
   103         TRAPD( 
       
   104             error, 
       
   105             iTtsPlayer = CPhoneAudioPlayer::NewTtsL( 
       
   106                *preparedString, 
       
   107                KAudioPriorityPhoneCall, 
       
   108                KAudioPrefTextToSpeechCallHardCoded,
       
   109                *this, 
       
   110                CPhoneRingingtonePlayer::EPlayerTts ) );
       
   111         if( error == KErrNone ) 
       
   112             {
       
   113             // To be played when default or personal tone will be played
       
   114             iTtsToneToBePlayed = ETrue;
       
   115             }
       
   116         __PHONELOG2(
       
   117             EBasic,
       
   118             EPhoneControl,
       
   119             "CPhoneTTSPlayer::PlayTtsTone - error(%d), iTtsPhoneToBePlayed(%d)",
       
   120             error, 
       
   121             iTtsToneToBePlayed );
       
   122 
       
   123         delete preparedString;  
       
   124         }
       
   125     }
       
   126 
       
   127 // -----------------------------------------------------------------------------
       
   128 // 
       
   129 // -----------------------------------------------------------------------------
       
   130 //
       
   131 void CPhoneTTSPlayer::StopPlaying()
       
   132     {
       
   133     __LOGMETHODSTARTEND( EPhoneControl, "CPhoneTTSPlayer::StopPlaying()" );
       
   134 
       
   135     iTtsToneToBePlayed = EFalse;
       
   136     iTTsTimeOutCounter = 0;
       
   137     iTtsDelayTimer->Cancel();
       
   138     if ( iTtsPlayer )
       
   139         {
       
   140         iTtsPlayer->StopPlaying();
       
   141         delete iTtsPlayer;
       
   142         iTtsPlayer = NULL;
       
   143         }
       
   144     }
       
   145 // -----------------------------------------------------------------------------
       
   146 // 
       
   147 // -----------------------------------------------------------------------------
       
   148 //
       
   149 void CPhoneTTSPlayer::HandleAudioPlayerError( 
       
   150     TPhoneAudioPlayerErrorEvent /*aEvent*/, 
       
   151     TInt /*aError*/, 
       
   152     TInt /*aId*/ )
       
   153     {
       
   154     __LOGMETHODSTARTEND( EPhoneControl, "CPhoneTTSPlayer::HandleAudioPlayerError()" );
       
   155 
       
   156     }
       
   157 
       
   158 // -----------------------------------------------------------------------------
       
   159 // 
       
   160 // -----------------------------------------------------------------------------
       
   161 //
       
   162 void CPhoneTTSPlayer::HandleAudioPlayerInitComplete( TInt /*aId*/ )
       
   163     {
       
   164     __LOGMETHODSTARTEND( EPhoneControl, "CPhoneTTSPlayer::HandleAudioPlayerInitComplete()" );
       
   165     // This is called from CPhoneAudioPlayer - we do not 
       
   166     // want to delete instances from which this method
       
   167     // is called. 
       
   168 
       
   169     if( iTtsToneToBePlayed )  
       
   170         {
       
   171         // Start TTS timer just before "normal ringtone component" starts 
       
   172         // playing, i.e. after init completed.
       
   173         if ( iTtsDelayIndex < iTtsDelaysCount )  
       
   174             {
       
   175             if ( !iTtsDelayTimer->IsActive() )
       
   176                 {
       
   177                 // There are TTS iterations to be played yet.
       
   178                 iTtsDelayTimer->After( 
       
   179                     KPhoneTtsDelays[iTtsDelayIndex], 
       
   180                     TCallBack( HandleTtsDelayTimeout, this ) );
       
   181                 }
       
   182             }
       
   183         }
       
   184     }
       
   185 
       
   186 // -----------------------------------------------------------------------------
       
   187 // CPhoneTTSPlayer::HandlePlayingComplete
       
   188 // -----------------------------------------------------------------------------
       
   189 //
       
   190 void CPhoneTTSPlayer::HandlePlayingComplete( TInt aId )
       
   191     {
       
   192     __LOGMETHODSTARTEND( EPhoneControl, "CPhoneTTSPlayer::HandlePlayingComplete()" );
       
   193     // This is called from CPhoneAudioPlayer - we do not 
       
   194     // want to delete instances from which this method
       
   195     // is called. 
       
   196 
       
   197   /*  __PHONELOG2(
       
   198         EBasic, 
       
   199         EPhoneControl,
       
   200         "CPhoneTTSPlayer::HandlePlayingComplete - aId(%d), iRingingType(%d)",
       
   201         aId,
       
   202         iRingingType );*/
       
   203     __PHONELOG2(
       
   204         EBasic, 
       
   205         EPhoneControl,
       
   206         "CPhoneTTSPlayer::HandlePlayingComplete - iTtsToneToBePlayed(%d), iTtsDelayIndex(%d)",
       
   207         iTtsToneToBePlayed,
       
   208         iTtsDelayIndex );
       
   209 
       
   210     if ( aId == CPhoneRingingtonePlayer::EPlayerTts ) 
       
   211         {
       
   212         __PHONELOG( EBasic, EPhoneControl, "CPhoneTTSPlayer::HandlePlayingComplete - resuming after TTS" );
       
   213 
       
   214         // TTS playing complete, normal tone player should volume up.
       
   215         if ( ( iTtsToneToBePlayed ) && ( ++iTtsDelayIndex < iTtsDelaysCount ) )  
       
   216             {
       
   217             __PHONELOG( EBasic, EPhoneControl, "CPhoneTTSPlayer::HandlePlayingComplete - resuming after TTS first time" );
       
   218             //Callers name is said once. increase tone player volume.    
       
   219             SolveNewVolumeAndRamptime( ESaidOnce );
       
   220             
       
   221             if ( !iTtsDelayTimer->IsActive() )
       
   222                 {
       
   223                 // There are TTS iterations to be played yet.
       
   224                 iTtsDelayTimer->After( KPhoneTtsDelays[iTtsDelayIndex], 
       
   225                     TCallBack( HandleTtsDelayTimeout,this ) );
       
   226                 }
       
   227             }
       
   228         else
       
   229             {
       
   230             __PHONELOG( EBasic, EPhoneControl, "CPhoneTTSPlayer::HandlePlayingComplete - resuming after TTS second time" );
       
   231             SolveNewVolumeAndRamptime( ESaidTwice );
       
   232             }
       
   233         
       
   234         // No further processing. All TTS player-related events are not 
       
   235         // "real" ringtone playing completions.
       
   236         return;
       
   237         }
       
   238     //Case: RingintonePlayer has completed playing with following set: ring once and
       
   239     // TTS is activated   
       
   240    if ( iTtsRingingType == EProfileRingingTypeRingingOnce && 
       
   241         iTtsToneToBePlayed )   
       
   242         {
       
   243         __PHONELOG( EBasic, EPhoneControl, "CPhoneTTSPlayer::HandlePlayingComplete - ring once and TTS" );
       
   244          if ( iTtsDelayIndex < iTtsDelaysCount )
       
   245             {
       
   246             ReStartRingingTonePlayer();
       
   247             //Do not destroy player yet. Return.
       
   248             return;            
       
   249             }
       
   250         }
       
   251     //Case: RingintonePlayer has completed playing with following set: ringing 
       
   252     //and TTS is activated. We need  to restart ringintoneplayer and new TTS
       
   253     //iterations are required.
       
   254     else if ( iTtsRingingType == EProfileRingingTypeRinging && iTtsToneToBePlayed )
       
   255         {  
       
   256         __PHONELOG( EBasic, EPhoneControl, "CPhoneTTSPlayer::HandlePlayingComplete - ringing and TTS" );
       
   257         if ( iTtsDelayIndex == iTtsDelaysCount )
       
   258             {
       
   259             //Case: Ringingtype is EProfileRingingTypeRinging. New TTS
       
   260             //iterations are required.    
       
   261             __PHONELOG( EBasic, EPhoneControl, "CPhoneTTSPlayer::HandlePlayingComplete - need to restart TTS sequence" );
       
   262             //TTS has completed its iterations,set index to zero. 
       
   263             iTtsDelayIndex = 0;
       
   264             if ( !iTtsDelayTimer->IsActive() )
       
   265                 {
       
   266                 //Restart TTS sequence
       
   267                 iTtsDelayTimer->After( 
       
   268                     KPhoneTtsDelays[iTtsDelayIndex], 
       
   269                     TCallBack( HandleTtsDelayTimeout, this ) ); 
       
   270                 }
       
   271             }
       
   272         
       
   273         ReStartRingingTonePlayer();
       
   274         //Do not destroy player yet. Return
       
   275         return;  
       
   276         }
       
   277     }
       
   278 
       
   279 
       
   280 // -----------------------------------------------------------------------------
       
   281 // 
       
   282 // -----------------------------------------------------------------------------
       
   283 //
       
   284 TInt CPhoneTTSPlayer::HandleTtsDelayTimeout( TAny* object )
       
   285     {
       
   286     __LOGMETHODSTARTEND( EPhoneControl, "CPhoneTTSPlayer::HandleTtsDelayTimeout()" );
       
   287     static_cast<CPhoneTTSPlayer*>( object )->
       
   288         DoHandleTtsDelayTimeout();
       
   289     return KErrNone;
       
   290     }
       
   291 
       
   292 // -----------------------------------------------------------------------------
       
   293 // 
       
   294 // -----------------------------------------------------------------------------
       
   295 //
       
   296 void CPhoneTTSPlayer::DoHandleTtsDelayTimeout()
       
   297     {
       
   298     __LOGMETHODSTARTEND( EPhoneControl, "CPhoneTTSPlayer::DoHandleTtsDelayTimeout()" );
       
   299     __PHONELOG2( EBasic, EPhoneControl, "CPhoneTTSPlayer::DoHandleTtsDelayTimeout - iTtsRingingType(%d), iTTsTimeOutCounter(%d)",
       
   300         iTtsRingingType, iTTsTimeOutCounter );
       
   301 
       
   302     CPhoneAudioPlayer* currPlayer = NULL;
       
   303     currPlayer = GetCurrentlyActiveAudioPlayerWithTTs();        
       
   304     
       
   305     if ( !currPlayer )
       
   306         {
       
   307         __PHONELOG( EBasic, EPhoneControl, "CPhoneTTSPlayer::DoHandleTtsDelayTimeout - null current player" );
       
   308         return;
       
   309         }
       
   310     TInt volume( 0 );
       
   311      //Ascending case. TTs player needs to be ascending.
       
   312     if ( iTtsRingingType == EProfileRingingTypeAscending )
       
   313         {
       
   314         //Volume needs to be different in different ascending steps
       
   315         __PHONELOG( EBasic, EPhoneControl, "CPhoneTTSPlayer::DoHandleTtsDelayTimeout - play ascending" ); 
       
   316         if ( !iTTsTimeOutCounter )
       
   317             {
       
   318             //We are saying callers name for the first time in ascending mode.
       
   319             //Say callers name by volume level KTtsVolumeMin.             
       
   320             volume = KTtsVolumeMin;
       
   321                 
       
   322             iTtsPlayer->Play(
       
   323                 ConvertRingingType( EProfileRingingTypeRingingOnce ),
       
   324                 volume,
       
   325                 iTtsToneToBePlayed );
       
   326             }
       
   327         else
       
   328             {
       
   329             //Checks the case that ringingtone is very quiet. Then do not play
       
   330             //TTS too loud
       
   331             volume = iTtsVolume < KTtsVolumeMin ? KTtsVolumeMin : KTtsVolumeAscendingRepeat;     
       
   332                            
       
   333             //We are saying callers name for the second time in ascending mode.
       
   334             //Say callers name by volume level KTtsVolumeAscendingRepeat and decrease current players volume
       
   335             //to KPlayerVolumeAscendingRepeat. RampTime is zero
       
   336             currPlayer->SetNewVolumeAndRamptime( KPlayerVolumeAscendingRepeat, 0 );
       
   337             iTtsPlayer->Play(
       
   338                 ConvertRingingType( EProfileRingingTypeRingingOnce ),
       
   339                 volume,
       
   340                 iTtsToneToBePlayed );
       
   341             }
       
   342        
       
   343         }
       
   344     else //Normal ringing case.
       
   345         {
       
   346         __PHONELOG( EBasic, EPhoneControl, "CPhoneTTSPlayer::DoHandleTtsDelayTimeout - play normal" ); 
       
   347         currPlayer->SetNewVolumeAndRamptime( iTtsVolume-5, 0 );                       
       
   348         iTtsPlayer->Play(
       
   349             ConvertRingingType( EProfileRingingTypeRingingOnce ),
       
   350             iTtsVolume,
       
   351             iTtsToneToBePlayed );
       
   352         }
       
   353       
       
   354      iTTsTimeOutCounter++;
       
   355     }
       
   356 
       
   357 // -----------------------------------------------------------------------------
       
   358 // CPhoneTTSPlayer::AddTtsPlaybackIfNeeded
       
   359 // -----------------------------------------------------------------------------
       
   360 //
       
   361 void CPhoneTTSPlayer::AddTtsPlaybackIfNeeded()
       
   362     {
       
   363     __LOGMETHODSTARTEND( EPhoneControl, "CPhoneTTSPlayer::AddTtsPlaybackIfNeeded()" );
       
   364     if ( iTtsToneToBePlayed )   
       
   365         {
       
   366         iTtsDelayIndex = 0;
       
   367         iTtsDelaysCount = KPhoneTtsDelaysCount;
       
   368         }
       
   369     }
       
   370 
       
   371 // -----------------------------------------------------------------------------
       
   372 // CPhoneTTSPlayer::SolveNewVolumeAndRamptime
       
   373 // -----------------------------------------------------------------------------
       
   374 //
       
   375 void CPhoneTTSPlayer::SolveNewVolumeAndRamptime( TTtsStatus aStatus )
       
   376     {
       
   377     __LOGMETHODSTARTEND( EPhoneControl, "CPhoneTTSPlayer::SolveNewVolumeAndRamptime()" );
       
   378 
       
   379     CPhoneAudioPlayer* currPlayer = NULL;
       
   380     currPlayer = GetCurrentlyActiveAudioPlayerWithTTs();
       
   381     if ( !currPlayer )
       
   382         {
       
   383         __PHONELOG( EBasic, EPhoneControl, "CPhoneTTSPlayer::SolveNewVolumeAndRamptime - null current player" );
       
   384         return;
       
   385         }
       
   386     TInt ramptime( 0 );
       
   387     //Here is handled the cases when Callers name is said. Sequence is:
       
   388     //3 sec. tone + name + 4 sec. tone + name +the tone until the end. 
       
   389     switch ( aStatus )
       
   390         {
       
   391         case ESaidOnce:
       
   392             if ( iTtsRingingType == EProfileRingingTypeAscending )
       
   393                 {
       
   394                 //The ramptime could be ( 4 )* KPhoneTtsAscendingStep but now
       
   395                 //we are setting this to 0 because when Say callers name is said
       
   396                 //for first time: play ringing tone on level 3 for four secs.
       
   397                 ramptime = 0;
       
   398                 __PHONELOG1( EBasic, EPhoneControl, "CPhoneTTSPlayer::SolveNewVolumeAndRamptime - said once ascending - ramptime(%d)",
       
   399                     ramptime ); 
       
   400                 currPlayer->SetNewVolumeAndRamptime( KPlayerVolumeAscendingRepeat, ramptime );
       
   401                 }
       
   402             else
       
   403                 {
       
   404                  //Normal ringingtone case. Adjust volume back to profile level. 
       
   405                 __PHONELOG1( EBasic, EPhoneControl, "CPhoneTTSPlayer::SolveNewVolumeAndRamptime - said once normal - ramptime(%d)",
       
   406                     ramptime ); 
       
   407                 currPlayer->SetNewVolumeAndRamptime( iTtsVolume, 0 );    
       
   408                 }
       
   409              break;
       
   410         
       
   411         case ESaidTwice:
       
   412             if ( iTtsRingingType == EProfileRingingTypeAscending )
       
   413                 {
       
   414                 TInt vol = iTtsVolume;
       
   415                 if ( vol > KPlayerVolumeAscendingRepeat )
       
   416                     {
       
   417                     vol = vol - KTtsVolumeAscendingDecrease;
       
   418                     }
       
   419                           
       
   420                 //TTS playing complete for second time. increase tone player volume.
       
   421                 ramptime = ( vol )*KPhoneTtsAscendingStep;
       
   422                 __PHONELOG1( EBasic, EPhoneControl, "CPhoneTTSPlayer::SolveNewVolumeAndRamptime - said twice ascending - ramptime(%d)",
       
   423                     ramptime ); 
       
   424                 currPlayer->SetNewVolumeAndRamptime( iTtsVolume, ramptime );
       
   425                 }
       
   426             else
       
   427                 {
       
   428                  //Normal ringingtone case. Adjust volume back to profile level. 
       
   429                 __PHONELOG1( EBasic, EPhoneControl, "CPhoneTTSPlayer::SolveNewVolumeAndRamptime - said twice normal - ramptime(%d)",
       
   430                     ramptime ); 
       
   431                 currPlayer->SetNewVolumeAndRamptime( iTtsVolume, 0 );
       
   432                 }
       
   433             break;
       
   434         
       
   435         default:
       
   436             break;
       
   437         }
       
   438     }
       
   439 
       
   440 
       
   441 // -----------------------------------------------------------------------------
       
   442 // 
       
   443 // -----------------------------------------------------------------------------
       
   444 //    
       
   445 CPhoneAudioPlayer* 
       
   446 CPhoneTTSPlayer::GetCurrentlyActiveAudioPlayerWithTTs()
       
   447     {    
       
   448     __LOGMETHODSTARTEND( EPhoneControl, "CPhoneTTSPlayer::GetCurrentlyActiveAudioPlayerWithTTs()" );
       
   449 
       
   450     return iRingingtonePlayer->GetCurrentlyActiveAudioPlayer();
       
   451     }
       
   452     
       
   453 // -----------------------------------------------------------------------------
       
   454 // 
       
   455 // -----------------------------------------------------------------------------
       
   456 //      
       
   457 void CPhoneTTSPlayer::ReStartRingingTonePlayer()
       
   458     {
       
   459     __LOGMETHODSTARTEND( EPhoneControl, "CPhoneTTSPlayer::ReStartRingingTonePlayer()" );
       
   460 
       
   461     //First get currently active player which plays among TTS player.
       
   462     CPhoneAudioPlayer* currPlayer = NULL;
       
   463     currPlayer = GetCurrentlyActiveAudioPlayerWithTTs();   
       
   464     if ( currPlayer )
       
   465         {
       
   466         currPlayer->ReStartPlaying();
       
   467         }      
       
   468     }
       
   469 
       
   470 // -----------------------------------------------------------------------------
       
   471 // 
       
   472 // -----------------------------------------------------------------------------
       
   473 //
       
   474 CPhoneAudioPlayer::TRingingType CPhoneTTSPlayer::ConvertRingingType(
       
   475     TProfileRingingType aRingingType )
       
   476     {
       
   477     __LOGMETHODSTARTEND( EPhoneControl, "CPhoneTTSPlayer::ConvertRingingType()" );
       
   478 
       
   479     CPhoneAudioPlayer::TRingingType ringingType = 
       
   480         CPhoneAudioPlayer::ETypeRinging;
       
   481     switch ( aRingingType )
       
   482         {
       
   483         case EProfileRingingTypeRinging:
       
   484             ringingType = CPhoneAudioPlayer::ETypeRinging;
       
   485             break;
       
   486 
       
   487         case EProfileRingingTypeAscending:
       
   488             ringingType = CPhoneAudioPlayer::ETypeAscending;
       
   489             break;
       
   490 
       
   491         case EProfileRingingTypeRingingOnce:
       
   492             ringingType = CPhoneAudioPlayer::ETypeRingingOnce;
       
   493             break;
       
   494 
       
   495         default:
       
   496             break;
       
   497         }
       
   498     return ringingType;
       
   499     }
       
   500 
       
   501  
       
   502