2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2002, Pauline Middelink
7 * See http://www.asterisk.org for more information about
8 * the Asterisk project. Please do not directly contact
9 * any of the maintainers of this project for assistance;
10 * the project provides a web site, mailing lists and IRC
11 * channels for your use.
13 * This program is free software, distributed under the terms of
14 * the GNU General Public License Version 2. See the LICENSE file
15 * at the top of the source tree.
20 * \brief Tone Management
22 * \author Pauline Middelink <middelink@polyware.nl>
24 * This set of function allow us to play a list of tones on a channel.
25 * Each element has two frequencies, which are mixed together and a
26 * duration. For silence both frequencies can be set to 0.
27 * The playtones can be given as a comma separated string.
33 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
40 #include "asterisk/lock.h"
41 #include "asterisk/linkedlists.h"
42 #include "asterisk/indications.h"
43 #include "asterisk/frame.h"
44 #include "asterisk/options.h"
45 #include "asterisk/channel.h"
46 #include "asterisk/logger.h"
47 #include "asterisk/utils.h"
49 static int midi_tohz[128] = {
50 8,8,9,9,10,10,11,12,12,13,14,
51 15,16,17,18,19,20,21,23,24,25,
52 27,29,30,32,34,36,38,41,43,46,
53 48,51,55,58,61,65,69,73,77,82,
54 87,92,97,103,110,116,123,130,138,146,
55 155,164,174,184,195,207,220,233,246,261,
56 277,293,311,329,349,369,391,415,440,466,
57 493,523,554,587,622,659,698,739,783,830,
58 880,932,987,1046,1108,1174,1244,1318,1396,1479,
59 1567,1661,1760,1864,1975,2093,2217,2349,2489,2637,
60 2793,2959,3135,3322,3520,3729,3951,4186,4434,4698,
61 4978,5274,5587,5919,6271,6644,7040,7458,7902,8372,
62 8869,9397,9956,10548,11175,11839,12543
65 struct playtones_item {
76 struct playtones_def {
81 struct playtones_item *items;
84 struct playtones_state {
94 struct playtones_item *items;
100 unsigned char offset[AST_FRIENDLY_OFFSET];
104 static void playtones_release(struct ast_channel *chan, void *params)
106 struct playtones_state *ps = params;
109 ast_set_write_format(chan, ps->origwfmt);
116 static void * playtones_alloc(struct ast_channel *chan, void *params)
118 struct playtones_def *pd = params;
119 struct playtones_state *ps = NULL;
121 if (!(ps = ast_calloc(1, sizeof(*ps))))
124 ps->origwfmt = chan->writeformat;
126 if (ast_set_write_format(chan, AST_FORMAT_SLINEAR)) {
127 ast_log(LOG_WARNING, "Unable to set '%s' to signed linear format (write)\n", chan->name);
128 playtones_release(NULL, ps);
132 ps->reppos = pd->reppos;
133 ps->nitems = pd->nitems;
134 ps->items = pd->items;
138 /* Let interrupts interrupt :) */
139 if (pd->interruptible)
140 ast_set_flag(chan, AST_FLAG_WRITE_INT);
142 ast_clear_flag(chan, AST_FLAG_WRITE_INT);
147 static int playtones_generator(struct ast_channel *chan, void *data, int len, int samples)
149 struct playtones_state *ps = data;
150 struct playtones_item *pi;
152 /* we need to prepare a frame with 16 * timelen samples as we're
153 * generating SLIN audio
156 if (len > sizeof(ps->data) / 2 - 1) {
157 ast_log(LOG_WARNING, "Can't generate that much data!\n");
160 memset(&ps->f, 0, sizeof(ps->f));
162 pi = &ps->items[ps->npos];
163 if (ps->oldnpos != ps->npos) {
164 /* Load new parameters */
166 ps->v2_1 = pi->init_v2_1;
167 ps->v3_1 = pi->init_v3_1;
169 ps->v2_2 = pi->init_v2_2;
170 ps->v3_2 = pi->init_v3_2;
171 ps->oldnpos = ps->npos;
173 for (x=0;x<len/2;x++) {
176 ps->v3_1 = (pi->fac1 * ps->v2_1 >> 15) - ps->v1_1;
180 ps->v3_2 = (pi->fac2 * ps->v2_2 >> 15) - ps->v1_2;
183 p = ps->v3_2 - 32768;
185 p = ((p * 9) / 10) + 1;
186 ps->data[x] = (ps->v3_1 * p) >> 15;
188 ps->data[x] = ps->v3_1 + ps->v3_2;
191 ps->f.frametype = AST_FRAME_VOICE;
192 ps->f.subclass = AST_FORMAT_SLINEAR;
194 ps->f.samples = samples;
195 ps->f.offset = AST_FRIENDLY_OFFSET;
196 ps->f.data = ps->data;
197 ps->f.delivery.tv_sec = 0;
198 ps->f.delivery.tv_usec = 0;
199 ast_write(chan, &ps->f);
202 if (pi->duration && ps->pos >= pi->duration * 8) { /* item finished? */
203 ps->pos = 0; /* start new item */
205 if (ps->npos >= ps->nitems) { /* last item? */
206 if (ps->reppos == -1) /* repeat set? */
208 ps->npos = ps->reppos; /* redo from top */
214 static struct ast_generator playtones = {
215 alloc: playtones_alloc,
216 release: playtones_release,
217 generate: playtones_generator,
220 int ast_playtones_start(struct ast_channel *chan, int vol, const char *playlst, int interruptible)
222 char *s, *data = ast_strdupa(playlst); /* cute */
223 struct playtones_def d = { vol, -1, 0, 1, NULL};
228 d.vol = 7219; /* Default to -8db */
230 d.interruptible = interruptible;
233 /* the stringp/data is not null here */
234 /* check if the data is separated with '|' or with ',' by default */
235 if (strchr(stringp,'|'))
239 s = strsep(&stringp,separator);
241 int freq1, freq2, time, modulate=0, midinote=0;
245 else if (d.reppos == -1)
247 if (sscanf(s, "%d+%d/%d", &freq1, &freq2, &time) == 3) {
248 /* f1+f2/time format */
249 } else if (sscanf(s, "%d+%d", &freq1, &freq2) == 2) {
252 } else if (sscanf(s, "%d*%d/%d", &freq1, &freq2, &time) == 3) {
253 /* f1*f2/time format */
255 } else if (sscanf(s, "%d*%d", &freq1, &freq2) == 2) {
259 } else if (sscanf(s, "%d/%d", &freq1, &time) == 2) {
262 } else if (sscanf(s, "%d", &freq1) == 1) {
266 } else if (sscanf(s, "M%d+M%d/%d", &freq1, &freq2, &time) == 3) {
267 /* Mf1+Mf2/time format */
269 } else if (sscanf(s, "M%d+M%d", &freq1, &freq2) == 2) {
273 } else if (sscanf(s, "M%d*M%d/%d", &freq1, &freq2, &time) == 3) {
274 /* Mf1*Mf2/time format */
277 } else if (sscanf(s, "M%d*M%d", &freq1, &freq2) == 2) {
282 } else if (sscanf(s, "M%d/%d", &freq1, &time) == 2) {
283 /* Mf1/time format */
286 } else if (sscanf(s, "M%d", &freq1) == 1) {
292 ast_log(LOG_WARNING,"%s: tone component '%s' of '%s' is no good\n",chan->name,s,playlst);
297 /* midi notes must be between 0 and 127 */
298 if ((freq1 >= 0) && (freq1 <= 127))
299 freq1 = midi_tohz[freq1];
303 if ((freq2 >= 0) && (freq2 <= 127))
304 freq2 = midi_tohz[freq2];
309 if (!(d.items = ast_realloc(d.items, (d.nitems + 1) * sizeof(*d.items)))) {
312 d.items[d.nitems].fac1 = 2.0 * cos(2.0 * M_PI * (freq1 / 8000.0)) * 32768.0;
313 d.items[d.nitems].init_v2_1 = sin(-4.0 * M_PI * (freq1 / 8000.0)) * d.vol;
314 d.items[d.nitems].init_v3_1 = sin(-2.0 * M_PI * (freq1 / 8000.0)) * d.vol;
316 d.items[d.nitems].fac2 = 2.0 * cos(2.0 * M_PI * (freq2 / 8000.0)) * 32768.0;
317 d.items[d.nitems].init_v2_2 = sin(-4.0 * M_PI * (freq2 / 8000.0)) * d.vol;
318 d.items[d.nitems].init_v3_2 = sin(-2.0 * M_PI * (freq2 / 8000.0)) * d.vol;
319 d.items[d.nitems].duration = time;
320 d.items[d.nitems].modulate = modulate;
323 s = strsep(&stringp,separator);
326 if (ast_activate_generator(chan, &playtones, &d)) {
333 void ast_playtones_stop(struct ast_channel *chan)
335 ast_deactivate_generator(chan);
338 /*--------------------------------------------*/
340 static AST_RWLIST_HEAD_STATIC(tone_zones, ind_tone_zone);
341 static struct ind_tone_zone *current_tonezone;
343 struct ind_tone_zone *ast_walk_indications(const struct ind_tone_zone *cur)
345 struct ind_tone_zone *tz = NULL;
347 AST_RWLIST_RDLOCK(&tone_zones);
348 /* If cur is not NULL, then we have to iterate through - otherwise just return the first entry */
350 AST_RWLIST_TRAVERSE(&tone_zones, tz, list) {
354 tz = AST_RWLIST_NEXT(tz, list);
356 tz = AST_RWLIST_FIRST(&tone_zones);
358 AST_RWLIST_UNLOCK(&tone_zones);
363 /* Set global indication country */
364 int ast_set_indication_country(const char *country)
366 struct ind_tone_zone *zone = NULL;
368 /* If no country is specified or we are unable to find the zone, then return not found */
369 if (!country || !(zone = ast_get_indication_zone(country)))
372 if (option_verbose > 2)
373 ast_verbose(VERBOSE_PREFIX_3 "Setting default indication country to '%s'\n", country);
375 /* Protect the current tonezone using the tone_zones lock as well */
376 AST_RWLIST_WRLOCK(&tone_zones);
377 current_tonezone = zone;
378 AST_RWLIST_UNLOCK(&tone_zones);
384 /* locate tone_zone, given the country. if country == NULL, use the default country */
385 struct ind_tone_zone *ast_get_indication_zone(const char *country)
387 struct ind_tone_zone *tz = NULL;
390 AST_RWLIST_RDLOCK(&tone_zones);
393 if (current_tonezone)
394 tz = current_tonezone;
396 tz = AST_LIST_FIRST(&tone_zones);
399 AST_RWLIST_TRAVERSE(&tone_zones, tz, list) {
400 if (!strcasecmp(tz->country, country))
405 /* If this is an alias then we have to search yet again otherwise we have found the zonezone */
406 if (tz->alias && tz->alias[0])
410 } while ((++alias_loop < 20) && tz);
413 AST_RWLIST_UNLOCK(&tone_zones);
415 /* If we reached the maximum loops to find the proper country via alias, print out a notice */
416 if (alias_loop == 20)
417 ast_log(LOG_NOTICE, "Alias loop for '%s' is bonkers\n", country);
422 /* locate a tone_zone_sound, given the tone_zone. if tone_zone == NULL, use the default tone_zone */
423 struct ind_tone_zone_sound *ast_get_indication_tone(const struct ind_tone_zone *zone, const char *indication)
425 struct ind_tone_zone_sound *ts = NULL;
427 AST_RWLIST_RDLOCK(&tone_zones);
429 /* If no zone is already specified we need to try to pick one */
431 if (current_tonezone) {
432 zone = current_tonezone;
433 } else if (!(zone = AST_LIST_FIRST(&tone_zones))) {
434 /* No zone has been found ;( */
435 AST_RWLIST_UNLOCK(&tone_zones);
440 /* Look through list of tones in the zone searching for the right one */
441 for (ts = zone->tones; ts; ts = ts->next) {
442 if (!strcasecmp(ts->name, indication))
446 AST_RWLIST_UNLOCK(&tone_zones);
451 /* helper function to delete a tone_zone in its entirety */
452 static inline void free_zone(struct ind_tone_zone* zone)
454 while (zone->tones) {
455 struct ind_tone_zone_sound *tmp = zone->tones->next;
456 free((void*)zone->tones->name);
457 free((void*)zone->tones->data);
462 if (zone->ringcadence)
463 free(zone->ringcadence);
468 /*--------------------------------------------*/
470 /* add a new country, if country exists, it will be replaced. */
471 int ast_register_indication_country(struct ind_tone_zone *zone)
473 struct ind_tone_zone *tz = NULL;
475 AST_RWLIST_WRLOCK(&tone_zones);
476 AST_RWLIST_TRAVERSE_SAFE_BEGIN(&tone_zones, tz, list) {
477 /* If this is not the same zone, then just continue to the next entry */
478 if (strcasecmp(zone->country, tz->country))
480 /* If this zone we are going to remove is the current default then make the new zone the default */
481 if (tz == current_tonezone)
482 current_tonezone = zone;
483 /* Remove from the linked list */
484 AST_RWLIST_REMOVE_CURRENT(&tone_zones, list);
485 /* Finally free the zone itself */
489 AST_RWLIST_TRAVERSE_SAFE_END
491 /* Add zone to the list */
492 AST_RWLIST_INSERT_TAIL(&tone_zones, zone, list);
495 AST_RWLIST_UNLOCK(&tone_zones);
497 if (option_verbose > 2)
498 ast_verbose(VERBOSE_PREFIX_3 "Registered indication country '%s'\n", zone->country);
503 /* remove an existing country and all its indications, country must exist.
504 * Also, all countries which are an alias for the specified country are removed. */
505 int ast_unregister_indication_country(const char *country)
507 struct ind_tone_zone *tz = NULL;
510 AST_RWLIST_WRLOCK(&tone_zones);
511 AST_RWLIST_TRAVERSE_SAFE_BEGIN(&tone_zones, tz, list) {
512 if (country && (strcasecmp(country, tz->country) && strcasecmp(country, tz->alias)))
514 /* If this tonezone is the current default then unset it */
515 if (tz == current_tonezone) {
516 ast_log(LOG_NOTICE,"Removed default indication country '%s'\n", tz->country);
517 current_tonezone = NULL;
519 /* Remove from the list */
520 AST_RWLIST_REMOVE_CURRENT(&tone_zones, list);
521 if (option_verbose > 2)
522 ast_verbose(VERBOSE_PREFIX_3 "Unregistered indication country '%s'\n", tz->country);
526 AST_RWLIST_TRAVERSE_SAFE_END
527 AST_RWLIST_UNLOCK(&tone_zones);
532 /* add a new indication to a tone_zone. tone_zone must exist. if the indication already
533 * exists, it will be replaced. */
534 int ast_register_indication(struct ind_tone_zone *zone, const char *indication, const char *tonelist)
536 struct ind_tone_zone_sound *ts, *ps;
538 /* is it an alias? stop */
542 AST_RWLIST_WRLOCK(&tone_zones);
543 for (ps=NULL,ts=zone->tones; ts; ps=ts,ts=ts->next) {
544 if (strcasecmp(indication,ts->name)==0) {
545 /* indication already there, replace */
546 free((void*)ts->name);
547 free((void*)ts->data);
552 /* not there, we have to add */
553 if (!(ts = ast_malloc(sizeof(*ts)))) {
554 AST_RWLIST_UNLOCK(&tone_zones);
559 if (!(ts->name = ast_strdup(indication)) || !(ts->data = ast_strdup(tonelist))) {
560 AST_RWLIST_UNLOCK(&tone_zones);
567 AST_RWLIST_UNLOCK(&tone_zones);
571 /* remove an existing country's indication. Both country and indication must exist */
572 int ast_unregister_indication(struct ind_tone_zone *zone, const char *indication)
574 struct ind_tone_zone_sound *ts,*ps = NULL, *tmp;
577 /* is it an alias? stop */
581 AST_RWLIST_WRLOCK(&tone_zones);
584 if (strcasecmp(indication,ts->name)==0) {
585 /* indication found */
591 free((void*)ts->name);
592 free((void*)ts->data);
598 /* next zone please */
603 /* indication not found, goodbye */
604 AST_RWLIST_UNLOCK(&tone_zones);