The Android Open Source Project | 2729ea9 | 2008-10-21 07:00:00 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef ANDROID_TONEGENERATOR_H_ |
| 18 | #define ANDROID_TONEGENERATOR_H_ |
| 19 | |
| 20 | #include <utils/RefBase.h> |
| 21 | #include <utils/Vector.h> |
| 22 | #include <utils/threads.h> |
| 23 | #include <media/AudioSystem.h> |
| 24 | #include <media/AudioTrack.h> |
| 25 | |
| 26 | namespace android { |
| 27 | |
| 28 | class ToneGenerator { |
| 29 | public: |
| 30 | |
| 31 | // List of all available tones |
| 32 | // This enum must be kept consistant with constants in ToneGenerator JAVA class |
| 33 | enum tone_type { |
| 34 | // DTMF tones ITU-T Recommendation Q.23 |
| 35 | TONE_DTMF_0 = 0, // 0 key: 1336Hz, 941Hz |
| 36 | TONE_DTMF_1, // 1 key: 1209Hz, 697Hz |
| 37 | TONE_DTMF_2, // 2 key: 1336Hz, 697Hz |
| 38 | TONE_DTMF_3, // 3 key: 1477Hz, 697Hz |
| 39 | TONE_DTMF_4, // 4 key: 1209Hz, 770Hz |
| 40 | TONE_DTMF_5, // 5 key: 1336Hz, 770Hz |
| 41 | TONE_DTMF_6, // 6 key: 1477Hz, 770Hz |
| 42 | TONE_DTMF_7, // 7 key: 1209Hz, 852Hz |
| 43 | TONE_DTMF_8, // 8 key: 1336Hz, 852Hz |
| 44 | TONE_DTMF_9, // 9 key: 1477Hz, 852Hz |
| 45 | TONE_DTMF_S, // * key: 1209Hz, 941Hz |
| 46 | TONE_DTMF_P, // # key: 1477Hz, 941Hz |
| 47 | TONE_DTMF_A, // A key: 1633Hz, 697Hz |
| 48 | TONE_DTMF_B, // B key: 1633Hz, 770Hz |
| 49 | TONE_DTMF_C, // C key: 1633Hz, 852Hz |
| 50 | TONE_DTMF_D, // D key: 1633Hz, 941Hz |
| 51 | // Call supervisory tones: 3GPP TS 22.001 (CEPT) |
| 52 | TONE_SUP_DIAL, // Dial tone: 425Hz, continuous |
| 53 | TONE_SUP_BUSY, // Busy tone: 425Hz, 500ms ON, 500ms OFF... |
| 54 | TONE_SUP_CONGESTION, // Congestion tone: 425Hz, 200ms ON, 200ms OFF... |
| 55 | TONE_SUP_RADIO_ACK, // Radio path acknowlegment: 425Hz, 200ms ON |
| 56 | TONE_SUP_RADIO_NOTAVAIL, // Radio path not available: 425Hz, 200ms ON, 200 OFF 3 bursts |
| 57 | TONE_SUP_ERROR, // Error/Special info: 950Hz+1400Hz+1800Hz, 330ms ON, 1s OFF... |
| 58 | TONE_SUP_CALL_WAITING, // Call Waiting: 425Hz, 200ms ON, 600ms OFF, 200ms ON, 3s OFF... |
| 59 | TONE_SUP_RINGTONE, // Ring Tone: 425Hz, 1s ON, 4s OFF... |
| 60 | // Proprietary tones: 3GPP TS 31.111 |
| 61 | TONE_PROP_BEEP, // General beep: 400Hz+1200Hz, 35ms ON |
| 62 | TONE_PROP_ACK, // Positive Acknowlgement: 1200Hz, 100ms ON, 100ms OFF 2 bursts |
| 63 | TONE_PROP_NACK, // Negative Acknowlgement: 300Hz+400Hz+500Hz, 400ms ON |
| 64 | TONE_PROP_PROMPT, // Prompt tone: 400Hz+1200Hz, 200ms ON |
| 65 | TONE_PROP_BEEP2, // General double beep: 400Hz+1200Hz, 35ms ON, 200ms OFF, 35ms on |
| 66 | NUM_TONES |
| 67 | }; |
| 68 | |
| 69 | ToneGenerator(int streamType, float volume); |
| 70 | ~ToneGenerator(); |
| 71 | |
| 72 | bool startTone(int toneType); |
| 73 | void stopTone(); |
| 74 | |
| 75 | bool isInited() { return (mState == TONE_IDLE)?false:true;} |
| 76 | |
| 77 | private: |
| 78 | |
| 79 | enum tone_state { |
| 80 | TONE_IDLE, // ToneGenerator is being initialized or initialization failed |
| 81 | TONE_INIT, // ToneGenerator has been successfully initialized and is not playing |
| 82 | TONE_STARTING, // ToneGenerator is starting playing |
| 83 | TONE_PLAYING, // ToneGenerator is playing |
| 84 | TONE_STOPPING, // ToneGenerator is stoping |
| 85 | TONE_RESTARTING // |
| 86 | }; |
| 87 | |
| 88 | static const unsigned int NUM_PCM_BUFFERS = 2; // number of pcm buffers of audio track |
| 89 | |
| 90 | static const unsigned int TONEGEN_MAX_WAVES = 3; |
| 91 | static const unsigned int TONEGEN_MAX_SEGMENTS = 4; // Maximun number of elenemts in |
| 92 | static const unsigned int TONEGEN_INF = 0xFFFFFFFF; // Represents infinite time duration |
| 93 | static const float TONEGEN_GAIN = 0.9; // Default gain passed to WaveGenerator(). |
| 94 | |
| 95 | // ToneDescriptor class contains all parameters needed to generate a tone: |
| 96 | // - The array waveFreq[] contains the frequencies of all individual waves making the multi-tone. |
| 97 | // The number of sine waves varies from 1 to TONEGEN_MAX_WAVES. |
| 98 | // The first null value indicates that no more waves are needed. |
| 99 | // - The array segments[] is used to generate the tone pulses. A segment is a period of time |
| 100 | // during which the tone is ON or OFF. Segments with even index (starting from 0) |
| 101 | // correspond to tone ON state and segments with odd index to OFF state. |
| 102 | // The data stored in segments[] is the duration of the corresponding period in ms. |
| 103 | // The first segment encountered with a 0 duration indicates that no more segment follows. |
| 104 | // - repeatCnt indicates the number of times the sequence described by segments[] array must be repeated. |
| 105 | // When the tone generator encounters the first 0 duration segment, it will compare repeatCnt to mCurCount. |
| 106 | // If mCurCount > repeatCnt, the tone is stopped automatically. |
| 107 | |
| 108 | class ToneDescriptor { |
| 109 | public: |
| 110 | unsigned short waveFreq[TONEGEN_MAX_WAVES+1]; |
| 111 | unsigned long segments[TONEGEN_MAX_SEGMENTS+1]; |
| 112 | unsigned long repeatCnt; |
| 113 | }; |
| 114 | |
| 115 | static const ToneDescriptor toneDescriptors[NUM_TONES]; |
| 116 | |
| 117 | unsigned int mTotalSmp; // Total number of audio samples played (gives current time) |
| 118 | unsigned int mNextSegSmp; // Position of next segment transition expressed in samples |
| 119 | // NOTE: because mTotalSmp, mNextSegSmp are stored on 32 bit, current design will operate properly |
| 120 | // only if tone duration is less than about 27 Hours(@44100Hz sampling rate). If this time is exceeded, |
| 121 | // no crash will occur but tone sequence will show a glitch. |
| 122 | |
| 123 | unsigned short mCurSegment; // Current segment index in ToneDescriptor segments[] |
| 124 | unsigned short mCurCount; // Current sequence repeat count |
| 125 | volatile unsigned short mState; // ToneGenerator state (tone_state) |
| 126 | const ToneDescriptor *mpToneDesc; // pointer to active tone descriptor |
| 127 | const ToneDescriptor *mpNewToneDesc; // pointer to next active tone descriptor |
| 128 | |
| 129 | unsigned int mSamplingRate; // Sampling rate |
| 130 | AudioTrack *mpAudioTrack; // Pointer to audio track used for playback |
| 131 | Mutex mLock; // Mutex to control concurent access to ToneGenerator object from audio callback and application API |
| 132 | Mutex mCbkCondLock; // Mutex associated to mWaitCbkCond |
| 133 | Condition mWaitCbkCond; // condition enabling interface to wait for audio callback completion after a change is requested |
| 134 | float mVolume; // Volume applied to audio track |
| 135 | |
| 136 | static void audioCallback(void* user, const AudioTrack::Buffer& info); |
| 137 | bool prepareWave(); |
| 138 | unsigned int numWaves(); |
| 139 | void clearWaveGens(); |
| 140 | |
| 141 | // WaveGenerator generates a single sine wave |
| 142 | class WaveGenerator { |
| 143 | public: |
| 144 | enum gen_command { |
| 145 | WAVEGEN_START, // Start/restart wave from phase 0 |
| 146 | WAVEGEN_CONT, // Continue wave from current phase |
| 147 | WAVEGEN_STOP // Stop wave on zero crossing |
| 148 | }; |
| 149 | |
| 150 | WaveGenerator(unsigned short samplingRate, unsigned short frequency, |
| 151 | float volume); |
| 152 | ~WaveGenerator(); |
| 153 | |
| 154 | void getSamples(short *outBuffer, unsigned int count, |
| 155 | unsigned int command); |
| 156 | |
| 157 | private: |
| 158 | static const short GEN_AMP = 32000; // amplitude of generator |
| 159 | static const short S_Q14 = 14; // shift for Q14 |
| 160 | static const short S_Q15 = 15; // shift for Q15 |
| 161 | |
| 162 | short mA1_Q14; // Q14 coefficient |
| 163 | // delay line of full amplitude generator |
| 164 | short mS1, mS2; // delay line S2 oldest |
| 165 | short mS2_0; // saved value for reinitialisation |
| 166 | short mAmplitude_Q15; // Q15 amplitude |
| 167 | }; |
| 168 | |
| 169 | Vector<WaveGenerator *> mWaveGens; // list of active wave generators. |
| 170 | }; |
| 171 | |
| 172 | } |
| 173 | ; // namespace android |
| 174 | |
| 175 | #endif /*ANDROID_TONEGENERATOR_H_*/ |