os/kernelhwsrv/kerneltest/e32test/multimedia/t_soundutils.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 // Copyright (c) 2006-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 the License "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 // e32test\multimedia\t_soundutils.cpp
    15 // 
    16 //
    17 
    18 /**
    19  @file Utilities used by the shared chunk sound driver test code.
    20 */
    21 
    22 #include <e32test.h>
    23 #include <e32math.h>
    24 #include "t_soundutils.h"
    25 
    26 const TInt SineAddressBits = 10;
    27 const TInt SineAddressFractionBits = 32-SineAddressBits;
    28 const TInt SineTableSize = 1<<SineAddressBits;
    29 
    30 TUint32 ToneIndex = 0;
    31 TUint32 ToneIndexStep = 0;
    32 
    33 TInt32* SineTable = NULL;
    34 
    35 _LIT(KSndDirCapsTitle, "Direction    :");
    36 _LIT(KSndDirRecord, " Record");
    37 _LIT(KSndDirPlay, " Playback");
    38 
    39 _LIT(KSndRateCapsTitle,"Sample rates :");
    40 _LIT(KSndRateConfigTitle,"Current rate :");
    41 _LIT(KSndRate7350Hz," 7.35KHz");
    42 _LIT(KSndRate8000Hz," 8KHz");
    43 _LIT(KSndRate8820Hz," 8.82KHz");
    44 _LIT(KSndRate9600Hz," 9.6KHz");
    45 _LIT(KSndRate11025Hz," 11.025KHz");
    46 _LIT(KSndRate12000Hz," 12KHz");
    47 _LIT(KSndRate14700Hz," 14.7KHz");
    48 _LIT(KSndRate16000Hz," 16KHz");
    49 _LIT(KSndRate22050Hz," 22.05KHz");
    50 _LIT(KSndRate24000Hz," 24KHz");
    51 _LIT(KSndRate29400Hz," 29.4KHz");
    52 _LIT(KSndRate32000Hz," 32KHz");
    53 _LIT(KSndRate44100Hz," 44.1KHz");
    54 _LIT(KSndRate48000Hz," 48KHz");
    55 
    56 _LIT(KSndChanConfigCapsTitle,"Chan configs :");
    57 _LIT(KSndChanConfigMono," Mono");
    58 _LIT(KSndChanConfigStereo," Stereo");
    59 _LIT(KSndChanConfig3Chan," 3Chan");
    60 _LIT(KSndChanConfig4Chan," 4Chan");
    61 _LIT(KSndChanConfig5Chan," 5Chan");
    62 _LIT(KSndChanConfig6Chan," 6Chan");
    63 
    64 _LIT(KSndEncodingCapsTitle,"Encodings    :");
    65 _LIT(KSndEncodingConfigTitle,"Encoding     :");
    66 _LIT(KSndEncoding8BitPCM," 8bit PCM");
    67 _LIT(KSndEncoding16BitPCM," 16bit PCM");
    68 _LIT(KSndEncoding24BitPCM," 24bit PCM");
    69 
    70 _LIT(KSndDataFormatCapsTitle,"Data formats :");
    71 _LIT(KSndDataFormatConfigTitle,"Data format  :");
    72 _LIT(KSndDataFormatInterleaved," Interleaved");
    73 _LIT(KSndDataFormatNonInterleaved," Non-interleaved");
    74 
    75 
    76 GLDEF_C TInt BytesPerSample(TCurrentSoundFormatV02& aFormat)
    77 	{
    78 	TInt bytes = aFormat.iChannels;
    79 	switch(aFormat.iEncoding)
    80 		{
    81 		case ESoundEncoding24BitPCM:
    82 			bytes *= 3;
    83 			break;	
    84 		case ESoundEncoding16BitPCM:
    85 			bytes *= 2;
    86 			break;
    87 		case ESoundEncoding8BitPCM:
    88 			break;
    89 		default:
    90 			bytes=0;
    91 			break;
    92 		}
    93 	return(bytes);
    94 	}
    95 
    96 GLDEF_C TInt RateInSamplesPerSecond(TSoundRate aRate)
    97 	{
    98 	switch(aRate)
    99 		{
   100 		case ESoundRate7350Hz: 	return(7350);
   101 		case ESoundRate8000Hz: 	return(8000);
   102 		case ESoundRate8820Hz: 	return(8820);
   103 		case ESoundRate9600Hz: 	return(9600);
   104 		case ESoundRate11025Hz: return(11025);
   105 		case ESoundRate12000Hz: return(12000);
   106 		case ESoundRate14700Hz:	return(14700);
   107 		case ESoundRate16000Hz: return(16000);
   108 		case ESoundRate22050Hz: return(22050);
   109 		case ESoundRate24000Hz: return(24000);
   110 		case ESoundRate29400Hz: return(29400);
   111 		case ESoundRate32000Hz: return(32000);
   112 		case ESoundRate44100Hz: return(44100);
   113 		case ESoundRate48000Hz: return(48000);
   114 		default: return(0);
   115 		};
   116 	}
   117 	
   118 GLDEF_C TInt BytesPerSecond(TCurrentSoundFormatV02& aFormat)
   119 	{
   120 	return(RateInSamplesPerSecond(aFormat.iRate) * BytesPerSample(aFormat));
   121 	}
   122 	
   123 GLDEF_C TInt ValidBufferSize(TInt aSize, TInt aDeviceMinSize, TCurrentSoundFormatV02& aFormat)
   124 	{
   125 	// Keep the buffer length a multiple of the number of bytes per sample
   126 	TInt bytesPerSample=BytesPerSample(aFormat);
   127 	aSize-=(aSize%bytesPerSample);
   128 	
   129 	// Keep the buffer length valid for driver.
   130 	if (aDeviceMinSize)
   131 		aSize&=~(aDeviceMinSize-1); 	
   132 	return(aSize);	
   133 	}			
   134 	
   135 GLDEF_C TInt MakeSineTable(TCurrentSoundFormatV02& aPlayFormat)
   136 	{
   137 	delete SineTable;
   138 	SineTable = (TInt32*)User::Alloc(SineTableSize*sizeof(TInt32));
   139 	if (!SineTable)
   140 		return(KErrNoMemory);
   141 
   142 	TInt i;
   143 	TReal scale;
   144 
   145 	switch(aPlayFormat.iEncoding)
   146 		{
   147 		case ESoundEncoding8BitPCM:
   148 			scale = 127;
   149 			break;
   150 		case ESoundEncoding16BitPCM:
   151 			scale = 32767;
   152 			break;
   153 		case ESoundEncoding24BitPCM:
   154 			scale = 8388607;
   155 			break;
   156 		default:
   157 			return(KErrNotSupported);
   158 		}
   159 			
   160 	for(i=0; i<SineTableSize; i++)
   161 		{
   162 		TReal r = KPi*2.0*(TReal)i/(TReal)SineTableSize;
   163 		Math::Sin(r,r);
   164 		r *= scale;
   165 		Math::Int(SineTable[i],r);
   166 		}
   167 	
   168 	return KErrNone;
   169 	};
   170 
   171 GLDEF_C TInt SetToneFrequency(TUint aFrequency,TCurrentSoundFormatV02& aPlayFormat)
   172 	{
   173 	TInt64 step(MAKE_TINT64(aFrequency,0));
   174 	step /= RateInSamplesPerSecond(aPlayFormat.iRate);
   175 	ToneIndexStep = I64LOW(step);
   176 	return((I64HIGH(step)==0) ?  KErrNone : KErrGeneral);
   177 	}
   178 
   179 GLDEF_C void WriteTone(TDes8& aBuffer,TCurrentSoundFormatV02& aPlayFormat)
   180 	{
   181 	aBuffer.SetMax();
   182 
   183 	TUint32 index = ToneIndex;
   184 	TUint32 step = ToneIndexStep;
   185 	TInt32* table = SineTable;
   186 
   187 	switch(aPlayFormat.iEncoding)
   188 		{
   189 		case ESoundEncoding16BitPCM:
   190 			{
   191 			TInt16* ptr = (TInt16*)aBuffer.Ptr();
   192 			TInt16* end = ptr+aBuffer.Length()/2;
   193 			while(ptr<end)
   194 				{
   195 				*ptr++ = (TInt16)table[index>>SineAddressFractionBits];
   196 				if (aPlayFormat.iChannels == 2)
   197 					*ptr++ = (TInt16)table[index>>SineAddressFractionBits];
   198 				index += step;
   199 				}
   200 			}
   201 			break;
   202 		case ESoundEncoding8BitPCM:
   203 			{
   204 			TUint8* ptr = (TUint8*)aBuffer.Ptr();
   205 			TUint8* end = ptr+aBuffer.Length();
   206 			while(ptr<end)
   207 				{
   208 				*ptr++ = (TUint8)table[index>>8];
   209 				if (aPlayFormat.iChannels == 2)
   210 					*ptr++ = (TInt8)table[index>>8];
   211 				index += step;
   212 				}
   213 			}
   214 			break;
   215 		case ESoundEncoding24BitPCM:
   216 			{
   217 			TUint8* ptr = (TUint8*)aBuffer.Ptr();
   218 			TUint8* end = ptr+aBuffer.Length();
   219 			while(ptr<end)
   220 				{
   221 				*ptr++ = (TUint8)table[index>>24];
   222 				if (aPlayFormat.iChannels == 2)
   223 					*ptr++ = (TInt8)table[index>>24];
   224 				index += step;
   225 				}
   226 			}
   227 			break;
   228 
   229 		default:
   230 			break;
   231 		}
   232 
   233 	ToneIndex = index;
   234 	}
   235 	
   236 GLDEF_C void Cleanup()
   237 	{
   238 	delete SineTable;
   239 	}	
   240 	
   241 GLDEF_C void PrintCaps(TSoundFormatsSupportedV02& aCaps,RTest& aTest)
   242 	{
   243 	TBuf<128> buf;
   244 	
   245 	aTest.Printf(_L("**Sound Capabilities**\r\n"));
   246 	
   247 	// Display the data transfer direction
   248 	buf.Zero();
   249 	buf.Append(KSndDirCapsTitle);
   250 	if (aCaps.iDirection==ESoundDirRecord)
   251 		buf.Append(KSndDirRecord);
   252 	else
   253 		buf.Append(KSndDirPlay);
   254 	buf.Append(_L("\r\n"));
   255 	aTest.Printf(buf);
   256 	
   257 	// Display the channel configuration
   258 	buf.Zero();
   259 	buf.Append(KSndChanConfigCapsTitle);
   260 	if (aCaps.iChannels & KSoundMonoChannel)
   261 		buf.Append(KSndChanConfigMono);
   262 	if (aCaps.iChannels & KSoundStereoChannel)
   263 		buf.Append(KSndChanConfigStereo);
   264 	if (aCaps.iChannels & KSoundThreeChannel)
   265 		buf.Append(KSndChanConfig3Chan);
   266 	if (aCaps.iChannels & KSoundFourChannel)
   267 		buf.Append(KSndChanConfig4Chan);
   268 	if (aCaps.iChannels & KSoundFiveChannel)
   269 		buf.Append(KSndChanConfig5Chan);
   270 	if (aCaps.iChannels & KSoundSixChannel)
   271 		buf.Append(KSndChanConfig6Chan);
   272 	buf.Append(_L("\r\n"));
   273 	aTest.Printf(buf);
   274 	
   275 	// Display the supported sample rates
   276 	buf.Zero();
   277 	buf.Append(KSndRateCapsTitle);
   278 	if (aCaps.iRates & KSoundRate7350Hz)
   279 		buf.Append(KSndRate7350Hz);
   280 	if (aCaps.iRates & KSoundRate8000Hz)
   281 		buf.Append(KSndRate8000Hz);
   282 	if (aCaps.iRates & KSoundRate8820Hz)
   283 		buf.Append(KSndRate8820Hz);
   284 	if (aCaps.iRates & KSoundRate9600Hz)
   285 		buf.Append(KSndRate9600Hz);
   286 	if (aCaps.iRates & KSoundRate11025Hz)
   287 		buf.Append(KSndRate11025Hz);
   288 	if (aCaps.iRates & KSoundRate12000Hz)
   289 		buf.Append(KSndRate12000Hz);
   290 	if (aCaps.iRates & KSoundRate14700Hz)
   291 		buf.Append(KSndRate14700Hz);
   292 	if (aCaps.iRates & KSoundRate16000Hz)
   293 		buf.Append(KSndRate16000Hz);
   294 	if (aCaps.iRates & KSoundRate22050Hz)
   295 		buf.Append(KSndRate22050Hz);
   296 	if (aCaps.iRates & KSoundRate24000Hz)
   297 		buf.Append(KSndRate24000Hz);
   298 	if (aCaps.iRates & KSoundRate29400Hz)
   299 		buf.Append(KSndRate29400Hz);
   300 	if (aCaps.iRates & KSoundRate32000Hz)
   301 		buf.Append(KSndRate32000Hz);
   302 	if (aCaps.iRates & KSoundRate44100Hz)
   303 		buf.Append(KSndRate44100Hz);
   304 	if (aCaps.iRates & KSoundRate48000Hz)
   305 		buf.Append(KSndRate48000Hz);
   306 	buf.Append(_L("\r\n"));
   307 	aTest.Printf(buf);
   308 	
   309 	// Display the sound encodings supported
   310 	buf.Zero();
   311 	buf.Append(KSndEncodingCapsTitle);
   312 	if (aCaps.iEncodings & KSoundEncoding8BitPCM)
   313 		buf.Append(KSndEncoding8BitPCM);
   314 	if (aCaps.iEncodings & KSoundEncoding16BitPCM)
   315 		buf.Append(KSndEncoding16BitPCM);
   316 	if (aCaps.iEncodings & KSoundEncoding24BitPCM)
   317 		buf.Append(KSndEncoding24BitPCM);
   318 	buf.Append(_L("\r\n"));
   319 	aTest.Printf(buf);
   320 	
   321 	// Display the data formats supported
   322 	buf.Zero();
   323 	buf.Append(KSndDataFormatCapsTitle);
   324 	if (aCaps.iDataFormats & KSoundDataFormatInterleaved)
   325 		buf.Append(KSndDataFormatInterleaved);
   326 	if (aCaps.iDataFormats & KSoundDataFormatNonInterleaved)
   327 		buf.Append(KSndDataFormatNonInterleaved);
   328 	buf.Append(_L("\r\n"));
   329 	aTest.Printf(buf);
   330 	
   331 	// Display the minimum request size and the request alignment factor.
   332 	aTest.Printf(_L("Min req size : %d\r\n"),aCaps.iRequestMinSize);
   333 	aTest.Printf(_L("Req alignment: %d\r\n"),aCaps.iRequestAlignment);
   334 	}
   335 	
   336 GLDEF_C void PrintConfig(TCurrentSoundFormatV02& aConfig,RTest& aTest)
   337 	{
   338 	TBuf<80> buf;
   339 	
   340 	aTest.Printf(_L("**Sound configuration**\r\n"));
   341 	
   342 	// Display the current channel configuration
   343 	aTest.Printf(_L("Channels     : %d\r\n"),aConfig.iChannels);
   344 	
   345 	// Display the current sample rate
   346 	buf.Zero();
   347 	buf.Append(KSndRateConfigTitle);
   348 	if (aConfig.iRate==ESoundRate7350Hz)
   349 		buf.Append(KSndRate7350Hz);
   350 	else if (aConfig.iRate==ESoundRate8000Hz)
   351 		buf.Append(KSndRate8000Hz);
   352 	else if (aConfig.iRate==ESoundRate8820Hz)
   353 		buf.Append(KSndRate8820Hz);
   354 	else if (aConfig.iRate==ESoundRate9600Hz)
   355 		buf.Append(KSndRate9600Hz);
   356 	else if (aConfig.iRate==ESoundRate11025Hz)
   357 		buf.Append(KSndRate11025Hz);
   358 	else if (aConfig.iRate==ESoundRate12000Hz)
   359 		buf.Append(KSndRate12000Hz);
   360 	else if (aConfig.iRate==ESoundRate14700Hz)
   361 		buf.Append(KSndRate14700Hz);
   362 	else if (aConfig.iRate==ESoundRate16000Hz)
   363 		buf.Append(KSndRate16000Hz);
   364 	else if (aConfig.iRate==ESoundRate22050Hz)
   365 		buf.Append(KSndRate22050Hz);
   366 	else if (aConfig.iRate==ESoundRate24000Hz)
   367 		buf.Append(KSndRate24000Hz);
   368 	else if (aConfig.iRate==ESoundRate29400Hz)
   369 		buf.Append(KSndRate29400Hz);
   370 	else if (aConfig.iRate==ESoundRate32000Hz)
   371 		buf.Append(KSndRate32000Hz);
   372 	else if (aConfig.iRate==ESoundRate44100Hz)
   373 		buf.Append(KSndRate44100Hz);
   374 	else if (aConfig.iRate==ESoundRate48000Hz)
   375 		buf.Append(KSndRate48000Hz);
   376 	buf.Append(_L("\r\n"));
   377 	aTest.Printf(buf);
   378 	
   379 	// Display the current encoding
   380 	buf.Zero();
   381 	buf.Append(KSndEncodingConfigTitle);
   382 	if (aConfig.iEncoding==ESoundEncoding8BitPCM)
   383 		buf.Append(KSndEncoding8BitPCM);
   384 	else if (aConfig.iEncoding==ESoundEncoding16BitPCM)
   385 		buf.Append(KSndEncoding16BitPCM);
   386 	else if (aConfig.iEncoding==ESoundEncoding24BitPCM)
   387 		buf.Append(KSndEncoding24BitPCM);
   388 	buf.Append(_L("\r\n"));
   389 	aTest.Printf(buf);
   390 	
   391 	// Display the current data format
   392 	buf.Zero();
   393 	buf.Append(KSndDataFormatConfigTitle);
   394 	if (aConfig.iDataFormat==ESoundDataFormatInterleaved)
   395 		buf.Append(KSndDataFormatInterleaved);
   396 	else if (aConfig.iDataFormat==ESoundDataFormatNonInterleaved)
   397 		buf.Append(KSndDataFormatNonInterleaved);
   398 	buf.Append(_L("\r\n"));
   399 	aTest.Printf(buf);
   400 	}
   401 	
   402 GLDEF_C void PrintBufferConf(TTestSharedChunkBufConfig& aBufConf,RTest& aTest)
   403 	{
   404 	TBuf<80> buf(0);
   405 	
   406 	aTest.Printf(_L("**Buffer configuration**\r\n"));
   407 	
   408 	// Display the buffer configuration
   409 	buf.Format(_L("NumBufs:%d Size:%xH(%d)\r\n"),aBufConf.iNumBuffers,aBufConf.iBufferSizeInBytes,aBufConf.iBufferSizeInBytes);
   410 	aTest.Printf(buf);
   411 	if (aBufConf.iFlags & KScFlagBufOffsetListInUse)
   412 		{
   413 		buf.Format(_L(" Offsets[%08xH,%08xH,%08xH,%08xH]\r\n"),aBufConf.iBufferOffsetList[0],aBufConf.iBufferOffsetList[1],aBufConf.iBufferOffsetList[2],aBufConf.iBufferOffsetList[3]);
   414 		aTest.Printf(buf);
   415 		buf.Format(_L(" Offsets[%08xH,%08xH,%08xH,%08xH]\r\n"),aBufConf.iBufferOffsetList[4],aBufConf.iBufferOffsetList[5],aBufConf.iBufferOffsetList[6],aBufConf.iBufferOffsetList[7]);
   416 		aTest.Printf(buf);
   417 		}
   418 	}