srsf/devtts/src/devttsalgorithmmanager.h
branchRCL_3
changeset 19 e36f3802f733
parent 0 bf1d17376201
equal deleted inserted replaced
18:cad71a31b7fc 19:e36f3802f733
       
     1 /*
       
     2 * Copyright (c) 2004-2006 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:  Implementations behind DevTTS interface
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #ifndef DEVTTSALGORITHMMANAGER_H
       
    20 #define DEVTTSALGORITHMMANAGER_H
       
    21 
       
    22 // INCLUDES
       
    23 #include "srsfbldvariant.hrh"
       
    24 #include "nssdevtts.h"
       
    25 #include "devttsaudio.h"
       
    26 
       
    27 #include <asrsttshwdevice.h>
       
    28 #include <asrsttphwdevice.h>
       
    29 #include <asrsnlphwdevice.h>
       
    30 
       
    31 // CLASS DECLARATION
       
    32 
       
    33 /**
       
    34 * CTTSAlgorithmManager contains implementations behind DevTTS interface.
       
    35 * Observer callbacks from TtsHwDevice and DevTTSAudio
       
    36 *
       
    37 *  @lib NssDevTts.lib
       
    38 */
       
    39 class CTTSAlgorithmManager : public CBase, public MDevTTSAudioObserver,
       
    40                              public MTtsHwDeviceObserver,
       
    41                              public MASRSTtpHwDeviceObserver,
       
    42                              public MNlpHwDeviceObserver
       
    43     {
       
    44     public:  // Constructors and destructor
       
    45         
       
    46         /**
       
    47         * Two-phased constructor.
       
    48         *
       
    49         * @param "MDevTTSObserver& aObserver"
       
    50         *        Reference to the callback observer.
       
    51         */ 
       
    52         static CTTSAlgorithmManager* NewL( MDevTTSObserver& aObserver/*, CConsoleBase* console*/ );
       
    53         
       
    54         /**
       
    55         * Destructor.
       
    56         */
       
    57         virtual ~CTTSAlgorithmManager();
       
    58         
       
    59     public: // New functions
       
    60         
       
    61         /**
       
    62         * Adds a new style to the collection. If equal style exists already,
       
    63         * new style is not added.
       
    64         * Will leave if an error occurs. Likely errors are: KErrNoMemory,
       
    65         * not enough memory to add new style. KErrNotSupported, the style 
       
    66         * is not supported. KErrArgument, the style contains an imposible 
       
    67         * value.
       
    68         *
       
    69         * @param "TTtsStyle& aStyle"
       
    70         *        Reference to the style which will be added to the style 
       
    71         *        collection.
       
    72         *
       
    73         * @return Identifier of added style.
       
    74         */
       
    75         TTtsStyleID AddStyleL( const TTtsStyle& aStyle );
       
    76 
       
    77         /**
       
    78         * Returns the current balance setting.
       
    79         *
       
    80         * @return Balance value.
       
    81         */
       
    82         TInt Balance();
       
    83         
       
    84         /**
       
    85         * Notifies that the buffer has been processed. The 
       
    86         * method is used if output mode of synthesizing is 
       
    87         * EDevTTSClientMode.
       
    88         *
       
    89         * @param "TDesC8& aBuffer"
       
    90         *        The buffer which has been processed.
       
    91         */
       
    92         void BufferProcessed( const TDesC8& aBuffer );
       
    93         
       
    94         /**
       
    95         * Request a pointer to the custom interface. Currently not in use.
       
    96         * 
       
    97         * @param "TUid aInterfaceID"
       
    98         *        Custom interface identifier.
       
    99         *
       
   100         * @return Pointer to custom interface. NULL if interface is not 
       
   101         *         found or initiation failures.
       
   102         */
       
   103         TAny* CustomInterface( TUid aInterfaceID );
       
   104 
       
   105         /**
       
   106         * Deletes style.
       
   107         * 
       
   108         * @param "TTtsStyleID aStyleID" Identifier of style to be deleted.
       
   109         */
       
   110         void DeleteStyleL( TTtsStyleID aStyleID );
       
   111         
       
   112         /**
       
   113         * Gives current position of synthesizing. The method will leave if an 
       
   114         * error occurs. Likely errors are: KErrNotReady, no synthesizing is 
       
   115         * ongoing.
       
   116         * 
       
   117         * @param "TTimeIntervalMicroSeconds& aTime"
       
   118         *        Time of the synthesizing position
       
   119         */
       
   120         void GetPositionL( TTimeIntervalMicroSeconds& aTime ) const;
       
   121         
       
   122         /**
       
   123         * Gives current position of synthesizing. The method will leave if an 
       
   124         * error occurs. Likely errors are: KErrNotReady, no synthesizing is 
       
   125         * ongoing.
       
   126         * 
       
   127         * @param "TTtsSegment& aSegment"
       
   128         *        Current segment which is synthesized, out parameter
       
   129         * @param "TInt& aWordIndex"
       
   130         *        Index of the current word, starting from the begining of 
       
   131         *        current segment
       
   132         */
       
   133         void GetPositionL( TTtsSegment& aSegment, TInt& aWordIndex ) const;
       
   134         
       
   135         /**
       
   136         * Tells if language is supported by the TTS engine or not.
       
   137         * 
       
   138         * @param "TLanguage aLanguage"
       
   139         *        Language code
       
   140         *
       
   141         * @return ETrue if supported, EFalse otherwise
       
   142         */
       
   143         TBool IsLanguageSupported( TLanguage aLanguage ) const;
       
   144 
       
   145         /**
       
   146         * Returns maximum volume value.
       
   147         * 
       
   148         * @return Maximum volume.
       
   149         */
       
   150         TInt MaxVolume();
       
   151         
       
   152         /**
       
   153         * Gives number of styles.
       
   154         * 
       
   155         * @return Number of styles.
       
   156         */
       
   157         TUint16 NumberOfStyles() const;
       
   158         
       
   159         /**
       
   160         * Pauses synthesizing.
       
   161         */
       
   162         void Pause();
       
   163         
       
   164         /**
       
   165         * Prepares for synthesis. The method will leave if an 
       
   166         * error occurs. Likely errors are: KErrInUse, synthesizing is 
       
   167         * currently ongoing. KErrNoMemory, not enough memory to start 
       
   168         * synthesis. KErrNotSupport, requested function is not supported.
       
   169         * 
       
   170         * @param "MTtsSegmentStream& aStream"
       
   171         *        Stream to be synthesized.
       
   172         */
       
   173         void PrimeSynthesisL( MTtsSegmentStream& aStream );
       
   174         
       
   175         /**
       
   176         * Prepares for synthesis. The method will leave if an 
       
   177         * error occurs. Likely errors are: KErrInUse, synthesizing is 
       
   178         * currently ongoing. KErrNoMemory, not enough memory to start 
       
   179         * synthesis. KErrNotSupport, requested function is not supported.
       
   180         * 
       
   181         * @param "CTtsParsedText& aText"
       
   182         *        Text containing multiple segments to be synthesized.
       
   183         */
       
   184         void PrimeSynthesisL( CTtsParsedText& aText );
       
   185         
       
   186         /**
       
   187         * Sets the priority for audio playback if in EDevTTSSoundDeviceMode.
       
   188         * 
       
   189         * @param "TInt iPriority"
       
   190         *        Priority value.
       
   191         * @param "TDevTTSAudioPreference iPref"'
       
   192         *        Preference setting. Quality, time or both.
       
   193         */
       
   194         void SetAudioPriority( TInt aPriority, TDevTTSAudioPreference aPref );
       
   195 
       
   196         /**
       
   197         * Sets the audio output routing
       
   198         * 
       
   199         * @param TInt aAudioOutput
       
   200         *        New output.
       
   201         */
       
   202         void SetAudioOutputL( TInt aAudioOutput );
       
   203 
       
   204         /**
       
   205         * Sets the playback device balance. Affects only when DevTTS is in 
       
   206         * EDevTTSSoundDeviceMode. Illegal values are neglected.
       
   207         * 
       
   208         * @param "TInt aBalance" Balance to be set. 
       
   209         */
       
   210         void SetBalance( TInt aBalance );
       
   211                
       
   212         /**
       
   213         * Sets new position for synthesizing. The method will leave if an 
       
   214         * error occurs. Likely errors are: KErrNotReady, no synthesizing is 
       
   215         * ongoing. KErrArgument, position cannot be set.
       
   216         * 
       
   217         * @param "TTimeIntervalMicroSeconds& aTime"
       
   218         *        Time of the synthesizing postion.
       
   219         */
       
   220         void SetPositionL( const TTimeIntervalMicroSeconds& aTime );
       
   221         
       
   222         /**
       
   223         * Sets new position for synthesizing. The method will leave if an 
       
   224         * error occurs. Likely errors are: KErrNotReady, no synthesizing is 
       
   225         * ongoing. KErrArgument, position cannot be set.
       
   226         * 
       
   227         * @param "TTTtsSegment& aSegment"
       
   228         *        The segment being synthesized.
       
   229         * @param "TInt aWordIndex"
       
   230         *        The index of word being synthesized.
       
   231         */
       
   232         void SetPositionL( const TTtsSegment& aSegment, TInt aWordIndex );
       
   233         
       
   234         /**
       
   235         * Sets the playback device volume. Affects only when DevTTS is in 
       
   236         * EDevTTSSoundDeviceMode. Range is from 0..MaxVolume(). Illegal values 
       
   237         * are neglected.
       
   238         * 
       
   239         * @param "TInt aVolume" Volume setting.
       
   240         */
       
   241         void SetVolume( TInt aVolume );
       
   242 
       
   243         /**
       
   244         * Sets the volume ramp duration. Start and end volumes must be set with
       
   245         * SetVolume() function.
       
   246         * 
       
   247         * @param "TTimeIntervalMicroSeconds& aRampDuration" 
       
   248         *        Ramp duration in microseconds.
       
   249         */
       
   250         void SetVolumeRamp( const TTimeIntervalMicroSeconds& aRampDuration );
       
   251 
       
   252         /**
       
   253         * Stops synthesizing and releases resources of synthesizing. 
       
   254         * The method does nothing if already stopped.
       
   255         */
       
   256         void Stop();
       
   257         
       
   258         /**
       
   259         * Receives style information. The method will leave if an error 
       
   260         * occurs. Likely errors are: KErrNotFound, style identifier not 
       
   261         * found. 
       
   262         * 
       
   263         * @param "TTtsStyleID aStyleID"
       
   264         *        Style identifier.
       
   265         *
       
   266         * @return Reference to style
       
   267         */
       
   268         TTtsStyle& StyleL( TTtsStyleID aStyleID ) const;
       
   269         
       
   270         /**
       
   271         * Receives style information. The method will leave if an error 
       
   272         * occurs. Likely errors are: KErrArgument, index is out of bounds.
       
   273         * 
       
   274         * @param "TUint16 aIndex"
       
   275         *        Index to style: 0..NumberOfStyles().
       
   276         *
       
   277         * @return Reference to style
       
   278         */ 
       
   279         TTtsStyle& StyleL( TUint16 aIndex ) const;
       
   280         
       
   281         /**
       
   282         * Starts synthesizing the text. The method is asynchronous, as soon
       
   283         * as the request has been placed, function call will return. 
       
   284         * MDevTTSObserver::MdtoEvent will be called when synthesizing finishes.
       
   285         * The method will leave if an error occurs. Likely errors are: 
       
   286         * KErrNoMemory, not enough memory. 
       
   287         * KErrInUse, synthesizing is currently ongoing
       
   288         * 
       
   289         * @param "TDevTTSOutputMode aOutputMode"
       
   290         *        Either use a sound device (EDevTTSSoundDeviceMode) or client 
       
   291         *        using callback (EDevTTSClientMode)
       
   292         */
       
   293         void SynthesizeL( TDevTTSOutputMode aOutputMode );
       
   294 
       
   295         /**
       
   296         * Returns current playback volume.
       
   297         * 
       
   298         * @return Volume setting.
       
   299         */       
       
   300         TInt Volume();
       
   301 
       
   302         /** 
       
   303         * Calls language identification for a given text.
       
   304         *
       
   305         * @since 3.0M
       
   306         * @param "CTtsParsedText& aText" Text
       
   307         * @param "TInt aNumberOfGuesses" Maximum number of guesses that will be provided
       
   308         * @param "RArray<TLanguage>& aLanguages" Output parameter which will contain the languages
       
   309         * @param "RArray<TInt>& aScores" Scores
       
   310         */
       
   311         void LanguageIdentificationL( CTtsParsedText& aText, 
       
   312                                       TInt aNumberOfGuesses, 
       
   313                                       RArray<TLanguage>& aLanguages,
       
   314                                       RArray<TInt>& aScores );
       
   315 
       
   316         /**
       
   317         * Calls Natural Language Processing (NLP) to normalize text.
       
   318         *
       
   319         * @since 3.0M
       
   320         * @param "CTtsParsedText& aText" Text which will be normalized
       
   321         */
       
   322         void NormalizeTextL( CTtsParsedText& aText );
       
   323 
       
   324         /**
       
   325         * Calls Natural Language Processing (NLP) to normalize and segment text.
       
   326         *
       
   327         * @since 3.0M
       
   328         * @param "CTtsParsedText& aText" Text which will be normalized
       
   329         */
       
   330         void NormalizeAndSegmentTextL( CTtsParsedText& aText );
       
   331         
       
   332     public: // Functions from base classes
       
   333         
       
   334         /**
       
   335         * From MDevTTSAudioObserver::InitializeComplete
       
   336         *
       
   337         * @param "TInt aError"
       
   338         *        KErrNone if everything is ok, system wide error code otherwise
       
   339         */
       
   340         void MdtaoInitializeComplete( TInt aError );
       
   341         
       
   342         /**
       
   343         * From MDevTTSAudioObserver::BufferToBeFilled
       
   344         *
       
   345         * @param "TDes8& aBuffer"
       
   346         *        Descriptor which should be filled
       
   347         * @param "TInt aSizeRequested"
       
   348         *        Number of bytes requested
       
   349         */
       
   350         void MdtaoBufferToBeFilled( TDes8& aBuffer, TInt aSizeRequested );
       
   351         
       
   352         /**
       
   353         * From MDevTTSAudioObserver::PlayFinished
       
   354         *
       
   355         * @param "TInt aError"
       
   356         *        KErrNone if everything is ok, system wide error code otherwise
       
   357         */
       
   358         void MdtaoPlayFinished( TInt aError );
       
   359         
       
   360         
       
   361         
       
   362         
       
   363         /**
       
   364         * From MTtsHwDeviceObserver::MthdTtsCompleted
       
   365         *
       
   366         * Called by the HW device when the synthesis has been completed.
       
   367         *
       
   368         * @param "TInt aStatus" Error code, KErrNone if success
       
   369         */
       
   370         void MthdTtsCompleted( TInt aStatus );
       
   371         
       
   372         /**
       
   373         * From MTtsHwDeviceObserver::MthdCustomEvent
       
   374         *
       
   375         * Called by the HW device when custom command implementation wants 
       
   376         * to notify a client.
       
   377         *
       
   378         * @param "TInt aEvent" Event code 
       
   379         * @param "TInt aStatus" Error code, KErrNone if success.
       
   380         * @param "TDesC8& aParameter" Optional parameter 
       
   381         */
       
   382         void MthdCustomEvent( TInt aEvent, TInt aStatus, 
       
   383                               const TDesC8& aParameter = KNullDesC8 );
       
   384         
       
   385         /**
       
   386         * From MTtsHwDeviceObserver::MthdProcessBuffer
       
   387         *
       
   388         * Called by the HW device when the HW device has synthesized a new audio buffer.
       
   389         *
       
   390         * @param "TDesC8& aBuffer"
       
   391         *         Synthesized audio buffer.
       
   392         */
       
   393         void MthdProcessBuffer( const TDesC8& aBuffer );
       
   394         
       
   395         /**
       
   396         * From MTtsHwDeviceObserver::MthdoConfigurationData
       
   397         *
       
   398         * Invoked by TTS HW Device when it needs a configuration data package.
       
   399         *
       
   400         * @param "TUint32 aPackageType" Type identifier. 
       
   401         * @param "TUint32 aPackageID" Identifier of package
       
   402         * @param "TUint32 aStartPosition" Start index in bytes
       
   403         * @param "TUint32 aEndPosition" End index in bytes. If the position is bigger 
       
   404         *         than the size of data package, the end part will be returned.
       
   405         * @return Pointer to buffer containing data.
       
   406         */
       
   407         HBufC8* MthdoConfigurationData( TUint32 aPackageType, 
       
   408                                         TUint32 aPackageID, 
       
   409                                         TUint32 aStartPosition, 
       
   410                                         TUint32 aEndPosition );
       
   411 
       
   412         
       
   413         
       
   414 
       
   415         /**
       
   416         * From MASRSTtpHwDeviceObserver::MathdoConfigurationData
       
   417         *
       
   418         * Callback function to load configuration data.
       
   419         * Client provides an implementation.
       
   420         *
       
   421         * @param "TUint32 aPackageType" Type identifier.
       
   422         * @param "TUint32 aPackageID" Package identifier.
       
   423         * @param "TUint32 aStartPosition" First byte.
       
   424         * @param "TUint32 aEndPosition" Last byte. If greater than the size of 
       
   425         *                               the data, rest of the data is given.
       
   426         * @return Pointer to the data buffer, TTP HW Device takes ownership.
       
   427         */	
       
   428         HBufC8* MathdoConfigurationData( TUint32 aPackageType, TUint32 aPackageID,
       
   429                                          TUint32 aStartPosition,
       
   430                                          TUint32 aEndPosition );
       
   431 
       
   432 		/**
       
   433         * From MASRSTtpHwDeviceObserver::MathdoWordListReady
       
   434         *
       
   435         * Callback function to notify that word list is converted.
       
   436         * Client provides an implementation.
       
   437         *
       
   438         * @param "TInt aError" Error code, KErrNone if successful.
       
   439         */	
       
   440 		void MathdoWordListReady( const TInt aError );
       
   441         
       
   442 
       
   443         /**
       
   444         * From MNlpHwDeviceObserver::MnhdNlpCompleted
       
   445         *
       
   446         * Called by the HW device when the synthesis has been completed.
       
   447         * @param aStatus Error code, KErrNone if success
       
   448         * @return none
       
   449         */
       
   450         void MnhdoNlpCompleted( TInt aStatus );
       
   451         
       
   452         /**
       
   453         * From MNlpHwDeviceObserver::MnhdoConfigurationData
       
   454         *
       
   455         * Invoked by TTS HW Device when it needs a configuration data package.
       
   456         * @param aPackageType Type identifier. 
       
   457         * @param aPackageID Identifier of package
       
   458         * @param aStartPosition Start index in bytes
       
   459         * @param aEndPosition End index in bytes. If the position is bigger 
       
   460         *         than the size of data package, the end part will be returned.
       
   461         * @return Data package in a buffer
       
   462         */
       
   463         HBufC8* MnhdoConfigurationData( TUint32 aPackageType, 
       
   464                                         TUint32 aPackageID, 
       
   465                                         TUint32 aStartPosition, 
       
   466                                         TUint32 aEndPosition );
       
   467 
       
   468         /**
       
   469         * From MNlpHwDeviceObserver::MnhdCustomEvent
       
   470         *
       
   471         * Returns style given the style id.
       
   472         */		
       
   473 	   	const TTtsStyle& MnhdoStyleL( TTtsStyleID styleID );
       
   474 
       
   475        
       
   476     protected:  // New functions
       
   477         
       
   478     protected:  // Functions from base classes
       
   479         
       
   480     private:
       
   481         
       
   482         /**
       
   483         * C++ default constructor.
       
   484         */
       
   485         CTTSAlgorithmManager( MDevTTSObserver& aObserver );
       
   486         
       
   487         /**
       
   488         * By default Symbian 2nd phase constructor is private.
       
   489         */
       
   490         void ConstructL( /*CConsoleBase* console*/ );
       
   491         
       
   492         /**
       
   493         * Checks if state is idle and leaves in case of error.
       
   494         */
       
   495         void CheckStateL();
       
   496  
       
   497         // Prohibit copy constructor
       
   498         CTTSAlgorithmManager( const CTTSAlgorithmManager& );
       
   499         // Prohibit assigment operator
       
   500         CTTSAlgorithmManager& operator=( const CTTSAlgorithmManager& );
       
   501         
       
   502     public:     // Data
       
   503         
       
   504     protected:  // Data
       
   505         
       
   506     private:    // Data
       
   507         
       
   508         //State enumeration
       
   509         enum TDevTTSState
       
   510             {
       
   511             EDevTTSNotInitialized = 0,
       
   512             EDevTTSStopped,
       
   513             EDevTTSPrimed,
       
   514             EDevTTSSynthesizing
       
   515             };
       
   516         
       
   517         enum TTTSAudioPlayerState
       
   518             {
       
   519             ETTSAudioPlayerNotInitialized = 0,
       
   520             ETTSAudioPlayerInitializing,
       
   521             ETTSAudioPlayerIdle,
       
   522             ETTSAudioPlayerPlaying,
       
   523             ETTSAudioPlayerRecording, // Reserved for future
       
   524             ETTSAudioPlayerPaused,
       
   525             ETTSAudioPlayerError
       
   526             };
       
   527         
       
   528         
       
   529         // Flag which will be set if there is an ongoing synthesis when we are 
       
   530         // in paused state.
       
   531         TBool iPaused;
       
   532         
       
   533         // Reference to the registered oberserver
       
   534         MDevTTSObserver& iObserver;
       
   535         
       
   536         // For audio playback
       
   537         CDevTTSAudio* iAudio;
       
   538         
       
   539         // Audio device state
       
   540         TTTSAudioPlayerState iAudioState;
       
   541         
       
   542         // Pointer to the tts hw device
       
   543         CTtsHwDevice* iHw;
       
   544         
       
   545         // Stored error value, either from audio device or from TtsHwDevice
       
   546         TInt iError;
       
   547         
       
   548         // Current state of the object
       
   549         TDevTTSState iState;
       
   550         
       
   551         // Current mode of operation, buffers to audio device or to client
       
   552         TDevTTSOutputMode iMode;
       
   553         
       
   554         // Flag which is set when last buffer from TtsHwDevice has arrived
       
   555         TBool iLastBufferFromTts;
       
   556         
       
   557         // Reserved pointer for future extension
       
   558         //TAny* iReserved;
       
   559        
       
   560         // TtpHwDevice
       
   561         CASRSTtpHwDevice* iTtpHw;
       
   562         
       
   563         // Pointer to the beginning of playback data
       
   564         TPtrC8 iPlaybackBuffer;
       
   565         
       
   566         // Start point for the next playback and length of the buffer
       
   567         // These are used to index iPlaybackBuffer
       
   568         TInt iStartPoint;
       
   569         TInt iStopPoint;
       
   570         
       
   571 #ifdef  DEVTTS_3DB_HACK
       
   572 		TBool iApplyGain;
       
   573 #endif
       
   574 
       
   575 #ifdef DEVTTS_AUDIOBUFFER_TO_FILE
       
   576         RFs iSession;
       
   577         RFile iFile;
       
   578         TBool iFileOpen;
       
   579         TInt iFileCounter;
       
   580 #endif
       
   581 
       
   582         // NlpHwDevice pointer
       
   583         CNlpHwDevice* iNlp;
       
   584 
       
   585         // ETrue if NLP has been successfully initialized
       
   586         TBool iNlpInitialized;
       
   587 
       
   588         // Wait loop for audio initialization
       
   589         CActiveSchedulerWait iAudioWait;
       
   590         
       
   591     public:     // Friend classes
       
   592         
       
   593     protected:  // Friend classes
       
   594         
       
   595     private:    // Friend classes
       
   596         
       
   597     };
       
   598     
       
   599 #endif    // DEVTTSALGORITHMMANAGER_H
       
   600     
       
   601 // End of File