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