Remove unused include statement
[platform/core/multimedia/libmm-sound.git] / server / plugin / tone / mm_sound_plugin_codec_tone.c
1 /*
2  * libmm-sound
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Seungbae Shin <seungbae.shin@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25
26 #include <semaphore.h>
27
28 #include "../../include/mm_sound_plugin_codec.h"
29 #include <mm_error.h>
30 #include <mm_debug.h>
31 #include <mm_sound.h>
32
33 /* For Beep */
34 #include <math.h>
35 #include <glib.h>
36
37 #include <pulse/sample.h>
38 #include "../../../include/mm_sound_pa_client.h"
39
40 #ifndef M_PI
41 #define M_PI  3.14159265358979323846
42 #endif
43
44 #ifndef M_PI_2
45 #define M_PI_2  1.57079632679489661923
46 #endif
47
48 #define SAMPLERATE 44100
49
50 #define SAMPLE_SIZE 16
51 #define CHANNELS 1
52 #define MAX_DURATION 100
53 #define TONE_COLUMN 6
54
55 typedef enum {
56    STATE_NONE = 0,
57    STATE_READY,
58    STATE_BEGIN,
59    STATE_PLAY,
60    STATE_STOP,
61 } state_e;
62
63 typedef enum {
64         CONTROL_STOPPED,
65         CONTROL_READY,
66         CONTROL_STARTING,
67         CONTROL_PLAYING,
68 } control_e;
69
70 #if 0
71 typedef struct {
72         pthread_mutex_t syncker;
73         pthread_cond_t cond;
74         int state;
75 } tone_control_t;
76 #endif
77
78 typedef struct {
79      /* AMR Buffer */
80         int                             size; /* sizeof hole amr data */
81
82      /* Audio Infomations */
83         int                             handle;
84
85      /* control Informations */
86         int                             repeat_count;
87         int                             (*stop_cb)(int);
88         int                             cb_param;
89         int                             state;
90         int                             number;
91         double                  volume;
92         int                             time;
93         int                             pid;
94
95 } tone_info_t;
96
97 typedef enum
98 {
99         LOW_FREQUENCY = 0,
100         MIDDLE_FREQUENCY,
101         HIGH_FREQUENCY,
102         PLAYING_TIME,
103         LOOP_COUNT,
104         LOOP_INDEX,
105 } volume_type_e;
106
107 typedef struct st_tone
108 {
109         float low_frequency;
110         float middle_frequency;
111         float high_frequency;
112         int playingTime;
113         int loopCnt;
114         int loopIndx;
115 } TONE;
116
117 static const int TONE_SEGMENT[][MM_SOUND_TONE_NUM] =
118 {
119         {941,   1336,   0,      -1,     0,      0,
120         -1,     -1,     -1,     -1,     0,      0},// 0 key: 1336Hz, 941Hz
121
122         {697,   1209,   0,      -1,     0,      0,
123         -1,     -1,     -1,     -1,     0,      0},// 1 key: 1209Hz, 697Hz
124
125         {697,   1336,   0,      -1,     0,      0,
126         -1,     -1,     -1,     -1,     0,      0},// 2 key: 1336Hz, 697Hz
127
128         {697,   1477,   0,      -1,     0,      0,
129         -1,     -1,     -1,     -1,     0,      0}, // 3 key: 1477Hz, 697Hz
130
131         {770,   1209,   0,      -1,     0,      0,
132         -1,     -1,     -1,     -1,     0,      0},// 4 key: 1209Hz, 770Hz
133
134         {770,   1336,   0,      -1,     0,      0,
135         -1,     -1,     -1,     -1,     0,      0},// 5 key: 1336Hz, 770Hz
136
137         {770,   1477,   0,      -1,     0,      0,
138         -1,     -1,     -1,     -1,     0,      0},// 6 key: 1477Hz, 770Hz
139
140         {852,   1209,   0,      -1,     0,      0,
141         -1,     -1,     -1,     -1,     0,      0},// 7 key: 1209Hz, 852Hz
142
143         {852,   1336,   0,      -1,     0,      0,
144         -1,     -1,     -1,     -1,     0,      0},// 8 key: 1336Hz, 852Hz
145
146         {852,   1477,   0,      -1,     0,      0,
147         -1,     -1,     -1,     -1,     0,      0},// 9 key: 1477Hz, 852Hz
148
149         {941,   1209,   0,      -1,     0,      0,
150         -1,     -1,     -1,     -1,     0,      0},// * key: 1209Hz, 941Hz
151
152         {941,   1477,   0,      -1,     0,      0,
153         -1,     -1,     -1,     -1,     0,      0},// # key: 1477Hz, 941Hz
154
155         {697,   1633,   0,      -1,     0,      0,
156         -1,     -1,     -1,     -1,     0,      0},// A key: 1633Hz, 697Hz
157
158         {770,   1633,   0,      -1,     0,      0,
159         -1,     -1,     -1,     -1,     0,      0},// B key: 1633Hz, 770Hz
160
161         {852,   1633,   0,      -1,     0,      0,
162         -1,     -1,     -1,     -1,     0,      0},// C key: 1633Hz, 852Hz
163
164         {941,   1633,   0,      -1,     0,      0,
165         -1,     -1,     -1,     -1,     0,      0}, // D key: 1633Hz, 941Hz
166
167         {425,   0,      0,      -1,     0,      0,
168         -1,     -1,     -1,     -1,     0,      0},  //Call supervisory tone, Dial tone: CEPT: 425Hz, continuous
169
170         {350,   440, 0, -1,     0,      0,
171         -1,     -1,     -1,     -1,     0,      0},  //Call supervisory tone, Dial tone: ANSI (IS-95): 350Hz+440Hz, continuous
172
173         {400,   0,      0,      -1,     0,      0,
174         -1,     -1,     -1,     -1,     0,      0},  //Call supervisory tone, Dial tone: JAPAN: 400Hz, continuous
175
176         {425,   0,      0,      500,    0,      0,
177                 0,      0,      0,      500,    0,      0,
178         -1,     -1,     -1,     -1,     0,      0},//Call supervisory tone, Busy: CEPT: 425Hz, 500ms ON, 500ms OFF...
179
180         {480,   620, 0, 500,    0,      0,
181                 0,      0,       0,     500,    0,      0,
182         -1,     -1,     -1,     -1,     0,      0}, //Call supervisory tone, Busy: ANSI (IS-95): 480Hz+620Hz, 500ms ON, 500ms OFF...
183
184         {400,   0,      0,      500,    0,      0,
185                 0,      0,       0,     500,    0,      0,
186         -1,     -1,     -1,     -1,     0,      0}, //Call supervisory tone, Busy: JAPAN: 400Hz, 500ms ON, 500ms OFF...
187
188         {425,   0,      0,      200,    0,      0,
189                 0,      0,      0,      200,    0,      0,
190         -1,     -1,     -1,     -1,     1,      0}, //Call supervisory tone, Congestion: CEPT, JAPAN: 425Hz, 200ms ON, 200ms OFF
191
192         {480,   620, 0, 250,    0,      0,
193                 0,      0,      0,      250,    0,      0,
194         -1,     -1,     -1,     -1,     0,      0}, //Call supervisory tone, Congestion: ANSI (IS-95): 480Hz+620Hz, 250ms ON, 250ms OFF...
195
196         {425,   0,      0,      200,    0,      0,
197         -1,     -1,     -1,     -1,     1,      0}, //Call supervisory tone, Radio path acknowlegment : CEPT, ANSI: 425Hz, 200ms ON
198
199         {400,   0,      0,      1000,   0,      0,
200                 0,      0,      0,      2000,   0,      0,
201         -1,     -1,     -1,     -1,     0,      0}, //Call supervisory tone, Radio path acknowlegment : JAPAN: 400Hz, 1s ON, 2s OFF...
202
203         {425,   0,      0,      200,    0,      0,
204                 0,      0,      0,      200,    0,      0,
205                 -1,     -1,     -1,     -1,     3,      0}, //Call supervisory tone, Radio path not available: 425Hz, 200ms ON, 200 OFF 3 bursts
206
207         {950, 1400, 1800,       330,    0,      0,
208         0,      0,      0,      1000,   0,      0,
209         -1,     -1,     -1,     -1,     0,      0}, //Call supervisory tone, Error/Special info: 950Hz+1400Hz+1800Hz, 330ms ON, 1s OFF...
210
211         {425,   0,      0,      200,    0,      0,
212                 0,      0,      0,      600,    0,      0,
213          425,   0,      0,      200,    0,      0,
214                 0,      0,      0,      3000,0, 0,
215         -1,     -1,     -1,     -1,     0,      0}, //Call supervisory tone, Call Waiting: CEPT, JAPAN: 425Hz, 200ms ON, 600ms OFF, 200ms ON, 3s OFF...
216
217         {440,   0,      0,      300,    0,      0,
218         0,      0,      0,      9700,   0,      0,
219         440,    0,      0,      100,    0,      0,
220         0,      0,      0,      100,    0,      0,
221         440,    0,      0,      100,    0,      0,
222         0,      0,      0,      9700,   0,      0,
223         -1,     -1,     -1,     -1,     0,      0}, //Call supervisory tone, Call Waiting: ANSI (IS-95): 440 Hz, 300 ms ON, 9.7 s OFF, (100 ms ON, 100 ms OFF, 100 ms ON, 9.7s OFF ...)
224
225         {425,   0,      0,      1000,   0,      0,
226                 0,      0,      0,      4000,   0,      0,
227         -1,     -1,     -1,     -1,     0,      0}, //Call supervisory tone, Ring Tone: CEPT, JAPAN: 425Hz, 1s ON, 4s OFF...
228
229         {440,  480,     0,      2000,   0,      0,
230                 0,        0,    0,      4000,   0,      0,
231         -1,     -1,     -1,     -1,     0,      0}, //Call supervisory tone, Ring Tone: ANSI (IS-95): 440Hz + 480Hz, 2s ON, 4s OFF...
232
233         {400,   1200,     0,    35,     0,       0,
234         -1,     -1,     -1,     -1,     1,      0}, // General beep: 400Hz+1200Hz, 35ms ON
235
236         {1200,  0,      0, 100, 0,      0,
237                 0,      0,      0, 100, 0,      0,
238                 -1,     -1,     -1,     -1,     2,      0}, //Proprietary tone, positive acknowlegement: 1200Hz, 100ms ON, 100ms OFF 2 bursts
239
240         {300, 400, 500,  400,   0,      0,
241                 -1,     -1,     -1,     -1,     1,      0}, //Proprietary tone, negative acknowlegement: 300Hz+400Hz+500Hz, 400ms ON
242
243         {400, 1200,   0,  200,  0,      0,
244                 -1,     -1,     -1,     -1,     1,      0}, //Proprietary tone, prompt tone: 400Hz+1200Hz, 200ms ON
245
246         {400,   1200,   0,      35,     0,      0,
247                 0,      0,      0,      200,            0,      0,
248         400,            1200,   0,      35,     0,      0,
249                 -1,     -1,     -1,     -1,     1,      0}, //Proprietary tone, general double beep: twice 400Hz+1200Hz, 35ms ON, 200ms OFF, 35ms ON
250
251         {440,   0,      0,      250,    0,      0,
252          620,   0,      0,      250,    0,      0,
253                 -1,     -1,     -1,     -1,     1,      0}, //Call supervisory tone (IS-95), intercept tone: alternating 440 Hz and 620 Hz tones, each on for 250 ms
254
255         {440,   0,      0,      250, 0, 0,
256         620,            0,      0,      250, 0, 0,
257                 -1,     -1,     -1,     -1,     8,      0}, //Call supervisory tone (IS-95), abbreviated intercept: intercept tone limited to 4 seconds
258
259         {480,   620,    0,      250,    0,      0,
260              0, 0,      0,      250,    0,      0,
261                 -1,     -1,     -1,     -1,     8,      0 }, //Call supervisory tone (IS-95), abbreviated congestion: congestion tone limited to 4 seconds
262
263         {350,   440,    0,      100,    0,      0,
264                 0,      0,      0,      100,    0,      0,
265                 -1,     -1,     -1,     -1,     3,      0}, //Call supervisory tone (IS-95), confirm tone: a 350 Hz tone added to a 440 Hz tone repeated 3 times in a 100 ms on, 100 ms off cycle
266
267         {480,   0,      0,      100,    0,      0,
268                 0,      0,      0,      100,    0,      0,
269                 -1,     -1,     -1,     -1,     4,      0}, //Call supervisory tone (IS-95), pip tone: four bursts of 480 Hz tone (0.1 s on, 0.1 s off).
270
271         { 425,  0,      0,      -1,     0,      0,
272                 -1,     -1,     -1,     -1,     0,      0}, //425Hz continuous
273
274         {440,   480,    0,      2000,   0,      0,
275                 0,      0,      0,      4000,   0,      0,
276                 -1,     -1,     -1,     -1,     0,      0}, //CDMA USA Ringback: 440Hz+480Hz 2s ON, 4000 OFF ...
277
278         {440,   0,      0,      250,    0,      0,
279         620,            0,      0,      250,    0,      0,
280                 -1,     -1,     -1,     -1,     0,      0}, //CDMA Intercept tone: 440Hz 250ms ON, 620Hz 250ms ON ...
281
282         {440,   0,      0,      250,    0,      0,
283         620,            0,      0,      250,    0,      0,
284                 -1,     -1,     -1,     -1,     1,      0 }, //CDMA Abbr Intercept tone: 440Hz 250ms ON, 620Hz 250ms ON
285
286         {480,   620,    0,       250,   0,      0,
287                 0,      0,      0,       250,   0,      0,
288                 -1,     -1,     -1,     -1,     0,      0 }, //CDMA Reorder tone: 480Hz+620Hz 250ms ON, 250ms OFF...
289
290         {480,   620,    0,      250,    0,      0,
291                 0,      0,      0,      250,    0,      0,
292                 -1,     -1,     -1,     -1,     8,      0}, //CDMA Abbr Reorder tone: 480Hz+620Hz 250ms ON, 250ms OFF repeated for 8 times
293
294         {480,   620,    0,      500,    0,      0,
295                 0,      0,      0,      500,    0,      0,
296                 -1,     -1,     -1,     -1,     0,      0}, //CDMA Network Busy tone: 480Hz+620Hz 500ms ON, 500ms OFF continuous
297
298         {350,   440,    0,      100,    0,      0,
299                 0,      0,      0,      100,    0,      0,
300                 -1,     -1,     -1,     -1,     3,      0}, //CDMA Confirm tone: 350Hz+440Hz 100ms ON, 100ms OFF repeated for 3 times
301
302         {660, 1000,     0,      500,    0,      0,
303                 0,      0,      0,      100,    0,      0,
304                 -1,     -1,     -1,     -1,     1,      0}, //CDMA answer tone: silent tone - defintion Frequency 0, 0ms ON, 0ms OFF
305
306         {440,   0,      0,      300,    0,      0,
307                 -1,     -1,     -1,     -1,     1,      0}, //CDMA Network Callwaiting tone: 440Hz 300ms ON
308
309         {480,   0,      0,      100,    0,      0,
310                 0,      0,      0,      100,    0,      0,
311                 -1,     -1,     -1,     -1,     4,      0}, //CDMA PIP tone: 480Hz 100ms ON, 100ms OFF repeated for 4 times
312
313         {2090,  0,      0,      32,     0,      0,
314         2556,   0,      0,      64,     19,     0,
315         2090,   0,      0,      32,     0,      0,
316         2556,   0,      0,      48,     0,      0,
317         0,      0,       0,     4000,   0,      0,
318                 -1,     -1,     -1,     -1,     1,      0}, //ISDN Call Signal Normal tone: {2091Hz 32ms ON, 2556 64ms ON} 20 times, 2091 32ms ON, 2556 48ms ON, 4s OFF
319
320         {2091,  0,      0,      32,     0,      0,
321         2556,   0,      0,      64,     7,      0,
322         2091,   0,      0,      32,     0,      0,
323         0,      0,      0,      400,            0,      0,
324         2091,   0,      0,      32,     0,      0,
325         2556,   0,      0,      64,     7,      4,
326         2091,   0,      0,      32,     0,      0,
327         0,      0,      0,      4000,   0,      0,
328                 -1,     -1,     -1,     -1,     1,      0}, //ISDN Call Signal Intergroup tone: {2091Hz 32ms ON, 2556 64ms ON} 8 times, 2091Hz 32ms ON, 400ms OFF, {2091Hz 32ms ON, 2556Hz 64ms ON} 8times, 2091Hz 32ms ON, 4s OFF
329
330         {2091,  0,      0,      32,     0,      0,
331         2556,   0,      0,      64,     3,      0,
332         2091,   0,      0,      32,     0,      0,
333                 0,      0,      0,      200,    0,      0,
334         2091,   0,      0,      32,     0,      0,
335         2556,   0,      0,      64,     3,      4,
336         2091,   0,      0,      32,     0,      0,
337                 0,      0,      0,      200,    0,      0,
338                 -1,     -1,     -1,     -1,     1,      0},//ISDN Call Signal SP PRI tone:{2091Hz 32ms ON, 2556 64ms ON} 4 times 2091Hz 16ms ON, 200ms OFF, {2091Hz 32ms ON, 2556Hz 64ms ON} 4 times, 2091Hz 16ms ON, 200ms OFF
339
340         {0,     0,      0,      -1,     0,      0,
341         -1,     -1,     -1,     -1,     1,      0}, //ISDN Call sign PAT3 tone: silent tone
342
343         {2091,  0,      0,      32,     0,      0,
344         2556,   0,      0,      64,     4,      0,
345         2091,   0,      0,      20,     0,      0,
346                 -1,     -1,     -1,     -1,     1,      0}, //ISDN Ping Ring tone: {2091Hz 32ms ON, 2556Hz 64ms ON} 5 times 2091Hz 20ms ON
347
348         {0,     0,      0,      -1,     0,      0,
349         -1,     -1,     -1,     -1,     1,      0}, //ISDN Pat5 tone: silent tone
350
351         {0,     0,      0,      -1,     0,      0,
352         -1,     -1,     -1,     -1,     1,      0}, //ISDN Pat6 tone: silent tone
353
354         {0,     0,      0,      -1,     0,      0,
355         -1,     -1,     -1,     -1,     1,      0}, //ISDN Pat7 tone: silent tone
356
357         {3700,  0,      0,      25,     0,      0,
358         4000,   0,      0,      25,     39,     0,
359                 0,      0,      0, 4000,        0,      0,
360                 -1,     -1,     -1,     -1,     0,      0}, //TONE_CDMA_HIGH_L tone: {3700Hz 25ms, 4000Hz 25ms} 40 times 4000ms OFF, Repeat ....
361
362         {2600,  0,      0,      25,     0,      0,
363         2900,   0,      0,      25,     39,     0,
364                 0,      0,      0, 4000,        0,      0,
365                 -1,     -1,     -1,     -1,     0,      0},//TONE_CDMA_MED_L tone: {2600Hz 25ms, 2900Hz 25ms} 40 times 4000ms OFF, Repeat ....
366
367         {1300,  0,      0,      25,     0,      0,
368         1450,   0,      0,      25,     39,     0,
369                 0,      0,      0,4000, 0,      0,
370         -1,     -1,     -1,     -1,     0,      0}, //TONE_CDMA_LOW_L tone: {1300Hz 25ms, 1450Hz 25ms} 40 times, 4000ms OFF, Repeat ....
371
372         {3700,  0,      0,      25,     0,      0,
373         4000,   0,      0,      25,     15,     0,
374         0,      0,      0, 400, 0,      0,
375         -1,     -1,     -1,     -1,     0,      0},//CDMA HIGH SS tone: {3700Hz 25ms, 4000Hz 25ms} repeat 16 times, 400ms OFF, repeat ....
376
377         {2600,  0,      0,      25,     0,      0,
378         2900,   0,      0,      25,     15,     0,
379                 0,      0,      0,400,  0,      0,
380         -1,     -1,     -1,     -1,     0,      0}, //CDMA MED SS tone: {2600Hz 25ms, 2900Hz 25ms} repeat 16 times, 400ms OFF, repeat ....
381
382         {1300,  0,      0,      25,     0,      0,
383         1450,   0,      0,      25,     15,     0,
384                 0,      0,      0,      400,    0,      0,
385         -1,     -1,     -1,     -1,     0,      0}, //CDMA LOW SS tone: {1300z 25ms, 1450Hz 25ms} repeat 16 times, 400ms OFF, repeat ....
386
387         {3700,  0,      0,      25,     0,      0,
388         4000,   0,      0,      25,     7,      0,
389                 0,      0,      0,      200,    0,      0,
390         3700,   0,      0,      25,     0,      0,
391         4000,   0,      0,      25,     7,      3,
392                 0,      0,      0,      200,    0,      0,
393         3700,   0,      0,      25,     0,      0,
394         4000,   0,      0,      25,     15,     6,
395                 0,      0,      0,      4000,0, 0,
396                 -1,     -1,     -1,     -1,     0,      0}, //CDMA HIGH SSL tone: {3700Hz 25ms, 4000Hz 25ms} 8 times, 200ms OFF, {3700Hz 25ms, 4000Hz 25ms} repeat 8 times, 200ms OFF, {3700Hz 25ms, 4000Hz 25ms} repeat 16 times, 4000ms OFF, repeat ...
397
398         {2600,  0,      0,      25,     0,      0,
399         2900,   0,      0,      25,     7,      0,
400                 0,      0,      0,      200,    0,      0,
401         2600,   0,      0,      25,     0,      0,
402         2900,   0,      0,      25,     7,      3,
403                 0,      0,      0,      200,    0,      0,
404         2600,   0,      0,      25,     0,      0,
405         2900,   0,      0,      25,     15,     6,
406                 0,      0,      0,      4000,0, 0,
407                 -1,     -1,     -1,     -1,     0,      0}, //CDMA MED SSL tone: {2600Hz 25ms, 2900Hz 25ms} 8 times, 200ms OFF, {2600Hz 25ms, 2900Hz 25ms} repeat 8 times, 200ms OFF, {2600Hz 25ms, 2900Hz 25ms} repeat 16 times, 4000ms OFF, repeat ...
408
409         {1300,  0,      0,      25,     0,      0,
410         1450,   0,      0,      25,     7,      0,
411                 0,      0,      0,      200,    0,      0,
412         1300,   0,      0,      25,     0,      0,
413         1450,   0,      0,      25,     7,      3,
414                 0,      0,      0,      200,    0,      0,
415         1300,   0,      0,      25,     0,      0,
416         1450,   0,      0,      25,     15,     6,
417                 0,      0,      0,      4000,0, 0,
418                 -1,     -1,     -1,     -1,     0,      0}, //CDMA LOW SSL tone: {1300Hz 25ms, 1450Hz 25ms} 8 times, 200ms OFF, {1300Hz 25ms, 1450Hz 25ms} repeat 8 times, 200ms OFF, {1300Hz 25ms, 1450Hz 25ms} repeat 16 times, 4000ms OFF, repeat ...
419
420         {3700,  0,      0,      25,     0,      0,
421         4000,   0,      0,      25,     19,     0,
422         0,      0,      0,      1000,   0,      0,
423         3700,   0,      0,      25,     0,      0,
424         4000,   0,      0,      25,     19,     3,
425         0,      0,      0,      3000,   0,      0,
426                 -1,     -1,     -1,     -1,     0,      0 },//CDMA HIGH SS2 tone: {3700Hz 25ms, 4000Hz 25ms} 20 times, 1000ms OFF, {3700Hz 25ms, 4000Hz 25ms} 20 times, 3000ms OFF, repeat ....
427
428         {2600,  0,      0,      25,     0,      0,
429         2900,   0,      0,      25,     19,     0,
430         0,      0,      0,      1000,   0,      0,
431         2600,   0,      0,      25,     0,      0,
432         2900,   0,      0,      25,     19,     3,
433         0,      0,      0,      3000,   0,      0,
434                 -1,     -1,     -1,     -1,     0,      0}, //CDMA MED SS2 tone: {2600Hz 25ms, 2900Hz 25ms} 20 times, 1000ms OFF, {2600Hz 25ms, 2900Hz 25ms} 20 times, 3000ms OFF, repeat ....
435
436         {1300,  0,      0,      25,     0,      0,
437         1450,   0,      0,      25,     19,     0,
438         0,      0,      0,      1000,   0,      0,
439         1300,   0,      0,      25,     0,      0,
440         1450,   0,      0,      25,     19,     3,
441         0,      0,      0,      3000,   0,      0,
442                 -1,     -1,     -1,     -1,     0,      0 }, //CDMA LOW SS2 tone: {1300Hz 25ms, 1450Hz 25ms} 20 times, 1000ms OFF, {1300Hz 25ms, 1450Hz 25ms} 20 times, 3000ms OFF, repeat ....
443
444         {3700,  0,      0,      25,     0,      0,
445         4000,   0,      0,      25,     9,      0,
446                 0,      0,      0,      500,    0,      0,
447         3700,   0,      0,      25,     0,      0,
448         4000,   0,      0,      25,     19,     3,
449                 0,      0,      0,      500,    0,      0,
450         3700,   0,      0,      25,     0,      0,
451         4000,   0,      0,      25,     9,      6,
452         0,      0,      0,      3000,   0,      0,
453                 -1,     -1,     -1,     -1,     0,      0}, //CDMA HIGH SLS tone: {3700Hz 25ms, 4000Hz 25ms} 10 times, 500ms OFF, {3700Hz 25ms, 4000Hz 25ms} 20 times, 500ms OFF, {3700Hz 25ms, 4000Hz 25ms} 10 times, 3000ms OFF, REPEAT....
454
455         {2600,  0,      0,      25,     0,      0,
456         2900,   0,      0,      25,     9,      0,
457                 0,      0,      0,      500,    0,      0,
458         2600,   0,      0,      25,     0,      0,
459         2900,   0,      0,      25,     19,     3,
460                 0,      0,      0,      500,    0,      0,
461         2600,   0,      0,      25,     0,      0,
462         2900,   0,      0,      25,     9,      6,
463         0,      0,      0,      3000,   0,      0,
464                 -1,     -1,     -1,     -1,     0,      0}, //CDMA MED SLS tone: {2600Hz 25ms, 2900Hz 25ms} 10 times, 500ms OFF, {2600Hz 25ms, 2900Hz 25ms} 20 times, 500ms OFF, {2600Hz 25ms, 2900Hz 25ms} 10 times, 3000ms OFF, REPEAT....
465
466         {1300,  0,      0,      25,     0,      0,
467         1450,   0,      0,      25,     9,      0,
468         0,      0,      0,      500,    0,      0,
469         1300,   0,      0,      25,     0,      0,
470         1450,   0,      0,      25,     19,     3,
471         0,      0,      0,      500,    0,      0,
472         1300,   0,      0,      25,     0,      0,
473         1450,   0,      0,      25,     9,      6,
474         0,      0,      0,      3000,   0,      0,
475                 -1,     -1,     -1,     -1,     0,      0}, //CDMA LOW SLS tone: {1300Hz 25ms, 1450Hz 25ms} 10 times, 500ms OFF, {1300Hz 25ms, 1450Hz 25ms} 20 times, 500ms OFF, {1300Hz 25ms, 1450Hz 25ms} 10 times, 3000ms OFF, REPEAT....
476
477         {3700,  0,      0,      25,     0,      0,
478         4000,   0,      0,      25,     9,      0,
479                 0,      0,      0,      500,    0,      0,
480         3700,   0,      0,      25,     0,      0,
481         4000,   0,      0,      25,     9,      3,
482                 0,      0,      0,      500,    0,      0,
483         3700,   0,      0,      25,     0,      0,
484         4000,   0,      0,      25,     9,      6,
485         0,      0,      0,      2500,   0,      0,
486                 -1,     -1,     -1,     -1,     0,      0}, ////CDMA HIGH S X4 tone: {3700Hz 25ms, 4000Hz 25ms} 10 times, 500ms OFF, {3700Hz 25ms, 4000Hz 25ms} 10 times, 500ms OFF, {3700Hz 25ms, 4000Hz 25ms} 10 times, 500ms OFF, {3700Hz 25ms, 4000Hz 25ms} 10 times, 2500ms OFF, REPEAT....
487
488         {2600,  0,      0,      25,     0,      0,
489         2900,   0,      0,      25,     9,      0,
490                 0,      0,      0,      500,    0,      0,
491         2600,   0,      0,      25,     0,      0,
492         2900,   0,      0,      25,     9,      4,
493                 0,      0,      0,      500,    0,      0,
494         2600,   0,      0,      25,     0,      0,
495         2900,   0,      0,      25,     9,      6,
496         0,      0,      0,      2500,   0,      0,
497                 -1,     -1,     -1,     -1,     0,      0 },//CDMA MED S X4 tone: {2600Hz 25ms, 2900Hz 25ms} 10 times, 500ms OFF, {2600Hz 25ms, 2900Hz 25ms} 10 times, 500ms OFF, {2600Hz 25ms, 2900Hz 25ms} 10 times, 500ms OFF, {2600Hz 25ms, 2900Hz 25ms} 10 times, 2500ms OFF, REPEAT....
498
499         {1300,  0,      0,      25,     0,      0,
500         1450,   0,      0,      25,     9,      0,
501                 0,      0,      0,      500,    0,      0,
502         1300,   0,      0,      25,     0,      0,
503         1450,   0,      0,      25,     9,      3,
504                 0,      0,      0,      500,    0,      0,
505         1300,   0,      0,      25,     0,      0,
506         1450,   0,      0,      25,     9,      6,
507         0,      0,      0,      2500,   0,      0,
508                 -1,     -1,     -1,     -1,     0,      0}, //CDMA LOW S X4 tone: {2600Hz 25ms, 2900Hz 25ms} 10 times, 500ms OFF, {2600Hz 25ms, 2900Hz 25ms} 10 times, 500ms OFF, {2600Hz 25ms, 2900Hz 25ms} 10 times, 500ms OFF, {2600Hz 25ms, 2900Hz 25ms} 10 times, 2500ms OFF, REPEAT....
509
510         {3700,  0,      0,      25,     0,      0,
511         4000,   0,      0,      25,     19,     0,
512         0,      0,      0,      2000,   0,      0,
513                 -1,     -1,     -1,     -1,     0,      0 },//CDMA HIGH PBX L: {3700Hz 25ms, 4000Hz 25ms}20 times, 2000ms OFF, REPEAT....
514
515         {2600,  0,      0,      25,     0,      0,
516         2900,   0,      0,      25,     19,     0,
517         0,      0,      0,      2000,   0,      0,
518                 -1,     -1,     -1,     -1,     0,      0}, //CDMA MED PBX L: {2600Hz 25ms, 2900Hz 25ms}20 times, 2000ms OFF, REPEAT....
519
520         {1300,  0,      0,      25,     0,      0,
521         1450,   0,      0,      25,     19,     0,
522         0,      0,      0,      2000,   0,      0,
523                 -1,     -1,     -1,     -1,     0,      0 },//CDMA LOW PBX L: {1300Hz 25ms,1450Hz 25ms}20 times, 2000ms OFF, REPEAT....
524
525         {3700,  0,      0,      25,     0,      0,
526         4000,   0,      0,      25,     7,      0,
527         0,      0,      0,      200,    0,      0,
528         3700,   0,      0,      25,     0,      0,
529         4000,   0,      0,      25,     7,      3,
530         0,      0,      0,      2000,   0,      0,
531                 -1,     -1,     -1,     -1,     0,      0}, //CDMA HIGH PBX SS tone: {3700Hz 25ms, 4000Hz 25ms} 8 times 200 ms OFF, {3700Hz 25ms 4000Hz 25ms}8 times, 2000ms OFF, REPEAT....
532
533         {2600,  0,      0,      25,     0,      0,
534         2900,   0,      0,      25,     7,      0,
535         0,      0,      0,      200,    0,      0,
536         2600,   0,      0,      25,     0,      0,
537         2900,   0,      0,      25,     7,      3,
538         0,      0,      0,      2000,   0,      0,
539                 -1,     -1,     -1,     -1,     0,      0 }, //CDMA MED PBX SS tone: {2600Hz 25ms, 2900Hz 25ms} 8 times 200 ms OFF, {2600Hz 25ms 2900Hz 25ms}8 times, 2000ms OFF, REPEAT....
540
541         {1300,  0,      0,      25,     0,      0,
542         1450,   0,      0,      25,     7,      0,
543         0,      0,      0,      200,    0,      0,
544         1300,   0,      0,      25,     0,      0,
545         1450,   0,      0,      25,     7,      3,
546         0,      0,      0,      2000,   0,      0,
547                 -1,     -1,     -1,     -1,     0,      0 },//CDMA LOW PBX SS tone: {1300Hz 25ms, 1450Hz 25ms} 8 times 200 ms OFF, {1300Hz 25ms 1450Hz 25ms}8 times, 2000ms OFF, REPEAT....
548
549         {3700,  0,      0,      25,     0,      0,
550         4000,   0,      0,      25,     7,      0,
551         0,      0,      0,      200,    0,      0,
552         3700,   0,      0,      25,     0,      0,
553         4000,   0,      0,      25,     7,      3,
554         0,      0,      0,      200,    0,      0,
555         3700,   0,      0,      25,     0,      0,
556         4000,   0,      0,      25,     15,     6,
557         0,      0,      0,      1000,   0,      0,
558                 -1,     -1,     -1,     -1,     0,      0}, //CDMA HIGH PBX SSL tone:{3700Hz 25ms, 4000Hz 25ms} 8 times 200ms OFF, {3700Hz 25ms, 4000Hz 25ms} 8 times, 200ms OFF, {3700Hz 25ms, 4000Hz 25ms} 16 times, 1000ms OFF, REPEAT....//
559
560         {2600,  0,      0,      25,     0,      0,
561         2900,   0,      0,      25,     7,      0,
562         0,      0,      0,      200,    0,      0,
563         2600,   0,      0,      25,     0,      0,
564         2900,   0,      0,      25,     7,      3,
565         0,      0,      0,      200,    0,      0,
566         2600,   0,      0,      25,     0,      0,
567         2900,   0,      0,      25,     15,     6,
568         0,      0,      0,      1000,   0,      0,
569                 -1,     -1,     -1,     -1,     0,      0}, //CDMA MED PBX SSL tone:{2600Hz 25ms, 2900Hz 25ms} 8 times 200ms OFF, {2600Hz 25ms, 2900Hz 25ms} 8 times, 200ms OFF, {2600Hz 25ms, 2900Hz 25ms} 16 times, 1000ms OFF, REPEAT....//
570
571         {1300,  0,      0,      25,     0,      0,
572         1450,   0,      0,      25,     7,      0,
573         0,      0,      0,      200,    0,      0,
574         1300,   0,      0,      25,     0,      0,
575         1450,   0,      0,      25,     7,      3,
576         0,      0,      0,      200,    0,      0,
577         1300,   0,      0,      25,     0,      0,
578         1450,   0,      0,      25,     15,     6,
579         0,      0,      0,      1000,   0,      0,
580                 -1,     -1,     -1,     -1,     0,      0}, //CDMA LOW PBX SSL tone:{1300Hz 25ms, 1450Hz 25ms} 8 times 200ms OFF, {1300Hz 25ms, 1450Hz 25ms} 8 times, 200ms OFF, {1300Hz 25ms, 1450Hz 25ms} 16 times, 1000ms OFF, REPEAT....//
581
582         {3700,  0,      0,      25,     0,      0,
583         4000,   0,      0,      25,     15,     0,
584         0,      0,      0,      200,    0,      0,
585         3700,   0,      0,      25,     0,      0,
586         4000,   0,      0,      25,     7,      3,
587         0,      0,      0,      1000,   0,      0,
588                 -1,     -1,     -1,     -1,     0,      0 },//CDMA HIGH PBX SLS tone:{3700Hz 25ms, 4000Hz 25ms} 8 times 200ms OFF, {3700Hz 25ms, 4000Hz 25ms} 16 times, 200ms OFF, {3700Hz 25ms, 4000Hz 25ms} 8 times, 1000ms OFF, REPEAT.... //
589
590         {2600,  0,      0,      25,     0,      0,
591         2900,   0,      0,      25,     15,     0,
592                 0,      0,      0,      200,    0,      0,
593         2600,   0,      0,      25,     0,      0,
594         2900,   0,      0,      25,     7,      3,
595                 0,      0,      0,      1000,0, 0,
596                 -1,     -1,     -1,     -1,     0,      0 }, //CDMA HIGH PBX SLS tone:{2600Hz 25ms, 2900Hz 25ms} 8 times 200ms OFF, {2600Hz 25ms, 2900Hz 25ms} 16 times, 200ms OFF, {2600Hz 25ms, 2900Hz 25ms} 8 times, 1000ms OFF, REPEAT....//
597
598         {1300,  0,      0,      25,     0,      0,
599         1450,   0,      0,      25,     15,     0,
600         0,      0,      0,      200,    0,      0,
601         1300,   0,      0,      25,     0,      0,
602         1450,   0,      0,      25,     7,      3,
603         0,      0,      0,      1000,   0,      0,
604                 -1,     -1,     -1,     -1,     0,      0 }, //CDMA HIGH PBX SLS tone:{1300Hz 25ms, 1450Hz 25ms} 8 times 200ms OFF, {1300Hz 25ms, 1450Hz 25ms} 16 times, 200ms OFF, {1300Hz 25ms, 1450Hz 25ms} 8 times, 1000ms OFF, REPEAT....//
605
606         {3700,  0,      0,      25,     0,      0,
607         4000,   0,      0,      25,     7,      0,
608         0,      0,      0,      200,    0,      0,
609         3700,   0,      0,      25,     0,      0,
610         4000,   0,      0,      25,     7,      3,
611         0,      0,      0,      200,    0,      0,
612         3700,   0,      0,      25,     0,      0,
613         4000,   0,      0,      25,     7,      6,
614         0,      0,      0,      200,    0,      0,
615         3700,   0,      0,      25,     0,      0,
616         4000,   0,      0,      25,     7,      9,
617         0,      0,      0,      800,    0,      0,
618                 -1,     -1,     -1,     -1,     0,      0}, //CDMA HIGH PBX X S4 tone: {3700Hz 25ms 4000Hz 25ms} 8 times, 200ms OFF, {3700Hz 25ms 4000Hz 25ms} 8 times, 200ms OFF, {3700Hz 25ms 4000Hz 25ms} 8 times, 200ms OFF, {3700Hz 25ms 4000Hz 25ms} 8 times, 800ms OFF, REPEAT...
619
620         {2600,  0,      0,      25,     0,      0,
621         2900,   0,      0,      25,     7,      0,
622         0,      0,      0,      200,    0,      0,
623         2600,   0,      0,      25,     0,      0,
624         2900,   0,      0,      25,     7,      3,
625         0,      0,      0,      200,    0,      0,
626         2600,   0,      0,      25,     0,      0,
627         2900,   0,      0,      25,     7,      6,
628         0,      0,      0,      200,    0,      0,
629         2600,   0,      0,      25,     0,      0,
630         2900,   0,      0,      25,     7,      9,
631         0,      0,      0,      800,    0,      0,
632                 -1,     -1,     -1,     -1,     0,      0}, //CDMA MED PBX X S4 tone: {2600Hz 25ms 2900Hz 25ms} 8 times, 200ms OFF, {2600Hz 25ms 2900Hz 25ms} 8 times, 200ms OFF, {2600Hz 25ms 2900Hz 25ms} 8 times, 200ms OFF, {2600Hz 25ms 2900Hz 25ms} 8 times, 800ms OFF, REPEAT...
633
634         {1300,  0,      0,      25,     0,      0,
635         1450,   0,      0,      25,     7,      0,
636                 0,      0,      0,      200,    0,      0,
637         1300,   0,      0,      25,     0,      0,
638         1450,   0,      0,      25,     7,      3,
639                 0,      0,      0,      200,    0,      0,
640         1300,   0,      0,      25,     0,      0,
641         1450,   0,      0,      25,     7,      6,
642                 0,      0,      0,      200,    0,      0,
643         1300,   0,      0,      25,     0,      0,
644         1450,   0,      0,      25,     7,      9,
645                 0,      0,      0,      800,    0,      0,
646                 -1,     -1,     -1,     -1,     0,      0}, //CDMA LOW PBX X S4 tone: {1300Hz 25ms 1450Hz 25ms} 8 times, 200ms OFF, {1300Hz 25ms 1450Hz 25ms} 8 times, 200ms OFF, {1300Hz 25ms 1450Hz 25ms} 8 times, 200ms OFF, {1300Hz 25ms 1450Hz 25ms} 8 times, 800ms OFF, REPEAT...
647
648         {1109,  0,      0,      62,     0,      0,
649                 784,    0,      0,      62,     0,      0,
650                 740,    0,      0,      62,     0,      0,
651                 622,    0,      0,      62,     0,      0,
652         1109,   0,      0,      62,     0,      0,
653                 -1,     -1,     -1,     -1,     1,      0}, //CDMA Alert Network Lite tone: 1109Hz 62ms ON, 784Hz 62ms ON, 740Hz 62ms ON 622Hz 62ms ON, 1109Hz 62ms ON
654
655         {1245,  0,      0,      62,     0,      0,
656         659,    0,      0,      62,     0,      0,
657         1245,   0,      0,      62,     0,      0,
658         659,    0,      0,      62,     0,      0,
659         1245,   0,      0,      62,     0,      0,
660         659,    0,      0,      62,     0,      0,
661         1245,   0,      0,      62,     0,      0,
662                 -1,     -1,     -1,     -1,     1,      0},//CDMA Alert Auto Redial tone: {1245Hz 62ms ON, 659Hz 62ms ON} 3 times, 1245 62ms ON//
663
664         {1150,  770, 0,  400, 0,        0,
665                 -1,     -1,     -1,     -1,     1,      0}, //CDMA One Min Beep tone: 1150Hz+770Hz 400ms ON//
666
667         {941, 1477,     0,  120, 0,     0,
668                 -1,     -1,     -1,     -1,     1,      0}, //CDMA KEYPAD Volume key lite tone: 941Hz+1477Hz 120ms ON
669
670         {587,   0,       0, 375, 0,     0,
671         1175,   0,       0, 125, 0,     0,
672                 -1,     -1,     -1,     -1,     1,      0}, //CDMA PRESSHOLDKEY LITE tone: 587Hz 375ms ON, 1175Hz 125ms ON
673
674         {587,   0,      0,      62, 0,  0,
675         784,            0,      0,      62, 0,  0,
676         831,            0,      0,      62, 0,  0,
677         784,            0,      0,      62, 0,  0,
678         1109,   0,      0,      62, 0,  0,
679         784,    0,      0,      62, 0,  0,
680         831,            0,      0,      62, 0,  0,
681         784,    0,      0,      62, 0,  0,
682                 -1,     -1,     -1,     -1,     1,      0}, //CDMA ALERT INCALL LITE tone: 587Hz 62ms, 784 62ms, 831Hz 62ms, 784Hz 62ms, 1109 62ms, 784Hz 62ms, 831Hz 62ms, 784Hz 62ms
683
684         {941,   0,      0,      125,    0,      0,
685                 0,      0,      0,      10,     0,      0,
686                 941,    0,      0,      125,    0,      0,
687                 0,      0,      0,      10,     0,      0,
688         1245,   0,      0,      62,     0,      0,
689                 0,      0,      0,      10,     0,      0,
690                 0,      0,      0,      4990, 0,        0,
691                 -1,     -1,     -1,     -1,     0,      0}, //CDMA EMERGENCY RINGBACK tone: {941Hz 125ms ON, 10ms OFF} 3times 4990ms OFF, REPEAT...
692
693         {1319,  0,      0,      125,    0,      0,
694                 0,      0,      0,      125,    0,      0,
695                 -1,     -1,     -1,     -1,     3,      0 }, //CDMA ALERT CALL GUARD tone: {1319Hz 125ms ON, 125ms OFF} 3 times
696
697         {1047,  0,      0,      125,    0,      0,
698                 370,    0,      0,      125,    0,      0,
699                 -1,     -1,     -1,     -1,     1,      0}, //CDMA SOFT ERROR LITE tone: 1047Hz 125ms ON, 370Hz 125ms
700
701         {1480,  0,      0,      125,    0,      0,
702         1397,   0,      0,      125,    0,      0,
703         784,    0,      0,      125,    0,      0,
704                 -1,     -1,     -1,     -1,     1,      0}, //CDMA CALLDROP LITE tone: 1480Hz 125ms, 1397Hz 125ms, 784Hz 125ms//
705
706         {425,   0,      0,      125,    0,      0,
707                 0,      0,      0,      125,    0,      0,
708                 -1,     -1,     -1,     -1,     1,      0},//CDMA_NETWORK_BUSY_ONE_SHOT tone: 425Hz 500ms ON, 500ms OFF
709
710         {1150,  770,    0,      400,    0,      0,
711                 -1,     -1,     -1,     -1,     1,      0}, //CDMA_ABBR_ALERT tone: 1150Hz+770Hz 400ms ON
712
713         {0,     0,      0,      -1,     0,      0,
714         -1,     -1,     -1,     -1,     1,      0}, //CDMA_SIGNAL_OFF - silent tone
715
716         {100,   0,      0,      -1,     0,      0,
717         -1,     -1,     -1,     -1,     0,      0}, //100Hz continuous
718
719         {200,   0,      0,      -1,     0,      0,
720         -1,     -1,     -1,     -1,     0,      0}, //200Hz continuous
721
722         {300,   0,      0,      -1,     0,      0,
723         -1,     -1,     -1,     -1,     0,      0}, //300Hz continuous
724 };
725
726 #if 0
727 static tone_control_t g_control;
728 #endif
729
730 static int (*g_thread_pool_func)(void*, void (*)(void*)) = NULL;
731 #if 0
732 static int _MMSoundToneInit(void);
733 static int _MMSoundToneFini(void);
734 #endif
735 static void _running_tone(void *param);
736
737
738
739 int* MMSoundPlugCodecToneGetSupportTypes(void)
740 {
741     debug_enter("\n");
742     static int suported[2] = {MM_SOUND_SUPPORTED_CODEC_DTMF, 0};
743     debug_leave("\n");
744     return suported;
745 }
746
747 int MMSoundPlugCodecToneParse(MMSourceType *source, mmsound_codec_info_t *info)
748 {
749         debug_enter("\n");
750         // do nothing
751         debug_leave("\n");
752         return MM_ERROR_NONE;
753 }
754
755 int MMSoundPlugCodecToneCreate(mmsound_codec_param_t *param, mmsound_codec_info_t *info, MMHandleType *handle)
756 {
757         tone_info_t *toneInfo;
758         pa_sample_spec ss;
759         mm_sound_handle_route_info route_info;
760
761         int size;
762
763         int result = MM_ERROR_NONE;
764
765         debug_enter("\n");
766
767         toneInfo = (tone_info_t *)malloc(sizeof(tone_info_t));
768         if (toneInfo == NULL) {
769                 debug_error("memory allocation error\n");
770                 return MM_ERROR_OUT_OF_MEMORY;
771         }
772
773         memset(toneInfo, 0, sizeof(tone_info_t));
774
775         toneInfo->state = STATE_READY;
776
777         ss.rate = SAMPLERATE;
778         ss.channels = CHANNELS;
779         ss.format = PA_SAMPLE_S16LE;
780         route_info.policy = HANDLE_ROUTE_POLICY_OUT_AUTO;
781
782         toneInfo->handle = mm_sound_pa_open(HANDLE_MODE_OUTPUT, &route_info, HANDLE_PRIORITY_NORMAL, param->volume_config, &ss, NULL, &size, param->stream_type, param->stream_index);
783         if (!toneInfo->handle) {
784                 debug_error("Device Open Error 0x%x\n", result);
785                 goto Error;
786         }
787         debug_log("Create audio_handle is %lu\n", toneInfo->handle);
788
789         debug_msg("tone : %d\n", param->tone);
790         debug_msg("repeat : %d\n", param->repeat_count);
791         debug_msg("volume config : %x\n", param->volume_config);
792         debug_msg("callback : %p\n", param->stop_cb);
793         debug_msg("pid : %d\n", param->pid);
794
795         toneInfo->number = param->tone;
796         toneInfo->time = param->repeat_count;
797         toneInfo->stop_cb = param->stop_cb;
798         toneInfo->cb_param = param->param;
799         toneInfo->pid = param->pid;
800         toneInfo->volume = param->volume;
801
802         result = g_thread_pool_func(toneInfo, _running_tone);
803         if (result != 0) {
804                 debug_error("pthread_create() fail in pcm thread\n");
805                 result = MM_ERROR_SOUND_INTERNAL;
806                 goto Error;
807         }
808
809         *handle = (MMHandleType)toneInfo;
810
811         debug_leave("\n");
812         return MM_ERROR_NONE;
813
814 Error:
815         if(toneInfo) {
816                 if(toneInfo->handle) {
817                         if(MM_ERROR_NONE != mm_sound_pa_close(toneInfo->handle))
818                                 debug_error("mm_sound_pa_close() fail. handle(%lu)\n", toneInfo->handle);
819                 }
820                 free(toneInfo);
821         }
822
823         return result;
824
825 }
826
827 int MMSoundPlugCodecToneDestroy(MMHandleType handle)
828 {
829         tone_info_t *toneInfo = (tone_info_t*) handle;
830         int err = MM_ERROR_NONE;
831         if (!toneInfo) {
832                 debug_critical("Confirm the hadle (is NULL)\n");
833                 return MM_ERROR_SOUND_INTERNAL;
834         }
835
836         debug_enter("(handle %x)\n", handle);
837
838         if (toneInfo)
839                 free (toneInfo);
840
841         debug_leave("\n");
842         return err;
843 }
844
845 static
846 int MMSoundPlugCodecTonePlay(MMHandleType handle)
847 {
848         tone_info_t *toneInfo = (tone_info_t *) handle;
849
850         debug_enter("(handle %x)\n", handle);
851
852         toneInfo->state = STATE_BEGIN;
853         debug_msg("sent start signal\n");
854
855         debug_leave("\n");
856
857         return MM_ERROR_NONE;
858 }
859
860 static char*
861 _create_tone(double *sample, TONE _TONE, double volume, int *toneSize, gboolean is_cycle_ended)
862 {
863         short *pbuf;
864         double i = 0;
865         double amplitude, f1, f2, f3;
866         int quota= 0;
867         float low_frequency;
868         float middle_frequency;
869         float high_frequency;
870         int sample_size = 0;
871         char* buffer;
872
873         if(is_cycle_ended) {
874                 low_frequency           = 0;
875                 middle_frequency                = 0;
876                 high_frequency          = 0;
877         } else {
878                 low_frequency           = _TONE.low_frequency;
879                 middle_frequency                = _TONE.middle_frequency;
880                 high_frequency          = _TONE.high_frequency;
881         }
882
883         if(sample == NULL) {
884                 debug_error("Sample buffer is not allocated\n");
885                 return NULL;
886         }
887
888         /* Create a buffer for the tone */
889         if((_TONE.playingTime >  MAX_DURATION) || (_TONE.playingTime == -1) ) {
890                 *toneSize = ((MAX_DURATION / 1000.) * SAMPLERATE * SAMPLE_SIZE * CHANNELS) / 8;
891         } else   {
892                 *toneSize = ((_TONE.playingTime / 1000.) * SAMPLERATE * SAMPLE_SIZE * CHANNELS) / 8;
893         }
894         *toneSize = ((*toneSize+1)>>1)<<1;
895         sample_size = (*toneSize) / (SAMPLE_SIZE / 8);
896
897         debug_log("%0.f, %0.f, %0.f _TONE.playing_time: %d toneSize: %d\n",
898                         low_frequency, middle_frequency, high_frequency, _TONE.playingTime, *toneSize);
899
900         buffer = g_malloc (*toneSize);
901         if(buffer == NULL) {
902                 debug_error("Buffer is not allocated\n");
903                 return NULL;
904         } else {
905                 pbuf = (short*)buffer;
906
907                 if(_TONE.low_frequency > 0) {
908                         quota++;
909                 }
910                 if(_TONE.middle_frequency > 0) {
911                         quota++;
912                 }
913                 if(_TONE.high_frequency > 0) {
914                         quota++;
915                 }
916
917                 for (i = 0; i < sample_size; i++) {
918                         /*
919                          * We add the fundamental frequencies together.
920                          */
921
922                         f1 = sin (2 * M_PI * low_frequency      * ((*sample) / SAMPLERATE));
923                         f2 = sin (2 * M_PI * middle_frequency   * ((*sample) / SAMPLERATE));
924                         f3 = sin (2 * M_PI * high_frequency     * ((*sample) / SAMPLERATE));
925
926                         if(f1 + f2 + f3 != 0) {
927                                 amplitude = (f1 + f2 + f3) / quota;
928                                 /* Adjust the volume */
929                                 amplitude *= volume;
930
931                                 /* Make the [-1:1] interval into a [-32767:32767] interval */
932                                 amplitude *= 32767;
933                         } else {
934                                 amplitude = 0;
935                         }
936
937                         /* Store it in the data buffer */
938                         *(pbuf++) = (short) amplitude;
939
940                         (*sample)++;
941                 }
942         }
943         return buffer;
944 }
945
946 static TONE
947 _mm_get_tone(int key, int CurIndex)
948 {
949         TONE _TONE;
950
951         _TONE.low_frequency             = TONE_SEGMENT[key][CurIndex * TONE_COLUMN + LOW_FREQUENCY];
952         _TONE.middle_frequency  = TONE_SEGMENT[key][CurIndex * TONE_COLUMN + MIDDLE_FREQUENCY];
953         _TONE.high_frequency            = TONE_SEGMENT[key][CurIndex * TONE_COLUMN + HIGH_FREQUENCY];
954         _TONE.playingTime                       = TONE_SEGMENT[key][CurIndex * TONE_COLUMN + PLAYING_TIME];
955         _TONE.loopCnt                   = TONE_SEGMENT[key][CurIndex * TONE_COLUMN + LOOP_COUNT];
956         _TONE.loopIndx                  = TONE_SEGMENT[key][CurIndex * TONE_COLUMN + LOOP_INDEX];
957
958         return _TONE;
959 }
960
961 static int
962 _mm_get_waveCnt_PlayingTime(int toneTime, TONE _TONE, int *waveCnt, int *waveRestPlayTime)
963 {
964         int ret = MM_ERROR_NONE;
965         if(waveCnt == NULL || waveRestPlayTime == NULL) {
966                 debug_error("waveCnt || waveRestPlayTime buffer is NULL\n");
967                 return MM_ERROR_SOUND_INTERNAL;
968         }
969         /*Set  wave count and wave playing time*/
970         if( _TONE.playingTime == -1) {
971                 *waveCnt = abs(toneTime) /MAX_DURATION;
972                 *waveRestPlayTime =  abs(toneTime) % MAX_DURATION;
973         } else {
974                 *waveCnt = _TONE.playingTime /MAX_DURATION;
975                 *waveRestPlayTime =  _TONE.playingTime % MAX_DURATION;
976         }
977         return ret;
978 }
979
980 static int
981 _mm_get_CurIndex(TONE _TONE, int *CurArrayPlayCnt, int *CurIndex)
982 {
983         int ret = MM_ERROR_NONE;
984         if(CurArrayPlayCnt == NULL || CurIndex == NULL) {
985                 debug_error("CurArrayPlayCnt || CurIndex buffer is NULL\n");
986                 return MM_ERROR_SOUND_INTERNAL;
987         }
988
989         if(_TONE.loopCnt != 0 && *CurArrayPlayCnt <= _TONE.loopCnt) {
990                 (*CurArrayPlayCnt)++;
991                 if(*CurArrayPlayCnt >_TONE.loopCnt) {
992                         (*CurIndex)++;
993                         *CurArrayPlayCnt = 0;
994                 } else {
995                         *CurIndex = _TONE.loopIndx;
996                 }
997         } else {
998                 (*CurIndex)++;
999         }
1000         debug_log("[%d] CurIndex: %d", *CurArrayPlayCnt, *CurIndex);
1001         return ret;
1002 }
1003
1004 static void _running_tone(void *param)
1005 {
1006         char *ptoneBuf = NULL;
1007         char filename[100];
1008
1009         if(param == NULL) {
1010                 debug_error("param Buffer is not allocated\n");
1011                 return;
1012         }
1013         tone_info_t *toneInfo = (tone_info_t*) param;
1014         int toneKey = 0;
1015         int toneTime =0;
1016         int prePlayingTime = 0;
1017         int duration = 0;
1018         int playingTime = 0;
1019         int toneSize = 0;
1020         int CurWaveIndex = 0;
1021         int numWave = 0;
1022         int waveRestPlayTime = 0;
1023         int CurIndex = 0;
1024         int CurArrayPlayCnt = 0;
1025         gboolean is_cycle_ended = FALSE;
1026
1027         debug_enter("\n");
1028         double sample = 0;
1029         toneTime = toneInfo->time;
1030         if(toneTime != 0) {
1031                 toneKey = toneInfo->number;
1032                 debug_msg ("toneKey number = %d\n", toneKey);
1033
1034                 /* Set pid check file */
1035                 snprintf(filename, sizeof(filename), "/proc/%d/cmdline", toneInfo->pid);
1036
1037                 debug_msg("Wait start signal\n");
1038                 while(toneInfo->state == STATE_READY)
1039                         usleep(10);
1040                 debug_msg("Recv start signal\n");
1041                 toneInfo->state = STATE_PLAY;
1042         } else {
1043                 return;
1044         }
1045
1046         while (1) {
1047                 TONE _TONE = _mm_get_tone(toneKey, CurIndex); /*Pop one of Tone Set */
1048                 if(_mm_get_waveCnt_PlayingTime(toneTime, _TONE, &numWave, &waveRestPlayTime) != MM_ERROR_NONE) {
1049                         debug_error("_mm_get_waveCnt_PlayingTime return value error\n");
1050                         goto exit;
1051                 }
1052
1053                 if(_mm_get_CurIndex(_TONE, &CurArrayPlayCnt, &CurIndex) != MM_ERROR_NONE) {
1054                         debug_error("_mm_get_CurIndex return value error\n");
1055                         goto exit;
1056                 }
1057
1058                 debug_log ("Predefined Tone[%d] Total Play time (ms) : %d, _TONE.playing_time: %d _numWave = %d low_frequency: %0.f, middle_frequency: %0.f, high_frequency: %0.f\n",
1059                         CurIndex, toneTime, _TONE.playingTime, numWave, _TONE.low_frequency, _TONE.middle_frequency, _TONE.high_frequency);
1060
1061
1062                 if (_TONE.low_frequency == -1) { /* skip frequency which's value is -1*/
1063                         if((_TONE.loopCnt > 0) && (CurArrayPlayCnt == _TONE.loopCnt)) {
1064                                 is_cycle_ended = TRUE;
1065                                 debug_log("[is_cycle_ended]");
1066                         }
1067                         CurIndex = _TONE.loopIndx;
1068                         continue;
1069                 }
1070
1071                 /* Write pcm data */
1072                 for(CurWaveIndex = 0; CurWaveIndex < numWave+1; CurWaveIndex++) {
1073                         if(CurWaveIndex == numWave ) { /* play the last tone set*/
1074                                 playingTime = waveRestPlayTime;
1075                         } else {
1076                                 playingTime = MAX_DURATION;
1077                         }
1078                         duration = playingTime;
1079
1080                         if(playingTime == 0) {
1081                                 break;
1082                         }
1083
1084                         if(prePlayingTime + playingTime > toneTime && toneTime != -1) {
1085                                 playingTime = toneTime - prePlayingTime;
1086                         }
1087
1088                         ptoneBuf = _create_tone(&sample, _TONE, toneInfo->volume, &toneSize, is_cycle_ended);
1089                         if(ptoneBuf == NULL) {
1090                                 debug_error("Tone Buffer is not allocated\n");
1091                                 goto exit;
1092                         }
1093                         debug_log ("[TONE] Play.....%dth %dms\n", CurWaveIndex, playingTime);
1094                         mm_sound_pa_write(toneInfo->handle, ptoneBuf, ((toneSize * playingTime /duration + 1)>>1)<<1);
1095                         prePlayingTime += playingTime;
1096                         debug_log ("previous_sum: %d\n", prePlayingTime);
1097                         g_free (ptoneBuf);
1098                         ptoneBuf = NULL;
1099
1100                         if(prePlayingTime == toneTime || toneInfo->state != STATE_PLAY) {
1101                                 debug_log ("Finished.....on Total Playing Time : %d _TONE.playing_time: %d\n", prePlayingTime, _TONE.playingTime);
1102                                 mm_sound_pa_drain(toneInfo->handle);
1103                                 debug_log ("Finished.....quit loop\n");
1104                                 goto exit;
1105                         }
1106                         debug_log ("[%d] CurIndex: %d previous_sum: %d\n", CurArrayPlayCnt, CurIndex, prePlayingTime);
1107                 }
1108         }
1109
1110 exit :
1111         if(MM_ERROR_NONE != mm_sound_pa_close(toneInfo->handle))
1112                 debug_error("mm_sound_pa_close() fail. handle(%lu)\n", toneInfo->handle);
1113
1114 #if 0
1115         pthread_mutex_destroy(&g_control.syncker);
1116 #endif
1117
1118         debug_msg("Play end\n");
1119         toneInfo->state = STATE_NONE;
1120
1121         if (toneInfo->stop_cb)
1122                 toneInfo->stop_cb(toneInfo->cb_param);
1123
1124         debug_leave("\n");
1125 }
1126
1127 static
1128 int MMSoundPlugCodecToneStop(MMHandleType handle)
1129 {
1130         tone_info_t *toneInfo = (tone_info_t*) handle;
1131
1132         debug_enter("(handle %x)\n", handle);
1133         toneInfo->state = STATE_STOP;
1134         debug_msg("sent stop signal\n");
1135
1136         while(toneInfo->state != STATE_NONE) {
1137                 usleep(30000);
1138         }
1139
1140         debug_leave("\n");
1141
1142         return MM_ERROR_NONE;
1143 }
1144
1145 static
1146 int MMSoundPlugCodecToneSetThreadPool(int (*func)(void*, void (*)(void*)))
1147 {
1148     debug_enter("(func : 0x%x)\n", func);
1149     g_thread_pool_func = func;
1150     debug_leave("\n");
1151     return MM_ERROR_NONE;
1152 }
1153
1154 #if 0
1155 static int _MMSoundToneInit(void)
1156 {
1157         memset(&g_control, 0, sizeof(tone_control_t));
1158         pthread_mutex_init(&g_control.syncker, NULL);
1159         return MM_ERROR_NONE;
1160 }
1161
1162 static int _MMSoundToneFini(void)
1163 {
1164         pthread_mutex_destroy(&g_control.syncker);
1165         return MM_ERROR_NONE;
1166 }
1167 #endif
1168
1169 EXPORT_API
1170 int MMSoundGetPluginType(void)
1171 {
1172     debug_enter("\n");
1173     debug_leave("\n");
1174     return MM_SOUND_PLUGIN_TYPE_CODEC;
1175 }
1176
1177 EXPORT_API
1178 int MMSoundPlugCodecGetInterface(mmsound_codec_interface_t *intf)
1179 {
1180     debug_enter("\n");
1181
1182     intf->GetSupportTypes   = MMSoundPlugCodecToneGetSupportTypes;
1183     intf->Parse             = MMSoundPlugCodecToneParse;
1184     intf->Create            = MMSoundPlugCodecToneCreate;
1185     intf->Destroy           = MMSoundPlugCodecToneDestroy;
1186     intf->Play              = MMSoundPlugCodecTonePlay;
1187     intf->Stop              = MMSoundPlugCodecToneStop;
1188     intf->SetThreadPool     = MMSoundPlugCodecToneSetThreadPool;
1189
1190     debug_leave("\n");
1191
1192     return MM_ERROR_NONE;
1193 }
1194