voiceui/voiceuivoicerecognition/src/vuicttsplayer.cpp
branchRCL_3
changeset 18 cad71a31b7fc
parent 17 8ce15fced3a6
child 19 e36f3802f733
equal deleted inserted replaced
17:8ce15fced3a6 18:cad71a31b7fc
     1 /*
       
     2 * Copyright (c) 2006-2007 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 
       
    19 // INCLUDE FILES
       
    20 #include "vuicttsplayer.h"
       
    21 
       
    22 #include "rubydebug.h"
       
    23 
       
    24 // CONSTANTS
       
    25 const TInt KMaxBasicSegments = 3;
       
    26 const TInt KMaxEnhancedSegments = 3;
       
    27     
       
    28 // -----------------------------------------------------------------------------
       
    29 // CTTSPlayer::NewL
       
    30 // Two-phased constructor.
       
    31 // -----------------------------------------------------------------------------
       
    32 //
       
    33 CTTSPlayer* CTTSPlayer::NewL( MTtsClientUtilityObserver* aObserver )
       
    34     {
       
    35     CTTSPlayer* self = new (ELeave) CTTSPlayer( aObserver );
       
    36     CleanupStack::PushL( self );
       
    37     self->ConstructL();
       
    38     CleanupStack::Pop( self );
       
    39     return self;
       
    40     }   
       
    41    
       
    42 // Destructor       
       
    43 CTTSPlayer::~CTTSPlayer()
       
    44     {
       
    45     RUBY_DEBUG0( "CTTSPlayer::~CTTSPlayer START" );
       
    46     
       
    47     Stop();
       
    48     
       
    49     RUBY_DEBUG0( "CTTSPlayer::~CTTSPlayer EXIT" );
       
    50     }
       
    51 
       
    52 // ---------------------------------------------------------
       
    53 // CTTSPlayer::PlayL
       
    54 // ---------------------------------------------------------
       
    55 //      
       
    56 void CTTSPlayer::PlayL( const TDesC& aText, TInt aPosition,
       
    57                         TInt aMaxLength, const TDesC& aSegment )
       
    58     {
       
    59     RUBY_DEBUG_BLOCK( "CTTSPlayer::PlayL" );
       
    60     
       
    61     Stop();
       
    62     
       
    63     CreateSegmentsL( aText, aPosition, aMaxLength, aSegment );
       
    64     
       
    65     iTtsData->iParsedText->SetTextL( aText );
       
    66         
       
    67     // Opens and plays the parsed text
       
    68     iTtsData->iTts->OpenAndPlayParsedTextL( *iTtsData->iParsedText );    
       
    69     }
       
    70 
       
    71 // ---------------------------------------------------------
       
    72 // CTTSPlayer::PlayL
       
    73 // ---------------------------------------------------------
       
    74 //      
       
    75 void CTTSPlayer::PlayL( const TDesC& aText, TInt aPosition,
       
    76                         TInt aMaxLength, const TDesC& aSegment,
       
    77                         const TDesC& aNewSegment1, const TDesC& aNewSegment2,
       
    78                         const TDesC& aNewSegment3  )
       
    79     {
       
    80     RUBY_DEBUG_BLOCK( "CTTSPlayer::PlayL" );
       
    81     
       
    82     Stop();
       
    83     
       
    84     HBufC* text = HBufC::NewLC( aText.Length() + aNewSegment1.Length() +
       
    85                                 aNewSegment2.Length() + aNewSegment3.Length() );
       
    86     
       
    87     TPtr textPtr = text->Des();
       
    88     textPtr.Append( aText );
       
    89                                   
       
    90     CreateSegmentsL( *text, aPosition, aMaxLength, aSegment );
       
    91     
       
    92     for ( int i = KMaxBasicSegments; i < KMaxBasicSegments + KMaxEnhancedSegments; ++i )
       
    93         {
       
    94         iTtsData->iStyle[i] = new (ELeave) TTtsStyle();
       
    95         iTtsData->iStyleId[i] = new (ELeave) TTtsStyleID( 0 );
       
    96         iTtsData->iSegment[i] = new (ELeave) TTtsSegment( 0 );
       
    97         
       
    98         iTtsData->iStyle[i]->iLanguage = User::Language();
       
    99         
       
   100         *(iTtsData->iStyleId[i]) = iTtsData->iTts->AddStyleL( *(iTtsData->iStyle[i]) );
       
   101     
       
   102         iTtsData->iSegment[i]->SetStyleID( *(iTtsData->iStyleId[i]) );
       
   103         }
       
   104     
       
   105     TInt location = KErrNone;
       
   106     
       
   107     textPtr.Append( aNewSegment1 );
       
   108         
       
   109     iTtsData->iSegment[3]->SetTextPtr( text->Mid( location, aNewSegment1.Length() ) );
       
   110 
       
   111     iTtsData->iParsedText->AddSegmentL( *(iTtsData->iSegment[3]) );
       
   112         
       
   113     if ( aNewSegment2 != KNullDesC )
       
   114         {
       
   115         location = text->Length();
       
   116         textPtr.Append( aNewSegment2 );
       
   117         
       
   118         iTtsData->iSegment[4]->SetTextPtr( text->Mid( location, aNewSegment2.Length() ) );
       
   119 
       
   120         iTtsData->iParsedText->AddSegmentL( *(iTtsData->iSegment[4]) );
       
   121         }
       
   122         
       
   123     if ( aNewSegment3 != KNullDesC )
       
   124         {
       
   125         location = text->Length();
       
   126         textPtr.Append( aNewSegment3 );
       
   127         
       
   128         iTtsData->iSegment[5]->SetTextPtr( text->Mid( location, aNewSegment3.Length() ) );
       
   129 
       
   130         iTtsData->iParsedText->AddSegmentL( *(iTtsData->iSegment[5]) );
       
   131         }
       
   132         
       
   133     iTtsData->iParsedText->SetTextL( *text );
       
   134            
       
   135     // Opens and plays the parsed text
       
   136     iTtsData->iTts->OpenAndPlayParsedTextL( *iTtsData->iParsedText );
       
   137     
       
   138     CleanupStack::PopAndDestroy( text );
       
   139     }
       
   140 
       
   141 // ---------------------------------------------------------
       
   142 // CTTSPlayer::Stop
       
   143 // ---------------------------------------------------------
       
   144 //          
       
   145 void CTTSPlayer::Stop()
       
   146     {
       
   147     RUBY_DEBUG0( "CTTSPlayer::Stop START" );
       
   148         
       
   149     if ( iTtsData )
       
   150         {
       
   151         if ( iTtsData->iTts )
       
   152             {
       
   153             iTtsData->iTts->Stop();
       
   154             iTtsData->iTts->Close();
       
   155             }
       
   156 
       
   157         delete iTtsData->iParsedText;
       
   158         iTtsData->iParsedText = NULL;
       
   159 
       
   160         delete iTtsData->iTts;
       
   161         iTtsData->iTts = NULL;
       
   162         
       
   163         for ( int i = 0; i < KMaxSegments; ++i )
       
   164             {
       
   165             delete iTtsData->iStyle[i];
       
   166             delete iTtsData->iStyleId[i];
       
   167             delete iTtsData->iSegment[i];
       
   168             }
       
   169 
       
   170         delete iTtsData;
       
   171         iTtsData = NULL;
       
   172         }
       
   173     
       
   174     RUBY_DEBUG0( "CTTSPlayer::Stop EXIT" );  
       
   175     }
       
   176 
       
   177 // ---------------------------------------------------------
       
   178 // CTTSPlayer::CTTSPlayer
       
   179 // ---------------------------------------------------------
       
   180 //              
       
   181 CTTSPlayer::CTTSPlayer( MTtsClientUtilityObserver* aObserver )
       
   182  : iObserver( aObserver )
       
   183     {
       
   184     }
       
   185 
       
   186 // ---------------------------------------------------------
       
   187 // CTTSPlayer::ConstructL
       
   188 // ---------------------------------------------------------
       
   189 //           
       
   190 void CTTSPlayer::ConstructL()
       
   191     {
       
   192     RUBY_DEBUG_BLOCK( "CTTSPlayer::ConstructL" );
       
   193     }
       
   194 
       
   195 // ---------------------------------------------------------
       
   196 // CTTSPlayer::CreateSegmentsL
       
   197 // ---------------------------------------------------------
       
   198 //      
       
   199 void CTTSPlayer::CreateSegmentsL( const TDesC& aText, TInt aPosition,
       
   200                                   TInt aMaxLength, const TDesC& aSegment )
       
   201     {
       
   202     RUBY_DEBUG_BLOCK( "CTTSPlayer::CreateSegmentsL" );
       
   203     
       
   204     TInt leftLength = aText.Length();
       
   205     TInt midLength = KErrNone;
       
   206     TInt rightLength = KErrNone;
       
   207     
       
   208     TInt location = KErrNotFound;    
       
   209     if ( aSegment != KNullDesC )
       
   210         {
       
   211         location = aText.Right( aText.Length() - aPosition ).FindC( aSegment ) + aPosition;
       
   212         
       
   213         if ( location != aPosition )
       
   214             {
       
   215             location = aPosition + aMaxLength - aSegment.Length();
       
   216             }
       
   217         
       
   218         // Calculate segment lengths
       
   219         if ( location == KErrNone )
       
   220             {
       
   221             leftLength = aSegment.Length();
       
   222             rightLength = aText.Length() - aSegment.Length();
       
   223             }
       
   224         else if ( location + aSegment.Length() < aText.Length() )
       
   225             {
       
   226             leftLength = location;
       
   227             midLength = aSegment.Length();
       
   228             rightLength = aText.Length() - location - aSegment.Length();
       
   229             }
       
   230         else if ( location != KErrNotFound )
       
   231             {
       
   232             leftLength = aText.Length() - aSegment.Length();
       
   233             rightLength = aSegment.Length();
       
   234             }
       
   235         }
       
   236     
       
   237     iTtsData = new (ELeave) TTtsData;
       
   238     
       
   239     iTtsData->iTts = CTtsUtility::NewL( *iObserver );
       
   240     
       
   241     iTtsData->iParsedText = CTtsParsedText::NewL();
       
   242 
       
   243     for ( int i = 0; i < KMaxBasicSegments; ++i )
       
   244         {
       
   245         iTtsData->iStyle[i] = new (ELeave) TTtsStyle();
       
   246         iTtsData->iStyleId[i] = new (ELeave) TTtsStyleID( 0 );
       
   247         iTtsData->iSegment[i] = new (ELeave) TTtsSegment( 0 );
       
   248         
       
   249         iTtsData->iStyle[i]->iLanguage = User::Language();
       
   250         
       
   251         *(iTtsData->iStyleId[i]) = iTtsData->iTts->AddStyleL( *(iTtsData->iStyle[i]) );
       
   252     
       
   253         iTtsData->iSegment[i]->SetStyleID( *(iTtsData->iStyleId[i]) );
       
   254         }
       
   255     
       
   256     // Create segments
       
   257     iTtsData->iSegment[0]->SetTextPtr( aText.Left( leftLength ) );
       
   258 
       
   259     iTtsData->iParsedText->AddSegmentL( *(iTtsData->iSegment[0]) );
       
   260 
       
   261     if ( midLength != KErrNone )
       
   262         {
       
   263         iTtsData->iSegment[1]->SetTextPtr( aText.Mid( location, midLength ) );
       
   264         iTtsData->iSegment[2]->SetTextPtr( aText.Right( rightLength ) );
       
   265 
       
   266         iTtsData->iParsedText->AddSegmentL( *(iTtsData->iSegment[1]) );
       
   267         iTtsData->iParsedText->AddSegmentL( *(iTtsData->iSegment[2]) );
       
   268         }
       
   269     else if ( rightLength != KErrNone )
       
   270         {
       
   271         iTtsData->iSegment[1]->SetTextPtr( aText.Right( rightLength ) );
       
   272 
       
   273         iTtsData->iParsedText->AddSegmentL( *(iTtsData->iSegment[1]) );
       
   274         }
       
   275     }
       
   276     
       
   277 // -----------------------------------------------------------------------------
       
   278 // CTTSPlayer::TTtsData
       
   279 // Initializes everything to zero.
       
   280 // -----------------------------------------------------------------------------
       
   281 //
       
   282 CTTSPlayer::TTtsData::TTtsData() 
       
   283  : iTts( NULL ), iParsedText( NULL )
       
   284     {
       
   285     for ( int i = 0; i < KMaxSegments; ++i )
       
   286         {
       
   287         iStyle[i] = NULL;
       
   288         iStyleId[i] = NULL;
       
   289         iSegment[i] = NULL;
       
   290         }
       
   291     }
       
   292     
       
   293 // End of File
       
   294