os/mm/mmhais/a3facl/src/audiogaincontrol/logicalaudiogaincontrol.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     1 // Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
     2 // All rights reserved.
     3 // This component and the accompanying materials are made available
     4 // under the terms of "Eclipse Public License v1.0"
     5 // which accompanies this distribution, and is available
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
     7 //
     8 // Initial Contributors:
     9 // Nokia Corporation - initial contribution.
    10 //
    11 // Contributors:
    12 //
    13 // Description:
    14 // Name        : audiogaincontrol.cpp
    15 // Part of     : ACL Logical layer
    16 //
    17 
    18 
    19 
    20 #include "logicalaudiogaincontrol.h"
    21 #include <a3f/audioprocessingunittypeuids.h>
    22 #include <ecom/implementationproxy.h> // For making it ECom plugin
    23 
    24 // ======== LOCAL FUNCTIONS ========
    25 // __________________________________________________________________________
    26 // Exported proxy for instantiation method resolution
    27 // Define the interface UIDs
    28 
    29 // Moved from header 
    30 const TInt KDefaultMaxRampTime = 10;
    31 
    32 const TImplementationProxy ImplementationTable[] = 
    33 	{
    34 	IMPLEMENTATION_PROXY_ENTRY(KAudioGainControlUid, CLogicalAudioGainControl::NewL)
    35 	};
    36 
    37 EXPORT_C const TImplementationProxy* ImplementationGroupProxy(TInt& aTableCount)
    38 	{
    39 	aTableCount = sizeof(ImplementationTable) / sizeof(TImplementationProxy);
    40 	return ImplementationTable;
    41 	}
    42 
    43 // ---------------------------------------------------------------------------
    44 // Constructor
    45 // ---------------------------------------------------------------------------
    46 //
    47 CLogicalAudioGainControl::CLogicalAudioGainControl(TAny* aParam) : CAudioProcessingUnit(aParam)
    48 	{
    49 	TRACE_CREATE();
    50 	DP_CONTEXT(CLogicalAudioGainControl::CLogicalAudioGainControl *CD1*, CtxDevSound, DPLOCAL);
    51 	DP_IN();
    52 	
    53 	iMaxRampTime = KDefaultMaxRampTime;
    54 	
    55 	DP_OUT();
    56 	}
    57 
    58 // ---------------------------------------------------------------------------
    59 // Factory method
    60 // ---------------------------------------------------------------------------
    61 //
    62 CLogicalAudioGainControl* CLogicalAudioGainControl::NewL(TAny* aParam)
    63 	{
    64 	DP_STATIC_CONTEXT(CLogicalAudioGainControl::NewL *CD0*, CtxDevSound, DPLOCAL);
    65 	DP_IN();
    66 	CLogicalAudioGainControl* self = new(ELeave)CLogicalAudioGainControl(aParam);
    67 	CleanupStack::PushL(self);
    68 	self->ConstructL();
    69 	CleanupStack::Pop(self);
    70 	DP0_RET(self, "0x%x");
    71 	}
    72 
    73 // ---------------------------------------------------------------------------
    74 // Second phase constructor
    75 // ---------------------------------------------------------------------------
    76 //
    77 void CLogicalAudioGainControl::ConstructL()
    78 	{
    79 	DP_CONTEXT(CLogicalAudioGainControl::ConstructL *CD1*, CtxDevSound, DPLOCAL);
    80 	DP_IN();
    81 	TAudioChannelGain left;
    82 	TAudioChannelGain right;
    83 	left.iLocation = TAudioChannelGain::ELeft;
    84 	right.iLocation = TAudioChannelGain::ERight;
    85 	iDesiredChannels.AppendL(left);
    86 	iDesiredChannels.AppendL(right);
    87 	DP_OUT();
    88 	}
    89 
    90 // ---------------------------------------------------------------------------
    91 // Destructor
    92 // ---------------------------------------------------------------------------
    93 //
    94 CLogicalAudioGainControl::~CLogicalAudioGainControl()
    95 	{
    96 	DP_CONTEXT(CLogicalAudioGainControl::~CLogicalAudioGainControl *CD1*, CtxDevSound, DPLOCAL);
    97 	DP_IN();
    98 	iActiveChannels.Close();
    99 	iDesiredChannels.Close();
   100 	iGainObservers.Close();
   101 	DP_OUT();
   102 	}
   103 
   104 // ---------------------------------------------------------------------------
   105 // from class MAudioGainControl
   106 // CLogicalAudioGainControl::GetMaxGain
   107 // ---------------------------------------------------------------------------
   108 //
   109 TInt CLogicalAudioGainControl::GetMaxGain(TInt& aMaxGain) const
   110 	{
   111 	DP_CONTEXT(CLogicalAudioGainControl::GetMaxGain *CD1*, CtxDevSound, DPLOCAL);
   112 	DP_IN();
   113 	// TODO: Fix this when the MMRC server is available
   114 	// This adaptationGain pointer will be replaced by the a pointer gotten from 
   115 	// the "control mechanism" provided by the real MMRC server
   116 	TInt err = KErrNotReady;
   117 	if(iAdaptationGain)
   118 		{
   119 		err = iAdaptationGain->GetMaxGain(aMaxGain);
   120 		}
   121 	DP0_RET(err, "%d");
   122 	}
   123 
   124 // ---------------------------------------------------------------------------
   125 // from class MAudioGainControl
   126 // CLogicalAudioGainControl::GetMaxRampTime
   127 // ---------------------------------------------------------------------------
   128 //
   129 TInt CLogicalAudioGainControl::GetMaxRampTime(TTimeIntervalMicroSeconds& aMaxRampTime) const
   130 	{
   131 	DP_CONTEXT(CLogicalAudioGainControl::GetMaxRampTime *CD1*, CtxDevSound, DPLOCAL);
   132 	DP_IN();
   133 	TInt err = KErrNone;
   134 	aMaxRampTime = KDefaultMaxRampTime;
   135 	if(iAdaptationGain)
   136 		{
   137 		err = iAdaptationGain->GetMaxRampTime(aMaxRampTime);
   138 		}
   139 	DP0_RET(err, "%d");
   140 	} 
   141 
   142 // ---------------------------------------------------------------------------
   143 // from class MAudioGainControl
   144 // CLogicalAudioGainControl::GetGain
   145 // ---------------------------------------------------------------------------
   146 //
   147 TInt CLogicalAudioGainControl::GetGain(RArray<TAudioChannelGain>& aChannels) const
   148 	{
   149 	DP_CONTEXT(CLogicalAudioGainControl::GetGain *CD1*, CtxDevSound, DPLOCAL);
   150 	DP_IN();
   151 	TInt err = KErrNone;
   152 	TInt count = iDesiredChannels.Count();
   153 	aChannels.Reset();
   154 	for (TUint i(0); i < count; i++)
   155 		{
   156 		TInt err = aChannels.Append(iDesiredChannels[i]);
   157 		}
   158 	DP0_RET(err, "%d");
   159 	}
   160 
   161 // ---------------------------------------------------------------------------
   162 // from class MAudioGainControl
   163 // CLogicalAudioGainControl::SetGain
   164 // ---------------------------------------------------------------------------
   165 //
   166 TInt CLogicalAudioGainControl::SetGain(RArray<TAudioChannelGain>& aChannels, TUid aRampOperation, const TTimeIntervalMicroSeconds& aRampDuration)
   167 	{
   168 	DP_CONTEXT(CLogicalAudioGainControl::SetGain *CD1*, CtxDevSound, DPLOCAL);
   169 	DP_IN();
   170 	TInt err = KErrNone;
   171 
   172 	iDesiredRampTime = static_cast<TInt>(aRampDuration.Int64());
   173 	iDesiredRampOperation = aRampOperation;
   174 
   175 	// The gain values are clippped to MaxGain. 
   176 	TInt a3fMaxGain = 0;
   177 	if(iAdaptationGain)
   178 		{
   179 		err = iAdaptationGain->GetMaxGain(a3fMaxGain);
   180 		}
   181 
   182 	if(err == KErrNone)
   183 		{
   184 		iDesiredChannels.Reset();
   185 		TUint count = aChannels.Count();
   186 		TAudioChannelGain gainChannel;
   187 		for (TUint i(0); i < count; i++)
   188 			{
   189 			gainChannel = aChannels[i];
   190 			// 
   191 			if(gainChannel.iGain > a3fMaxGain)
   192 				{
   193 				gainChannel.iGain = a3fMaxGain;
   194 				}
   195 			else if(gainChannel.iGain < 0)
   196 				{
   197 				err = KErrArgument;
   198 				break;
   199 				}
   200 			
   201 			err = iDesiredChannels.Append( gainChannel );
   202 			if(err != KErrNone)
   203 				{
   204 				break;
   205 				}
   206 			}
   207 		}
   208 
   209 	if(err == KErrNone)
   210 		{
   211 		if(iSettingsObserver)
   212 			{
   213 			iSettingsObserver->ReceiveComponentSettingsChange(iType, EComponentAlterationGain);
   214 			}
   215 		}
   216 
   217 	DP0_RET(err, "%d");
   218 	}
   219 
   220 // ---------------------------------------------------------------------------
   221 // from class MAudioGainControl
   222 // CLogicalAudioGainControl::SetGain
   223 // ---------------------------------------------------------------------------
   224 //
   225 TInt CLogicalAudioGainControl::SetGain(RArray<TAudioChannelGain>& aChannels)
   226 	{
   227 	DP_CONTEXT(CLogicalAudioGainControl::SetGain *CD1*, CtxDevSound, DPLOCAL);
   228 	DP_IN();
   229 	TInt err = KErrNone;
   230 	err = SetGain(aChannels, KNullUid, 0);
   231 	DP0_RET(err, "%d");
   232 	}
   233 
   234 // ---------------------------------------------------------------------------
   235 // from class MAudioGainControl
   236 // CLogicalAudioGainControl::RegisterAudioGainControlObserver
   237 // ---------------------------------------------------------------------------
   238 //
   239 TInt CLogicalAudioGainControl::RegisterAudioGainControlObserver(MAudioGainControlObserver& aObserver)
   240 	{
   241 	DP_CONTEXT(CLogicalAudioGainControl::RegisterAudioGainControlObserver *CD1*, CtxDevSound, DPLOCAL);
   242 	DP_IN();
   243 	TInt err = iGainObservers.Find(&aObserver);
   244 	if( err != KErrNotFound )
   245 		{
   246 		err = KErrAlreadyExists;
   247 		}
   248 	else
   249 		{
   250 		err = iGainObservers.Append(&aObserver);
   251 		}
   252 	DP0_RET(err, "%d");
   253 	}
   254 
   255 // ---------------------------------------------------------------------------
   256 // from class MAudioGainControl
   257 // CLogicalAudioGainControl::UnregisterAudioGainControlObserver
   258 // ---------------------------------------------------------------------------
   259 //
   260 void CLogicalAudioGainControl::UnregisterAudioGainControlObserver(MAudioGainControlObserver& aObserver)
   261 	{
   262 	DP_CONTEXT(CLogicalAudioGainControl::UnregisterAudioGainControlObserver *CD1*, CtxDevSound, DPLOCAL);
   263 	DP_IN();
   264 	TInt idxOrErr = iGainObservers.Find(&aObserver);
   265 	if( idxOrErr != KErrNotFound )
   266 		{
   267 		iGainObservers.Remove(idxOrErr);
   268 		}
   269 	DP_OUT();
   270 	}
   271 
   272 // ---------------------------------------------------------------------------
   273 // from class MAudioProcessingUnit
   274 // CLogicalAudioGainControl::IsTypeOf
   275 // ---------------------------------------------------------------------------
   276 TBool CLogicalAudioGainControl::IsTypeOf(TUid aTypeId) const
   277 	{
   278 	DP_CONTEXT(CLogicalAudioGainControl::IsTypeOf *CD1*, CtxDevSound, DPLOCAL);
   279 	DP_IN();
   280 	TBool result = EFalse;
   281 	if (iType == aTypeId)
   282 		{
   283 		result = ETrue;
   284 		}
   285 	DP0_RET(result, "%d");
   286 	}
   287 
   288 // ---------------------------------------------------------------------------
   289 // from class MAudioProcessingUnit
   290 // CLogicalAudioGainControl::Interface
   291 // ---------------------------------------------------------------------------
   292 TAny* CLogicalAudioGainControl::Interface(TUid aType)
   293 	{
   294 	DP_CONTEXT(CLogicalAudioGainControl::GetInterface *CD1*, CtxDevSound, DPLOCAL);
   295 	DP_IN();
   296 	MAudioGainControl* ptr = NULL;
   297 	if(aType==KUidAudioGainControl)
   298 		{
   299 		ptr = this;
   300 		}
   301 	DP_OUT();
   302 	return ptr;
   303 	}
   304 
   305 // ---------------------------------------------------------------------------
   306 // from class MAudioGainControlObserver
   307 // CLogicalAudioGainControl::MaxRampTimeChanged
   308 // ---------------------------------------------------------------------------
   309 void CLogicalAudioGainControl::MaxRampTimeChanged(MAudioGainControl& /*aGain*/)
   310 	{
   311 	DP_CONTEXT(CLogicalAudioGainControl::MaxRampTimeChanged *CD1*, CtxDevSound, DPLOCAL);
   312 	DP_IN();
   313 	TUint count = iGainObservers.Count();
   314 	for ( TUint idx(0); idx < count; idx++ )
   315 		{
   316 		iGainObservers[idx]->MaxRampTimeChanged(*this);
   317 		}
   318 	DP_OUT();
   319 	}
   320 
   321 // ---------------------------------------------------------------------------
   322 // from class MAudioGainControlObserver
   323 // CLogicalAudioGainControl::MaxGainChanged
   324 // ---------------------------------------------------------------------------
   325 void CLogicalAudioGainControl::MaxGainChanged(MAudioGainControl& /*aGain*/)
   326 	{
   327 	DP_CONTEXT(CLogicalAudioGainControl::MaxGainChanged *CD1*, CtxDevSound, DPLOCAL);
   328 	DP_IN();
   329 	TUint count = iGainObservers.Count();
   330 	for ( TUint idx(0); idx < count; idx++ )
   331 		{
   332 		iGainObservers[idx]->MaxGainChanged(*this);
   333 		}
   334 	DP_OUT();
   335 	}
   336 
   337 // ---------------------------------------------------------------------------
   338 // from class MAudioGainControlObserver
   339 // CLogicalAudioGainControl::GainChanged
   340 // ---------------------------------------------------------------------------
   341 void CLogicalAudioGainControl::GainChanged(MAudioGainControl& /*aGain*/, TInt aError)
   342 	{
   343 	DP_CONTEXT(CLogicalAudioGainControl::GainChanged *CD1*, CtxDevSound, DPLOCAL);
   344 	DP_IN();
   345 	TUint count = iDesiredChannels.Count();
   346 	iActiveChannels.Reset();
   347 	for(TUint i=0; i<count; i++)
   348 		{
   349 		iActiveChannels.Append(iDesiredChannels[i]);
   350 		}
   351 	count = iGainObservers.Count();
   352 	for ( TUint idx(0); idx < count; idx++ )
   353 		{
   354 		iGainObservers[idx]->GainChanged(*this, aError);
   355 		}
   356 	DP_OUT();
   357 	}