mmhais/refacladapt/src/tonehwdevice/ToneGenerator.h
changeset 0 40261b775718
equal deleted inserted replaced
-1:000000000000 0:40261b775718
       
     1 // Copyright (c) 1997-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 "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 #ifndef __TONEGENERATOR_H__
       
    17 #define __TONEGENERATOR_H__
       
    18 
       
    19 #include <e32base.h>
       
    20 
       
    21 //
       
    22 // Sine tone generator
       
    23 // 
       
    24 
       
    25 const TInt KMaxSineTable = 256;
       
    26 const TUint KToneBufferSize = 8192;
       
    27 
       
    28 // one second in microseconds
       
    29 const TInt KOneMillionMicroSeconds = 1000000;
       
    30 
       
    31 
       
    32 class TSineGen
       
    33 /**
       
    34 *@internalTechnology
       
    35 */
       
    36 	{
       
    37 public:
       
    38 	void SetFrequency(TInt aFrequency,TInt aAmplitude);
       
    39 	TInt NextSample();
       
    40 private:
       
    41 	TUint iPosition;
       
    42 	TUint iStep;
       
    43 	TInt iAmplitude;
       
    44 	static const TInt16 SineTable[KMaxSineTable];
       
    45 	static const TInt16 IncTable[KMaxSineTable];
       
    46 	};
       
    47 
       
    48 class TSineWave
       
    49 /**
       
    50 *@internalTechnology
       
    51 */
       
    52 	{
       
    53 public:
       
    54 	void Generate(TInt16* aDest,TInt aCount);
       
    55 	void SetFrequency(TInt aFrequency,TInt aAmplitude);
       
    56 	void SetFrequency(TInt aFrequency1,TInt aAmplitude1,TInt aFrequency2,TInt aAmplitude2);
       
    57 private:
       
    58 	TSineGen iGen1;
       
    59 	TSineGen iGen2;
       
    60 	};
       
    61 
       
    62 //
       
    63 // Tone synthesis interface
       
    64 // Defines the abstract interface for tone synthesis
       
    65 // Capable of filling buffers with audio data
       
    66 //
       
    67 
       
    68 class MMdaToneSynthesis
       
    69 /**
       
    70 *@internalTechnology
       
    71 */
       
    72 	{
       
    73 public:
       
    74 	// Allocate necessary resources for this kind of synthesis
       
    75 	virtual void Configure(TInt aRate, TInt aChannels, TInt aRepeats, TInt aSilence, TInt aRampUp)=0;
       
    76 	// Begin generating data from start again
       
    77 	virtual void Reset()=0;
       
    78 	// Fill supplied buffer with next block of 16bit PCM audio data
       
    79 	virtual TInt FillBuffer(TDes8& aBuffer)=0;
       
    80 	};
       
    81 
       
    82 //
       
    83 // Tone generator base class
       
    84 //
       
    85 
       
    86 class TMdaToneGenerator : public MMdaToneSynthesis
       
    87 	{
       
    88 public:
       
    89 	virtual void Configure(TInt aRate, TInt aChannels, TInt aRepeats, TInt aSilence, TInt aRampUp);
       
    90 	virtual TInt FillBuffer(TDes8& aBuffer);
       
    91 protected:
       
    92 	virtual TInt GetNextTone()=0;
       
    93 	//
       
    94 	TInt DurationToSamples(const TTimeIntervalMicroSeconds& aDuration);
       
    95 protected:
       
    96 	TSineWave iSineWave;
       
    97 	TInt iRate;
       
    98 	TInt iChannels;
       
    99 	TInt iSamplesLeft;
       
   100 	TInt iTrailingSilence;
       
   101 	TBool iRampUp;
       
   102 	TBool iRampDown;
       
   103 	TBool iIncompleteRampDown;
       
   104 	TBool iIncompleteRampUp;
       
   105 	TInt iIncompleteVolume;
       
   106 	TInt iRampUpRemainder;
       
   107 	TInt iRepeats;
       
   108 	TInt iSilenceBetweenRepeats;
       
   109 	TBool iAfterRepeatSilence;
       
   110 	TInt iRampUpCount;
       
   111 	TInt iRampUpLeft;
       
   112 	};
       
   113 
       
   114 //
       
   115 // Simple tone synthesis
       
   116 //
       
   117 
       
   118 class TMdaSimpleToneGenerator : public TMdaToneGenerator
       
   119 /**
       
   120 *@internalTechnology
       
   121 */
       
   122 	{
       
   123 public:
       
   124 	void SetFrequencyAndDuration(TInt aFrequency, const TTimeIntervalMicroSeconds& aDuration);
       
   125 	virtual void Reset();
       
   126 	virtual TInt GetNextTone();
       
   127 private:
       
   128 	TTimeIntervalMicroSeconds iDuration;
       
   129 	TInt iFrequency;
       
   130 	TBool iPlayed;
       
   131 	};
       
   132 
       
   133 
       
   134 /**
       
   135  * Dual tone synthesis
       
   136  * Generates a tone consisting of two sine waves of different 
       
   137  * frequencies summed together.
       
   138  * 
       
   139  * @internalTechnology
       
   140  */
       
   141 class TMdaDualToneGenerator : public TMdaToneGenerator
       
   142 	{
       
   143 public:
       
   144 	void SetFrequencyAndDuration(TInt aFrequencyOne, TInt aFrequencyTwo, const TTimeIntervalMicroSeconds& aDuration);
       
   145 	virtual void Reset();
       
   146 	virtual TInt GetNextTone();
       
   147 private:
       
   148 	TTimeIntervalMicroSeconds iDuration;
       
   149 	TInt iFrequencyOne;
       
   150 	TInt iFrequencyTwo;
       
   151 	TBool iPlayed;
       
   152 	};
       
   153 
       
   154 //
       
   155 // DTMF tone synthesis
       
   156 //
       
   157 
       
   158 class TMdaDTMFGenerator : public TMdaToneGenerator
       
   159 /**
       
   160 *@internalTechnology
       
   161 */
       
   162 	{
       
   163 public:
       
   164 	virtual void Reset();
       
   165 	void SetToneDurations(	const TTimeIntervalMicroSeconds32 aOn,
       
   166 							const TTimeIntervalMicroSeconds32 aOff,
       
   167 							const TTimeIntervalMicroSeconds32 aPause);
       
   168 	void SetString(const TDesC& aDTMFString); 
       
   169 private:
       
   170 	virtual TInt GetNextTone();
       
   171 private:
       
   172 	const TDesC* iDTMFString;
       
   173 	TTimeIntervalMicroSeconds32 iOn;
       
   174 	TTimeIntervalMicroSeconds32 iOff;
       
   175 	TTimeIntervalMicroSeconds32 iPause;
       
   176 	TInt iOnSamples;
       
   177 	TInt iOffSamples;
       
   178 	TInt iPauseSamples;
       
   179 	TInt iChar;
       
   180 	TBool iPlayToneOff;
       
   181 	};
       
   182 
       
   183 //
       
   184 // Tone sequence synthesis
       
   185 //
       
   186 
       
   187 const TInt KMaxSequenceStack = 6;
       
   188 class TMdaSequenceGenerator : public TMdaToneGenerator
       
   189 /**
       
   190 *@internalTechnology
       
   191 */
       
   192 	{
       
   193 public:
       
   194 	virtual void Reset();
       
   195 	void SetSequenceData(const TDesC8& aSequenceData); 
       
   196 private:
       
   197 	virtual TInt GetNextTone();
       
   198 private:
       
   199 	const TDesC8* iSequenceData;
       
   200 	const TInt16* iInstructionPtr;
       
   201 	const TInt16* iLastInstruction;
       
   202 	TInt iStack[KMaxSequenceStack];
       
   203 	TInt iStackIndex;
       
   204 	};
       
   205 
       
   206 const TInt KBufferLength = 0x1000;
       
   207 
       
   208 // Public Media Server includes
       
   209 
       
   210 class TMdaPtr8 : public TPtr8 //needed for this WINS Impl of Tone Gen
       
   211 	{
       
   212 public:
       
   213 	TMdaPtr8()
       
   214 		: TPtr8(0,0,0) {};
       
   215 	inline void Set(const TDes8& aDes)
       
   216 		{ TPtr8::Set((TUint8*)(aDes.Ptr()),aDes.Length(),aDes.MaxLength()); };
       
   217 	inline void SetLengthOnly(const TDes8& aDes)
       
   218 		{ TPtr8::Set((TUint8*)(aDes.Ptr()),aDes.Length(),aDes.Length()); };
       
   219 	inline void Set(const TPtrC8& aDes)
       
   220 		{ TPtr8::Set((TUint8*)(aDes.Ptr()),aDes.Length(),aDes.Length()); };
       
   221 	inline void Shift(TInt aOffset)
       
   222 		{ SetLength(Length()-aOffset); iMaxLength-=aOffset; iPtr+=aOffset; };
       
   223 	};
       
   224 
       
   225 
       
   226 #endif