The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [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> |
Eric Laurent | 0b62e24 | 2009-05-05 00:49:01 -0700 | [diff] [blame] | 21 | #include <utils/KeyedVector.h> |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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) |
Eric Laurent | 0b62e24 | 2009-05-05 00:49:01 -0700 | [diff] [blame] | 52 | TONE_SUP_DIAL, // Dial tone: CEPT: 425Hz, continuous |
| 53 | FIRST_SUP_TONE = TONE_SUP_DIAL, |
| 54 | TONE_SUP_BUSY, // Busy tone, CEPT: 425Hz, 500ms ON, 500ms OFF... |
| 55 | TONE_SUP_CONGESTION, // Congestion tone CEPT, JAPAN: 425Hz, 200ms ON, 200ms OFF... |
| 56 | TONE_SUP_RADIO_ACK, // Radio path acknowlegment, CEPT, ANSI: 425Hz, 200ms ON |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 57 | TONE_SUP_RADIO_NOTAVAIL, // Radio path not available: 425Hz, 200ms ON, 200 OFF 3 bursts |
| 58 | TONE_SUP_ERROR, // Error/Special info: 950Hz+1400Hz+1800Hz, 330ms ON, 1s OFF... |
Eric Laurent | 0b62e24 | 2009-05-05 00:49:01 -0700 | [diff] [blame] | 59 | TONE_SUP_CALL_WAITING, // Call Waiting CEPT,JAPAN: 425Hz, 200ms ON, 600ms OFF, 200ms ON, 3s OFF... |
| 60 | TONE_SUP_RINGTONE, // Ring Tone CEPT, JAPAN: 425Hz, 1s ON, 4s OFF... |
| 61 | LAST_SUP_TONE = TONE_SUP_RINGTONE, |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 62 | // Proprietary tones: 3GPP TS 31.111 |
| 63 | TONE_PROP_BEEP, // General beep: 400Hz+1200Hz, 35ms ON |
| 64 | TONE_PROP_ACK, // Positive Acknowlgement: 1200Hz, 100ms ON, 100ms OFF 2 bursts |
Eric Laurent | 0b62e24 | 2009-05-05 00:49:01 -0700 | [diff] [blame] | 65 | TONE_PROP_NACK, // Negative Acknowlgement: 300Hz+400Hz+500Hz, 400ms ON |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 66 | TONE_PROP_PROMPT, // Prompt tone: 400Hz+1200Hz, 200ms ON |
| 67 | TONE_PROP_BEEP2, // General double beep: 400Hz+1200Hz, 35ms ON, 200ms OFF, 35ms on |
Eric Laurent | 0b62e24 | 2009-05-05 00:49:01 -0700 | [diff] [blame] | 68 | // Additional call supervisory tones: specified by IS-95 only |
| 69 | TONE_SUP_INTERCEPT, // Intercept tone: alternating 440 Hz and 620 Hz tones, each on for 250 ms. |
| 70 | TONE_SUP_INTERCEPT_ABBREV, // Abbreviated intercept: intercept tone limited to 4 seconds |
| 71 | TONE_SUP_CONGESTION_ABBREV, // Abbreviated congestion: congestion tone limited to 4 seconds |
| 72 | 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. |
| 73 | TONE_SUP_PIP, // Pip tone: four bursts of 480 Hz tone (0.1 s on, 0.1 s off). |
| 74 | NUM_TONES, |
| 75 | NUM_SUP_TONES = LAST_SUP_TONE-FIRST_SUP_TONE+1 |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 76 | }; |
| 77 | |
| 78 | ToneGenerator(int streamType, float volume); |
| 79 | ~ToneGenerator(); |
| 80 | |
| 81 | bool startTone(int toneType); |
| 82 | void stopTone(); |
| 83 | |
| 84 | bool isInited() { return (mState == TONE_IDLE)?false:true;} |
| 85 | |
| 86 | private: |
| 87 | |
| 88 | enum tone_state { |
| 89 | TONE_IDLE, // ToneGenerator is being initialized or initialization failed |
| 90 | TONE_INIT, // ToneGenerator has been successfully initialized and is not playing |
| 91 | TONE_STARTING, // ToneGenerator is starting playing |
| 92 | TONE_PLAYING, // ToneGenerator is playing |
| 93 | TONE_STOPPING, // ToneGenerator is stoping |
| 94 | TONE_RESTARTING // |
| 95 | }; |
| 96 | |
Eric Laurent | 0b62e24 | 2009-05-05 00:49:01 -0700 | [diff] [blame] | 97 | |
| 98 | // Region specific tones. |
| 99 | // These supervisory tones are different depending on the region (USA/CANADA, JAPAN, rest of the world). |
| 100 | // When a tone in the range [FIRST_SUP_TONE, LAST_SUP_TONE] is requested, the region is determined |
| 101 | // from system property gsm.operator.iso-country and the proper tone descriptor is selected with the |
| 102 | // help of sToneMappingTable[] |
| 103 | enum regional_tone_type { |
| 104 | // ANSI supervisory tones |
| 105 | TONE_ANSI_DIAL = NUM_TONES, // Dial tone: a continuous 350 Hz + 440 Hz tone. |
| 106 | TONE_ANSI_BUSY, // Busy tone on: a 480 Hz + 620 Hz tone repeated in a 500 ms on, 500 ms off cycle. |
| 107 | TONE_ANSI_CONGESTION, // Network congestion (reorder) tone on: a 480 Hz + 620 Hz tone repeated in a 250 ms on, 250 ms off cycle. |
| 108 | TONE_ANSI_CALL_WAITING, // Call waiting tone on: 440 Hz, on for 300 ms, 9,7 s off followed by |
| 109 | // (440 Hz, on for 100 ms off for 100 ms, on for 100 ms, 9,7s off and repeated as necessary). |
| 110 | TONE_ANSI_RINGTONE, // Ring Tone: a 440 Hz + 480 Hz tone repeated in a 2 s on, 4 s off pattern. |
| 111 | // JAPAN Supervisory tones |
| 112 | TONE_JAPAN_DIAL, // Dial tone: 400Hz, continuous |
| 113 | TONE_JAPAN_BUSY, // Busy tone: 400Hz, 500ms ON, 500ms OFF... |
| 114 | TONE_JAPAN_RADIO_ACK, // Radio path acknowlegment: 400Hz, 1s ON, 2s OFF... |
| 115 | NUM_ALTERNATE_TONES |
| 116 | }; |
| 117 | |
| 118 | enum region { |
| 119 | ANSI, |
| 120 | JAPAN, |
| 121 | CEPT, |
| 122 | NUM_REGIONS |
| 123 | }; |
| 124 | |
| 125 | static const unsigned char sToneMappingTable[NUM_REGIONS-1][NUM_SUP_TONES]; |
| 126 | |
| 127 | static const unsigned int TONEGEN_MAX_WAVES = 3; // Maximun number of sine waves in a tone segment |
| 128 | static const unsigned int TONEGEN_MAX_SEGMENTS = 5; // Maximun number of segments in a tone descriptor |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 129 | static const unsigned int TONEGEN_INF = 0xFFFFFFFF; // Represents infinite time duration |
| 130 | static const float TONEGEN_GAIN = 0.9; // Default gain passed to WaveGenerator(). |
| 131 | |
| 132 | // ToneDescriptor class contains all parameters needed to generate a tone: |
Eric Laurent | 0b62e24 | 2009-05-05 00:49:01 -0700 | [diff] [blame] | 133 | // - The array waveFreq[]: |
| 134 | // 1 for static tone descriptors: contains the frequencies of all individual waves making the multi-tone. |
| 135 | // 2 for active tone descritors: contains the indexes of the WaveGenerator objects in mWaveGens |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 136 | // The number of sine waves varies from 1 to TONEGEN_MAX_WAVES. |
| 137 | // The first null value indicates that no more waves are needed. |
| 138 | // - The array segments[] is used to generate the tone pulses. A segment is a period of time |
| 139 | // during which the tone is ON or OFF. Segments with even index (starting from 0) |
| 140 | // correspond to tone ON state and segments with odd index to OFF state. |
| 141 | // The data stored in segments[] is the duration of the corresponding period in ms. |
| 142 | // The first segment encountered with a 0 duration indicates that no more segment follows. |
| 143 | // - repeatCnt indicates the number of times the sequence described by segments[] array must be repeated. |
Eric Laurent | 0b62e24 | 2009-05-05 00:49:01 -0700 | [diff] [blame] | 144 | // When the tone generator encounters the first 0 duration segment, it will compare repeatCnt to mCurCount. |
| 145 | // If mCurCount > repeatCnt, the tone is stopped automatically. Otherwise, tone sequence will be |
| 146 | // restarted from segment repeatSegment. |
| 147 | // - repeatSegment number of the first repeated segment when repeatCnt is not null |
| 148 | |
| 149 | class ToneSegment { |
| 150 | public: |
| 151 | unsigned int duration; |
| 152 | unsigned short waveFreq[TONEGEN_MAX_WAVES+1]; |
| 153 | }; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 154 | |
| 155 | class ToneDescriptor { |
| 156 | public: |
Eric Laurent | 0b62e24 | 2009-05-05 00:49:01 -0700 | [diff] [blame] | 157 | ToneSegment segments[TONEGEN_MAX_SEGMENTS+1]; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 158 | unsigned long repeatCnt; |
Eric Laurent | 0b62e24 | 2009-05-05 00:49:01 -0700 | [diff] [blame] | 159 | unsigned long repeatSegment; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 160 | }; |
| 161 | |
Eric Laurent | 0b62e24 | 2009-05-05 00:49:01 -0700 | [diff] [blame] | 162 | static const ToneDescriptor sToneDescriptors[]; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 163 | |
| 164 | unsigned int mTotalSmp; // Total number of audio samples played (gives current time) |
| 165 | unsigned int mNextSegSmp; // Position of next segment transition expressed in samples |
| 166 | // NOTE: because mTotalSmp, mNextSegSmp are stored on 32 bit, current design will operate properly |
| 167 | // only if tone duration is less than about 27 Hours(@44100Hz sampling rate). If this time is exceeded, |
| 168 | // no crash will occur but tone sequence will show a glitch. |
| 169 | |
| 170 | unsigned short mCurSegment; // Current segment index in ToneDescriptor segments[] |
| 171 | unsigned short mCurCount; // Current sequence repeat count |
| 172 | volatile unsigned short mState; // ToneGenerator state (tone_state) |
Eric Laurent | 0b62e24 | 2009-05-05 00:49:01 -0700 | [diff] [blame] | 173 | unsigned short mRegion; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 174 | const ToneDescriptor *mpToneDesc; // pointer to active tone descriptor |
| 175 | const ToneDescriptor *mpNewToneDesc; // pointer to next active tone descriptor |
| 176 | |
| 177 | int mSamplingRate; // AudioFlinger Sampling rate |
| 178 | AudioTrack *mpAudioTrack; // Pointer to audio track used for playback |
| 179 | Mutex mLock; // Mutex to control concurent access to ToneGenerator object from audio callback and application API |
| 180 | Mutex mCbkCondLock; // Mutex associated to mWaitCbkCond |
| 181 | Condition mWaitCbkCond; // condition enabling interface to wait for audio callback completion after a change is requested |
| 182 | float mVolume; // Volume applied to audio track |
| 183 | int mStreamType; // Audio stream used for output |
| 184 | unsigned int mProcessSize; // Size of audio blocks generated at a time by audioCallback() (in PCM frames). |
| 185 | |
| 186 | bool initAudioTrack(); |
| 187 | static void audioCallback(int event, void* user, void *info); |
| 188 | bool prepareWave(); |
Eric Laurent | 0b62e24 | 2009-05-05 00:49:01 -0700 | [diff] [blame] | 189 | unsigned int numWaves(unsigned int segmentIdx); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 190 | void clearWaveGens(); |
Eric Laurent | 0b62e24 | 2009-05-05 00:49:01 -0700 | [diff] [blame] | 191 | int getToneForRegion(int toneType); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 192 | |
| 193 | // WaveGenerator generates a single sine wave |
| 194 | class WaveGenerator { |
| 195 | public: |
| 196 | enum gen_command { |
| 197 | WAVEGEN_START, // Start/restart wave from phase 0 |
| 198 | WAVEGEN_CONT, // Continue wave from current phase |
| 199 | WAVEGEN_STOP // Stop wave on zero crossing |
| 200 | }; |
| 201 | |
| 202 | WaveGenerator(unsigned short samplingRate, unsigned short frequency, |
| 203 | float volume); |
| 204 | ~WaveGenerator(); |
| 205 | |
| 206 | void getSamples(short *outBuffer, unsigned int count, |
| 207 | unsigned int command); |
| 208 | |
| 209 | private: |
| 210 | static const short GEN_AMP = 32000; // amplitude of generator |
| 211 | static const short S_Q14 = 14; // shift for Q14 |
| 212 | static const short S_Q15 = 15; // shift for Q15 |
| 213 | |
| 214 | short mA1_Q14; // Q14 coefficient |
| 215 | // delay line of full amplitude generator |
| 216 | short mS1, mS2; // delay line S2 oldest |
| 217 | short mS2_0; // saved value for reinitialisation |
| 218 | short mAmplitude_Q15; // Q15 amplitude |
| 219 | }; |
| 220 | |
Eric Laurent | 0b62e24 | 2009-05-05 00:49:01 -0700 | [diff] [blame] | 221 | KeyedVector<unsigned short, WaveGenerator *> mWaveGens; // list of active wave generators. |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 222 | }; |
| 223 | |
| 224 | } |
| 225 | ; // namespace android |
| 226 | |
| 227 | #endif /*ANDROID_TONEGENERATOR_H_*/ |