os/mm/mmhais/refacladapt/src/tonehwdevice/ToneGenerator.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
// Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     2
// All rights reserved.
sl@0
     3
// This component and the accompanying materials are made available
sl@0
     4
// under the terms of "Eclipse Public License v1.0"
sl@0
     5
// which accompanies this distribution, and is available
sl@0
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     7
//
sl@0
     8
// Initial Contributors:
sl@0
     9
// Nokia Corporation - initial contribution.
sl@0
    10
//
sl@0
    11
// Contributors:
sl@0
    12
//
sl@0
    13
// Description:
sl@0
    14
// This file contains an implementation of the ToneGenerator interface
sl@0
    15
// that converts all tone generation requests in to sampled audio 
sl@0
    16
// data to be played through the normal local sampled audio interface
sl@0
    17
// 
sl@0
    18
//
sl@0
    19
sl@0
    20
#include "ToneGenerator.h"
sl@0
    21
#include <e32math.h>
sl@0
    22
#include <mda/common/resource.h>
sl@0
    23
sl@0
    24
/******************************************************************************
sl@0
    25
*	Tone Generators
sl@0
    26
*
sl@0
    27
*	The following classes are used to generate simple frequency/duration tones,
sl@0
    28
*	DTMF, and SymbianOS tone sequences in a WINS environment.  The below code
sl@0
    29
*	should only be considered for WINS.
sl@0
    30
******************************************************************************/
sl@0
    31
sl@0
    32
// this defines the maximum possible amplitude allowed for TSineGen::SetFrequency()
sl@0
    33
const TInt KMaxAmplitude = 0x8000;
sl@0
    34
sl@0
    35
// default number of samples for trailing silence following a Tone
sl@0
    36
const TInt KDefaultTrailingSilenceSamples = 20;
sl@0
    37
sl@0
    38
//
sl@0
    39
// Sine tone generator
sl@0
    40
//
sl@0
    41
sl@0
    42
const TInt16 TSineGen::SineTable[KMaxSineTable] =
sl@0
    43
	{
sl@0
    44
		 0,   804,  1607,  2410,  3211,  4011,  4807,  5601,
sl@0
    45
	  6392,  7179,  7961,  8739,  9511, 10278, 11038, 11792,
sl@0
    46
	 12539, 13278, 14009, 14732, 15446, 16150, 16845, 17530,
sl@0
    47
	 18204, 18867, 19519, 20159, 20787, 21402, 22004, 22594,
sl@0
    48
	 23169, 23731, 24278, 24811, 25329, 25831, 26318, 26789,
sl@0
    49
	 27244, 27683, 28105, 28510, 28897, 29268, 29621, 29955,
sl@0
    50
	 30272, 30571, 30851, 31113, 31356, 31580, 31785, 31970,
sl@0
    51
	 32137, 32284, 32412, 32520, 32609, 32678, 32727, 32757,
sl@0
    52
	 32767, 32757, 32727, 32678, 32609, 32520, 32412, 32284,
sl@0
    53
	 32137, 31970, 31785, 31580, 31356, 31113, 30851, 30571,
sl@0
    54
	 30272, 29955, 29621, 29268, 28897, 28510, 28105, 27683,
sl@0
    55
	 27244, 26789, 26318, 25831, 25329, 24811, 24278, 23731,
sl@0
    56
	 23169, 22594, 22004, 21402, 20787, 20159, 19519, 18867,
sl@0
    57
	 18204, 17530, 16845, 16150, 15446, 14732, 14009, 13278,
sl@0
    58
	 12539, 11792, 11038, 10278,  9511,  8739,  7961,  7179,
sl@0
    59
	  6392,  5601,  4807,  4011,  3211,  2410,  1607,   804,
sl@0
    60
		 0,  -804, -1607, -2410, -3211, -4011, -4807, -5601,
sl@0
    61
	 -6392, -7179, -7961, -8739, -9511,-10278,-11038,-11792,
sl@0
    62
	-12539,-13278,-14009,-14732,-15446,-16150,-16845,-17530,
sl@0
    63
	-18204,-18867,-19519,-20159,-20787,-21402,-22004,-22594,
sl@0
    64
	-23169,-23731,-24278,-24811,-25329,-25831,-26318,-26789,
sl@0
    65
	-27244,-27683,-28105,-28510,-28897,-29268,-29621,-29955,
sl@0
    66
	-30272,-30571,-30851,-31113,-31356,-31580,-31785,-31970,
sl@0
    67
	-32137,-32284,-32412,-32520,-32609,-32678,-32727,-32757,
sl@0
    68
	-32767,-32757,-32727,-32678,-32609,-32520,-32412,-32284,
sl@0
    69
	-32137,-31970,-31785,-31580,-31356,-31113,-30851,-30571,
sl@0
    70
	-30272,-29955,-29621,-29268,-28897,-28510,-28105,-27683,
sl@0
    71
	-27244,-26789,-26318,-25831,-25329,-24811,-24278,-23731,
sl@0
    72
	-23169,-22594,-22004,-21402,-20787,-20159,-19519,-18867,
sl@0
    73
	-18204,-17530,-16845,-16150,-15446,-14732,-14009,-13278,
sl@0
    74
	-12539,-11792,-11038,-10278, -9511, -8739, -7961, -7179,
sl@0
    75
	 -6392, -5601, -4807, -4011, -3211, -2410, -1607,  -804,
sl@0
    76
	};
sl@0
    77
sl@0
    78
