indications.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2002, Pauline Middelink
  5. *
  6. *
  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.
  12. *
  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.
  16. */
  17. /*! \file
  18. *
  19. * \brief Tone Management
  20. *
  21. * \author Pauline Middelink <middelink@polyware.nl>
  22. *
  23. * This set of function allow us to play a list of tones on a channel.
  24. * Each element has two frequencies, which are mixed together and a
  25. * duration. For silence both frequencies can be set to 0.
  26. * The playtones can be given as a comma separated string.
  27. *
  28. */
  29. #include "asterisk.h"
  30. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  31. #include <math.h>
  32. #include "asterisk/lock.h"
  33. #include "asterisk/linkedlists.h"
  34. #include "asterisk/indications.h"
  35. #include "asterisk/frame.h"
  36. #include "asterisk/channel.h"
  37. #include "asterisk/utils.h"
  38. static int midi_tohz[128] = {
  39. 8,8,9,9,10,10,11,12,12,13,14,
  40. 15,16,17,18,19,20,21,23,24,25,
  41. 27,29,30,32,34,36,38,41,43,46,
  42. 48,51,55,58,61,65,69,73,77,82,
  43. 87,92,97,103,110,116,123,130,138,146,
  44. 155,164,174,184,195,207,220,233,246,261,
  45. 277,293,311,329,349,369,391,415,440,466,
  46. 493,523,554,587,622,659,698,739,783,830,
  47. 880,932,987,1046,1108,1174,1244,1318,1396,1479,
  48. 1567,1661,1760,1864,1975,2093,2217,2349,2489,2637,
  49. 2793,2959,3135,3322,3520,3729,3951,4186,4434,4698,
  50. 4978,5274,5587,5919,6271,6644,7040,7458,7902,8372,
  51. 8869,9397,9956,10548,11175,11839,12543
  52. };
  53. struct playtones_item {
  54. int fac1;
  55. int init_v2_1;
  56. int init_v3_1;
  57. int fac2;
  58. int init_v2_2;
  59. int init_v3_2;
  60. int modulate;
  61. int duration;
  62. };
  63. struct playtones_def {
  64. int vol;
  65. int reppos;
  66. int nitems;
  67. int interruptible;
  68. struct playtones_item *items;
  69. };
  70. struct playtones_state {
  71. int vol;
  72. int v1_1;
  73. int v2_1;
  74. int v3_1;
  75. int v1_2;
  76. int v2_2;
  77. int v3_2;
  78. int reppos;
  79. int nitems;
  80. struct playtones_item *items;
  81. int npos;
  82. int oldnpos;
  83. int pos;
  84. int origwfmt;
  85. struct ast_frame f;
  86. unsigned char offset[AST_FRIENDLY_OFFSET];
  87. short data[4000];
  88. };
  89. static void playtones_release(struct ast_channel *chan, void *params)
  90. {
  91. struct playtones_state *ps = params;
  92. if (chan)
  93. ast_set_write_format(chan, ps->origwfmt);
  94. if (ps->items)
  95. ast_free(ps->items);
  96. ast_free(ps);
  97. }
  98. static void * playtones_alloc(struct ast_channel *chan, void *params)
  99. {
  100. struct playtones_def *pd = params;
  101. struct playtones_state *ps = NULL;
  102. if (!(ps = ast_calloc(1, sizeof(*ps))))
  103. return NULL;
  104. ps->origwfmt = chan->writeformat;
  105. if (ast_set_write_format(chan, AST_FORMAT_SLINEAR)) {
  106. ast_log(LOG_WARNING, "Unable to set '%s' to signed linear format (write)\n", chan->name);
  107. playtones_release(NULL, ps);
  108. ps = NULL;
  109. } else {
  110. ps->vol = pd->vol;
  111. ps->reppos = pd->reppos;
  112. ps->nitems = pd->nitems;
  113. ps->items = pd->items;
  114. ps->oldnpos = -1;
  115. }
  116. /* Let interrupts interrupt :) */
  117. if (pd->interruptible)
  118. ast_set_flag(chan, AST_FLAG_WRITE_INT);
  119. else
  120. ast_clear_flag(chan, AST_FLAG_WRITE_INT);
  121. return ps;
  122. }
  123. static int playtones_generator(struct ast_channel *chan, void *data, int len, int samples)
  124. {
  125. struct playtones_state *ps = data;
  126. struct playtones_item *pi;
  127. int x;
  128. /* we need to prepare a frame with 16 * timelen samples as we're
  129. * generating SLIN audio
  130. */
  131. len = samples * 2;
  132. if (len > sizeof(ps->data) / 2 - 1) {
  133. ast_log(LOG_WARNING, "Can't generate that much data!\n");
  134. return -1;
  135. }
  136. memset(&ps->f, 0, sizeof(ps->f));
  137. pi = &ps->items[ps->npos];
  138. if (ps->oldnpos != ps->npos) {
  139. /* Load new parameters */
  140. ps->v1_1 = 0;
  141. ps->v2_1 = pi->init_v2_1;
  142. ps->v3_1 = pi->init_v3_1;
  143. ps->v1_2 = 0;
  144. ps->v2_2 = pi->init_v2_2;
  145. ps->v3_2 = pi->init_v3_2;
  146. ps->oldnpos = ps->npos;
  147. }
  148. for (x = 0; x < len/2; x++) {
  149. ps->v1_1 = ps->v2_1;
  150. ps->v2_1 = ps->v3_1;
  151. ps->v3_1 = (pi->fac1 * ps->v2_1 >> 15) - ps->v1_1;
  152. ps->v1_2 = ps->v2_2;
  153. ps->v2_2 = ps->v3_2;
  154. ps->v3_2 = (pi->fac2 * ps->v2_2 >> 15) - ps->v1_2;
  155. if (pi->modulate) {
  156. int p;
  157. p = ps->v3_2 - 32768;
  158. if (p < 0) p = -p;
  159. p = ((p * 9) / 10) + 1;
  160. ps->data[x] = (ps->v3_1 * p) >> 15;
  161. } else
  162. ps->data[x] = ps->v3_1 + ps->v3_2;
  163. }
  164. ps->f.frametype = AST_FRAME_VOICE;
  165. ps->f.subclass = AST_FORMAT_SLINEAR;
  166. ps->f.datalen = len;
  167. ps->f.samples = samples;
  168. ps->f.offset = AST_FRIENDLY_OFFSET;
  169. ps->f.data = ps->data;
  170. ps->f.delivery.tv_sec = 0;
  171. ps->f.delivery.tv_usec = 0;
  172. ast_write(chan, &ps->f);
  173. ps->pos += x;
  174. if (pi->duration && ps->pos >= pi->duration * 8) { /* item finished? */
  175. ps->pos = 0; /* start new item */
  176. ps->npos++;
  177. if (ps->npos >= ps->nitems) { /* last item? */
  178. if (ps->reppos == -1) /* repeat set? */
  179. return -1;
  180. ps->npos = ps->reppos; /* redo from top */
  181. }
  182. }
  183. return 0;
  184. }
  185. static struct ast_generator playtones = {
  186. alloc: playtones_alloc,
  187. release: playtones_release,
  188. generate: playtones_generator,
  189. };
  190. int ast_playtones_start(struct ast_channel *chan, int vol, const char *playlst, int interruptible)
  191. {
  192. char *s, *data = ast_strdupa(playlst); /* cute */
  193. struct playtones_def d = { vol, -1, 0, 1, NULL};
  194. char *stringp;
  195. char *separator;
  196. if (vol < 1)
  197. d.vol = 7219; /* Default to -8db */
  198. d.interruptible = interruptible;
  199. stringp=data;
  200. /* the stringp/data is not null here */
  201. /* check if the data is separated with '|' or with ',' by default */
  202. if (strchr(stringp,'|'))
  203. separator = "|";
  204. else
  205. separator = ",";
  206. s = strsep(&stringp,separator);
  207. while (s && *s) {
  208. int freq1, freq2, time, modulate = 0, midinote = 0;
  209. if (s[0]=='!')
  210. s++;
  211. else if (d.reppos == -1)
  212. d.reppos = d.nitems;
  213. if (sscanf(s, "%30d+%30d/%30d", &freq1, &freq2, &time) == 3) {
  214. /* f1+f2/time format */
  215. } else if (sscanf(s, "%30d+%30d", &freq1, &freq2) == 2) {
  216. /* f1+f2 format */
  217. time = 0;
  218. } else if (sscanf(s, "%30d*%30d/%30d", &freq1, &freq2, &time) == 3) {
  219. /* f1*f2/time format */
  220. modulate = 1;
  221. } else if (sscanf(s, "%30d*%30d", &freq1, &freq2) == 2) {
  222. /* f1*f2 format */
  223. time = 0;
  224. modulate = 1;
  225. } else if (sscanf(s, "%30d/%30d", &freq1, &time) == 2) {
  226. /* f1/time format */
  227. freq2 = 0;
  228. } else if (sscanf(s, "%30d", &freq1) == 1) {
  229. /* f1 format */
  230. freq2 = 0;
  231. time = 0;
  232. } else if (sscanf(s, "M%30d+M%30d/%30d", &freq1, &freq2, &time) == 3) {
  233. /* Mf1+Mf2/time format */
  234. midinote = 1;
  235. } else if (sscanf(s, "M%30d+M%30d", &freq1, &freq2) == 2) {
  236. /* Mf1+Mf2 format */
  237. time = 0;
  238. midinote = 1;
  239. } else if (sscanf(s, "M%30d*M%30d/%30d", &freq1, &freq2, &time) == 3) {
  240. /* Mf1*Mf2/time format */
  241. modulate = 1;
  242. midinote = 1;
  243. } else if (sscanf(s, "M%30d*M%30d", &freq1, &freq2) == 2) {
  244. /* Mf1*Mf2 format */
  245. time = 0;
  246. modulate = 1;
  247. midinote = 1;
  248. } else if (sscanf(s, "M%30d/%30d", &freq1, &time) == 2) {
  249. /* Mf1/time format */
  250. freq2 = -1;
  251. midinote = 1;
  252. } else if (sscanf(s, "M%30d", &freq1) == 1) {
  253. /* Mf1 format */
  254. freq2 = -1;
  255. time = 0;
  256. midinote = 1;
  257. } else {
  258. ast_log(LOG_WARNING,"%s: tone component '%s' of '%s' is no good\n",chan->name,s,playlst);
  259. return -1;
  260. }
  261. if (midinote) {
  262. /* midi notes must be between 0 and 127 */
  263. if ((freq1 >= 0) && (freq1 <= 127))
  264. freq1 = midi_tohz[freq1];
  265. else
  266. freq1 = 0;
  267. if ((freq2 >= 0) && (freq2 <= 127))
  268. freq2 = midi_tohz[freq2];
  269. else
  270. freq2 = 0;
  271. }
  272. if (!(d.items = ast_realloc(d.items, (d.nitems + 1) * sizeof(*d.items)))) {
  273. return -1;
  274. }
  275. d.items[d.nitems].fac1 = 2.0 * cos(2.0 * M_PI * (freq1 / 8000.0)) * 32768.0;
  276. d.items[d.nitems].init_v2_1 = sin(-4.0 * M_PI * (freq1 / 8000.0)) * d.vol;
  277. d.items[d.nitems].init_v3_1 = sin(-2.0 * M_PI * (freq1 / 8000.0)) * d.vol;
  278. d.items[d.nitems].fac2 = 2.0 * cos(2.0 * M_PI * (freq2 / 8000.0)) * 32768.0;
  279. d.items[d.nitems].init_v2_2 = sin(-4.0 * M_PI * (freq2 / 8000.0)) * d.vol;
  280. d.items[d.nitems].init_v3_2 = sin(-2.0 * M_PI * (freq2 / 8000.0)) * d.vol;
  281. d.items[d.nitems].duration = time;
  282. d.items[d.nitems].modulate = modulate;
  283. d.nitems++;
  284. s = strsep(&stringp,separator);
  285. }
  286. if (ast_activate_generator(chan, &playtones, &d)) {
  287. ast_free(d.items);
  288. return -1;
  289. }
  290. return 0;
  291. }
  292. void ast_playtones_stop(struct ast_channel *chan)
  293. {
  294. ast_deactivate_generator(chan);
  295. }
  296. /*--------------------------------------------*/
  297. static AST_RWLIST_HEAD_STATIC(tone_zones, tone_zone);
  298. static struct tone_zone *current_tonezone;
  299. struct tone_zone *ast_walk_indications(const struct tone_zone *cur)
  300. {
  301. struct tone_zone *tz = NULL;
  302. AST_RWLIST_RDLOCK(&tone_zones);
  303. /* If cur is not NULL, then we have to iterate through - otherwise just return the first entry */
  304. if (cur) {
  305. AST_RWLIST_TRAVERSE(&tone_zones, tz, list) {
  306. if (tz == cur)
  307. break;
  308. }
  309. tz = AST_RWLIST_NEXT(tz, list);
  310. } else {
  311. tz = AST_RWLIST_FIRST(&tone_zones);
  312. }
  313. AST_RWLIST_UNLOCK(&tone_zones);
  314. return tz;
  315. }
  316. /* Set global indication country */
  317. int ast_set_indication_country(const char *country)
  318. {
  319. struct tone_zone *zone = NULL;
  320. /* If no country is specified or we are unable to find the zone, then return not found */
  321. if (!country || !(zone = ast_get_indication_zone(country)))
  322. return 1;
  323. ast_verb(3, "Setting default indication country to '%s'\n", country);
  324. /* Protect the current tonezone using the tone_zones lock as well */
  325. AST_RWLIST_WRLOCK(&tone_zones);
  326. current_tonezone = zone;
  327. AST_RWLIST_UNLOCK(&tone_zones);
  328. /* Zone was found */
  329. return 0;
  330. }
  331. /* locate tone_zone, given the country. if country == NULL, use the default country */
  332. struct tone_zone *ast_get_indication_zone(const char *country)
  333. {
  334. struct tone_zone *tz = NULL;
  335. int alias_loop = 0;
  336. AST_RWLIST_RDLOCK(&tone_zones);
  337. if (ast_strlen_zero(country)) {
  338. tz = current_tonezone ? current_tonezone : AST_LIST_FIRST(&tone_zones);
  339. } else {
  340. do {
  341. AST_RWLIST_TRAVERSE(&tone_zones, tz, list) {
  342. if (!strcasecmp(tz->country, country))
  343. break;
  344. }
  345. if (!tz)
  346. break;
  347. /* If this is an alias then we have to search yet again otherwise we have found the zonezone */
  348. if (tz->alias && tz->alias[0])
  349. country = tz->alias;
  350. else
  351. break;
  352. } while ((++alias_loop < 20) && tz);
  353. }
  354. AST_RWLIST_UNLOCK(&tone_zones);
  355. /* If we reached the maximum loops to find the proper country via alias, print out a notice */
  356. if (alias_loop == 20)
  357. ast_log(LOG_NOTICE, "Alias loop for '%s' is bonkers\n", country);
  358. return tz;
  359. }
  360. /* locate a tone_zone_sound, given the tone_zone. if tone_zone == NULL, use the default tone_zone */
  361. struct tone_zone_sound *ast_get_indication_tone(const struct tone_zone *zone, const char *indication)
  362. {
  363. struct tone_zone_sound *ts = NULL;
  364. AST_RWLIST_RDLOCK(&tone_zones);
  365. /* If no zone is already specified we need to try to pick one */
  366. if (!zone) {
  367. if (current_tonezone) {
  368. zone = current_tonezone;
  369. } else if (!(zone = AST_LIST_FIRST(&tone_zones))) {
  370. /* No zone has been found ;( */
  371. AST_RWLIST_UNLOCK(&tone_zones);
  372. return NULL;
  373. }
  374. }
  375. /* Look through list of tones in the zone searching for the right one */
  376. for (ts = zone->tones; ts; ts = ts->next) {
  377. if (!strcasecmp(ts->name, indication))
  378. break;
  379. }
  380. AST_RWLIST_UNLOCK(&tone_zones);
  381. return ts;
  382. }
  383. /* helper function to delete a tone_zone in its entirety */
  384. static inline void free_zone(struct tone_zone* zone)
  385. {
  386. while (zone->tones) {
  387. struct tone_zone_sound *tmp = zone->tones->next;
  388. ast_free((void *)zone->tones->name);
  389. ast_free((void *)zone->tones->data);
  390. ast_free(zone->tones);
  391. zone->tones = tmp;
  392. }
  393. if (zone->ringcadence)
  394. ast_free(zone->ringcadence);
  395. ast_free(zone);
  396. }
  397. /*--------------------------------------------*/
  398. /* add a new country, if country exists, it will be replaced. */
  399. int ast_register_indication_country(struct tone_zone *zone)
  400. {
  401. struct tone_zone *tz = NULL;
  402. AST_RWLIST_WRLOCK(&tone_zones);
  403. AST_RWLIST_TRAVERSE_SAFE_BEGIN(&tone_zones, tz, list) {
  404. /* If this is not the same zone, then just continue to the next entry */
  405. if (strcasecmp(zone->country, tz->country))
  406. continue;
  407. /* If this zone we are going to remove is the current default then make the new zone the default */
  408. if (tz == current_tonezone)
  409. current_tonezone = zone;
  410. /* Remove from the linked list */
  411. AST_RWLIST_REMOVE_CURRENT(list);
  412. /* Finally free the zone itself */
  413. free_zone(tz);
  414. break;
  415. }
  416. AST_RWLIST_TRAVERSE_SAFE_END;
  417. /* Add zone to the list */
  418. AST_RWLIST_INSERT_TAIL(&tone_zones, zone, list);
  419. /* It's all over. */
  420. AST_RWLIST_UNLOCK(&tone_zones);
  421. ast_verb(3, "Registered indication country '%s'\n", zone->country);
  422. return 0;
  423. }
  424. /* remove an existing country and all its indications, country must exist.
  425. * Also, all countries which are an alias for the specified country are removed. */
  426. int ast_unregister_indication_country(const char *country)
  427. {
  428. struct tone_zone *tz = NULL;
  429. int res = -1;
  430. AST_RWLIST_WRLOCK(&tone_zones);
  431. AST_RWLIST_TRAVERSE_SAFE_BEGIN(&tone_zones, tz, list) {
  432. if (country && (strcasecmp(country, tz->country) && strcasecmp(country, tz->alias)))
  433. continue;
  434. /* If this tonezone is the current default then unset it */
  435. if (tz == current_tonezone) {
  436. ast_log(LOG_NOTICE,"Removed default indication country '%s'\n", tz->country);
  437. current_tonezone = NULL;
  438. }
  439. /* Remove from the list */
  440. AST_RWLIST_REMOVE_CURRENT(list);
  441. ast_verb(3, "Unregistered indication country '%s'\n", tz->country);
  442. free_zone(tz);
  443. res = 0;
  444. }
  445. AST_RWLIST_TRAVERSE_SAFE_END;
  446. AST_RWLIST_UNLOCK(&tone_zones);
  447. return res;
  448. }
  449. /* add a new indication to a tone_zone. tone_zone must exist. if the indication already
  450. * exists, it will be replaced. */
  451. int ast_register_indication(struct tone_zone *zone, const char *indication, const char *tonelist)
  452. {
  453. struct tone_zone_sound *ts, *ps;
  454. /* is it an alias? stop */
  455. if (zone->alias[0])
  456. return -1;
  457. AST_RWLIST_WRLOCK(&tone_zones);
  458. for (ps=NULL,ts=zone->tones; ts; ps=ts,ts=ts->next) {
  459. if (!strcasecmp(indication,ts->name)) {
  460. /* indication already there, replace */
  461. ast_free((void*)ts->name);
  462. ast_free((void*)ts->data);
  463. break;
  464. }
  465. }
  466. if (!ts) {
  467. /* not there, we have to add */
  468. if (!(ts = ast_malloc(sizeof(*ts)))) {
  469. AST_RWLIST_UNLOCK(&tone_zones);
  470. return -2;
  471. }
  472. ts->next = NULL;
  473. }
  474. if (!(ts->name = ast_strdup(indication)) || !(ts->data = ast_strdup(tonelist))) {
  475. AST_RWLIST_UNLOCK(&tone_zones);
  476. return -2;
  477. }
  478. if (ps)
  479. ps->next = ts;
  480. else
  481. zone->tones = ts;
  482. AST_RWLIST_UNLOCK(&tone_zones);
  483. return 0;
  484. }
  485. /* remove an existing country's indication. Both country and indication must exist */
  486. int ast_unregister_indication(struct tone_zone *zone, const char *indication)
  487. {
  488. struct tone_zone_sound *ts,*ps = NULL, *tmp;
  489. int res = -1;
  490. /* is it an alias? stop */
  491. if (zone->alias[0])
  492. return -1;
  493. AST_RWLIST_WRLOCK(&tone_zones);
  494. ts = zone->tones;
  495. while (ts) {
  496. if (!strcasecmp(indication,ts->name)) {
  497. /* indication found */
  498. tmp = ts->next;
  499. if (ps)
  500. ps->next = tmp;
  501. else
  502. zone->tones = tmp;
  503. ast_free((void*)ts->name);
  504. ast_free((void*)ts->data);
  505. ast_free(ts);
  506. ts = tmp;
  507. res = 0;
  508. }
  509. else {
  510. /* next zone please */
  511. ps = ts;
  512. ts = ts->next;
  513. }
  514. }
  515. /* indication not found, goodbye */
  516. AST_RWLIST_UNLOCK(&tone_zones);
  517. return res;
  518. }