messagingappbase/smartmessaging/ringbc/src/RingBCToneConverter.cpp
changeset 0 72b543305e3a
equal deleted inserted replaced
-1:000000000000 0:72b543305e3a
       
     1 /*
       
     2 * Copyright (c) 2002 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:  
       
    15 *       For checking the validity of Smart messaging ringing tone and for 
       
    16 *       ripping the ringing tone title from binary data. 
       
    17 *
       
    18 */
       
    19 
       
    20 
       
    21 
       
    22 // INCLUDE FILES
       
    23 
       
    24 #include <e32svr.h>
       
    25 #include "RingBCToneConverter.h"
       
    26 #include "NsmRingTone.h"
       
    27 
       
    28 #ifdef _DEBUG_
       
    29 #define DEBUG(s) RDebug::Print( _L(s) )
       
    30 #define DEBUG1(s,t) RDebug::Print( _L(s),t )
       
    31 #else
       
    32 #define DEBUG(s)
       
    33 #define DEBUG1(s,t)
       
    34 #endif
       
    35 
       
    36 
       
    37 
       
    38 // LOCAL CONSTANTS AND MACROS
       
    39 const TInt KCompMaxSongLength = 240; // maximum number of notes
       
    40 
       
    41 
       
    42 // ================= MEMBER FUNCTIONS =======================
       
    43 
       
    44 // ---------------------------------------------------------
       
    45 // NewL()
       
    46 //
       
    47 // ---------------------------------------------------------
       
    48 CRingBCNSMConverter* CRingBCNSMConverter::NewL()
       
    49     {
       
    50 	CRingBCNSMConverter* self = new (ELeave) CRingBCNSMConverter();
       
    51 	CleanupStack::PushL(self);
       
    52 	self->ConstructL();
       
    53 	CleanupStack::Pop();	// self
       
    54 	return self;
       
    55     }
       
    56 
       
    57 // ---------------------------------------------------------
       
    58 // CRingBCNSMConverter()
       
    59 //
       
    60 // ---------------------------------------------------------
       
    61 CRingBCNSMConverter::CRingBCNSMConverter()
       
    62     {
       
    63 	iScale = EScale2;
       
    64 	iStyle = EStyleNatural;
       
    65     }
       
    66 
       
    67 // ---------------------------------------------------------
       
    68 // CRingBCNSMConverter()
       
    69 //
       
    70 // ---------------------------------------------------------
       
    71 void CRingBCNSMConverter::ConstructL()
       
    72     {
       
    73     }
       
    74 
       
    75 // ---------------------------------------------------------
       
    76 // ~CRingBCNSMConverter()
       
    77 //
       
    78 // ---------------------------------------------------------
       
    79 CRingBCNSMConverter::~CRingBCNSMConverter()
       
    80     {
       
    81 	delete iCompData.iSongData; 	
       
    82     }
       
    83 
       
    84 
       
    85 // ---------------------------------------------------------
       
    86 // ConvertNsmToCompDataL()
       
    87 //
       
    88 // ---------------------------------------------------------
       
    89 TCompData* CRingBCNSMConverter::ConvertNsmToCompDataL( const TDesC8& aFileData )
       
    90     {
       
    91 #ifdef _DEBUG_
       
    92     DEBUG( "***BEGIN" );
       
    93 #endif
       
    94 
       
    95 	iReceivedNsmRTData.Set( aFileData );
       
    96 
       
    97     iReadOnly = EFalse;
       
    98 	ResetForCompData();
       
    99 	delete iCompData.iSongData;
       
   100 	iCompData.iSongData = NULL;
       
   101 	iCompData.iSongData = new(ELeave) CArrayFixFlat<TInstruction>( KCompMaxSongLength );
       
   102 
       
   103 #ifdef _DEBUG_
       
   104     DEBUG1( "***NSM DATA LENGTH: %d", iReceivedNsmRTData.Length() );
       
   105 #endif
       
   106     if( iReceivedNsmRTData.Length() == 0 )
       
   107         {
       
   108         DEBUG( "!!!ZERO LENGTH" );
       
   109         User::Leave( KErrCorrupt );
       
   110         }
       
   111 
       
   112 	TInt commandlength( 1 );
       
   113 	while( commandlength != KrtCommandEnd && 
       
   114         iNsmPosition < iReceivedNsmRTData.Length() )
       
   115         {
       
   116 		commandlength = GetBits(KrtCommandLengthBits);
       
   117 #ifdef _DEBUG_
       
   118         DEBUG1( "***NEXT CMD LENGTH: %d", commandlength );
       
   119 #endif
       
   120 		if (commandlength > 3)
       
   121             {
       
   122             DEBUG( "!!!CMD TOO LONG" );
       
   123             User::Leave( KErrCorrupt );
       
   124             }
       
   125 
       
   126 		for (TInt i=0; i<commandlength; i++)
       
   127             {		
       
   128 #ifdef _DEBUG_
       
   129             DEBUG1( "***CMD NUMBER %d", i );
       
   130 #endif
       
   131 			ProcessNsmRingToneCommandPartL();
       
   132             }
       
   133         }
       
   134 
       
   135 	return &iCompData;
       
   136     }
       
   137 
       
   138 // ---------------------------------------------------------
       
   139 // ProcessNsmRingToneCommandPartL()
       
   140 //
       
   141 // ---------------------------------------------------------
       
   142 void CRingBCNSMConverter::ProcessNsmRingToneCommandPartL()
       
   143     {
       
   144 	TInt commandpart( GetBits(KrtCommandPartBits) );
       
   145 #ifdef _DEBUG_
       
   146     DEBUG1( "***CMD PART: %d", commandpart );
       
   147 #endif
       
   148 	switch (commandpart)
       
   149 	    {
       
   150 	    case KrtCommandRTProgramming:
       
   151 			{
       
   152 			iNsmRTFlags |= KrtFlagRTProgramming;
       
   153 		    break;
       
   154 			}
       
   155 	    case KrtCommandUnicode:
       
   156 			{
       
   157 			iNsmRTFlags |= KrtFlagUnicode;
       
   158 		    break;
       
   159 			}
       
   160 	    case KrtCommandCancelCommand:
       
   161 			{
       
   162 		    if (GetBits(KrtCommandPartBits) == KrtCommandUnicode)
       
   163                 {
       
   164 			    iNsmRTFlags &= ~KrtFlagUnicode;
       
   165                 }
       
   166 		    break;
       
   167 			}
       
   168 	    case KrtCommandSound:
       
   169 			{
       
   170 			if ((iNsmRTFlags & KrtFlagRTProgramming) == 0)
       
   171                 {
       
   172                 DEBUG( "!!!CMD SOUND WITH NO RT PROGRAMMING" );
       
   173                 User::Leave( KErrCorrupt );
       
   174                 }
       
   175 		    ProcessNsmRingToneSoundSpecL();
       
   176 		    break;
       
   177 			}
       
   178 	    default:
       
   179 			{
       
   180             // Ignore unknown commands
       
   181             DEBUG( "???CMD UNKNOWN" );
       
   182             iReadOnly = ETrue;
       
   183 		    break;
       
   184 			}
       
   185 	    }
       
   186 
       
   187 	GetBits(KrtFiller); // skip the filler bits
       
   188     }
       
   189 
       
   190 // ---------------------------------------------------------
       
   191 // ProcessNsmRingToneSoundSpecL()
       
   192 //
       
   193 // ---------------------------------------------------------
       
   194 void CRingBCNSMConverter::ProcessNsmRingToneSoundSpecL()
       
   195     {
       
   196 	TInt soundspec( GetBits(KrtSongTypeBits) );
       
   197 #ifdef _DEBUG_
       
   198     DEBUG1( "***SONG TYPE: %d", soundspec );
       
   199 #endif
       
   200 	switch (soundspec)
       
   201 	    {
       
   202 	    case KrtSongTypeBasic:
       
   203 			{
       
   204 		    ProcessNsmRingToneSongTitleL();
       
   205 		    ProcessNsmRingToneSongL();
       
   206 		    break;
       
   207 			}
       
   208 	    case KrtSongTypeTemporary:
       
   209             {
       
   210 		    ProcessNsmRingToneSongL();
       
   211 		    break;
       
   212 			}
       
   213 	    default:
       
   214             {
       
   215 		    iReadOnly = ETrue;
       
   216 		    break;
       
   217 			}
       
   218 	    }
       
   219 
       
   220 	iCompData.iSongLength = iCompDataPosition;
       
   221     }
       
   222 
       
   223 // ---------------------------------------------------------
       
   224 // ProcessNsmRingToneSongTitleL()
       
   225 //
       
   226 // ---------------------------------------------------------
       
   227 void CRingBCNSMConverter::ProcessNsmRingToneSongTitleL()
       
   228 {
       
   229 	TInt titlelength( GetBits(KrtSongTitleTextLengthBits) );
       
   230     DEBUG1( "***TITLE LENGTH %d", titlelength );
       
   231 
       
   232     TInt charWidth( KrtDefaultCharBits );
       
   233 	if( iNsmRTFlags & KrtFlagUnicode )
       
   234     	{
       
   235         charWidth = KrtUnicodeCharBits;
       
   236         }
       
   237     DEBUG1( "***TITLE CHAR WIDTH: %d", charWidth );
       
   238 
       
   239     TBuf<KrtSongTitleMaxLength> title;
       
   240 	for( TInt i( 0 ); i < titlelength; i++ )
       
   241         {
       
   242         title.Append( GetBits(charWidth) );
       
   243         }
       
   244     DEBUG1( "***TITLE: %S", &title );
       
   245 
       
   246     SetTitle( title );
       
   247     }
       
   248 
       
   249 // ---------------------------------------------------------
       
   250 // ProcessNsmRingToneSong()
       
   251 //
       
   252 // ---------------------------------------------------------
       
   253 void CRingBCNSMConverter::ProcessNsmRingToneSongL()
       
   254     {
       
   255 	TInt songsequencelength( GetBits(KrtSongsequenceLengthBits) );
       
   256     DEBUG1( "***SEQUENCE LENGTH %d", songsequencelength );
       
   257 
       
   258 	/*if( songsequencelength == 0 )
       
   259 		{
       
   260 		iReadOnly = ETrue;
       
   261 		}*/
       
   262 
       
   263 	for (TInt i( 0 ); i<songsequencelength; i++)
       
   264 	    {
       
   265 		TInt patternheaderid( GetBits(KrtInstructionIdLengthBit) );
       
   266         DEBUG1( "***PATTERN HEADER ID: %d", patternheaderid );
       
   267 		if (patternheaderid != EPatternHeaderId)
       
   268             {
       
   269             DEBUG( "!!!PATTERN HEADER ID INVALID" );
       
   270 			User::Leave(KErrCorrupt);
       
   271             }
       
   272 
       
   273 		TInt patternid( GetBits(KrtPatternIdLengthBit) );
       
   274         DEBUG1( "***PATTERN ID: %d", patternid );
       
   275 		TInt loopvalue( GetBits(KrtPatternLoopValueBits) );
       
   276         DEBUG1( "***PATTERN LOOP VALUE: %d", loopvalue );
       
   277 		TInt patternspec( GetBits(KrtPatternSpecBits) );
       
   278         DEBUG1( "***PATTERN SPEC: %d", patternspec );
       
   279 		
       
   280 		if( patternspec != KrtPatternDefined )
       
   281 		    {
       
   282             DEBUG( "***PATTERN NOT DEFINED" );
       
   283 			iNsmRTPatterns[patternid].iStart = iCompData.iSongData->Count();
       
   284 			
       
   285             for (TInt l( 0 ); l < patternspec && iCompDataPosition < KCompMaxSongLength; l++)
       
   286                 {
       
   287 				ProcessNsmRingTonePatternInstructionL();
       
   288                 }
       
   289 			iNsmRTPatterns[patternid].iEnd = iCompDataPosition - 1;
       
   290 		    }
       
   291 		// check if the pattern is really already defined before copying
       
   292 		TInt start( iNsmRTPatterns[patternid].iStart );
       
   293 		TInt end( iNsmRTPatterns[patternid].iEnd );
       
   294 		if( start < end )
       
   295 		    {
       
   296 			if( loopvalue < KrtPatternRepeatInfinite )
       
   297 			    {
       
   298 				for( TInt loopNum( 0 ); loopNum < loopvalue; loopNum++ ) // handle repeat
       
   299 				    {
       
   300 					for (TInt k=start; (k<=end)&&(iCompDataPosition<KCompMaxSongLength); 
       
   301 						k++, iCompDataPosition++)
       
   302                         {
       
   303 						iCompData.iSongData->AppendL( iCompData.iSongData->At( k ) );
       
   304                         }
       
   305 				    }
       
   306 			    }
       
   307 			else // handle infinite repeat
       
   308 			    {
       
   309 				while( iCompDataPosition<KCompMaxSongLength )
       
   310 				    {
       
   311 					for (TInt k=start; (k<=end)&&(iCompDataPosition<KCompMaxSongLength); 
       
   312 						k++, iCompDataPosition++)
       
   313                         {
       
   314 						iCompData.iSongData->AppendL( iCompData.iSongData->At( k ) );
       
   315                         }
       
   316 				    }
       
   317 			}
       
   318 		}
       
   319 	}
       
   320 }
       
   321 
       
   322 // ---------------------------------------------------------
       
   323 // ProcessNsmRingTonePatternInstruction()
       
   324 //
       
   325 // ---------------------------------------------------------
       
   326 void CRingBCNSMConverter::ProcessNsmRingTonePatternInstructionL()
       
   327     {
       
   328 	TInt instructionid( GetBits(KrtInstructionIdLengthBit) );
       
   329     DEBUG1( "***INSTRUCTION ID: %d", instructionid );
       
   330 	switch (instructionid)
       
   331 	    {
       
   332 	    case ENoteInstructionId:
       
   333 			{
       
   334             DEBUG1( "***INSTRUCTION ID: NOTE", instructionid );
       
   335 		    ProcessNsmRingToneNoteInstructionL(iScale, iStyle);
       
   336 		    break;
       
   337 			}
       
   338 	    case EScaleInstructionId:
       
   339 			{
       
   340 			iScale = GetBits(KrtNoteScaleBits);
       
   341             DEBUG1( "***INSTRUCTION SCALE: %d", iScale );
       
   342 		    break;
       
   343 			}
       
   344 	    case EStyleInstructionId:
       
   345 			{
       
   346 			iStyle = GetBits(KrtNoteStyleBits);
       
   347             DEBUG1( "***INSTRUCTION STYLE: %d", iStyle );
       
   348             if( iStyle == EStyleReserved )
       
   349                 {
       
   350                 DEBUG( "???INSTRUCTION STYLE NOT SUPPORTED" );
       
   351                 iReadOnly = ETrue;
       
   352                 }
       
   353 		    break;
       
   354 			}
       
   355 	    case ETempoInstructionId:
       
   356 			{
       
   357 			iCompData.iTempo = GetBits(KrtTempoBits);
       
   358             DEBUG1( "***INSTRUCTION TEMPO: %d", iCompData.iTempo );
       
   359             if( iCompData.iTempo > ETempo250 )
       
   360                 {
       
   361                 DEBUG( "???TEMPO TOO HIGH" );
       
   362                 iReadOnly = ETrue;
       
   363                 }
       
   364 		    break;
       
   365 			}
       
   366 	    case EVolumeInstructionId:
       
   367             {
       
   368 			DEBUG( "***INSTRUCTION VOLUME" );
       
   369 		    GetBits(KrtVolumebits);
       
   370 		    break;
       
   371 			}
       
   372 	    default:
       
   373             {
       
   374 			DEBUG( "???INSTRUCTION NOT SUPPORTED" );
       
   375             iReadOnly = ETrue;
       
   376 		    break;
       
   377 			}
       
   378 	    }
       
   379     }
       
   380 
       
   381 // ---------------------------------------------------------
       
   382 // ProcessNsmRingToneNoteInstruction()
       
   383 //
       
   384 // ---------------------------------------------------------
       
   385 void CRingBCNSMConverter::ProcessNsmRingToneNoteInstructionL(TInt aScale, TInt aStyle)
       
   386     {
       
   387 	TInt notevalue( GetBits(KrtNoteValueBits) );
       
   388     DEBUG1( "***NOTE VALUE: %d", notevalue );
       
   389     if( notevalue != ENotePause && (notevalue > ENoteB 
       
   390         || iScale == EScale4 && notevalue > ENoteGis
       
   391         || iScale == EScale1 && notevalue < ENoteG ) )
       
   392         {
       
   393         DEBUG( "???NOTE VALUE OUT OF RANGE" );
       
   394         iReadOnly = ETrue;
       
   395         }
       
   396 
       
   397 	TInt noteduration( GetBits(KrtNoteDurationBits) );
       
   398     DEBUG1( "***NOTE DURATION: %d", noteduration );
       
   399     if( noteduration > EDurationThirtysecond )
       
   400         {
       
   401         DEBUG( "???NOTE DURATION OUT OF RANGE" );
       
   402         iReadOnly = ETrue;
       
   403         }
       
   404 
       
   405 	TInt notedurspecifier( GetBits(KrtNoteDurSpecifierBits) );
       
   406     DEBUG1( "***NOTE DURATION SPECIFIER: %d", notedurspecifier);
       
   407     if( notedurspecifier == EDurSpecifireDoubleDotted
       
   408         || notedurspecifier > EDurSpecifierTriplet )
       
   409         {
       
   410         DEBUG( "???NOTE DURATION SPECIFIER OUT OF RANGE" );
       
   411         iReadOnly = ETrue;
       
   412         }
       
   413 
       
   414 	TInstruction symbol;
       
   415 	symbol.iValue = notevalue;
       
   416 	symbol.iDuration = noteduration;
       
   417 	symbol.iDurspecifier = notedurspecifier;
       
   418 	symbol.iScale = aScale;
       
   419 	symbol.iStyle = aStyle;
       
   420 
       
   421 	iCompData.iSongData->AppendL( symbol );
       
   422 	iCompDataPosition ++;
       
   423     }
       
   424 
       
   425 // ---------------------------------------------------------
       
   426 // GetBits()
       
   427 //
       
   428 // ---------------------------------------------------------
       
   429 TInt CRingBCNSMConverter::GetBits(TInt aNumBits)
       
   430     {
       
   431 	TUint32 buf( 0 );   
       
   432 	if( aNumBits == 0 ) // handle byte alignment
       
   433 	    {
       
   434 		if (iNsmPositionBit != 0)
       
   435 		    {
       
   436 			iNsmPositionBit = 0;	// skip filler bits
       
   437 			iNsmPosition ++;			
       
   438 		    }
       
   439 		return buf;
       
   440 	    }
       
   441 
       
   442 	for (TInt n=0; n<4; n++)
       
   443 	    {
       
   444 		buf <<= 8;
       
   445 		if ( iNsmPosition+n < iReceivedNsmRTData.Length() )
       
   446             {
       
   447 			buf |= iReceivedNsmRTData[iNsmPosition+n];
       
   448             }
       
   449 	    }
       
   450 
       
   451 	TUint32 filter = 0;
       
   452 	buf = (buf >> (32 - iNsmPositionBit - aNumBits)) & ~(~filter << aNumBits);
       
   453 
       
   454 	iNsmPositionBit += aNumBits;
       
   455 	while (iNsmPositionBit > 7)
       
   456 	    {
       
   457 		iNsmPositionBit -= 8;
       
   458 		iNsmPosition ++;
       
   459 	    }
       
   460 	return buf;
       
   461     }
       
   462 
       
   463 // ---------------------------------------------------------
       
   464 // ResetForCompData()
       
   465 //
       
   466 // ---------------------------------------------------------
       
   467 void CRingBCNSMConverter::ResetForCompData()
       
   468     {
       
   469 	iNsmRTPatterns[EPatternIdA].iStart = 0;
       
   470 	iNsmRTPatterns[EPatternIdA].iEnd = 0;
       
   471 	iNsmRTPatterns[EPatternIdB].iStart = 0;
       
   472 	iNsmRTPatterns[EPatternIdB].iEnd = 0;
       
   473 	iNsmRTPatterns[EPatternIdC].iStart = 0;
       
   474 	iNsmRTPatterns[EPatternIdC].iEnd = 0;
       
   475 	iNsmRTPatterns[EPatternIdD].iStart = 0;
       
   476 	iNsmRTPatterns[EPatternIdD].iEnd = 0;
       
   477 	iNsmRTFlags = 0;
       
   478     iNsmPosition = 0;
       
   479 	iNsmPositionBit = 0;
       
   480 	iCompDataPosition = 0;
       
   481 
       
   482 	iCompData.iTempo = KrtBpmDefault;
       
   483 	iCompData.iSongLength = 0;
       
   484     }
       
   485 
       
   486 // ---------------------------------------------------------
       
   487 // SetTitle()
       
   488 //
       
   489 // ---------------------------------------------------------
       
   490 void CRingBCNSMConverter::SetTitle( const TDesC& aFileName )
       
   491     {
       
   492     iCompData.iSongTitle = aFileName.Left( KrtSongTitleTextLength );
       
   493     }
       
   494 
       
   495 
       
   496 // ---------------------------------------------------------
       
   497 // TitleLC()
       
   498 //
       
   499 // ---------------------------------------------------------
       
   500 HBufC* CRingBCNSMConverter::TitleLC(TPtr8& aFileData)
       
   501 {
       
   502 	iReceivedCompData = ConvertNsmToCompDataL(aFileData);
       
   503 
       
   504 	HBufC* titlePtr = iReceivedCompData->iSongTitle.AllocLC();
       
   505 	return titlePtr;
       
   506 }
       
   507 
       
   508 
       
   509 // ---------------------------------------------------------
       
   510 // IsRingToneMimeTypeL()
       
   511 //
       
   512 // ---------------------------------------------------------
       
   513 TBool  CRingBCNSMConverter::IsRingToneMimeTypeL(TPtr8& aFileData)
       
   514     {
       
   515 	TRAPD( returnError, 
       
   516         iReceivedCompData = ConvertNsmToCompDataL(aFileData) );
       
   517     if( returnError != KErrCorrupt 
       
   518         && returnError != KErrNotSupported )
       
   519         {
       
   520         User::LeaveIfError( returnError );
       
   521         }
       
   522 
       
   523     return returnError != KErrCorrupt;
       
   524     }
       
   525 // end of file