devsound/a3fdevsound/src/devsoundadaptor/cdevaudiocontrol.cpp
changeset 0 b8ed18f6c07b
equal deleted inserted replaced
-1:000000000000 0:b8ed18f6c07b
       
     1 // Copyright (c) 2006-2010 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 //
       
    15 
       
    16 
       
    17 
       
    18 #include "cdevaudiocontrol.h"
       
    19 
       
    20 #include <a3f/audioprocessingunittypeuids.h>
       
    21 #include <a3f/maudiocontext.h>
       
    22 #include <a3f/maudiostream.h>
       
    23 #include <a3f/maudiocodec.h>
       
    24 #include <a3f/maudiogaincontrol.h>
       
    25 
       
    26 
       
    27 const TInt KMicroSecondsInSecond = 1000000;
       
    28 const TInt KDefaultBufferSize = 4096;
       
    29 
       
    30 const TUint KMaxCallbacksExpected = 2;
       
    31 const TUint KDefaultSampleRate = 8000;
       
    32 const TUid KDefaultMode = {KA3FModeMonoValue};
       
    33 
       
    34 class TSampleRateTableEntry
       
    35 	{
       
    36 public:
       
    37 	TInt		iSampleRateValue;
       
    38 	TMMFSampleRate iSampleRate;
       
    39 	};
       
    40 
       
    41 const TSampleRateTableEntry KRateTableLookup[] = { 
       
    42 							{ 8000, EMMFSampleRate8000Hz },
       
    43 							{ 11025, EMMFSampleRate11025Hz },
       
    44 							{ 12000, EMMFSampleRate12000Hz },
       
    45 							{ 16000, EMMFSampleRate16000Hz },
       
    46 							{ 22050, EMMFSampleRate22050Hz },
       
    47 							{ 24000, EMMFSampleRate24000Hz },
       
    48 							{ 32000, EMMFSampleRate32000Hz },
       
    49 							{ 44100, EMMFSampleRate44100Hz },
       
    50 							{ 48000, EMMFSampleRate48000Hz },
       
    51 							{ 64000, EMMFSampleRate64000Hz },
       
    52 							{ 88200, EMMFSampleRate88200Hz },
       
    53 							{ 96000, EMMFSampleRate96000Hz },
       
    54 						};
       
    55 const TInt KMaxSampleRateIndex = 11; // must agree with length of table
       
    56 
       
    57 class TAudioModeTableEntry
       
    58 	{
       
    59 public:
       
    60 	TMMFMonoStereo	iAudioModeValue;
       
    61 	TUid			iAudioMode;
       
    62 	};
       
    63 
       
    64 const TAudioModeTableEntry KModeTableLookup[] = {
       
    65 							{ EMMFMono, {KA3FModeMonoValue} },
       
    66 							{ EMMFStereo, {KA3FModeStereoNonInterleavedValue} },
       
    67 							};
       
    68 
       
    69 const TInt KMaxModeIndex = 1; // must agree with length of table
       
    70 
       
    71 // ---------------------------------------------------------------------------
       
    72 // Default constructor
       
    73 // ---------------------------------------------------------------------------
       
    74 //
       
    75 CDevAudioControl::CDevAudioControl()
       
    76 	{
       
    77 	TRACE_CREATE();
       
    78 	DP_CONTEXT(CDevAudioControl::CDevAudioControl *CD1*, CtxDevSound, DPLOCAL);
       
    79 	DP_IN();
       
    80 
       
    81 	// Local cache default values
       
    82 	iCurrentSampleRate = KDefaultSampleRate;
       
    83 	iCurrentMode = KDefaultMode; 
       
    84 	iOutstandingCallbacks = KMaxCallbacksExpected; //by default we expect 2 callbacks for capabilities.
       
    85 	iCallbackFromAdaptor = KCallbackNone;
       
    86 	
       
    87 	DP_OUT();
       
    88 	}
       
    89 
       
    90 
       
    91 void CDevAudioControl::ConstructL(CDevAudio* aDevAudio, MDevSoundAdaptationObserver& aDevSoundObserver)
       
    92 	{
       
    93 	TRACE_CREATE();
       
    94 	DP_CONTEXT(CDevAudioControl::ConstructL *CD1*, CtxDevSound, DPLOCAL);
       
    95 	DP_IN();
       
    96 	iDevAudio = aDevAudio; 
       
    97 	iAdaptationObserver = &aDevSoundObserver;
       
    98 	
       
    99 	iObserverRegistered = EFalse;
       
   100 
       
   101 	TAudioChannelGain left;
       
   102 	TAudioChannelGain right;
       
   103 	
       
   104 	left.iLocation = TAudioChannelGain::ELeft;
       
   105 	right.iLocation = TAudioChannelGain::ERight;
       
   106 		
       
   107 	User::LeaveIfError(iChannelGains.Append(left)); // assumed element 0 in rest of code
       
   108 	User::LeaveIfError(iChannelGains.Append(right)); // assumed element 1 in rest of code
       
   109 
       
   110 	// Note this could be delayed now (volume is internal to adaptor until later in cycle)
       
   111 	// Needed to allow CMMFDevSound::MaxVolume and similar calls before CMMFDevSound::InitializeL
       
   112 	TAny* interface(NULL);
       
   113 	interface = iDevAudio->iGainControl->Interface(KUidAudioGainControl);
       
   114 	iGainControl = static_cast<MAudioGainControl*>(interface);
       
   115 	DP_OUT();
       
   116 	}
       
   117 
       
   118 // ---------------------------------------------------------------------------
       
   119 // Destructor
       
   120 // ---------------------------------------------------------------------------
       
   121 //
       
   122 CDevAudioControl::~CDevAudioControl()
       
   123 	{
       
   124 	DP_CONTEXT(CDevAudioControl::~CDevAudioControl *CD1*, CtxDevSound, DPLOCAL);
       
   125 	DP_IN();
       
   126 	iChannelGains.Close();
       
   127 	iSupportedRates.Close();
       
   128 	iSupportedModes.Close();
       
   129 	DP_OUT();
       
   130 	}
       
   131 	
       
   132 // -----------------------------------------------------------------------------
       
   133 // CacheAudioCodecIf
       
   134 //
       
   135 TInt CDevAudioControl::CacheAudioCodecIf()
       
   136 	{
       
   137 	DP_CONTEXT(CDevAudioControl::CacheAudioCodecIf *CD1*, CtxDevSound, DPLOCAL);
       
   138 	DP_IN();
       
   139 	if (iDevAudio->iAudioCodec==NULL)
       
   140 		{
       
   141 		DP0_RET(KErrNotReady,"%d");
       
   142 		}
       
   143 	TInt err = KErrNone;
       
   144 	if (iAudioCodecIf==NULL)
       
   145 		{
       
   146 		MAudioCodec* codecInterface = static_cast<MAudioCodec*>(iDevAudio->iAudioCodec->Interface(KUidAudioCodec));
       
   147 		__ASSERT_ALWAYS (codecInterface, CDevAudioControl::Panic(EAudioCodecIsNull));
       
   148 		iAudioCodecIf = codecInterface; 
       
   149 		}
       
   150 	DP0_RET(err,"%d");
       
   151 	}
       
   152 
       
   153 
       
   154 // -----------------------------------------------------------------------------
       
   155 // CDevAudioControl::Initialize
       
   156 // -----------------------------------------------------------------------------
       
   157 //
       
   158 TInt CDevAudioControl::Initialize(TUid /*aFormat*/)
       
   159 	{
       
   160 	DP_CONTEXT(CDevAudioControl::Initialize *CD1*, CtxDevSound, DPLOCAL);
       
   161 	DP_IN();
       
   162 	ASSERT(EFalse);
       
   163 	iObserverRegistered = EFalse;
       
   164 	DP0_RET(KErrNone,"%d");
       
   165 	}
       
   166 
       
   167 // -----------------------------------------------------------------------------
       
   168 // CDevAudioControl::Uninitialize
       
   169 // -----------------------------------------------------------------------------
       
   170 //
       
   171 TInt CDevAudioControl::Uninitialize()
       
   172 	{
       
   173 	DP_CONTEXT(CDevAudioControl::Uninitialize *CD1*, CtxDevSound, DPLOCAL);
       
   174 	DP_IN();
       
   175 
       
   176 	// Remove pUnits only allowed when stream is uninitialized
       
   177 	TInt err = iDevAudio->iAudioStream->Uninitialize();
       
   178 
       
   179 	if (err == KErrNone)
       
   180 		{
       
   181 		err = iDevAudio->CommitAudioContext();
       
   182 		}
       
   183 	if (err == KErrNone)
       
   184 		{
       
   185 		iDevAudio->iActiveState = EDevSoundAdaptorUninitialising;
       
   186 		}
       
   187 
       
   188 	DP0_RET(err,"%d");
       
   189 	}
       
   190 
       
   191 // -----------------------------------------------------------------------------
       
   192 // CDevAudioControl::Unload
       
   193 // -----------------------------------------------------------------------------
       
   194 //
       
   195 TInt CDevAudioControl::Unload()
       
   196 	{
       
   197 	DP_CONTEXT(CDevAudioControl::Unload *CD1*, CtxDevSound, DPLOCAL);
       
   198 	DP_IN();
       
   199 
       
   200 	TInt err = iDevAudio->iAudioStream->Unload();
       
   201 	if (err == KErrNone)
       
   202 		{
       
   203 		err = iDevAudio->CommitAudioContext();
       
   204 		}
       
   205 	if (err == KErrNone)
       
   206 		{
       
   207 		iDevAudio->iActiveState = EDevSoundAdaptorUnloading;
       
   208 		}
       
   209 
       
   210 	DP0_RET(err,"%d");
       
   211 	}
       
   212 
       
   213 // -----------------------------------------------------------------------------
       
   214 // CDevAudioControl::GetCapabilities
       
   215 // -----------------------------------------------------------------------------
       
   216 //
       
   217 TInt CDevAudioControl::GetCapabilities(TMMFCapabilities& aCap)
       
   218 	{
       
   219 	DP_CONTEXT(CDevAudioControl::GetCapabilities *CD1*, CtxDevSound, DPLOCAL);
       
   220 	DP_IN();
       
   221 	// At this phase of CAP, we only care about current codec capabilities. 
       
   222 	// This will be supported as soon as the A3F API changes to support this are ready
       
   223 	TInt err = KErrNone;
       
   224 		
       
   225 	if (iDevAudio->iActiveState != EDevSoundAdaptorCreated_Uninitialised)
       
   226 		{
       
   227 		err = CacheAudioCodecIf();
       
   228 		if (err != KErrNone)
       
   229 			{
       
   230 			DP0_RET(err, "%d");			
       
   231 			}
       
   232 			
       
   233 		if (err == KErrNone)
       
   234 			{
       
   235 			err = iAudioCodecIf->GetSupportedModes(iSupportedModes);
       
   236 			if (err == KErrNone)
       
   237 				{
       
   238 				aCap.iChannels = GetModes(iSupportedModes);
       
   239 				err = iAudioCodecIf->GetSupportedSamplesRates(iSupportedRates);
       
   240 				
       
   241 				if (err == KErrNone)
       
   242 					{
       
   243 					aCap.iRate = GetSampleRates(iSupportedRates);
       
   244 					}
       
   245 				else
       
   246 					{
       
   247 					//If was a problem getting sampleRates we donīt expect callbacks and return
       
   248 					iOutstandingCallbacks = 0;
       
   249 					}
       
   250 					
       
   251 				}
       
   252 			else
       
   253 				{
       
   254 				//If was a problem getting modes we donīt expect callbacks and return
       
   255 				iOutstandingCallbacks = 0;
       
   256 				}
       
   257 			}
       
   258 		}
       
   259 	else
       
   260 		{
       
   261 		err = KErrNotReady;
       
   262 		}
       
   263 	DP0_RET(err, "%d");
       
   264 	}
       
   265 
       
   266 // -----------------------------------------------------------------------------
       
   267 // CDevAudioControl::GetConfig
       
   268 // -----------------------------------------------------------------------------
       
   269 //
       
   270 TInt CDevAudioControl::GetConfig(TMMFCapabilities& aConfig)
       
   271 	{
       
   272 	DP_CONTEXT(CDevAudioControl::GetConfig *CD1*, CtxDevSound, DPLOCAL);
       
   273 	DP_IN();
       
   274 	TInt err(KErrNone);
       
   275 
       
   276 	//TODO add a return error code if the sample rate or the channels are not found
       
   277 	//We need to transform the local values to a supported value for the client
       
   278 	aConfig.iRate = static_cast<TMMFSampleRate>(GetSampleRate(iCurrentSampleRate));
       
   279 	aConfig.iChannels = static_cast<TMMFMonoStereo>(GetMode(iCurrentMode));
       
   280 	aConfig.iBufferSize = KDefaultBufferSize;
       
   281 	
       
   282 	DP2(DLINFO, "rate 0x%x, channels 0x%x", aConfig.iRate, aConfig.iChannels);
       
   283 
       
   284 	DP0_RET(err, "%d");
       
   285 	}
       
   286 
       
   287 // -----------------------------------------------------------------------------
       
   288 // CDevAudioControl::SetConfig
       
   289 // -----------------------------------------------------------------------------
       
   290 //
       
   291 TInt CDevAudioControl::SetConfig(const TMMFCapabilities& aConfig)
       
   292 	{
       
   293 	DP_CONTEXT(CDevAudioControl::SetConfig *CD1*, CtxDevSound, DPLOCAL);
       
   294 	DP_IN();
       
   295 	
       
   296 	// TODO need to ensure if iChannels or iSampleRate is outside known values, then
       
   297 	// the code handles that gracefully and returns the appropriate error code
       
   298 	
       
   299 	TInt err(KErrNone);
       
   300 	TUid mode = KNullUid;
       
   301 	//Reset the desired values
       
   302 	iDesiredSampleRate = 0;
       
   303 	iDesiredMode = KNullUid;
       
   304 
       
   305 	err = ResolveMode(aConfig.iChannels, mode);
       
   306 	if (err != KErrNone)
       
   307 		{
       
   308 		DP0_RET(err, "%d");
       
   309 		}
       
   310 
       
   311 	err = ResolveSampleRate(aConfig.iRate, iDesiredSampleRate);
       
   312 	if (err != KErrNone)
       
   313 		{
       
   314 		DP0_RET(err, "%d");
       
   315 		}
       
   316 
       
   317 	// At this phase of CAP, we only care about codec, which checks config against
       
   318 	// its own capabilities. Verification against stream specific capabilities
       
   319 	// should be added later on.
       
   320 	
       
   321 	err = CacheAudioCodecIf();
       
   322 	if (err != KErrNone)
       
   323 		{
       
   324 		DP0_RET(err, "%d");
       
   325 		}
       
   326 	
       
   327 	err = iAudioCodecIf->SetSampleRate(iDesiredSampleRate);
       
   328 		
       
   329 	
       
   330 	if(err == KErrNone)
       
   331 		{
       
   332 		err = iAudioCodecIf->SetMode(mode);
       
   333 		}
       
   334 	
       
   335 	if(err == KErrNone)
       
   336 		{
       
   337 		err = iDevAudio->CommitAudioContext();
       
   338 		if (err == KErrNone)
       
   339 			{
       
   340 			iDesiredMode = mode;
       
   341 			}
       
   342 		}
       
   343 
       
   344 	DP0_RET(err, "%d");
       
   345 	}
       
   346 
       
   347 // -----------------------------------------------------------------------------
       
   348 // CDevAudioControl::ProcessInit
       
   349 // -----------------------------------------------------------------------------
       
   350 //
       
   351 TInt CDevAudioControl::ProcessInit()
       
   352 	{
       
   353 	DP_CONTEXT(CDevAudioControl::ProcessInit *CD1*, CtxDevSound, DPLOCAL);
       
   354 	DP_IN();
       
   355 	ASSERT(EFalse);
       
   356 	DP0_RET(KErrNone,"%d");
       
   357 	}
       
   358 
       
   359 // -----------------------------------------------------------------------------
       
   360 // CDevAudioControl::ProcessData
       
   361 // -----------------------------------------------------------------------------
       
   362 //
       
   363 void CDevAudioControl::ProcessData()
       
   364 	{
       
   365 	DP_CONTEXT(CDevAudioControl::ProcessData *CD1*, CtxDevSound, DPLOCAL);
       
   366 	DP_IN();
       
   367 	ASSERT(EFalse);
       
   368 	DP_OUT();
       
   369 	}
       
   370 
       
   371 // -----------------------------------------------------------------------------
       
   372 // CDevAudioControl::GetSamples
       
   373 // -----------------------------------------------------------------------------
       
   374 //
       
   375 TInt CDevAudioControl::GetSamples()
       
   376 	{
       
   377 	DP_CONTEXT(CDevAudioControl::GetSamples *CD1*, CtxDevSound, DPLOCAL);
       
   378 	DP_IN();
       
   379 	TInt err(KErrNone);
       
   380 	TInt samplesplayed(0);
       
   381 	
       
   382 	TTimeIntervalMicroSeconds timeProcessed(0);
       
   383 	err = iDevAudio->iAudioStream->GetStreamTime(timeProcessed);
       
   384 
       
   385 	DP1(DLINFO,"CDevAudioControl::GetSamples iCurrentSampleRate %d",iCurrentSampleRate);
       
   386 	
       
   387 	if(err == KErrNone)
       
   388 		{
       
   389 		samplesplayed = (timeProcessed.Int64() * iCurrentSampleRate + KMicroSecondsInSecond/2) / TInt64(KMicroSecondsInSecond);
       
   390 		}
       
   391 	//TODO manage the error
       
   392 
       
   393 	DP0_RET(samplesplayed, "%d");
       
   394 	}
       
   395 
       
   396 // -----------------------------------------------------------------------------
       
   397 // CDevAudioControl::Stop
       
   398 // -----------------------------------------------------------------------------
       
   399 //
       
   400 TInt CDevAudioControl::Stop()
       
   401 	{
       
   402 	DP_CONTEXT(CDevAudioControl::Stop *CD1*, CtxDevSound, DPLOCAL);
       
   403 	DP_IN();
       
   404 	ASSERT(EFalse);
       
   405 	DP0_RET(KErrNone, "%d");
       
   406 	}
       
   407 
       
   408 // -----------------------------------------------------------------------------
       
   409 // CDevAudioControl::Pause
       
   410 // -----------------------------------------------------------------------------
       
   411 //
       
   412 TInt CDevAudioControl::Pause()
       
   413 	{
       
   414 	DP_CONTEXT(CDevAudioControl::Pause *CD1*, CtxDevSound, DPLOCAL);
       
   415 	DP_IN();
       
   416 	ASSERT(EFalse);
       
   417 	DP0_RET(KErrNone, "%d");
       
   418 	}
       
   419 
       
   420 // -----------------------------------------------------------------------------
       
   421 // CDevAudioControl::CustomInterface
       
   422 // -----------------------------------------------------------------------------
       
   423 //
       
   424 TAny* CDevAudioControl::CustomInterface(TUid aInterfaceId)
       
   425 	{
       
   426 	DP_CONTEXT(CDevAudioControl::CustomInterface *CD1*, CtxDevSound, DPLOCAL);
       
   427 	DP_IN();
       
   428 	TAny* ciPtr(NULL);
       
   429 	TInt err = KErrNone;
       
   430 	if(iDevAudio->iAudioStream)
       
   431 		{
       
   432 		TAny* ptr = iDevAudio->iAudioStream->Interface(KUidExtensionInferface);
       
   433 		if(ptr)
       
   434 			{
       
   435 			MCustomInterfaceSupport* ciSupport  = static_cast<MCustomInterfaceSupport*>(ptr);
       
   436 			if(!iObserverRegistered)
       
   437 				{
       
   438 				err = ciSupport->RegisterObserver(*this);
       
   439 				if(err == KErrNone)
       
   440 					{
       
   441 					iObserverRegistered = ETrue;
       
   442 					}
       
   443 				}
       
   444 			err = ciSupport->RequestCustomInterface(aInterfaceId, ciPtr);
       
   445 			if( err != KErrNone)
       
   446 				{
       
   447 				ciPtr = NULL;
       
   448 				}
       
   449 			}
       
   450 		}
       
   451 	DP0_RET(ciPtr, "0x%x");
       
   452 	}
       
   453 
       
   454 // -----------------------------------------------------------------------------
       
   455 // CDevAudioControl::SetGain
       
   456 // -----------------------------------------------------------------------------
       
   457 TInt CDevAudioControl::SetGains(TInt aDevSoundGain, TInt aDevSoundMaxGain, TInt aBalance[2], const TTimeIntervalMicroSeconds& aRampDuration, TBool aBecomingActive)
       
   458 	{
       
   459 	DP_CONTEXT(CDevAudioControl::SetGains *CD1*, CtxDevSound, DPLOCAL);
       
   460 	DP_IN();
       
   461 	ASSERT(aDevSoundGain>=0 && aDevSoundGain<=aDevSoundMaxGain); // higher layer assumed to scale
       
   462 	ASSERT(aBalance[ELeftCh]>=0 && aBalance[ELeftCh]<=100);
       
   463 	ASSERT(aBalance[ERightCh]>=0 && aBalance[ERightCh]<=100);
       
   464 	ASSERT(aDevSoundMaxGain>0); // assumed max gain is positive
       
   465 	TInt a3fMaxGain;
       
   466 	TInt err = iGainControl->GetMaxGain(a3fMaxGain);
       
   467 	if (err==KErrNone)
       
   468 		{
       
   469 		iLegacyGain = TInt(TReal(aDevSoundGain)*TReal(a3fMaxGain)/TReal(aDevSoundMaxGain)+0.5);
       
   470 		ASSERT(iLegacyGain>=0 && iLegacyGain<=a3fMaxGain);
       
   471 		iLegacyLeft = aBalance[ELeftCh];
       
   472 		iLegacyRight = aBalance[ERightCh];
       
   473 
       
   474 		MapGains();
       
   475 
       
   476 		// VolumeRamp is only applied when DevSound is becoming active
       
   477 		if(err == KErrNone)
       
   478 			{
       
   479 			if (aRampDuration > 0 && aBecomingActive)
       
   480 				{
       
   481 				err = iGainControl->SetGain(iChannelGains, KUidGainSawTooth, aRampDuration);
       
   482 				}
       
   483 			else
       
   484 				{
       
   485 				err = iGainControl->SetGain(iChannelGains);
       
   486 				}
       
   487 			}
       
   488 
       
   489 		// This call will result on commit only when we are already active
       
   490 		// otherwise the changes will be commited by the DevAudioControl 
       
   491 		// It means we're here due to RequestGainAndBalance call
       
   492 		if(err == KErrNone && !aBecomingActive)
       
   493 			{
       
   494 			err = iDevAudio->CommitAudioContext();
       
   495 			}		
       
   496 		}
       
   497 	DP0_RET(err,"%d");
       
   498 	}
       
   499 
       
   500 
       
   501 // -----------------------------------------------------------------------------
       
   502 // CDevAudioControl::MapAndSetGains
       
   503 // -----------------------------------------------------------------------------
       
   504 //
       
   505 void CDevAudioControl::MapGains()
       
   506 	{
       
   507 	DP_CONTEXT(CDevAudioControl::MapGains *CD1*, CtxDevSound, DPLOCAL);
       
   508 	DP_IN();
       
   509 
       
   510 	// Map legacy values to CAP channel array.
       
   511 	if ( iLegacyLeft == iLegacyRight )
       
   512 		{
       
   513 		iChannelGains[ELeftCh].iGain = iLegacyGain;
       
   514 		iChannelGains[ERightCh].iGain = iLegacyGain;
       
   515 		}
       
   516 	else if ( iLegacyLeft > iLegacyRight )
       
   517 		{
       
   518 		iChannelGains[ELeftCh].iGain = iLegacyGain;
       
   519 		iChannelGains[ERightCh].iGain =
       
   520 						static_cast<TUint>((iLegacyGain*iLegacyRight)/iLegacyLeft);
       
   521 		}
       
   522 	else
       
   523 		{
       
   524 		iChannelGains[ERightCh].iGain = iLegacyGain;
       
   525 		iChannelGains[ELeftCh].iGain =
       
   526 						static_cast<TUint>((iLegacyGain*iLegacyLeft)/iLegacyRight);
       
   527 		}
       
   528 
       
   529 	DP_OUT();
       
   530 	}
       
   531 
       
   532 // -----------------------------------------------------------------------------
       
   533 // CDevAudioControl::DestroyChain
       
   534 // -----------------------------------------------------------------------------
       
   535 //
       
   536 TBool CDevAudioControl::DestroyChain()
       
   537 	{
       
   538 	DP_CONTEXT(CDevAudioControl::DestroyChain *CD1*, CtxDevSound, DPLOCAL);
       
   539 	DP_IN();
       
   540 
       
   541 	TInt err = KErrNone;
       
   542 	TBool readyToDestroy = EFalse;
       
   543 	switch(iDevAudio->iActiveState)
       
   544 		{
       
   545 		case EDevSoundAdaptorActive_Active:
       
   546 		case EDevSoundAdaptorPaused_Primed:
       
   547 			{
       
   548 			err = iDevAudio->iAudioStream->Stop();
       
   549 			if(err == KErrNone)
       
   550 				{
       
   551 				err = iDevAudio->CommitAudioContext();
       
   552 				}
       
   553 			if (err == KErrNone)
       
   554 				{
       
   555 				iDevAudio->iActiveState = EDevSoundAdaptorStopping;
       
   556 				}
       
   557 			}
       
   558 			break;
       
   559 		case EDevSoundAdaptorInitialised_Idle:
       
   560 			{
       
   561 			err = iDevAudio->iAudioStream->Unload();
       
   562 			if(err == KErrNone)
       
   563 				{
       
   564 				err = iDevAudio->CommitAudioContext();
       
   565 				}
       
   566 			if (err == KErrNone)
       
   567 				{
       
   568 				iDevAudio->iActiveState = EDevSoundAdaptorUnloading;
       
   569 				}
       
   570 			}
       
   571 			break;
       
   572 		case EDevSoundAdaptorInitialised_Initialised:
       
   573 			{
       
   574 			err = iDevAudio->iAudioStream->Uninitialize();
       
   575 			if(err == KErrNone)
       
   576 				{
       
   577 				err = iDevAudio->CommitAudioContext();
       
   578 				}
       
   579 			if (err == KErrNone)
       
   580 				{
       
   581 				iDevAudio->iActiveState = EDevSoundAdaptorUninitialising;
       
   582 				}
       
   583 			}
       
   584 			break;
       
   585 		case EDevSoundAdaptorCreated_Uninitialised:
       
   586 			readyToDestroy = ETrue;
       
   587 			break;
       
   588 		case EDevSoundAdaptorUnitialised_Uninitialised:
       
   589 			//If following condition is true, then we are here because of a
       
   590 			//pre-emption clash in last Commit cycle started from
       
   591 			//CDevCommonControl::ContextEventUpdateWithStateEventNoError.
       
   592 			if(iDevAudio->iPreviousState == EDevSoundAdaptorRemovingProcessingUnits)
       
   593 				{
       
   594 				err = RemoveProcessingUnits();
       
   595 				break;
       
   596 				}
       
   597 		default:
       
   598 			break;
       
   599 		}
       
   600 	
       
   601 	// Destroy sequence fail!
       
   602 	if(err != KErrNone)
       
   603 		{
       
   604 		DP0(DLINFO, "================ Destroy sequence fail! ================");
       
   605 		readyToDestroy = ETrue;
       
   606 		}
       
   607 	// Set the flag only when needed
       
   608 	iDevAudio->iClosing = !readyToDestroy;
       
   609 	DP0_RET(readyToDestroy,"%d");
       
   610 	}
       
   611 
       
   612 
       
   613 // -----------------------------------------------------------------------------
       
   614 // CDevAudioControl::RemoveProcessingUnits
       
   615 // -----------------------------------------------------------------------------
       
   616 //
       
   617 TInt CDevAudioControl::RemoveProcessingUnits()
       
   618 	{
       
   619 	DP_CONTEXT(CDevAudioControl::RemoveProcessingUnits *CD1*, CtxDevSound, DPLOCAL);
       
   620 	DP_IN();
       
   621 
       
   622 	// Remove pUnits only allowed when stream is uninitialized
       
   623 	TInt err = iDevAudio->iAudioStream->RemoveProcessingUnit(iDevAudio->iAudioSource);
       
   624 		
       
   625 	if(err == KErrNone)
       
   626 		{
       
   627 		err = iDevAudio->iAudioStream->RemoveProcessingUnit(iDevAudio->iAudioSink);	
       
   628 		}
       
   629 	if(err == KErrNone)
       
   630 		{
       
   631 		err = iDevAudio->iAudioStream->RemoveProcessingUnit(iDevAudio->iAudioCodec);
       
   632 		}
       
   633 
       
   634 	if (err == KErrNone)
       
   635 		{
       
   636 		err = iDevAudio->CommitAudioContext();
       
   637 		}
       
   638 		
       
   639 	if(err == KErrNone)
       
   640 		{
       
   641 		iDevAudio->iActiveState = EDevSoundAdaptorRemovingProcessingUnits;		
       
   642 		}
       
   643 	DP0_RET(err,"%d");
       
   644 	}
       
   645 
       
   646 // -----------------------------------------------------------------------------
       
   647 // From class MAudioStreamObserver
       
   648 // CDevAudioControl::StateEvent
       
   649 // -----------------------------------------------------------------------------
       
   650 //
       
   651 void CDevAudioControl::StateEvent(MAudioStream& /*aStream*/, 
       
   652 									TInt /*aReason*/, 
       
   653 									TAudioState /*aNewState*/)
       
   654 	{
       
   655 	DP_CONTEXT(CDevAudio::StateEvent *CD1*, CtxDevSound, DPLOCAL);
       
   656 	DP_IN();
       
   657 	ASSERT(EFalse);
       
   658 	DP_OUT();
       
   659 	}
       
   660 
       
   661 // -----------------------------------------------------------------------------
       
   662 // From class MAudioStreamObserver
       
   663 // CDevAudioControl::AddProcessingUnitComplete
       
   664 // -----------------------------------------------------------------------------
       
   665 //
       
   666 void CDevAudioControl::AddProcessingUnitComplete(MAudioStream& /*aStream*/, 
       
   667 												MAudioProcessingUnit* /*aInstance*/,
       
   668 												TInt /*aError*/)
       
   669 	{
       
   670 	}
       
   671 
       
   672 // -----------------------------------------------------------------------------
       
   673 // From class MAudioStreamObserver
       
   674 // CDevAudioControl::RemoveProcessingUnitComplete
       
   675 // -----------------------------------------------------------------------------
       
   676 //
       
   677 void CDevAudioControl::RemoveProcessingUnitComplete(MAudioStream& /*aStream*/,
       
   678 													MAudioProcessingUnit* /*aInstance*/,
       
   679 													TInt /*aError*/)
       
   680 	{
       
   681 	}
       
   682 
       
   683 // -----------------------------------------------------------------------------
       
   684 // From class MAudioStreamObserver
       
   685 // CDevAudioControl::ProcessingFinished
       
   686 // -----------------------------------------------------------------------------
       
   687 //
       
   688 void CDevAudioControl::ProcessingFinished(MAudioStream& /*aStream*/)
       
   689 	{
       
   690 	}
       
   691 
       
   692 // -----------------------------------------------------------------------------
       
   693 // From class MAudioStreamObserver
       
   694 // CDevAudioControl::FlushComplete
       
   695 // -----------------------------------------------------------------------------
       
   696 //
       
   697 void CDevAudioControl::FlushComplete (MAudioStream& /*aStream*/, TInt aError)
       
   698 	{
       
   699 	// no action needed - should complete as part of the ContextEvent.
       
   700 	// otherwise could callback.
       
   701 	TInt err = KErrNone;
       
   702 	
       
   703 	if(iPauseResumeSequenceDueToEmptyBuffers)
       
   704 		{
       
   705 		// Flush operation failed
       
   706 		if(aError != KErrNone)
       
   707 			{
       
   708 			iPauseResumeSequenceDueToEmptyBuffers = EFalse;
       
   709 			iAdaptationObserver->CallbackFromAdaptorReceived(KCallbackFlushComplete, aError);
       
   710 			}
       
   711 		// Handle throw-off scenarios, resume is not possible from here
       
   712 		// 1. ProcessingFinished has occurred
       
   713 		// 2. Preemption occurred 
       
   714 		else if(iCallbackFromAdaptor != KCallbackNone || 
       
   715 			iDevAudio->iActiveState != EDevSoundAdaptorPaused_Primed)
       
   716 			{
       
   717 			iPauseResumeSequenceDueToEmptyBuffers = EFalse;
       
   718 			iAdaptationObserver->CallbackFromAdaptorReceived(KCallbackFlushComplete, KErrNone);
       
   719 			
       
   720 			}
       
   721 		else
       
   722 			{
       
   723 			err = Resume();
       
   724 			if(err != KErrNone)
       
   725 				{
       
   726 				iPauseResumeSequenceDueToEmptyBuffers = EFalse;
       
   727 				iAdaptationObserver->CallbackFromAdaptorReceived(KCallbackFlushComplete, aError);
       
   728 				}
       
   729 			// Once ContextEvent be received 
       
   730 			// EmptyBuffers can be considered completed
       
   731 			}
       
   732 		}
       
   733 	// EmptyBuffers operation has concluded here
       
   734 	// we didn't go through pause - resume sequence
       
   735 	else
       
   736 		{
       
   737 		iAdaptationObserver->CallbackFromAdaptorReceived(KCallbackFlushComplete, aError);
       
   738 		}
       
   739 	}
       
   740 	
       
   741 // -----------------------------------------------------------------------------
       
   742 // From class MAudioGainControlObserver
       
   743 // CDevAudioControl::MaxRampTimeChanged
       
   744 // -----------------------------------------------------------------------------
       
   745 //
       
   746 void CDevAudioControl::MaxRampTimeChanged(MAudioGainControl& /*aGain*/)
       
   747 	{
       
   748 	DP_CONTEXT(CDevAudioControl::MaxRampTimeChanged *CD1*, CtxDevSound, DPLOCAL);
       
   749 	DP_IN();
       
   750 	// this is not cached, no actions needed
       
   751 	DP_OUT();
       
   752 	}
       
   753 
       
   754 // -----------------------------------------------------------------------------
       
   755 // From class MAudioGainControlObserver
       
   756 // CDevAudioControl::MaxGainChanged
       
   757 // -----------------------------------------------------------------------------
       
   758 //
       
   759 void CDevAudioControl::MaxGainChanged(MAudioGainControl& /*aGain*/)
       
   760 	{
       
   761 	DP_CONTEXT(CDevAudioControl::MaxGainChanged *CD1*, CtxDevSound, DPLOCAL);
       
   762 	DP_IN();
       
   763 	// this is not cached, no actions needed
       
   764 	DP_OUT();
       
   765 	}
       
   766 
       
   767 // -----------------------------------------------------------------------------
       
   768 // From class MAudioGainControlObserver
       
   769 // CDevAudioControl::GainChanged
       
   770 // -----------------------------------------------------------------------------
       
   771 //
       
   772 void CDevAudioControl::GainChanged(MAudioGainControl& aGain, TInt aError)
       
   773 	{
       
   774 	DP_CONTEXT(CDevAudioControl::GainChanged *CD1*, CtxDevSound, DPLOCAL);
       
   775 	DP1_IN("aError=%d", aError);
       
   776 
       
   777 	if(aError != KErrNone)
       
   778 		{
       
   779 		// Either our request failed, or MMRC has forced some values, and we
       
   780 		// have to update local values.
       
   781 		aGain.GetGain(iChannelGains);
       
   782 		ASSERT(iChannelGains.Count()==2);
       
   783 		// Map CAP channel array to legacy values. 
       
   784 		// assumption: left%+right%=100
       
   785 		if ( iChannelGains[ELeftCh].iGain == iChannelGains[ERightCh].iGain )
       
   786 			{
       
   787 			iLegacyGain = iChannelGains[ELeftCh].iGain;
       
   788 			iLegacyLeft = 50;
       
   789 			iLegacyRight = 50;
       
   790 			}
       
   791 		else if ( iChannelGains[ELeftCh].iGain > iChannelGains[ERightCh].iGain )
       
   792 			{
       
   793 			iLegacyGain = iChannelGains[ELeftCh].iGain;
       
   794 			iLegacyLeft = static_cast<TUint>
       
   795 				((100*iLegacyGain)/(iLegacyGain+iChannelGains[ERightCh].iGain));
       
   796 			iLegacyRight = 100 - iLegacyLeft;
       
   797 			//(not that accurate, but sufficient for now)
       
   798 			}
       
   799 		else
       
   800 			{
       
   801 			iLegacyGain = iChannelGains[ERightCh].iGain;
       
   802 			iLegacyRight = static_cast<TUint>
       
   803 				((100*iLegacyGain)/(iLegacyGain+iChannelGains[ELeftCh].iGain));
       
   804 			iLegacyLeft = 100 - iLegacyRight;
       
   805 			}
       
   806 
       
   807 		DP3(DLINFO,"New values :iLegacyGain %d, iLegacyLeft %d, iLegacyRight %d",
       
   808 				iLegacyGain,iLegacyLeft,iLegacyRight);
       
   809 		}
       
   810 	else
       
   811 		{
       
   812 		// our request completed succesfully, no need to update cached values
       
   813 		// just print values in debug version
       
   814 		#ifdef _DEBUG
       
   815 		RArray<TAudioChannelGain> gains;
       
   816 		TUint left;
       
   817 		TUint right;
       
   818 		TUint gain;
       
   819 		aGain.GetGain(gains);
       
   820 		ASSERT(gains.Count()==2);
       
   821 		if ( iChannelGains[ELeftCh].iGain == iChannelGains[ERightCh].iGain )
       
   822 			{
       
   823 			gain = iChannelGains[ELeftCh].iGain;
       
   824 			left = 50;
       
   825 			right = 50;
       
   826 			}
       
   827 		else if ( iChannelGains[ELeftCh].iGain > iChannelGains[ERightCh].iGain )
       
   828 			{
       
   829 			gain = iChannelGains[ELeftCh].iGain;
       
   830 			left = 
       
   831 				static_cast<TUint>((100*gain)/(gain+iChannelGains[ERightCh].iGain));
       
   832 			right = 100 - left;
       
   833 			}
       
   834 		else
       
   835 			{
       
   836 			gain = iChannelGains[ERightCh].iGain;
       
   837 			right = 
       
   838 				static_cast<TUint>((100*gain)/(gain+iChannelGains[ELeftCh].iGain));
       
   839 			left = 100 - right;
       
   840 			}
       
   841 		gains.Close();
       
   842 		DP3(DLINFO,"KErrNone (gain %d, left %d, right %d)", gain,left,right);
       
   843 		#endif
       
   844 		}
       
   845 
       
   846 	DP_OUT();
       
   847 	}
       
   848 
       
   849 // -----------------------------------------------------------------------------
       
   850 // From class MAudioCodecObserver
       
   851 // CDevAudioControl::SampleRateSet
       
   852 // -----------------------------------------------------------------------------
       
   853 void CDevAudioControl::SampleRateSet(TInt aError)
       
   854 	{
       
   855 	if(aError==KErrNone)
       
   856 		{
       
   857 		//Review if we call SetConfig or is only the first time that we load the codec
       
   858 		if (iDesiredSampleRate > 0)
       
   859 			{
       
   860 			iCurrentSampleRate = iDesiredSampleRate;
       
   861 			}
       
   862 		}
       
   863 	else
       
   864 	    {
       
   865         iAdaptationObserver->NotifyError(aError);
       
   866 	    }
       
   867     iDesiredSampleRate = 0;
       
   868 	}
       
   869 
       
   870 // -----------------------------------------------------------------------------
       
   871 // From class MAudioCodecObserver
       
   872 // CDevAudioControl::ModeSet
       
   873 // -----------------------------------------------------------------------------
       
   874 void CDevAudioControl::ModeSet(TInt aError)
       
   875 	{
       
   876 	if(aError==KErrNone)
       
   877 		{
       
   878 		//Review if we call SetConfig or is only the first time that we load the codec
       
   879 		if (iDesiredMode != KNullUid)
       
   880 			{
       
   881 			iCurrentMode = iDesiredMode;
       
   882 			}
       
   883 		}
       
   884     else
       
   885         {
       
   886         iAdaptationObserver->NotifyError(aError);
       
   887         }
       
   888     iDesiredMode = KNullUid;
       
   889 	}
       
   890 
       
   891 // -----------------------------------------------------------------------------
       
   892 // From class MAudioCodecObserver
       
   893 // CDevAudioControl::GetSupportedSampleRatesComplete
       
   894 // -----------------------------------------------------------------------------
       
   895 void CDevAudioControl::GetSupportedSampleRatesComplete(TInt aError)
       
   896 	{
       
   897 	iSupportedRates.Reset();
       
   898 	CompleteMessageCap(aError);
       
   899 	}
       
   900 
       
   901 // -----------------------------------------------------------------------------
       
   902 // From class MAudioCodecObserver
       
   903 // CDevAudioControl::GetSupportedModesComplete
       
   904 // -----------------------------------------------------------------------------
       
   905 void CDevAudioControl::GetSupportedModesComplete(TInt aError)
       
   906 	{
       
   907 	iSupportedModes.Reset();
       
   908 	CompleteMessageCap(aError);
       
   909 	}
       
   910 
       
   911 // -----------------------------------------------------------------------------
       
   912 // CDevAudioControl::CompleteMessageCap
       
   913 // -----------------------------------------------------------------------------
       
   914 void CDevAudioControl::CompleteMessageCap(TInt aError)
       
   915 	{
       
   916 	if (iOutstandingCallbacks > 1) //waiting until the 2 outstanding callbacks arrival.
       
   917 		{
       
   918 		iOutstandingCallbacks--;
       
   919 		iError = aError; //keeping the error.
       
   920 		}
       
   921 	else
       
   922 		{
       
   923 		if (iError == KErrNone)
       
   924 			{
       
   925 			iAdaptationObserver->AsynchronousOperationComplete(aError, ETrue);
       
   926 			}
       
   927 		else
       
   928 			{
       
   929 			iAdaptationObserver->AsynchronousOperationComplete(iError, ETrue);
       
   930 			}
       
   931 		iError = KErrNone;
       
   932 		iOutstandingCallbacks = KMaxCallbacksExpected;
       
   933 		}
       
   934 	}
       
   935 
       
   936 // -----------------------------------------------------------------------------
       
   937 // CDevAudioControl::SetToneData
       
   938 // -----------------------------------------------------------------------------
       
   939 //
       
   940 TInt CDevAudioControl::SetToneData(TToneData& /*aToneData*/)
       
   941 	{
       
   942 	DP_CONTEXT(CDevAudioControl::SetToneData *CD1*, CtxDevSound, DPLOCAL);
       
   943 	DP_IN();
       
   944 	DP0_RET(KErrNotSupported, "%d");
       
   945 	}
       
   946 
       
   947 
       
   948 // -----------------------------------------------------------------------------
       
   949 // From class MAudioContextObserver
       
   950 // CDevAudio::ContextEvent
       
   951 // -----------------------------------------------------------------------------
       
   952 //
       
   953 void CDevAudioControl::ContextEvent(TUid aEvent, TInt aError)
       
   954 	{
       
   955 	DP_CONTEXT(CDevAudioControl::ContextEvent *CD1*, CtxDevSound, DPLOCAL);
       
   956 	DP_IN();
       
   957 
       
   958     if(!(iAdaptationObserver->AdaptorControlsContext()))
       
   959         {
       
   960         iIgnoreAsyncOpComplete = ETrue;
       
   961         }
       
   962     
       
   963 	if (aEvent == KUidA3FContextUpdateComplete)
       
   964 		{
       
   965 	    if(iIgnoreAsyncOpComplete)
       
   966 			{
       
   967             iAdaptationObserver->PreemptionFinishedCallbackReceived(ETrue);
       
   968 		    iIgnoreAsyncOpComplete = EFalse;
       
   969    	    	}
       
   970         else
       
   971             {
       
   972             iAdaptationObserver->AsynchronousOperationComplete(aError, ETrue);
       
   973            	}
       
   974 		}
       
   975 	else if(aEvent == KUidA3FContextPreEmption)
       
   976 		{
       
   977 		//If we are in a normal pre-emption cycle, we should not be in a mid-state.
       
   978 		__ASSERT_DEBUG(!iDevAudio->IsMidState(iDevAudio->iActiveState), Panic(EInvalidStateDuringPreemptionCycle));
       
   979 		iIgnoreAsyncOpComplete = ETrue;
       
   980 		iAdaptationObserver->PreemptionStartedCallbackReceived();
       
   981 		}
       
   982 	//In a clashing pre-emption cycle we must be in a commit cycle, so do nothing here - CDevCommonControl deals
       
   983 	//with this case.
       
   984 	DP_OUT();
       
   985 	}
       
   986 
       
   987 
       
   988 // -----------------------------------------------------------------------------
       
   989 // From class MCustomInterfaceSupportObserver
       
   990 // CDevAudio::CustomInterfaceRemoval
       
   991 // -----------------------------------------------------------------------------
       
   992 //
       
   993 void CDevAudioControl::CustomInterfaceRemoval(TUid aUid, TAny* /*aPtr*/)
       
   994 	{
       
   995 	DP_CONTEXT(CDevAudioControl::CustomInterfaceRemoval *CD1*, CtxDevSound, DPLOCAL);
       
   996 	DP_IN();
       
   997 	// TODO: Review this functionality
       
   998 	iAdaptationObserver->InterfaceDeleted(aUid);
       
   999 	DP_OUT();
       
  1000 	}
       
  1001 
       
  1002 // ---------------------------------------------------------------------------
       
  1003 // CDevAudioControl::ResolveSampleRate
       
  1004 // ---------------------------------------------------------------------------
       
  1005 TInt CDevAudioControl::ResolveSampleRate(TInt aSampleRate, TInt& aSampleRateValue)
       
  1006 	{
       
  1007 	DP_CONTEXT(CDevAudioControl::ResolveSampleRate, CtxDevSound, DPLOCAL);
       
  1008 	DP_IN();
       
  1009 	TInt err(KErrArgument);
       
  1010 
       
  1011 	for (TUint i=0; i<=KMaxSampleRateIndex; i++)
       
  1012 		{
       
  1013 		if(KRateTableLookup[i].iSampleRate == aSampleRate)
       
  1014 			{
       
  1015 			aSampleRateValue = KRateTableLookup[i].iSampleRateValue;
       
  1016 			err = KErrNone;
       
  1017 			break;
       
  1018 			}
       
  1019 		}
       
  1020 
       
  1021 	//To avoid the value return a non desired value.
       
  1022 	if (err != KErrNone)
       
  1023 		{
       
  1024 		aSampleRateValue = 0;
       
  1025 		}
       
  1026 
       
  1027 	DP0_RET(err, "%d");
       
  1028 	}
       
  1029 
       
  1030 
       
  1031 // ---------------------------------------------------------------------------
       
  1032 // CDevAudioControl::ResolveMode
       
  1033 // ---------------------------------------------------------------------------
       
  1034 TInt CDevAudioControl::ResolveMode(TUint aModeValue, TUid& aMode)
       
  1035 	{
       
  1036 	DP_CONTEXT(CDevAudioControl::ResolveMode *CD1*, CtxDevSound, DPLOCAL);
       
  1037 	DP_IN();
       
  1038 	TInt err(KErrArgument);
       
  1039 
       
  1040 	
       
  1041 	for (TInt i=0; i<=KMaxModeIndex; i++)
       
  1042 		{
       
  1043 		const TAudioModeTableEntry& entry = KModeTableLookup[i];
       
  1044 		if (entry.iAudioModeValue == aModeValue)
       
  1045 			{
       
  1046 			aMode = entry.iAudioMode;
       
  1047 			err = KErrNone;
       
  1048 			break;
       
  1049 			}
       
  1050 		}
       
  1051 	
       
  1052 	DP0_RET(err,"%d");
       
  1053 	}
       
  1054 
       
  1055 // ---------------------------------------------------------------------------
       
  1056 // CDevAudioControl::GetModes
       
  1057 // ---------------------------------------------------------------------------
       
  1058 TUint CDevAudioControl::GetModes(const RArray<TUid>& aMode)
       
  1059 	{
       
  1060 	DP_CONTEXT(CDevAudioControl::GetModes *CD1*, CtxDevSound, DPLOCAL);
       
  1061 	DP_IN();
       
  1062 
       
  1063 	TUint result = 0;
       
  1064 	TInt count = aMode.Count();
       
  1065 
       
  1066 	for (TInt i=0; i<count; i++)
       
  1067 		{
       
  1068 		result |= GetMode(aMode[i]);
       
  1069 		}
       
  1070 	DP0_RET(result,"%d");
       
  1071 	}
       
  1072 
       
  1073 
       
  1074 // ---------------------------------------------------------------------------
       
  1075 // CDevAudioControl::GetMode
       
  1076 // ---------------------------------------------------------------------------
       
  1077 TUint CDevAudioControl::GetMode(TUid aMode)
       
  1078 	{
       
  1079 	DP_CONTEXT(CDevAudioControl::GetMode *CD1*, CtxDevSound, DPLOCAL);
       
  1080 	DP_IN();
       
  1081 	
       
  1082 	TUint result = 0;
       
  1083 	
       
  1084 	for (TInt e=0; e<=KMaxModeIndex; e++)
       
  1085 		{
       
  1086 		if(KModeTableLookup[e].iAudioMode == aMode)
       
  1087 			{
       
  1088 			result = KModeTableLookup[e].iAudioModeValue;
       
  1089 			break;
       
  1090 			}
       
  1091 		}
       
  1092 	DP0_RET(result,"%d");
       
  1093 	}
       
  1094 
       
  1095 // ---------------------------------------------------------------------------
       
  1096 // CDevAudioControl::GetSampleRates
       
  1097 // ---------------------------------------------------------------------------
       
  1098 TUint CDevAudioControl::GetSampleRates(const RArray<TInt>& aSampleRates)
       
  1099 	{
       
  1100 	DP_CONTEXT(CDevAudioControl::GetSampleRates *CD1*, CtxDevSound, DPLOCAL);
       
  1101 	DP_IN();
       
  1102 	
       
  1103 	TUint result = 0;
       
  1104 	TInt count = aSampleRates.Count();
       
  1105 	
       
  1106 	for (TInt i=0; i<count; i++)
       
  1107 		{
       
  1108 		result |= GetSampleRate(aSampleRates[i]);
       
  1109 		}
       
  1110 	DP0_RET(result,"%d");
       
  1111 	}
       
  1112 
       
  1113 // ---------------------------------------------------------------------------
       
  1114 // CDevAudioControl::GetSampleRate
       
  1115 // ---------------------------------------------------------------------------
       
  1116 TUint CDevAudioControl::GetSampleRate(TInt aSampleRates)
       
  1117 	{
       
  1118 	DP_CONTEXT(CDevAudioControl::GetSampleRate *CD1*, CtxDevSound, DPLOCAL);
       
  1119 	DP_IN();
       
  1120 
       
  1121 	TUint result = 0;
       
  1122 	TInt position = 0;
       
  1123 	TInt lowerbound = 0;
       
  1124 	TInt upperbound = KMaxSampleRateIndex;
       
  1125 
       
  1126 	if ((aSampleRates < KRateTableLookup[lowerbound].iSampleRateValue) || (aSampleRates > KRateTableLookup[upperbound].iSampleRateValue)) 
       
  1127 		{
       
  1128 		//value request not found in the array.
       
  1129 		DP0_RET(result,"%d");
       
  1130 		}
       
  1131 
       
  1132 	//Binary Search
       
  1133 	position = ( lowerbound + upperbound) / 2;
       
  1134 
       
  1135 	while((KRateTableLookup[position].iSampleRateValue != aSampleRates) && (lowerbound <= upperbound))
       
  1136 		{
       
  1137 		if (KRateTableLookup[position].iSampleRateValue > aSampleRates)
       
  1138 			{
       
  1139 			upperbound = position - 1;
       
  1140 			}
       
  1141 		else
       
  1142 			{
       
  1143 			lowerbound = position + 1;
       
  1144 			}
       
  1145 		position = (lowerbound + upperbound) / 2;
       
  1146 		}
       
  1147 
       
  1148 	result = KRateTableLookup[position].iSampleRate;
       
  1149 
       
  1150 	DP0_RET(result,"%d");
       
  1151 	}
       
  1152 
       
  1153 // ---------------------------------------------------------------------------
       
  1154 // CDevAudioControl::ProcessingFinishedReceived
       
  1155 // ---------------------------------------------------------------------------
       
  1156 TInt CDevAudioControl::ProcessingFinishedReceived(TBool& /*aAyncOperation*/)
       
  1157 	{
       
  1158 	return KErrNone;
       
  1159 	}
       
  1160 	
       
  1161 // ---------------------------------------------------------------------------
       
  1162 // CDevAudioControl::ProcessingError
       
  1163 // ---------------------------------------------------------------------------
       
  1164 TInt CDevAudioControl::ProcessingError(TBool& /*aAyncOperation*/)
       
  1165     {
       
  1166     return KErrNone;
       
  1167     }
       
  1168 
       
  1169 // ---------------------------------------------------------------------------
       
  1170 // CDevAudioControl::RequestEmptyBuffers
       
  1171 // ---------------------------------------------------------------------------	
       
  1172 TInt CDevAudioControl::RequestEmptyBuffers()
       
  1173 	{
       
  1174 	DP_CONTEXT(CDevAudioControl::RequestEmptyBuffers *CD1*, CtxDevSound, DPLOCAL);
       
  1175 	DP_IN();
       
  1176 
       
  1177 	TInt err(KErrNotReady);
       
  1178 
       
  1179 	if(iDevAudio)
       
  1180 		{
       
  1181 		if(iDevAudio->iActiveState == EDevSoundAdaptorPaused_Primed)
       
  1182 			{
       
  1183 			err = iDevAudio->iAudioStream->Flush();
       
  1184 			}
       
  1185 		else if (iDevAudio->iActiveState == EDevSoundAdaptorActive_Active)
       
  1186 			{
       
  1187 			err = Pause();
       
  1188 			if(err == KErrNone)
       
  1189 				{
       
  1190 				iPauseResumeSequenceDueToEmptyBuffers = ETrue;
       
  1191 				}
       
  1192 			
       
  1193 			}
       
  1194 		}
       
  1195 	DP0_RET(err,"%d");
       
  1196 	};
       
  1197 
       
  1198 void CDevAudioControl::Panic(TDevSoundAdaptorPanicCode aCode)
       
  1199 	{
       
  1200 	_LIT(KMMFDevSoundAdaptorPanicCategory, "DevSoundAdaptor");
       
  1201 	User::Panic(KMMFDevSoundAdaptorPanicCategory, aCode);
       
  1202 	}
       
  1203 
       
  1204 // ---------------------------------------------------------------------------
       
  1205 // CDevAudioControl::GetTimePlayed
       
  1206 // ---------------------------------------------------------------------------
       
  1207 TInt CDevAudioControl::GetTimePlayed(TTimeIntervalMicroSeconds& aTime)
       
  1208 	{
       
  1209 	DP_CONTEXT(CDevAudioControl::GetTimePlayed *CD1*, CtxDevSound, DPLOCAL);
       
  1210 	DP_IN();
       
  1211 	TInt err = iDevAudio->iAudioStream->GetStreamTime(aTime);
       
  1212 	DP0_RET(err,"%d");
       
  1213 	}
       
  1214 
       
  1215 // ---------------------------------------------------------------------------
       
  1216 // CDevAudioControl::Resume
       
  1217 // ---------------------------------------------------------------------------
       
  1218 TBool CDevAudioControl::Resume()
       
  1219 	{
       
  1220 	DP_CONTEXT(CDevAudioControl::Stop *CD1*, CtxDevSound, DPLOCAL);
       
  1221 	DP_IN();
       
  1222 	ASSERT(EFalse);
       
  1223 	DP0_RET(KErrNone, "%d");
       
  1224 	}
       
  1225 
       
  1226 // -----------------------------------------------------------------------------
       
  1227 // CDevAudioControl::BufferErrorEvent
       
  1228 // -----------------------------------------------------------------------------
       
  1229 //
       
  1230 void CDevAudioControl::BufferErrorEvent()
       
  1231 	{
       
  1232 	ASSERT(EFalse); //This should never happen
       
  1233 	}
       
  1234 // End of file