const TInt16 TSineGen::IncTable[KMaxSineTable] =
sl@0
    79
	{
sl@0
    80
			804,  803,  803,  801,  800,  796,  794,
sl@0
    81
	  791,  787,  782,  778,  772,  767,  760,  754,
sl@0
    82
	  747,  739,  731,  723,  714,  704,  695,  685,
sl@0
    83
	  674,  663,  652,  640,  628,  615,  602,  590,
sl@0
    84
	  575,  562,  547,  533,  518,  502,  487,  471,
sl@0
    85
	  455,  439,  422,  405,  387,  371,  353,  334,
sl@0
    86
	  317,  299,  280,  262,  243,  224,  205,  185,
sl@0
    87
	  167,  147,  128,  108,   89,   69,   49,   30,
sl@0
    88
	   10,  -10,  -30,  -49,  -69,  -89, -108, -128,
sl@0
    89
	 -147, -167, -185, -205, -224, -243, -262, -280,
sl@0
    90
	 -299, -317, -334, -353, -371, -387, -405, -422,
sl@0
    91
	 -439, -455, -471, -487, -502, -518, -533, -547,
sl@0
    92
	 -562, -575, -590, -602, -615, -628, -640, -652,
sl@0
    93
	 -663, -674, -685, -695, -704, -714, -723, -731,
sl@0
    94
	 -739, -747, -754, -760, -767, -772, -778, -782,
sl@0
    95
	 -787, -791, -794, -796, -800, -801, -803, -803,
sl@0
    96
	 -804, -804, -803, -803, -801, -800, -796, -794,
sl@0
    97
	 -791, -787, -782, -778, -772, -767, -760, -754,
sl@0
    98
	 -747, -739, -731, -723, -714, -704, -695, -685,
sl@0
    99
	 -674, -663, -652, -640, -628, -615, -602, -590,
sl@0
   100
	 -575, -562, -547, -533, -518, -502, -487, -471,
sl@0
   101
	 -455, -439, -422, -405, -387, -371, -353, -334,
sl@0
   102
	 -317, -299, -280, -262, -243, -224, -205, -185,
sl@0
   103
	 -167, -147, -128, -108,  -89,  -69,  -49,  -30,
sl@0
   104
	  -10,   10,   30,   49,   69,   89,  108,  128,
sl@0
   105
	  147,  167,  185,  205,  224,  243,  262,  280,
sl@0
   106
	  299,  317,  334,  353,  371,  387,  405,  422,
sl@0
   107
	  439,  455,  471,  487,  502,  518,  533,  547,
sl@0
   108
	  562,  575,  590,  602,  615,  628,  640,  652,
sl@0
   109
	  663,  674,  685,  695,  704,  714,  723,  731,
sl@0
   110
	  739,  747,  754,  760,  767,  772,  778,  782,
sl@0
   111
	  787,  791,  794,  796,  800,  801,  803,  803,
sl@0
   112
	  804
sl@0
   113
	};
sl@0
   114
sl@0
   115
void TSineGen::SetFrequency(TInt aFrequency,TInt aAmplitude)
sl@0
   116
//
sl@0
   117
// Given the frequency set iStep.
sl@0
   118
// Reset iPosition to the equivalent of 0 degrees.
sl@0
   119
// In the special case of aFrequency==4KHz set iPosition to 90 degrees.
sl@0
   120
//
sl@0
   121
	{
sl@0
   122
sl@0
   123
	if (aAmplitude>(1<<15))
sl@0
   124
		iAmplitude=(1<<15);
sl@0
   125
	else if (aAmplitude<-(1<<15))
sl@0
   126
		iAmplitude=-(1<<15);
sl@0
   127
	else
sl@0
   128
		iAmplitude=aAmplitude;
sl@0
   129
//
sl@0
   130
// There are 256 entries in the sine table to traverse 360 degrees.
sl@0
   131
// The codec requires samples at a rate of 8000 per second.
sl@0
   132
// Thus for a 1Hz tone the step will be 256/8000 or 4/125.
sl@0
   133
// Now we need need the integer part of the result to end up in
sl@0
   134
// the MSB so we need to multiply by 2^24. This gives the formula
sl@0
   135
// step = (f*4*2^24)/125 or (f*2^26)/125.
sl@0
   136
// Our highest frequency is 4KHz so that the term (f*2^26) exceeds
sl@0
   137
// a 32 bit result by 4000/2^6 (2^6 is the number of significant bits
sl@0
   138
// left after a multiply by 2^26). i.e. 6 bits. We overcome this by
sl@0
   139
// having 6 bits less in the fraction, so the new formula becomes
sl@0
   140
// ((f*2^20)/125)*2^6. This still gives us 20 significant bits in the
sl@0
   141
// fraction.
sl@0
   142
//
sl@0
   143
	
sl@0
   144
	iStep=(((TUint)aFrequency<<20)/125)<<6;
sl@0
   145
	iPosition=(aFrequency==4000 ? 0x40000000 : 0);
sl@0
   146
	}
sl@0
   147
sl@0
   148
TInt TSineGen::NextSample()
sl@0
   149
//
sl@0
   150
// Generate the next sample using linear interpolation
sl@0
   151
//
sl@0
   152
	{
sl@0
   153
	TUint pos=iPosition>>24;
sl@0
   154
	TInt amp=((IncTable[pos]*((iPosition&0x00ffffff)>>20)));
sl@0
   155
	amp>>=4;
sl@0
   156
	amp+=SineTable[pos];
sl@0
   157
	amp=(amp*iAmplitude)>>15;
sl@0
   158
	iPosition+=iStep;
sl@0
   159
	return(amp);
sl@0
   160
	}
sl@0
   161
sl@0
   162
void TSineWave::Generate(TInt16* aDest,TInt aCount)
sl@0
   163
//
sl@0
   164
// Called when more samples need to be generated.
sl@0
   165
//
sl@0
   166
	{
sl@0
   167
	while (aCount--)
sl@0
   168
		{
sl@0
   169
		*aDest++=STATIC_CAST(TInt16,iGen1.NextSample()+iGen2.NextSample());
sl@0
   170
		}
sl@0
   171
	}
sl@0
   172
sl@0
   173
void TSineWave::SetFrequency(TInt aFrequency,TInt aAmplitude)
sl@0
   174
//
sl@0
   175
// Set to generate a single frequency
sl@0
   176
//
sl@0
   177
	{
sl@0
   178
	SetFrequency(aFrequency,aAmplitude,0,0);
sl@0
   179
	}
sl@0
   180
sl@0
   181
void TSineWave::SetFrequency(TInt aFrequency1,TInt aAmplitude1,TInt aFrequency2,TInt aAmplitude2)
sl@0
   182
//
sl@0
   183
// Set to generate two frequencies
sl@0
   184
//
sl@0
   185
	{
sl@0
   186
	iGen1.SetFrequency(aFrequency1,aAmplitude1);
sl@0
   187
	iGen2.SetFrequency(aFrequency2,aAmplitude2);
sl@0
   188
	}
sl@0
   189
sl@0
   190
//
sl@0
   191
// TMdaToneGenerator
sl@0
   192
//
sl@0
   193
sl@0
   194
