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.

203 lines
5.2KB

  1. /*
  2. ** Copyright (c) 2004-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 <time.h>
  12. #include <unistd.h>
  13. #include <samplerate.h>
  14. #include "config.h"
  15. #include "util.h"
  16. #include "float_cast.h"
  17. #define BUFFER_LEN (1<<16)
  18. static float input [BUFFER_LEN] ;
  19. static float output [BUFFER_LEN] ;
  20. static long
  21. throughput_test (int converter, long best_throughput)
  22. { SRC_DATA src_data ;
  23. clock_t start_time, clock_time ;
  24. double duration ;
  25. long total_frames = 0, throughput ;
  26. int error ;
  27. printf (" %-30s ", src_get_name (converter)) ;
  28. fflush (stdout) ;
  29. src_data.data_in = input ;
  30. src_data.input_frames = ARRAY_LEN (input) ;
  31. src_data.data_out = output ;
  32. src_data.output_frames = ARRAY_LEN (output) ;
  33. src_data.src_ratio = 0.99 ;
  34. sleep (2) ;
  35. start_time = clock () ;
  36. do
  37. {
  38. if ((error = src_simple (&src_data, converter, 1)) != 0)
  39. { puts (src_strerror (error)) ;
  40. exit (1) ;
  41. } ;
  42. total_frames += src_data.output_frames_gen ;
  43. clock_time = clock () - start_time ;
  44. duration = (1.0 * clock_time) / CLOCKS_PER_SEC ;
  45. }
  46. while (duration < 3.0) ;
  47. if (src_data.input_frames_used != ARRAY_LEN (input))
  48. { printf ("\n\nLine %d : input frames used %ld should be %d\n", __LINE__, src_data.input_frames_used, ARRAY_LEN (input)) ;
  49. exit (1) ;
  50. } ;
  51. if (fabs (src_data.src_ratio * src_data.input_frames_used - src_data.output_frames_gen) > 2)
  52. { printf ("\n\nLine %d : input / output length mismatch.\n\n", __LINE__) ;
  53. printf (" input len : %d\n", ARRAY_LEN (input)) ;
  54. printf (" output len : %ld (should be %g +/- 2)\n\n", src_data.output_frames_gen,
  55. floor (0.5 + src_data.src_ratio * src_data.input_frames_used)) ;
  56. exit (1) ;
  57. } ;
  58. throughput = lrint (floor (total_frames / duration)) ;
  59. if (best_throughput == 0)
  60. { best_throughput = MAX (throughput, best_throughput) ;
  61. printf ("%5.2f %10ld\n", duration, throughput) ;
  62. }
  63. else
  64. { best_throughput = MAX (throughput, best_throughput) ;
  65. printf ("%5.2f %10ld %10ld\n", duration, throughput, best_throughput) ;
  66. }
  67. return best_throughput ;
  68. } /* throughput_test */
  69. static void
  70. single_run (void)
  71. {
  72. printf ("\n CPU name : %s\n", get_cpu_name ()) ;
  73. puts (
  74. "\n"
  75. " Converter Duration Throughput\n"
  76. " -----------------------------------------------------------"
  77. ) ;
  78. throughput_test (SRC_ZERO_ORDER_HOLD, 0) ;
  79. throughput_test (SRC_LINEAR, 0) ;
  80. throughput_test (SRC_SINC_FASTEST, 0) ;
  81. throughput_test (SRC_SINC_MEDIUM_QUALITY, 0) ;
  82. throughput_test (SRC_SINC_BEST_QUALITY, 0) ;
  83. puts ("") ;
  84. return ;
  85. } /* single_run */
  86. static void
  87. multi_run (int run_count)
  88. { long zero_order_hold = 0, linear = 0 ;
  89. long sinc_fastest = 0, sinc_medium = 0, sinc_best = 0 ;
  90. int k ;
  91. puts (
  92. "\n"
  93. " Converter Duration Throughput Best Throughput\n"
  94. " --------------------------------------------------------------------------------"
  95. ) ;
  96. for (k = 0 ; k < run_count ; k++)
  97. { zero_order_hold = throughput_test (SRC_ZERO_ORDER_HOLD, zero_order_hold) ;
  98. linear = throughput_test (SRC_LINEAR, linear) ;
  99. sinc_fastest = throughput_test (SRC_SINC_FASTEST, sinc_fastest) ;
  100. sinc_medium = throughput_test (SRC_SINC_MEDIUM_QUALITY, sinc_medium) ;
  101. sinc_best = throughput_test (SRC_SINC_BEST_QUALITY, sinc_best) ;
  102. puts ("") ;
  103. /* Let the CPU cool down. We might be running on a laptop. */
  104. sleep (10) ;
  105. } ;
  106. printf ("\n CPU name : %s\n", get_cpu_name ()) ;
  107. puts (
  108. "\n"
  109. " Converter Best Throughput\n"
  110. " ------------------------------------------------"
  111. ) ;
  112. printf (" %-30s %10ld\n", src_get_name (SRC_ZERO_ORDER_HOLD), zero_order_hold) ;
  113. printf (" %-30s %10ld\n", src_get_name (SRC_LINEAR), linear) ;
  114. printf (" %-30s %10ld\n", src_get_name (SRC_SINC_FASTEST), sinc_fastest) ;
  115. printf (" %-30s %10ld\n", src_get_name (SRC_SINC_MEDIUM_QUALITY), sinc_medium) ;
  116. printf (" %-30s %10ld\n", src_get_name (SRC_SINC_BEST_QUALITY), sinc_best) ;
  117. puts ("") ;
  118. } /* multi_run */
  119. static void
  120. usage_exit (const char * argv0)
  121. { const char * cptr ;
  122. if ((cptr = strrchr (argv0, '/')) != NULL)
  123. argv0 = cptr ;
  124. printf (
  125. "Usage :\n"
  126. " %s - Single run of the throughput test.\n"
  127. " %s --best-of N - Do N runs of test a print bext result.\n"
  128. "\n",
  129. argv0, argv0) ;
  130. exit (0) ;
  131. } /* usage_exit */
  132. int
  133. main (int argc, char ** argv)
  134. { double freq ;
  135. memset (input, 0, sizeof (input)) ;
  136. freq = 0.01 ;
  137. gen_windowed_sines (1, &freq, 1.0, input, BUFFER_LEN) ;
  138. if (argc == 1)
  139. single_run () ;
  140. else if (argc == 3 && strcmp (argv [1], "--best-of") == 0)
  141. { int run_count = atoi (argv [2]) ;
  142. if (run_count < 1 || run_count > 20)
  143. { printf ("Please be sensible. Run count should be in range (1, 10].\n") ;
  144. exit (1) ;
  145. } ;
  146. multi_run (run_count) ;
  147. }
  148. else
  149. usage_exit (argv [0]) ;
  150. puts (
  151. " Duration is in seconds.\n"
  152. " Throughput is in samples/sec (more is better).\n"
  153. ) ;
  154. return 0 ;
  155. } /* main */