tone_generate_tests.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. * SpanDSP - a series of DSP components for telephony
  3. *
  4. * tone_generate_tests.c
  5. *
  6. * Written by Steve Underwood <steveu@coppice.org>
  7. *
  8. * Copyright (C) 2001 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. /*! \page tone_generate_tests_page Tone generation tests
  26. \section tone_generate_tests_page_sec_1 What does it do?
  27. */
  28. #if defined(HAVE_CONFIG_H)
  29. #include "config.h"
  30. #endif
  31. #include <stdlib.h>
  32. #include <stdio.h>
  33. #include <fcntl.h>
  34. #include <string.h>
  35. #include <time.h>
  36. #include <sndfile.h>
  37. #include "spandsp.h"
  38. #include "spandsp-sim.h"
  39. #define OUTPUT_FILE_NAME "tone_generate.wav"
  40. int main(int argc, char *argv[])
  41. {
  42. tone_gen_descriptor_t tone_desc;
  43. tone_gen_state_t tone_state;
  44. int i;
  45. int16_t amp[16384];
  46. int len;
  47. SNDFILE *outhandle;
  48. if ((outhandle = sf_open_telephony_write(OUTPUT_FILE_NAME, 1)) == NULL)
  49. {
  50. fprintf(stderr, " Cannot open audio file '%s'\n", OUTPUT_FILE_NAME);
  51. exit(2);
  52. }
  53. /* Try a tone pair */
  54. tone_gen_descriptor_init(&tone_desc,
  55. 440,
  56. -10,
  57. 620,
  58. -15,
  59. 100,
  60. 200,
  61. 300,
  62. 400,
  63. false);
  64. tone_gen_init(&tone_state, &tone_desc);
  65. for (i = 0; i < 1000; i++)
  66. {
  67. len = tone_gen(&tone_state, amp, 160);
  68. printf("Generated %d samples\n", len);
  69. if (len <= 0)
  70. break;
  71. sf_writef_short(outhandle, amp, len);
  72. }
  73. tone_gen_release(&tone_state);
  74. /* Try a different tone pair */
  75. tone_gen_descriptor_init(&tone_desc,
  76. 350,
  77. -10,
  78. 440,
  79. -15,
  80. 400,
  81. 300,
  82. 200,
  83. 100,
  84. true);
  85. tone_gen_init(&tone_state, &tone_desc);
  86. for (i = 0; i < 1000; i++)
  87. {
  88. len = tone_gen(&tone_state, amp, 160);
  89. printf("Generated %d samples\n", len);
  90. if (len <= 0)
  91. break;
  92. sf_writef_short(outhandle, amp, len);
  93. }
  94. tone_gen_release(&tone_state);
  95. /* Try a different tone pair */
  96. tone_gen_descriptor_init(&tone_desc,
  97. 400,
  98. -10,
  99. 450,
  100. -10,
  101. 100,
  102. 200,
  103. 300,
  104. 400,
  105. true);
  106. tone_gen_init(&tone_state, &tone_desc);
  107. for (i = 0; i < 1000; i++)
  108. {
  109. len = tone_gen(&tone_state, amp, 160);
  110. printf("Generated %d samples\n", len);
  111. if (len <= 0)
  112. break;
  113. sf_writef_short(outhandle, amp, len);
  114. }
  115. tone_gen_release(&tone_state);
  116. /* Try a single tone */
  117. tone_gen_descriptor_init(&tone_desc,
  118. 400,
  119. -10,
  120. 0,
  121. 0,
  122. 100,
  123. 200,
  124. 300,
  125. 400,
  126. true);
  127. tone_gen_init(&tone_state, &tone_desc);
  128. for (i = 0; i < 1000; i++)
  129. {
  130. len = tone_gen(&tone_state, amp, 160);
  131. printf("Generated %d samples\n", len);
  132. if (len <= 0)
  133. break;
  134. sf_writef_short(outhandle, amp, len);
  135. }
  136. tone_gen_release(&tone_state);
  137. /* Try a single non-repeating tone */
  138. tone_gen_descriptor_init(&tone_desc,
  139. 820,
  140. -10,
  141. 0,
  142. 0,
  143. 2000,
  144. 0,
  145. 0,
  146. 0,
  147. false);
  148. tone_gen_init(&tone_state, &tone_desc);
  149. for (i = 0; i < 1000; i++)
  150. {
  151. len = tone_gen(&tone_state, amp, 160);
  152. printf("Generated %d samples\n", len);
  153. if (len <= 0)
  154. break;
  155. sf_writef_short(outhandle, amp, len);
  156. }
  157. tone_gen_release(&tone_state);
  158. /* Try a single non-repeating tone at 0dBm0 */
  159. tone_gen_descriptor_init(&tone_desc,
  160. 820,
  161. 0,
  162. 0,
  163. 0,
  164. 2000,
  165. 0,
  166. 0,
  167. 0,
  168. false);
  169. tone_gen_init(&tone_state, &tone_desc);
  170. for (i = 0; i < 1000; i++)
  171. {
  172. len = tone_gen(&tone_state, amp, 160);
  173. printf("Generated %d samples\n", len);
  174. if (len <= 0)
  175. break;
  176. sf_writef_short(outhandle, amp, len);
  177. }
  178. tone_gen_release(&tone_state);
  179. /* Try an AM modulated tone at a modest modulation level (25%) */
  180. tone_gen_descriptor_init(&tone_desc,
  181. 425,
  182. -10,
  183. -50,
  184. 25,
  185. 100,
  186. 200,
  187. 300,
  188. 400,
  189. true);
  190. tone_gen_init(&tone_state, &tone_desc);
  191. for (i = 0; i < 1000; i++)
  192. {
  193. len = tone_gen(&tone_state, amp, 160);
  194. printf("Generated %d samples\n", len);
  195. if (len <= 0)
  196. break;
  197. sf_writef_short(outhandle, amp, len);
  198. }
  199. tone_gen_release(&tone_state);
  200. /* Try an AM modulated tone at maximum modulation level (100%) */
  201. tone_gen_descriptor_init(&tone_desc,
  202. 425,
  203. -10,
  204. -50,
  205. 100,
  206. 100,
  207. 200,
  208. 300,
  209. 400,
  210. true);
  211. tone_gen_init(&tone_state, &tone_desc);
  212. for (i = 0; i < 1000; i++)
  213. {
  214. len = tone_gen(&tone_state, amp, 160);
  215. printf("Generated %d samples\n", len);
  216. if (len <= 0)
  217. break;
  218. sf_writef_short(outhandle, amp, len);
  219. }
  220. tone_gen_release(&tone_state);
  221. if (sf_close_telephony(outhandle))
  222. {
  223. fprintf(stderr, " Cannot close audio file '%s'\n", OUTPUT_FILE_NAME);
  224. exit (2);
  225. }
  226. return 0;
  227. }
  228. /*- End of function --------------------------------------------------------*/
  229. /*- End of file ------------------------------------------------------------*/