void TMdaToneGenerator::Configure(TInt aRate, TInt aChannels, TInt aRepeats, TInt aSilence, TInt aRampUp)
sl@0
   195
//
sl@0
   196
// Set up this tone generator to generate data at the desired sample rate
sl@0
   197
// and number of channels (typically mono/stereo)
sl@0
   198
// 
sl@0
   199
	{
sl@0
   200
	iRate = aRate;
sl@0
   201
	iChannels = aChannels;
sl@0
   202
	iSamplesLeft = 0;
sl@0
   203
	iIncompleteVolume = 0;
sl@0
   204
	iRampUpRemainder = 0;
sl@0
   205
	iRampUp = ETrue; // Default ramping to on as it is normally useful
sl@0
   206
	iRampDown = ETrue;
sl@0
   207
	iIncompleteRampDown = EFalse;
sl@0
   208
	iIncompleteRampUp = EFalse;
sl@0
   209
	iRepeats = aRepeats;
sl@0
   210
	iSilenceBetweenRepeats = aSilence;
sl@0
   211
	iRampUpCount = aRampUp;
sl@0
   212
	iRampUpLeft = aRampUp;
sl@0
   213
	iAfterRepeatSilence = EFalse;
sl@0
   214
	}
sl@0
   215
sl@0
   216
LOCAL_C void RampVolume(TInt16* aData,TInt aCount,TInt aStartVol,TInt aEndVol)
sl@0
   217
//
sl@0
   218
// Simple function to ramp down the volume of some samples 
sl@0
   219
// Typically used to prevent "clicking" artifacts at the beginning/end of tones
sl@0
   220
//
sl@0
   221
	{
sl@0
   222
	TInt step = (aEndVol - aStartVol)/aCount;
sl@0
   223
	while (aCount--)
sl@0
   224
		{
sl@0
   225
		TInt data = TInt(*aData) * aStartVol;
sl@0
   226
		*aData++ = TInt16(data>>15);
sl@0
   227
		aStartVol += step;
sl@0
   228
		}
sl@0
   229
	}
sl@0
   230
sl@0
   231
TInt TMdaToneGenerator::FillBuffer(TDes8& aBuffer)
sl@0
   232
//
sl@0
   233
// Fill the supplied buffer with tone data
sl@0
   234
// Sets the buffer length to zero if there is no more data to play
sl@0
   235
// The buffer must have a max length of at least one sample * channels
sl@0
   236
// e.g. 2 bytes mono, 4 bytes stereo
sl@0
   237
