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