Merge midi changes (bug #4441)
[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 separated string.
17  */
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <math.h>                       /* For PI */
23
24 #include "asterisk.h"
25
26 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
27
28 #include "asterisk/indications.h"
29 #include "asterisk/frame.h"
30 #include "asterisk/options.h"
31 #include "asterisk/channel.h"
32 #include "asterisk/logger.h"
33 #include "asterisk/lock.h"
34 #include "asterisk/utils.h"
35
36 static int midi_tohz[128] = {
37                         8,8,9,9,10,10,11,12,12,13,14,
38                         15,16,17,18,19,20,21,23,24,25,
39                         27,29,30,32,34,36,38,41,43,46,
40                         48,51,55,58,61,65,69,73,77,82,
41                         87,92,97,103,110,116,123,130,138,146,
42                         155,164,174,184,195,207,220,233,246,261,
43                         277,293,311,329,349,369,391,415,440,466,
44                         493,523,554,587,622,659,698,739,783,830,
45                         880,932,987,1046,1108,1174,1244,1318,1396,1479,
46                         1567,1661,1760,1864,1975,2093,2217,2349,2489,2637,
47                         2793,2959,3135,3322,3520,3729,3951,4186,4434,4698,
48                         4978,5274,5587,5919,6271,6644,7040,7458,7902,8372,
49                         8869,9397,9956,10548,11175,11839,12543
50                         };
51
52 struct playtones_item {
53         int freq1;
54         int freq2;
55         int duration;
56         int modulate;
57 };
58
59 struct playtones_def {
60         int vol;
61         int reppos;
62         int nitems;
63         int interruptible;
64         struct playtones_item *items;
65 };
66
67 struct playtones_state {
68         int vol;
69         int reppos;
70         int nitems;
71         struct playtones_item *items;
72         int npos;
73         int pos;
74         int origwfmt;
75         struct ast_frame f;
76         unsigned char offset[AST_FRIENDLY_OFFSET];
77         short data[4000];
78 };
79
80 static void playtones_release(struct ast_channel *chan, void *params)
81 {
82         struct playtones_state *ps = params;
83         if (chan) {
84                 ast_set_write_format(chan, ps->origwfmt);
85         }
86         if (ps->items) free(ps->items);
87         free(ps);
88 }
89
90 static void * playtones_alloc(struct ast_channel *chan, void *params)
91 {
92         struct playtones_def *pd = params;
93         struct playtones_state *ps = malloc(sizeof(struct playtones_state));
94         if (!ps)
95                 return NULL;
96         memset(ps, 0, sizeof(struct playtones_state));
97         ps->origwfmt = chan->writeformat;
98         if (ast_set_write_format(chan, AST_FORMAT_SLINEAR)) {
99                 ast_log(LOG_WARNING, "Unable to set '%s' to signed linear format (write)\n", chan->name);
100                 playtones_release(NULL, ps);
101                 ps = NULL;
102         } else {
103                 ps->vol = pd->vol;
104                 ps->reppos = pd->reppos;
105                 ps->nitems = pd->nitems;
106                 ps->items = pd->items;
107         }
108         /* Let interrupts interrupt :) */
109         if (pd->interruptible)
110                 ast_set_flag(chan, AST_FLAG_WRITE_INT);
111         else
112                 ast_clear_flag(chan, AST_FLAG_WRITE_INT);
113         return ps;
114 }
115
116 static int playtones_generator(struct ast_channel *chan, void *data, int len, int samples)
117 {
118         struct playtones_state *ps = data;
119         struct playtones_item *pi;
120         int x;
121         /* we need to prepare a frame with 16 * timelen samples as we're 
122          * generating SLIN audio
123          */
124         len = samples * 2;
125         if (len > sizeof(ps->data) / 2 - 1) {
126                 ast_log(LOG_WARNING, "Can't generate that much data!\n");
127                 return -1;
128         }
129         memset(&ps->f, 0, sizeof(ps->f));
130
131         pi = &ps->items[ps->npos];
132         for (x=0;x<len/2;x++) {
133                 if (pi->modulate)
134                 /* Modulate 1st tone with 2nd, to 90% modulation depth */
135                 ps->data[x] = ps->vol * 2 * (
136                         sin((pi->freq1 * 2.0 * M_PI / 8000.0) * (ps->pos + x)) *
137                         (0.9 * fabs(sin((pi->freq2 * 2.0 * M_PI / 8000.0) * (ps->pos + x))) + 0.1)
138                         );
139                 else
140                         /* Add 2 tones together */
141                         ps->data[x] = ps->vol * (
142                                 sin((pi->freq1 * 2.0 * M_PI / 8000.0) * (ps->pos + x)) +
143                                 sin((pi->freq2 * 2.0 * M_PI / 8000.0) * (ps->pos + x))
144                         );
145         }
146         ps->f.frametype = AST_FRAME_VOICE;
147         ps->f.subclass = AST_FORMAT_SLINEAR;
148         ps->f.datalen = len;
149         ps->f.samples = samples;
150         ps->f.offset = AST_FRIENDLY_OFFSET;
151         ps->f.data = ps->data;
152         ps->f.delivery.tv_sec = 0;
153         ps->f.delivery.tv_usec = 0;
154         ast_write(chan, &ps->f);
155
156         ps->pos += x;
157         if (pi->duration && ps->pos >= pi->duration * 8) {      /* item finished? */
158                 ps->pos = 0;                                    /* start new item */
159                 ps->npos++;
160                 if (ps->npos >= ps->nitems) {                   /* last item? */
161                         if (ps->reppos == -1)                   /* repeat set? */
162                                 return -1;
163                         ps->npos = ps->reppos;                  /* redo from top */
164                 }
165         }
166         return 0;
167 }
168
169 static struct ast_generator playtones = {
170         alloc: playtones_alloc,
171         release: playtones_release,
172         generate: playtones_generator,
173 };
174
175 int ast_playtones_start(struct ast_channel *chan, int vol, const char *playlst, int interruptible)
176 {
177         char *s, *data = ast_strdupa(playlst); /* cute */
178         struct playtones_def d = { vol, -1, 0, 1, NULL};
179         char *stringp=NULL;
180         char *separator;
181         if (!data)
182                 return -1;
183         if (vol < 1)
184                 d.vol = 8192;
185
186         d.interruptible = interruptible;
187         
188         stringp=data;
189         /* the stringp/data is not null here */
190         /* check if the data is separated with '|' or with ',' by default */
191         if (strchr(stringp,'|'))
192                 separator = "|";
193         else
194                 separator = ",";
195         s = strsep(&stringp,separator);
196         while (s && *s) {
197                 int freq1, freq2, time, modulate=0, midinote=0;
198
199                 if (s[0]=='!')
200                         s++;
201                 else if (d.reppos == -1)
202                         d.reppos = d.nitems;
203                 if (sscanf(s, "%d+%d/%d", &freq1, &freq2, &time) == 3) {
204                         /* f1+f2/time format */
205                 } else if (sscanf(s, "%d+%d", &freq1, &freq2) == 2) {
206                         /* f1+f2 format */
207                         time = 0;
208                 } else if (sscanf(s, "%d*%d/%d", &freq1, &freq2, &time) == 3) {
209                         /* f1*f2/time format */
210                         modulate = 1;
211                 } else if (sscanf(s, "%d*%d", &freq1, &freq2) == 2) {
212                         /* f1*f2 format */
213                         time = 0;
214                         modulate = 1;
215                 } else if (sscanf(s, "%d/%d", &freq1, &time) == 2) {
216                         /* f1/time format */
217                         freq2 = 0;
218                 } else if (sscanf(s, "%d", &freq1) == 1) {
219                         /* f1 format */
220                         freq2 = 0;
221                         time = 0;
222                 } else if (sscanf(s, "M%d+M%d/%d", &freq1, &freq2, &time) == 3) {
223                         /* Mf1+Mf2/time format */
224                         midinote = 1;
225                 } else if (sscanf(s, "M%d+M%d", &freq1, &freq2) == 2) {
226                         /* Mf1+Mf2 format */
227                         time = 0;
228                         midinote = 1;
229                 } else if (sscanf(s, "M%d*M%d/%d", &freq1, &freq2, &time) == 3) {
230                         /* Mf1*Mf2/time format */
231                         modulate = 1;
232                         midinote = 1;
233                 } else if (sscanf(s, "M%d*M%d", &freq1, &freq2) == 2) {
234                         /* Mf1*Mf2 format */
235                         time = 0;
236                         modulate = 1;
237                         midinote = 1;
238                 } else if (sscanf(s, "M%d/%d", &freq1, &time) == 2) {
239                         /* Mf1/time format */
240                         freq2 = -1;
241                         midinote = 1;
242                 } else if (sscanf(s, "M%d", &freq1) == 1) {
243                         /* Mf1 format */
244                         freq2 = -1;
245                         time = 0;
246                         midinote = 1;
247                 } else {
248                         ast_log(LOG_WARNING,"%s: tone component '%s' of '%s' is no good\n",chan->name,s,playlst);
249                         return -1;
250                 }
251
252                 if (midinote) {
253                         /* midi notes must be between 0 and 127 */
254                         if ((freq1 >= 0) && (freq1 <= 127))
255                                 freq1 = midi_tohz[freq1];
256                         else
257                                 freq1 = 0;
258
259                         if ((freq2 >= 0) && (freq2 <= 127))
260                                 freq2 = midi_tohz[freq2];
261                         else
262                                 freq2 = 0;
263                 }
264
265                 d.items = realloc(d.items,(d.nitems+1)*sizeof(struct playtones_item));
266                 if (d.items == NULL) {
267                         ast_log(LOG_WARNING, "Realloc failed!\n");
268                         return -1;
269                 }
270                 d.items[d.nitems].freq1    = freq1;
271                 d.items[d.nitems].freq2    = freq2;
272                 d.items[d.nitems].duration = time;
273                 d.items[d.nitems].modulate = modulate;
274                 d.nitems++;
275
276                 s = strsep(&stringp,separator);
277         }
278
279         if (ast_activate_generator(chan, &playtones, &d)) {
280                 free(d.items);
281                 return -1;
282         }
283         return 0;
284 }
285
286 void ast_playtones_stop(struct ast_channel *chan)
287 {
288         ast_deactivate_generator(chan);
289 }
290
291 /*--------------------------------------------*/
292
293 struct tone_zone *tone_zones;
294 static struct tone_zone *current_tonezone;
295
296 /* Protect the tone_zones list (highly unlikely that two things would change
297  * it at the same time, but still! */
298 AST_MUTEX_DEFINE_EXPORTED(tzlock);
299
300 /* Set global indication country */
301 int ast_set_indication_country(const char *country)
302 {
303         if (country) {
304                 struct tone_zone *z = ast_get_indication_zone(country);
305                 if (z) {
306                         if (option_verbose > 2)
307                                 ast_verbose(VERBOSE_PREFIX_3 "Setting default indication country to '%s'\n",country);
308                         current_tonezone = z;
309                         return 0;
310                 }
311         }
312         return 1; /* not found */
313 }
314
315 /* locate tone_zone, given the country. if country == NULL, use the default country */
316 struct tone_zone *ast_get_indication_zone(const char *country)
317 {
318         struct tone_zone *tz;
319         int alias_loop = 0;
320
321         /* we need some tonezone, pick the first */
322         if (country == NULL && current_tonezone)
323                 return current_tonezone;        /* default country? */
324         if (country == NULL && tone_zones)
325                 return tone_zones;              /* any country? */
326         if (country == NULL)
327                 return 0;       /* not a single country insight */
328
329         if (ast_mutex_lock(&tzlock)) {
330                 ast_log(LOG_WARNING, "Unable to lock tone_zones list\n");
331                 return 0;
332         }
333         do {
334                 for (tz=tone_zones; tz; tz=tz->next) {
335                         if (strcasecmp(country,tz->country)==0) {
336                                 /* tone_zone found */
337                                 if (tz->alias && tz->alias[0]) {
338                                         country = tz->alias;
339                                         break;
340                                 }
341                                 ast_mutex_unlock(&tzlock);
342                                 return tz;
343                         }
344                 }
345         } while (++alias_loop<20 && tz);
346         ast_mutex_unlock(&tzlock);
347         if (alias_loop==20)
348                 ast_log(LOG_NOTICE,"Alias loop for '%s' forcefull broken\n",country);
349         /* nothing found, sorry */
350         return 0;
351 }
352
353 /* locate a tone_zone_sound, given the tone_zone. if tone_zone == NULL, use the default tone_zone */
354 struct tone_zone_sound *ast_get_indication_tone(const struct tone_zone *zone, const char *indication)
355 {
356         struct tone_zone_sound *ts;
357
358         /* we need some tonezone, pick the first */
359         if (zone == NULL && current_tonezone)
360                 zone = current_tonezone;        /* default country? */
361         if (zone == NULL && tone_zones)
362                 zone = tone_zones;              /* any country? */
363         if (zone == NULL)
364                 return 0;       /* not a single country insight */
365
366         if (ast_mutex_lock(&tzlock)) {
367                 ast_log(LOG_WARNING, "Unable to lock tone_zones list\n");
368                 return 0;
369         }
370         for (ts=zone->tones; ts; ts=ts->next) {
371                 if (strcasecmp(indication,ts->name)==0) {
372                         /* found indication! */
373                         ast_mutex_unlock(&tzlock);
374                         return ts;
375                 }
376         }
377         /* nothing found, sorry */
378         ast_mutex_unlock(&tzlock);
379         return 0;
380 }
381
382 /* helper function to delete a tone_zone in its entirety */
383 static inline void free_zone(struct tone_zone* zone)
384 {
385         while (zone->tones) {
386                 struct tone_zone_sound *tmp = zone->tones->next;
387                 free((void*)zone->tones->name);
388                 free((void*)zone->tones->data);
389                 free(zone->tones);
390                 zone->tones = tmp;
391         }
392         if (zone->ringcadance)
393                 free((void*)zone->ringcadance);
394         free(zone);
395 }
396
397 /*--------------------------------------------*/
398
399 /* add a new country, if country exists, it will be replaced. */
400 int ast_register_indication_country(struct tone_zone *zone)
401 {
402         struct tone_zone *tz,*pz;
403
404         if (ast_mutex_lock(&tzlock)) {
405                 ast_log(LOG_WARNING, "Unable to lock tone_zones list\n");
406                 return -1;
407         }
408         for (pz=NULL,tz=tone_zones; tz; pz=tz,tz=tz->next) {
409                 if (strcasecmp(zone->country,tz->country)==0) {
410                         /* tone_zone already there, replace */
411                         zone->next = tz->next;
412                         if (pz)
413                                 pz->next = zone;
414                         else
415                                 tone_zones = zone;
416                         /* if we are replacing the default zone, re-point it */
417                         if (tz == current_tonezone)
418                                 current_tonezone = zone;
419                         /* now free the previous zone */
420                         free_zone(tz);
421                         ast_mutex_unlock(&tzlock);
422                         return 0;
423                 }
424         }
425         /* country not there, add */
426         zone->next = NULL;
427         if (pz)
428                 pz->next = zone;
429         else
430                 tone_zones = zone;
431         ast_mutex_unlock(&tzlock);
432
433         if (option_verbose > 2)
434                 ast_verbose(VERBOSE_PREFIX_3 "Registered indication country '%s'\n",zone->country);
435         return 0;
436 }
437
438 /* remove an existing country and all its indications, country must exist.
439  * Also, all countries which are an alias for the specified country are removed. */
440 int ast_unregister_indication_country(const char *country)
441 {
442         struct tone_zone *tz, *pz = NULL, *tmp;
443         int res = -1;
444
445         if (ast_mutex_lock(&tzlock)) {
446                 ast_log(LOG_WARNING, "Unable to lock tone_zones list\n");
447                 return -1;
448         }
449         tz = tone_zones;
450         while (tz) {
451                 if (country==NULL ||
452                     (strcasecmp(country, tz->country)==0 ||
453                      strcasecmp(country, tz->alias)==0)) {
454                         /* tone_zone found, remove */
455                         tmp = tz->next;
456                         if (pz)
457                                 pz->next = tmp;
458                         else
459                                 tone_zones = tmp;
460                         /* if we are unregistering the default country, w'll notice */
461                         if (tz == current_tonezone) {
462                                 ast_log(LOG_NOTICE,"Removed default indication country '%s'\n",tz->country);
463                                 current_tonezone = NULL;
464                         }
465                         if (option_verbose > 2)
466                                 ast_verbose(VERBOSE_PREFIX_3 "Unregistered indication country '%s'\n",tz->country);
467                         free_zone(tz);
468                         if (tone_zones == tz)
469                                 tone_zones = tmp;
470                         tz = tmp;
471                         res = 0;
472                 }
473                 else {
474                         /* next zone please */
475                         pz = tz;
476                         tz = tz->next;
477                 }
478         }
479         ast_mutex_unlock(&tzlock);
480         return res;
481 }
482
483 /* add a new indication to a tone_zone. tone_zone must exist. if the indication already
484  * exists, it will be replaced. */
485 int ast_register_indication(struct tone_zone *zone, const char *indication, const char *tonelist)
486 {
487         struct tone_zone_sound *ts,*ps;
488
489         /* is it an alias? stop */
490         if (zone->alias[0])
491                 return -1;
492
493         if (ast_mutex_lock(&tzlock)) {
494                 ast_log(LOG_WARNING, "Unable to lock tone_zones list\n");
495                 return -2;
496         }
497         for (ps=NULL,ts=zone->tones; ts; ps=ts,ts=ts->next) {
498                 if (strcasecmp(indication,ts->name)==0) {
499                         /* indication already there, replace */
500                         free((void*)ts->name);
501                         free((void*)ts->data);
502                         break;
503                 }
504         }
505         if (!ts) {
506                 /* not there, we have to add */
507                 ts = malloc(sizeof(struct tone_zone_sound));
508                 if (!ts) {
509                         ast_log(LOG_WARNING, "Out of memory\n");
510                         ast_mutex_unlock(&tzlock);
511                         return -2;
512                 }
513                 ts->next = NULL;
514         }
515         ts->name = strdup(indication);
516         ts->data = strdup(tonelist);
517         if (ts->name==NULL || ts->data==NULL) {
518                 ast_log(LOG_WARNING, "Out of memory\n");
519                 ast_mutex_unlock(&tzlock);
520                 return -2;
521         }
522         if (ps)
523                 ps->next = ts;
524         else
525                 zone->tones = ts;
526         ast_mutex_unlock(&tzlock);
527         return 0;
528 }
529
530 /* remove an existing country's indication. Both country and indication must exist */
531 int ast_unregister_indication(struct tone_zone *zone, const char *indication)
532 {
533         struct tone_zone_sound *ts,*ps = NULL, *tmp;
534         int res = -1;
535
536         /* is it an alias? stop */
537         if (zone->alias[0])
538                 return -1;
539
540         if (ast_mutex_lock(&tzlock)) {
541                 ast_log(LOG_WARNING, "Unable to lock tone_zones list\n");
542                 return -1;
543         }
544         ts = zone->tones;
545         while (ts) {
546                 if (strcasecmp(indication,ts->name)==0) {
547                         /* indication found */
548                         tmp = ts->next;
549                         if (ps)
550                                 ps->next = tmp;
551                         else
552                                 zone->tones = tmp;
553                         free((void*)ts->name);
554                         free((void*)ts->data);
555                         free(ts);
556                         ts = tmp;
557                         res = 0;
558                 }
559                 else {
560                         /* next zone please */
561                         ps = ts;
562                         ts = ts->next;
563                 }
564         }
565         /* indication not found, goodbye */
566         ast_mutex_unlock(&tzlock);
567         return res;
568 }