//
sl@0
   238
	{
sl@0
   239
	const TInt KRampUpSamples = 50;
sl@0
   240
	const TInt KRampDownSamples = 50;	
sl@0
   241
	
sl@0
   242
	ASSERT(aBuffer.MaxLength()>= (iChannels<<1));
sl@0
   243
	aBuffer.SetMax();
sl@0
   244
sl@0
   245
	TBool silence;
sl@0
   246
	TInt samples = 0; // 
sl@0
   247
	TInt used = 0; // Data used
sl@0
   248
	TInt avail = aBuffer.Length(); // Data filled
sl@0
   249
	TInt count = 0; // Data to be converted
sl@0
   250
	TBool rampUp = EFalse;
sl@0
   251
sl@0
   252
	TMdaPtr8 fill;
sl@0
   253
	fill.Set(aBuffer); // Pointer to data left to be filled
sl@0
   254
sl@0
   255
	// 
sl@0
   256
	// The rest of this function will loop around continually until the buffer
sl@0
   257
	// is filled or there is no more data to play
sl@0
   258
	//
sl@0
   259
sl@0
   260
Restart:
sl@0
   261
	silence = EFalse; // Reset
sl@0
   262
	if (iSamplesLeft == 0)
sl@0
   263
		{
sl@0
   264
		if (iTrailingSilence == 0)
sl@0
   265
			{
sl@0
   266
			TInt error = GetNextTone();
sl@0
   267
			if (error)
sl@0
   268
				return error;
sl@0
   269
			
sl@0
   270
			rampUp = ETrue;
sl@0
   271
			if ((iSamplesLeft==0)&&(iTrailingSilence==0))
sl@0
   272
				{ 
sl@0
   273
				if ((iSilenceBetweenRepeats)&&(!iAfterRepeatSilence))
sl@0
   274
					{
sl@0
   275
					iTrailingSilence = iSilenceBetweenRepeats;
sl@0
   276
					iAfterRepeatSilence = ETrue;
sl@0
   277
					goto Restart;
sl@0
   278
					}
sl@0
   279
				else
sl@0
   280
					{
sl@0
   281
					if ((iRepeats>0)||(iRepeats==KMdaRepeatForever))
sl@0
   282
						{
sl@0
   283
						iAfterRepeatSilence = EFalse;
sl@0
   284
						if (iRepeats>0)
sl@0
   285
							iRepeats--;
sl@0
   286
	
sl@0
   287
						Reset();
sl@0
   288
						goto Restart;
sl@0
   289
						}
sl@0
   290
					}
sl@0
   291
				// No more to play
sl@0
   292
				goto Finished;
sl@0
   293
				}
sl@0
   294
			goto Restart;
sl@0
   295
			}
sl@0
   296
		else
sl@0
   297
			{
sl@0
   298
			silence = ETrue;
sl@0
   299
			samples = iTrailingSilence;
sl@0
   300
			}
sl@0
   301
		}
sl@0
   302
	else
sl@0
   303
		samples = iSamplesLeft;
sl@0
   304
	
sl@0
   305
	count = Min(samples,avail>>1);
sl@0
   306
	fill.SetLength(count<<1);
sl@0
   307
sl@0
   308
	if (!silence)
sl@0
   309
		{ // Generate wave
sl@0
   310
		iSineWave.Generate(REINTERPRET_CAST(TInt16*,&fill[0]),count);
sl@0
   311
		
sl@0
   312
		if (iRampUp)
sl@0
   313
			{ 
sl@0
   314
			// Ramp up volume at beginning of tone
sl@0
   315
			if (rampUp)
sl@0
   316
				{ // Fade in first few samples
sl@0
   317
				if(count < KRampUpSamples)
sl@0
   318
					{
sl@0
   319
					// Partial rampup due to being at the end of the buffer
sl@0
   320
					TInt fadeInLength = Min(KRampUpSamples,(fill.Length()>>1));
sl@0
   321
					iIncompleteVolume = (count*((1<<15)/KRampUpSamples));
sl@0
   322
					RampVolume(CONST_CAST(TInt16*,REINTERPRET_CAST(const TInt16*,(&fill[0]))),fadeInLength,0,iIncompleteVolume);
sl@0
   323
					iRampUpRemainder = fadeInLength;
sl@0
   324
					iIncompleteRampUp = ETrue;
sl@0
   325
					}
sl@0
   326
				else
sl@0
   327
					{
sl@0
   328
					// Normal rampup
sl@0
   329
					TInt fadeInLength = Min(Min(KRampUpSamples,iSamplesLeft),(fill.Length()>>1));
sl@0
   330
					RampVolume(CONST_CAST(TInt16*,REINTERPRET_CAST(const TInt16*,(&fill[0]))),fadeInLength,0,1<<15);	
sl@0
   331
					iIncompleteRampUp = EFalse;
sl@0
   332
					}				
sl@0
   333
				}
sl@0
   334
			else if (iIncompleteRampUp)	
sl@0
   335
				{
sl@0
   336
				// Completing partial rampup at the start of a new buffer
sl@0
   337
				TInt fadeInLength = Min(Min((KRampUpSamples-iRampUpRemainder),iSamplesLeft),(fill.Length()>>1));
sl@0
   338
				RampVolume(CONST_CAST(TInt16*,REINTERPRET_CAST(const TInt16*,(&fill[0]))),fadeInLength,iIncompleteVolume,1<<15);
sl@0
   339
				iIncompleteRampUp = EFalse;
sl@0
   340
				}								
sl@0
   341
			}
sl@0
   342
		if (iRampDown)
sl@0
   343
			{ // Ramp down volume at end of tone
sl@0
   344
			if ((iSamplesLeft-count) < KRampDownSamples)
sl@0
   345
				{ 
sl@0
   346
				if(iSamplesLeft-count == 0)
sl@0
   347
					{
sl@0
   348
					// Fade out last few samples
sl@0
   349
					TInt startVolume = 1<<15;;
sl@0
   350
					TInt fadeOutLength = Min(Min(KRampDownSamples,iSamplesLeft),(fill.Length()>>1));
sl@0
   351
sl@0
   352
					if(iIncompleteRampDown)
sl@0
   353
						{
sl@0
   354
						// Completing partial rampdown at the start of a new buffer
sl@0
   355
						startVolume -= iIncompleteVolume;
sl@0
   356
						iIncompleteRampDown = EFalse;
sl@0
   357
						}
sl@0
   358
sl@0
   359
					RampVolume(CONST_CAST(TInt16*,REINTERPRET_CAST(const TInt16*,(&(fill.Right(fadeOutLength<<1))[0]))),fadeOutLength,startVolume,0);
sl@0
   360
					}					
sl@0
   361
				else if(iSamplesLeft-count > 0)
sl@0
   362
					{
sl@0
   363
					// Partial rampdown due to being at the end of the buffer
sl@0
   364
					TInt rampDifference = (KRampDownSamples-(iSamplesLeft-count));
sl@0
   365
					TInt fadeOutLength = Min(Min(rampDifference,iSamplesLeft),(fill.Length()>>1));
sl@0
   366
					iIncompleteVolume = ((rampDifference*(1<<15))/KRampDownSamples);
sl@0
   367
sl@0
   368
					RampVolume(CONST_CAST(TInt16*,REINTERPRET_CAST(const TInt16*,(&(fill.Right(fadeOutLength<<1))[0]))),fadeOutLength,1<<15,(1<<15)-iIncompleteVolume);
sl@0
   369
sl@0
   370
					iIncompleteRampDown = ETrue;
sl@0
   371
					}  					
sl@0
   372
				}
sl@0
   373
			}					
sl@0
   374
		iSamplesLeft -= count;
sl@0
   375
		}
sl@0
   376
	else
sl@0
   377
		{ // Generate silence
sl@0
   378
		fill.FillZ(count<<1);
sl@0
   379
		iTrailingSilence -= count;
sl@0
   380
		}
sl@0
   381
sl@0
   382
	used += count<<1;
sl@0
   383
	avail -= count<<1;
sl@0
   384
	fill.Shift(count<<1);	
sl@0
   385
	
sl@0
   386
	if (avail>(iChannels<<1))
sl@0
   387
		goto Restart;
sl@0
   388
sl@0
   389
Finished:
sl@0
   390
sl@0
   391
	aBuffer.SetLength(used);
sl@0
   392
sl@0
   393
	// Do any ramp up that is required
sl@0
   394
	if (iRampUpLeft>0)
sl@0
   395
		{
sl@0
   396
		TInt words = iRampUpLeft * iChannels;
sl@0
   397
		words = Min(words,used>>1);
sl@0
   398
		if (words>0) // In case buffer has zero length...
sl@0
   399
			{
sl@0
   400
			TInt left = iRampUpLeft * iChannels;
sl@0
   401
			TInt rampup = iRampUpCount * iChannels;
sl@0
   402
			iRampUpLeft -= words/iChannels;
sl@0
   403
			TInt16* sample = REINTERPRET_CAST(TInt16*,&aBuffer[0]);
sl@0
   404
			while (words--)
sl@0
   405
				{
sl@0
   406
				*sample++ = STATIC_CAST(TInt16,(TInt32(*sample)*(rampup-(left--)))/rampup);
sl@0
   407
				}
sl@0
   408
			}
sl@0
   409
		}
sl@0
   410
sl@0
   411
	return KErrNone;
sl@0
   412
	}
sl@0
   413
sl@0
   414
TInt TMdaToneGenerator::DurationToSamples(const TTimeIntervalMicroSeconds& aDuration)
sl@0
   415
//
sl@0
   416
// Convert the given duration to a sample count using the current settings
sl@0
   417
//
sl@0
   418
	{
sl@0
   419
	const TInt64 KTInt64OneMilion = 1000000;
sl@0
   420
sl@0
   421
	// Calculate duration as samples
sl@0
   422
	TInt64 microSeconds(aDuration.Int64());  // MSVC doesn't like "aDuration.Int64()" in line below
sl@0
   423
	TInt64 dur = ((TInt64(iRate) * TInt64(iChannels) * microSeconds) / KTInt64OneMilion);
sl@0
   424
	if (I64HIGH(dur)>0)
sl@0
   425
		return KMaxTInt; // Ridiculous!
sl@0
   426
	else
sl@0
   427
		return I64LOW(dur);
sl@0
   428
	}
