Fix to generate tones having one amplitude modulated by another: Bug #746
[asterisk/asterisk.git] / indications.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * Tone Management
5  * 
6  * Copyright (C) 2002, Pauline Middelink
7  *
8  * Pauline Middelink <middelink@polyware.nl>
9  *
10  * This program is free software, distributed under the terms of
11  * the GNU General Public License
12  *
13  * This set of function allow us to play a list of tones on a channel.
14  * Each element has two frequencies, which are mixed together and a
15  * duration. For silence both frequencies can be set to 0.
16  * The playtones can be given as a comma seperated string.
17  */
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <pthread.h>
22 #include <string.h>
23 #include <math.h>                       /* For PI */
24 #include <asterisk/indications.h>
25 #include <asterisk/frame.h>
26 #include <asterisk/options.h>
27 #include <asterisk/channel.h>
28 #include <asterisk/logger.h>
29
30 struct playtones_item {
31         int freq1;
32         int freq2;
33         int duration;
34         int modulate;
35 };
36
37 struct playtones_def {
38         int vol;
39         int reppos;
40         int nitems;
41         int interruptible;
42         struct playtones_item *items;
43 };
44
45 struct playtones_state {
46         int vol;
47         int reppos;
48         int nitems;
49         struct playtones_item *items;
50         int npos;
51         int pos;
52         int origwfmt;
53         struct ast_frame f;
54         short data[4000];
55 };
56
57 static void playtones_release(struct ast_channel *chan, void *params)
58 {
59         struct playtones_state *ps = params;
60         if (chan) {
61                 ast_set_write_format(chan, ps->origwfmt);
62         }
63         if (ps->items) free(ps->items);
64         free(ps);
65 }
66
67 static void * playtones_alloc(struct ast_channel *chan, void *params)
68 {
69         struct playtones_def *pd = params;
70         struct playtones_state *ps = malloc(sizeof(struct playtones_state));
71         if (!ps)
72                 return NULL;
73         memset(ps, 0, sizeof(struct playtones_state));
74         ps->origwfmt = chan->writeformat;
75         if (ast_set_write_format(chan, AST_FORMAT_SLINEAR)) {
76                 ast_log(LOG_WARNING, "Unable to set '%s' to signed linear format (write)\n", chan->name);
77                 playtones_release(NULL, ps);
78                 ps = NULL;
79         } else {
80                 ps->vol = pd->vol;
81                 ps->reppos = pd->reppos;
82                 ps->nitems = pd->nitems;
83                 ps->items = pd->items;
84         }
85         /* Let interrupts interrupt :) */
86         chan->writeinterrupt = pd->interruptible;
87         return ps;
88 }
89
90 static int playtones_generator(struct ast_channel *chan, void *data, int len, int samples)
91 {
92         struct playtones_state *ps = data;
93         struct playtones_item *pi;
94         int x;
95         /* we need to prepare a frame with 16 * timelen samples as we're 
96          * generating SLIN audio
97          */
98         len = samples * 2;
99         if (len > sizeof(ps->data) / 2 - 1) {
100                 ast_log(LOG_WARNING, "Can't generate that much data!\n");
101                 return -1;
102         }
103         memset(&ps->f, 0, sizeof(ps->f));
104
105         pi = &ps->items[ps->npos];
106         for (x=0;x<len/2;x++) {
107                 if (pi->modulate)
108                 /* Modulate 1st tone with 2nd, to 90% modulation depth */
109                 ps->data[x] = ps->vol * 2 * (
110                         sin((pi->freq1 * 2.0 * M_PI / 8000.0) * (ps->pos + x)) *
111                         (0.9 * fabs(sin((pi->freq2 * 2.0 * M_PI / 8000.0) * (ps->pos + x))) + 0.1)
112                         );
113                 else
114                         /* Add 2 tones together */
115                         ps->data[x] = ps->vol * (
116                                 sin((pi->freq1 * 2.0 * M_PI / 8000.0) * (ps->pos + x)) +
117                                 sin((pi->freq2 * 2.0 * M_PI / 8000.0) * (ps->pos + x))
118                         );
119         }
120         ps->f.frametype = AST_FRAME_VOICE;
121         ps->f.subclass = AST_FORMAT_SLINEAR;
122         ps->f.datalen = len;
123         ps->f.samples = samples;
124         ps->f.offset = AST_FRIENDLY_OFFSET;
125         ps->f.data = ps->data;
126         ast_write(chan, &ps->f);
127
128         ps->pos += x;
129         if (pi->duration && ps->pos >= pi->duration * 8) {      /* item finished? */
130                 ps->pos = 0;                                    /* start new item */
131                 ps->npos++;
132                 if (ps->npos >= ps->nitems) {                   /* last item? */
133                         if (ps->reppos == -1)                   /* repeat set? */
134                                 return -1;
135                         ps->npos = ps->reppos;                  /* redo from top */
136                 }
137         }
138         return 0;
139 }
140
141 static struct ast_generator playtones = {
142         alloc: playtones_alloc,
143         release: playtones_release,
144         generate: playtones_generator,
145 };
146
147 int ast_playtones_start(struct ast_channel *chan, int vol, const char *playlst, int interruptible)
148 {
149         char *s, *data = ast_strdupa(playlst); /* cute */
150         struct playtones_def d = { vol, -1, 0, 1, NULL};
151         char *stringp=NULL;
152         char *separator;
153         if (!data)
154                 return -1;
155         if (vol < 1)
156                 d.vol = 8192;
157
158         d.interruptible = interruptible;
159         
160         stringp=data;
161         /* the stringp/data is not null here */
162         /* check if the data is separated with '|' or with ',' by default */
163         if (strchr(stringp,'|'))
164                 separator = "|";
165         else
166                 separator = ",";
167         s = strsep(&stringp,separator);
168         while(s && *s) {
169                 int freq1, freq2, time, modulate=0;
170
171                 if (s[0]=='!')
172                         s++;
173                 else if (d.reppos == -1)
174                         d.reppos = d.nitems;
175                 if (sscanf(s, "%d+%d/%d", &freq1, &freq2, &time) == 3) {
176                         /* f1+f2/time format */
177                 } else if (sscanf(s, "%d+%d", &freq1, &freq2) == 2) {
178                         /* f1+f2 format */
179                         time = 0;
180                 } else if (sscanf(s, "%d*%d/%d", &freq1, &freq2, &time) == 3) {
181                         /* f1*f2/time format */
182                         modulate = 1;
183                 } else if (sscanf(s, "%d*%d", &freq1, &freq2) == 2) {
184                         /* f1*f2 format */
185                         time = 0;
186                         modulate = 1;
187                 } else if (sscanf(s, "%d/%d", &freq1, &time) == 2) {
188                         /* f1/time format */
189                         freq2 = 0;
190                 } else if (sscanf(s, "%d", &freq1) == 1) {
191                         /* f1 format */
192                         freq2 = 0;
193                         time = 0;
194                 } else {
195                         ast_log(LOG_WARNING,"%s: tone component '%s' of '%s' is no good\n",chan->name,s,playlst);
196                         return -1;
197                 }
198
199                 d.items = realloc(d.items,(d.nitems+1)*sizeof(struct playtones_item));
200                 if (d.items == NULL)
201                         return -1;
202                 d.items[d.nitems].freq1    = freq1;
203                 d.items[d.nitems].freq2    = freq2;
204                 d.items[d.nitems].duration = time;
205                 d.items[d.nitems].modulate = modulate;
206                 d.nitems++;
207
208                 s = strsep(&stringp,separator);
209         }
210
211         if (ast_activate_generator(chan, &playtones, &d)) {
212                 free(d.items);
213                 return -1;
214         }
215         return 0;
216 }
217
218 void ast_playtones_stop(struct ast_channel *chan)
219 {
220         ast_deactivate_generator(chan);
221 }
222
223 /*--------------------------------------------*/
224
225 struct tone_zone *tone_zones;
226 static struct tone_zone *current_tonezone;
227
228 /* Protect the tone_zones list (highly unlikely that two things would change
229  * it at the same time, but still! */
230 ast_mutex_t tzlock = AST_MUTEX_INITIALIZER;
231
232 /* Set global indication country */
233 int ast_set_indication_country(const char *country)
234 {
235         if (country) {
236                 struct tone_zone *z = ast_get_indication_zone(country);
237                 if (z) {
238                         ast_verbose(VERBOSE_PREFIX_3 "Setting default indication country to '%s'\n",country);
239                         current_tonezone = z;
240                         return 0;
241                 }
242         }
243         return 1; /* not found */
244 }
245
246 /* locate tone_zone, given the country. if country == NULL, use the default country */
247 struct tone_zone *ast_get_indication_zone(const char *country)
248 {
249         struct tone_zone *tz;
250         int alias_loop = 0;
251
252         /* we need some tonezone, pick the first */
253         if (country == NULL && current_tonezone)
254                 return current_tonezone;        /* default country? */
255         if (country == NULL && tone_zones)
256                 return tone_zones;              /* any country? */
257         if (country == NULL)
258                 return 0;       /* not a single country insight */
259
260         if (ast_mutex_lock(&tzlock)) {
261                 ast_log(LOG_WARNING, "Unable to lock tone_zones list\n");
262                 return 0;
263         }
264         do {
265                 for (tz=tone_zones; tz; tz=tz->next) {
266                         if (strcasecmp(country,tz->country)==0) {
267                                 /* tone_zone found */
268                                 if (tz->alias && tz->alias[0]) {
269                                         country = tz->alias;
270                                         break;
271                                 }
272                                 ast_mutex_unlock(&tzlock);
273                                 return tz;
274                         }
275                 }
276         } while (++alias_loop<20 && tz);
277         ast_mutex_unlock(&tzlock);
278         if (alias_loop==20)
279                 ast_log(LOG_NOTICE,"Alias loop for '%s' forcefull broken\n",country);
280         /* nothing found, sorry */
281         return 0;
282 }
283
284 /* locate a tone_zone_sound, given the tone_zone. if tone_zone == NULL, use the default tone_zone */
285 struct tone_zone_sound *ast_get_indication_tone(const struct tone_zone *zone, const char *indication)
286 {
287         struct tone_zone_sound *ts;
288
289         /* we need some tonezone, pick the first */
290         if (zone == NULL && current_tonezone)
291                 zone = current_tonezone;        /* default country? */
292         if (zone == NULL && tone_zones)
293                 zone = tone_zones;              /* any country? */
294         if (zone == NULL)
295                 return 0;       /* not a single country insight */
296
297         if (ast_mutex_lock(&tzlock)) {
298                 ast_log(LOG_WARNING, "Unable to lock tone_zones list\n");
299                 return 0;
300         }
301         for (ts=zone->tones; ts; ts=ts->next) {
302                 if (strcasecmp(indication,ts->name)==0) {
303                         /* found indication! */
304                         ast_mutex_unlock(&tzlock);
305                         return ts;
306                 }
307         }
308         /* nothing found, sorry */
309         ast_mutex_unlock(&tzlock);
310         return 0;
311 }
312
313 /* helper function to delete a tone_zone in its entirety */
314 static inline void free_zone(struct tone_zone* zone)
315 {
316         while (zone->tones) {
317                 struct tone_zone_sound *tmp = zone->tones->next;
318                 free((void*)zone->tones->name);
319                 free((void*)zone->tones->data);
320                 free(zone->tones);
321                 zone->tones = tmp;
322         }
323         free(zone);
324 }
325
326 /*--------------------------------------------*/
327
328 /* add a new country, if country exists, it will be replaced. */
329 int ast_register_indication_country(struct tone_zone *zone)
330 {
331         struct tone_zone *tz,*pz;
332
333         if (ast_mutex_lock(&tzlock)) {
334                 ast_log(LOG_WARNING, "Unable to lock tone_zones list\n");
335                 return -1;
336         }
337         for (pz=NULL,tz=tone_zones; tz; pz=tz,tz=tz->next) {
338                 if (strcasecmp(zone->country,tz->country)==0) {
339                         /* tone_zone already there, replace */
340                         zone->next = tz->next;
341                         if (pz)
342                                 pz->next = zone;
343                         else
344                                 tone_zones = zone;
345                         /* if we are replacing the default zone, re-point it */
346                         if (tz == current_tonezone)
347                                 current_tonezone = zone;
348                         /* now free the previous zone */
349                         free_zone(tz);
350                         ast_mutex_unlock(&tzlock);
351                         return 0;
352                 }
353         }
354         /* country not there, add */
355         zone->next = NULL;
356         if (pz)
357                 pz->next = zone;
358         else
359                 tone_zones = zone;
360         ast_mutex_unlock(&tzlock);
361
362         ast_verbose(VERBOSE_PREFIX_3 "Registered indication country '%s'\n",zone->country);
363         return 0;
364 }
365
366 /* remove an existing country and all its indications, country must exist.
367  * Also, all countries which are an alias for the specified country are removed. */
368 int ast_unregister_indication_country(const char *country)
369 {
370         struct tone_zone *tz, *pz = NULL, *tmp;
371         int res = -1;
372
373         if (ast_mutex_lock(&tzlock)) {
374                 ast_log(LOG_WARNING, "Unable to lock tone_zones list\n");
375                 return -1;
376         }
377         tz = tone_zones;
378         while (tz) {
379                 if (country==NULL ||
380                     (strcasecmp(country, tz->country)==0 ||
381                      strcasecmp(country, tz->alias)==0)) {
382                         /* tone_zone found, remove */
383                         tmp = tz->next;
384                         if (pz)
385                                 pz->next = tmp;
386                         else
387                                 tone_zones = tmp;
388                         /* if we are unregistering the default country, w'll notice */
389                         if (tz == current_tonezone) {
390                                 ast_log(LOG_NOTICE,"Removed default indication country '%s'\n",tz->country);
391                                 current_tonezone = NULL;
392                         }
393                         ast_verbose(VERBOSE_PREFIX_3 "Unregistered indication country '%s'\n",tz->country);
394                         free_zone(tz);
395                         tz = tmp;
396                         res = 0;
397                 }
398                 else {
399                         /* next zone please */
400                         pz = tz;
401                         tz = tz->next;
402                 }
403         }
404         ast_mutex_unlock(&tzlock);
405         return res;
406 }
407
408 /* add a new indication to a tone_zone. tone_zone must exist. if the indication already
409  * exists, it will be replaced. */
410 int ast_register_indication(struct tone_zone *zone, const char *indication, const char *tonelist)
411 {
412         struct tone_zone_sound *ts,*ps;
413
414         /* is it an alias? stop */
415         if (zone->alias[0])
416                 return -1;
417
418         if (ast_mutex_lock(&tzlock)) {
419                 ast_log(LOG_WARNING, "Unable to lock tone_zones list\n");
420                 return -2;
421         }
422         for (ps=NULL,ts=zone->tones; ts; ps=ts,ts=ts->next) {
423                 if (strcasecmp(indication,ts->name)==0) {
424                         /* indication already there, replace */
425                         free((void*)ts->name);
426                         free((void*)ts->data);
427                         break;
428                 }
429         }
430         if (!ts) {
431                 /* not there, we have to add */
432                 ts = malloc(sizeof(struct tone_zone_sound));
433                 if (!ts) {
434                         ast_log(LOG_WARNING, "Out of memory\n");
435                         ast_mutex_unlock(&tzlock);
436                         return -2;
437                 }
438                 ts->next = NULL;
439         }
440         ts->name = strdup(indication);
441         ts->data = strdup(tonelist);
442         if (ts->name==NULL || ts->data==NULL) {
443                 ast_log(LOG_WARNING, "Out of memory\n");
444                 ast_mutex_unlock(&tzlock);
445                 return -2;
446         }
447         if (ps)
448                 ps->next = ts;
449         else
450                 zone->tones = ts;
451         ast_mutex_unlock(&tzlock);
452         return 0;
453 }
454
455 /* remove an existing country's indication. Both country and indication must exist */
456 int ast_unregister_indication(struct tone_zone *zone, const char *indication)
457 {
458         struct tone_zone_sound *ts,*ps = NULL, *tmp;
459         int res = -1;
460
461         /* is it an alias? stop */
462         if (zone->alias[0])
463                 return -1;
464
465         if (ast_mutex_lock(&tzlock)) {
466                 ast_log(LOG_WARNING, "Unable to lock tone_zones list\n");
467                 return -1;
468         }
469         ts = zone->tones;
470         while (ts) {
471                 if (strcasecmp(indication,ts->name)==0) {
472                         /* indication found */
473                         tmp = ts->next;
474                         if (ps)
475                                 ps->next = tmp;
476                         else
477                                 zone->tones = tmp;
478                         free((void*)ts->name);
479                         free((void*)ts->data);
480                         free(ts);
481                         ts = tmp;
482                         res = 0;
483                 }
484                 else {
485                         /* next zone please */
486                         ps = ts;
487                         ts = ts->next;
488                 }
489         }
490         /* indication not found, goodbye */
491         ast_mutex_unlock(&tzlock);
492         return res;
493 }