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