sl@0
   429
sl@0
   430
//
sl@0
   431
// TMdaSimpleToneGenerator
sl@0
   432
//
sl@0
   433
sl@0
   434
void TMdaSimpleToneGenerator::Reset()
sl@0
   435
	{
sl@0
   436
	iPlayed = EFalse;
sl@0
   437
	}
sl@0
   438
sl@0
   439
void TMdaSimpleToneGenerator::SetFrequencyAndDuration(TInt aFrequency, const TTimeIntervalMicroSeconds& aDuration)
sl@0
   440
//
sl@0
   441
// Store the frequency and duration of the specified sine tone
sl@0
   442
//
sl@0
   443
	{
sl@0
   444
	iFrequency = aFrequency;
sl@0
   445
	iDuration = aDuration;
sl@0
   446
	iPlayed = EFalse;
sl@0
   447
	}
sl@0
   448
sl@0
   449
TInt TMdaSimpleToneGenerator::GetNextTone()
sl@0
   450
//
sl@0
   451
// Simple implementation - just sets the supplied frequency and duration
sl@0
   452
//
sl@0
   453
	{
sl@0
   454
	// This class only plays one tone for the specified duration
sl@0
   455
	if (!iPlayed)
sl@0
   456
		{
sl@0
   457
		iSamplesLeft = I64LOW((iDuration.Int64() * TInt64(iRate))/1000000);
sl@0
   458
		iSineWave.SetFrequency(iFrequency,1<<14);
sl@0
   459
		iPlayed = ETrue;
sl@0
   460
		iTrailingSilence = 20; // Just to stop clicking
sl@0
   461
		}
sl@0
   462
	return KErrNone;
sl@0
   463
	}
sl@0
   464
sl@0
   465
//
sl@0
   466
// TMdaDualToneGenerator
sl@0
   467
//
sl@0
   468
sl@0
   469
void TMdaDualToneGenerator::Reset()
sl@0
   470
	{
sl@0
   471
	iPlayed = EFalse;
sl@0
   472
	}
sl@0
   473
sl@0
   474
void TMdaDualToneGenerator::SetFrequencyAndDuration(TInt aFrequencyOne, TInt aFrequencyTwo, const TTimeIntervalMicroSeconds& aDuration)
sl@0
   475
	{
sl@0
   476
	// Store the frequencies and duration of the specified dual tone
sl@0
   477
	iFrequencyOne = aFrequencyOne;
sl@0
   478
	iFrequencyTwo = aFrequencyTwo;
sl@0
   479
	iDuration = aDuration;
sl@0
   480
	iPlayed = EFalse;
sl@0
   481
	}
sl@0
   482
sl@0
   483
// 
sl@0
   484
// This is called by TMdaToneGenerator::FillBuffer() 
sl@0
   485
// to calculate the number of samples (iSamplesLeft) that will be needed 
sl@0
   486
// for the tone to be played and to initialize the sine wave generator.
sl@0
   487
// If the tone has already been played, then leaves iSamplesLeft 
sl@0
   488
// unmodified (should be zero) to indicate that it has finished.
sl@0
   489
//
sl@0
   490
TInt TMdaDualToneGenerator::GetNextTone()
sl@0
   491
	{
sl@0
   492
	// This class only plays one tone for the specified duration
sl@0
   493
	if (!iPlayed)
sl@0
   494
		{
sl@0
   495
		iSamplesLeft = I64LOW((iDuration.Int64() * TInt64(iRate))/KOneMillionMicroSeconds);
sl@0
   496
		iSineWave.SetFrequency(iFrequencyOne, KMaxAmplitude/2, iFrequencyTwo, KMaxAmplitude/2);
sl@0
   497
		iPlayed = ETrue;
sl@0
   498
		iTrailingSilence = KDefaultTrailingSilenceSamples; // Just to stop clicking
sl@0
   499
		}
sl@0
   500
	return KErrNone;
sl@0
   501
	}
sl@0
   502
//
sl@0
   503
// TMdaDTMFGenerator
sl@0
   504
//
sl@0
   505
sl@0
   506
const TInt KRecalculateToneLengths = KMinTInt;
sl@0
   507
sl@0
   508
void TMdaDTMFGenerator::Reset()
sl@0
   509
	{
sl@0
   510
	iChar = 0;
sl@0
   511
	}
sl@0
   512
sl@0
   513
void TMdaDTMFGenerator::SetToneDurations(const TTimeIntervalMicroSeconds32 aOn,
sl@0
   514
							const TTimeIntervalMicroSeconds32 aOff,
sl@0
   515
							const TTimeIntervalMicroSeconds32 aPause)
sl@0
   516
//
sl@0
   517
// Setup the DTMF tone durations
sl@0
   518
// aOn can be == -1 indicating should play first tone indefinately
sl@0
   519
//
sl@0
   520
	{
sl@0
   521
	ASSERT(aOn.Int() >=-1);
sl@0
   522
	ASSERT(aOff.Int()>=0);
sl@0
   523
	ASSERT(aPause.Int()>=0);
sl@0
   524
sl@0
   525
	iOn = aOn;
sl@0
   526
	iOff = aOff;
sl@0
   527
	iPause = aPause;
sl@0
   528
sl@0
   529
	iOnSamples = KRecalculateToneLengths; // Must recalculate these later
sl@0
   530
	}
sl@0
   531
sl@0
   532
void TMdaDTMFGenerator::SetString(const TDesC& aDTMFString)
sl@0
   533
//
sl@0
   534
// Store the DTMF string to be played
sl@0
   535
// No need to validate it as it will already have been checked 
sl@0
   536
//
sl@0
   537
	{
sl@0
   538
	iChar = 0;
sl@0
   539
	iDTMFString = &aDTMFString;
sl@0
   540
	}
sl@0
   541
sl@0
   542
const TUint8 KDtmfVolumeTable[4][4]=
sl@0
   543
//
sl@0
   544
// Relative strengths to assign to different DTMF tones
sl@0
   545
//
sl@0
   546
// This is only important if DTMFs are being played through a speaker
sl@0
   547
// and need to be machine-recognisable. This table compensates for frequency
sl@0
   548
