dtmf_tx_tests.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * SpanDSP - a series of DSP components for telephony
  3. *
  4. * dtmf_tx_tests.c - Test the DTMF generator.
  5. *
  6. * Written by Steve Underwood <steveu@coppice.org>
  7. *
  8. * Copyright (C) 2003 Steve Underwood
  9. *
  10. * All rights reserved.
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License version 2, as
  14. * published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  24. */
  25. /*! \file */
  26. /*! \page dtmf_tx_tests_page DTMF generation tests
  27. \section dtmf_tx_tests_page_sec_1 What does it do?
  28. ???.
  29. \section dtmf_tx_tests_page_sec_2 How does it work?
  30. ???.
  31. */
  32. #if defined(HAVE_CONFIG_H)
  33. #include "config.h"
  34. #endif
  35. #include <stdlib.h>
  36. #include <string.h>
  37. #include <stdio.h>
  38. #include <fcntl.h>
  39. #include <time.h>
  40. #include <sndfile.h>
  41. #include "spandsp.h"
  42. #include "spandsp-sim.h"
  43. #define OUTPUT_FILE_NAME "dtmf.wav"
  44. int main(int argc, char *argv[])
  45. {
  46. dtmf_tx_state_t *gen;
  47. int16_t amp[16384];
  48. int len;
  49. SNDFILE *outhandle;
  50. int add_digits;
  51. if ((outhandle = sf_open_telephony_write(OUTPUT_FILE_NAME, 1)) == NULL)
  52. {
  53. fprintf(stderr, " Cannot open audio file '%s'\n", OUTPUT_FILE_NAME);
  54. exit(2);
  55. }
  56. gen = dtmf_tx_init(NULL, NULL, NULL);
  57. len = dtmf_tx(gen, amp, 16384);
  58. printf("Generated %d samples\n", len);
  59. sf_writef_short(outhandle, amp, len);
  60. if (dtmf_tx_put(gen, "123", -1))
  61. {
  62. printf("Ooops\n");
  63. exit(2);
  64. }
  65. len = dtmf_tx(gen, amp, 16384);
  66. printf("Generated %d samples\n", len);
  67. sf_writef_short(outhandle, amp, len);
  68. if (dtmf_tx_put(gen, "456", -1))
  69. {
  70. printf("Ooops\n");
  71. exit(2);
  72. }
  73. len = dtmf_tx(gen, amp, 160);
  74. printf("Generated %d samples\n", len);
  75. sf_writef_short(outhandle, amp, len);
  76. if (dtmf_tx_put(gen, "789", -1))
  77. {
  78. printf("Ooops\n");
  79. exit(2);
  80. }
  81. len = dtmf_tx(gen, amp, 160);
  82. printf("Generated %d samples\n", len);
  83. sf_writef_short(outhandle, amp, len);
  84. if (dtmf_tx_put(gen, "*#", -1))
  85. {
  86. printf("Ooops\n");
  87. exit(2);
  88. }
  89. len = dtmf_tx(gen, amp, 160);
  90. printf("Generated %d samples\n", len);
  91. sf_writef_short(outhandle, amp, len);
  92. add_digits = 1;
  93. do
  94. {
  95. len = dtmf_tx(gen, amp, 160);
  96. printf("Generated %d samples\n", len);
  97. if (len > 0)
  98. sf_writef_short(outhandle, amp, len);
  99. if (add_digits)
  100. {
  101. if (dtmf_tx_put(gen, "1234567890", -1))
  102. {
  103. printf("Digit buffer full\n");
  104. add_digits = 0;
  105. }
  106. }
  107. }
  108. while (len > 0);
  109. dtmf_tx_init(gen, NULL, NULL);
  110. len = dtmf_tx(gen, amp, 16384);
  111. printf("Generated %d samples\n", len);
  112. sf_writef_short(outhandle, amp, len);
  113. if (dtmf_tx_put(gen, "123", -1))
  114. {
  115. printf("Ooops\n");
  116. exit(2);
  117. }
  118. len = dtmf_tx(gen, amp, 16384);
  119. printf("Generated %d samples\n", len);
  120. sf_writef_short(outhandle, amp, len);
  121. if (dtmf_tx_put(gen, "456", -1))
  122. {
  123. printf("Ooops\n");
  124. exit(2);
  125. }
  126. len = dtmf_tx(gen, amp, 160);
  127. printf("Generated %d samples\n", len);
  128. sf_writef_short(outhandle, amp, len);
  129. if (dtmf_tx_put(gen, "789", -1))
  130. {
  131. printf("Ooops\n");
  132. exit(2);
  133. }
  134. len = dtmf_tx(gen, amp, 160);
  135. printf("Generated %d samples\n", len);
  136. sf_writef_short(outhandle, amp, len);
  137. if (dtmf_tx_put(gen, "0*#", -1))
  138. {
  139. printf("Ooops\n");
  140. exit(2);
  141. }
  142. len = dtmf_tx(gen, amp, 160);
  143. printf("Generated %d samples\n", len);
  144. sf_writef_short(outhandle, amp, len);
  145. if (dtmf_tx_put(gen, "ABCD", -1))
  146. {
  147. printf("Ooops\n");
  148. exit(2);
  149. }
  150. len = dtmf_tx(gen, amp, 160);
  151. printf("Generated %d samples\n", len);
  152. sf_writef_short(outhandle, amp, len);
  153. /* Try modifying the level and length of the digits */
  154. printf("Try different levels and timing\n");
  155. dtmf_tx_set_level(gen, -20, 5);
  156. dtmf_tx_set_timing(gen, 100, 200);
  157. if (dtmf_tx_put(gen, "123", -1))
  158. {
  159. printf("Ooops\n");
  160. exit(2);
  161. }
  162. do
  163. {
  164. len = dtmf_tx(gen, amp, 160);
  165. printf("Generated %d samples\n", len);
  166. if (len > 0)
  167. sf_writef_short(outhandle, amp, len);
  168. }
  169. while (len > 0);
  170. printf("Restore normal levels and timing\n");
  171. dtmf_tx_set_level(gen, -10, 0);
  172. dtmf_tx_set_timing(gen, 50, 55);
  173. if (dtmf_tx_put(gen, "A", -1))
  174. {
  175. printf("Ooops\n");
  176. exit(2);
  177. }
  178. add_digits = true;
  179. do
  180. {
  181. len = dtmf_tx(gen, amp, 160);
  182. printf("Generated %d samples\n", len);
  183. if (len > 0)
  184. sf_writef_short(outhandle, amp, len);
  185. if (add_digits)
  186. {
  187. if (dtmf_tx_put(gen, "1234567890", -1))
  188. {
  189. printf("Digit buffer full\n");
  190. add_digits = false;
  191. }
  192. }
  193. }
  194. while (len > 0);
  195. if (sf_close_telephony(outhandle))
  196. {
  197. fprintf(stderr, " Cannot close audio file '%s'\n", OUTPUT_FILE_NAME);
  198. exit(2);
  199. }
  200. dtmf_tx_free(gen);
  201. return 0;
  202. }
  203. /*- End of function --------------------------------------------------------*/
  204. /*- End of file ------------------------------------------------------------*/