srsf/speechsynthesis/client/src/speechsynthesis.cpp
changeset 13 57b735022c18
parent 1 b13cd05eeb2f
equal deleted inserted replaced
1:b13cd05eeb2f 13:57b735022c18
     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     if ( Handle() == 0 )
       
   261         {
       
   262         User::Leave( KErrBadHandle );
       
   263         }
       
   264     
       
   265     TPckgBuf<TInt> pckg;
       
   266     
       
   267     User::LeaveIfError( SendReceive( EGetLanguageCount, TIpcArgs( &pckg ) ) );
       
   268     
       
   269     const TInt languageCount( pckg() );
       
   270 
       
   271     if ( languageCount > 0 )    
       
   272         {
       
   273         const TInt tlanguageSize( sizeof(TLanguage) );
       
   274     
       
   275         HBufC8* data = HBufC8::NewLC( languageCount * tlanguageSize );
       
   276         TPtr8 data_ptr( data->Des() );
       
   277     
       
   278         User::LeaveIfError( SendReceive( EGetLanguages, TIpcArgs( &data_ptr ) ) );
       
   279         
       
   280         for ( TInt i( 0 ); i < languageCount; i++ )
       
   281             {
       
   282             TPtr8 tmp = data_ptr.MidTPtr( i * tlanguageSize, tlanguageSize );
       
   283             
       
   284             const TLanguage* lang = (TLanguage*)tmp.Ptr();
       
   285             
       
   286             aLanguages.AppendL( *lang );
       
   287             }
       
   288 
       
   289         CleanupStack::PopAndDestroy( data );
       
   290         }
       
   291     }
       
   292 
       
   293 // ----------------------------------------------------------------------------
       
   294 // RSpeechSynthesis::GetVoicesL
       
   295 // 
       
   296 // ----------------------------------------------------------------------------
       
   297 //
       
   298 EXPORT_C void RSpeechSynthesis::GetVoicesL( RArray<TVoice>& aVoices,
       
   299                                             TLanguage aLanguage )
       
   300     {
       
   301     if ( Handle() == 0 )
       
   302         {
       
   303         User::Leave( KErrBadHandle );
       
   304         }
       
   305     
       
   306     TPckgBuf<TInt> pckg;
       
   307     
       
   308     User::LeaveIfError( SendReceive( EGetVoiceCount, 
       
   309                                      TIpcArgs( &pckg, aLanguage ) ) );
       
   310     
       
   311     const TInt voiceCount( pckg() );
       
   312     
       
   313     if ( voiceCount > 0 ) 
       
   314         {
       
   315         const TInt tvoiceSize( sizeof(TVoice) );
       
   316         
       
   317         HBufC8* data = HBufC8::NewLC( voiceCount * tvoiceSize );
       
   318         TPtr8 data_ptr( data->Des() );
       
   319     
       
   320         User::LeaveIfError( SendReceive( EGetVoices, TIpcArgs( &data_ptr ) ) );
       
   321         
       
   322         for ( TInt i( 0 ); i < voiceCount; i++ )
       
   323             {
       
   324             TPtr8 tmp = data_ptr.MidTPtr( i * tvoiceSize, tvoiceSize );
       
   325             
       
   326             const TVoice* voice = (TVoice*)tmp.Ptr();
       
   327             
       
   328             aVoices.AppendL( *voice );
       
   329             }
       
   330         
       
   331         CleanupStack::PopAndDestroy( data );   
       
   332         }
       
   333     }
       
   334 
       
   335 // ----------------------------------------------------------------------------
       
   336 // RSpeechSynthesis::VoiceL
       
   337 // 
       
   338 // ----------------------------------------------------------------------------
       
   339 //
       
   340 EXPORT_C TVoice RSpeechSynthesis::VoiceL()
       
   341     {
       
   342     if ( Handle() == 0 )
       
   343         {
       
   344         User::Leave( KErrBadHandle );
       
   345         }
       
   346     
       
   347     TPckgBuf<TVoice> pckg;
       
   348     
       
   349     User::LeaveIfError( SendReceive( EVoice, TIpcArgs( &pckg ) ) );
       
   350     
       
   351     return pckg();
       
   352     }
       
   353 
       
   354 // ----------------------------------------------------------------------------
       
   355 // RSpeechSynthesis::SetVoiceL
       
   356 // 
       
   357 // ----------------------------------------------------------------------------
       
   358 //
       
   359 EXPORT_C void RSpeechSynthesis::SetVoiceL( const TVoice& aVoice )
       
   360     {
       
   361     if ( Handle() == 0 )
       
   362         {
       
   363         User::Leave( KErrBadHandle );
       
   364         }
       
   365     
       
   366     TPckgBuf<TVoice> pckg = aVoice;
       
   367     
       
   368     User::LeaveIfError( SendReceive( ESetVoice, TIpcArgs( &pckg ) ) );
       
   369     }
       
   370 
       
   371 // ----------------------------------------------------------------------------
       
   372 // RSpeechSynthesis::MaxSpeakingRateL
       
   373 // 
       
   374 // ----------------------------------------------------------------------------
       
   375 //
       
   376 EXPORT_C TInt RSpeechSynthesis::MaxSpeakingRateL()
       
   377     {
       
   378     if ( Handle() == 0 )
       
   379         {
       
   380         User::Leave( KErrBadHandle );
       
   381         }
       
   382     
       
   383     TPckgBuf<TInt> pckg;
       
   384     
       
   385     User::LeaveIfError( SendReceive( EMaxSpeakingRate, TIpcArgs( &pckg ) ) );
       
   386     
       
   387     return pckg();
       
   388     }
       
   389 
       
   390 // ----------------------------------------------------------------------------
       
   391 // RSpeechSynthesis::SpeakingRateL
       
   392 // 
       
   393 // ----------------------------------------------------------------------------
       
   394 //
       
   395 EXPORT_C TInt RSpeechSynthesis::SpeakingRateL()
       
   396     {
       
   397     if ( Handle() == 0 )
       
   398         {
       
   399         User::Leave( KErrBadHandle );
       
   400         }
       
   401     
       
   402     TPckgBuf<TInt> pckg;
       
   403     
       
   404     User::LeaveIfError( SendReceive( ESpeakingRate, TIpcArgs( &pckg ) ) );
       
   405     
       
   406     return pckg();
       
   407     }
       
   408 
       
   409 // ----------------------------------------------------------------------------
       
   410 // RSpeechSynthesis::SetSpeakingRateL
       
   411 // 
       
   412 // ----------------------------------------------------------------------------
       
   413 //
       
   414 EXPORT_C void RSpeechSynthesis::SetSpeakingRateL( TInt aSpeakingRate )
       
   415     {
       
   416     if ( Handle() == 0 )
       
   417         {
       
   418         User::Leave( KErrBadHandle );
       
   419         }
       
   420     
       
   421     User::LeaveIfError( SendReceive( ESetSpeakingRate, 
       
   422                                      TIpcArgs( aSpeakingRate ) ) );
       
   423     }
       
   424 
       
   425 // ----------------------------------------------------------------------------
       
   426 // RSpeechSynthesis::MaxVolumeL
       
   427 // 
       
   428 // ----------------------------------------------------------------------------
       
   429 //
       
   430 EXPORT_C TInt RSpeechSynthesis::MaxVolumeL()
       
   431     {
       
   432     if ( Handle() == 0 )
       
   433         {
       
   434         User::Leave( KErrBadHandle );
       
   435         }
       
   436     
       
   437     TPckgBuf<TInt> pckg;
       
   438     
       
   439     User::LeaveIfError( SendReceive( EMaxVolume, TIpcArgs( &pckg ) ) );
       
   440     
       
   441     return pckg();
       
   442     }
       
   443 
       
   444 // ----------------------------------------------------------------------------
       
   445 // RSpeechSynthesis::VolumeL
       
   446 // 
       
   447 // ----------------------------------------------------------------------------
       
   448 //
       
   449 EXPORT_C TInt RSpeechSynthesis::VolumeL()
       
   450     {
       
   451     if ( Handle() == 0 )
       
   452         {
       
   453         User::Leave( KErrBadHandle );
       
   454         }
       
   455     
       
   456     TPckgBuf<TInt> pckg;
       
   457     
       
   458     User::LeaveIfError( SendReceive( EVolume, TIpcArgs( &pckg ) ) );
       
   459     
       
   460     return pckg();
       
   461     }
       
   462 
       
   463 // ----------------------------------------------------------------------------
       
   464 // RSpeechSynthesis::SetVolumeL
       
   465 // 
       
   466 // ----------------------------------------------------------------------------
       
   467 //
       
   468 EXPORT_C void RSpeechSynthesis::SetVolumeL( TInt aVolume )
       
   469     {
       
   470     if ( Handle() == 0 )
       
   471         {
       
   472         User::Leave( KErrBadHandle );
       
   473         }
       
   474     
       
   475     User::LeaveIfError( SendReceive( ESetVolume, TIpcArgs( aVolume ) ) );
       
   476     }
       
   477 
       
   478 // ----------------------------------------------------------------------------
       
   479 // RSpeechSynthesis::SetAudioPriorityL
       
   480 // 
       
   481 // ----------------------------------------------------------------------------
       
   482 //
       
   483 EXPORT_C void RSpeechSynthesis::SetAudioPriorityL( TInt aPriority, 
       
   484                                                    TInt aPreference )
       
   485     {
       
   486     if ( Handle() == 0 )
       
   487         {
       
   488         User::Leave( KErrBadHandle );
       
   489         }
       
   490     
       
   491     User::LeaveIfError( SendReceive( ESetAudioPriority, 
       
   492                                      TIpcArgs( aPriority, aPreference ) ) );
       
   493     }
       
   494 
       
   495 // ----------------------------------------------------------------------------
       
   496 // RSpeechSynthesis::SetAudioOutputL
       
   497 // 
       
   498 // ----------------------------------------------------------------------------
       
   499 //
       
   500 EXPORT_C void RSpeechSynthesis::SetAudioOutputL( TTtsAudioOutput aAudioOutput )
       
   501     {
       
   502     if ( Handle() == 0 )
       
   503         {
       
   504         User::Leave( KErrBadHandle );
       
   505         }
       
   506     
       
   507     User::LeaveIfError( SendReceive( ESetAudioOutput, TIpcArgs( aAudioOutput ) ) );
       
   508     }
       
   509 
       
   510 // ----------------------------------------------------------------------------
       
   511 // RSpeechSynthesis::CustomCommandL
       
   512 // 
       
   513 // ----------------------------------------------------------------------------
       
   514 //
       
   515 EXPORT_C void RSpeechSynthesis::CustomCommandL( TInt aCommand, TInt aValue )
       
   516     {
       
   517     if ( Handle() == 0 )
       
   518         {
       
   519         User::Leave( KErrBadHandle );
       
   520         }
       
   521     
       
   522     User::LeaveIfError( SendReceive( ECustomCommand, 
       
   523                                      TIpcArgs( aCommand, aValue ) ) );
       
   524     }
       
   525 
       
   526 // ----------------------------------------------------------------------------
       
   527 // RSpeechSynthesis::DoInitialiseSynthesisL
       
   528 // 
       
   529 // ----------------------------------------------------------------------------
       
   530 //
       
   531 void RSpeechSynthesis::DoInitialiseSynthesisL( const TDesC& aText, 
       
   532                                                const TFileName& aFileName, 
       
   533                                                TRequestStatus& aStatus ) 
       
   534     {
       
   535     if ( Handle() == 0 )
       
   536         {
       
   537         User::Leave( KErrBadHandle );
       
   538         }
       
   539         
       
   540     // @todo Add support for all supported audio formats 
       
   541     TFileName tmpName( aFileName );
       
   542     tmpName.LowerCase();
       
   543             
       
   544     TParse parser;
       
   545     User::LeaveIfError( parser.Set( tmpName, NULL, NULL ) );
       
   546     
       
   547     TPtrC extension = parser.Ext(); 
       
   548     
       
   549     if ( extension != _L(".raw") ) 
       
   550         {
       
   551         User::Leave( KErrArgument );
       
   552         }
       
   553 
       
   554     RFs fs;
       
   555     RFile file;
       
   556 
       
   557     User::LeaveIfError( fs.Connect() ); 
       
   558     CleanupClosePushL( fs );    
       
   559     User::LeaveIfError( fs.ShareProtected() ); 
       
   560     
       
   561     User::LeaveIfError( file.Replace( fs, aFileName, EFileWrite ) ); 
       
   562     CleanupClosePushL( file );
       
   563     
       
   564     TIpcArgs ipcArgs;
       
   565     User::LeaveIfError( file.TransferToServer( ipcArgs, KFsHandleIndex, KFileHandleIndex ) ); 
       
   566     User::LeaveIfError( SendReceive( ETransferFileHandle, ipcArgs ) );
       
   567     
       
   568     CleanupStack::PopAndDestroy( &file ); 
       
   569     CleanupStack::PopAndDestroy( &fs ); 
       
   570     
       
   571     SendReceive( EPrimeFileOutput, TIpcArgs( &aText ), aStatus ); 
       
   572     }
       
   573     
       
   574 // End of file