// drop-off in the speaker and can boost the relative volume of some 
sl@0
   549
// frequencies so they are still within tolerance.
sl@0
   550
// 
sl@0
   551
// The values normally need to be determined using a frequency analyser on 
sl@0
   552
// the hardware
sl@0
   553
// 
sl@0
   554
// Each column == same low frequency (697, 770, 852, 941 Hz)
sl@0
   555
// Each row == same high frequency (1209, 1336, 1477, 1633 Hz)
sl@0
   556
//
sl@0
   557
// The value are interpreted as ratios:
sl@0
   558
//		0  == 100% low
sl@0
   559
//		7f == 50% low, 50% high
sl@0
   560
//		ff == 100% high
sl@0
   561
//
sl@0
   562
	{
sl@0
   563
	{38,27,29,37},
sl@0
   564
	{46,36,36,46},
sl@0
   565
	{62,47,49,58},
sl@0
   566
	{70,56,60,68}
sl@0
   567
	};
sl@0
   568
sl@0
   569
const TUint8 KDtmfTone697=0x0;
sl@0
   570
const TUint8 KDtmfTone770=0x1;
sl@0
   571
const TUint8 KDtmfTone852=0x2;
sl@0
   572
const TUint8 KDtmfTone941=0x3;
sl@0
   573
sl@0
   574
const TUint8 KDtmfTone1209=0x00;
sl@0
   575
const TUint8 KDtmfTone1336=0x10;
sl@0
   576
const TUint8 KDtmfTone1477=0x20;
sl@0
   577
const TUint8 KDtmfTone1633=0x30;
sl@0
   578
sl@0
   579
const TUint8 KDtmfToneTable[16]=
sl@0
   580
	{
sl@0
   581
	KDtmfTone941|KDtmfTone1336,//0
sl@0
   582
	KDtmfTone697|KDtmfTone1209,//1
sl@0
   583
	KDtmfTone697|KDtmfTone1336,//2
sl@0
   584
	KDtmfTone697|KDtmfTone1477,//3
sl@0
   585
	KDtmfTone770|KDtmfTone1209,//4
sl@0
   586
	KDtmfTone770|KDtmfTone1336,//5
sl@0
   587
	KDtmfTone770|KDtmfTone1477,//6
sl@0
   588
	KDtmfTone852|KDtmfTone1209,//7
sl@0
   589
	KDtmfTone852|KDtmfTone1336,//8
sl@0
   590
	KDtmfTone852|KDtmfTone1477,//9
sl@0
   591
sl@0
   592
	KDtmfTone697|KDtmfTone1633,//A
sl@0
   593
	KDtmfTone770|KDtmfTone1633,//B
sl@0
   594
	KDtmfTone852|KDtmfTone1633,//C
sl@0
   595
	KDtmfTone941|KDtmfTone1633,//D
sl@0
   596
	KDtmfTone941|KDtmfTone1209,//E or *
sl@0
   597
	KDtmfTone941|KDtmfTone1477,//F or #
sl@0
   598
	};
sl@0
   599
sl@0
   600
TInt TMdaDTMFGenerator::GetNextTone()
sl@0
   601
//
sl@0
   602
// Setup frequency/duration/silence settings for next DTMF tone
sl@0
   603
// Supported characters are 0-9 A-F * # , and any kind of white space
sl@0
   604
//
sl@0
   605
	{
sl@0
   606
	TBool onlyPlayFirstTone = EFalse;
sl@0
   607
sl@0
   608
	if (iOnSamples == KRecalculateToneLengths)
sl@0
   609
		{
sl@0
   610
		// Must recalculate tone durations as samples
sl@0
   611
sl@0
   612
		// Handle special case where tone on duration negative
sl@0
   613
		// - meaning play first character indefinately
sl@0
   614
		if (iOn.Int()>=0)
sl@0
   615
			iOnSamples = DurationToSamples(TInt64(iOn.Int()));
sl@0
   616
		else 
sl@0
   617
			{
sl@0
   618
			onlyPlayFirstTone = ETrue;
sl@0
   619
			iOnSamples = -1; 
sl@0
   620
			}
sl@0
   621
sl@0
   622
		iOffSamples = DurationToSamples(TInt64(iOff.Int()));
sl@0
   623
		iPauseSamples = DurationToSamples(TInt64(iPause.Int()));
sl@0
   624
		}
sl@0
   625
sl@0
   626
	ASSERT(iDTMFString);
sl@0
   627
sl@0
   628
	if (iChar==iDTMFString->Length())
sl@0
   629
		return KErrNone; // Finished. Nothing to do
sl@0
   630
sl@0
   631
	TInt highFrequency = 0;
sl@0
   632
	TInt highVolume = 0;
sl@0
   633
	TInt lowFrequency = 0; 
sl@0
   634
	TInt lowVolume =0;
sl@0
   635
sl@0
   636
Retry:
sl@0
   637
   	TChar c((*iDTMFString)[iChar++]);
sl@0
   638
   	if ((TUint)c=='#' || (TUint)c=='*' || c.IsHexDigit())
sl@0
   639
   		{
sl@0
   640
    	TInt tableIndex;
sl@0
   641
		switch ((TUint)c)
sl@0
   642
			{
sl@0
   643
		case '*':
sl@0
   644
			tableIndex=14;
sl@0
   645
			break;
sl@0
   646
		case '#':
sl@0
   647
			tableIndex=15;
sl@0
   648
			break;
sl@0
   649
		default:
sl@0
   650
			if (c.IsDigit())
sl@0
   651
    			tableIndex=(TUint)c-'0';
sl@0
   652
			else //letter
sl@0
   653
		   		{
sl@0
   654
				c.UpperCase();
sl@0
   655
    			tableIndex=(TUint)c-'A'+10;
sl@0
   656
				}
sl@0
   657
			}
sl@0
   658
		TInt high=KDtmfToneTable[tableIndex]&0xf0;
sl@0
   659
		TInt low=KDtmfToneTable[tableIndex]&0x0f;
sl@0
   660
		switch(high)
sl@0
   661
			{
sl@0
   662
		case KDtmfTone1209:
sl@0
   663
			highFrequency=1209;
sl@0
   664
			break;
sl@0
   665
		case KDtmfTone1336:
sl@0
   666
			highFrequency=1336;
sl@0
   667
			break;
sl@0
   668
		case KDtmfTone1477:
sl@0
   669
			highFrequency=1477;
sl@0
   670
			break;
sl@0
   671
		default://KDtmfTone1633:
sl@0
   672
			highFrequency=1633;
sl@0
   673
			break;
sl@0
   674
			}
sl@0
   675
		switch(low)
sl@0
   676
			{
sl@0
   677
		case KDtmfTone697:
sl@0
   678
			lowFrequency=697;
sl@0
   679
			break;
sl@0
   680
		case KDtmfTone770:
sl@0
   681
			lowFrequency=770;
sl@0
   682
			break;
sl@0
   683
		case KDtmfTone852:
sl@0
   684
			lowFrequency=852;
sl@0
   685
			break;
sl@0
   686
		default://KDtmfTone941:
sl@0
   687
			lowFrequency=941;
sl@0
   688
			break;
sl@0
   689
			}
sl@0
   690
		high>>=4;
sl@0
   691
		const TUint8* dtmfVolumes=&KDtmfVolumeTable[0][0];
sl@0
   692
		TInt volume=dtmfVolumes[((low)<<2)+(high)]<<7;
sl@0
   693
		highVolume = volume;
sl@0
   694
		lowVolume = (1<<15)-volume;
sl@0
   695
sl@0
   696
		iTrailingSilence = iOffSamples;
sl@0
   697
		iSamplesLeft = iOnSamples;
sl@0
   698
		}
sl@0
   699
   	else if ((TUint)c==',')
sl@0
   700
		{
sl@0
   701
  		iTrailingSilence = iPauseSamples;
sl@0
   702
 		iSamplesLeft = 0;
sl@0
   703
    	}
sl@0
   704
	else if (c.IsSpace())
sl@0
   705
		{
sl@0
   706
		if (iChar < iDTMFString->Length())
sl@0
   707
			goto Retry;
sl@0
   708
		}
sl@0
   709
	else
sl@0
   710
		return KErrCorrupt;
sl@0
   711
sl@0
   712
	if (iOnSamples < 0) // Play only first character for ever
sl@0
   713
		{
sl@0
   714
		iTrailingSilence = 0;
sl@0
   715
		iSamplesLeft = iRate * iChannels; // One second of samples
sl@0
   716
		iChar = 0; // Reset so this character is played again next time
sl@0
   717
		iRampDown = EFalse;
sl@0
   718
		if (!onlyPlayFirstTone)
sl@0
   719
			{
sl@0
   720
			iRampUp = EFalse;
sl@0
   721
			// This is not the first time around so we should not
sl@0
   722
			// reset the tone generator - it will already have the
sl@0
   723
			// correct settings and setting them again would cause
sl@0
   724
			// an audible discontinuity
sl@0
   725
			return KErrNone; 
sl@0
   726
			}
sl@0
   727
		}
sl@0
   728
sl@0
   729
	iSineWave.SetFrequency(highFrequency,highVolume,lowFrequency,lowVolume);
sl@0
   730
	return KErrNone;
sl@0
   731
	}
