1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/mm/devsoundextensions/effects/DopplerBase/Src/DopplerBase.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,297 @@
1.4 +/*
1.5 +* Copyright (c) 2004 Nokia Corporation and/or its subsidiary(-ies).
1.6 +* All rights reserved.
1.7 +* This component and the accompanying materials are made available
1.8 +* under the terms of "Eclipse Public License v1.0"
1.9 +* which accompanies this distribution, and is available
1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.11 +*
1.12 +* Initial Contributors:
1.13 +* Nokia Corporation - initial contribution.
1.14 +*
1.15 +* Contributors:
1.16 +*
1.17 +* Description: Implementation of the doppler effect class
1.18 +*
1.19 +*/
1.20 +
1.21 +
1.22 +
1.23 +
1.24 +// INCLUDE FILES
1.25 +
1.26 +#ifdef _DEBUG
1.27 +#include <e32svr.h>
1.28 +#endif
1.29 +
1.30 +#include <DopplerBase.h>
1.31 +#include <e32math.h>
1.32 +#include <math.h>
1.33 +
1.34 +
1.35 +//360 degrees:
1.36 +#define TWO_PI 6283
1.37 +//180 degrees:
1.38 +#define PI 3142
1.39 +//90 degrees:
1.40 +#define QUARTER_PI 1570
1.41 +
1.42 +// ============================ MEMBER FUNCTIONS ===============================
1.43 +
1.44 +// -----------------------------------------------------------------------------
1.45 +// CDoppler::CDoppler
1.46 +// C++ default constructor can NOT contain any code, that
1.47 +// might leave.
1.48 +// -----------------------------------------------------------------------------
1.49 +//
1.50 +EXPORT_C CDoppler::CDoppler()
1.51 + : iDopplerData(),
1.52 + iDataPckgTo(iDopplerData),
1.53 + iDataPckgFrom(iDopplerData)
1.54 + {
1.55 + }
1.56 +
1.57 +// Destructor
1.58 +EXPORT_C CDoppler::~CDoppler()
1.59 + {
1.60 + }
1.61 +
1.62 +
1.63 +// -----------------------------------------------------------------------------
1.64 +// CDoppler::CartesianVelocity
1.65 +// -----------------------------------------------------------------------------
1.66 +//
1.67 +
1.68 +EXPORT_C void CDoppler::CartesianVelocity( TInt32& aX, TInt32& aY, TInt32& aZ )
1.69 + {
1.70 + aX = iDopplerData.iVelocityX;
1.71 + aY = iDopplerData.iVelocityY;
1.72 + aZ = iDopplerData.iVelocityZ;
1.73 + }
1.74 +
1.75 +// -----------------------------------------------------------------------------
1.76 +// CDoppler::Factor
1.77 +// -----------------------------------------------------------------------------
1.78 +//
1.79 +
1.80 +EXPORT_C TUint32 CDoppler::Factor() const
1.81 + {
1.82 + return iDopplerData.iFactor;
1.83 + }
1.84 +
1.85 +// -----------------------------------------------------------------------------
1.86 +// CDoppler::FactorMax
1.87 +// -----------------------------------------------------------------------------
1.88 +//
1.89 +
1.90 +EXPORT_C TUint32 CDoppler::FactorMax() const
1.91 + {
1.92 + return iDopplerData.iMaxFactor;
1.93 + }
1.94 +// -----------------------------------------------------------------------------
1.95 +// CDoppler::SetCartesianVelocityL
1.96 +// -----------------------------------------------------------------------------
1.97 +//
1.98 +
1.99 +EXPORT_C void CDoppler::SetCartesianVelocityL( TInt32 aX, TInt32 aY, TInt32 aZ )
1.100 + {
1.101 + iDopplerData.iVelocityX = aX;
1.102 + iDopplerData.iVelocityY = aY;
1.103 + iDopplerData.iVelocityZ = aZ;
1.104 +
1.105 +
1.106 + TReal SqrtXYZ = 0, squareX = 0, squareY = 0, squareZ = 0;
1.107 + Math::Pow(squareX, aX, 2);
1.108 + Math::Pow(squareY, aY, 2);
1.109 + Math::Pow(squareZ, aZ, 2);
1.110 +
1.111 + TReal sum = squareX + squareY + squareZ;
1.112 +
1.113 + Math::Sqrt(SqrtXYZ, sum);
1.114 +
1.115 + //Singularity region
1.116 + if(!((aX==0) && (aZ==0)))
1.117 + {
1.118 +
1.119 + TReal zDividedByXAtan = atan2 (-aX, -aZ);
1.120 +
1.121 + if (zDividedByXAtan > 0)
1.122 + iDopplerData.iAzimuth = -(TInt32) (zDividedByXAtan * 1000 + 0.5);
1.123 + else
1.124 + iDopplerData.iAzimuth = -(TInt32) (zDividedByXAtan * 1000 - 0.5);
1.125 + }
1.126 + // else { we are exactly on Y-axis and therefore azimuth is undefined; let's use the previous azimuth value instead }
1.127 +
1.128 +
1.129 + if (!((aX ==0) && (aY == 0) && (aZ == 0)))
1.130 + {
1.131 +
1.132 + TReal result;
1.133 + TReal yDividedBySqrtXYZ = aY/SqrtXYZ;
1.134 + User::LeaveIfError(Math::ASin(result, yDividedBySqrtXYZ)); //was ACos
1.135 +
1.136 + if (result > 0)
1.137 + iDopplerData.iElevation = (TInt32) (result * 1000 + 0.5);
1.138 + else
1.139 + iDopplerData.iElevation = (TInt32) (result * 1000 - 0.5);
1.140 +
1.141 + }
1.142 + // else { we are exactly in origin and therefore elevation is undefined; let's use the previous elevation value instead }
1.143 +
1.144 + iDopplerData.iRadius= (TInt32) (SqrtXYZ + 0.5);
1.145 +
1.146 +
1.147 + while(iDopplerData.iElevation > PI)
1.148 + {
1.149 + iDopplerData.iElevation = iDopplerData.iElevation - TWO_PI;
1.150 + }
1.151 +
1.152 + if(iDopplerData.iElevation > QUARTER_PI)
1.153 + {
1.154 + iDopplerData.iElevation = iDopplerData.iElevation - (iDopplerData.iElevation - QUARTER_PI) * 2;
1.155 + iDopplerData.iAzimuth = iDopplerData.iAzimuth + PI;
1.156 + }
1.157 +
1.158 + while(iDopplerData.iElevation < -PI)
1.159 + {
1.160 + iDopplerData.iElevation = iDopplerData.iElevation + TWO_PI;
1.161 + }
1.162 + if(iDopplerData.iElevation < -QUARTER_PI)
1.163 + {
1.164 + iDopplerData.iElevation = iDopplerData.iElevation + (QUARTER_PI - iDopplerData.iElevation) * 2;
1.165 + iDopplerData.iAzimuth = iDopplerData.iAzimuth + PI;
1.166 + }
1.167 +
1.168 + while (iDopplerData.iAzimuth < 0)
1.169 + iDopplerData.iAzimuth = iDopplerData.iAzimuth + TWO_PI;
1.170 + while (iDopplerData.iAzimuth > TWO_PI)
1.171 + iDopplerData.iAzimuth = iDopplerData.iAzimuth - TWO_PI;
1.172 +
1.173 +
1.174 + }
1.175 +// -----------------------------------------------------------------------------
1.176 +// CDoppler::SetFactorL
1.177 +// -----------------------------------------------------------------------------
1.178 +//
1.179 +
1.180 +EXPORT_C void CDoppler::SetFactorL( TUint32 aFactor )
1.181 + {
1.182 + if ( (aFactor <= iDopplerData.iMaxFactor) )
1.183 + {
1.184 + iDopplerData.iFactor = aFactor;
1.185 + }
1.186 + else
1.187 + {
1.188 + User::Leave(KErrArgument);
1.189 + }
1.190 + }
1.191 +// -----------------------------------------------------------------------------
1.192 +// CDoppler::SetSphericalVelocityL
1.193 +// -----------------------------------------------------------------------------
1.194 +//
1.195 +
1.196 +EXPORT_C void CDoppler::SetSphericalVelocityL( TInt32 aAzimuth, TInt32 aElevation, TInt32 aRadius )
1.197 + {
1.198 +
1.199 + while(aElevation > PI)
1.200 + {
1.201 + aElevation = aElevation - TWO_PI;
1.202 + }
1.203 +
1.204 + if(aElevation > QUARTER_PI)
1.205 + {
1.206 + aElevation = aElevation - (aElevation - QUARTER_PI) * 2;
1.207 + aAzimuth = aAzimuth + PI;
1.208 + }
1.209 +
1.210 + while(aElevation < -PI)
1.211 + {
1.212 + aElevation = aElevation + TWO_PI;
1.213 + }
1.214 + if(aElevation < -QUARTER_PI)
1.215 + {
1.216 + aElevation = aElevation + (QUARTER_PI - aElevation) * 2;
1.217 + aAzimuth = aAzimuth + PI;
1.218 + }
1.219 +
1.220 + while (aAzimuth < 0)
1.221 + aAzimuth = aAzimuth + TWO_PI;
1.222 + while (aAzimuth > TWO_PI)
1.223 + aAzimuth = aAzimuth - TWO_PI;
1.224 +
1.225 +
1.226 + iDopplerData.iAzimuth = aAzimuth;
1.227 + iDopplerData.iElevation = aElevation;
1.228 + iDopplerData.iRadius = aRadius;
1.229 +
1.230 +
1.231 + TReal elevation = aElevation / 1000.0; // conversion from milliradians to radians because Sin and Cos functions eat radians
1.232 +
1.233 + TReal elevationSin;
1.234 + TReal elevationCos;
1.235 + User::LeaveIfError( Math::Sin( elevationSin, elevation ) );
1.236 + User::LeaveIfError( Math::Cos( elevationCos, elevation ) );
1.237 +
1.238 + TReal azimuthSin;
1.239 + TReal azimuthCos;
1.240 + User::LeaveIfError( Math::Sin( azimuthSin, aAzimuth / 1000.0) );
1.241 + User::LeaveIfError( Math::Cos(azimuthCos, aAzimuth / 1000.0) );
1.242 +
1.243 +
1.244 + iDopplerData.iVelocityX = (TInt32)(0.5 + aRadius * elevationCos * azimuthSin);
1.245 + iDopplerData.iVelocityY = (TInt32)(0.5 + aRadius * elevationSin);
1.246 + iDopplerData.iVelocityZ = (TInt32)(0.5 - aRadius * elevationCos * azimuthCos);
1.247 + }
1.248 +
1.249 +
1.250 +// -----------------------------------------------------------------------------
1.251 +// CDoppler::SphericalVelocity
1.252 +// -----------------------------------------------------------------------------
1.253 +//
1.254 +
1.255 +EXPORT_C void CDoppler::SphericalVelocity( TInt32& aAzimuth, TInt32& aElevation, TInt32& aRadius )
1.256 + {
1.257 + aAzimuth = iDopplerData.iAzimuth;
1.258 + aElevation = iDopplerData.iElevation;
1.259 + aRadius = iDopplerData.iRadius ;
1.260 +
1.261 + }
1.262 +
1.263 +
1.264 +// -----------------------------------------------------------------------------
1.265 +// CDoppler::DoEffectData
1.266 +// -----------------------------------------------------------------------------
1.267 +//
1.268 +EXPORT_C const TDesC8& CDoppler::DoEffectData()
1.269 + {
1.270 +#ifdef _DEBUG
1.271 + RDebug::Print(_L("CDoppler::DoEffectData"));
1.272 +#endif
1.273 + iDataPckgTo = iDopplerData;
1.274 + return iDataPckgTo;
1.275 + }
1.276 +
1.277 +// -----------------------------------------------------------------------------
1.278 +// CDoppler::SetEffectData
1.279 +// -----------------------------------------------------------------------------
1.280 +//
1.281 +EXPORT_C void CDoppler::SetEffectData(
1.282 + const TDesC8& aEffectDataBuffer )
1.283 + {
1.284 +#ifdef _DEBUG
1.285 + RDebug::Print(_L("CDoppler::SetEffectData"));
1.286 +#endif
1.287 + TEfDopplerDataPckg dataPckg;
1.288 + dataPckg.Copy(aEffectDataBuffer);
1.289 + iDopplerData = dataPckg();
1.290 + iEnabled = iDopplerData.iEnabled;
1.291 + iEnforced = iDopplerData.iEnforced;
1.292 + iHaveUpdateRights = iDopplerData.iHaveUpdateRights;
1.293 +
1.294 + }
1.295 +
1.296 +
1.297 +// ========================== OTHER EXPORTED FUNCTIONS =========================
1.298 +
1.299 +// End of File
1.300 +