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