You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

166 lines
3.4KB

  1. /*
  2. ** Copyright (c) 2002-2016, Erik de Castro Lopo <erikd@mega-nerd.com>
  3. ** All rights reserved.
  4. **
  5. ** This code is released under 2-clause BSD license. Please see the
  6. ** file at : https://github.com/erikd/libsamplerate/blob/master/COPYING
  7. */
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <samplerate.h>
  12. #include "util.h"
  13. static void name_test (void) ;
  14. static void error_test (void) ;
  15. static void src_ratio_test (void) ;
  16. static void zero_input_test (int converter) ;
  17. int
  18. main (void)
  19. {
  20. puts ("") ;
  21. printf (" version : %s\n\n", src_get_version ()) ;
  22. /* Current max converter is SRC_LINEAR. */
  23. name_test () ;
  24. error_test () ;
  25. src_ratio_test () ;
  26. zero_input_test (SRC_ZERO_ORDER_HOLD) ;
  27. zero_input_test (SRC_LINEAR) ;
  28. zero_input_test (SRC_SINC_FASTEST) ;
  29. puts ("") ;
  30. return 0 ;
  31. } /* main */
  32. static void
  33. name_test (void)
  34. { const char *name ;
  35. int k = 0 ;
  36. puts (" name_test :") ;
  37. while (1)
  38. { name = src_get_name (k) ;
  39. if (name == NULL)
  40. break ;
  41. printf ("\tName %d : %s\n", k, name) ;
  42. printf ("\tDesc %d : %s\n", k, src_get_description (k)) ;
  43. k ++ ;
  44. } ;
  45. puts ("") ;
  46. return ;
  47. } /* name_test */
  48. /*------------------------------------------------------------------------------
  49. */
  50. typedef struct
  51. { double ratio ;
  52. int should_pass ;
  53. } RATIO_TEST ;
  54. static RATIO_TEST ratio_test [] =
  55. { { 1.0 / 256.1, 0 },
  56. { 1.0 / 256.0, 1 },
  57. { 1.0, 1 },
  58. { 256.0, 1 },
  59. { 256.1, 0 },
  60. { -1.0, 0 }
  61. } ;
  62. static void
  63. src_ratio_test (void)
  64. { int k ;
  65. puts (" src_ratio_test (SRC ratio must be in range [1/256, 256]):" ) ;
  66. for (k = 0 ; k < ARRAY_LEN (ratio_test) ; k++)
  67. { if (ratio_test [k].should_pass && src_is_valid_ratio (ratio_test [k].ratio) == 0)
  68. { printf ("\n\nLine %d : SRC ratio %f should have passed.\n\n", __LINE__, ratio_test [k].ratio) ;
  69. exit (1) ;
  70. } ;
  71. if (! ratio_test [k].should_pass && src_is_valid_ratio (ratio_test [k].ratio) != 0)
  72. { printf ("\n\nLine %d : SRC ratio %f should not have passed.\n\n", __LINE__, ratio_test [k].ratio) ;
  73. exit (1) ;
  74. } ;
  75. printf ("\t SRC ratio (%9.5f) : %s ................... ok\n", ratio_test [k].ratio,
  76. (ratio_test [k].should_pass ? "pass" : "fail")) ;
  77. } ;
  78. puts ("") ;
  79. return ;
  80. } /* src_ratio_test */
  81. static void
  82. error_test (void)
  83. { const char *errorstr ;
  84. int k, errors = 0 ;
  85. puts (" error_test :") ;
  86. for (k = 0 ; 1 ; k++)
  87. { errorstr = src_strerror (k) ;
  88. printf ("\t%-2d : %s\n", k, errorstr) ;
  89. if (errorstr == NULL)
  90. { errors ++ ;
  91. continue ;
  92. } ;
  93. if (strstr (errorstr, "Placeholder.") == errorstr)
  94. break ;
  95. } ;
  96. if (errors != 0)
  97. { printf ("\n\nLine %d : Missing error numbers above.\n\n", __LINE__) ;
  98. exit (1) ;
  99. } ;
  100. puts ("") ;
  101. return ;
  102. } /* error_test */
  103. static void
  104. zero_input_test (int converter)
  105. { SRC_DATA data ;
  106. SRC_STATE *state ;
  107. float out [100] ;
  108. int error ;
  109. printf (" %s (%-26s) ........ ", __func__, src_get_name (converter)) ;
  110. fflush (stdout) ;
  111. if ((state = src_new (converter, 1, &error)) == NULL)
  112. { printf ("\n\nLine %d : src_new failed : %s.\n\n", __LINE__, src_strerror (error)) ;
  113. exit (1) ;
  114. } ;
  115. data.data_in = (float *) 0xdeadbeef ;
  116. data.input_frames = 0 ;
  117. data.data_out = out ;
  118. data.output_frames = ARRAY_LEN (out) ;
  119. data.end_of_input = 0 ;
  120. data.src_ratio = 1.0 ;
  121. if ((error = src_process (state, &data)))
  122. { printf ("\n\nLine %d : src_new failed : %s.\n\n", __LINE__, src_strerror (error)) ;
  123. exit (1) ;
  124. } ;
  125. state = src_delete (state) ;
  126. puts ("ok") ;
  127. } /* zero_input_test */