srsf/sispeechrecognitiondata/src/nsssidataserialize.h
branchRCL_3
changeset 19 e36f3802f733
parent 0 bf1d17376201
equal deleted inserted replaced
18:cad71a31b7fc 19:e36f3802f733
       
     1 /*
       
     2 * Copyright (c) 2004 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:  Serialization functions for CSIGrammar and CSILexicon
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 #ifndef SIDATASERIALIZE_H
       
    21 #define SIDATASERIALIZE_H
       
    22 
       
    23 //  INCLUDES
       
    24 //#include <?include_file>
       
    25 #include <nsssispeechrecognitiondatadevasr.h>
       
    26 
       
    27 // CONSTANTS
       
    28 //const ?type ?constant_var = ?constant;
       
    29 
       
    30 
       
    31 // The grammar and the lexicon have a binary format.
       
    32 // These IDs identify the format.
       
    33 const TInt32 KBinaryGrammarID   = 0xAACF1234;
       
    34 const TInt32 KBinaryCompiledGrammarID = 0xAACF1235;
       
    35 const TInt32 KBinaryLexiconID   = 0xAACF1236;
       
    36 const TInt32 KBinaryResultSetID = 0xAACF1237;
       
    37 const TInt32 KBinaryParameterID = 0xAACF1238;
       
    38 
       
    39 const TInt32 KBinaryNibble16ID  = 0xAACF1239;
       
    40 const TInt32 KBinaryNibble4ID   = 0xAACF123A;
       
    41 const TInt32 KBinaryRLEID       = 0xAACF123B;
       
    42 const TInt32 KBinaryYesNoID     = 0xAACF123C;
       
    43 
       
    44 const TInt KShortPronunLimit   = 0xffff;
       
    45 
       
    46 // MACROS
       
    47 //#define ?macro ?macro_def
       
    48 
       
    49 // DATA TYPES
       
    50 //enum ?declaration
       
    51 //typedef ?declaration
       
    52 //extern ?data_type;
       
    53 struct TLexiconPhoneme // comment
       
    54     {
       
    55     TBuf8<KMaxPhonemeLength+1> iPhoneme;
       
    56     TInt                       iIndex;
       
    57     };
       
    58 
       
    59 // FUNCTION PROTOTYPES
       
    60 //?type ?function_name(?arg_list);
       
    61 
       
    62 // FORWARD DECLARATIONS
       
    63 //class ?FORWARD_CLASSNAME;
       
    64 
       
    65 // CLASS DECLARATION
       
    66 
       
    67 
       
    68 
       
    69 /**
       
    70 *  Serialization of CSILexicon
       
    71 *  Lexicon is a container of pronunciations. It contains 5 types of information.
       
    72 *  This class puts the informaiton to 5 piles, and uses primitive encoding
       
    73 *  methods to reduce the space needed. The methods used are:
       
    74 *
       
    75 *   * Nibble coding: Suppose you have k 32-bit values, but most of them fit to
       
    76 *                    16 bit. This method creates a k-length array of 16-bit
       
    77 *                    values. If the original value fits, it is placed there.
       
    78 *                    If it is too large, the value in this compact array
       
    79 *                    signals that the real value is in a 32-bit 'overflow' array.
       
    80 *   * RLE coding: For values, which are almost always the same. For example,
       
    81 *                 model bank for pronunciations, and lexicon for rule variants.
       
    82 *
       
    83 *  @lib ?library
       
    84 *  @since ?Series60_version
       
    85 */
       
    86 
       
    87 
       
    88 
       
    89 /**
       
    90 * Codes 32-bit unsigned integers to 16 bits.
       
    91 * Sorts the values to two classes: those, which fit to 16 bits, and those,
       
    92 * which require 32 bits. Then those, which require 32 bits are placed to an
       
    93 * overflow array.
       
    94 *
       
    95 * Space is saved, if over 50% of values fit to 16 bits.
       
    96 *
       
    97 * @lib SISpeechRecognitionData.lib
       
    98 * @since 2.8
       
    99 */
       
   100 class CNibble16Coder : public CBase
       
   101     {
       
   102 public: // Constructors and destructors
       
   103     /**
       
   104     * Codes an array.
       
   105     * @since 2.8
       
   106     * @param aPlainArray Array to be encoded
       
   107     * @return none
       
   108     */
       
   109     static CNibble16Coder* NewL( const RArray<TUint32>& aPlainArray );
       
   110 
       
   111     /**
       
   112     * Reads an array from stream.
       
   113     * @since 2.8
       
   114     * @param aStream a stream, where a CNibble16Coder was previously stored.
       
   115     * @return none
       
   116     */
       
   117     static CNibble16Coder* NewL( RReadStream& aStrem );
       
   118 
       
   119     /**
       
   120     * Destructor..
       
   121     */
       
   122     ~CNibble16Coder();
       
   123 
       
   124 public: // New functions
       
   125     /**
       
   126     * Initializes the iterator.
       
   127     * @since 2.8
       
   128     * @param aIterator The iterator
       
   129     * @return none
       
   130     */
       
   131     void DecodeReset();
       
   132 
       
   133     /**
       
   134     * Decodes one value. Leaves with KErrOverflow, if all values in the array
       
   135     * have been read.
       
   136     * @since 2.8
       
   137     * @return The decoded value.
       
   138     */
       
   139     TUint32 NextL();
       
   140 
       
   141     /**
       
   142     * Externalizes the array to the stream.
       
   143     * @since 2.8
       
   144     * @param aStream Stream to store into.
       
   145     * @return none
       
   146     */
       
   147     void ExternalizeL(RWriteStream& aStream);
       
   148 
       
   149 private:
       
   150     /**
       
   151     * C++ default constructor.
       
   152     */
       
   153     CNibble16Coder();
       
   154 
       
   155 private:
       
   156     // Those values, which fit to 16 bits
       
   157     RArray<TUint32> iMainArray;
       
   158 
       
   159     // The king size values requiring 32 bits
       
   160     RArray<TUint32> iOverflowArray;
       
   161 
       
   162     /**************** Iterator variables ***************/
       
   163     // Intex to the main array
       
   164     TInt iPosition;
       
   165 
       
   166     // Index to the overflow array
       
   167     TInt iOverflowPos;
       
   168     /************** Iterator variables end *************/
       
   169     };
       
   170 
       
   171 /**
       
   172 * Codes 32-bit unsigned integers to 4 bits.
       
   173 * Sorts the values to two classes: those, which fit to 4 bits, and those,
       
   174 * which require 32 bits. Then those, which require 32 bits are placed to an
       
   175 * overflow array.
       
   176 *
       
   177 * @lib SISpeechRecognitionData.lib
       
   178 * @since 2.8
       
   179 */
       
   180 class CNibble4Coder : public CBase
       
   181     {
       
   182 public: // Constructors and destructors
       
   183     /**
       
   184     * Codes an array of 32-bit variables to 2 arrays,
       
   185     * one 4-bit and the other 32-bit.
       
   186     * @since 2.8
       
   187     * @param aMainArray Populated during the function
       
   188     * @param aOverflowArray Populated during the function
       
   189     * @param aPlainArray Array to be encoded
       
   190     * @return none
       
   191     */
       
   192     static CNibble4Coder* NewL( const RArray<TUint32>& aPlainArray );
       
   193 
       
   194     /**
       
   195     * Reads an array from stream.
       
   196     * @since 2.8
       
   197     * @param aStream a stream, where a CNibble4Coder was previously stored.
       
   198     * @return none
       
   199     */
       
   200     static CNibble4Coder* NewL( RReadStream& aStrem );
       
   201 
       
   202     /**
       
   203     * Destructor..
       
   204     */
       
   205     ~CNibble4Coder();
       
   206 
       
   207 public: // New functions
       
   208 
       
   209     /**
       
   210     * Initializes the iterator.
       
   211     * @since 2.8
       
   212     * @param aIterator The iterator
       
   213     * @return none
       
   214     */
       
   215     void DecodeReset();
       
   216 
       
   217     /**
       
   218     * Decodes one value. Leaves with KErrOverflow, if all values in the array
       
   219     * have been read.
       
   220     * @since 2.8
       
   221     * @param aResult Result is placed to this variable.
       
   222     * @param aIterator The iterator
       
   223     * @param aMainArray Contains coded data.
       
   224     * @param aOverflowArray Contains coded data.
       
   225     * @return none
       
   226     */
       
   227     TUint32 NextL();
       
   228 
       
   229     /**
       
   230     * Externalizes the array to the stream.
       
   231     * @since 2.8
       
   232     * @param aStream Stream to store into.
       
   233     * @return none
       
   234     */
       
   235     void ExternalizeL(RWriteStream& aStream);
       
   236 
       
   237 private:
       
   238     /**
       
   239     * C++ default constructor.
       
   240     */
       
   241     CNibble4Coder();
       
   242 
       
   243 private:
       
   244     // Those values, which fit to 4 bits
       
   245     RArray<TUint32> iMainArray;
       
   246 
       
   247     // The values requiring more
       
   248     RArray<TUint32> iOverflowArray;
       
   249 
       
   250     /**************** Iterator variables ***************/
       
   251     // Intex to the main array
       
   252     TInt iPosition;
       
   253 
       
   254     // 4-bit slot in the 32-bit entry
       
   255     TInt iSlot;
       
   256 
       
   257     // Index to the overflow array
       
   258     TInt iOverflowPos;
       
   259     /************** Iterator variables end *************/
       
   260     };
       
   261 
       
   262 /**
       
   263 * Codes 32-bit unsigned integers using Run Length Encoding.
       
   264 *
       
   265 * @lib SISpeechRecognitionData.lib
       
   266 * @since 2.8
       
   267 */
       
   268 class CRLECoder : public CBase
       
   269     {
       
   270 public: // Constructors and destructors
       
   271     /**
       
   272     * Codes an array of 32-bit variables using Run Length Encoding.
       
   273     * @since 2.8
       
   274     * @param aPlainArray Array to be coded
       
   275     * @return none
       
   276     */
       
   277     static CRLECoder* NewL(const RArray<TUint16>& aPlainArray);
       
   278 
       
   279     /**
       
   280     * Internalizes an array from stream.
       
   281     * @since 2.8
       
   282     * @param aStream A stream containing the object.
       
   283     * @return none
       
   284     */
       
   285     static CRLECoder* NewL( RReadStream& aStream );
       
   286 
       
   287     /**
       
   288     * Destructor..
       
   289     */
       
   290     ~CRLECoder();
       
   291 
       
   292 public: // New functions
       
   293 
       
   294     /**
       
   295     * Initializes the iterator.
       
   296     * @since 2.8
       
   297     * @return none
       
   298     */
       
   299     void DecodeReset();
       
   300 
       
   301     /**
       
   302     * Decodes one value. Leaves with KErrOverflow, if all values in the array
       
   303     * have been read.
       
   304     * @since 2.8
       
   305     * @param aResult Result is placed to this variable.
       
   306     * @param aIterator The iterator
       
   307     * @param aRleArray Contains coded data.
       
   308     * @return none
       
   309     */
       
   310     TUint16 NextL();
       
   311 
       
   312     /**
       
   313     * Externalizes the array to the stream.
       
   314     * @since 2.8
       
   315     * @param aStream Stream to store into.
       
   316     * @return none
       
   317     */
       
   318     void ExternalizeL(RWriteStream& aStream);
       
   319 
       
   320 private:
       
   321     /**
       
   322     * C++ default constructor.
       
   323     */
       
   324     CRLECoder();
       
   325 
       
   326 private: // Class-internal data types
       
   327 
       
   328     typedef struct
       
   329         {
       
   330         // How many times the value is repeated
       
   331         TUint16 iCount;
       
   332         // The value
       
   333         TUint16 iValue;
       
   334 
       
   335         } TValueAndCount;
       
   336 private:
       
   337 
       
   338     // The values, stored using run length encoding.
       
   339     RArray<TValueAndCount> iRleArray;
       
   340 
       
   341     /**************** Iterator variables ***************/
       
   342     // Intex to the main array
       
   343     TInt iPosition;
       
   344 
       
   345     // RLE repetition counter
       
   346     TUint16 iRepetition;
       
   347     /************** Iterator variables end *************/
       
   348     };
       
   349 
       
   350 class CYesNoCoder : public CBase
       
   351     {
       
   352 public: // Constructors and destructors
       
   353 
       
   354     /**
       
   355     * 2-phase constructor.
       
   356     */
       
   357     static CYesNoCoder* NewL();
       
   358 
       
   359     /**
       
   360     * Internalizes a bit array from stream.
       
   361     * @since 2.8
       
   362     * @param aStream A stream containing the object.
       
   363     * @return none
       
   364     */
       
   365     static CYesNoCoder* NewL( RReadStream& aStream );
       
   366 
       
   367     /**
       
   368     * Destructor..
       
   369     */
       
   370     ~CYesNoCoder();
       
   371 
       
   372 public: // New functions
       
   373 
       
   374     /**
       
   375     * Encodes a single bit. Increases the iterator. 
       
   376     * @since 2.8
       
   377     * @param aValue The bit to be coded (true or false)
       
   378     * @return none
       
   379     */
       
   380     void EncodeL( TBool aValue );
       
   381 
       
   382     /**
       
   383     * Initializes the iterator.
       
   384     * @since 2.8
       
   385     * @return none
       
   386     */
       
   387     void DecodeReset();
       
   388 
       
   389     /**
       
   390     * Decodes a single bit. Increases the iterator. Leaves, if the storage ends.
       
   391     * @since 2.8
       
   392     * @return Yes or no.
       
   393     */
       
   394     TBool NextL();
       
   395 
       
   396     /**
       
   397     * Externalizes the array to the stream.
       
   398     * @since 2.8
       
   399     * @param aStream Stream to store into.
       
   400     * @return none
       
   401     */
       
   402     void ExternalizeL(RWriteStream& aStream);
       
   403 
       
   404 private:
       
   405     /**
       
   406     * C++ default constructor.
       
   407     */
       
   408     CYesNoCoder();
       
   409 
       
   410     /**
       
   411     * Makes the iterator point to the next bit.
       
   412     */
       
   413     void NextBit();
       
   414 
       
   415 private:
       
   416     // Storage for the bits
       
   417     RArray<TUint32> iStore;
       
   418 
       
   419     /**************** Iterator variables ***************/
       
   420     // Points to the correct TUint32 in the array.
       
   421     TInt iPosition;
       
   422 
       
   423     // Bit mask for getting the next bit.
       
   424     TUint32 iMask;
       
   425     /************** Iterator variables end *************/
       
   426     };
       
   427 
       
   428 /**
       
   429 * Serialization helper class for CSILexicon.
       
   430 *
       
   431 * The native form of CSILexicon is an array of pronunciations. This class
       
   432 * piles each data type to its own pile - model bank IDs, pronunciations,
       
   433 * etc. - and then serializes these piles.
       
   434 *
       
   435 * @lib SISpeechRecognitionData.lib
       
   436 * @since 2.8
       
   437 */
       
   438 class CSILexiconSerializer : public CBase
       
   439     {
       
   440 public:  // Constructors and destructor
       
   441 
       
   442     /**
       
   443     * Two-phased constructor. For externalization.
       
   444     */
       
   445     static CSILexiconSerializer* NewLC( const CSILexicon& aLexicon );
       
   446 
       
   447     /**
       
   448     * Two-phased constructor. For internalization.
       
   449     */
       
   450     static CSILexiconSerializer* NewLC( RReadStream& aStream );
       
   451 
       
   452     /**
       
   453     * Destructor.
       
   454     */
       
   455     virtual ~CSILexiconSerializer();
       
   456         
       
   457 public: // New functions
       
   458         
       
   459     /**
       
   460     * Stores the object to a stream.
       
   461     * @since 2.8
       
   462     * @param aStream Stream to serialize to
       
   463     * @return none
       
   464     */
       
   465     void ExternalizeL( RWriteStream& aStream );
       
   466         
       
   467     /**
       
   468     * Populates a lexicon.
       
   469     * @since 2.8
       
   470     * @param aLexicon The lexicon to internalize to
       
   471     * @param aLexiconID CSILexicon does not contain a function to set
       
   472     *                   Lexicon ID, so we can't return it with aLexicon.
       
   473     * @return none
       
   474     */
       
   475     void RestoreL( CSILexicon& aLexicon, TSILexiconID& aLexiconID );
       
   476 
       
   477 private:
       
   478         
       
   479     /**
       
   480     * C++ default constructor.
       
   481     */
       
   482     CSILexiconSerializer();
       
   483         
       
   484     /**
       
   485     * By default Symbian 2nd phase constructor is private.
       
   486     */
       
   487     void ConstructL(const CSILexicon& aLexicon);
       
   488         
       
   489     /**
       
   490     * By default Symbian 2nd phase constructor is private.
       
   491     */
       
   492     void ConstructL(RReadStream& aStream);
       
   493 
       
   494     void SavePronunciationL(CSIPronunciation* aPronun);
       
   495     bool NextPhonemeL( TDes8& phonemeBuf, const TDesC8& phonemeSeq,
       
   496                        TInt&  aReadIterator );
       
   497     HBufC8* Index2PhonemeLC(const TDesC8& aIndexSeq);
       
   498 
       
   499     void InternalizeL( RReadStream& aStream );
       
   500     // Prohibit copy constructor if not deriving from CBase.
       
   501     // ?classname( const ?classname& );
       
   502     // Prohibit assigment operator if not deriving from CBase.
       
   503     // ?classname& operator=( const ?classname& );
       
   504         
       
   505 public:     // Data
       
   506         // ?one_line_short_description_of_data
       
   507         //?data_declaration;
       
   508         
       
   509 protected:  // Data
       
   510         // ?one_line_short_description_of_data
       
   511         //?data_declaration;
       
   512         
       
   513 private:    // Data
       
   514 
       
   515     // !!!! Check the pronunciation storing once more.
       
   516     RPointerArray<CSIPronunciation> iPronunciations;
       
   517     RPointerArray<HBufC8>           iIndexPronuns;
       
   518 
       
   519     RArray<TLexiconPhoneme> iConversionTable;
       
   520 
       
   521     // Pronunciation IDs as 16-bit nibble-coded numbers
       
   522     CNibble16Coder* iPronunIDs;
       
   523 
       
   524     // Model bank IDs as RLEd 32-bit numbers
       
   525     CRLECoder* iModelBankIDs;
       
   526 
       
   527     // Parameters are serialized as such, since we know nothing about them.
       
   528     CBufFlat* iParamBuf;
       
   529 
       
   530     // Total number of pronunciations
       
   531     TInt iCount; 
       
   532 
       
   533     // Lexicon ID
       
   534     TSILexiconID iLexiconID;
       
   535 
       
   536     };
       
   537 
       
   538 /**
       
   539 * Serialization helper class for CSIGrammar.
       
   540 *
       
   541 * The native form of CSIGrammar is an array of rules and rule variants.
       
   542 * This class piles each data type to its own pile - model bank IDs,
       
   543 * pronunciation IDs, etc. - and then serializes these piles.
       
   544 *
       
   545 * @lib SISpeechRecognitionData.lib
       
   546 * @since 2.8
       
   547 */
       
   548 class CSIGrammarSerializer : public CBase
       
   549     {
       
   550     public:  // Constructors and destructor
       
   551         
       
   552         /**
       
   553         * Two-phased constructor for externalization.
       
   554         */
       
   555         static CSIGrammarSerializer* NewLC( const CSIGrammar& aGrammar );
       
   556         
       
   557         /**
       
   558         * Two-phased constructor for internalization.
       
   559         */
       
   560         static CSIGrammarSerializer* NewLC( RReadStream& aStream );
       
   561 
       
   562         /**
       
   563         * Destructor.
       
   564         */
       
   565         virtual ~CSIGrammarSerializer();
       
   566         
       
   567     public: // New functions
       
   568         
       
   569         /**
       
   570         * ?member_description.
       
   571         * @since ?Series60_version
       
   572         * @param ?arg1 ?description
       
   573         * @return ?description
       
   574         */
       
   575         //?type ?member_function( ?type ?arg1 );
       
   576 
       
   577         /**
       
   578         * Stores the object to a stream.
       
   579         * @since 2.8
       
   580         * @param aStream Stream to serialize to
       
   581         * @return none
       
   582         */
       
   583         void ExternalizeL( RWriteStream& aStream );
       
   584 
       
   585         /**
       
   586         * Populates the grammar.
       
   587         * @since 2.8
       
   588         * @param aGrammar The grammar to be filled.
       
   589         * @param aGrammarID Grammar ID is returned in this variable, as
       
   590         *                   CSIGrammar does not contain SetGrammarID() method.
       
   591         * @return none
       
   592         */
       
   593         void RestoreL( CSIGrammar& aGrammar, TSIGrammarID& aGrammarID );
       
   594 
       
   595     public: // Functions from base classes
       
   596         
       
   597         /**
       
   598         * From ?base_class ?member_description.
       
   599         * @since ?Series60_version
       
   600         * @param ?arg1 ?description
       
   601         * @return ?description
       
   602         */
       
   603         //?type ?member_function( ?type ?arg1 );
       
   604         
       
   605     protected:  // New functions
       
   606         
       
   607         /**
       
   608         * ?member_description.
       
   609         * @since ?Series60_version
       
   610         * @param ?arg1 ?description
       
   611         * @return ?description
       
   612         */
       
   613         //?type ?member_function( ?type ?arg1 );
       
   614         
       
   615     protected:  // Functions from base classes
       
   616         
       
   617         /**
       
   618         * From ?base_class ?member_description
       
   619         */
       
   620         //?type ?member_function();
       
   621         
       
   622     private:
       
   623         
       
   624         /**
       
   625         * C++ default constructor.
       
   626         */
       
   627         CSIGrammarSerializer();
       
   628         
       
   629         /**
       
   630         * By default Symbian 2nd phase constructor is private.
       
   631         */
       
   632         void ConstructL( const CSIGrammar& aGrammar );
       
   633         
       
   634         /**
       
   635         * By default Symbian 2nd phase constructor is private.
       
   636         */
       
   637         void ConstructL( RReadStream& aStream );
       
   638 
       
   639         // Prohibit copy constructor if not deriving from CBase.
       
   640         // ?classname( const ?classname& );
       
   641         // Prohibit assigment operator if not deriving from CBase.
       
   642         // ?classname& operator=( const ?classname& );
       
   643         
       
   644     public:     // Data
       
   645         // ?one_line_short_description_of_data
       
   646         //?data_declaration;
       
   647         
       
   648     protected:  // Data
       
   649         // ?one_line_short_description_of_data
       
   650         //?data_declaration;
       
   651 
       
   652 private:    // Data
       
   653 
       
   654     // Grammar ID
       
   655     TSIGrammarID iGrammarID;
       
   656 
       
   657     // Rule count
       
   658     TInt       iCount;
       
   659 
       
   660     // Rule IDs
       
   661     CNibble16Coder* iRuleIDs;
       
   662 
       
   663     // Numbers of rule variant in rules
       
   664     CNibble4Coder* iRuleVariantCounts;
       
   665 
       
   666     // Rule variant IDs
       
   667     CNibble4Coder* iRuleVariantIDs;
       
   668 
       
   669     // Lexicon IDs of variants
       
   670     CRLECoder* iRuleVariantLexiconIDs;
       
   671 
       
   672     // Languages of variants
       
   673     CNibble4Coder* iRuleVariantLanguages;
       
   674 
       
   675     // Lengths of the pronunciation sequences
       
   676     CNibble4Coder* iPronunIDSeqLengths;
       
   677 
       
   678     // Pronunciation IDs of the pronunciation seuqences
       
   679     CNibble16Coder* iPronunIDSequences;
       
   680 
       
   681     CBufFlat* iParamBuf;
       
   682     // Reserved pointer for future extension
       
   683     //TAny* iReserved;
       
   684     };
       
   685 
       
   686 #endif      // SIDATASERIALIZE_H
       
   687 
       
   688 // End of File