voiceui/voiceuivoicerecognition/src/vuictoneplayer.cpp
branchRCL_3
changeset 19 e36f3802f733
equal deleted inserted replaced
18:cad71a31b7fc 19:e36f3802f733
       
     1 /*
       
     2 * Copyright (c) 2004-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:  Handles the tone playing for the VoiceUIRecognition
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include <aknsoundsystem.h>
       
    21 #include <aknappui.h>
       
    22 #include <AccessoryServer.h>        //RAccessoryServer
       
    23 #include <AccessoryConnection.h>    //RAccessoryConnection
       
    24 
       
    25 #include <centralrepository.h>
       
    26 
       
    27 #include <StringLoader.h>
       
    28 #include <AudioPreference.h>
       
    29 
       
    30 #include <srsfdomaincrkeys.h>
       
    31 
       
    32 #include <vuivoicerecognition.rsg>
       
    33 #include "vuictoneplayer.h"
       
    34 
       
    35 #include "rubydebug.h"
       
    36 
       
    37 // CONSTANTS
       
    38 
       
    39 // Maximum value of the volume setting in Central Repository
       
    40 const TInt KMaxRepositoryVolume = 10;
       
    41 
       
    42 
       
    43 // ================= MEMBER FUNCTIONS =======================
       
    44 
       
    45 // ---------------------------------------------------------
       
    46 // CTonePlayer* CTonePlayer::NewL
       
    47 // Two-phased constructor.
       
    48 // ---------------------------------------------------------
       
    49 //
       
    50 CTonePlayer* CTonePlayer::NewL()
       
    51     {
       
    52     CTonePlayer* self = new (ELeave) CTonePlayer;    
       
    53     CleanupStack::PushL( self );
       
    54     self->ConstructL();
       
    55     CleanupStack::Pop( self );
       
    56     return self;
       
    57     }
       
    58     
       
    59 // Destructor
       
    60 CTonePlayer::~CTonePlayer()
       
    61     {
       
    62     RUBY_DEBUG0( "CTonePlayer::~CTonePlayer START" );
       
    63 
       
    64     if ( iAudioPlayer )
       
    65         {
       
    66         iAudioPlayer->Close();
       
    67         delete iAudioPlayer;
       
    68         }
       
    69 
       
    70     delete iStartSound;
       
    71     delete iConfirmationSound;
       
    72     delete iBtStartSound;
       
    73 
       
    74     iObserver = NULL;
       
    75     
       
    76     RUBY_DEBUG0( "CTonePlayer::~CTonePlayer EXIT" );
       
    77     }
       
    78 
       
    79 // -----------------------------------------------------------------------------
       
    80 // CTonePlayer::CTonePlayer
       
    81 // C++ default constructor can NOT contain any code, that
       
    82 // might leave.
       
    83 // -----------------------------------------------------------------------------
       
    84 //
       
    85 CTonePlayer::CTonePlayer()
       
    86     {
       
    87     }
       
    88 
       
    89 // -----------------------------------------------------------------------------
       
    90 // CTonePlayer::ConstructL
       
    91 // Symbian 2nd phase constructor can leave.
       
    92 // -----------------------------------------------------------------------------
       
    93 //
       
    94 void CTonePlayer::ConstructL()
       
    95     {
       
    96     RUBY_DEBUG_BLOCK( "CTonePlayer::ConstructL" );
       
    97 
       
    98     iAudioPlayer = CMdaAudioPlayerUtility::NewL( *this,
       
    99                                                  KAudioPriorityVoiceDial, 
       
   100                                                  TMdaPriorityPreference( KAudioPrefVoiceStarting ) );
       
   101 
       
   102     iVolume = KErrGeneral;
       
   103     
       
   104     CRepository* client = CRepository::NewLC( KCRUidSRSFSettings );
       
   105     client->Get( KSRSFPlaybackVolume, iVolume );
       
   106     CleanupStack::PopAndDestroy( client );
       
   107 
       
   108     iStartSound = StringLoader::LoadL( R_VOICE_START_SOUND_PATH );
       
   109     iConfirmationSound = StringLoader::LoadL( R_VOICE_CONFIRMATION_SOUND_PATH );
       
   110     iBtStartSound = StringLoader::LoadL( R_VOICE_BT_START_SOUND_PATH );
       
   111     iBTAcc = EFalse;
       
   112     
       
   113     ChekcAccesoryStatusL();
       
   114     }
       
   115   
       
   116  // ---------------------------------------------------------
       
   117 // CTonePlayer::RegisterObserver
       
   118 // Sets the observer
       
   119 // ---------------------------------------------------------
       
   120 //  
       
   121 void CTonePlayer::RegisterObserver( MMdaAudioPlayerCallback* aObserver )
       
   122     {
       
   123     RUBY_DEBUG0( "CTonePlayer::RegisterObserver START" );
       
   124     
       
   125     iObserver = aObserver;
       
   126     
       
   127     RUBY_DEBUG0( "CTonePlayer::RegisterObserver EXIT" );
       
   128     }
       
   129 
       
   130 // ---------------------------------------------------------
       
   131 // CTonePlayer::InitToneL
       
   132 // Perform initialization for Playing the appropriate tone
       
   133 // ---------------------------------------------------------
       
   134 //
       
   135 void CTonePlayer::InitToneL( TInt aSid )
       
   136    {
       
   137     RUBY_DEBUG_BLOCK( "CTonePlayer::PlayToneInitL" );
       
   138     
       
   139     iAudioPlayer->Stop();
       
   140     iAudioPlayer->Close();
       
   141 
       
   142     if ( aSid == EAvkonSIDNameDiallerStartTone )
       
   143         {
       
   144         RUBY_DEBUG0( "CTonePlayer::InitToneL - case: EAvkonSIDNameDiallerStartTone" );
       
   145         TInt err = KErrNone;
       
   146         if ( iBTAcc )
       
   147             {
       
   148             TRAP( err, iAudioPlayer->OpenFileL( *iBtStartSound ) );
       
   149             }
       
   150         else
       
   151             {
       
   152             TRAP( err, iAudioPlayer->OpenFileL( *iStartSound ) );
       
   153             }
       
   154         if ( err )
       
   155             {
       
   156             RUBY_ERROR1( "CTonePlayer::InitToneL - err [%d]", err );
       
   157                     
       
   158             // Cannot play sound, but observer still needs to be notified
       
   159             iObserver->MapcInitComplete( err, 0 );
       
   160             }
       
   161         }
       
   162     else if ( aSid == EAvkonSIDConfirmationTone )
       
   163         {
       
   164         RUBY_DEBUG0( "CTonePlayer::InitToneL - case: EAvkonSIDConfirmationTone" );
       
   165 
       
   166         TRAPD( err, iAudioPlayer->OpenFileL( *iConfirmationSound ) );
       
   167         if ( err )
       
   168             {
       
   169             RUBY_ERROR1( "CTonePlayer::InitToneL - err [%d]", err );
       
   170                     
       
   171             // Cannot play sound, but observer still needs to be notified
       
   172             iObserver->MapcInitComplete( err, 0 );
       
   173             }
       
   174         }
       
   175     }
       
   176 
       
   177 // ---------------------------------------------------------
       
   178 // CTonePlayer::PlayTone
       
   179 // Plays the appropriate tone
       
   180 // ---------------------------------------------------------
       
   181 //
       
   182 void CTonePlayer::PlayTone( TInt aSid )
       
   183     {
       
   184     RUBY_DEBUG0( "CTonePlayer::PlayTone START" );
       
   185     
       
   186     switch ( aSid )
       
   187         {
       
   188         case EAvkonSIDNameDiallerStartTone:
       
   189         case EAvkonSIDConfirmationTone:
       
   190             {
       
   191             iAudioPlayer->SetVolume( ScaledVolume() );
       
   192             iAudioPlayer->Play();
       
   193             break;
       
   194             }
       
   195             
       
   196         case EAvkonSIDNameDiallerAbortTone:
       
   197         case EAvkonSIDNameDiallerErrorTone:
       
   198             {
       
   199             if ( iAvkonAppUiBase )
       
   200                 {               
       
   201                 iAudioPlayer->Stop();
       
   202                 iAudioPlayer->Close();
       
   203                 
       
   204                 iAvkonAppUiBase->KeySounds()->PlaySound( aSid );
       
   205                 }
       
   206 
       
   207             MapcPlayComplete( KErrNone );
       
   208             break;
       
   209             }
       
   210         
       
   211         default:
       
   212             break;
       
   213         }
       
   214     
       
   215     RUBY_DEBUG0( "CTonePlayer::PlayTone EXIT" );
       
   216     }
       
   217 
       
   218 // ---------------------------------------------------------------------------
       
   219 // CTonePlayer::MapcInitComplete
       
   220 // CMdaAudioPlayerUtility initialization complete
       
   221 // ---------------------------------------------------------------------------
       
   222 //
       
   223 void CTonePlayer::MapcInitComplete( TInt aError,
       
   224                                     const TTimeIntervalMicroSeconds& aDuration )
       
   225     {
       
   226     RUBY_DEBUG0( "CTonePlayer::MapcInitComplete START" );
       
   227 
       
   228     iObserver->MapcInitComplete( aError, aDuration );
       
   229     
       
   230     RUBY_DEBUG0( "CTonePlayer::MapcInitComplete EXIT" );
       
   231     }
       
   232 
       
   233 // ---------------------------------------------------------------------------
       
   234 // CTonePlayer::MapcPlayComplete
       
   235 // Playback complete, notify observer
       
   236 // ---------------------------------------------------------------------------
       
   237 //
       
   238 void CTonePlayer::MapcPlayComplete( TInt aError )
       
   239     {
       
   240     RUBY_DEBUG0( "CTonePlayer::MapcPlayComplete START" );
       
   241 
       
   242     iAudioPlayer->Stop();
       
   243     iAudioPlayer->Close();
       
   244     iObserver->MapcPlayComplete( aError );
       
   245 
       
   246     RUBY_DEBUG0( "CTonePlayer::MapcPlayComplete EXIT" );
       
   247     }
       
   248 
       
   249 // ---------------------------------------------------------------------------
       
   250 // CTonePlayer::ScaledVolume
       
   251 // ---------------------------------------------------------------------------
       
   252 //    
       
   253 TInt CTonePlayer::ScaledVolume()
       
   254     {
       
   255     TInt volume = iAudioPlayer->MaxVolume();
       
   256     if ( iVolume >= 0 )
       
   257         {
       
   258         // Scale value
       
   259         volume = ( iVolume * volume ) / KMaxRepositoryVolume;
       
   260         }
       
   261         
       
   262     return volume;
       
   263     }
       
   264 
       
   265 // ---------------------------------------------------------
       
   266 // CTonePlayer::ChekcAccesoryStatus
       
   267 // ---------------------------------------------------------
       
   268 //  
       
   269 void CTonePlayer::ChekcAccesoryStatusL()
       
   270     {    
       
   271     // Connection to the accessory server
       
   272     RAccessoryServer accessoryServer;    
       
   273     // Subsession for server
       
   274     RAccessoryConnection accessoryConnection;    
       
   275     //Generic ID array for accessories
       
   276     TAccPolGenericIDArray accessoryArray;
       
   277     
       
   278     TInt err = KErrNone;
       
   279     //connect to accessory server
       
   280     err = accessoryServer.Connect();
       
   281     if ( err == KErrNone )
       
   282         {
       
   283         //create subsession to accessory server
       
   284         err = accessoryConnection.CreateSubSession( accessoryServer );
       
   285         if ( err == KErrNone )
       
   286             {
       
   287             accessoryConnection.GetAccessoryConnectionStatus( accessoryArray );
       
   288             
       
   289             TAccPolGenericID genId;
       
   290             for ( TInt i = 0; i < accessoryArray.Count(); i++ )
       
   291                 {
       
   292                 genId = accessoryArray.GetGenericIDL( i );
       
   293 
       
   294                 if ( genId.PhysicalConnectionCaps( KPCBluetooth ) && 
       
   295                     ( genId.DeviceTypeCaps( KDTHeadset ) || genId.DeviceTypeCaps( KDTCarKit ) ) )
       
   296                     {
       
   297                     iBTAcc = ETrue;
       
   298                     }
       
   299                 }
       
   300             }
       
   301         }
       
   302 
       
   303     accessoryConnection.CloseSubSession();
       
   304     accessoryServer.Close();
       
   305     }
       
   306 
       
   307 //  End of File
       
   308