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 //#define LOG_NDEBUG 0
18 #define LOG_TAG "ToneGenerator"
19 
20 #include <math.h>
21 #include <utils/Log.h>
22 #include <cutils/properties.h>
23 #include "media/ToneGenerator.h"
24 
25 
26 namespace android {
27 
28 
29 // Descriptors for all available tones (See ToneGenerator::ToneDescriptor class declaration for details)
30 const ToneGenerator::ToneDescriptor ToneGenerator::sToneDescriptors[] = {
31         { .segments = { { .duration = ToneGenerator::TONEGEN_INF, .waveFreq = { 1336, 941, 0 }, 0, 0},
32                         { .duration = 0 , .waveFreq = { 0 }, 0, 0}},
33           .repeatCnt = ToneGenerator::TONEGEN_INF,
34           .repeatSegment = 0 },                              // TONE_DTMF_0
35         { .segments = { { .duration = ToneGenerator::TONEGEN_INF, .waveFreq = { 1209, 697, 0 }, 0, 0 },
36                         { .duration = 0 , .waveFreq = { 0 }, 0, 0}},
37           .repeatCnt = ToneGenerator::TONEGEN_INF,
38           .repeatSegment = 0 },                              // TONE_DTMF_1
39         { .segments = { { .duration = ToneGenerator::TONEGEN_INF, .waveFreq = { 1336, 697, 0 }, 0, 0 },
40                         { .duration = 0 , .waveFreq = { 0 }, 0, 0}},
41           .repeatCnt = ToneGenerator::TONEGEN_INF,
42           .repeatSegment = 0 },                              // TONE_DTMF_2
43         { .segments = { { .duration = ToneGenerator::TONEGEN_INF, .waveFreq = { 1477, 697, 0 }, 0, 0 },
44                         { .duration = 0 , .waveFreq = { 0 }, 0, 0}},
45           .repeatCnt = ToneGenerator::TONEGEN_INF,
46           .repeatSegment = 0 },                              // TONE_DTMF_3
47         { .segments = { { .duration = ToneGenerator::TONEGEN_INF, .waveFreq = { 1209, 770, 0 }, 0, 0 },
48                         { .duration = 0 , .waveFreq = { 0 }, 0, 0}},
49           .repeatCnt = ToneGenerator::TONEGEN_INF,
50           .repeatSegment = 0 },                              // TONE_DTMF_4
51         { .segments = { { .duration = ToneGenerator::TONEGEN_INF, .waveFreq = { 1336, 770, 0 }, 0, 0 },
52                         { .duration = 0 , .waveFreq = { 0 }, 0, 0}},
53           .repeatCnt = ToneGenerator::TONEGEN_INF,
54           .repeatSegment = 0 },                              // TONE_DTMF_5
55         { .segments = { { .duration = ToneGenerator::TONEGEN_INF, .waveFreq = { 1477, 770, 0 }, 0, 0 },
56                         { .duration = 0 , .waveFreq = { 0 }, 0, 0}},
57           .repeatCnt = ToneGenerator::TONEGEN_INF,
58           .repeatSegment = 0 },                              // TONE_DTMF_6
59         { .segments = { { .duration = ToneGenerator::TONEGEN_INF, .waveFreq = { 1209, 852, 0 }, 0, 0 },
60                         { .duration = 0 , .waveFreq = { 0 }, 0, 0}},
61           .repeatCnt = ToneGenerator::TONEGEN_INF,
62           .repeatSegment = 0 },                              // TONE_DTMF_7
63         { .segments = { { .duration = ToneGenerator::TONEGEN_INF, .waveFreq = { 1336, 852, 0 }, 0, 0 },
64                         { .duration = 0 , .waveFreq = { 0 }, 0, 0}},
65           .repeatCnt = ToneGenerator::TONEGEN_INF,
66           .repeatSegment = 0 },                              // TONE_DTMF_8
67         { .segments = { { .duration = ToneGenerator::TONEGEN_INF, .waveFreq = { 1477, 852, 0 }, 0, 0 },
68                         { .duration = 0 , .waveFreq = { 0 }, 0, 0}},
69           .repeatCnt = ToneGenerator::TONEGEN_INF,
70           .repeatSegment = 0 },                              // TONE_DTMF_9
71         { .segments = { { .duration = ToneGenerator::TONEGEN_INF, .waveFreq = { 1209, 941, 0 }, 0, 0 },
72                         { .duration = 0 , .waveFreq = { 0 }, 0, 0}},
73           .repeatCnt = ToneGenerator::TONEGEN_INF,
74           .repeatSegment = 0 },                              // TONE_DTMF_S
75         { .segments = { { .duration = ToneGenerator::TONEGEN_INF, .waveFreq = { 1477, 941, 0 }, 0, 0 },
76                         { .duration = 0 , .waveFreq = { 0 }, 0, 0}},
77           .repeatCnt = ToneGenerator::TONEGEN_INF,
78           .repeatSegment = 0 },                              // TONE_DTMF_P
79         { .segments = { { .duration = ToneGenerator::TONEGEN_INF, .waveFreq = { 1633, 697, 0 }, 0, 0 },
80                         { .duration = 0 , .waveFreq = { 0 }, 0, 0}},
81           .repeatCnt = ToneGenerator::TONEGEN_INF,
82           .repeatSegment = 0 },                              // TONE_DTMF_A
83         { .segments = { { .duration = ToneGenerator::TONEGEN_INF, .waveFreq = { 1633, 770, 0 }, 0, 0 },
84                         { .duration = 0 , .waveFreq = { 0 }, 0, 0}},
85           .repeatCnt = ToneGenerator::TONEGEN_INF,
86           .repeatSegment = 0 },                             // TONE_DTMF_B
87         { .segments = { { .duration = ToneGenerator::TONEGEN_INF, .waveFreq = { 1633, 852, 0 }, 0, 0 },
88                         { .duration = 0 , .waveFreq = { 0 }, 0, 0}},
89           .repeatCnt = ToneGenerator::TONEGEN_INF,
90           .repeatSegment = 0 },                              // TONE_DTMF_C
91         { .segments = { { .duration = ToneGenerator::TONEGEN_INF, .waveFreq = { 1633, 941, 0 }, 0, 0 },
92                         { .duration = 0 , .waveFreq = { 0 }, 0, 0}},
93           .repeatCnt = ToneGenerator::TONEGEN_INF,
94           .repeatSegment = 0 },                              // TONE_DTMF_D
95         { .segments = { { .duration = ToneGenerator::TONEGEN_INF, .waveFreq = { 425, 0 }, 0, 0 },
96                         { .duration = 0 , .waveFreq = { 0 }, 0, 0}},
97           .repeatCnt = ToneGenerator::TONEGEN_INF,
98           .repeatSegment = 0 },                              // TONE_SUP_DIAL
99         { .segments = { { .duration = 500 , .waveFreq = { 425, 0 }, 0, 0},
100                         { .duration = 500, .waveFreq = { 0 }, 0, 0},
101                            { .duration = 0 , .waveFreq = { 0 }, 0, 0}},
102           .repeatCnt = ToneGenerator::TONEGEN_INF,
103           .repeatSegment = 0 },                              // TONE_SUP_BUSY
104         { .segments = { { .duration = 200, .waveFreq = { 425, 0 }, 0, 0 },
105                         { .duration = 200, .waveFreq = { 0 }, 0, 0 },
106                         { .duration = 0 , .waveFreq = { 0 }, 0, 0}},
107           .repeatCnt = ToneGenerator::TONEGEN_INF,
108           .repeatSegment = 0 },                              // TONE_SUP_CONGESTION
109         { .segments = { { .duration = 200, .waveFreq = { 425, 0 }, 0, 0 },
110                         { .duration = 0 , .waveFreq = { 0 }, 0, 0}},
111           .repeatCnt = 0,
112           .repeatSegment = 0 },                              // TONE_SUP_RADIO_ACK
113         { .segments = { { .duration = 200, .waveFreq = { 425, 0 }, 0, 0},
114                         { .duration = 200, .waveFreq = { 0 }, 0, 0},
115                         { .duration = 0 , .waveFreq = { 0 }, 0, 0}},
116           .repeatCnt = 2,
117           .repeatSegment = 0 },                              // TONE_SUP_RADIO_NOTAVAIL
118         { .segments = { { .duration = 330, .waveFreq = { 950, 1400, 1800, 0 }, 0, 0},
119                         { .duration = 1000, .waveFreq = { 0 }, 0, 0},
120                         { .duration = 0 , .waveFreq = { 0 }, 0, 0}},
121           .repeatCnt = ToneGenerator::TONEGEN_INF,
122           .repeatSegment = 0 },                              // TONE_SUP_ERROR
123         { .segments = { { .duration = 200, .waveFreq = { 425, 0 }, 0, 0 },
124                         { .duration = 600, .waveFreq = { 0 }, 0, 0 },
125                         { .duration = 200, .waveFreq = { 425, 0 }, 0, 0 },
126                         { .duration = 3000, .waveFreq = { 0 }, 0, 0 },
127                         { .duration = 0 , .waveFreq = { 0 }, 0, 0}},
128           .repeatCnt = ToneGenerator::TONEGEN_INF,
129           .repeatSegment = 0 },                              // TONE_SUP_CALL_WAITING
130         { .segments = { { .duration = 1000, .waveFreq = { 425, 0 }, 0, 0 },
131                         { .duration = 4000, .waveFreq = { 0 }, 0, 0 },
132                         { .duration = 0 , .waveFreq = { 0 }, 0, 0}},
133           .repeatCnt = ToneGenerator::TONEGEN_INF,
134           .repeatSegment = 0 },                              // TONE_SUP_RINGTONE
135         { .segments = { { .duration = 40, .waveFreq = { 400, 1200, 0 }, 0, 0 },
136                         { .duration = 0 , .waveFreq = { 0 }, 0, 0}},
137           .repeatCnt = 0,
138           .repeatSegment = 0 },                              // TONE_PROP_BEEP
139         { .segments = { { .duration = 100, .waveFreq = { 1200, 0 }, 0, 0 },
140                         { .duration = 100, .waveFreq = { 0 }, 0, 0  },
141                         { .duration = 0 , .waveFreq = { 0 }, 0, 0}},
142           .repeatCnt = 1,
143           .repeatSegment = 0 },                              // TONE_PROP_ACK
144         { .segments = { { .duration = 400, .waveFreq = { 300, 400, 500, 0 }, 0, 0 },
145                         { .duration = 0 , .waveFreq = { 0 }, 0, 0}},
146           .repeatCnt = 0,
147           .repeatSegment = 0 },                              // TONE_PROP_NACK
148         { .segments = { { .duration = 200, .waveFreq = { 400, 1200, 0 }, 0, 0 },
149                         { .duration = 0 , .waveFreq = { 0 }, 0, 0}},
150           .repeatCnt = 0,
151           .repeatSegment = 0 },                              // TONE_PROP_PROMPT
152         { .segments = { { .duration = 40, .waveFreq = { 400, 1200, 0 }, 0, 0 },
153                         { .duration = 200, .waveFreq = { 0 }, 0, 0 },
154                         { .duration = 40, .waveFreq = { 400, 1200, 0 }, 0, 0 },
155                         { .duration = 0 , .waveFreq = { 0 }, 0, 0}},
156           .repeatCnt = 0,
157           .repeatSegment = 0 },                             // TONE_PROP_BEEP2
158         { .segments = { { .duration = 250, .waveFreq = { 440, 0 }, 0, 0 },
159                         { .duration = 250, .waveFreq = { 620, 0 }, 0, 0 },
160                         { .duration = 0 , .waveFreq = { 0 }, 0, 0 }},
161           .repeatCnt = ToneGenerator::TONEGEN_INF,
162           .repeatSegment = 0 },                              // TONE_SUP_INTERCEPT
163         { .segments = { { .duration = 250, .waveFreq = { 440, 0 }, 0, 0 },
164                         { .duration = 250, .waveFreq = { 620, 0 }, 0, 0 },
165                         { .duration = 0 , .waveFreq = { 0 }, 0, 0}},
166           .repeatCnt = 7,
167           .repeatSegment = 0 },                             // TONE_SUP_INTERCEPT_ABBREV
168         { .segments = { { .duration = 250, .waveFreq = { 480, 620, 0 }, 0, 0 },
169                         { .duration = 250, .waveFreq = { 0 }, 0, 0 },
170                         { .duration = 0 , .waveFreq = { 0 }, 0, 0}},
171           .repeatCnt = 7,
172           .repeatSegment = 0 },                             // TONE_SUP_CONGESTION_ABBREV
173         { .segments = { { .duration = 100, .waveFreq = { 350, 440, 0 }, 0, 0 },
174                         { .duration = 100, .waveFreq = { 0 }, 0, 0 },
175                         { .duration = 0 , .waveFreq = { 0 }, 0, 0}},
176           .repeatCnt = 2,
177           .repeatSegment = 0 },                             // TONE_SUP_CONFIRM
178         { .segments = { { .duration = 100, .waveFreq = { 480, 0 }, 0, 0 },
179                         { .duration = 100, .waveFreq = { 0 }, 0, 0 },
180                         { .duration = 0 , .waveFreq = { 0 }, 0, 0}},
181           .repeatCnt = 3,
182           .repeatSegment = 0 },                              // TONE_SUP_PIP
183         { .segments = {{ .duration = ToneGenerator::TONEGEN_INF, .waveFreq = { 425, 0 }, 0, 0},
184                        { .duration = 0 , .waveFreq = { 0 }, 0, 0}},
185           .repeatCnt = ToneGenerator::TONEGEN_INF,
186           .repeatSegment = 0 },                              // TONE_CDMA_DIAL_TONE_LITE
187         { .segments = { { .duration = 2000, .waveFreq = { 440, 480, 0 }, 0, 0 },
188                         { .duration = 4000, .waveFreq = { 0 }, 0, 0 },
189                         { .duration = 0 , .waveFreq = { 0 }, 0, 0}},
190           .repeatCnt = ToneGenerator::TONEGEN_INF,
191           .repeatSegment = 0 },                              // TONE_CDMA_NETWORK_USA_RINGBACK
192         { .segments = { { .duration = 250, .waveFreq = { 440, 0 }, 0, 0 },
193                         { .duration = 250, .waveFreq = { 620, 0 }, 0, 0 },
194                         { .duration = 0 , .waveFreq = { 0 }, 0, 0}},
195           .repeatCnt =  ToneGenerator::TONEGEN_INF,
196           .repeatSegment = 0 },                             // TONE_CDMA_INTERCEPT
197         { .segments = { { .duration = 250, .waveFreq = { 440, 0 }, 0, 0 },
198                         { .duration = 250, .waveFreq = { 620, 0 }, 0, 0 },
199                         { .duration = 0 , .waveFreq = { 0 }, 0, 0}},
200           .repeatCnt =  0,
201           .repeatSegment = 0 },                             // TONE_CDMA_ABBR_INTERCEPT
202         { .segments = { { .duration = 250, .waveFreq = { 480, 620, 0 }, 0, 0 },
203                         { .duration = 250, .waveFreq = { 0 }, 0, 0 },
204                         { .duration = 0 , .waveFreq = { 0 }, 0, 0}},
205           .repeatCnt = ToneGenerator::TONEGEN_INF,
206           .repeatSegment = 0 },                              // TONE_CDMA_REORDER
207         { .segments = { { .duration = 250, .waveFreq = { 480, 620, 0 }, 0, 0 },
208                         { .duration = 250, .waveFreq = { 0 }, 0, 0 },
209                         { .duration = 0 , .waveFreq = { 0 }, 0, 0}},
210           .repeatCnt = 7,
211           .repeatSegment = 0 },                              // TONE_CDMA_ABBR_REORDER
212         { .segments = { { .duration = 500, .waveFreq = { 480, 620, 0 }, 0, 0 },
213                         { .duration = 500, .waveFreq = { 0 }, 0, 0 },
214                         { .duration = 0 , .waveFreq = { 0 }, 0, 0}},
215           .repeatCnt = ToneGenerator::TONEGEN_INF,
216           .repeatSegment = 0 },                              // TONE_CDMA_NETWORK_BUSY
217         { .segments = { { .duration = 100, .waveFreq = { 350, 440, 0 }, 0, 0 },
218                         { .duration = 100, .waveFreq = { 0 }, 0, 0 },
219                         { .duration = 0 , .waveFreq = { 0 }, 0, 0}},
220           .repeatCnt = 2,
221           .repeatSegment = 0 },                              // TONE_CDMA_CONFIRM
222         { .segments = { { .duration = 500, .waveFreq = { 660, 1000, 0 }, 0, 0 },
223                         { .duration = 0 , .waveFreq = { 0 }, 0, 0}},
224           .repeatCnt = 0,
225           .repeatSegment = 0 },                              // TONE_CDMA_ANSWER
226         { .segments = { { .duration = 300, .waveFreq = { 440, 0 }, 0, 0 },
227                         { .duration = 0 , .waveFreq = { 0 }, 0, 0}},
228           .repeatCnt = 0,
229           .repeatSegment = 0 },                              // TONE_CDMA_NETWORK_CALLWAITING
230         { .segments = { { .duration = 100, .waveFreq = { 480, 0 }, 0, 0 },
231                         { .duration = 100, .waveFreq = { 0 }, 0, 0 },
232                         { .duration = 0 , .waveFreq = { 0 }, 0, 0}},
233           .repeatCnt = 3,
234           .repeatSegment = 0 },                              // TONE_CDMA_PIP
235 
236         { .segments = { { .duration = 32, .waveFreq = { 2091, 0}, 0, 0 },
237                         { .duration = 64, .waveFreq = { 2556, 0}, 19, 0},
238                         { .duration = 32, .waveFreq = { 2091, 0}, 0, 0},
239                         { .duration = 48, .waveFreq = { 2556, 0}, 0, 0},
240                         { .duration = 4000, .waveFreq = { 0 }, 0, 0},
241                         { .duration = 0,  .waveFreq = { 0 }, 0, 0}},
242           .repeatCnt = 0,
243           .repeatSegment = 0 },                             // TONE_CDMA_CALL_SIGNAL_ISDN_NORMAL
244         { .segments = { { .duration = 32, .waveFreq = { 2091, 0}, 0, 0 },
245                         { .duration = 64, .waveFreq = { 2556, 0}, 7, 0 },
246                         { .duration = 32, .waveFreq = { 2091, 0}, 0, 0 },
247                         { .duration = 400, .waveFreq = { 0 }, 0, 0 },
248                         { .duration = 32,  .waveFreq = { 2091, 0}, 0, 0 },
249                         { .duration = 64,  .waveFreq = { 2556, 0}, 7, 4 },
250                         { .duration = 32,  .waveFreq = { 2091, 0}, 0, 0 },
251                         { .duration = 4000, .waveFreq = { 0 }, 0, 0 },
252                         { .duration = 0,    .waveFreq = { 0 }, 0, 0 } },
253           .repeatCnt = 0,
254           .repeatSegment = 0 },                              // TONE_CDMA_CALL_SIGNAL_ISDN_INTERGROUP
255         { .segments = { { .duration = 32, .waveFreq = { 2091, 0}, 0, 0 },
256                         { .duration = 64, .waveFreq = { 2556, 0}, 3, 0 },
257                         { .duration = 16, .waveFreq = { 2091, 0}, 0, 0 },
258                         { .duration = 200, .waveFreq = { 0 },     0, 0 },
259                         { .duration = 32, .waveFreq = { 2091, 0}, 0, 0 },
260                         { .duration = 64, .waveFreq = { 2556, 0}, 3, 4 },
261                         { .duration = 16, .waveFreq = { 2091, 0}, 0, 0 },
262                         { .duration = 200, .waveFreq = { 0 },     0, 0 },
263                         { .duration = 0,   .waveFreq = { 0 },     0, 0 } },
264           .repeatCnt = 0,
265           .repeatSegment = 0 },                            // TONE_CDMA_CALL_SIGNAL_ISDN_SP_PRI
266         { .segments = { { .duration = 0,  .waveFreq = { 0 }, 0, 0} },
267           .repeatCnt = 0,
268           .repeatSegment = 0 },                            // TONE_CDMA_CALL_SIGNAL_ISDN_PAT3
269         { .segments = { { .duration = 32, .waveFreq = { 2091, 0 }, 0, 0 },
270                         { .duration = 64, .waveFreq = { 2556, 0 }, 4, 0 },
271                         { .duration = 20, .waveFreq = { 2091, 0 }, 0, 0 },
272                         { .duration = 0,  .waveFreq = { 0 }      , 0, 0 } },
273           .repeatCnt = 0,
274           .repeatSegment = 0 },                             // TONE_CDMA_CALL_SIGNAL_ISDN_PING_RING
275         { .segments = { { .duration = 0,  .waveFreq = { 0 }, 0, 0} },
276           .repeatCnt = 0,
277           .repeatSegment = 0 },                             // TONE_CDMA_CALL_SIGNAL_ISDN_PAT5
278         { .segments = { { .duration = 0,  .waveFreq = { 0 }, 0, 0} },
279           .repeatCnt = 0,
280           .repeatSegment = 0 },                             // TONE_CDMA_CALL_SIGNAL_ISDN_PAT6
281         { .segments = { { .duration = 0,  .waveFreq = { 0 }, 0, 0} },
282           .repeatCnt = 0,
283           .repeatSegment = 0 },                             // TONE_CDMA_CALL_SIGNAL_ISDN_PAT7
284 
285         { .segments = { { .duration = 25, .waveFreq = { 3700, 0 }, 0, 0 },
286                         { .duration = 25, .waveFreq = { 4000, 0 }, 39, 0 },
287                         { .duration = 4000, .waveFreq = { 0 },     0, 0 },
288                         { .duration = 0,    .waveFreq = { 0 },     0, 0 } },
289           .repeatCnt = ToneGenerator::TONEGEN_INF,
290           .repeatSegment = 0 },                           // TONE_CDMA_HIGH_L
291         { .segments = { { .duration = 25, .waveFreq = { 2600, 0 }, 0, 0 },
292                         { .duration = 25, .waveFreq = { 2900, 0 }, 39, 0 },
293                         { .duration = 4000, .waveFreq = { 0 },     0, 0 },
294                         { .duration = 0,    .waveFreq = { 0 },     0, 0 } },
295           .repeatCnt = ToneGenerator::TONEGEN_INF,
296           .repeatSegment = 0 },                           // TONE_CDMA_MED_L
297         { .segments = { { .duration = 25, .waveFreq = { 1300, 0 }, 0, 0 },
298                         { .duration = 25, .waveFreq = { 1450, 0 }, 39, 0 },
299                         { .duration = 4000, .waveFreq = { 0 },     0, 0 },
300                         { .duration = 0,    .waveFreq = { 0 },     0, 0 } },
301           .repeatCnt = ToneGenerator::TONEGEN_INF,
302           .repeatSegment = 0 },                           // TONE_CDMA_LOW_L
303         { .segments = { { .duration = 25, .waveFreq = { 3700, 0 }, 0, 0 },
304                         { .duration = 25, .waveFreq = { 4000, 0 }, 15, 0 },
305                         { .duration = 400, .waveFreq = { 0 }, 0, 0 },
306                         { .duration = 0, .waveFreq = { 0 }, 0, 0 } },
307           .repeatCnt = ToneGenerator::TONEGEN_INF,
308           .repeatSegment = 0 },                           // TONE_CDMA_HIGH_SS
309         { .segments = { { .duration = 25, .waveFreq = { 2600, 0 }, 0, 0 },
310                         { .duration = 25, .waveFreq = { 2900, 0 }, 15, 0 },
311                         { .duration = 400, .waveFreq = { 0 }, 0, 0 },
312                         { .duration = 0, .waveFreq = { 0 }, 0, 0 }},
313           .repeatCnt = ToneGenerator::TONEGEN_INF,
314           .repeatSegment = 0 },                           // TONE_CDMA_MED_SS
315         { .segments = { { .duration = 25, .waveFreq = { 1300, 0 }, 0, 0 },
316                         { .duration = 25, .waveFreq = { 1450, 0 }, 15, 0 },
317                         { .duration = 400, .waveFreq = { 0 }, 0, 0 },
318                         { .duration = 0, .waveFreq = { 0 }, 0, 0 }},
319           .repeatCnt = ToneGenerator::TONEGEN_INF,
320           .repeatSegment = 0 },                           // TONE_CDMA_LOW_SS
321         { .segments = { { .duration = 25, .waveFreq = { 3700, 0 }, 0, 0 },
322                         { .duration = 25, .waveFreq = { 4000, 0 }, 7, 0 },
323                         { .duration = 200, .waveFreq = { 0 }, 0, 0 },
324                         { .duration = 25, .waveFreq = { 3700, 0 }, 0, 0 },
325                         { .duration = 25, .waveFreq = { 4000, 0 }, 7, 3 },
326                         { .duration = 200, .waveFreq = { 0 }, 0, 0 },
327                         { .duration = 25, .waveFreq = { 3700, 0 }, 0, 0 },
328                         { .duration = 25, .waveFreq = { 4000, 0 }, 15, 6 },
329                         { .duration = 4000, .waveFreq = { 0 }, 0, 0 },
330                         { .duration = 0, .waveFreq = { 0 }, 0, 0 }},
331           .repeatCnt = ToneGenerator::TONEGEN_INF,
332           .repeatSegment = 0 },                           // TONE_CDMA_HIGH_SSL
333         { .segments = { { .duration = 25, .waveFreq = { 2600, 0 }, 0, 0 },
334                         { .duration = 25, .waveFreq = { 2900, 0 }, 7, 0 },
335                         { .duration = 200, .waveFreq = { 0 }, 0, 0 },
336                         { .duration = 25, .waveFreq = { 2600, 0 }, 0, 0 },
337                         { .duration = 25, .waveFreq = { 2900, 0 }, 7, 3 },
338                         { .duration = 200, .waveFreq = { 0 }, 0, 0 },
339                         { .duration = 25, .waveFreq = { 2600, 0 }, 0, 0 },
340                         { .duration = 25, .waveFreq = { 2900, 0 }, 15, 6 },
341                         { .duration = 4000, .waveFreq = { 0 }, 0, 0 },
342                         { .duration = 0, .waveFreq = { 0 }, 0, 0 }},
343           .repeatCnt = ToneGenerator::TONEGEN_INF,
344           .repeatSegment = 0 },                           // TONE_CDMA_MED_SSL
345         { .segments = { { .duration = 25, .waveFreq = { 1300, 0 }, 0, 0 },
346                         { .duration = 25, .waveFreq = { 1450, 0 }, 7, 0 },
347                         { .duration = 200, .waveFreq = { 0 }, 0, 0 },
348                         { .duration = 25, .waveFreq = { 1300, 0 }, 0, 0 },
349                         { .duration = 25, .waveFreq = { 1450, 0 }, 7, 3 },
350                         { .duration = 200, .waveFreq = { 0 }, 0, 0 },
351                         { .duration = 25, .waveFreq = { 1300, 0 }, 0, 0 },
352                         { .duration = 25, .waveFreq = { 1450, 0 }, 15, 6 },
353                         { .duration = 4000, .waveFreq = { 0 }, 0, 0 },
354                         { .duration = 0, .waveFreq = { 0 }, 0, 0 }},
355           .repeatCnt = ToneGenerator::TONEGEN_INF,
356           .repeatSegment = 0 },                           // TONE_CDMA_LOW_SSL
357         { .segments = { { .duration = 25, .waveFreq = { 3700, 0 }, 0, 0 },
358                           { .duration = 25, .waveFreq = { 4000, 0 }, 19, 0 },
359                         { .duration = 1000, .waveFreq = { 0 }, 0, 0 },
360                         { .duration = 25, .waveFreq = { 3700, 0 }, 0, 0 },
361                         { .duration = 25, .waveFreq = { 4000, 0 }, 19, 3 },
362                         { .duration = 3000, .waveFreq = { 0 }, 0, 0 },
363                         { .duration = 0, .waveFreq = { 0 }, 0, 0 }},
364           .repeatCnt = ToneGenerator::TONEGEN_INF,
365           .repeatSegment = 0 },                           // TONE_CDMA_HIGH_SS_2
366         { .segments = { { .duration = 25, .waveFreq = { 2600, 0 }, 0, 0 },
367                         { .duration = 25, .waveFreq = { 2900, 0 }, 19, 0 },
368                         { .duration = 1000, .waveFreq = { 0 }, 0, 0 },
369                         { .duration = 25, .waveFreq = { 2600, 0 }, 0, 0 },
370                         { .duration = 25, .waveFreq = { 2900, 0 }, 19, 3 },
371                         { .duration = 3000, .waveFreq = { 0 }, 0, 0 },
372                         { .duration = 0, .waveFreq = { 0 }, 0, 0 }},
373           .repeatCnt = ToneGenerator::TONEGEN_INF,
374           .repeatSegment = 0 },                           // TONE_CDMA_MED_SS_2
375         { .segments = { { .duration = 25, .waveFreq = { 1300, 0 }, 0, 0 },
376                         { .duration = 25, .waveFreq = { 1450, 0 }, 19, 0 },
377                         { .duration = 1000, .waveFreq = { 0 }, 0, 0 },
378                         { .duration = 25, .waveFreq = { 1300, 0 }, 0, 0 },
379                         { .duration = 25, .waveFreq = { 1450, 0 }, 19, 3 },
380                         { .duration = 3000, .waveFreq = { 0 }, 0, 0 },
381                         { .duration = 0, .waveFreq = { 0 }, 0, 0 }},
382           .repeatCnt = ToneGenerator::TONEGEN_INF,
383           .repeatSegment = 0 },                           // TONE_CDMA_LOW_SS_2
384         { .segments = { { .duration = 25, .waveFreq = { 3700, 0 }, 0, 0 },
385                         { .duration = 25, .waveFreq = { 4000, 0 }, 9, 0 },
386                         { .duration = 500, .waveFreq = { 0 }, 0, 0 },
387                         { .duration = 25, .waveFreq = { 3700, 0 }, 0, 0 },
388                         { .duration = 25, .waveFreq = { 4000, 0 }, 19, 3 },
389                         { .duration = 500, .waveFreq = { 0 }, 0, 0 },
390                         { .duration = 25, .waveFreq = { 3700, 0 }, 0, 0 },
391                         { .duration = 25, .waveFreq = { 4000, 0 }, 9, 6 },
392                         { .duration = 3000, .waveFreq = { 0 }, 0, 0 },
393                         { .duration = 0, .waveFreq = { 0 }, 0, 0 }},
394           .repeatCnt = ToneGenerator::TONEGEN_INF,
395           .repeatSegment = 0 },                           // TONE_CDMA_HIGH_SLS
396         { .segments = { { .duration = 25, .waveFreq = { 2600, 0 }, 0, 0 },
397                         { .duration = 25, .waveFreq = { 2900, 0 }, 9, 0 },
398                         { .duration = 500, .waveFreq = { 0 }, 0, 0 },
399                         { .duration = 25, .waveFreq = { 2600, 0 }, 0, 0 },
400                         { .duration = 25, .waveFreq = { 2900, 0 }, 19, 3 },
401                         { .duration = 500, .waveFreq = { 0 }, 0, 0 },
402                         { .duration = 25, .waveFreq = { 2600, 0 }, 0, 0 },
403                         { .duration = 25, .waveFreq = { 2900, 0 }, 9, 6 },
404                         { .duration = 3000, .waveFreq = { 0 }, 0, 0 },
405                         { .duration = 0, .waveFreq = { 0 }, 0, 0 }},
406           .repeatCnt = ToneGenerator::TONEGEN_INF,
407           .repeatSegment = 0 },                           // TONE_CDMA_MED_SLS
408         { .segments = { { .duration = 25, .waveFreq = { 1300, 0 }, 0, 0 },
409                         { .duration = 25, .waveFreq = { 1450, 0 }, 9, 0 },
410                         { .duration = 500, .waveFreq = { 0 }, 0, 0 },
411                         { .duration = 25, .waveFreq = { 1300, 0 }, 0, 0 },
412                         { .duration = 25, .waveFreq = { 1450, 0 }, 19, 3 },
413                         { .duration = 500, .waveFreq = { 0 }, 0, 0 },
414                         { .duration = 25, .waveFreq = { 1300, 0 }, 0, 0 },
415                         { .duration = 25, .waveFreq = { 1450, 0 }, 9, 6 },
416                         { .duration = 3000, .waveFreq = { 0 }, 0, 0 },
417                         { .duration = 0, .waveFreq = { 0 }, 0, 0 }},
418           .repeatCnt = ToneGenerator::TONEGEN_INF,
419           .repeatSegment = 0 },                           // TONE_CDMA_LOW_SLS
420         { .segments = { { .duration = 25, .waveFreq = { 3700, 0 }, 0, 0 },
421                         { .duration = 25, .waveFreq = { 4000, 0 }, 9, 0 },
422                         { .duration = 500, .waveFreq = { 0 }, 0, 0 },
423                         { .duration = 25, .waveFreq = { 3700, 0 }, 0, 0 },
424                         { .duration = 25, .waveFreq = { 4000, 0 }, 9, 3 },
425                         { .duration = 500, .waveFreq = { 0 }, 0, 0 },
426                         { .duration = 25, .waveFreq = { 3700, 0 }, 0, 0 },
427                         { .duration = 25, .waveFreq = { 4000, 0 }, 9, 6 },
428                         { .duration = 500, .waveFreq = { 0 }, 0, 0 },
429                         { .duration = 25, .waveFreq = { 3700, 0 }, 0, 0 },
430                         { .duration = 25, .waveFreq = { 4000, 0 }, 9, 9 },
431                         { .duration = 2500, .waveFreq = { 0 }, 0, 0 },
432                         { .duration = 0, .waveFreq = { 0 }, 0, 0 }},
433           .repeatCnt = ToneGenerator::TONEGEN_INF,
434           .repeatSegment = 0 },                           // TONE_CDMA_HIGH_S_X4
435         { .segments = { { .duration = 25, .waveFreq = { 2600, 0 }, 0, 0 },
436                         { .duration = 25, .waveFreq = { 2900, 0 }, 9, 0 },
437                         { .duration = 500, .waveFreq = { 0 }, 0, 0 },
438                         { .duration = 25, .waveFreq = { 2600, 0 }, 0, 0 },
439                         { .duration = 25, .waveFreq = { 2900, 0 }, 9, 3 },
440                         { .duration = 500, .waveFreq = { 0 }, 0, 0 },
441                         { .duration = 25, .waveFreq = { 2600, 0 }, 0, 0 },
442                         { .duration = 25, .waveFreq = { 2900, 0 }, 9, 6 },
443                         { .duration = 500, .waveFreq = { 0 }, 0, 0 },
444                         { .duration = 25, .waveFreq = { 2600, 0 }, 0, 0 },
445                         { .duration = 25, .waveFreq = { 2900, 0 }, 9, 9 },
446                         { .duration = 2500, .waveFreq = { 0 }, 0, 0 },
447                         { .duration = 0, .waveFreq = { 0 }, 0, 0 }},
448           .repeatCnt = ToneGenerator::TONEGEN_INF,
449           .repeatSegment = 0 },                           // TONE_CDMA_MED_S_X4
450         { .segments = { { .duration = 25, .waveFreq = { 1300, 0 }, 0, 0 },
451                         { .duration = 25, .waveFreq = { 1450, 0 }, 9, 0 },
452                         { .duration = 500, .waveFreq = { 0 }, 0, 0 },
453                         { .duration = 25, .waveFreq = { 1300, 0 }, 0, 0 },
454                         { .duration = 25, .waveFreq = { 1450, 0 }, 9, 3 },
455                         { .duration = 500, .waveFreq = { 0 }, 0, 0 },
456                         { .duration = 25, .waveFreq = { 1300, 0 }, 0, 0 },
457                         { .duration = 25, .waveFreq = { 1450, 0 }, 9, 6 },
458                         { .duration = 500, .waveFreq = { 0 }, 0, 0 },
459                         { .duration = 25, .waveFreq = { 1300, 0 }, 0, 0 },
460                         { .duration = 25, .waveFreq = { 1450, 0 }, 9, 9 },
461                         { .duration = 2500, .waveFreq = { 0 }, 0, 0 },
462                         { .duration = 0, .waveFreq = { 0 }, 0, 0 }},
463           .repeatCnt = ToneGenerator::TONEGEN_INF,
464           .repeatSegment = 0 },                           // TONE_CDMA_LOW_S_X4
465         { .segments = { { .duration = 25, .waveFreq = { 3700, 0 }, 0, 0 },
466                         { .duration = 25, .waveFreq = { 4000, 0 }, 19, 0 },
467                         { .duration = 2000, .waveFreq = { 0 }, 0, 0 },
468                         { .duration = 0, .waveFreq = { 0 }, 0, 0 }},
469           .repeatCnt = ToneGenerator::TONEGEN_INF,
470           .repeatSegment = 0 },                           // TONE_CDMA_HIGH_PBX_L
471         { .segments = { { .duration = 25, .waveFreq = { 2600, 0 }, 0, 0 },
472                         { .duration = 25, .waveFreq = { 2900, 0 }, 19, 0 },
473                         { .duration = 2000, .waveFreq = { 0 }, 0, 0 },
474                         { .duration = 0, .waveFreq = { 0 }, 0, 0 }},
475           .repeatCnt = ToneGenerator::TONEGEN_INF,
476           .repeatSegment = 0 },                           // TONE_CDMA_MED_PBX_L
477         { .segments = { { .duration = 25, .waveFreq = { 1300, 0 }, 0, 0 },
478                         { .duration = 25, .waveFreq = { 1450, 0 }, 19, 0 },
479                         { .duration = 2000, .waveFreq = { 0 }, 0, 0 },
480                         { .duration = 0, .waveFreq = { 0 }, 0, 0 }},
481           .repeatCnt = ToneGenerator::TONEGEN_INF,
482           .repeatSegment = 0 },                           // TONE_CDMA_LOW_PBX_L
483         { .segments = { { .duration = 25, .waveFreq = { 3700, 0 }, 0, 0 },
484                         { .duration = 25, .waveFreq = { 4000, 0 }, 7, 0 },
485                         { .duration = 200, .waveFreq = { 0 }, 0, 0 },
486                         { .duration = 25, .waveFreq = { 3700, 0 }, 0, 0 },
487                         { .duration = 25, .waveFreq = { 4000, 0 }, 7, 3 },
488                         { .duration = 2000, .waveFreq = { 0 }, 0, 0 },
489                         { .duration = 0, .waveFreq = { 0 }, 0, 0 }},
490           .repeatCnt = ToneGenerator::TONEGEN_INF,
491           .repeatSegment = 0 },                           // TONE_CDMA_HIGH_PBX_SS
492         { .segments = { { .duration = 25, .waveFreq = { 2600, 0 }, 0, 0 },
493                         { .duration = 25, .waveFreq = { 2900, 0 }, 7, 0 },
494                         { .duration = 200, .waveFreq = { 0 }, 0, 0 },
495                         { .duration = 25, .waveFreq = { 2600, 0 }, 0, 0 },
496                         { .duration = 25, .waveFreq = { 2900, 0 }, 7, 3 },
497                         { .duration = 2000, .waveFreq = { 0 }, 0, 0 },
498                         { .duration = 0, .waveFreq = { 0 }, 0, 0 }},
499           .repeatCnt = ToneGenerator::TONEGEN_INF,
500           .repeatSegment = 0 },                           // TONE_CDMA_MED_PBX_SS
501         { .segments = { { .duration = 25, .waveFreq = { 1300, 0 }, 0, 0 },
502                         { .duration = 25, .waveFreq = { 1450, 0 }, 7, 0 },
503                         { .duration = 200, .waveFreq = { 0 }, 0, 0 },
504                         { .duration = 25, .waveFreq = { 1300, 0 }, 0, 0 },
505                         { .duration = 25, .waveFreq = { 1450, 0 }, 7, 3 },
506                         { .duration = 2000, .waveFreq = { 0 }, 0, 0 },
507                         { .duration = 0, .waveFreq = { 0 }, 0, 0 }},
508           .repeatCnt = ToneGenerator::TONEGEN_INF,
509           .repeatSegment = 0 },                           // TONE_CDMA_LOW_PBX_SS
510         { .segments = { { .duration = 25, .waveFreq = { 3700, 0 }, 0, 0 },
511                         { .duration = 25, .waveFreq = { 4000, 0 }, 7, 0 },
512                         { .duration = 200, .waveFreq = { 0 }, 0, 0 },
513                         { .duration = 25, .waveFreq = { 3700, 0 }, 0, 0 },
514                         { .duration = 25, .waveFreq = { 4000, 0 }, 7, 3 },
515                         { .duration = 200, .waveFreq = { 0 }, 0, 0 },
516                         { .duration = 25, .waveFreq = { 3700, 0 }, 0, 0 },
517                         { .duration = 25, .waveFreq = { 4000, 0 }, 15, 6 },
518                         { .duration = 1000, .waveFreq = { 0 }, 0, 0 },
519                         { .duration = 0, .waveFreq = { 0 }, 0, 0 }},
520           .repeatCnt = ToneGenerator::TONEGEN_INF,
521           .repeatSegment = 0 },                           // TONE_CDMA_HIGH_PBX_SSL
522         { .segments = { { .duration = 25, .waveFreq = { 2600, 0 }, 0, 0 },
523                         { .duration = 25, .waveFreq = { 2900, 0 }, 7, 0 },
524                         { .duration = 200, .waveFreq = { 0 }, 0, 0 },
525                         { .duration = 25, .waveFreq = { 2600, 0 }, 0, 0 },
526                         { .duration = 25, .waveFreq = { 2900, 0 }, 7, 3 },
527                         { .duration = 200, .waveFreq = { 0 }, 0, 0 },
528                         { .duration = 25, .waveFreq = { 2600, 0 }, 0, 0 },
529                         { .duration = 25, .waveFreq = { 2900, 0 }, 15, 6 },
530                         { .duration = 1000, .waveFreq = { 0 }, 0, 0 },
531                         { .duration = 0, .waveFreq = { 0 }, 0, 0 }},
532           .repeatCnt = ToneGenerator::TONEGEN_INF,
533           .repeatSegment = 0 },                           // TONE_CDMA_MED_PBX_SSL
534         { .segments = { { .duration = 25, .waveFreq = { 1300, 0 }, 0, 0 },
535                         { .duration = 25, .waveFreq = { 1450, 0 }, 7, 0 },
536                         { .duration = 200, .waveFreq = { 0 }, 0, 0 },
537                         { .duration = 25, .waveFreq = { 1300, 0 }, 0, 0 },
538                         { .duration = 25, .waveFreq = { 1450, 0 }, 7, 3 },
539                         { .duration = 200, .waveFreq = { 0 }, 0, 0 },
540                         { .duration = 25, .waveFreq = { 1300, 0 }, 0, 0 },
541                         { .duration = 25, .waveFreq = { 1450, 0 }, 15, 6 },
542                         { .duration = 1000, .waveFreq = { 0 }, 0, 0 },
543                         { .duration = 0, .waveFreq = { 0 }, 0, 0 }},
544           .repeatCnt = ToneGenerator::TONEGEN_INF,
545           .repeatSegment = 0 },                           // TONE_CDMA_LOW_PBX_SSL
546         { .segments = { { .duration = 25, .waveFreq = { 3700, 0 }, 0, 0 },
547                         { .duration = 25, .waveFreq = { 4000, 0 }, 7, 0 },
548                         { .duration = 200, .waveFreq = { 0 }, 0, 0 },
549                         { .duration = 25, .waveFreq = { 3700, 0 }, 0, 0 },
550                         { .duration = 25, .waveFreq = { 4000, 0 }, 15, 3 },
551                         { .duration = 200, .waveFreq = { 0 }, 0, 0 },
552                         { .duration = 25, .waveFreq = { 3700, 0 }, 0, 0 },
553                         { .duration = 25, .waveFreq = { 4000, 0 }, 7, 6 },
554                         { .duration = 1000, .waveFreq = { 0 }, 0, 0 },
555                         { .duration = 0, .waveFreq = { 0 }, 0, 0 }},
556           .repeatCnt = ToneGenerator::TONEGEN_INF,
557           .repeatSegment = 0 },                           // TONE_CDMA_HIGH_PBX_SLS
558         { .segments = { { .duration = 25, .waveFreq = { 2600, 0 }, 0, 0 },
559                         { .duration = 25, .waveFreq = { 2900, 0 }, 7, 0 },
560                         { .duration = 200, .waveFreq = { 0 }, 0, 0 },
561                         { .duration = 25, .waveFreq = { 2600, 0 }, 0, 0 },
562                         { .duration = 25, .waveFreq = { 2900, 0 }, 15, 3 },
563                         { .duration = 200, .waveFreq = { 0 }, 0, 0 },
564                         { .duration = 25, .waveFreq = { 2600, 0 }, 0, 0 },
565                         { .duration = 25, .waveFreq = { 2900, 0 }, 7, 6 },
566                         { .duration = 1000, .waveFreq = { 0 }, 0, 0 },
567                         { .duration = 0, .waveFreq = { 0 }, 0, 0 }},
568           .repeatCnt = ToneGenerator::TONEGEN_INF,
569           .repeatSegment = 0 },                           // TONE_CDMA_MED_PBX_SLS
570         { .segments = { { .duration = 25, .waveFreq = { 1300, 0 }, 0, 0 },
571                         { .duration = 25, .waveFreq = { 1450, 0 }, 7, 0 },
572                         { .duration = 200, .waveFreq = { 0 }, 0, 0 },
573                         { .duration = 25, .waveFreq = { 1300, 0 }, 0, 0 },
574                         { .duration = 25, .waveFreq = { 1450, 0 }, 15, 3 },
575                         { .duration = 200, .waveFreq = { 0 }, 0, 0 },
576                         { .duration = 25, .waveFreq = { 1300, 0 }, 0, 0 },
577                         { .duration = 25, .waveFreq = { 1450, 0 }, 7, 6 },
578                         { .duration = 1000, .waveFreq = { 0 }, 0, 0 },
579                         { .duration = 0, .waveFreq = { 0 }, 0, 0 }},
580            .repeatCnt = ToneGenerator::TONEGEN_INF,
581           .repeatSegment = 0 },                           // TONE_CDMA_LOW_PBX_SLS
582         { .segments = { { .duration = 25, .waveFreq = { 3700, 0 }, 0, 0 },
583                         { .duration = 25, .waveFreq = { 4000, 0 }, 7, 0 },
584                         { .duration = 200, .waveFreq = { 0 }, 0, 0 },
585                         { .duration = 25, .waveFreq = { 3700, 0 }, 0, 0 },
586                         { .duration = 25, .waveFreq = { 4000, 0 }, 7, 3 },
587                         { .duration = 200, .waveFreq = { 0 }, 0, 0 },
588                         { .duration = 25, .waveFreq = { 3700, 0 }, 0, 0 },
589                         { .duration = 25, .waveFreq = { 4000, 0 }, 7, 6 },
590                         { .duration = 200, .waveFreq = { 0 }, 0, 0 },
591                         { .duration = 25, .waveFreq = { 3700, 0 }, 0, 0 },
592                         { .duration = 25, .waveFreq = { 4000, 0 }, 7, 9 },
593                         { .duration = 800, .waveFreq = { 0 }, 0, 0 },
594                         { .duration = 0, .waveFreq = { 0 }, 0, 0 }},
595           .repeatCnt = ToneGenerator::TONEGEN_INF,
596           .repeatSegment = 0 },                           // TONE_CDMA_HIGH_PBX_S_X4
597         { .segments = { { .duration = 25, .waveFreq = { 2600, 0 }, 0, 0 },
598                         { .duration = 25, .waveFreq = { 2900, 0 }, 7, 0 },
599                         { .duration = 200, .waveFreq = { 0 }, 0, 0 },
600                         { .duration = 25, .waveFreq = { 2600, 0 }, 0, 0 },
601                         { .duration = 25, .waveFreq = { 2900, 0 }, 7, 3 },
602                         { .duration = 200, .waveFreq = { 0 }, 0, 0 },
603                         { .duration = 25, .waveFreq = { 2600, 0 }, 0, 0 },
604                         { .duration = 25, .waveFreq = { 2900, 0 }, 7, 6 },
605                         { .duration = 200, .waveFreq = { 0 }, 0, 0 },
606                         { .duration = 25, .waveFreq = { 2600, 0 }, 0, 0 },
607                         { .duration = 25, .waveFreq = { 2900, 0 }, 7, 9 },
608                         { .duration = 800, .waveFreq = { 0 }, 0, 0 },
609                         { .duration = 0, .waveFreq = { 0 }, 0, 0 }},
610           .repeatCnt = ToneGenerator::TONEGEN_INF,
611           .repeatSegment = 0 },                           // TONE_CDMA_MED_PBX_S_X4
612         { .segments = { { .duration = 25, .waveFreq = { 1300, 0 }, 0, 0 },
613                         { .duration = 25, .waveFreq = { 1450, 0 }, 7, 0 },
614                         { .duration = 200, .waveFreq = { 0 }, 0, 0 },
615                         { .duration = 25, .waveFreq = { 1300, 0 }, 0, 0 },
616                         { .duration = 25, .waveFreq = { 1450, 0 }, 7, 3 },
617                         { .duration = 200, .waveFreq = { 0 }, 0, 0 },
618                         { .duration = 25, .waveFreq = { 1300, 0 }, 0, 0 },
619                         { .duration = 25, .waveFreq = { 1450, 0 }, 7, 6 },
620                         { .duration = 200, .waveFreq = { 0 }, 0, 0 },
621                         { .duration = 25, .waveFreq = { 1300, 0 }, 0, 0 },
622                         { .duration = 25, .waveFreq = { 1450, 0 }, 7, 9 },
623                         { .duration = 800, .waveFreq = { 0 }, 0, 0 },
624                         { .duration = 0, .waveFreq = { 0 }, 0, 0 }},
625           .repeatCnt = ToneGenerator::TONEGEN_INF,
626           .repeatSegment = 0 },                           // TONE_CDMA_LOW_PBX_S_X4
627 
628         { .segments = { { .duration = 62, .waveFreq = { 1109, 0 }, 0, 0 },
629                         { .duration = 62, .waveFreq = { 784, 0 },  0, 0 },
630                         { .duration = 62, .waveFreq = { 740, 0 },  0, 0 },
631                         { .duration = 62, .waveFreq = { 622, 0 },  0, 0 },
632                         { .duration = 62, .waveFreq = { 1109, 0 }, 0, 0 },
633                         { .duration = 0,  .waveFreq = { 0 },       0, 0 } },
634           .repeatCnt = 0,
635           .repeatSegment = 0 },                            // TONE_CDMA_ALERT_NETWORK_LITE
636         { .segments = { { .duration = 62, .waveFreq = { 1245, 0 }, 0, 0 },
637                         { .duration = 62, .waveFreq = { 659, 0 },  2, 0 },
638                         { .duration = 62, .waveFreq = { 1245, 0 }, 0, 0 },
639                         { .duration = 0,  .waveFreq = { 0 },       0, 0 } },
640           .repeatCnt = 0,
641           .repeatSegment = 0 },                            // TONE_CDMA_ALERT_AUTOREDIAL_LITE
642         { .segments = { { .duration = 400, .waveFreq = { 1150, 770, 0 }, 0, 0 },
643                         { .duration = 0,   .waveFreq = { 0 },            0, 0 } },
644           .repeatCnt = 0,
645           .repeatSegment = 0 },                            // TONE_CDMA_ONE_MIN_BEEP
646         { .segments = { { .duration = 120, .waveFreq = { 941, 1477, 0 }, 0, 0 },
647                         { .duration = 0,   .waveFreq = { 0 },            0, 0 } },
648           .repeatCnt = 0,
649           .repeatSegment = 0 },                            // TONE_CDMA_KEYPAD_VOLUME_KEY_LITE
650         { .segments = { { .duration = 375, .waveFreq = { 587, 0 }, 0, 0 },
651                         { .duration = 125, .waveFreq = { 1175, 0 }, 0, 0 },
652                         { .duration = 0,   .waveFreq = { 0 },       0, 0 } },
653           .repeatCnt = 0,
654           .repeatSegment = 0 },                            // TONE_CDMA_PRESSHOLDKEY_LITE
655         { .segments = { { .duration = 62, .waveFreq = { 587, 0 }, 0, 0 },
656                         { .duration = 62, .waveFreq = { 784, 0 }, 0, 0 },
657                         { .duration = 62, .waveFreq = { 831, 0 }, 0, 0 },
658                         { .duration = 62, .waveFreq = { 784, 0 }, 0, 0 },
659                         { .duration = 62, .waveFreq = { 1109, 0 }, 0, 0 },
660                         { .duration = 62, .waveFreq = { 784, 0 }, 0, 0 },
661                         { .duration = 62, .waveFreq = { 831, 0 }, 0, 0 },
662                         { .duration = 62, .waveFreq = { 784, 0 }, 0, 0 },
663                         { .duration = 0,  .waveFreq = { 0 },      0, 0 } },
664           .repeatCnt = 0,
665           .repeatSegment = 0 },                             // TONE_CDMA_ALERT_INCALL_LITE
666         { .segments = { { .duration = 125, .waveFreq = { 941, 0 }, 0, 0 },
667                         { .duration = 10,  .waveFreq = { 0 },      2, 0 },
668                         { .duration = 4990, .waveFreq = { 0 },     0, 0 },
669                         { .duration = 0,    .waveFreq = { 0 },     0, 0 } },
670           .repeatCnt = ToneGenerator::TONEGEN_INF,
671           .repeatSegment = 0 },                            // TONE_CDMA_EMERGENCY_RINGBACK
672         { .segments = { { .duration = 125, .waveFreq = { 1319, 0 }, 0, 0 },
673                         { .duration = 125, .waveFreq = { 0 },       0, 0 },
674                         { .duration = 0,   .waveFreq = { 0 },       0, 0 } },
675           .repeatCnt = 2,
676           .repeatSegment = 0 },                            // TONE_CDMA_ALERT_CALL_GUARD
677         { .segments = { { .duration = 125, .waveFreq = { 1047, 0 }, 0, 0 },
678                         { .duration = 125, .waveFreq = { 370,  0 }, 0, 0 },
679                         { .duration = 0,   .waveFreq = { 0 },       0, 0 } },
680           .repeatCnt = 0,
681           .repeatSegment = 0 },                            // TONE_CDMA_SOFT_ERROR_LITE
682         { .segments = { { .duration = 125, .waveFreq = { 1480, 0 }, 0, 0 },
683                         { .duration = 125, .waveFreq = { 1397, 0 }, 0, 0 },
684                         { .duration = 125, .waveFreq = { 784, 0 },  0, 0 },
685                         { .duration = 0,   .waveFreq = { 0 },       0, 0 } },
686           .repeatCnt = 0,
687           .repeatSegment = 0 },                            // TONE_CDMA_CALLDROP_LITE
688 
689         { .segments = { { .duration = 500, .waveFreq = { 425, 0 }, 0, 0 },
690                         { .duration = 500, .waveFreq = { 0 }, 0, 0 },
691                         { .duration = 0, .waveFreq = { 0 }, 0, 0 }},
692           .repeatCnt = 0,
693           .repeatSegment = 0 },                           // TONE_CDMA_NETWORK_BUSY_ONE_SHOT
694         { .segments = { { .duration = 400, .waveFreq = { 1150, 770 }, 0, 0 },
695                         { .duration = 0, .waveFreq = { 0 }, 0, 0 }},
696           .repeatCnt = 0,
697           .repeatSegment = 0 },                           // TONE_CDMA_ABBR_ALERT
698           { .segments = { { .duration = 0, .waveFreq = { 0 }, 0, 0 }},
699           .repeatCnt = 0,
700           .repeatSegment = 0 },                            // TONE_CDMA_SIGNAL_OFF
701 
702         { .segments = { { .duration = ToneGenerator::TONEGEN_INF, .waveFreq = { 350, 440, 0 }, 0, 0 },
703                         { .duration = 0 , .waveFreq = { 0 }, 0, 0}},
704           .repeatCnt = ToneGenerator::TONEGEN_INF,
705           .repeatSegment = 0 },                              // TONE_ANSI_DIAL
706         { .segments = { { .duration = 500, .waveFreq = { 480, 620, 0 }, 0, 0 },
707                         { .duration = 500, .waveFreq = { 0 }, 0, 0 },
708                         { .duration = 0 , .waveFreq = { 0 }, 0, 0}},
709           .repeatCnt = ToneGenerator::TONEGEN_INF,
710           .repeatSegment = 0 },                              // TONE_ANSI_BUSY
711         { .segments = { { .duration = 250, .waveFreq = { 480, 620, 0 }, 0, 0 },
712                         { .duration = 250, .waveFreq = { 0 }, 0, 0 },
713                         { .duration = 0 , .waveFreq = { 0 }, 0, 0}},
714           .repeatCnt = ToneGenerator::TONEGEN_INF,
715           .repeatSegment = 0 },                              // TONE_ANSI_CONGESTION
716         { .segments = { { .duration = 300, .waveFreq = { 440, 0 }, 0, 0 },
717                         { .duration = 9700, .waveFreq = { 0 }, 0, 0 },
718                         { .duration = 100, .waveFreq = { 440, 0 }, 0, 0 },
719                         { .duration = 100, .waveFreq = { 0 }, 0, 0 },
720                         { .duration = 100, .waveFreq = { 440, 0 }, 0, 0 },
721                         { .duration = 0 , .waveFreq = { 0 }, 0, 0}},
722           .repeatCnt = ToneGenerator::TONEGEN_INF,
723           .repeatSegment = 1 },                              // TONE_ANSI_CALL_WAITING
724         { .segments = { { .duration = 2000, .waveFreq = { 440, 480, 0 }, 0, 0 },
725                         { .duration = 4000, .waveFreq = { 0 }, 0, 0 },
726                         { .duration = 0 , .waveFreq = { 0 }, 0, 0}},
727           .repeatCnt = ToneGenerator::TONEGEN_INF,
728           .repeatSegment = 0 },                              // TONE_ANSI_RINGTONE
729         { .segments = { { .duration = ToneGenerator::TONEGEN_INF, .waveFreq = { 400, 0 }, 0, 0 },
730                         { .duration = 0 , .waveFreq = { 0 }, 0, 0}},
731           .repeatCnt = ToneGenerator::TONEGEN_INF,
732           .repeatSegment = 0 },                              // TONE_JAPAN_DIAL
733         { .segments = { { .duration = 500, .waveFreq = { 400, 0 }, 0, 0 },
734                         { .duration = 500, .waveFreq = { 0 }, 0, 0 },
735                         { .duration = 0 , .waveFreq = { 0 }, 0, 0}},
736           .repeatCnt = ToneGenerator::TONEGEN_INF,
737           .repeatSegment = 0 },                              // TONE_JAPAN_BUSY
738         { .segments = { { .duration = 1000, .waveFreq = { 400, 0 }, 0, 0 },
739                         { .duration = 2000, .waveFreq = { 0 }, 0, 0 },
740                         { .duration = 0 , .waveFreq = { 0 }, 0, 0}},
741           .repeatCnt = ToneGenerator::TONEGEN_INF,
742           .repeatSegment = 0 },                              // TONE_JAPAN_RADIO_ACK
743         { .segments = { { .duration = 400, .waveFreq = { 400, 450, 0 }, 0, 0 },
744                         { .duration = 200, .waveFreq = { 0 }, 0, 0 },
745                         { .duration = 400, .waveFreq = { 400, 450, 0 }, 0, 0 },
746                         { .duration = 2000, .waveFreq = { 0 }, 0, 0},
747                         { .duration = 0, .waveFreq = { 0 }, 0, 0}},
748           .repeatCnt = ToneGenerator::TONEGEN_INF,
749           .repeatSegment = 0 },                              // TONE_UK_RINGTONE
750 
751 
752 
753 };
754 
755 // Used by ToneGenerator::getToneForRegion() to convert user specified supervisory tone type
756 // to actual tone for current region.
757 const unsigned char /*tone_type*/ ToneGenerator::sToneMappingTable[NUM_REGIONS-1][NUM_SUP_TONES] = {
758         {   // ANSI
759             TONE_ANSI_DIAL,             // TONE_SUP_DIAL
760             TONE_ANSI_BUSY,             // TONE_SUP_BUSY
761             TONE_ANSI_CONGESTION,       // TONE_SUP_CONGESTION
762             TONE_SUP_RADIO_ACK,         // TONE_SUP_RADIO_ACK
763             TONE_SUP_RADIO_NOTAVAIL,    // TONE_SUP_RADIO_NOTAVAIL
764             TONE_SUP_ERROR,             // TONE_SUP_ERROR
765             TONE_ANSI_CALL_WAITING,     // TONE_SUP_CALL_WAITING
766             TONE_ANSI_RINGTONE          // TONE_SUP_RINGTONE
767         },
768         {   // JAPAN
769             TONE_JAPAN_DIAL,             // TONE_SUP_DIAL
770             TONE_JAPAN_BUSY,             // TONE_SUP_BUSY
771             TONE_SUP_CONGESTION,         // TONE_SUP_CONGESTION
772             TONE_JAPAN_RADIO_ACK,        // TONE_SUP_RADIO_ACK
773             TONE_SUP_RADIO_NOTAVAIL,     // TONE_SUP_RADIO_NOTAVAIL
774             TONE_SUP_ERROR,              // TONE_SUP_ERROR
775             TONE_SUP_CALL_WAITING,       // TONE_SUP_CALL_WAITING
776             TONE_SUP_RINGTONE            // TONE_SUP_RINGTONE
777         },
778         {   // UK
779             TONE_SUP_DIAL,               // TONE_SUP_DIAL
780             TONE_SUP_BUSY,               // TONE_SUP_BUSY
781             TONE_SUP_CONGESTION,         // TONE_SUP_CONGESTION
782             TONE_SUP_RADIO_ACK,          // TONE_SUP_RADIO_ACK
783             TONE_SUP_RADIO_NOTAVAIL,     // TONE_SUP_RADIO_NOTAVAIL
784             TONE_SUP_ERROR,              // TONE_SUP_ERROR
785             TONE_SUP_CALL_WAITING,       // TONE_SUP_CALL_WAITING
786             TONE_UK_RINGTONE             // TONE_SUP_RINGTONE
787         }
788 
789 };
790 
791 
792 ////////////////////////////////////////////////////////////////////////////////
793 //                           ToneGenerator class Implementation
794 ////////////////////////////////////////////////////////////////////////////////
795 
796 
797 //---------------------------------- public methods ----------------------------
798 
799 
800 ////////////////////////////////////////////////////////////////////////////////
801 //
802 //    Method:        ToneGenerator::ToneGenerator()
803 //
804 //    Description:    Constructor. Initializes the tone sequencer, intantiates required sine wave
805 //        generators, instantiates output audio track.
806 //
807 //    Input:
808 //        streamType:        Type of stream used for tone playback
809 //        volume:            volume applied to tone (0.0 to 1.0)
810 //
811 //    Output:
812 //        none
813 //
814 ////////////////////////////////////////////////////////////////////////////////
ToneGenerator(audio_stream_type_t streamType,float volume,bool threadCanCallJava)815 ToneGenerator::ToneGenerator(audio_stream_type_t streamType, float volume, bool threadCanCallJava) {
816 
817     ALOGV("ToneGenerator constructor: streamType=%d, volume=%f", streamType, volume);
818 
819     mState = TONE_IDLE;
820 
821     if (AudioSystem::getOutputSamplingRate(&mSamplingRate, streamType) != NO_ERROR) {
822         ALOGE("Unable to marshal AudioFlinger");
823         return;
824     }
825     mThreadCanCallJava = threadCanCallJava;
826     mStreamType = streamType;
827     mVolume = volume;
828     mpToneDesc = NULL;
829     mpNewToneDesc = NULL;
830     // Generate tone by chunks of 20 ms to keep cadencing precision
831     mProcessSize = (mSamplingRate * 20) / 1000;
832 
833     char value[PROPERTY_VALUE_MAX];
834     property_get("gsm.operator.iso-country", value, "");
835     if (strcmp(value,"us") == 0 ||
836         strcmp(value,"ca") == 0) {
837         mRegion = ANSI;
838     } else if (strcmp(value,"jp") == 0) {
839         mRegion = JAPAN;
840     } else if (strcmp(value,"uk") == 0 ||
841                strcmp(value,"uk,uk") == 0) {
842         mRegion = UK;
843     } else {
844         mRegion = CEPT;
845     }
846 
847     if (initAudioTrack()) {
848         ALOGV("ToneGenerator INIT OK, time: %d", (unsigned int)(systemTime()/1000000));
849     } else {
850         ALOGV("!!!ToneGenerator INIT FAILED!!!");
851     }
852 }
853 
854 
855 
856 
857 ////////////////////////////////////////////////////////////////////////////////
858 //
859 //    Method:        ToneGenerator::~ToneGenerator()
860 //
861 //    Description:    Destructor. Stop sound playback and delete audio track if
862 //      needed and delete sine wave generators.
863 //
864 //    Input:
865 //        none
866 //
867 //    Output:
868 //        none
869 //
870 ////////////////////////////////////////////////////////////////////////////////
~ToneGenerator()871 ToneGenerator::~ToneGenerator() {
872     ALOGV("ToneGenerator destructor");
873 
874     if (mpAudioTrack != 0) {
875         stopTone();
876         ALOGV("Delete Track: %p", mpAudioTrack.get());
877         mpAudioTrack.clear();
878     }
879 }
880 
881 ////////////////////////////////////////////////////////////////////////////////
882 //
883 //    Method:        ToneGenerator::startTone()
884 //
885 //    Description:    Starts tone playback.
886 //
887 //    Input:
888 //        toneType:        Type of tone generated (values in enum tone_type)
889 //        durationMs:      The tone duration in milliseconds. If the tone is limited in time by definition,
890 //              the actual duration will be the minimum of durationMs and the defined tone duration.
891 //              Ommiting or setting durationMs to -1 does not limit tone duration.
892 //
893 //    Output:
894 //        none
895 //
896 ////////////////////////////////////////////////////////////////////////////////
startTone(tone_type toneType,int durationMs)897 bool ToneGenerator::startTone(tone_type toneType, int durationMs) {
898     bool lResult = false;
899     status_t lStatus;
900 
901     if ((toneType < 0) || (toneType >= NUM_TONES))
902         return lResult;
903 
904     toneType = getToneForRegion(toneType);
905     if (toneType == TONE_CDMA_SIGNAL_OFF) {
906         return true;
907     }
908 
909     if (mState == TONE_IDLE) {
910         ALOGV("startTone: try to re-init AudioTrack");
911         if (!initAudioTrack()) {
912             return lResult;
913         }
914     }
915 
916     ALOGV("startTone");
917 
918     mLock.lock();
919 
920     // Get descriptor for requested tone
921     mpNewToneDesc = &sToneDescriptors[toneType];
922 
923     mDurationMs = durationMs;
924 
925     if (mState == TONE_STOPPED) {
926         ALOGV("Start waiting for previous tone to stop");
927         lStatus = mWaitCbkCond.waitRelative(mLock, seconds(3));
928         if (lStatus != NO_ERROR) {
929             ALOGE("--- start wait for stop timed out, status %d", lStatus);
930             mState = TONE_IDLE;
931             mLock.unlock();
932             return lResult;
933         }
934     }
935 
936     if (mState == TONE_INIT) {
937         if (prepareWave()) {
938             ALOGV("Immediate start, time %d", (unsigned int)(systemTime()/1000000));
939             lResult = true;
940             mState = TONE_STARTING;
941             if (clock_gettime(CLOCK_MONOTONIC, &mStartTime) != 0) {
942                 mStartTime.tv_sec = 0;
943             }
944             mLock.unlock();
945             mpAudioTrack->start();
946             mLock.lock();
947             if (mState == TONE_STARTING) {
948                 ALOGV("Wait for start callback");
949                 lStatus = mWaitCbkCond.waitRelative(mLock, seconds(3));
950                 if (lStatus != NO_ERROR) {
951                     ALOGE("--- Immediate start timed out, status %d", lStatus);
952                     mState = TONE_IDLE;
953                     lResult = false;
954                 }
955             }
956         } else {
957             mState = TONE_IDLE;
958         }
959     } else {
960         ALOGV("Delayed start");
961         mState = TONE_RESTARTING;
962         mStartTime.tv_sec = 0;
963         lStatus = mWaitCbkCond.waitRelative(mLock, seconds(3));
964         if (lStatus == NO_ERROR) {
965             if (mState != TONE_IDLE) {
966                 lResult = true;
967             }
968             ALOGV("cond received");
969         } else {
970             ALOGE("--- Delayed start timed out, status %d", lStatus);
971             mState = TONE_IDLE;
972         }
973     }
974     mLock.unlock();
975 
976     ALOGV_IF(lResult, "Tone started, time %d", (unsigned int)(systemTime()/1000000));
977     ALOGW_IF(!lResult, "Tone start failed!!!, time %d", (unsigned int)(systemTime()/1000000));
978 
979     return lResult;
980 }
981 
982 ////////////////////////////////////////////////////////////////////////////////
983 //
984 //    Method:        ToneGenerator::stopTone()
985 //
986 //    Description:    Stops tone playback.
987 //
988 //    Input:
989 //        none
990 //
991 //    Output:
992 //        none
993 //
994 ////////////////////////////////////////////////////////////////////////////////
stopTone()995 void ToneGenerator::stopTone() {
996     ALOGV("stopTone");
997 
998     mLock.lock();
999     if (mState != TONE_IDLE && mState != TONE_INIT) {
1000         if (mState == TONE_PLAYING || mState == TONE_STARTING || mState == TONE_RESTARTING) {
1001             struct timespec stopTime;
1002             // If the start time is valid, make sure that the number of audio samples produced
1003             // corresponds at least to the time between the start and stop commands.
1004             // This is needed in case of cold start of the output stream.
1005             if ((mStartTime.tv_sec != 0) && (clock_gettime(CLOCK_MONOTONIC, &stopTime) == 0)) {
1006                 time_t sec = stopTime.tv_sec - mStartTime.tv_sec;
1007                 long nsec = stopTime.tv_nsec - mStartTime.tv_nsec;
1008                 if (nsec < 0) {
1009                     --sec;
1010                     nsec += 1000000000;
1011                 }
1012 
1013                 if ((sec + 1) > ((long)(INT_MAX / mSamplingRate))) {
1014                     mMaxSmp = sec * mSamplingRate;
1015                 } else {
1016                     // mSamplingRate is always > 1000
1017                     sec = sec * 1000 + nsec / 1000000; // duration in milliseconds
1018                     mMaxSmp = (unsigned int)(((int64_t)sec * mSamplingRate) / 1000);
1019                 }
1020                 ALOGV("stopTone() forcing mMaxSmp to %d, total for far %d", mMaxSmp,  mTotalSmp);
1021             } else {
1022                 mState = TONE_STOPPING;
1023             }
1024         }
1025         ALOGV("waiting cond");
1026         status_t lStatus = mWaitCbkCond.waitRelative(mLock, seconds(3));
1027         if (lStatus == NO_ERROR) {
1028             // If the tone was restarted exit now before calling clearWaveGens();
1029             if (mState != TONE_INIT) {
1030                 mLock.unlock();
1031                 return;
1032             }
1033             ALOGV("track stop complete, time %d", (unsigned int)(systemTime()/1000000));
1034         } else {
1035             ALOGE("--- Stop timed out");
1036             mState = TONE_IDLE;
1037             mpAudioTrack->stop();
1038         }
1039         clearWaveGens();
1040     }
1041 
1042     mLock.unlock();
1043 }
1044 
1045 //---------------------------------- private methods ---------------------------
1046 
1047 
1048 
1049 
1050 ////////////////////////////////////////////////////////////////////////////////
1051 //
1052 //    Method:        ToneGenerator::initAudioTrack()
1053 //
1054 //    Description:    Allocates and configures AudioTrack used for PCM output.
1055 //
1056 //    Input:
1057 //        none
1058 //
1059 //    Output:
1060 //        none
1061 //
1062 ////////////////////////////////////////////////////////////////////////////////
initAudioTrack()1063 bool ToneGenerator::initAudioTrack() {
1064 
1065     // Open audio track in mono, PCM 16bit, default sampling rate, default buffer size
1066     mpAudioTrack = new AudioTrack();
1067     ALOGV("Create Track: %p", mpAudioTrack.get());
1068 
1069     mpAudioTrack->set(mStreamType,
1070                       0,    // sampleRate
1071                       AUDIO_FORMAT_PCM_16_BIT,
1072                       AUDIO_CHANNEL_OUT_MONO,
1073                       0,    // frameCount
1074                       AUDIO_OUTPUT_FLAG_FAST,
1075                       audioCallback,
1076                       this, // user
1077                       0,    // notificationFrames
1078                       0,    // sharedBuffer
1079                       mThreadCanCallJava,
1080                       AUDIO_SESSION_ALLOCATE,
1081                       AudioTrack::TRANSFER_CALLBACK);
1082 
1083     if (mpAudioTrack->initCheck() != NO_ERROR) {
1084         ALOGE("AudioTrack->initCheck failed");
1085         goto initAudioTrack_exit;
1086     }
1087 
1088     mpAudioTrack->setVolume(mVolume);
1089 
1090     mState = TONE_INIT;
1091 
1092     return true;
1093 
1094 initAudioTrack_exit:
1095 
1096     ALOGV("Init failed: %p", mpAudioTrack.get());
1097 
1098     // Cleanup
1099     mpAudioTrack.clear();
1100 
1101     return false;
1102 }
1103 
1104 
1105 ////////////////////////////////////////////////////////////////////////////////
1106 //
1107 //    Method:        ToneGenerator::audioCallback()
1108 //
1109 //    Description:    AudioTrack callback implementation. Generates a block of
1110 //        PCM samples
1111 //        and manages tone generator sequencer: tones pulses, tone duration...
1112 //
1113 //    Input:
1114 //        user    reference (pointer to our ToneGenerator)
1115 //        info    audio buffer descriptor
1116 //
1117 //    Output:
1118 //        returned value: always true.
1119 //
1120 ////////////////////////////////////////////////////////////////////////////////
audioCallback(int event,void * user,void * info)1121 void ToneGenerator::audioCallback(int event, void* user, void *info) {
1122 
1123     if (event != AudioTrack::EVENT_MORE_DATA) return;
1124 
1125     AudioTrack::Buffer *buffer = static_cast<AudioTrack::Buffer *>(info);
1126     ToneGenerator *lpToneGen = static_cast<ToneGenerator *>(user);
1127     short *lpOut = buffer->i16;
1128     unsigned int lNumSmp = buffer->size/sizeof(short);
1129     const ToneDescriptor *lpToneDesc = lpToneGen->mpToneDesc;
1130 
1131     if (buffer->size == 0) return;
1132 
1133 
1134     // Clear output buffer: WaveGenerator accumulates into lpOut buffer
1135     memset(lpOut, 0, buffer->size);
1136 
1137     while (lNumSmp) {
1138         unsigned int lReqSmp = lNumSmp < lpToneGen->mProcessSize*2 ? lNumSmp : lpToneGen->mProcessSize;
1139         unsigned int lGenSmp;
1140         unsigned int lWaveCmd = WaveGenerator::WAVEGEN_CONT;
1141         bool lSignal = false;
1142 
1143         lpToneGen->mLock.lock();
1144 
1145 
1146         // Update pcm frame count and end time (current time at the end of this process)
1147         lpToneGen->mTotalSmp += lReqSmp;
1148 
1149         // Update tone gen state machine and select wave gen command
1150         switch (lpToneGen->mState) {
1151         case TONE_PLAYING:
1152             lWaveCmd = WaveGenerator::WAVEGEN_CONT;
1153             break;
1154         case TONE_STARTING:
1155             ALOGV("Starting Cbk");
1156 
1157             lWaveCmd = WaveGenerator::WAVEGEN_START;
1158             break;
1159         case TONE_STOPPING:
1160         case TONE_RESTARTING:
1161             ALOGV("Stop/restart Cbk");
1162 
1163             lWaveCmd = WaveGenerator::WAVEGEN_STOP;
1164             lpToneGen->mNextSegSmp = TONEGEN_INF; // forced to skip state machine management below
1165             break;
1166         case TONE_STOPPED:
1167             ALOGV("Stopped Cbk");
1168             goto audioCallback_EndLoop;
1169         default:
1170             ALOGV("Extra Cbk");
1171             goto audioCallback_EndLoop;
1172         }
1173 
1174         // Exit if tone sequence is over
1175         if (lpToneDesc->segments[lpToneGen->mCurSegment].duration == 0 ||
1176             lpToneGen->mTotalSmp > lpToneGen->mMaxSmp) {
1177             if (lpToneGen->mState == TONE_PLAYING) {
1178                 lpToneGen->mState = TONE_STOPPING;
1179             }
1180             if (lpToneDesc->segments[lpToneGen->mCurSegment].duration == 0) {
1181                 goto audioCallback_EndLoop;
1182             }
1183             // fade out before stopping if maximum duration reached
1184             lWaveCmd = WaveGenerator::WAVEGEN_STOP;
1185             lpToneGen->mNextSegSmp = TONEGEN_INF; // forced to skip state machine management below
1186         }
1187 
1188         if (lpToneGen->mTotalSmp > lpToneGen->mNextSegSmp) {
1189             // Time to go to next sequence segment
1190 
1191             ALOGV("End Segment, time: %d", (unsigned int)(systemTime()/1000000));
1192 
1193             lGenSmp = lReqSmp;
1194 
1195             // If segment,  ON -> OFF transition : ramp volume down
1196             if (lpToneDesc->segments[lpToneGen->mCurSegment].waveFreq[0] != 0) {
1197                 lWaveCmd = WaveGenerator::WAVEGEN_STOP;
1198                 unsigned int lFreqIdx = 0;
1199                 unsigned short lFrequency = lpToneDesc->segments[lpToneGen->mCurSegment].waveFreq[lFreqIdx];
1200 
1201                 while (lFrequency != 0) {
1202                     WaveGenerator *lpWaveGen = lpToneGen->mWaveGens.valueFor(lFrequency);
1203                     lpWaveGen->getSamples(lpOut, lGenSmp, lWaveCmd);
1204                     lFrequency = lpToneDesc->segments[lpToneGen->mCurSegment].waveFreq[++lFreqIdx];
1205                 }
1206                 ALOGV("ON->OFF, lGenSmp: %d, lReqSmp: %d", lGenSmp, lReqSmp);
1207             }
1208 
1209             // check if we need to loop and loop for the reqd times
1210             if (lpToneDesc->segments[lpToneGen->mCurSegment].loopCnt) {
1211                 if (lpToneGen->mLoopCounter < lpToneDesc->segments[lpToneGen->mCurSegment].loopCnt) {
1212                     ALOGV ("in if loop loopCnt(%d) loopctr(%d), CurSeg(%d)",
1213                           lpToneDesc->segments[lpToneGen->mCurSegment].loopCnt,
1214                           lpToneGen->mLoopCounter,
1215                           lpToneGen->mCurSegment);
1216                     lpToneGen->mCurSegment = lpToneDesc->segments[lpToneGen->mCurSegment].loopIndx;
1217                     ++lpToneGen->mLoopCounter;
1218                 } else {
1219                     // completed loop. go to next segment
1220                     lpToneGen->mLoopCounter = 0;
1221                     lpToneGen->mCurSegment++;
1222                     ALOGV ("in else loop loopCnt(%d) loopctr(%d), CurSeg(%d)",
1223                           lpToneDesc->segments[lpToneGen->mCurSegment].loopCnt,
1224                           lpToneGen->mLoopCounter,
1225                           lpToneGen->mCurSegment);
1226                 }
1227             } else {
1228                 lpToneGen->mCurSegment++;
1229                 ALOGV ("Goto next seg loopCnt(%d) loopctr(%d), CurSeg(%d)",
1230                       lpToneDesc->segments[lpToneGen->mCurSegment].loopCnt,
1231                       lpToneGen->mLoopCounter,
1232                       lpToneGen->mCurSegment);
1233 
1234             }
1235 
1236             // Handle loop if last segment reached
1237             if (lpToneDesc->segments[lpToneGen->mCurSegment].duration == 0) {
1238                 ALOGV("Last Seg: %d", lpToneGen->mCurSegment);
1239 
1240                 // Pre increment loop count and restart if total count not reached. Stop sequence otherwise
1241                 if (++lpToneGen->mCurCount <= lpToneDesc->repeatCnt) {
1242                     ALOGV("Repeating Count: %d", lpToneGen->mCurCount);
1243 
1244                     lpToneGen->mCurSegment = lpToneDesc->repeatSegment;
1245                     if (lpToneDesc->segments[lpToneDesc->repeatSegment].waveFreq[0] != 0) {
1246                         lWaveCmd = WaveGenerator::WAVEGEN_START;
1247                     }
1248 
1249                     ALOGV("New segment %d, Next Time: %d", lpToneGen->mCurSegment,
1250                             (lpToneGen->mNextSegSmp*1000)/lpToneGen->mSamplingRate);
1251 
1252                 } else {
1253                     lGenSmp = 0;
1254                     ALOGV("End repeat, time: %d", (unsigned int)(systemTime()/1000000));
1255                 }
1256             } else {
1257                 ALOGV("New segment %d, Next Time: %d", lpToneGen->mCurSegment,
1258                         (lpToneGen->mNextSegSmp*1000)/lpToneGen->mSamplingRate);
1259                 if (lpToneDesc->segments[lpToneGen->mCurSegment].waveFreq[0] != 0) {
1260                     // If next segment is not silent,  OFF -> ON transition : reset wave generator
1261                     lWaveCmd = WaveGenerator::WAVEGEN_START;
1262 
1263                     ALOGV("OFF->ON, lGenSmp: %d, lReqSmp: %d", lGenSmp, lReqSmp);
1264                 } else {
1265                     lGenSmp = 0;
1266                 }
1267             }
1268 
1269             // Update next segment transition position. No harm to do it also for last segment as lpToneGen->mNextSegSmp won't be used any more
1270             lpToneGen->mNextSegSmp
1271                     += (lpToneDesc->segments[lpToneGen->mCurSegment].duration * lpToneGen->mSamplingRate) / 1000;
1272 
1273         } else {
1274             // Inside a segment keep tone ON or OFF
1275             if (lpToneDesc->segments[lpToneGen->mCurSegment].waveFreq[0] == 0) {
1276                 lGenSmp = 0;  // If odd segment, tone is currently OFF
1277             } else {
1278                 lGenSmp = lReqSmp;  // If event segment, tone is currently ON
1279             }
1280         }
1281 
1282         if (lGenSmp) {
1283             // If samples must be generated, call all active wave generators and acumulate waves in lpOut
1284             unsigned int lFreqIdx = 0;
1285             unsigned short lFrequency = lpToneDesc->segments[lpToneGen->mCurSegment].waveFreq[lFreqIdx];
1286 
1287             while (lFrequency != 0) {
1288                 WaveGenerator *lpWaveGen = lpToneGen->mWaveGens.valueFor(lFrequency);
1289                 lpWaveGen->getSamples(lpOut, lGenSmp, lWaveCmd);
1290                 lFrequency = lpToneDesc->segments[lpToneGen->mCurSegment].waveFreq[++lFreqIdx];
1291             }
1292         }
1293 
1294         lNumSmp -= lReqSmp;
1295         lpOut += lReqSmp;
1296 
1297 audioCallback_EndLoop:
1298 
1299         switch (lpToneGen->mState) {
1300         case TONE_RESTARTING:
1301             ALOGV("Cbk restarting track");
1302             if (lpToneGen->prepareWave()) {
1303                 lpToneGen->mState = TONE_STARTING;
1304                 if (clock_gettime(CLOCK_MONOTONIC, &lpToneGen->mStartTime) != 0) {
1305                     lpToneGen->mStartTime.tv_sec = 0;
1306                 }
1307                 // must reload lpToneDesc as prepareWave() may change mpToneDesc
1308                 lpToneDesc = lpToneGen->mpToneDesc;
1309             } else {
1310                 ALOGW("Cbk restarting prepareWave() failed");
1311                 lpToneGen->mState = TONE_IDLE;
1312                 lpToneGen->mpAudioTrack->stop();
1313                 // Force loop exit
1314                 lNumSmp = 0;
1315             }
1316             lSignal = true;
1317             break;
1318         case TONE_STOPPING:
1319             ALOGV("Cbk Stopping");
1320             lpToneGen->mState = TONE_STOPPED;
1321             // Force loop exit
1322             lNumSmp = 0;
1323             break;
1324         case TONE_STOPPED:
1325             lpToneGen->mState = TONE_INIT;
1326             ALOGV("Cbk Stopped track");
1327             lpToneGen->mpAudioTrack->stop();
1328             // Force loop exit
1329             lNumSmp = 0;
1330             buffer->size = 0;
1331             lSignal = true;
1332             break;
1333         case TONE_STARTING:
1334             ALOGV("Cbk starting track");
1335             lpToneGen->mState = TONE_PLAYING;
1336             lSignal = true;
1337             break;
1338         case TONE_PLAYING:
1339             break;
1340         default:
1341             // Force loop exit
1342             lNumSmp = 0;
1343             buffer->size = 0;
1344             break;
1345         }
1346 
1347         if (lSignal)
1348             lpToneGen->mWaitCbkCond.broadcast();
1349         lpToneGen->mLock.unlock();
1350     }
1351 }
1352 
1353 
1354 ////////////////////////////////////////////////////////////////////////////////
1355 //
1356 //    Method:        ToneGenerator::prepareWave()
1357 //
1358 //    Description:    Prepare wave generators and reset tone sequencer state machine.
1359 //      mpNewToneDesc must have been initialized before calling this function.
1360 //    Input:
1361 //        none
1362 //
1363 //    Output:
1364 //        returned value:   true if wave generators have been created, false otherwise
1365 //
1366 ////////////////////////////////////////////////////////////////////////////////
prepareWave()1367 bool ToneGenerator::prepareWave() {
1368     unsigned int segmentIdx = 0;
1369 
1370     if (mpNewToneDesc == NULL) {
1371         return false;
1372     }
1373 
1374     // Remove existing wave generators if any
1375     clearWaveGens();
1376 
1377     mpToneDesc = mpNewToneDesc;
1378 
1379     if (mDurationMs == -1) {
1380         mMaxSmp = TONEGEN_INF;
1381     } else {
1382         if (mDurationMs > (int)(TONEGEN_INF / mSamplingRate)) {
1383             mMaxSmp = (mDurationMs / 1000) * mSamplingRate;
1384         } else {
1385             mMaxSmp = (mDurationMs * mSamplingRate) / 1000;
1386         }
1387         ALOGV("prepareWave, duration limited to %d ms", mDurationMs);
1388     }
1389 
1390     while (mpToneDesc->segments[segmentIdx].duration) {
1391         // Get total number of sine waves: needed to adapt sine wave gain.
1392         unsigned int lNumWaves = numWaves(segmentIdx);
1393         unsigned int freqIdx = 0;
1394         unsigned int frequency = mpToneDesc->segments[segmentIdx].waveFreq[freqIdx];
1395         while (frequency) {
1396             // Instantiate a wave generator if  ot already done for this frequency
1397             if (mWaveGens.indexOfKey(frequency) == NAME_NOT_FOUND) {
1398                 ToneGenerator::WaveGenerator *lpWaveGen =
1399                         new ToneGenerator::WaveGenerator((unsigned short)mSamplingRate,
1400                                 frequency,
1401                                 TONEGEN_GAIN/lNumWaves);
1402                 mWaveGens.add(frequency, lpWaveGen);
1403             }
1404             frequency = mpNewToneDesc->segments[segmentIdx].waveFreq[++freqIdx];
1405         }
1406         segmentIdx++;
1407     }
1408 
1409     // Initialize tone sequencer
1410     mTotalSmp = 0;
1411     mCurSegment = 0;
1412     mCurCount = 0;
1413     mLoopCounter = 0;
1414     if (mpToneDesc->segments[0].duration == TONEGEN_INF) {
1415         mNextSegSmp = TONEGEN_INF;
1416     } else{
1417         mNextSegSmp = (mpToneDesc->segments[0].duration * mSamplingRate) / 1000;
1418     }
1419 
1420     return true;
1421 }
1422 
1423 
1424 ////////////////////////////////////////////////////////////////////////////////
1425 //
1426 //    Method:        ToneGenerator::numWaves()
1427 //
1428 //    Description:    Count number of sine waves needed to generate a tone segment (e.g 2 for DTMF).
1429 //
1430 //    Input:
1431 //        segmentIdx        tone segment index
1432 //
1433 //    Output:
1434 //        returned value:    nummber of sine waves
1435 //
1436 ////////////////////////////////////////////////////////////////////////////////
numWaves(unsigned int segmentIdx)1437 unsigned int ToneGenerator::numWaves(unsigned int segmentIdx) {
1438     unsigned int lCnt = 0;
1439 
1440     if (mpToneDesc->segments[segmentIdx].duration) {
1441         while (mpToneDesc->segments[segmentIdx].waveFreq[lCnt]) {
1442             lCnt++;
1443         }
1444         lCnt++;
1445     }
1446 
1447     return lCnt;
1448 }
1449 
1450 
1451 ////////////////////////////////////////////////////////////////////////////////
1452 //
1453 //    Method:        ToneGenerator::clearWaveGens()
1454 //
1455 //    Description:    Removes all wave generators.
1456 //
1457 //    Input:
1458 //        none
1459 //
1460 //    Output:
1461 //        none
1462 //
1463 ////////////////////////////////////////////////////////////////////////////////
clearWaveGens()1464 void ToneGenerator::clearWaveGens() {
1465     ALOGV("Clearing mWaveGens:");
1466 
1467     for (size_t lIdx = 0; lIdx < mWaveGens.size(); lIdx++) {
1468         delete mWaveGens.valueAt(lIdx);
1469     }
1470     mWaveGens.clear();
1471 }
1472 
1473 ////////////////////////////////////////////////////////////////////////////////
1474 //
1475 //    Method:       ToneGenerator::getToneForRegion()
1476 //
1477 //    Description:  Get correct ringtone type according to current region.
1478 //      The corrected ring tone type is the tone descriptor index in sToneDescriptors[].
1479 //
1480 //    Input:
1481 //        none
1482 //
1483 //    Output:
1484 //        none
1485 //
1486 ////////////////////////////////////////////////////////////////////////////////
getToneForRegion(tone_type toneType)1487 ToneGenerator::tone_type ToneGenerator::getToneForRegion(tone_type toneType) {
1488     tone_type regionTone;
1489 
1490     if (mRegion == CEPT || toneType < FIRST_SUP_TONE || toneType > LAST_SUP_TONE) {
1491         regionTone = toneType;
1492     } else {
1493         regionTone = (tone_type) sToneMappingTable[mRegion][toneType - FIRST_SUP_TONE];
1494     }
1495 
1496     ALOGV("getToneForRegion, tone %d, region %d, regionTone %d", toneType, mRegion, regionTone);
1497 
1498     return regionTone;
1499 }
1500 
1501 
1502 ////////////////////////////////////////////////////////////////////////////////
1503 //                WaveGenerator::WaveGenerator class    Implementation
1504 ////////////////////////////////////////////////////////////////////////////////
1505 
1506 //---------------------------------- public methods ----------------------------
1507 
1508 ////////////////////////////////////////////////////////////////////////////////
1509 //
1510 //    Method:        WaveGenerator::WaveGenerator()
1511 //
1512 //    Description:    Constructor.
1513 //
1514 //    Input:
1515 //        samplingRate:    Output sampling rate in Hz
1516 //        frequency:       Frequency of the sine wave to generate in Hz
1517 //        volume:          volume (0.0 to 1.0)
1518 //
1519 //    Output:
1520 //        none
1521 //
1522 ////////////////////////////////////////////////////////////////////////////////
WaveGenerator(unsigned short samplingRate,unsigned short frequency,float volume)1523 ToneGenerator::WaveGenerator::WaveGenerator(unsigned short samplingRate,
1524         unsigned short frequency, float volume) {
1525     double d0;
1526     double F_div_Fs;  // frequency / samplingRate
1527 
1528     F_div_Fs = frequency / (double)samplingRate;
1529     d0 = - (float)GEN_AMP * sin(2 * M_PI * F_div_Fs);
1530     mS2_0 = (short)d0;
1531     mS1 = 0;
1532     mS2 = mS2_0;
1533 
1534     mAmplitude_Q15 = (short)(32767. * 32767. * volume / GEN_AMP);
1535     // take some margin for amplitude fluctuation
1536     if (mAmplitude_Q15 > 32500)
1537         mAmplitude_Q15 = 32500;
1538 
1539     d0 = 32768.0 * cos(2 * M_PI * F_div_Fs);  // Q14*2*cos()
1540     if (d0 > 32767)
1541         d0 = 32767;
1542     mA1_Q14 = (short) d0;
1543 
1544     ALOGV("WaveGenerator init, mA1_Q14: %d, mS2_0: %d, mAmplitude_Q15: %d",
1545             mA1_Q14, mS2_0, mAmplitude_Q15);
1546 }
1547 
1548 ////////////////////////////////////////////////////////////////////////////////
1549 //
1550 //    Method:        WaveGenerator::~WaveGenerator()
1551 //
1552 //    Description:    Destructor.
1553 //
1554 //    Input:
1555 //        none
1556 //
1557 //    Output:
1558 //        none
1559 //
1560 ////////////////////////////////////////////////////////////////////////////////
~WaveGenerator()1561 ToneGenerator::WaveGenerator::~WaveGenerator() {
1562 }
1563 
1564 ////////////////////////////////////////////////////////////////////////////////
1565 //
1566 //    Method:        WaveGenerator::getSamples()
1567 //
1568 //    Description:    Generates count samples of a sine wave and accumulates
1569 //        result in outBuffer.
1570 //
1571 //    Input:
1572 //        outBuffer:      Output buffer where to accumulate samples.
1573 //        count:          number of samples to produce.
1574 //        command:        special action requested (see enum gen_command).
1575 //
1576 //    Output:
1577 //        none
1578 //
1579 ////////////////////////////////////////////////////////////////////////////////
getSamples(short * outBuffer,unsigned int count,unsigned int command)1580 void ToneGenerator::WaveGenerator::getSamples(short *outBuffer,
1581         unsigned int count, unsigned int command) {
1582     long lS1, lS2;
1583     long lA1, lAmplitude;
1584     long Sample;  // current sample
1585 
1586     // init local
1587     if (command == WAVEGEN_START) {
1588         lS1 = (long)0;
1589         lS2 = (long)mS2_0;
1590     } else {
1591         lS1 = (long)mS1;
1592         lS2 = (long)mS2;
1593     }
1594     lA1 = (long)mA1_Q14;
1595     lAmplitude = (long)mAmplitude_Q15;
1596 
1597     if (command == WAVEGEN_STOP) {
1598         lAmplitude <<= 16;
1599         if (count == 0) {
1600             return;
1601         }
1602         long dec = lAmplitude/count;
1603         // loop generation
1604         while (count) {
1605             count--;
1606             Sample = ((lA1 * lS1) >> S_Q14) - lS2;
1607             // shift delay
1608             lS2 = lS1;
1609             lS1 = Sample;
1610             Sample = ((lAmplitude>>16) * Sample) >> S_Q15;
1611             *(outBuffer++) += (short)Sample;  // put result in buffer
1612             lAmplitude -= dec;
1613         }
1614     } else {
1615         // loop generation
1616         while (count) {
1617             count--;
1618             Sample = ((lA1 * lS1) >> S_Q14) - lS2;
1619             // shift delay
1620             lS2 = lS1;
1621             lS1 = Sample;
1622             Sample = (lAmplitude * Sample) >> S_Q15;
1623             *(outBuffer++) += (short)Sample;  // put result in buffer
1624         }
1625     }
1626 
1627     // save status
1628     mS1 = (short)lS1;
1629     mS2 = (short)lS2;
1630 }
1631 
1632 }  // end namespace android
1633