devsoundextensions/effects/DopplerBase/Src/DopplerBase.cpp
changeset 0 40261b775718
equal deleted inserted replaced
-1:000000000000 0:40261b775718
       
     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:   Implementation of the doppler effect class
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 // INCLUDE FILES
       
    22 
       
    23 #ifdef _DEBUG
       
    24 #include <e32svr.h>
       
    25 #endif
       
    26 
       
    27 #include <DopplerBase.h>
       
    28 #include <e32math.h>
       
    29 #include <math.h>
       
    30 
       
    31 
       
    32 //360 degrees:
       
    33 #define TWO_PI 6283
       
    34 //180 degrees:
       
    35 #define PI 3142
       
    36 //90 degrees:
       
    37 #define QUARTER_PI 1570
       
    38 
       
    39 // ============================ MEMBER FUNCTIONS ===============================
       
    40 
       
    41 // -----------------------------------------------------------------------------
       
    42 // CDoppler::CDoppler
       
    43 // C++ default constructor can NOT contain any code, that
       
    44 // might leave.
       
    45 // -----------------------------------------------------------------------------
       
    46 //
       
    47 EXPORT_C CDoppler::CDoppler()
       
    48     : 	iDopplerData(),
       
    49     	iDataPckgTo(iDopplerData),
       
    50     	iDataPckgFrom(iDopplerData)
       
    51     {
       
    52     }
       
    53 
       
    54 // Destructor
       
    55 EXPORT_C CDoppler::~CDoppler()
       
    56     {
       
    57     }
       
    58 
       
    59 
       
    60 // -----------------------------------------------------------------------------
       
    61 // CDoppler::CartesianVelocity
       
    62 // -----------------------------------------------------------------------------
       
    63 //
       
    64 
       
    65 EXPORT_C void CDoppler::CartesianVelocity( TInt32& aX, TInt32& aY, TInt32& aZ )
       
    66 	{
       
    67 		aX = iDopplerData.iVelocityX;
       
    68 		aY = iDopplerData.iVelocityY;
       
    69 		aZ = iDopplerData.iVelocityZ;
       
    70 	}
       
    71 
       
    72 // -----------------------------------------------------------------------------
       
    73 // CDoppler::Factor
       
    74 // -----------------------------------------------------------------------------
       
    75 //
       
    76 
       
    77 EXPORT_C TUint32 CDoppler::Factor() const
       
    78 	{
       
    79 		return iDopplerData.iFactor;
       
    80 	}
       
    81 
       
    82 // -----------------------------------------------------------------------------
       
    83 // CDoppler::FactorMax
       
    84 // -----------------------------------------------------------------------------
       
    85 //
       
    86 
       
    87 EXPORT_C TUint32 CDoppler::FactorMax() const
       
    88 	{
       
    89 		return iDopplerData.iMaxFactor;
       
    90 	}
       
    91 // -----------------------------------------------------------------------------
       
    92 // CDoppler::SetCartesianVelocityL
       
    93 // -----------------------------------------------------------------------------
       
    94 //
       
    95 
       
    96 EXPORT_C void CDoppler::SetCartesianVelocityL( TInt32 aX, TInt32 aY, TInt32 aZ )
       
    97 	{
       
    98 	    iDopplerData.iVelocityX = aX;
       
    99 		iDopplerData.iVelocityY = aY;
       
   100 		iDopplerData.iVelocityZ = aZ;
       
   101 
       
   102 
       
   103         TReal SqrtXYZ = 0, squareX = 0, squareY = 0, squareZ = 0;
       
   104         Math::Pow(squareX, aX, 2);
       
   105         Math::Pow(squareY, aY, 2);
       
   106         Math::Pow(squareZ, aZ, 2);
       
   107         
       
   108         TReal sum = squareX + squareY + squareZ; 
       
   109  
       
   110         Math::Sqrt(SqrtXYZ, sum);
       
   111         
       
   112         //Singularity region 
       
   113         if(!((aX==0) && (aZ==0)))
       
   114         {
       
   115 
       
   116           TReal zDividedByXAtan = atan2 (-aX,  -aZ);
       
   117  
       
   118           if (zDividedByXAtan > 0)
       
   119             iDopplerData.iAzimuth = -(TInt32) (zDividedByXAtan * 1000 + 0.5);
       
   120           else
       
   121             iDopplerData.iAzimuth = -(TInt32) (zDividedByXAtan * 1000 - 0.5);
       
   122         }
       
   123 	    // else { we are exactly on Y-axis and therefore azimuth is undefined; let's use the previous azimuth value instead } 
       
   124 
       
   125  
       
   126        if (!((aX ==0) && (aY == 0) && (aZ == 0))) 
       
   127        {
       
   128 
       
   129          TReal result;
       
   130          TReal yDividedBySqrtXYZ = aY/SqrtXYZ; 
       
   131          User::LeaveIfError(Math::ASin(result, yDividedBySqrtXYZ)); //was ACos
       
   132          
       
   133          if (result > 0)
       
   134             iDopplerData.iElevation = (TInt32) (result * 1000 + 0.5);
       
   135           else
       
   136             iDopplerData.iElevation = (TInt32) (result * 1000 - 0.5);
       
   137           
       
   138        }
       
   139 	   // else { we are exactly in origin and therefore elevation is undefined; let's use the previous elevation value instead } 
       
   140       
       
   141        iDopplerData.iRadius= (TInt32) (SqrtXYZ + 0.5);
       
   142 
       
   143  
       
   144       while(iDopplerData.iElevation > PI) 
       
   145        { 
       
   146 	   iDopplerData.iElevation = iDopplerData.iElevation - TWO_PI; 
       
   147        }
       
   148     
       
   149       if(iDopplerData.iElevation > QUARTER_PI) 
       
   150        {
       
   151 	   iDopplerData.iElevation = iDopplerData.iElevation - (iDopplerData.iElevation - QUARTER_PI) * 2;
       
   152 	   iDopplerData.iAzimuth = iDopplerData.iAzimuth + PI;
       
   153        }
       
   154 
       
   155       while(iDopplerData.iElevation < -PI) 
       
   156        {
       
   157 	   iDopplerData.iElevation = iDopplerData.iElevation + TWO_PI;
       
   158        }
       
   159       if(iDopplerData.iElevation < -QUARTER_PI) 
       
   160        {
       
   161 	   iDopplerData.iElevation = iDopplerData.iElevation + (QUARTER_PI - iDopplerData.iElevation) * 2;
       
   162 	   iDopplerData.iAzimuth = iDopplerData.iAzimuth + PI;
       
   163        }
       
   164     
       
   165       while (iDopplerData.iAzimuth < 0)
       
   166 	    iDopplerData.iAzimuth = iDopplerData.iAzimuth + TWO_PI;
       
   167       while (iDopplerData.iAzimuth > TWO_PI)
       
   168 	   iDopplerData.iAzimuth = iDopplerData.iAzimuth - TWO_PI;
       
   169 	   
       
   170 
       
   171 	}
       
   172 // -----------------------------------------------------------------------------
       
   173 // CDoppler::SetFactorL
       
   174 // -----------------------------------------------------------------------------
       
   175 //
       
   176 
       
   177 EXPORT_C void CDoppler::SetFactorL( TUint32 aFactor )
       
   178 	{
       
   179 	if ( (aFactor <= iDopplerData.iMaxFactor) )
       
   180 		{
       
   181 		iDopplerData.iFactor = aFactor;
       
   182 		}
       
   183 	else
       
   184 		{
       
   185 		User::Leave(KErrArgument);
       
   186 		}
       
   187 	}
       
   188 // -----------------------------------------------------------------------------
       
   189 // CDoppler::SetSphericalVelocityL
       
   190 // -----------------------------------------------------------------------------
       
   191 //
       
   192 
       
   193 EXPORT_C void CDoppler::SetSphericalVelocityL( TInt32 aAzimuth, TInt32 aElevation, TInt32 aRadius )
       
   194 	{
       
   195 	
       
   196       while(aElevation > PI) 
       
   197        { 
       
   198 	   aElevation = aElevation - TWO_PI; 
       
   199        }
       
   200     
       
   201       if(aElevation > QUARTER_PI) 
       
   202        {
       
   203 	   aElevation = aElevation - (aElevation - QUARTER_PI) * 2;
       
   204 	   aAzimuth = aAzimuth + PI;
       
   205        }
       
   206 
       
   207       while(aElevation < -PI) 
       
   208        {
       
   209 	   aElevation = aElevation + TWO_PI;
       
   210        }
       
   211       if(aElevation < -QUARTER_PI) 
       
   212        {
       
   213 	   aElevation = aElevation + (QUARTER_PI - aElevation) * 2;
       
   214 	   aAzimuth = aAzimuth + PI;
       
   215        }
       
   216     
       
   217       while (aAzimuth < 0)
       
   218 	    aAzimuth = aAzimuth + TWO_PI;
       
   219       while (aAzimuth > TWO_PI)
       
   220 	   aAzimuth = aAzimuth - TWO_PI;
       
   221 	   
       
   222 	
       
   223 	iDopplerData.iAzimuth = aAzimuth;
       
   224 	iDopplerData.iElevation = aElevation;
       
   225 	iDopplerData.iRadius = aRadius;
       
   226 
       
   227 
       
   228     TReal elevation = aElevation / 1000.0; // conversion from milliradians to radians because Sin and Cos functions eat radians
       
   229 
       
   230     TReal elevationSin;
       
   231     TReal elevationCos;
       
   232     User::LeaveIfError( Math::Sin( elevationSin, elevation ) );
       
   233     User::LeaveIfError( Math::Cos( elevationCos, elevation ) );
       
   234     
       
   235     TReal azimuthSin;
       
   236     TReal azimuthCos;
       
   237     User::LeaveIfError( Math::Sin( azimuthSin, aAzimuth / 1000.0) );
       
   238     User::LeaveIfError( Math::Cos(azimuthCos, aAzimuth / 1000.0) );
       
   239 
       
   240 
       
   241 	iDopplerData.iVelocityX = (TInt32)(0.5 + aRadius * elevationCos * azimuthSin);
       
   242     iDopplerData.iVelocityY = (TInt32)(0.5 + aRadius * elevationSin);
       
   243     iDopplerData.iVelocityZ = (TInt32)(0.5 - aRadius * elevationCos * azimuthCos);		
       
   244    }
       
   245 
       
   246 
       
   247 // -----------------------------------------------------------------------------
       
   248 // CDoppler::SphericalVelocity
       
   249 // -----------------------------------------------------------------------------
       
   250 //
       
   251 
       
   252 EXPORT_C void CDoppler::SphericalVelocity( TInt32& aAzimuth, TInt32& aElevation, TInt32& aRadius )
       
   253 	{
       
   254 		aAzimuth   = iDopplerData.iAzimuth;
       
   255 		aElevation = iDopplerData.iElevation;
       
   256 		aRadius    = iDopplerData.iRadius ;
       
   257 
       
   258 	}
       
   259 
       
   260 
       
   261 // -----------------------------------------------------------------------------
       
   262 // CDoppler::DoEffectData
       
   263 // -----------------------------------------------------------------------------
       
   264 //
       
   265 EXPORT_C const TDesC8& CDoppler::DoEffectData()
       
   266 	{
       
   267 #ifdef _DEBUG
       
   268     RDebug::Print(_L("CDoppler::DoEffectData"));
       
   269 #endif
       
   270 	iDataPckgTo = iDopplerData;
       
   271 	return iDataPckgTo;
       
   272 	}
       
   273 
       
   274 // -----------------------------------------------------------------------------
       
   275 // CDoppler::SetEffectData
       
   276 // -----------------------------------------------------------------------------
       
   277 //
       
   278 EXPORT_C void CDoppler::SetEffectData(
       
   279 	const TDesC8& aEffectDataBuffer )
       
   280 	{
       
   281 #ifdef _DEBUG
       
   282     RDebug::Print(_L("CDoppler::SetEffectData"));
       
   283 #endif
       
   284 	TEfDopplerDataPckg dataPckg;
       
   285 	dataPckg.Copy(aEffectDataBuffer);
       
   286 	iDopplerData = dataPckg();
       
   287 	iEnabled = iDopplerData.iEnabled;
       
   288 	iEnforced = iDopplerData.iEnforced;
       
   289 	iHaveUpdateRights = iDopplerData.iHaveUpdateRights;
       
   290 
       
   291 	}
       
   292 
       
   293 
       
   294 // ========================== OTHER EXPORTED FUNCTIONS =========================
       
   295 
       
   296 // End of File
       
   297