app_morsecode.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (c) 2006, Tilghman Lesher. All rights reserved.
  5. *
  6. * Tilghman Lesher <app_morsecode__v001@the-tilghman.com>
  7. *
  8. * This code is released by the author with no restrictions on usage.
  9. *
  10. * See http://www.asterisk.org for more information about
  11. * the Asterisk project. Please do not directly contact
  12. * any of the maintainers of this project for assistance;
  13. * the project provides a web site, mailing lists and IRC
  14. * channels for your use.
  15. *
  16. */
  17. /*! \file
  18. *
  19. * \brief Morsecode application
  20. *
  21. * \author Tilghman Lesher <app_morsecode__v001@the-tilghman.com>
  22. *
  23. * \ingroup applications
  24. */
  25. /*** MODULEINFO
  26. <support_level>extended</support_level>
  27. ***/
  28. #include "asterisk.h"
  29. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  30. #include "asterisk/file.h"
  31. #include "asterisk/channel.h"
  32. #include "asterisk/pbx.h"
  33. #include "asterisk/module.h"
  34. #include "asterisk/indications.h"
  35. /*** DOCUMENTATION
  36. <application name="Morsecode" language="en_US">
  37. <synopsis>
  38. Plays morse code.
  39. </synopsis>
  40. <syntax>
  41. <parameter name="string" required="true">
  42. <para>String to playback as morse code to channel</para>
  43. </parameter>
  44. </syntax>
  45. <description>
  46. <para>Plays the Morse code equivalent of the passed string.</para>
  47. <para>This application does not automatically answer and should be preceeded by
  48. an application such as Answer() or Progress().</para>
  49. <para>This application uses the following variables:</para>
  50. <variablelist>
  51. <variable name="MORSEDITLEN">
  52. <para>Use this value in (ms) for length of dit</para>
  53. </variable>
  54. <variable name="MORSETONE">
  55. <para>The pitch of the tone in (Hz), default is 800</para>
  56. </variable>
  57. </variablelist>
  58. </description>
  59. <see-also>
  60. <ref type="application">SayAlpha</ref>
  61. <ref type="application">SayPhonetic</ref>
  62. </see-also>
  63. </application>
  64. ***/
  65. static const char app_morsecode[] = "Morsecode";
  66. static const char * const morsecode[] = {
  67. "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", /* 0-15 */
  68. "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", /* 16-31 */
  69. " ", /* 32 - <space> */
  70. ".-.-.-", /* 33 - ! */
  71. ".-..-.", /* 34 - " */
  72. "", /* 35 - # */
  73. "", /* 36 - $ */
  74. "", /* 37 - % */
  75. "", /* 38 - & */
  76. ".----.", /* 39 - ' */
  77. "-.--.-", /* 40 - ( */
  78. "-.--.-", /* 41 - ) */
  79. "", /* 42 - * */
  80. "", /* 43 - + */
  81. "--..--", /* 44 - , */
  82. "-....-", /* 45 - - */
  83. ".-.-.-", /* 46 - . */
  84. "-..-.", /* 47 - / */
  85. "-----", ".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----.", /* 48-57 - 0-9 */
  86. "---...", /* 58 - : */
  87. "-.-.-.", /* 59 - ; */
  88. "", /* 60 - < */
  89. "-...-", /* 61 - = */
  90. "", /* 62 - > */
  91. "..--..", /* 63 - ? */
  92. ".--.-.", /* 64 - @ */
  93. ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--",
  94. "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..",
  95. "-.--.-", /* 91 - [ (really '(') */
  96. "-..-.", /* 92 - \ (really '/') */
  97. "-.--.-", /* 93 - ] (really ')') */
  98. "", /* 94 - ^ */
  99. "..--.-", /* 95 - _ */
  100. ".----.", /* 96 - ` */
  101. ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--",
  102. "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..",
  103. "-.--.-", /* 123 - { (really '(') */
  104. "", /* 124 - | */
  105. "-.--.-", /* 125 - } (really ')') */
  106. "-..-.", /* 126 - ~ (really bar) */
  107. ". . .", /* 127 - <del> (error) */
  108. };
  109. static void playtone(struct ast_channel *chan, int tone, int len)
  110. {
  111. char dtmf[20];
  112. snprintf(dtmf, sizeof(dtmf), "%d/%d", tone, len);
  113. ast_playtones_start(chan, 0, dtmf, 0);
  114. ast_safe_sleep(chan, len);
  115. ast_playtones_stop(chan);
  116. }
  117. static int morsecode_exec(struct ast_channel *chan, const char *data)
  118. {
  119. int res=0, ditlen, tone;
  120. const char *digit;
  121. const char *ditlenc, *tonec;
  122. if (ast_strlen_zero(data)) {
  123. ast_log(LOG_WARNING, "Syntax: Morsecode(<string>) - no argument found\n");
  124. return 0;
  125. }
  126. /* Use variable MORESEDITLEN, if set (else 80) */
  127. ast_channel_lock(chan);
  128. ditlenc = pbx_builtin_getvar_helper(chan, "MORSEDITLEN");
  129. if (ast_strlen_zero(ditlenc) || (sscanf(ditlenc, "%30d", &ditlen) != 1)) {
  130. ditlen = 80;
  131. }
  132. ast_channel_unlock(chan);
  133. /* Use variable MORSETONE, if set (else 800) */
  134. ast_channel_lock(chan);
  135. tonec = pbx_builtin_getvar_helper(chan, "MORSETONE");
  136. if (ast_strlen_zero(tonec) || (sscanf(tonec, "%30d", &tone) != 1)) {
  137. tone = 800;
  138. }
  139. ast_channel_unlock(chan);
  140. for (digit = data; *digit; digit++) {
  141. int digit2 = *digit;
  142. const char *dahdit;
  143. if (digit2 < 0) {
  144. continue;
  145. }
  146. for (dahdit = morsecode[digit2]; *dahdit; dahdit++) {
  147. if (*dahdit == '-') {
  148. playtone(chan, tone, 3 * ditlen);
  149. } else if (*dahdit == '.') {
  150. playtone(chan, tone, 1 * ditlen);
  151. } else {
  152. /* Account for ditlen of silence immediately following */
  153. playtone(chan, 0, 2 * ditlen);
  154. }
  155. /* Pause slightly between each dit and dah */
  156. playtone(chan, 0, 1 * ditlen);
  157. }
  158. /* Pause between characters */
  159. playtone(chan, 0, 2 * ditlen);
  160. }
  161. return res;
  162. }
  163. static int unload_module(void)
  164. {
  165. return ast_unregister_application(app_morsecode);
  166. }
  167. static int load_module(void)
  168. {
  169. return ast_register_application_xml(app_morsecode, morsecode_exec);
  170. }
  171. AST_MODULE_INFO_STANDARD_EXTENDED(ASTERISK_GPL_KEY, "Morse code");