os/kernelhwsrv/kerneltest/e32test/multimedia/t_soundutils.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/kernelhwsrv/kerneltest/e32test/multimedia/t_soundutils.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,418 @@
     1.4 +// Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.5 +// All rights reserved.
     1.6 +// This component and the accompanying materials are made available
     1.7 +// under the terms of the License "Eclipse Public License v1.0"
     1.8 +// which accompanies this distribution, and is available
     1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.10 +//
    1.11 +// Initial Contributors:
    1.12 +// Nokia Corporation - initial contribution.
    1.13 +//
    1.14 +// Contributors:
    1.15 +//
    1.16 +// Description:
    1.17 +// e32test\multimedia\t_soundutils.cpp
    1.18 +// 
    1.19 +//
    1.20 +
    1.21 +/**
    1.22 + @file Utilities used by the shared chunk sound driver test code.
    1.23 +*/
    1.24 +
    1.25 +#include <e32test.h>
    1.26 +#include <e32math.h>
    1.27 +#include "t_soundutils.h"
    1.28 +
    1.29 +const TInt SineAddressBits = 10;
    1.30 +const TInt SineAddressFractionBits = 32-SineAddressBits;
    1.31 +const TInt SineTableSize = 1<<SineAddressBits;
    1.32 +
    1.33 +TUint32 ToneIndex = 0;
    1.34 +TUint32 ToneIndexStep = 0;
    1.35 +
    1.36 +TInt32* SineTable = NULL;
    1.37 +
    1.38 +_LIT(KSndDirCapsTitle, "Direction    :");
    1.39 +_LIT(KSndDirRecord, " Record");
    1.40 +_LIT(KSndDirPlay, " Playback");
    1.41 +
    1.42 +_LIT(KSndRateCapsTitle,"Sample rates :");
    1.43 +_LIT(KSndRateConfigTitle,"Current rate :");
    1.44 +_LIT(KSndRate7350Hz," 7.35KHz");
    1.45 +_LIT(KSndRate8000Hz," 8KHz");
    1.46 +_LIT(KSndRate8820Hz," 8.82KHz");
    1.47 +_LIT(KSndRate9600Hz," 9.6KHz");
    1.48 +_LIT(KSndRate11025Hz," 11.025KHz");
    1.49 +_LIT(KSndRate12000Hz," 12KHz");
    1.50 +_LIT(KSndRate14700Hz," 14.7KHz");
    1.51 +_LIT(KSndRate16000Hz," 16KHz");
    1.52 +_LIT(KSndRate22050Hz," 22.05KHz");
    1.53 +_LIT(KSndRate24000Hz," 24KHz");
    1.54 +_LIT(KSndRate29400Hz," 29.4KHz");
    1.55 +_LIT(KSndRate32000Hz," 32KHz");
    1.56 +_LIT(KSndRate44100Hz," 44.1KHz");
    1.57 +_LIT(KSndRate48000Hz," 48KHz");
    1.58 +
    1.59 +_LIT(KSndChanConfigCapsTitle,"Chan configs :");
    1.60 +_LIT(KSndChanConfigMono," Mono");
    1.61 +_LIT(KSndChanConfigStereo," Stereo");
    1.62 +_LIT(KSndChanConfig3Chan," 3Chan");
    1.63 +_LIT(KSndChanConfig4Chan," 4Chan");
    1.64 +_LIT(KSndChanConfig5Chan," 5Chan");
    1.65 +_LIT(KSndChanConfig6Chan," 6Chan");
    1.66 +
    1.67 +_LIT(KSndEncodingCapsTitle,"Encodings    :");
    1.68 +_LIT(KSndEncodingConfigTitle,"Encoding     :");
    1.69 +_LIT(KSndEncoding8BitPCM," 8bit PCM");
    1.70 +_LIT(KSndEncoding16BitPCM," 16bit PCM");
    1.71 +_LIT(KSndEncoding24BitPCM," 24bit PCM");
    1.72 +
    1.73 +_LIT(KSndDataFormatCapsTitle,"Data formats :");
    1.74 +_LIT(KSndDataFormatConfigTitle,"Data format  :");
    1.75 +_LIT(KSndDataFormatInterleaved," Interleaved");
    1.76 +_LIT(KSndDataFormatNonInterleaved," Non-interleaved");
    1.77 +
    1.78 +
    1.79 +GLDEF_C TInt BytesPerSample(TCurrentSoundFormatV02& aFormat)
    1.80 +	{
    1.81 +	TInt bytes = aFormat.iChannels;
    1.82 +	switch(aFormat.iEncoding)
    1.83 +		{
    1.84 +		case ESoundEncoding24BitPCM:
    1.85 +			bytes *= 3;
    1.86 +			break;	
    1.87 +		case ESoundEncoding16BitPCM:
    1.88 +			bytes *= 2;
    1.89 +			break;
    1.90 +		case ESoundEncoding8BitPCM:
    1.91 +			break;
    1.92 +		default:
    1.93 +			bytes=0;
    1.94 +			break;
    1.95 +		}
    1.96 +	return(bytes);
    1.97 +	}
    1.98 +
    1.99 +GLDEF_C TInt RateInSamplesPerSecond(TSoundRate aRate)
   1.100 +	{
   1.101 +	switch(aRate)
   1.102 +		{
   1.103 +		case ESoundRate7350Hz: 	return(7350);
   1.104 +		case ESoundRate8000Hz: 	return(8000);
   1.105 +		case ESoundRate8820Hz: 	return(8820);
   1.106 +		case ESoundRate9600Hz: 	return(9600);
   1.107 +		case ESoundRate11025Hz: return(11025);
   1.108 +		case ESoundRate12000Hz: return(12000);
   1.109 +		case ESoundRate14700Hz:	return(14700);
   1.110 +		case ESoundRate16000Hz: return(16000);
   1.111 +		case ESoundRate22050Hz: return(22050);
   1.112 +		case ESoundRate24000Hz: return(24000);
   1.113 +		case ESoundRate29400Hz: return(29400);
   1.114 +		case ESoundRate32000Hz: return(32000);
   1.115 +		case ESoundRate44100Hz: return(44100);
   1.116 +		case ESoundRate48000Hz: return(48000);
   1.117 +		default: return(0);
   1.118 +		};
   1.119 +	}
   1.120 +	
   1.121 +GLDEF_C TInt BytesPerSecond(TCurrentSoundFormatV02& aFormat)
   1.122 +	{
   1.123 +	return(RateInSamplesPerSecond(aFormat.iRate) * BytesPerSample(aFormat));
   1.124 +	}
   1.125 +	
   1.126 +GLDEF_C TInt ValidBufferSize(TInt aSize, TInt aDeviceMinSize, TCurrentSoundFormatV02& aFormat)
   1.127 +	{
   1.128 +	// Keep the buffer length a multiple of the number of bytes per sample
   1.129 +	TInt bytesPerSample=BytesPerSample(aFormat);
   1.130 +	aSize-=(aSize%bytesPerSample);
   1.131 +	
   1.132 +	// Keep the buffer length valid for driver.
   1.133 +	if (aDeviceMinSize)
   1.134 +		aSize&=~(aDeviceMinSize-1); 	
   1.135 +	return(aSize);	
   1.136 +	}			
   1.137 +	
   1.138 +GLDEF_C TInt MakeSineTable(TCurrentSoundFormatV02& aPlayFormat)
   1.139 +	{
   1.140 +	delete SineTable;
   1.141 +	SineTable = (TInt32*)User::Alloc(SineTableSize*sizeof(TInt32));
   1.142 +	if (!SineTable)
   1.143 +		return(KErrNoMemory);
   1.144 +
   1.145 +	TInt i;
   1.146 +	TReal scale;
   1.147 +
   1.148 +	switch(aPlayFormat.iEncoding)
   1.149 +		{
   1.150 +		case ESoundEncoding8BitPCM:
   1.151 +			scale = 127;
   1.152 +			break;
   1.153 +		case ESoundEncoding16BitPCM:
   1.154 +			scale = 32767;
   1.155 +			break;
   1.156 +		case ESoundEncoding24BitPCM:
   1.157 +			scale = 8388607;
   1.158 +			break;
   1.159 +		default:
   1.160 +			return(KErrNotSupported);
   1.161 +		}
   1.162 +			
   1.163 +	for(i=0; i<SineTableSize; i++)
   1.164 +		{
   1.165 +		TReal r = KPi*2.0*(TReal)i/(TReal)SineTableSize;
   1.166 +		Math::Sin(r,r);
   1.167 +		r *= scale;
   1.168 +		Math::Int(SineTable[i],r);
   1.169 +		}
   1.170 +	
   1.171 +	return KErrNone;
   1.172 +	};
   1.173 +
   1.174 +GLDEF_C TInt SetToneFrequency(TUint aFrequency,TCurrentSoundFormatV02& aPlayFormat)
   1.175 +	{
   1.176 +	TInt64 step(MAKE_TINT64(aFrequency,0));
   1.177 +	step /= RateInSamplesPerSecond(aPlayFormat.iRate);
   1.178 +	ToneIndexStep = I64LOW(step);
   1.179 +	return((I64HIGH(step)==0) ?  KErrNone : KErrGeneral);
   1.180 +	}
   1.181 +
   1.182 +GLDEF_C void WriteTone(TDes8& aBuffer,TCurrentSoundFormatV02& aPlayFormat)
   1.183 +	{
   1.184 +	aBuffer.SetMax();
   1.185 +
   1.186 +	TUint32 index = ToneIndex;
   1.187 +	TUint32 step = ToneIndexStep;
   1.188 +	TInt32* table = SineTable;
   1.189 +
   1.190 +	switch(aPlayFormat.iEncoding)
   1.191 +		{
   1.192 +		case ESoundEncoding16BitPCM:
   1.193 +			{
   1.194 +			TInt16* ptr = (TInt16*)aBuffer.Ptr();
   1.195 +			TInt16* end = ptr+aBuffer.Length()/2;
   1.196 +			while(ptr<end)
   1.197 +				{
   1.198 +				*ptr++ = (TInt16)table[index>>SineAddressFractionBits];
   1.199 +				if (aPlayFormat.iChannels == 2)
   1.200 +					*ptr++ = (TInt16)table[index>>SineAddressFractionBits];
   1.201 +				index += step;
   1.202 +				}
   1.203 +			}
   1.204 +			break;
   1.205 +		case ESoundEncoding8BitPCM:
   1.206 +			{
   1.207 +			TUint8* ptr = (TUint8*)aBuffer.Ptr();
   1.208 +			TUint8* end = ptr+aBuffer.Length();
   1.209 +			while(ptr<end)
   1.210 +				{
   1.211 +				*ptr++ = (TUint8)table[index>>8];
   1.212 +				if (aPlayFormat.iChannels == 2)
   1.213 +					*ptr++ = (TInt8)table[index>>8];
   1.214 +				index += step;
   1.215 +				}
   1.216 +			}
   1.217 +			break;
   1.218 +		case ESoundEncoding24BitPCM:
   1.219 +			{
   1.220 +			TUint8* ptr = (TUint8*)aBuffer.Ptr();
   1.221 +			TUint8* end = ptr+aBuffer.Length();
   1.222 +			while(ptr<end)
   1.223 +				{
   1.224 +				*ptr++ = (TUint8)table[index>>24];
   1.225 +				if (aPlayFormat.iChannels == 2)
   1.226 +					*ptr++ = (TInt8)table[index>>24];
   1.227 +				index += step;
   1.228 +				}
   1.229 +			}
   1.230 +			break;
   1.231 +
   1.232 +		default:
   1.233 +			break;
   1.234 +		}
   1.235 +
   1.236 +	ToneIndex = index;
   1.237 +	}
   1.238 +	
   1.239 +GLDEF_C void Cleanup()
   1.240 +	{
   1.241 +	delete SineTable;
   1.242 +	}	
   1.243 +	
   1.244 +GLDEF_C void PrintCaps(TSoundFormatsSupportedV02& aCaps,RTest& aTest)
   1.245 +	{
   1.246 +	TBuf<128> buf;
   1.247 +	
   1.248 +	aTest.Printf(_L("**Sound Capabilities**\r\n"));
   1.249 +	
   1.250 +	// Display the data transfer direction
   1.251 +	buf.Zero();
   1.252 +	buf.Append(KSndDirCapsTitle);
   1.253 +	if (aCaps.iDirection==ESoundDirRecord)
   1.254 +		buf.Append(KSndDirRecord);
   1.255 +	else
   1.256 +		buf.Append(KSndDirPlay);
   1.257 +	buf.Append(_L("\r\n"));
   1.258 +	aTest.Printf(buf);
   1.259 +	
   1.260 +	// Display the channel configuration
   1.261 +	buf.Zero();
   1.262 +	buf.Append(KSndChanConfigCapsTitle);
   1.263 +	if (aCaps.iChannels & KSoundMonoChannel)
   1.264 +		buf.Append(KSndChanConfigMono);
   1.265 +	if (aCaps.iChannels & KSoundStereoChannel)
   1.266 +		buf.Append(KSndChanConfigStereo);
   1.267 +	if (aCaps.iChannels & KSoundThreeChannel)
   1.268 +		buf.Append(KSndChanConfig3Chan);
   1.269 +	if (aCaps.iChannels & KSoundFourChannel)
   1.270 +		buf.Append(KSndChanConfig4Chan);
   1.271 +	if (aCaps.iChannels & KSoundFiveChannel)
   1.272 +		buf.Append(KSndChanConfig5Chan);
   1.273 +	if (aCaps.iChannels & KSoundSixChannel)
   1.274 +		buf.Append(KSndChanConfig6Chan);
   1.275 +	buf.Append(_L("\r\n"));
   1.276 +	aTest.Printf(buf);
   1.277 +	
   1.278 +	// Display the supported sample rates
   1.279 +	buf.Zero();
   1.280 +	buf.Append(KSndRateCapsTitle);
   1.281 +	if (aCaps.iRates & KSoundRate7350Hz)
   1.282 +		buf.Append(KSndRate7350Hz);
   1.283 +	if (aCaps.iRates & KSoundRate8000Hz)
   1.284 +		buf.Append(KSndRate8000Hz);
   1.285 +	if (aCaps.iRates & KSoundRate8820Hz)
   1.286 +		buf.Append(KSndRate8820Hz);
   1.287 +	if (aCaps.iRates & KSoundRate9600Hz)
   1.288 +		buf.Append(KSndRate9600Hz);
   1.289 +	if (aCaps.iRates & KSoundRate11025Hz)
   1.290 +		buf.Append(KSndRate11025Hz);
   1.291 +	if (aCaps.iRates & KSoundRate12000Hz)
   1.292 +		buf.Append(KSndRate12000Hz);
   1.293 +	if (aCaps.iRates & KSoundRate14700Hz)
   1.294 +		buf.Append(KSndRate14700Hz);
   1.295 +	if (aCaps.iRates & KSoundRate16000Hz)
   1.296 +		buf.Append(KSndRate16000Hz);
   1.297 +	if (aCaps.iRates & KSoundRate22050Hz)
   1.298 +		buf.Append(KSndRate22050Hz);
   1.299 +	if (aCaps.iRates & KSoundRate24000Hz)
   1.300 +		buf.Append(KSndRate24000Hz);
   1.301 +	if (aCaps.iRates & KSoundRate29400Hz)
   1.302 +		buf.Append(KSndRate29400Hz);
   1.303 +	if (aCaps.iRates & KSoundRate32000Hz)
   1.304 +		buf.Append(KSndRate32000Hz);
   1.305 +	if (aCaps.iRates & KSoundRate44100Hz)
   1.306 +		buf.Append(KSndRate44100Hz);
   1.307 +	if (aCaps.iRates & KSoundRate48000Hz)
   1.308 +		buf.Append(KSndRate48000Hz);
   1.309 +	buf.Append(_L("\r\n"));
   1.310 +	aTest.Printf(buf);
   1.311 +	
   1.312 +	// Display the sound encodings supported
   1.313 +	buf.Zero();
   1.314 +	buf.Append(KSndEncodingCapsTitle);
   1.315 +	if (aCaps.iEncodings & KSoundEncoding8BitPCM)
   1.316 +		buf.Append(KSndEncoding8BitPCM);
   1.317 +	if (aCaps.iEncodings & KSoundEncoding16BitPCM)
   1.318 +		buf.Append(KSndEncoding16BitPCM);
   1.319 +	if (aCaps.iEncodings & KSoundEncoding24BitPCM)
   1.320 +		buf.Append(KSndEncoding24BitPCM);
   1.321 +	buf.Append(_L("\r\n"));
   1.322 +	aTest.Printf(buf);
   1.323 +	
   1.324 +	// Display the data formats supported
   1.325 +	buf.Zero();
   1.326 +	buf.Append(KSndDataFormatCapsTitle);
   1.327 +	if (aCaps.iDataFormats & KSoundDataFormatInterleaved)
   1.328 +		buf.Append(KSndDataFormatInterleaved);
   1.329 +	if (aCaps.iDataFormats & KSoundDataFormatNonInterleaved)
   1.330 +		buf.Append(KSndDataFormatNonInterleaved);
   1.331 +	buf.Append(_L("\r\n"));
   1.332 +	aTest.Printf(buf);
   1.333 +	
   1.334 +	// Display the minimum request size and the request alignment factor.
   1.335 +	aTest.Printf(_L("Min req size : %d\r\n"),aCaps.iRequestMinSize);
   1.336 +	aTest.Printf(_L("Req alignment: %d\r\n"),aCaps.iRequestAlignment);
   1.337 +	}
   1.338 +	
   1.339 +GLDEF_C void PrintConfig(TCurrentSoundFormatV02& aConfig,RTest& aTest)
   1.340 +	{
   1.341 +	TBuf<80> buf;
   1.342 +	
   1.343 +	aTest.Printf(_L("**Sound configuration**\r\n"));
   1.344 +	
   1.345 +	// Display the current channel configuration
   1.346 +	aTest.Printf(_L("Channels     : %d\r\n"),aConfig.iChannels);
   1.347 +	
   1.348 +	// Display the current sample rate
   1.349 +	buf.Zero();
   1.350 +	buf.Append(KSndRateConfigTitle);
   1.351 +	if (aConfig.iRate==ESoundRate7350Hz)
   1.352 +		buf.Append(KSndRate7350Hz);
   1.353 +	else if (aConfig.iRate==ESoundRate8000Hz)
   1.354 +		buf.Append(KSndRate8000Hz);
   1.355 +	else if (aConfig.iRate==ESoundRate8820Hz)
   1.356 +		buf.Append(KSndRate8820Hz);
   1.357 +	else if (aConfig.iRate==ESoundRate9600Hz)
   1.358 +		buf.Append(KSndRate9600Hz);
   1.359 +	else if (aConfig.iRate==ESoundRate11025Hz)
   1.360 +		buf.Append(KSndRate11025Hz);
   1.361 +	else if (aConfig.iRate==ESoundRate12000Hz)
   1.362 +		buf.Append(KSndRate12000Hz);
   1.363 +	else if (aConfig.iRate==ESoundRate14700Hz)
   1.364 +		buf.Append(KSndRate14700Hz);
   1.365 +	else if (aConfig.iRate==ESoundRate16000Hz)
   1.366 +		buf.Append(KSndRate16000Hz);
   1.367 +	else if (aConfig.iRate==ESoundRate22050Hz)
   1.368 +		buf.Append(KSndRate22050Hz);
   1.369 +	else if (aConfig.iRate==ESoundRate24000Hz)
   1.370 +		buf.Append(KSndRate24000Hz);
   1.371 +	else if (aConfig.iRate==ESoundRate29400Hz)
   1.372 +		buf.Append(KSndRate29400Hz);
   1.373 +	else if (aConfig.iRate==ESoundRate32000Hz)
   1.374 +		buf.Append(KSndRate32000Hz);
   1.375 +	else if (aConfig.iRate==ESoundRate44100Hz)
   1.376 +		buf.Append(KSndRate44100Hz);
   1.377 +	else if (aConfig.iRate==ESoundRate48000Hz)
   1.378 +		buf.Append(KSndRate48000Hz);
   1.379 +	buf.Append(_L("\r\n"));
   1.380 +	aTest.Printf(buf);
   1.381 +	
   1.382 +	// Display the current encoding
   1.383 +	buf.Zero();
   1.384 +	buf.Append(KSndEncodingConfigTitle);
   1.385 +	if (aConfig.iEncoding==ESoundEncoding8BitPCM)
   1.386 +		buf.Append(KSndEncoding8BitPCM);
   1.387 +	else if (aConfig.iEncoding==ESoundEncoding16BitPCM)
   1.388 +		buf.Append(KSndEncoding16BitPCM);
   1.389 +	else if (aConfig.iEncoding==ESoundEncoding24BitPCM)
   1.390 +		buf.Append(KSndEncoding24BitPCM);
   1.391 +	buf.Append(_L("\r\n"));
   1.392 +	aTest.Printf(buf);
   1.393 +	
   1.394 +	// Display the current data format
   1.395 +	buf.Zero();
   1.396 +	buf.Append(KSndDataFormatConfigTitle);
   1.397 +	if (aConfig.iDataFormat==ESoundDataFormatInterleaved)
   1.398 +		buf.Append(KSndDataFormatInterleaved);
   1.399 +	else if (aConfig.iDataFormat==ESoundDataFormatNonInterleaved)
   1.400 +		buf.Append(KSndDataFormatNonInterleaved);
   1.401 +	buf.Append(_L("\r\n"));
   1.402 +	aTest.Printf(buf);
   1.403 +	}
   1.404 +	
   1.405 +GLDEF_C void PrintBufferConf(TTestSharedChunkBufConfig& aBufConf,RTest& aTest)
   1.406 +	{
   1.407 +	TBuf<80> buf(0);
   1.408 +	
   1.409 +	aTest.Printf(_L("**Buffer configuration**\r\n"));
   1.410 +	
   1.411 +	// Display the buffer configuration
   1.412 +	buf.Format(_L("NumBufs:%d Size:%xH(%d)\r\n"),aBufConf.iNumBuffers,aBufConf.iBufferSizeInBytes,aBufConf.iBufferSizeInBytes);
   1.413 +	aTest.Printf(buf);
   1.414 +	if (aBufConf.iFlags & KScFlagBufOffsetListInUse)
   1.415 +		{
   1.416 +		buf.Format(_L(" Offsets[%08xH,%08xH,%08xH,%08xH]\r\n"),aBufConf.iBufferOffsetList[0],aBufConf.iBufferOffsetList[1],aBufConf.iBufferOffsetList[2],aBufConf.iBufferOffsetList[3]);
   1.417 +		aTest.Printf(buf);
   1.418 +		buf.Format(_L(" Offsets[%08xH,%08xH,%08xH,%08xH]\r\n"),aBufConf.iBufferOffsetList[4],aBufConf.iBufferOffsetList[5],aBufConf.iBufferOffsetList[6],aBufConf.iBufferOffsetList[7]);
   1.419 +		aTest.Printf(buf);
   1.420 +		}
   1.421 +	}