indications.c 15 KB

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