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 struct playtones_item {
43 struct playtones_def {
48 struct playtones_item *items;
51 struct playtones_state {
55 struct playtones_item *items;
60 unsigned char offset[AST_FRIENDLY_OFFSET];
64 static void playtones_release(struct ast_channel *chan, void *params)
66 struct playtones_state *ps = params;
68 ast_set_write_format(chan, ps->origwfmt);
70 if (ps->items) free(ps->items);
74 static void * playtones_alloc(struct ast_channel *chan, void *params)
76 struct playtones_def *pd = params;
77 struct playtones_state *ps = malloc(sizeof(struct playtones_state));
80 memset(ps, 0, sizeof(struct playtones_state));
81 ps->origwfmt = chan->writeformat;
82 if (ast_set_write_format(chan, AST_FORMAT_SLINEAR)) {
83 ast_log(LOG_WARNING, "Unable to set '%s' to signed linear format (write)\n", chan->name);
84 playtones_release(NULL, ps);
88 ps->reppos = pd->reppos;
89 ps->nitems = pd->nitems;
90 ps->items = pd->items;
92 /* Let interrupts interrupt :) */
93 if (pd->interruptible)
94 ast_set_flag(chan, AST_FLAG_WRITE_INT);
96 ast_clear_flag(chan, AST_FLAG_WRITE_INT);
100 static int playtones_generator(struct ast_channel *chan, void *data, int len, int samples)
102 struct playtones_state *ps = data;
103 struct playtones_item *pi;
105 /* we need to prepare a frame with 16 * timelen samples as we're
106 * generating SLIN audio
109 if (len > sizeof(ps->data) / 2 - 1) {
110 ast_log(LOG_WARNING, "Can't generate that much data!\n");
113 memset(&ps->f, 0, sizeof(ps->f));
115 pi = &ps->items[ps->npos];
116 for (x=0;x<len/2;x++) {
118 /* Modulate 1st tone with 2nd, to 90% modulation depth */
119 ps->data[x] = ps->vol * 2 * (
120 sin((pi->freq1 * 2.0 * M_PI / 8000.0) * (ps->pos + x)) *
121 (0.9 * fabs(sin((pi->freq2 * 2.0 * M_PI / 8000.0) * (ps->pos + x))) + 0.1)
124 /* Add 2 tones together */
125 ps->data[x] = ps->vol * (
126 sin((pi->freq1 * 2.0 * M_PI / 8000.0) * (ps->pos + x)) +
127 sin((pi->freq2 * 2.0 * M_PI / 8000.0) * (ps->pos + x))
130 ps->f.frametype = AST_FRAME_VOICE;
131 ps->f.subclass = AST_FORMAT_SLINEAR;
133 ps->f.samples = samples;
134 ps->f.offset = AST_FRIENDLY_OFFSET;
135 ps->f.data = ps->data;
136 ps->f.delivery.tv_sec = 0;
137 ps->f.delivery.tv_usec = 0;
138 ast_write(chan, &ps->f);
141 if (pi->duration && ps->pos >= pi->duration * 8) { /* item finished? */
142 ps->pos = 0; /* start new item */
144 if (ps->npos >= ps->nitems) { /* last item? */
145 if (ps->reppos == -1) /* repeat set? */
147 ps->npos = ps->reppos; /* redo from top */
153 static struct ast_generator playtones = {
154 alloc: playtones_alloc,
155 release: playtones_release,
156 generate: playtones_generator,
159 int ast_playtones_start(struct ast_channel *chan, int vol, const char *playlst, int interruptible)
161 char *s, *data = ast_strdupa(playlst); /* cute */
162 struct playtones_def d = { vol, -1, 0, 1, NULL};
170 d.interruptible = interruptible;
173 /* the stringp/data is not null here */
174 /* check if the data is separated with '|' or with ',' by default */
175 if (strchr(stringp,'|'))
179 s = strsep(&stringp,separator);
181 int freq1, freq2, time, modulate=0;
185 else if (d.reppos == -1)
187 if (sscanf(s, "%d+%d/%d", &freq1, &freq2, &time) == 3) {
188 /* f1+f2/time format */
189 } else if (sscanf(s, "%d+%d", &freq1, &freq2) == 2) {
192 } else if (sscanf(s, "%d*%d/%d", &freq1, &freq2, &time) == 3) {
193 /* f1*f2/time format */
195 } else if (sscanf(s, "%d*%d", &freq1, &freq2) == 2) {
199 } else if (sscanf(s, "%d/%d", &freq1, &time) == 2) {
202 } else if (sscanf(s, "%d", &freq1) == 1) {
207 ast_log(LOG_WARNING,"%s: tone component '%s' of '%s' is no good\n",chan->name,s,playlst);
211 d.items = realloc(d.items,(d.nitems+1)*sizeof(struct playtones_item));
214 d.items[d.nitems].freq1 = freq1;
215 d.items[d.nitems].freq2 = freq2;
216 d.items[d.nitems].duration = time;
217 d.items[d.nitems].modulate = modulate;
220 s = strsep(&stringp,separator);
223 if (ast_activate_generator(chan, &playtones, &d)) {
230 void ast_playtones_stop(struct ast_channel *chan)
232 ast_deactivate_generator(chan);
235 /*--------------------------------------------*/
237 struct tone_zone *tone_zones;
238 static struct tone_zone *current_tonezone;
240 /* Protect the tone_zones list (highly unlikely that two things would change
241 * it at the same time, but still! */
242 AST_MUTEX_DEFINE_EXPORTED(tzlock);
244 /* Set global indication country */
245 int ast_set_indication_country(const char *country)
248 struct tone_zone *z = ast_get_indication_zone(country);
250 if (option_verbose > 2)
251 ast_verbose(VERBOSE_PREFIX_3 "Setting default indication country to '%s'\n",country);
252 current_tonezone = z;
256 return 1; /* not found */
259 /* locate tone_zone, given the country. if country == NULL, use the default country */
260 struct tone_zone *ast_get_indication_zone(const char *country)
262 struct tone_zone *tz;
265 /* we need some tonezone, pick the first */
266 if (country == NULL && current_tonezone)
267 return current_tonezone; /* default country? */
268 if (country == NULL && tone_zones)
269 return tone_zones; /* any country? */
271 return 0; /* not a single country insight */
273 if (ast_mutex_lock(&tzlock)) {
274 ast_log(LOG_WARNING, "Unable to lock tone_zones list\n");
278 for (tz=tone_zones; tz; tz=tz->next) {
279 if (strcasecmp(country,tz->country)==0) {
280 /* tone_zone found */
281 if (tz->alias && tz->alias[0]) {
285 ast_mutex_unlock(&tzlock);
289 } while (++alias_loop<20 && tz);
290 ast_mutex_unlock(&tzlock);
292 ast_log(LOG_NOTICE,"Alias loop for '%s' forcefull broken\n",country);
293 /* nothing found, sorry */
297 /* locate a tone_zone_sound, given the tone_zone. if tone_zone == NULL, use the default tone_zone */
298 struct tone_zone_sound *ast_get_indication_tone(const struct tone_zone *zone, const char *indication)
300 struct tone_zone_sound *ts;
302 /* we need some tonezone, pick the first */
303 if (zone == NULL && current_tonezone)
304 zone = current_tonezone; /* default country? */
305 if (zone == NULL && tone_zones)
306 zone = tone_zones; /* any country? */
308 return 0; /* not a single country insight */
310 if (ast_mutex_lock(&tzlock)) {
311 ast_log(LOG_WARNING, "Unable to lock tone_zones list\n");
314 for (ts=zone->tones; ts; ts=ts->next) {
315 if (strcasecmp(indication,ts->name)==0) {
316 /* found indication! */
317 ast_mutex_unlock(&tzlock);
321 /* nothing found, sorry */
322 ast_mutex_unlock(&tzlock);
326 /* helper function to delete a tone_zone in its entirety */
327 static inline void free_zone(struct tone_zone* zone)
329 while (zone->tones) {
330 struct tone_zone_sound *tmp = zone->tones->next;
331 free((void*)zone->tones->name);
332 free((void*)zone->tones->data);
336 if (zone->ringcadance)
337 free((void*)zone->ringcadance);
341 /*--------------------------------------------*/
343 /* add a new country, if country exists, it will be replaced. */
344 int ast_register_indication_country(struct tone_zone *zone)
346 struct tone_zone *tz,*pz;
348 if (ast_mutex_lock(&tzlock)) {
349 ast_log(LOG_WARNING, "Unable to lock tone_zones list\n");
352 for (pz=NULL,tz=tone_zones; tz; pz=tz,tz=tz->next) {
353 if (strcasecmp(zone->country,tz->country)==0) {
354 /* tone_zone already there, replace */
355 zone->next = tz->next;
360 /* if we are replacing the default zone, re-point it */
361 if (tz == current_tonezone)
362 current_tonezone = zone;
363 /* now free the previous zone */
365 ast_mutex_unlock(&tzlock);
369 /* country not there, add */
375 ast_mutex_unlock(&tzlock);
377 if (option_verbose > 2)
378 ast_verbose(VERBOSE_PREFIX_3 "Registered indication country '%s'\n",zone->country);
382 /* remove an existing country and all its indications, country must exist.
383 * Also, all countries which are an alias for the specified country are removed. */
384 int ast_unregister_indication_country(const char *country)
386 struct tone_zone *tz, *pz = NULL, *tmp;
389 if (ast_mutex_lock(&tzlock)) {
390 ast_log(LOG_WARNING, "Unable to lock tone_zones list\n");
396 (strcasecmp(country, tz->country)==0 ||
397 strcasecmp(country, tz->alias)==0)) {
398 /* tone_zone found, remove */
404 /* if we are unregistering the default country, w'll notice */
405 if (tz == current_tonezone) {
406 ast_log(LOG_NOTICE,"Removed default indication country '%s'\n",tz->country);
407 current_tonezone = NULL;
409 if (option_verbose > 2)
410 ast_verbose(VERBOSE_PREFIX_3 "Unregistered indication country '%s'\n",tz->country);
412 if (tone_zones == tz)
418 /* next zone please */
423 ast_mutex_unlock(&tzlock);
427 /* add a new indication to a tone_zone. tone_zone must exist. if the indication already
428 * exists, it will be replaced. */
429 int ast_register_indication(struct tone_zone *zone, const char *indication, const char *tonelist)
431 struct tone_zone_sound *ts,*ps;
433 /* is it an alias? stop */
437 if (ast_mutex_lock(&tzlock)) {
438 ast_log(LOG_WARNING, "Unable to lock tone_zones list\n");
441 for (ps=NULL,ts=zone->tones; ts; ps=ts,ts=ts->next) {
442 if (strcasecmp(indication,ts->name)==0) {
443 /* indication already there, replace */
444 free((void*)ts->name);
445 free((void*)ts->data);
450 /* not there, we have to add */
451 ts = malloc(sizeof(struct tone_zone_sound));
453 ast_log(LOG_WARNING, "Out of memory\n");
454 ast_mutex_unlock(&tzlock);
459 ts->name = strdup(indication);
460 ts->data = strdup(tonelist);
461 if (ts->name==NULL || ts->data==NULL) {
462 ast_log(LOG_WARNING, "Out of memory\n");
463 ast_mutex_unlock(&tzlock);
470 ast_mutex_unlock(&tzlock);
474 /* remove an existing country's indication. Both country and indication must exist */
475 int ast_unregister_indication(struct tone_zone *zone, const char *indication)
477 struct tone_zone_sound *ts,*ps = NULL, *tmp;
480 /* is it an alias? stop */
484 if (ast_mutex_lock(&tzlock)) {
485 ast_log(LOG_WARNING, "Unable to lock tone_zones list\n");
490 if (strcasecmp(indication,ts->name)==0) {
491 /* indication found */
497 free((void*)ts->name);
498 free((void*)ts->data);
504 /* next zone please */
509 /* indication not found, goodbye */
510 ast_mutex_unlock(&tzlock);