profilesservices/RingingTone3DPlugin/src/C3DAudioPattern.cpp
branchRCL_3
changeset 24 8ee96d21d9bf
parent 23 8bda91a87a00
child 25 7e0eff37aedb
equal deleted inserted replaced
23:8bda91a87a00 24:8ee96d21d9bf
     1 /*
       
     2 * Copyright (c) 2005 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:  Handles accessing 3D motion pattern data.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include "C3DAudioPattern.h"		// C3DAudioPattern
       
    22 #include <s32std.h>				// RReadStream, RWriteStream
       
    23 
       
    24 // CONSTANTS
       
    25 // Granularity for array holding updates.
       
    26 const TInt KPatternArrayGranularity = 84;
       
    27 
       
    28 // Maximum length for name of the pattern.
       
    29 const TUint KMaxPatternNameLength = 100;
       
    30 
       
    31 // ============================ MEMBER FUNCTIONS ===============================
       
    32 
       
    33 // -----------------------------------------------------------------------------
       
    34 // C3DAudioPattern::NewL
       
    35 // Two-phased constructor. 
       
    36 // -----------------------------------------------------------------------------
       
    37 //
       
    38 C3DAudioPattern* C3DAudioPattern::NewL()
       
    39     {
       
    40     C3DAudioPattern * self = C3DAudioPattern::NewLC();
       
    41     CleanupStack::Pop( self );
       
    42     return self;
       
    43     }
       
    44 
       
    45 // -----------------------------------------------------------------------------
       
    46 // C3DAudioPattern::NewLC
       
    47 // Two-phased constructor. Leaves pointer on cleanup stack.
       
    48 // -----------------------------------------------------------------------------
       
    49 //
       
    50 C3DAudioPattern* C3DAudioPattern::NewLC()
       
    51     {
       
    52     C3DAudioPattern * self = new ( ELeave ) C3DAudioPattern();
       
    53     CleanupStack::PushL( self );
       
    54     return self;
       
    55     }
       
    56 
       
    57 // -----------------------------------------------------------------------------
       
    58 // C3DAudioPattern::NewL
       
    59 // Two-phased constructor. 
       
    60 // -----------------------------------------------------------------------------
       
    61 //
       
    62 
       
    63 C3DAudioPattern* C3DAudioPattern::NewL( RReadStream& aStream )
       
    64 	{
       
    65 	C3DAudioPattern* self = C3DAudioPattern::NewLC( aStream );
       
    66 	CleanupStack::Pop( self );
       
    67 	return self;
       
    68 	}
       
    69 
       
    70 
       
    71 // -----------------------------------------------------------------------------
       
    72 // C3DAudioPattern::NewLC
       
    73 // Two-phased constructor. Leaves pointer on cleanup stack.
       
    74 // -----------------------------------------------------------------------------
       
    75 //
       
    76 C3DAudioPattern* C3DAudioPattern::NewLC( RReadStream& aStream )
       
    77 	{
       
    78 	C3DAudioPattern* self = new ( ELeave ) C3DAudioPattern();
       
    79 	CleanupStack::PushL( self );
       
    80 	self->InternalizeL( aStream );
       
    81 	return self;
       
    82 	}
       
    83 
       
    84 
       
    85 // Destructor
       
    86 C3DAudioPattern::~C3DAudioPattern()
       
    87     {
       
    88     iScript.Close();
       
    89     iLoops.Close();
       
    90     iOpenLoops.Close();
       
    91     delete iName;
       
    92     }
       
    93 
       
    94 // -----------------------------------------------------------------------------
       
    95 // C3DAudioPattern::ExternalizeL
       
    96 // Externalizes pattern to writestream.
       
    97 // -----------------------------------------------------------------------------
       
    98 //
       
    99 void C3DAudioPattern::ExternalizeL(	RWriteStream& aStream ) const
       
   100 	{
       
   101 	// Name of the pattern
       
   102 	aStream << *iName;
       
   103 	
       
   104 	// Initial doppler and reverb.
       
   105 	aStream.WriteInt8L( iInitialDoppler );
       
   106 	aStream.WriteUint8L( iInitialReverb );
       
   107 
       
   108 	// Number of loops
       
   109 	aStream.WriteUint8L( iLoops.Count() );
       
   110 	// Each loop
       
   111 	for ( TInt i = 0; i < iLoops.Count(); ++i )
       
   112 		{
       
   113 		aStream.WriteUint8L( iLoops[i].iLoopStart );
       
   114 		aStream.WriteUint8L( iLoops[i].iLoopEnd );
       
   115 		aStream.WriteUint8L( iLoops[i].iCount );
       
   116 		}
       
   117 
       
   118 	// Number of updates in the script		
       
   119 	aStream.WriteUint8L( iScript.Count() );
       
   120 	// Each update
       
   121 	for ( TInt i = 0; i < iScript.Count(); ++i )
       
   122 		{
       
   123 		// Position of the update
       
   124 		aStream.WriteInt16L( iScript[i].iPosition.iX );
       
   125 		aStream.WriteInt16L( iScript[i].iPosition.iY );
       
   126 		aStream.WriteInt16L( iScript[i].iPosition.iZ );
       
   127 		
       
   128 		// Velocity of the update
       
   129 		aStream.WriteInt8L( iScript[i].iVelocity.iScalar );
       
   130 		aStream.WriteUint16L( iScript[i].iVelocity.iScalarVelocity );
       
   131 		aStream.WriteInt16L( iScript[i].iVelocity.iXVector );
       
   132 		aStream.WriteInt16L( iScript[i].iVelocity.iYVector );
       
   133 		aStream.WriteInt16L( iScript[i].iVelocity.iZVector );
       
   134 		
       
   135 		// Time reserved for the update.
       
   136 		aStream.WriteUint16L( iScript[i].iDTime );
       
   137 		}
       
   138 	}
       
   139 
       
   140 // -----------------------------------------------------------------------------
       
   141 // C3DAudioPattern::InternalizeL
       
   142 // Internalizes pattern from readstream
       
   143 // -----------------------------------------------------------------------------
       
   144 //
       
   145 void C3DAudioPattern::InternalizeL(	RReadStream& aStream )
       
   146 	{
       
   147 	// Internalize the name from stream.
       
   148 	delete iName;
       
   149 	iName = NULL;
       
   150 	iName = HBufC8::NewL( aStream, KMaxPatternNameLength );
       
   151 	
       
   152 	// Initial doppler and reverberation
       
   153 	iInitialDoppler = aStream.ReadInt8L();
       
   154 	iInitialReverb = aStream.ReadUint8L();
       
   155 
       
   156 	// Reset the array of loops.
       
   157 	iLoops.Reset();
       
   158 	TInt loopCount = aStream.ReadUint8L();
       
   159 	// Read each loop.
       
   160 	for (TInt i = 0; i < loopCount; ++i)
       
   161 		{
       
   162 		T3DLoop tempLoop;
       
   163 		tempLoop.iLoopStart = aStream.ReadUint8L();
       
   164 		tempLoop.iLoopEnd = aStream.ReadUint8L();
       
   165 		tempLoop.iCount = aStream.ReadUint8L();
       
   166 		iLoops.AppendL( tempLoop );
       
   167 		}
       
   168 	
       
   169 	// Reset the array of loops.
       
   170 	iScript.Reset();
       
   171 	TInt scriptCount = aStream.ReadUint8L();
       
   172 	// Read each update.
       
   173 	for (TInt i = 0; i < scriptCount; ++i)
       
   174 		{
       
   175 		T3DPosition tempPosition;
       
   176 		tempPosition.iX = aStream.ReadInt16L();
       
   177 		tempPosition.iY = aStream.ReadInt16L();
       
   178 		tempPosition.iZ = aStream.ReadInt16L();
       
   179 		
       
   180 		T3DVelocity tempVelocity;
       
   181 		tempVelocity.iScalar = aStream.ReadInt8L();
       
   182 		tempVelocity.iScalarVelocity = aStream.ReadUint16L();
       
   183 		tempVelocity.iXVector = aStream.ReadInt16L();
       
   184 		tempVelocity.iYVector = aStream.ReadInt16L();
       
   185 		tempVelocity.iZVector = aStream.ReadInt16L();
       
   186 		
       
   187 		T3DScriptUpdate tempScript;
       
   188 		tempScript.iDTime = aStream.ReadUint16L();
       
   189 		tempScript.iPosition = tempPosition;
       
   190 		tempScript.iVelocity = tempVelocity;
       
   191 		iScript.AppendL( tempScript );
       
   192 		}
       
   193 	}
       
   194 
       
   195 // -----------------------------------------------------------------------------
       
   196 // C3DAudioPattern::SetName
       
   197 // Set the name of the pattern. Memory will be allocated.
       
   198 // -----------------------------------------------------------------------------
       
   199 //
       
   200 void C3DAudioPattern::SetNameL( const TDesC8& aName )
       
   201 	{
       
   202 	delete iName;
       
   203 	iName = NULL;
       
   204 	iName = aName.AllocL();
       
   205 	}
       
   206 
       
   207 // -----------------------------------------------------------------------------
       
   208 // C3DAudioPattern::SetReverb
       
   209 // Set the value of reverberation.
       
   210 // -----------------------------------------------------------------------------
       
   211 //	
       
   212 void C3DAudioPattern::SetReverb( const TUint aReverb )
       
   213 	{
       
   214 	iInitialReverb = aReverb;
       
   215 	}
       
   216 
       
   217 // -----------------------------------------------------------------------------
       
   218 // C3DAudioPattern::SetDoppler
       
   219 // Set the status of doppler.
       
   220 // -----------------------------------------------------------------------------
       
   221 //		
       
   222 void C3DAudioPattern::SetDoppler( const TBool aDoppler )
       
   223 	{
       
   224 	iInitialDoppler = aDoppler;
       
   225 	}
       
   226 
       
   227 // -----------------------------------------------------------------------------
       
   228 // C3DAudioPattern::AddUpdate
       
   229 // Adds a update to script.
       
   230 // -----------------------------------------------------------------------------
       
   231 //
       
   232 void C3DAudioPattern::AddUpdateL( T3DScriptUpdate& aUpdate )
       
   233 	{
       
   234 	User::LeaveIfError( iScript.Append( aUpdate ) );
       
   235 	}
       
   236 
       
   237 // -----------------------------------------------------------------------------
       
   238 // C3DAudioPattern::StartLoop
       
   239 // Start loop at current place in the script.
       
   240 // -----------------------------------------------------------------------------
       
   241 //
       
   242 void C3DAudioPattern::StartLoopL( TUint& aCount )
       
   243 	{
       
   244 	// Create T3DLoop and append it to array.
       
   245 	T3DLoop loop = { iScript.Count(), 0, aCount };
       
   246 	User::LeaveIfError( iLoops.Append( loop ) );
       
   247 	
       
   248 	// 	Update open loops array, to be able to determine which loop to end.
       
   249 	User::LeaveIfError( iOpenLoops.Append( iLoops.Count() - 1 ) );
       
   250 	}
       
   251 
       
   252 // -----------------------------------------------------------------------------
       
   253 // C3DAudioPattern::EndLoop
       
   254 // End the lastly started loop.
       
   255 // -----------------------------------------------------------------------------
       
   256 //	
       
   257 void C3DAudioPattern::EndLoop()
       
   258 	{
       
   259 	// There is at least one open loop.
       
   260 	if ( iOpenLoops.Count() != 0 )
       
   261 		{
       
   262 		// Get the index of the loop.
       
   263 		TInt aIndex = iOpenLoops[ iOpenLoops.Count() - 1 ];
       
   264 		// Set the index of loop end and remove loop from array of open loops.
       
   265 		iLoops[ aIndex ].iLoopEnd = iScript.Count() - 1;
       
   266 		iOpenLoops.Remove( iOpenLoops.Count() - 1 );
       
   267 		}
       
   268 	}
       
   269 
       
   270 // -----------------------------------------------------------------------------
       
   271 // C3DAudioPattern::Name
       
   272 // Get the name of the pattern.
       
   273 // -----------------------------------------------------------------------------
       
   274 //	
       
   275 const TDesC8& C3DAudioPattern::Name()
       
   276     {
       
   277     return *iName;
       
   278     }
       
   279 
       
   280 // -----------------------------------------------------------------------------
       
   281 // C3DAudioPattern::Script
       
   282 // Get the script of pattern.
       
   283 // -----------------------------------------------------------------------------
       
   284 //	
       
   285 RArray< T3DScriptUpdate > C3DAudioPattern::Script()
       
   286 	{
       
   287 	return iScript;
       
   288 	}
       
   289 
       
   290 // -----------------------------------------------------------------------------
       
   291 // C3DAudioPattern::DopplerAvailable
       
   292 // Is doppler available in the pattern.
       
   293 // -----------------------------------------------------------------------------
       
   294 //		
       
   295 TBool C3DAudioPattern::DopplerAvailable() const
       
   296 	{
       
   297 	return iInitialDoppler;
       
   298 	}
       
   299 
       
   300 // -----------------------------------------------------------------------------
       
   301 // C3DAudioPattern::Reverb
       
   302 // Get initial reverb of pattern.
       
   303 // -----------------------------------------------------------------------------
       
   304 //			
       
   305 TInt C3DAudioPattern::Reverb() const
       
   306 	{
       
   307 	return iInitialReverb;
       
   308 	}
       
   309 
       
   310 // -----------------------------------------------------------------------------
       
   311 // C3DAudioPattern::C3DAudioPattern
       
   312 // C++ default constructor can NOT contain any code, that
       
   313 // might leave.
       
   314 // -----------------------------------------------------------------------------
       
   315 //
       
   316 C3DAudioPattern::C3DAudioPattern(): 
       
   317 	iScript( KPatternArrayGranularity )
       
   318     {
       
   319     }
       
   320 
       
   321 // End of File