srsf/devtts/src/devttsaudio.cpp
branchRCL_3
changeset 23 e36f3802f733
parent 0 bf1d17376201
equal deleted inserted replaced
22:cad71a31b7fc 23:e36f3802f733
       
     1 /*
       
     2 * Copyright (c) 2004-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:  Audio playback wrapper for DevTTS.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include "devttsaudio.h"
       
    21 #include "devttscenrep.h"
       
    22 #include <AudioPreference.h>
       
    23 #include <AudioOutput.h>
       
    24 #include <mmfbase.h>
       
    25 #include "rubydebug.h"
       
    26 
       
    27 // CONSTANTS
       
    28 // Sample rates
       
    29 const TUint KSampleRate8k = 8000;
       
    30 const TUint KSampleRate11k = 11025;
       
    31 const TUint KSampleRate16k = 16000;
       
    32 const TUint KSampleRate22k = 22050;
       
    33 const TUint KSampleRate32k = 32000;
       
    34 const TUint KSampleRate44k = 44100;
       
    35 const TUint KSampleRate48k = 48000;
       
    36 const TUint KSampleRate88k = 88200;
       
    37 const TUint KSampleRate96k = 96000;
       
    38 
       
    39 // Bits per sample
       
    40 const TUint KBitsPerSample8 = 8;
       
    41 const TUint KBitsPerSample16 = 16;
       
    42 
       
    43 // Volume min
       
    44 const TUint KMinVolume = 0;
       
    45 
       
    46 const TUint KBalanceMiddle = 100;
       
    47 
       
    48 // Default audio priority
       
    49 
       
    50 // ============================ MEMBER FUNCTIONS ===============================
       
    51 
       
    52 // -----------------------------------------------------------------------------
       
    53 // CDevTTSAudio::CDevTTSAudio
       
    54 // C++ default constructor can NOT contain any code, that
       
    55 // might leave.
       
    56 // -----------------------------------------------------------------------------
       
    57 //
       
    58 CDevTTSAudio::CDevTTSAudio( MDevTTSAudioObserver& aObserver, TInt aPriority ) : 
       
    59         iObserver( aObserver ), 
       
    60         iDevsound( NULL ),
       
    61         iBuffer( NULL ),
       
    62         iPriority( aPriority ),
       
    63         iBalance( KDevTTSBalanceMiddle ),
       
    64         iVolume( KMinVolume )
       
    65     {
       
    66     // Nothing
       
    67     }
       
    68 
       
    69 // -----------------------------------------------------------------------------
       
    70 // CDevTTSAudio::ConstructL
       
    71 // Symbian 2nd phase constructor can leave.
       
    72 // -----------------------------------------------------------------------------
       
    73 //
       
    74 void CDevTTSAudio::ConstructL()
       
    75     {
       
    76     RUBY_DEBUG_BLOCK( "CDevTTSAudio::ConstructL" );
       
    77 
       
    78     // Construct DevSound object
       
    79     iDevsound = CMMFDevSound::NewL();
       
    80     
       
    81 #ifndef __WINS__// This is not supported in emulator
       
    82     iAudioOutput = CAudioOutput::NewL( *iDevsound );
       
    83 #endif
       
    84 
       
    85     // Handler for the CenRep settings
       
    86     CDevTtsCenRep* cenrep = CDevTtsCenRep::NewL();
       
    87     CleanupStack::PushL( cenrep ); 
       
    88     
       
    89     // Set volume to default
       
    90     RUBY_DEBUG0( "CDevTTSAudio::ConstructL Set volume" );
       
    91     SetVolume( cenrep->Volume( MaxVolume() ) );
       
    92     
       
    93     CleanupStack::PopAndDestroy( cenrep );
       
    94 
       
    95     // Set default playback priority
       
    96     SetAudioPriority( KAudioPriorityVoiceDial, 
       
    97                       (TDevTTSAudioPreference) KAudioPrefVocosPlayback );
       
    98     }
       
    99 
       
   100 // -----------------------------------------------------------------------------
       
   101 // CDevTTSAudio::NewL
       
   102 // Two-phased constructor.
       
   103 // -----------------------------------------------------------------------------
       
   104 //
       
   105 CDevTTSAudio* CDevTTSAudio::NewL( MDevTTSAudioObserver& aObserver, 
       
   106                                   TInt aPriority )
       
   107     {
       
   108     CDevTTSAudio* self = new( ELeave ) CDevTTSAudio( aObserver, aPriority );
       
   109  
       
   110     CleanupStack::PushL( self );
       
   111     self->ConstructL();
       
   112     CleanupStack::Pop( self );
       
   113     
       
   114     return self;
       
   115     }
       
   116 
       
   117 
       
   118 // -----------------------------------------------------------------------------
       
   119 // CDevTTSAudio::~CDevTTSAudio
       
   120 // Destructor
       
   121 // -----------------------------------------------------------------------------
       
   122 //
       
   123 CDevTTSAudio::~CDevTTSAudio()
       
   124     {
       
   125     delete iAudioOutput; 
       
   126     delete iDevsound; 
       
   127     }
       
   128 
       
   129 // -----------------------------------------------------------------------------
       
   130 // CDevTTSAudio::InitializeL
       
   131 // Initializes audio playback device.
       
   132 // -----------------------------------------------------------------------------
       
   133 //
       
   134 void CDevTTSAudio::InitializeL() 
       
   135     {
       
   136     iDevsound->InitializeL(*(this), EMMFStatePlaying );
       
   137     }
       
   138     
       
   139 // -----------------------------------------------------------------------------
       
   140 // CDevTTSAudio::ConfigL
       
   141 // Configures audio playback device.
       
   142 // -----------------------------------------------------------------------------
       
   143 //
       
   144 void CDevTTSAudio::ConfigL( TUint aSamplingRate, TUint aBitsPerSample, 
       
   145                             TBool aStereo, TUint aBufferSize )
       
   146     {
       
   147     // Resolve configuration
       
   148     iDevCap = iDevsound->Config();
       
   149     
       
   150     // Set the preferred buffer size
       
   151     iDevCap.iBufferSize = aBufferSize;
       
   152    
       
   153     // Resolve sampling rate
       
   154     switch ( aSamplingRate )
       
   155         {
       
   156         case KSampleRate8k:
       
   157             iDevCap.iRate = EMMFSampleRate8000Hz;
       
   158             break;
       
   159             
       
   160         case KSampleRate11k:
       
   161             iDevCap.iRate = EMMFSampleRate11025Hz;
       
   162             break;
       
   163             
       
   164         case KSampleRate16k:
       
   165             iDevCap.iRate = EMMFSampleRate16000Hz;
       
   166             break;
       
   167             
       
   168         case KSampleRate22k:
       
   169             iDevCap.iRate = EMMFSampleRate22050Hz;
       
   170             break;
       
   171             
       
   172         case KSampleRate32k:
       
   173             iDevCap.iRate = EMMFSampleRate32000Hz;
       
   174             break;
       
   175             
       
   176         case KSampleRate44k:
       
   177             iDevCap.iRate = EMMFSampleRate44100Hz;
       
   178             break;
       
   179             
       
   180         case KSampleRate48k:
       
   181             iDevCap.iRate = EMMFSampleRate48000Hz;
       
   182             break;
       
   183             
       
   184         case KSampleRate88k:
       
   185             iDevCap.iRate = EMMFSampleRate88200Hz;
       
   186             break;
       
   187             
       
   188         case KSampleRate96k:
       
   189             iDevCap.iRate = EMMFSampleRate96000Hz;
       
   190             break;
       
   191             
       
   192         default:
       
   193             User::Leave(KErrNotSupported);
       
   194             break;
       
   195         }
       
   196     
       
   197     // Resolve bits per sample
       
   198     switch ( aBitsPerSample )
       
   199         {
       
   200         case KBitsPerSample8:
       
   201             iDevCap.iEncoding = EMMFSoundEncoding8BitPCM;
       
   202             break;
       
   203             
       
   204         case KBitsPerSample16:
       
   205             iDevCap.iEncoding = EMMFSoundEncoding16BitPCM;
       
   206             break;
       
   207             
       
   208         default:
       
   209             User::Leave(KErrNotSupported);
       
   210             break;
       
   211         }
       
   212     
       
   213     // Resolve mono/stereo
       
   214     if ( aStereo )
       
   215         {
       
   216         iDevCap.iChannels = EMMFStereo;
       
   217         }
       
   218     else
       
   219         {
       
   220         iDevCap.iChannels = EMMFMono;
       
   221         }
       
   222     
       
   223     // Set DevSound configuration in place
       
   224     iDevsound->SetConfigL( iDevCap );
       
   225     
       
   226     // Set the audio priority settings to default
       
   227     iAudioPriority.iPriority = KAudioPriorityVoiceDial;
       
   228     iAudioPriority.iPref = ( TMdaPriorityPreference ) KAudioPrefVocosPlayback;
       
   229     iAudioPriority.iState = EMMFStatePlaying;
       
   230     
       
   231     iDevsound->SetPrioritySettings( iAudioPriority );
       
   232     }
       
   233 
       
   234 // -----------------------------------------------------------------------------
       
   235 // CDevTTSAudio::Clear
       
   236 // No counter part for initialize in DevSound. Function is in this class just
       
   237 // for future convenience.
       
   238 // -----------------------------------------------------------------------------
       
   239 //
       
   240 void CDevTTSAudio::Clear()
       
   241     {
       
   242     // Nothing
       
   243     }
       
   244 
       
   245 // -----------------------------------------------------------------------------
       
   246 // CDevTTSAudio::PlayInitL
       
   247 // Forwards call to DevSound.
       
   248 // -----------------------------------------------------------------------------
       
   249 //
       
   250 void CDevTTSAudio::PlayInitL()
       
   251     {
       
   252     iDevsound->PlayInitL();
       
   253     }
       
   254 
       
   255 // -----------------------------------------------------------------------------
       
   256 // CDevTTSAudio::PlayData
       
   257 // Forwards call to DevSound.
       
   258 // -----------------------------------------------------------------------------
       
   259 //
       
   260 void CDevTTSAudio::PlayData( TBool aLastBuffer ) 
       
   261     {
       
   262     iBuffer->SetLastBuffer( aLastBuffer );
       
   263     iDevsound->PlayData();
       
   264     }
       
   265 
       
   266 // -----------------------------------------------------------------------------
       
   267 // CDevTTSAudio::SetAudioPriority
       
   268 // Resolves arguments and forwards call to DevSound.
       
   269 // -----------------------------------------------------------------------------
       
   270 //
       
   271 void CDevTTSAudio::SetAudioPriority( TInt aPriority, TDevTTSAudioPreference aPref )
       
   272     {
       
   273     // Set the priority settings
       
   274     iPriority = aPriority;
       
   275     iAudioPriority.iPriority = aPriority;
       
   276     iAudioPriority.iState = EMMFStatePlaying;
       
   277     iAudioPriority.iPref = ( TMdaPriorityPreference ) aPref;
       
   278   
       
   279     iDevsound->SetPrioritySettings( iAudioPriority );
       
   280     }
       
   281 
       
   282 // -----------------------------------------------------------------------------
       
   283 // CDevTTSAudio::SetAudioOutputL
       
   284 // 
       
   285 // -----------------------------------------------------------------------------
       
   286 // 
       
   287 void CDevTTSAudio::SetAudioOutputL( TInt aAudioOutput )
       
   288     {
       
   289     RUBY_DEBUG1( "Setting output to %d", aAudioOutput );
       
   290     
       
   291     if ( iAudioOutput )
       
   292         {
       
   293         iAudioOutput->SetAudioOutputL( 
       
   294             (CAudioOutput::TAudioOutputPreference)aAudioOutput );
       
   295         }
       
   296     }
       
   297 
       
   298 // -----------------------------------------------------------------------------
       
   299 // CDevTTSAudio::Balance
       
   300 // Forwards call to DevSound.
       
   301 // -----------------------------------------------------------------------------
       
   302 //
       
   303 TInt CDevTTSAudio::Balance()
       
   304     {
       
   305     return iBalance;
       
   306     }
       
   307 
       
   308 // -----------------------------------------------------------------------------
       
   309 // CDevTTSAudio::SetBalance
       
   310 // Checks parameter validity and forwards call to DevSound.
       
   311 // -----------------------------------------------------------------------------
       
   312 //
       
   313 void CDevTTSAudio::SetBalance( TInt aBalance )
       
   314     {
       
   315     TInt right( 0 );
       
   316     TInt left( 0 );
       
   317 
       
   318     if ( ( aBalance > KDevTTSBalanceRight ) || ( aBalance < KDevTTSBalanceLeft ) )
       
   319         {
       
   320         return;
       
   321         }
       
   322     iBalance = aBalance;
       
   323 
       
   324     if ( aBalance < 0 )
       
   325         {
       
   326         left = KBalanceMiddle;
       
   327         right = KBalanceMiddle + aBalance;
       
   328         }
       
   329     else if ( aBalance > 0 )
       
   330         {
       
   331         left = KBalanceMiddle - aBalance;
       
   332         right = KBalanceMiddle;
       
   333         }
       
   334     else
       
   335         {
       
   336         left = KBalanceMiddle;
       
   337         right = KBalanceMiddle;
       
   338         }
       
   339 
       
   340     RUBY_DEBUG2( "CDevTTSAudio::SetBalance Balance setting left: %d, right: %d", left, right );
       
   341 
       
   342     TRAPD( error, iDevsound->SetPlayBalanceL( left, right ) );
       
   343     if ( error == KErrNone )
       
   344         {
       
   345         RUBY_DEBUG0( "CDevTTSAudio::SetBalance Balance set" );
       
   346         }
       
   347     else
       
   348         {
       
   349         RUBY_DEBUG1( "CDevTTSAudio::SetBalance Balance set error: %d", error );
       
   350         }
       
   351     }
       
   352 
       
   353 // -----------------------------------------------------------------------------
       
   354 // CDevTTSAudio::SetVolume
       
   355 // Forwards call to DevSound.
       
   356 // -----------------------------------------------------------------------------
       
   357 //
       
   358 void CDevTTSAudio::SetVolume( TInt aVolume )
       
   359     {
       
   360     if ( ( aVolume > iDevsound->MaxVolume() ) || ( aVolume < KMinVolume ) )
       
   361         {
       
   362         return;
       
   363         }
       
   364     
       
   365     iVolume = aVolume;
       
   366     RUBY_DEBUG1( "CDevTTSAudio::SetVolume %i", aVolume ); 
       
   367 
       
   368     iDevsound->SetVolume( aVolume );
       
   369     }
       
   370 
       
   371 // -----------------------------------------------------------------------------
       
   372 // CDevTTSAudio::SetVolumeRamp
       
   373 // Forwards call to DevSound.
       
   374 // -----------------------------------------------------------------------------
       
   375 //
       
   376 void CDevTTSAudio::SetVolumeRamp( const TTimeIntervalMicroSeconds& aRampDuration )
       
   377     {
       
   378     iDevsound->SetVolumeRamp( aRampDuration );
       
   379     }
       
   380 
       
   381 // -----------------------------------------------------------------------------
       
   382 // CDevTTSAudio::MaxVolume
       
   383 // Returns DevSound maximum volume.
       
   384 // -----------------------------------------------------------------------------
       
   385 //
       
   386 TInt CDevTTSAudio::MaxVolume()
       
   387     {
       
   388     return iDevsound->MaxVolume();
       
   389     }
       
   390 
       
   391 // -----------------------------------------------------------------------------
       
   392 // CDevTTSAudio::Volume
       
   393 // Forwards call to DevSound.
       
   394 // -----------------------------------------------------------------------------
       
   395 //
       
   396 TInt CDevTTSAudio::Volume()
       
   397     {
       
   398     return iVolume;
       
   399     }
       
   400 
       
   401 // -----------------------------------------------------------------------------
       
   402 // CDevTTSAudio::Stop
       
   403 // Forwards call to DevSound.
       
   404 // -----------------------------------------------------------------------------
       
   405 //
       
   406 void CDevTTSAudio::Stop()
       
   407     {
       
   408     iDevsound->Stop();
       
   409     }
       
   410 
       
   411 // -----------------------------------------------------------------------------
       
   412 // CDevTTSAudio::Pause
       
   413 // Forwards call to DevSound.
       
   414 // -----------------------------------------------------------------------------
       
   415 //
       
   416 void CDevTTSAudio::Pause()
       
   417     {
       
   418     iDevsound->Pause();
       
   419     }
       
   420 
       
   421 
       
   422 // ========================== MDevSoundObserver CALLBACKS ======================
       
   423 
       
   424 // -----------------------------------------------------------------------------
       
   425 // CDevTTSAudio::InitializeComplete
       
   426 // Forwards callback
       
   427 // -----------------------------------------------------------------------------
       
   428 //
       
   429 void CDevTTSAudio::InitializeComplete( TInt aError ) 
       
   430     {
       
   431     iObserver.MdtaoInitializeComplete( aError );
       
   432     }
       
   433 
       
   434 // -----------------------------------------------------------------------------
       
   435 // CDevTTSAudio::ToneFinished
       
   436 // Not needed when using only waveform playback
       
   437 // -----------------------------------------------------------------------------
       
   438 //
       
   439 void CDevTTSAudio::ToneFinished(TInt /*aError*/)
       
   440     {
       
   441     }
       
   442 
       
   443 // -----------------------------------------------------------------------------
       
   444 // CDevTTSAudio::BufferToBeFilled
       
   445 // Forwards callback
       
   446 // -----------------------------------------------------------------------------
       
   447 //
       
   448 void CDevTTSAudio::BufferToBeFilled( CMMFBuffer* aBuffer )
       
   449     {
       
   450     iBuffer = aBuffer;
       
   451     CMMFDataBuffer* buffer = STATIC_CAST( CMMFDataBuffer*, aBuffer );
       
   452     
       
   453     iObserver.MdtaoBufferToBeFilled( buffer->Data(), aBuffer->RequestSize() );
       
   454     }
       
   455 
       
   456 // -----------------------------------------------------------------------------
       
   457 // CDevTTSAudio::PlayError
       
   458 // Forwards callback.
       
   459 // Spec says that KErrNone is used when audio playback is ok. In practise it
       
   460 // seems that KErrUnderflow means the same thing.
       
   461 // -----------------------------------------------------------------------------
       
   462 //
       
   463 void CDevTTSAudio::PlayError( TInt aError )
       
   464     {
       
   465     if ( aError == KErrNone || aError == KErrUnderflow ) 
       
   466         {
       
   467         iObserver.MdtaoPlayFinished( KErrNone );
       
   468         }
       
   469     else
       
   470         {
       
   471         iObserver.MdtaoPlayFinished( aError );
       
   472         }
       
   473     }
       
   474 
       
   475 // -----------------------------------------------------------------------------
       
   476 // CDevTTSAudio::BufferToBeEmptied
       
   477 // Not needed when using only waveform playback
       
   478 // -----------------------------------------------------------------------------
       
   479 //
       
   480 void CDevTTSAudio::BufferToBeEmptied( CMMFBuffer* /*aBuffer*/ )
       
   481     {
       
   482     }
       
   483 
       
   484 // -----------------------------------------------------------------------------
       
   485 // CDevTTSAudio::RecordError
       
   486 // Not needed when using only waveform playback
       
   487 // -----------------------------------------------------------------------------
       
   488 //
       
   489 void CDevTTSAudio::RecordError( TInt /*aError*/ )
       
   490     {
       
   491     }
       
   492 
       
   493 // -----------------------------------------------------------------------------
       
   494 // CDevTTSAudio::ConvertError
       
   495 // Not needed when using only waveform playback
       
   496 // -----------------------------------------------------------------------------
       
   497 //
       
   498 void CDevTTSAudio::ConvertError( TInt /*aError*/ )
       
   499     {
       
   500     }
       
   501 
       
   502 // -----------------------------------------------------------------------------
       
   503 // CDevTTSAudio::DeviceMessage
       
   504 // Not needed, no custom commands issued to audio hw device
       
   505 // -----------------------------------------------------------------------------
       
   506 //
       
   507 void CDevTTSAudio::DeviceMessage( TUid /*aMessageType*/, const TDesC8& /*aMsg*/ )
       
   508     {
       
   509     }
       
   510 
       
   511 // -----------------------------------------------------------------------------
       
   512 // CDevTTSAudio::SendEventToClient
       
   513 // Called when an attempt to acquire a sound device is rejected by audio policy 
       
   514 // server
       
   515 // -----------------------------------------------------------------------------
       
   516 //
       
   517 void CDevTTSAudio::SendEventToClient( const TMMFEvent& /*aEvent*/ )
       
   518     {
       
   519     // Inform our own observer
       
   520     iObserver.MdtaoPlayFinished( KErrCancel );
       
   521     }
       
   522 
       
   523 // End of File