srsf/speechsynthesis/client/src/speechsynthesis.cpp
branchRCL_3
changeset 19 e36f3802f733
equal deleted inserted replaced
18:cad71a31b7fc 19:e36f3802f733
       
     1 /*
       
     2 * Copyright (c) 2006-2008 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:  Speech synthesis server, client side code
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // @todo Add better file write support to Klatt and HQ plugins
       
    20 // @todo Add support for lipsync information
       
    21 
       
    22 #include <e32svr.h>
       
    23 #include <f32file.h> 
       
    24 #include <s32mem.h>
       
    25 #include <speechsynthesis.h>
       
    26 
       
    27 #include "speechsynthesisclientserver.h"
       
    28 
       
    29 // CONSTANTS
       
    30 
       
    31 const TInt KMajorVersion = 1;
       
    32 const TInt KMinorVersion = 0;
       
    33 const TInt KBuildNumber  = 1;
       
    34 
       
    35 // ----------------------------------------------------------------------------
       
    36 // StartServer
       
    37 // Server startup code
       
    38 // ----------------------------------------------------------------------------
       
    39 // 
       
    40 static TInt StartServer()
       
    41     {
       
    42     RProcess server;
       
    43 
       
    44     TInt error = server.Create( KSpeechSynthesisServerImg, KNullDesC );
       
    45 
       
    46     if ( !error )
       
    47         {
       
    48         // Server started
       
    49         TRequestStatus status;
       
    50         server.Rendezvous( status );
       
    51         
       
    52         if ( status != KRequestPending )
       
    53             {
       
    54             server.Kill( KErrGeneral ); // Abort startup
       
    55             }
       
    56         else
       
    57             {
       
    58             server.Resume();            // Logon OK - start the server
       
    59             }
       
    60             
       
    61         User::WaitForRequest( status ); // Wait for start or death
       
    62         
       
    63         error = ( server.ExitType() == EExitPanic ) ? KErrGeneral : status.Int();
       
    64         
       
    65         // Close handle
       
    66         server.Close();
       
    67         }
       
    68         
       
    69     return error;
       
    70     }
       
    71     
       
    72     
       
    73 // ----------------------------------------------------------------------------
       
    74 // RSpeechSynthesis::RSpeechSynthesis
       
    75 // Constructor
       
    76 // ----------------------------------------------------------------------------
       
    77 //
       
    78 EXPORT_C RSpeechSynthesis::RSpeechSynthesis()
       
    79     {
       
    80     // Nothing
       
    81     }
       
    82 
       
    83 // ----------------------------------------------------------------------------
       
    84 // RSpeechSynthesis::Open
       
    85 // Creates a session to server
       
    86 // ----------------------------------------------------------------------------
       
    87 //
       
    88 EXPORT_C TInt RSpeechSynthesis::Open()
       
    89     {
       
    90     const TInt KMessageSlots( 4 );  // 1 async, 1 sync, 1 cancel, 1 kernel
       
    91     
       
    92     const TVersion requiredServerVersion( KMajorVersion, 
       
    93                                           KMinorVersion, 
       
    94                                           KBuildNumber );
       
    95 
       
    96     // Try to create session with following parameters:
       
    97     // 1) Name of the server
       
    98     // 2) The lowest version of the server with which this client is compatible
       
    99     // 3) The number of message slots 
       
   100     TInt error = CreateSession( KSpeechSynthesisServerName, 
       
   101                                 requiredServerVersion, KMessageSlots );
       
   102     
       
   103     if ( error == KErrNotFound || error == KErrServerTerminated )
       
   104         {
       
   105         // Server not running -> try to start it
       
   106         error = StartServer();
       
   107         
       
   108         if ( error == KErrNone || error == KErrAlreadyExists )
       
   109             {
       
   110             // Server startup succeeded -> try to create session to it
       
   111             error = CreateSession( KSpeechSynthesisServerName, 
       
   112                                    requiredServerVersion, KMessageSlots );
       
   113 
       
   114             }
       
   115         }
       
   116 
       
   117     return error;
       
   118     }
       
   119     
       
   120 // ----------------------------------------------------------------------------
       
   121 // RSpeechSynthesis::Close
       
   122 // 
       
   123 // ----------------------------------------------------------------------------
       
   124 //
       
   125 EXPORT_C void RSpeechSynthesis::Close()
       
   126     {
       
   127     RSessionBase::Close();  
       
   128     }
       
   129    
       
   130 // ----------------------------------------------------------------------------
       
   131 // RSpeechSynthesis::InitialiseSynthesis
       
   132 // 
       
   133 // ----------------------------------------------------------------------------
       
   134 //
       
   135 EXPORT_C void RSpeechSynthesis::InitialiseSynthesis( const TDesC& aText, 
       
   136                                                      TRequestStatus& aStatus )
       
   137     {
       
   138     if ( Handle() )
       
   139         {
       
   140         SendReceive( EPrime, TIpcArgs( &aText ), aStatus ); 
       
   141         }
       
   142     else
       
   143         {
       
   144         TRequestStatus *statusPointer = &aStatus;
       
   145         User::RequestComplete( statusPointer, KErrBadHandle );
       
   146         }
       
   147     }
       
   148     
       
   149 // ----------------------------------------------------------------------------
       
   150 // RSpeechSynthesis::InitialiseSynthesis
       
   151 // 
       
   152 // ----------------------------------------------------------------------------
       
   153 //
       
   154 EXPORT_C void RSpeechSynthesis::InitialiseSynthesis( const TDesC& aText, 
       
   155                                                      const TFileName& aFileName,
       
   156                                                      TRequestStatus& aStatus )
       
   157     {
       
   158     TRAPD( error, DoInitialiseSynthesisL( aText, aFileName, aStatus ) );
       
   159     if ( error )
       
   160         {
       
   161         TRequestStatus *statusPointer = &aStatus;
       
   162         User::RequestComplete( statusPointer, error );
       
   163         }
       
   164     }
       
   165     
       
   166 // ----------------------------------------------------------------------------
       
   167 // RSpeechSynthesis::InitialiseSynthesis
       
   168 // 
       
   169 // ----------------------------------------------------------------------------
       
   170 //
       
   171 EXPORT_C void RSpeechSynthesis::InitialiseSynthesis( const TDesC& aText,
       
   172                                                      TDes8& aDataBuffer, 
       
   173                                                      TRequestStatus& aStatus )
       
   174     {
       
   175     if ( Handle() )
       
   176         {
       
   177         SendReceive( EPrimeStreamOutput, TIpcArgs( &aText, &aDataBuffer ), aStatus ); 
       
   178         }
       
   179     else
       
   180         {
       
   181         TRequestStatus *statusPointer = &aStatus;
       
   182         User::RequestComplete( statusPointer, KErrBadHandle );
       
   183         }
       
   184     }
       
   185     
       
   186 // ----------------------------------------------------------------------------
       
   187 // RSpeechSynthesis::Synthesise
       
   188 // 
       
   189 // ----------------------------------------------------------------------------
       
   190 //
       
   191 EXPORT_C void RSpeechSynthesis::Synthesise( TRequestStatus& aStatus )
       
   192     {
       
   193     if ( Handle() )
       
   194         {
       
   195         SendReceive( ESynthesise, aStatus );
       
   196         }
       
   197     else
       
   198         {
       
   199         TRequestStatus *statusPointer = &aStatus;
       
   200         User::RequestComplete( statusPointer, KErrBadHandle );
       
   201         }
       
   202     }
       
   203     
       
   204 // ----------------------------------------------------------------------------
       
   205 // RSpeechSynthesis::StopL
       
   206 // 
       
   207 // ----------------------------------------------------------------------------
       
   208 //
       
   209 EXPORT_C void RSpeechSynthesis::StopL()
       
   210     {
       
   211     if ( Handle() == 0 )
       
   212         {
       
   213         User::Leave( KErrBadHandle );
       
   214         }
       
   215     
       
   216     User::LeaveIfError( SendReceive( EStopSynthesis ) ); 
       
   217     }
       
   218 
       
   219 // ----------------------------------------------------------------------------
       
   220 // RSpeechSynthesis::PauseL
       
   221 // 
       
   222 // ----------------------------------------------------------------------------
       
   223 //
       
   224 EXPORT_C void RSpeechSynthesis::PauseL()
       
   225     {
       
   226     if ( Handle() == 0 )
       
   227         {
       
   228         User::Leave( KErrBadHandle );
       
   229         }
       
   230     
       
   231     User::LeaveIfError( SendReceive( EPauseSynthesis ) );
       
   232     }     
       
   233 
       
   234 // ----------------------------------------------------------------------------
       
   235 // RSpeechSynthesis::DurationL
       
   236 // 
       
   237 // ----------------------------------------------------------------------------
       
   238 //
       
   239 EXPORT_C TTimeIntervalMicroSeconds RSpeechSynthesis::DurationL()
       
   240     {
       
   241     if ( Handle() == 0 )
       
   242         {
       
   243         User::Leave( KErrBadHandle );
       
   244         }
       
   245     
       
   246     TPckgBuf<TTimeIntervalMicroSeconds> pckg;
       
   247     
       
   248     User::LeaveIfError( SendReceive( EGetDuration, TIpcArgs( &pckg ) ) ); 
       
   249     
       
   250     return pckg();
       
   251     }
       
   252     
       
   253 // ----------------------------------------------------------------------------
       
   254 // RSpeechSynthesis::GetLanguagesL
       
   255 // 
       
   256 // ----------------------------------------------------------------------------
       
   257 //
       
   258 EXPORT_C void RSpeechSynthesis::GetLanguagesL( RArray<TLanguage>& aLanguages )
       
   259     {
       
   260 	CleanupClosePushL( aLanguages ); 
       
   261     if ( Handle() == 0 )
       
   262         {
       
   263         User::Leave( KErrBadHandle );
       
   264         }
       
   265     
       
   266     TPckgBuf<TInt> pckg;
       
   267     
       
   268     User::LeaveIfError( SendReceive( EGetLanguageCount, TIpcArgs( &pckg ) ) );
       
   269     
       
   270     const TInt languageCount( pckg() );
       
   271 
       
   272     if ( languageCount > 0 )    
       
   273         {
       
   274         const TInt tlanguageSize( sizeof(TLanguage) );
       
   275     
       
   276         HBufC8* data = HBufC8::NewLC( languageCount * tlanguageSize );
       
   277         TPtr8 data_ptr( data->Des() );
       
   278     
       
   279         User::LeaveIfError( SendReceive( EGetLanguages, TIpcArgs( &data_ptr ) ) );
       
   280         
       
   281         for ( TInt i( 0 ); i < languageCount; i++ )
       
   282             {
       
   283             TPtr8 tmp = data_ptr.MidTPtr( i * tlanguageSize, tlanguageSize );
       
   284             
       
   285             const TLanguage* lang = (TLanguage*)tmp.Ptr();
       
   286             
       
   287             aLanguages.AppendL( *lang );
       
   288             }
       
   289 
       
   290         CleanupStack::PopAndDestroy( data );
       
   291         }
       
   292     CleanupStack::Pop(); 
       
   293     }
       
   294 
       
   295 // ----------------------------------------------------------------------------
       
   296 // RSpeechSynthesis::GetVoicesL
       
   297 // 
       
   298 // ----------------------------------------------------------------------------
       
   299 //
       
   300 EXPORT_C void RSpeechSynthesis::GetVoicesL( RArray<TVoice>& aVoices,
       
   301                                             TLanguage aLanguage )
       
   302     {
       
   303 	CleanupClosePushL( aVoices ); 
       
   304     if ( Handle() == 0 )
       
   305         {
       
   306         User::Leave( KErrBadHandle );
       
   307         }
       
   308     
       
   309     TPckgBuf<TInt> pckg;
       
   310     
       
   311     User::LeaveIfError( SendReceive( EGetVoiceCount, 
       
   312                                      TIpcArgs( &pckg, aLanguage ) ) );
       
   313     
       
   314     const TInt voiceCount( pckg() );
       
   315     
       
   316     if ( voiceCount > 0 ) 
       
   317         {
       
   318         const TInt tvoiceSize( sizeof(TVoice) );
       
   319         
       
   320         HBufC8* data = HBufC8::NewLC( voiceCount * tvoiceSize );
       
   321         TPtr8 data_ptr( data->Des() );
       
   322     
       
   323         User::LeaveIfError( SendReceive( EGetVoices, TIpcArgs( &data_ptr ) ) );
       
   324         
       
   325         for ( TInt i( 0 ); i < voiceCount; i++ )
       
   326             {
       
   327             TPtr8 tmp = data_ptr.MidTPtr( i * tvoiceSize, tvoiceSize );
       
   328             
       
   329             const TVoice* voice = (TVoice*)tmp.Ptr();
       
   330             
       
   331             aVoices.AppendL( *voice );
       
   332             }
       
   333         
       
   334         CleanupStack::PopAndDestroy( data );   
       
   335         }
       
   336     CleanupStack::Pop(); 
       
   337     }
       
   338 
       
   339 // ----------------------------------------------------------------------------
       
   340 // RSpeechSynthesis::VoiceL
       
   341 // 
       
   342 // ----------------------------------------------------------------------------
       
   343 //
       
   344 EXPORT_C TVoice RSpeechSynthesis::VoiceL()
       
   345     {
       
   346     if ( Handle() == 0 )
       
   347         {
       
   348         User::Leave( KErrBadHandle );
       
   349         }
       
   350     
       
   351     TPckgBuf<TVoice> pckg;
       
   352     
       
   353     User::LeaveIfError( SendReceive( EVoice, TIpcArgs( &pckg ) ) );
       
   354     
       
   355     return pckg();
       
   356     }
       
   357 
       
   358 // ----------------------------------------------------------------------------
       
   359 // RSpeechSynthesis::SetVoiceL
       
   360 // 
       
   361 // ----------------------------------------------------------------------------
       
   362 //
       
   363 EXPORT_C void RSpeechSynthesis::SetVoiceL( const TVoice& aVoice )
       
   364     {
       
   365     if ( Handle() == 0 )
       
   366         {
       
   367         User::Leave( KErrBadHandle );
       
   368         }
       
   369     
       
   370     TPckgBuf<TVoice> pckg = aVoice;
       
   371     
       
   372     User::LeaveIfError( SendReceive( ESetVoice, TIpcArgs( &pckg ) ) );
       
   373     }
       
   374 
       
   375 // ----------------------------------------------------------------------------
       
   376 // RSpeechSynthesis::MaxSpeakingRateL
       
   377 // 
       
   378 // ----------------------------------------------------------------------------
       
   379 //
       
   380 EXPORT_C TInt RSpeechSynthesis::MaxSpeakingRateL()
       
   381     {
       
   382     if ( Handle() == 0 )
       
   383         {
       
   384         User::Leave( KErrBadHandle );
       
   385         }
       
   386     
       
   387     TPckgBuf<TInt> pckg;
       
   388     
       
   389     User::LeaveIfError( SendReceive( EMaxSpeakingRate, TIpcArgs( &pckg ) ) );
       
   390     
       
   391     return pckg();
       
   392     }
       
   393 
       
   394 // ----------------------------------------------------------------------------
       
   395 // RSpeechSynthesis::SpeakingRateL
       
   396 // 
       
   397 // ----------------------------------------------------------------------------
       
   398 //
       
   399 EXPORT_C TInt RSpeechSynthesis::SpeakingRateL()
       
   400     {
       
   401     if ( Handle() == 0 )
       
   402         {
       
   403         User::Leave( KErrBadHandle );
       
   404         }
       
   405     
       
   406     TPckgBuf<TInt> pckg;
       
   407     
       
   408     User::LeaveIfError( SendReceive( ESpeakingRate, TIpcArgs( &pckg ) ) );
       
   409     
       
   410     return pckg();
       
   411     }
       
   412 
       
   413 // ----------------------------------------------------------------------------
       
   414 // RSpeechSynthesis::SetSpeakingRateL
       
   415 // 
       
   416 // ----------------------------------------------------------------------------
       
   417 //
       
   418 EXPORT_C void RSpeechSynthesis::SetSpeakingRateL( TInt aSpeakingRate )
       
   419     {
       
   420     if ( Handle() == 0 )
       
   421         {
       
   422         User::Leave( KErrBadHandle );
       
   423         }
       
   424     
       
   425     User::LeaveIfError( SendReceive( ESetSpeakingRate, 
       
   426                                      TIpcArgs( aSpeakingRate ) ) );
       
   427     }
       
   428 
       
   429 // ----------------------------------------------------------------------------
       
   430 // RSpeechSynthesis::MaxVolumeL
       
   431 // 
       
   432 // ----------------------------------------------------------------------------
       
   433 //
       
   434 EXPORT_C TInt RSpeechSynthesis::MaxVolumeL()
       
   435     {
       
   436     if ( Handle() == 0 )
       
   437         {
       
   438         User::Leave( KErrBadHandle );
       
   439         }
       
   440     
       
   441     TPckgBuf<TInt> pckg;
       
   442     
       
   443     User::LeaveIfError( SendReceive( EMaxVolume, TIpcArgs( &pckg ) ) );
       
   444     
       
   445     return pckg();
       
   446     }
       
   447 
       
   448 // ----------------------------------------------------------------------------
       
   449 // RSpeechSynthesis::VolumeL
       
   450 // 
       
   451 // ----------------------------------------------------------------------------
       
   452 //
       
   453 EXPORT_C TInt RSpeechSynthesis::VolumeL()
       
   454     {
       
   455     if ( Handle() == 0 )
       
   456         {
       
   457         User::Leave( KErrBadHandle );
       
   458         }
       
   459     
       
   460     TPckgBuf<TInt> pckg;
       
   461     
       
   462     User::LeaveIfError( SendReceive( EVolume, TIpcArgs( &pckg ) ) );
       
   463     
       
   464     return pckg();
       
   465     }
       
   466 
       
   467 // ----------------------------------------------------------------------------
       
   468 // RSpeechSynthesis::SetVolumeL
       
   469 // 
       
   470 // ----------------------------------------------------------------------------
       
   471 //
       
   472 EXPORT_C void RSpeechSynthesis::SetVolumeL( TInt aVolume )
       
   473     {
       
   474     if ( Handle() == 0 )
       
   475         {
       
   476         User::Leave( KErrBadHandle );
       
   477         }
       
   478     
       
   479     User::LeaveIfError( SendReceive( ESetVolume, TIpcArgs( aVolume ) ) );
       
   480     }
       
   481 
       
   482 // ----------------------------------------------------------------------------
       
   483 // RSpeechSynthesis::SetAudioPriorityL
       
   484 // 
       
   485 // ----------------------------------------------------------------------------
       
   486 //
       
   487 EXPORT_C void RSpeechSynthesis::SetAudioPriorityL( TInt aPriority, 
       
   488                                                    TInt aPreference )
       
   489     {
       
   490     if ( Handle() == 0 )
       
   491         {
       
   492         User::Leave( KErrBadHandle );
       
   493         }
       
   494     
       
   495     User::LeaveIfError( SendReceive( ESetAudioPriority, 
       
   496                                      TIpcArgs( aPriority, aPreference ) ) );
       
   497     }
       
   498 
       
   499 // ----------------------------------------------------------------------------
       
   500 // RSpeechSynthesis::SetAudioOutputL
       
   501 // 
       
   502 // ----------------------------------------------------------------------------
       
   503 //
       
   504 EXPORT_C void RSpeechSynthesis::SetAudioOutputL( TTtsAudioOutput aAudioOutput )
       
   505     {
       
   506     if ( Handle() == 0 )
       
   507         {
       
   508         User::Leave( KErrBadHandle );
       
   509         }
       
   510     
       
   511     User::LeaveIfError( SendReceive( ESetAudioOutput, TIpcArgs( aAudioOutput ) ) );
       
   512     }
       
   513 
       
   514 // ----------------------------------------------------------------------------
       
   515 // RSpeechSynthesis::CustomCommandL
       
   516 // 
       
   517 // ----------------------------------------------------------------------------
       
   518 //
       
   519 EXPORT_C void RSpeechSynthesis::CustomCommandL( TInt aCommand, TInt aValue )
       
   520     {
       
   521     if ( Handle() == 0 )
       
   522         {
       
   523         User::Leave( KErrBadHandle );
       
   524         }
       
   525     
       
   526     User::LeaveIfError( SendReceive( ECustomCommand, 
       
   527                                      TIpcArgs( aCommand, aValue ) ) );
       
   528     }
       
   529 
       
   530 // ----------------------------------------------------------------------------
       
   531 // RSpeechSynthesis::DoInitialiseSynthesisL
       
   532 // 
       
   533 // ----------------------------------------------------------------------------
       
   534 //
       
   535 void RSpeechSynthesis::DoInitialiseSynthesisL( const TDesC& aText, 
       
   536                                                const TFileName& aFileName, 
       
   537                                                TRequestStatus& aStatus ) 
       
   538     {
       
   539     if ( Handle() == 0 )
       
   540         {
       
   541         User::Leave( KErrBadHandle );
       
   542         }
       
   543         
       
   544     // @todo Add support for all supported audio formats 
       
   545     TFileName tmpName( aFileName );
       
   546     tmpName.LowerCase();
       
   547             
       
   548     TParse parser;
       
   549     User::LeaveIfError( parser.Set( tmpName, NULL, NULL ) );
       
   550     
       
   551     TPtrC extension = parser.Ext(); 
       
   552     
       
   553     if ( extension != _L(".raw") ) 
       
   554         {
       
   555         User::Leave( KErrArgument );
       
   556         }
       
   557 
       
   558     RFs fs;
       
   559     RFile file;
       
   560 
       
   561     User::LeaveIfError( fs.Connect() ); 
       
   562     CleanupClosePushL( fs );    
       
   563     User::LeaveIfError( fs.ShareProtected() ); 
       
   564     
       
   565     User::LeaveIfError( file.Replace( fs, aFileName, EFileWrite ) ); 
       
   566     CleanupClosePushL( file );
       
   567     
       
   568     TIpcArgs ipcArgs;
       
   569     User::LeaveIfError( file.TransferToServer( ipcArgs, KFsHandleIndex, KFileHandleIndex ) ); 
       
   570     User::LeaveIfError( SendReceive( ETransferFileHandle, ipcArgs ) );
       
   571     
       
   572     CleanupStack::PopAndDestroy( &file ); 
       
   573     CleanupStack::PopAndDestroy( &fs ); 
       
   574     
       
   575     SendReceive( EPrimeFileOutput, TIpcArgs( &aText ), aStatus ); 
       
   576     }
       
   577     
       
   578 // End of file