sl@0
   732
sl@0
   733
//
sl@0
   734
// TMdaSequenceGenerator
sl@0
   735
//
sl@0
   736
sl@0
   737
//
sl@0
   738
// Sequence constants
sl@0
   739
// 
sl@0
   740
sl@0
   741
//const TInt KMaxFixedSequenceStack=KMaxSequenceStack;//Max nesting level of FixedSequences * 2 
sl@0
   742
#ifdef _DEBUG
sl@0
   743
const TInt16 KFixedSequenceSignatureOne='S'+('Q'<<8); 
sl@0
   744
const TInt16 KFixedSequenceSignatureTwo='N'+('C'<<8);
sl@0
   745
#endif // _DEBUG
sl@0
   746
sl@0
   747
const TInt KFixedSequenceFunctionReturn=-1;
sl@0
   748
const TInt KFixedSequenceFunctionStartLoop=-2;
sl@0
   749
const TInt KFixedSequenceFunctionEndLoop=-3;
sl@0
   750
sl@0
   751
void TMdaSequenceGenerator::Reset()
sl@0
   752
	{
sl@0
   753
	iInstructionPtr = REINTERPRET_CAST(const TInt16*,&((*iSequenceData)[0]));
sl@0
   754
	iInstructionPtr += 2; // Skip signature
sl@0
   755
	iStackIndex = 0;
sl@0
   756
	}
sl@0
   757
	
sl@0
   758
void TMdaSequenceGenerator::SetSequenceData(const TDesC8& aSequenceData)
sl@0
   759
//
sl@0
   760
// Store the sequence data to be played
sl@0
   761
// No need to validate it as it will already have been checked 
sl@0
   762
//
sl@0
   763
	{
sl@0
   764
	iSequenceData = &aSequenceData;
sl@0
   765
	iInstructionPtr = REINTERPRET_CAST(const TInt16*,&aSequenceData[0]);
sl@0
   766
	iLastInstruction = iInstructionPtr + (iSequenceData->Length()>>1) - 1;
sl@0
   767
sl@0
   768
	// These are asserts because this should not be called if signature not present
sl@0
   769
	ASSERT(*iInstructionPtr == KFixedSequenceSignatureOne);
sl@0
   770
	ASSERT(*(iInstructionPtr+1) == KFixedSequenceSignatureTwo);
sl@0
   771
sl@0
   772
	iInstructionPtr += 2; // Skip signature
sl@0
   773
sl@0
   774
	iStackIndex = 0;
sl@0
   775
	}
sl@0
   776
sl@0
   777
TInt TMdaSequenceGenerator::GetNextTone()
sl@0
   778
//
sl@0
   779
