swept_tone_tests.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * SpanDSP - a series of DSP components for telephony
  3. *
  4. * swept_tone_tests.c
  5. *
  6. * Written by Steve Underwood <steveu@coppice.org>
  7. *
  8. * Copyright (C) 2009 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. #if defined(HAVE_CONFIG_H)
  27. #include "config.h"
  28. #endif
  29. #include <stdlib.h>
  30. #include <stdio.h>
  31. #include <fcntl.h>
  32. #include <memory.h>
  33. #include <sndfile.h>
  34. #include "spandsp.h"
  35. #include "spandsp-sim.h"
  36. #define OUTPUT_FILE_NAME "swept_tone.wav"
  37. #define BLOCK_LEN 160
  38. int main(int argc, char *argv[])
  39. {
  40. int i;
  41. int j;
  42. int outframes;
  43. int len;
  44. SNDFILE *outhandle;
  45. power_meter_t meter;
  46. swept_tone_state_t *s;
  47. int16_t buf[BLOCK_LEN];
  48. power_meter_init(&meter, 10);
  49. if ((outhandle = sf_open_telephony_write(OUTPUT_FILE_NAME, 1)) == NULL)
  50. {
  51. fprintf(stderr, " Cannot create audio file '%s'\n", OUTPUT_FILE_NAME);
  52. exit(2);
  53. }
  54. printf("Test with swept tone.\n");
  55. s = swept_tone_init(NULL, 200.0f, 3900.0f, -10.0f, 60*SAMPLE_RATE, true);
  56. for (j = 0; j < 60*SAMPLE_RATE; j += BLOCK_LEN)
  57. {
  58. len = swept_tone(s, buf, BLOCK_LEN);
  59. for (i = 0; i < len; i++)
  60. power_meter_update(&meter, buf[i]);
  61. outframes = sf_writef_short(outhandle, buf, len);
  62. if (outframes != len)
  63. {
  64. fprintf(stderr, " Error writing audio file\n");
  65. exit(2);
  66. }
  67. #if 0
  68. printf("Current freq %.1fHz, Level is %fdBOv/%fdBm0\n",
  69. swept_tone_current_frequency(s),
  70. power_meter_current_dbov(&meter),
  71. power_meter_current_dbm0(&meter));
  72. #else
  73. printf("%.1f %f\n",
  74. swept_tone_current_frequency(s),
  75. power_meter_current_dbm0(&meter));
  76. #endif
  77. }
  78. if (sf_close_telephony(outhandle))
  79. {
  80. fprintf(stderr, " Cannot close audio file '%s'\n", OUTPUT_FILE_NAME);
  81. exit(2);
  82. }
  83. swept_tone_free(s);
  84. power_meter_release(&meter);
  85. printf("Tests passed.\n");
  86. return 0;
  87. }
  88. /*- End of function --------------------------------------------------------*/
  89. /*- End of file ------------------------------------------------------------*/