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 <media/AudioSystem.h> 21 #include <media/AudioTrack.h> 22 #include <utils/Compat.h> 23 #include <utils/KeyedVector.h> 24 #include <utils/RefBase.h> 25 #include <utils/threads.h> 26 27 namespace android { 28 29 class ToneGenerator { 30 public: 31 32 // List of all available tones 33 // This enum must be kept consistant with constants in ToneGenerator JAVA class 34 enum tone_type { 35 // DTMF tones ITU-T Recommendation Q.23 36 TONE_DTMF_0 = 0, // 0 key: 1336Hz, 941Hz 37 TONE_DTMF_1, // 1 key: 1209Hz, 697Hz 38 TONE_DTMF_2, // 2 key: 1336Hz, 697Hz 39 TONE_DTMF_3, // 3 key: 1477Hz, 697Hz 40 TONE_DTMF_4, // 4 key: 1209Hz, 770Hz 41 TONE_DTMF_5, // 5 key: 1336Hz, 770Hz 42 TONE_DTMF_6, // 6 key: 1477Hz, 770Hz 43 TONE_DTMF_7, // 7 key: 1209Hz, 852Hz 44 TONE_DTMF_8, // 8 key: 1336Hz, 852Hz 45 TONE_DTMF_9, // 9 key: 1477Hz, 852Hz 46 TONE_DTMF_S, // * key: 1209Hz, 941Hz 47 TONE_DTMF_P, // # key: 1477Hz, 941Hz 48 TONE_DTMF_A, // A key: 1633Hz, 697Hz 49 TONE_DTMF_B, // B key: 1633Hz, 770Hz 50 TONE_DTMF_C, // C key: 1633Hz, 852Hz 51 TONE_DTMF_D, // D key: 1633Hz, 941Hz 52 // Call supervisory tones: 3GPP TS 22.001 (CEPT) 53 TONE_SUP_DIAL, // Dial tone: CEPT: 425Hz, continuous 54 FIRST_SUP_TONE = TONE_SUP_DIAL, 55 TONE_SUP_BUSY, // Busy tone, CEPT: 425Hz, 500ms ON, 500ms OFF... 56 TONE_SUP_CONGESTION, // Congestion tone CEPT, JAPAN: 425Hz, 200ms ON, 200ms OFF... 57 TONE_SUP_RADIO_ACK, // Radio path acknowlegment, CEPT, ANSI: 425Hz, 200ms ON 58 TONE_SUP_RADIO_NOTAVAIL, // Radio path not available: 425Hz, 200ms ON, 200 OFF 3 bursts 59 TONE_SUP_ERROR, // Error/Special info: 950Hz+1400Hz+1800Hz, 330ms ON, 1s OFF... 60 TONE_SUP_CALL_WAITING, // Call Waiting CEPT,JAPAN: 425Hz, 200ms ON, 600ms OFF, 200ms ON, 3s OFF... 61 TONE_SUP_RINGTONE, // Ring Tone CEPT, JAPAN: 425Hz, 1s ON, 4s OFF... 62 LAST_SUP_TONE = TONE_SUP_RINGTONE, 63 // Proprietary tones: 3GPP TS 31.111 64 TONE_PROP_BEEP, // General beep: 400Hz+1200Hz, 35ms ON 65 TONE_PROP_ACK, // Positive Acknowlgement: 1200Hz, 100ms ON, 100ms OFF 2 bursts 66 TONE_PROP_NACK, // Negative Acknowlgement: 300Hz+400Hz+500Hz, 400ms ON 67 TONE_PROP_PROMPT, // Prompt tone: 400Hz+1200Hz, 200ms ON 68 TONE_PROP_BEEP2, // General double beep: 400Hz+1200Hz, 35ms ON, 200ms OFF, 35ms on 69 // Additional call supervisory tones: specified by IS-95 only 70 TONE_SUP_INTERCEPT, // Intercept tone: alternating 440 Hz and 620 Hz tones, each on for 250 ms. 71 TONE_SUP_INTERCEPT_ABBREV, // Abbreviated intercept: intercept tone limited to 4 seconds 72 TONE_SUP_CONGESTION_ABBREV, // Abbreviated congestion: congestion tone limited to 4 seconds 73 TONE_SUP_CONFIRM, // Confirm tone: a 350 Hz tone added to a 440 Hz tone repeated 3 times in a 100 ms on, 100 ms off cycle. 74 TONE_SUP_PIP, // Pip tone: four bursts of 480 Hz tone (0.1 s on, 0.1 s off). 75 76 // CDMA Tones 77 TONE_CDMA_DIAL_TONE_LITE, 78 TONE_CDMA_NETWORK_USA_RINGBACK, 79 TONE_CDMA_INTERCEPT, 80 TONE_CDMA_ABBR_INTERCEPT, 81 TONE_CDMA_REORDER, 82 TONE_CDMA_ABBR_REORDER, 83 TONE_CDMA_NETWORK_BUSY, 84 TONE_CDMA_CONFIRM, 85 TONE_CDMA_ANSWER, 86 TONE_CDMA_NETWORK_CALLWAITING, 87 TONE_CDMA_PIP, 88 89 // ISDN 90 TONE_CDMA_CALL_SIGNAL_ISDN_NORMAL, // ISDN Alert Normal 91 TONE_CDMA_CALL_SIGNAL_ISDN_INTERGROUP, // ISDN Intergroup 92 TONE_CDMA_CALL_SIGNAL_ISDN_SP_PRI, // ISDN SP PRI 93 TONE_CDMA_CALL_SIGNAL_ISDN_PAT3, // ISDN Alert PAT3 94 TONE_CDMA_CALL_SIGNAL_ISDN_PING_RING, // ISDN Alert PING RING 95 TONE_CDMA_CALL_SIGNAL_ISDN_PAT5, // ISDN Alert PAT5 96 TONE_CDMA_CALL_SIGNAL_ISDN_PAT6, // ISDN Alert PAT6 97 TONE_CDMA_CALL_SIGNAL_ISDN_PAT7, // ISDN Alert PAT7 98 // ISDN end 99 100 // IS54 101 TONE_CDMA_HIGH_L, // IS54 High Pitch Long 102 TONE_CDMA_MED_L, // IS54 Med Pitch Long 103 TONE_CDMA_LOW_L, // IS54 Low Pitch Long 104 TONE_CDMA_HIGH_SS, // IS54 High Pitch Short Short 105 TONE_CDMA_MED_SS, // IS54 Medium Pitch Short Short 106 TONE_CDMA_LOW_SS, // IS54 Low Pitch Short Short 107 TONE_CDMA_HIGH_SSL, // IS54 High Pitch Short Short Long 108 TONE_CDMA_MED_SSL, // IS54 Medium Pitch Short Short Long 109 TONE_CDMA_LOW_SSL, // IS54 Low Pitch Short Short Long 110 TONE_CDMA_HIGH_SS_2, // IS54 High Pitch Short Short 2 111 TONE_CDMA_MED_SS_2, // IS54 Med Pitch Short Short 2 112 TONE_CDMA_LOW_SS_2, // IS54 Low Pitch Short Short 2 113 TONE_CDMA_HIGH_SLS, // IS54 High Pitch Short Long Short 114 TONE_CDMA_MED_SLS, // IS54 Med Pitch Short Long Short 115 TONE_CDMA_LOW_SLS, // IS54 Low Pitch Short Long Short 116 TONE_CDMA_HIGH_S_X4, // IS54 High Pitch Short Short Short Short 117 TONE_CDMA_MED_S_X4, // IS54 Med Pitch Short Short Short Short 118 TONE_CDMA_LOW_S_X4, // IS54 Low Pitch Short Short Short Short 119 TONE_CDMA_HIGH_PBX_L, // PBX High Pitch Long 120 TONE_CDMA_MED_PBX_L, // PBX Med Pitch Long 121 TONE_CDMA_LOW_PBX_L, // PBX Low Pitch Long 122 TONE_CDMA_HIGH_PBX_SS, // PBX High Short Short 123 TONE_CDMA_MED_PBX_SS, // PBX Med Short Short 124 TONE_CDMA_LOW_PBX_SS, // PBX Low Short Short 125 TONE_CDMA_HIGH_PBX_SSL, // PBX High Short Short Long 126 TONE_CDMA_MED_PBX_SSL, // PBX Med Short Short Long 127 TONE_CDMA_LOW_PBX_SSL, // PBX Low Short Short Long 128 TONE_CDMA_HIGH_PBX_SLS, // PBX High SLS 129 TONE_CDMA_MED_PBX_SLS, // PBX Med SLS 130 TONE_CDMA_LOW_PBX_SLS, // PBX Low SLS 131 TONE_CDMA_HIGH_PBX_S_X4, // PBX High SSSS 132 TONE_CDMA_MED_PBX_S_X4, // PBX Med SSSS 133 TONE_CDMA_LOW_PBX_S_X4, // PBX LOW SSSS 134 //IS54 end 135 // proprietary 136 TONE_CDMA_ALERT_NETWORK_LITE, 137 TONE_CDMA_ALERT_AUTOREDIAL_LITE, 138 TONE_CDMA_ONE_MIN_BEEP, 139 TONE_CDMA_KEYPAD_VOLUME_KEY_LITE, 140 TONE_CDMA_PRESSHOLDKEY_LITE, 141 TONE_CDMA_ALERT_INCALL_LITE, 142 TONE_CDMA_EMERGENCY_RINGBACK, 143 TONE_CDMA_ALERT_CALL_GUARD, 144 TONE_CDMA_SOFT_ERROR_LITE, 145 TONE_CDMA_CALLDROP_LITE, 146 // proprietary end 147 TONE_CDMA_NETWORK_BUSY_ONE_SHOT, 148 TONE_CDMA_ABBR_ALERT, 149 TONE_CDMA_SIGNAL_OFF, 150 //CDMA end 151 NUM_TONES, 152 NUM_SUP_TONES = LAST_SUP_TONE-FIRST_SUP_TONE+1 153 }; 154 155 ToneGenerator(audio_stream_type_t streamType, float volume, bool threadCanCallJava = false); 156 ~ToneGenerator(); 157 158 bool startTone(tone_type toneType, int durationMs = -1); 159 void stopTone(); 160 161 bool isInited() { return (mState == TONE_IDLE)?false:true;} 162 163 // returns the audio session this ToneGenerator belongs to or 0 if an error occured. 164 int getSessionId() { return (mpAudioTrack == 0) ? 0 : mpAudioTrack->getSessionId(); } 165 166 private: 167 168 enum tone_state { 169 TONE_IDLE, // ToneGenerator is being initialized or initialization failed 170 TONE_INIT, // ToneGenerator has been successfully initialized and is not playing 171 TONE_STARTING, // ToneGenerator is starting playing 172 TONE_PLAYING, // ToneGenerator is playing 173 TONE_STOPPING, // ToneGenerator is stoping 174 TONE_STOPPED, // ToneGenerator is stopped: the AudioTrack will be stopped 175 TONE_RESTARTING // A start request was received in active state (playing or stopping) 176 }; 177 178 179 // Region specific tones. 180 // These supervisory tones are different depending on the region (USA/CANADA, JAPAN, rest of the world). 181 // When a tone in the range [FIRST_SUP_TONE, LAST_SUP_TONE] is requested, the region is determined 182 // from system property gsm.operator.iso-country and the proper tone descriptor is selected with the 183 // help of sToneMappingTable[] 184 enum regional_tone_type { 185 // ANSI supervisory tones 186 TONE_ANSI_DIAL = NUM_TONES, // Dial tone: a continuous 350 Hz + 440 Hz tone. 187 TONE_ANSI_BUSY, // Busy tone on: a 480 Hz + 620 Hz tone repeated in a 500 ms on, 500 ms off cycle. 188 TONE_ANSI_CONGESTION, // Network congestion (reorder) tone on: a 480 Hz + 620 Hz tone repeated in a 250 ms on, 250 ms off cycle. 189 TONE_ANSI_CALL_WAITING, // Call waiting tone on: 440 Hz, on for 300 ms, 9,7 s off followed by 190 // (440 Hz, on for 100 ms off for 100 ms, on for 100 ms, 9,7s off and repeated as necessary). 191 TONE_ANSI_RINGTONE, // Ring Tone: a 440 Hz + 480 Hz tone repeated in a 2 s on, 4 s off pattern. 192 // JAPAN Supervisory tones 193 TONE_JAPAN_DIAL, // Dial tone: 400Hz, continuous 194 TONE_JAPAN_BUSY, // Busy tone: 400Hz, 500ms ON, 500ms OFF... 195 TONE_JAPAN_RADIO_ACK, // Radio path acknowlegment: 400Hz, 1s ON, 2s OFF... 196 // GB Supervisory tones 197 TONE_GB_RINGTONE, // Ring Tone: A 400Hz + 450Hz tone repeated in a 0.4s on, 0.2s off, 0.4s on, 2.0s off pattern. 198 // AUSTRALIA Supervisory tones 199 TONE_AUSTRALIA_RINGTONE, // Ring tone: A 400Hz + 450Hz tone repeated in a 0.4s on, 0.2s off, 0.4s on, 2.0s off pattern. 200 TONE_AUSTRALIA_BUSY, // Busy tone: 425 Hz repeated in a 0.375s on, 0.375s off pattern. 201 TONE_AUSTRALIA_CALL_WAITING,// Call waiting tone: 425Hz tone repeated in a 0.2s on, 0.2s off, 0.2s on, 4.4s off pattern. 202 TONE_AUSTRALIA_CONGESTION, // Congestion tone: 425Hz tone repeated in a 0.375s on, 0.375s off pattern 203 NUM_ALTERNATE_TONES 204 }; 205 206 enum region { 207 ANSI, 208 JAPAN, 209 GB, 210 AUSTRALIA, 211 CEPT, 212 NUM_REGIONS 213 }; 214 215 static const unsigned char sToneMappingTable[NUM_REGIONS-1][NUM_SUP_TONES]; 216 217 static const unsigned int TONEGEN_MAX_WAVES = 3; // Maximun number of sine waves in a tone segment 218 static const unsigned int TONEGEN_MAX_SEGMENTS = 12; // Maximun number of segments in a tone descriptor 219 static const unsigned int TONEGEN_INF = 0xFFFFFFFF; // Represents infinite time duration 220 static const CONSTEXPR float TONEGEN_GAIN = 0.9; // Default gain passed to WaveGenerator(). 221 222 // ToneDescriptor class contains all parameters needed to generate a tone: 223 // - The array waveFreq[]: 224 // 1 for static tone descriptors: contains the frequencies of all individual waves making the multi-tone. 225 // 2 for active tone descritors: contains the indexes of the WaveGenerator objects in mWaveGens 226 // The number of sine waves varies from 1 to TONEGEN_MAX_WAVES. 227 // The first null value indicates that no more waves are needed. 228 // - The array segments[] is used to generate the tone pulses. A segment is a period of time 229 // during which the tone is ON or OFF. Segments with even index (starting from 0) 230 // correspond to tone ON state and segments with odd index to OFF state. 231 // The data stored in segments[] is the duration of the corresponding period in ms. 232 // The first segment encountered with a 0 duration indicates that no more segment follows. 233 // - loopCnt - Number of times to repeat a sequence of seqments after playing this 234 // - loopIndx - The segment index to go back and play is loopcnt > 0 235 // - repeatCnt indicates the number of times the sequence described by segments[] array must be repeated. 236 // When the tone generator encounters the first 0 duration segment, it will compare repeatCnt to mCurCount. 237 // If mCurCount > repeatCnt, the tone is stopped automatically. Otherwise, tone sequence will be 238 // restarted from segment repeatSegment. 239 // - repeatSegment number of the first repeated segment when repeatCnt is not null 240 241 class ToneSegment { 242 public: 243 unsigned int duration; 244 unsigned short waveFreq[TONEGEN_MAX_WAVES+1]; 245 unsigned short loopCnt; 246 unsigned short loopIndx; 247 }; 248 249 class ToneDescriptor { 250 public: 251 ToneSegment segments[TONEGEN_MAX_SEGMENTS+1]; 252 unsigned long repeatCnt; 253 unsigned long repeatSegment; 254 }; 255 256 static const ToneDescriptor sToneDescriptors[]; 257 258 bool mThreadCanCallJava; 259 unsigned int mTotalSmp; // Total number of audio samples played (gives current time) 260 unsigned int mNextSegSmp; // Position of next segment transition expressed in samples 261 // NOTE: because mTotalSmp, mNextSegSmp are stored on 32 bit, current design will operate properly 262 // only if tone duration is less than about 27 Hours(@44100Hz sampling rate). If this time is exceeded, 263 // no crash will occur but tone sequence will show a glitch. 264 unsigned int mMaxSmp; // Maximum number of audio samples played (maximun tone duration) 265 int mDurationMs; // Maximum tone duration in ms 266 267 unsigned short mCurSegment; // Current segment index in ToneDescriptor segments[] 268 unsigned short mCurCount; // Current sequence repeat count 269 volatile unsigned short mState; // ToneGenerator state (tone_state) 270 unsigned short mRegion; 271 const ToneDescriptor *mpToneDesc; // pointer to active tone descriptor 272 const ToneDescriptor *mpNewToneDesc; // pointer to next active tone descriptor 273 274 unsigned short mLoopCounter; // Current tone loopback count 275 276 uint32_t mSamplingRate; // AudioFlinger Sampling rate 277 sp<AudioTrack> mpAudioTrack; // Pointer to audio track used for playback 278 Mutex mLock; // Mutex to control concurent access to ToneGenerator object from audio callback and application API 279 Mutex mCbkCondLock; // Mutex associated to mWaitCbkCond 280 Condition mWaitCbkCond; // condition enabling interface to wait for audio callback completion after a change is requested 281 float mVolume; // Volume applied to audio track 282 audio_stream_type_t mStreamType; // Audio stream used for output 283 unsigned int mProcessSize; // Size of audio blocks generated at a time by audioCallback() (in PCM frames). 284 struct timespec mStartTime; // tone start time: needed to guaranty actual tone duration 285 286 bool initAudioTrack(); 287 static void audioCallback(int event, void* user, void *info); 288 bool prepareWave(); 289 unsigned int numWaves(unsigned int segmentIdx); 290 void clearWaveGens(); 291 tone_type getToneForRegion(tone_type toneType); 292 293 // WaveGenerator generates a single sine wave 294 class WaveGenerator { 295 public: 296 enum gen_command { 297 WAVEGEN_START, // Start/restart wave from phase 0 298 WAVEGEN_CONT, // Continue wave from current phase 299 WAVEGEN_STOP // Stop wave on zero crossing 300 }; 301 302 WaveGenerator(unsigned short samplingRate, unsigned short frequency, 303 float volume); 304 ~WaveGenerator(); 305 306 void getSamples(short *outBuffer, unsigned int count, 307 unsigned int command); 308 309 private: 310 static const short GEN_AMP = 32000; // amplitude of generator 311 static const short S_Q14 = 14; // shift for Q14 312 static const short S_Q15 = 15; // shift for Q15 313 314 short mA1_Q14; // Q14 coefficient 315 // delay line of full amplitude generator 316 long mS1, mS2; // delay line S2 oldest 317 short mS2_0; // saved value for reinitialisation 318 short mAmplitude_Q15; // Q15 amplitude 319 }; 320 321 KeyedVector<unsigned short, WaveGenerator *> mWaveGens; // list of active wave generators. 322 }; 323 324 } 325 ; // namespace android 326 327 #endif /*ANDROID_TONEGENERATOR_H_*/ 328