First public contribution.
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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // e32test\multimedia\t_soundutils.cpp
19 @file Utilities used by the shared chunk sound driver test code.
24 #include "t_soundutils.h"
26 const TInt SineAddressBits = 10;
27 const TInt SineAddressFractionBits = 32-SineAddressBits;
28 const TInt SineTableSize = 1<<SineAddressBits;
30 TUint32 ToneIndex = 0;
31 TUint32 ToneIndexStep = 0;
33 TInt32* SineTable = NULL;
35 _LIT(KSndDirCapsTitle, "Direction :");
36 _LIT(KSndDirRecord, " Record");
37 _LIT(KSndDirPlay, " Playback");
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");
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");
64 _LIT(KSndEncodingCapsTitle,"Encodings :");
65 _LIT(KSndEncodingConfigTitle,"Encoding :");
66 _LIT(KSndEncoding8BitPCM," 8bit PCM");
67 _LIT(KSndEncoding16BitPCM," 16bit PCM");
68 _LIT(KSndEncoding24BitPCM," 24bit PCM");
70 _LIT(KSndDataFormatCapsTitle,"Data formats :");
71 _LIT(KSndDataFormatConfigTitle,"Data format :");
72 _LIT(KSndDataFormatInterleaved," Interleaved");
73 _LIT(KSndDataFormatNonInterleaved," Non-interleaved");
76 GLDEF_C TInt BytesPerSample(TCurrentSoundFormatV02& aFormat)
78 TInt bytes = aFormat.iChannels;
79 switch(aFormat.iEncoding)
81 case ESoundEncoding24BitPCM:
84 case ESoundEncoding16BitPCM:
87 case ESoundEncoding8BitPCM:
96 GLDEF_C TInt RateInSamplesPerSecond(TSoundRate aRate)
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);
118 GLDEF_C TInt BytesPerSecond(TCurrentSoundFormatV02& aFormat)
120 return(RateInSamplesPerSecond(aFormat.iRate) * BytesPerSample(aFormat));
123 GLDEF_C TInt ValidBufferSize(TInt aSize, TInt aDeviceMinSize, TCurrentSoundFormatV02& aFormat)
125 // Keep the buffer length a multiple of the number of bytes per sample
126 TInt bytesPerSample=BytesPerSample(aFormat);
127 aSize-=(aSize%bytesPerSample);
129 // Keep the buffer length valid for driver.
131 aSize&=~(aDeviceMinSize-1);
135 GLDEF_C TInt MakeSineTable(TCurrentSoundFormatV02& aPlayFormat)
138 SineTable = (TInt32*)User::Alloc(SineTableSize*sizeof(TInt32));
140 return(KErrNoMemory);
145 switch(aPlayFormat.iEncoding)
147 case ESoundEncoding8BitPCM:
150 case ESoundEncoding16BitPCM:
153 case ESoundEncoding24BitPCM:
157 return(KErrNotSupported);
160 for(i=0; i<SineTableSize; i++)
162 TReal r = KPi*2.0*(TReal)i/(TReal)SineTableSize;
165 Math::Int(SineTable[i],r);
171 GLDEF_C TInt SetToneFrequency(TUint aFrequency,TCurrentSoundFormatV02& aPlayFormat)
173 TInt64 step(MAKE_TINT64(aFrequency,0));
174 step /= RateInSamplesPerSecond(aPlayFormat.iRate);
175 ToneIndexStep = I64LOW(step);
176 return((I64HIGH(step)==0) ? KErrNone : KErrGeneral);
179 GLDEF_C void WriteTone(TDes8& aBuffer,TCurrentSoundFormatV02& aPlayFormat)
183 TUint32 index = ToneIndex;
184 TUint32 step = ToneIndexStep;
185 TInt32* table = SineTable;
187 switch(aPlayFormat.iEncoding)
189 case ESoundEncoding16BitPCM:
191 TInt16* ptr = (TInt16*)aBuffer.Ptr();
192 TInt16* end = ptr+aBuffer.Length()/2;
195 *ptr++ = (TInt16)table[index>>SineAddressFractionBits];
196 if (aPlayFormat.iChannels == 2)
197 *ptr++ = (TInt16)table[index>>SineAddressFractionBits];
202 case ESoundEncoding8BitPCM:
204 TUint8* ptr = (TUint8*)aBuffer.Ptr();
205 TUint8* end = ptr+aBuffer.Length();
208 *ptr++ = (TUint8)table[index>>8];
209 if (aPlayFormat.iChannels == 2)
210 *ptr++ = (TInt8)table[index>>8];
215 case ESoundEncoding24BitPCM:
217 TUint8* ptr = (TUint8*)aBuffer.Ptr();
218 TUint8* end = ptr+aBuffer.Length();
221 *ptr++ = (TUint8)table[index>>24];
222 if (aPlayFormat.iChannels == 2)
223 *ptr++ = (TInt8)table[index>>24];
236 GLDEF_C void Cleanup()
241 GLDEF_C void PrintCaps(TSoundFormatsSupportedV02& aCaps,RTest& aTest)
245 aTest.Printf(_L("**Sound Capabilities**\r\n"));
247 // Display the data transfer direction
249 buf.Append(KSndDirCapsTitle);
250 if (aCaps.iDirection==ESoundDirRecord)
251 buf.Append(KSndDirRecord);
253 buf.Append(KSndDirPlay);
254 buf.Append(_L("\r\n"));
257 // Display the channel configuration
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"));
275 // Display the supported sample rates
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"));
309 // Display the sound encodings supported
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"));
321 // Display the data formats supported
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"));
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);
336 GLDEF_C void PrintConfig(TCurrentSoundFormatV02& aConfig,RTest& aTest)
340 aTest.Printf(_L("**Sound configuration**\r\n"));
342 // Display the current channel configuration
343 aTest.Printf(_L("Channels : %d\r\n"),aConfig.iChannels);
345 // Display the current sample rate
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"));
379 // Display the current encoding
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"));
391 // Display the current data format
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"));
402 GLDEF_C void PrintBufferConf(TTestSharedChunkBufConfig& aBufConf,RTest& aTest)
406 aTest.Printf(_L("**Buffer configuration**\r\n"));
408 // Display the buffer configuration
409 buf.Format(_L("NumBufs:%d Size:%xH(%d)\r\n"),aBufConf.iNumBuffers,aBufConf.iBufferSizeInBytes,aBufConf.iBufferSizeInBytes);
411 if (aBufConf.iFlags & KScFlagBufOffsetListInUse)
413 buf.Format(_L(" Offsets[%08xH,%08xH,%08xH,%08xH]\r\n"),aBufConf.iBufferOffsetList[0],aBufConf.iBufferOffsetList[1],aBufConf.iBufferOffsetList[2],aBufConf.iBufferOffsetList[3]);
415 buf.Format(_L(" Offsets[%08xH,%08xH,%08xH,%08xH]\r\n"),aBufConf.iBufferOffsetList[4],aBufConf.iBufferOffsetList[5],aBufConf.iBufferOffsetList[6],aBufConf.iBufferOffsetList[7]);