2 * Asterisk -- A telephony toolkit for Linux.
6 * Copyright (C) 2002, Pauline Middelink
8 * Pauline Middelink <middelink@polyware.nl>
10 * This program is free software, distributed under the terms of
11 * the GNU General Public License
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.
22 #include <math.h> /* For PI */
26 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
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"
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
52 struct playtones_item {
59 struct playtones_def {
64 struct playtones_item *items;
67 struct playtones_state {
71 struct playtones_item *items;
76 unsigned char offset[AST_FRIENDLY_OFFSET];
80 static void playtones_release(struct ast_channel *chan, void *params)
82 struct playtones_state *ps = params;
84 ast_set_write_format(chan, ps->origwfmt);
86 if (ps->items) free(ps->items);
90 static void * playtones_alloc(struct ast_channel *chan, void *params)
92 struct playtones_def *pd = params;
93 struct playtones_state *ps = malloc(sizeof(struct playtones_state));
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);
104 ps->reppos = pd->reppos;
105 ps->nitems = pd->nitems;
106 ps->items = pd->items;
108 /* Let interrupts interrupt :) */
109 if (pd->interruptible)
110 ast_set_flag(chan, AST_FLAG_WRITE_INT);
112 ast_clear_flag(chan, AST_FLAG_WRITE_INT);
116 static int playtones_generator(struct ast_channel *chan, void *data, int len, int samples)
118 struct playtones_state *ps = data;
119 struct playtones_item *pi;
121 /* we need to prepare a frame with 16 * timelen samples as we're
122 * generating SLIN audio
125 if (len > sizeof(ps->data) / 2 - 1) {
126 ast_log(LOG_WARNING, "Can't generate that much data!\n");
129 memset(&ps->f, 0, sizeof(ps->f));
131 pi = &ps->items[ps->npos];
132 for (x=0;x<len/2;x++) {
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)
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))
146 ps->f.frametype = AST_FRAME_VOICE;
147 ps->f.subclass = AST_FORMAT_SLINEAR;
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);
157 if (pi->duration && ps->pos >= pi->duration * 8) { /* item finished? */
158 ps->pos = 0; /* start new item */
160 if (ps->npos >= ps->nitems) { /* last item? */
161 if (ps->reppos == -1) /* repeat set? */
163 ps->npos = ps->reppos; /* redo from top */
169 static struct ast_generator playtones = {
170 alloc: playtones_alloc,
171 release: playtones_release,
172 generate: playtones_generator,
175 int ast_playtones_start(struct ast_channel *chan, int vol, const char *playlst, int interruptible)
177 char *s, *data = ast_strdupa(playlst); /* cute */
178 struct playtones_def d = { vol, -1, 0, 1, NULL};
186 d.interruptible = interruptible;
189 /* the stringp/data is not null here */
190 /* check if the data is separated with '|' or with ',' by default */
191 if (strchr(stringp,'|'))
195 s = strsep(&stringp,separator);
197 int freq1, freq2, time, modulate=0, midinote=0;
201 else if (d.reppos == -1)
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) {
208 } else if (sscanf(s, "%d*%d/%d", &freq1, &freq2, &time) == 3) {
209 /* f1*f2/time format */
211 } else if (sscanf(s, "%d*%d", &freq1, &freq2) == 2) {
215 } else if (sscanf(s, "%d/%d", &freq1, &time) == 2) {
218 } else if (sscanf(s, "%d", &freq1) == 1) {
222 } else if (sscanf(s, "M%d+M%d/%d", &freq1, &freq2, &time) == 3) {
223 /* Mf1+Mf2/time format */
225 } else if (sscanf(s, "M%d+M%d", &freq1, &freq2) == 2) {
229 } else if (sscanf(s, "M%d*M%d/%d", &freq1, &freq2, &time) == 3) {
230 /* Mf1*Mf2/time format */
233 } else if (sscanf(s, "M%d*M%d", &freq1, &freq2) == 2) {
238 } else if (sscanf(s, "M%d/%d", &freq1, &time) == 2) {
239 /* Mf1/time format */
242 } else if (sscanf(s, "M%d", &freq1) == 1) {
248 ast_log(LOG_WARNING,"%s: tone component '%s' of '%s' is no good\n",chan->name,s,playlst);
253 /* midi notes must be between 0 and 127 */
254 if ((freq1 >= 0) && (freq1 <= 127))
255 freq1 = midi_tohz[freq1];
259 if ((freq2 >= 0) && (freq2 <= 127))
260 freq2 = midi_tohz[freq2];
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");
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;
276 s = strsep(&stringp,separator);
279 if (ast_activate_generator(chan, &playtones, &d)) {
286 void ast_playtones_stop(struct ast_channel *chan)
288 ast_deactivate_generator(chan);
291 /*--------------------------------------------*/
293 struct tone_zone *tone_zones;
294 static struct tone_zone *current_tonezone;
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);
300 /* Set global indication country */
301 int ast_set_indication_country(const char *country)
304 struct tone_zone *z = ast_get_indication_zone(country);
306 if (option_verbose > 2)
307 ast_verbose(VERBOSE_PREFIX_3 "Setting default indication country to '%s'\n",country);
308 current_tonezone = z;
312 return 1; /* not found */
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)
318 struct tone_zone *tz;
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? */
327 return 0; /* not a single country insight */
329 if (ast_mutex_lock(&tzlock)) {
330 ast_log(LOG_WARNING, "Unable to lock tone_zones list\n");
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]) {
341 ast_mutex_unlock(&tzlock);
345 } while (++alias_loop<20 && tz);
346 ast_mutex_unlock(&tzlock);
348 ast_log(LOG_NOTICE,"Alias loop for '%s' forcefull broken\n",country);
349 /* nothing found, sorry */
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)
356 struct tone_zone_sound *ts;
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? */
364 return 0; /* not a single country insight */
366 if (ast_mutex_lock(&tzlock)) {
367 ast_log(LOG_WARNING, "Unable to lock tone_zones list\n");
370 for (ts=zone->tones; ts; ts=ts->next) {
371 if (strcasecmp(indication,ts->name)==0) {
372 /* found indication! */
373 ast_mutex_unlock(&tzlock);
377 /* nothing found, sorry */
378 ast_mutex_unlock(&tzlock);
382 /* helper function to delete a tone_zone in its entirety */
383 static inline void free_zone(struct tone_zone* zone)
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);
392 if (zone->ringcadance)
393 free((void*)zone->ringcadance);
397 /*--------------------------------------------*/
399 /* add a new country, if country exists, it will be replaced. */
400 int ast_register_indication_country(struct tone_zone *zone)
402 struct tone_zone *tz,*pz;
404 if (ast_mutex_lock(&tzlock)) {
405 ast_log(LOG_WARNING, "Unable to lock tone_zones list\n");
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;
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 */
421 ast_mutex_unlock(&tzlock);
425 /* country not there, add */
431 ast_mutex_unlock(&tzlock);
433 if (option_verbose > 2)
434 ast_verbose(VERBOSE_PREFIX_3 "Registered indication country '%s'\n",zone->country);
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)
442 struct tone_zone *tz, *pz = NULL, *tmp;
445 if (ast_mutex_lock(&tzlock)) {
446 ast_log(LOG_WARNING, "Unable to lock tone_zones list\n");
452 (strcasecmp(country, tz->country)==0 ||
453 strcasecmp(country, tz->alias)==0)) {
454 /* tone_zone found, remove */
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;
465 if (option_verbose > 2)
466 ast_verbose(VERBOSE_PREFIX_3 "Unregistered indication country '%s'\n",tz->country);
468 if (tone_zones == tz)
474 /* next zone please */
479 ast_mutex_unlock(&tzlock);
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)
487 struct tone_zone_sound *ts,*ps;
489 /* is it an alias? stop */
493 if (ast_mutex_lock(&tzlock)) {
494 ast_log(LOG_WARNING, "Unable to lock tone_zones list\n");
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);
506 /* not there, we have to add */
507 ts = malloc(sizeof(struct tone_zone_sound));
509 ast_log(LOG_WARNING, "Out of memory\n");
510 ast_mutex_unlock(&tzlock);
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);
526 ast_mutex_unlock(&tzlock);
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)
533 struct tone_zone_sound *ts,*ps = NULL, *tmp;
536 /* is it an alias? stop */
540 if (ast_mutex_lock(&tzlock)) {
541 ast_log(LOG_WARNING, "Unable to lock tone_zones list\n");
546 if (strcasecmp(indication,ts->name)==0) {
547 /* indication found */
553 free((void*)ts->name);
554 free((void*)ts->data);
560 /* next zone please */
565 /* indication not found, goodbye */
566 ast_mutex_unlock(&tzlock);