//
sl@0
   780
	{
sl@0
   781
	ASSERT(iInstructionPtr); // Sanity check
sl@0
   782
sl@0
   783
	TInt ret = KRequestPending;
sl@0
   784
	while (ret == KRequestPending)
sl@0
   785
		{
sl@0
   786
		if (iInstructionPtr > iLastInstruction)
sl@0
   787
			ret = KErrCorrupt;
sl@0
   788
		else if (*iInstructionPtr<=0)
sl@0
   789
	   		{
sl@0
   790
	   		switch (*iInstructionPtr)
sl@0
   791
	   			{
sl@0
   792
	   		case KFixedSequenceFunctionReturn: // End of sequence
sl@0
   793
				ret = KErrNone;
sl@0
   794
				break;
sl@0
   795
sl@0
   796
	   		case KFixedSequenceFunctionStartLoop:
sl@0
   797
				if (iStackIndex>2) // Validate - can only nest twice
sl@0
   798
					ret = KErrCorrupt;
sl@0
   799
				else if ((iInstructionPtr+2) > iLastInstruction)
sl@0
   800
					ret = KErrCorrupt; // Don't run off end of sequence
sl@0
   801
				else
sl@0
   802
					{
sl@0
   803
		   			iStack[iStackIndex++]=(TInt)(iInstructionPtr+2);
sl@0
   804
		   			iStack[iStackIndex++]=(TInt)*(iInstructionPtr+1);
sl@0
   805
	   				iInstructionPtr+=2;
sl@0
   806
					}
sl@0
   807
	   			break;
sl@0
   808
sl@0
   809
	   		case KFixedSequenceFunctionEndLoop:
sl@0
   810
				if (iStackIndex==0) // Validate - must already be nested
sl@0
   811
					ret = KErrCorrupt;
sl@0
   812
				else
sl@0
   813
					{
sl@0
   814
		   			if ((--iStack[iStackIndex-1])!=0)
sl@0
   815
		   				iInstructionPtr=(TInt16*)iStack[iStackIndex-2];
sl@0
   816
		   			else
sl@0
   817
		   				{
sl@0
   818
		   				iStackIndex-=2;
sl@0
   819
		   				iInstructionPtr++;
sl@0
   820
		   				}
sl@0
   821
					}
sl@0
   822
	   			break;
sl@0
   823
sl@0
   824
	   		default: // Bad sequence
sl@0
   825
				ret = KErrCorrupt;
sl@0
   826
	   			}
sl@0
   827
			}
sl@0
   828
		else
sl@0
   829
			{
sl@0
   830
			if ((iInstructionPtr+5) > iLastInstruction)
sl@0
   831
				ret = KErrCorrupt; // Don't run off end of sequence
sl@0
   832
			else
sl@0
   833
				{
sl@0
   834
				iSamplesLeft = *iInstructionPtr++;
sl@0
   835
				TInt freqOne = *iInstructionPtr++;
sl@0
   836
				TInt volOne  = *iInstructionPtr++;
sl@0
   837
				TInt freqTwo = *iInstructionPtr++;
sl@0
   838
				TInt volTwo  = *iInstructionPtr++;
sl@0
   839
sl@0
   840
				if ((volOne> 1<<15)||(volTwo > 1<<15))
sl@0
   841
					ret = KErrCorrupt;
sl@0
   842
				else	
sl@0
   843
					{
sl@0
   844
					iSineWave.SetFrequency(freqOne,volOne,freqTwo,volTwo);
sl@0
   845
					ret = KErrNone;
sl@0
   846
					}
sl@0
   847
				}
sl@0
   848
			}
sl@0
   849
		}
sl@0
   850
	return ret;
sl@0
   851
	}
sl@0
   852
sl@0
   853
// ---------------------------------
sl@0
   854
// Code to generate sine table files used by tone generator
sl@0
   855
// Optionally called from InitL()
sl@0
   856
// #define GENERATE_SINE_TABLES 1
sl@0
   857
#ifdef GENERATE_SINE_TABLES
sl@0
   858
LOCAL_C GenerateSineTableL()
sl@0
   859
	{
sl@0
   860
	_LIT(KSineFile,"sine.txt");
sl@0
   861
	_LIT(KSineIncFile,"sineinc.txt");
sl@0
   862
sl@0
   863
	RFile file;
sl@0
   864
	file.Replace(MdaManager::Fs(),KSineFile,EFileWrite);
sl@0
   865
	CleanupClosePushL(file);
sl@0
   866
sl@0
   867
	RFile file2;
sl@0
   868
	file2.Replace(MdaManager::Fs(),KSineIncFile,EFileWrite);
sl@0
   869
	CleanupClosePushL(file2);
sl@0
   870
sl@0
   871
	const TReal pi=3.141592653589;
sl@0
   872
	const TReal twopi=pi*2;
sl@0
   873
	const TReal samples = 256.0;
sl@0
   874
	const TReal step = twopi/samples;
sl@0
   875
sl@0
   876
	TBuf8<128> sinebuffer;
sl@0
   877
	TBuf8<128> incbuffer;
sl@0
   878
	TReal res;
sl@0
   879
	TInt first=0;
sl@0
   880
	TInt last=KMaxTInt;
sl@0
   881
	TInt current;
sl@0
   882
	_LIT8(KFormat,"%6d,");
sl@0
   883
	_LIT8(KNewLine,"\n");
sl@0
   884
sl@0
   885
	for(TReal angle=0.0;angle<=(twopi-step);) // Copes with rounding errors
sl@0
   886
		{
sl@0
   887
		sinebuffer.Zero();
sl@0
   888
		incbuffer.Zero();
sl@0
   889
		for (int i=0;i<8;i++)
sl@0
   890
			{
sl@0
   891
			User::LeaveIfError(Math::Sin(res,angle));
sl@0
   892
			current = TInt(KMaxTInt16*res);
sl@0
   893
			sinebuffer.AppendFormat(KFormat,current);
sl@0
   894
			if (last != KMaxTInt)
sl@0
   895
				incbuffer.AppendFormat(KFormat,current-last);
sl@0
   896
			else
sl@0
   897
				first = current;
sl@0
   898
			last = current;
sl@0
   899
			angle += step;
sl@0
   900
			}
sl@0
   901
		sinebuffer.Append(KNewLine);
sl@0
   902
		incbuffer.Append(KNewLine);
sl@0
   903
		file.Write(sinebuffer);
sl@0
   904
		file2.Write(incbuffer);
sl@0
   905
		}
sl@0
   906
sl@0
   907
	// Write fine difference to incbuffer - differnece between first and last
sl@0
   908
	incbuffer.Zero();
sl@0
   909
	incbuffer.AppendFormat(KFormat,first-last);
sl@0
   910
	incbuffer.Append(KNewLine);
sl@0
   911
	file2.Write(incbuffer);
sl@0
   912
sl@0
   913
	CleanupStack::PopAndDestroy(2);
sl@0
   914
	}
sl@0
   915
#endif
sl@0
   916
//-------------------------------