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.

229 lines
5.8KB

  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 <samplerate.h>
  11. #include "util.h"
  12. #define BUFFER_LEN 2048
  13. #define CB_READ_LEN 256
  14. static void process_reset_test (int converter) ;
  15. static void callback_reset_test (int converter) ;
  16. static float data_one [BUFFER_LEN] ;
  17. static float data_zero [BUFFER_LEN] ;
  18. int
  19. main (void)
  20. {
  21. puts ("") ;
  22. process_reset_test (SRC_ZERO_ORDER_HOLD) ;
  23. process_reset_test (SRC_LINEAR) ;
  24. process_reset_test (SRC_SINC_FASTEST) ;
  25. callback_reset_test (SRC_ZERO_ORDER_HOLD) ;
  26. callback_reset_test (SRC_LINEAR) ;
  27. callback_reset_test (SRC_SINC_FASTEST) ;
  28. puts ("") ;
  29. return 0 ;
  30. } /* main */
  31. static void
  32. process_reset_test (int converter)
  33. { static float output [BUFFER_LEN] ;
  34. SRC_STATE *src_state ;
  35. SRC_DATA src_data ;
  36. int k, error ;
  37. printf ("\tprocess_reset_test (%-28s) ....... ", src_get_name (converter)) ;
  38. fflush (stdout) ;
  39. for (k = 0 ; k < BUFFER_LEN ; k++)
  40. { data_one [k] = 1.0 ;
  41. data_zero [k] = 0.0 ;
  42. } ;
  43. /* Get a converter. */
  44. if ((src_state = src_new (converter, 1, &error)) == NULL)
  45. { printf ("\n\nLine %d : src_new() failed : %s.\n\n", __LINE__, src_strerror (error)) ;
  46. exit (1) ;
  47. } ;
  48. /* Process a bunch of 1.0 valued samples. */
  49. src_data.data_in = data_one ;
  50. src_data.data_out = output ;
  51. src_data.input_frames = BUFFER_LEN ;
  52. src_data.output_frames = BUFFER_LEN ;
  53. src_data.src_ratio = 0.9 ;
  54. src_data.end_of_input = 1 ;
  55. if ((error = src_process (src_state, &src_data)) != 0)
  56. { printf ("\n\nLine %d : src_simple () returned error : %s\n\n", __LINE__, src_strerror (error)) ;
  57. exit (1) ;
  58. } ;
  59. /* Reset the state of the converter.*/
  60. src_reset (src_state) ;
  61. /* Now process some zero data. */
  62. src_data.data_in = data_zero ;
  63. src_data.data_out = output ;
  64. src_data.input_frames = BUFFER_LEN ;
  65. src_data.output_frames = BUFFER_LEN ;
  66. src_data.src_ratio = 0.9 ;
  67. src_data.end_of_input = 1 ;
  68. if ((error = src_process (src_state, &src_data)) != 0)
  69. { printf ("\n\nLine %d : src_simple () returned error : %s\n\n", __LINE__, src_strerror (error)) ;
  70. exit (1) ;
  71. } ;
  72. /* Finally make sure that the output data is zero ie reset was sucessful. */
  73. for (k = 0 ; k < BUFFER_LEN / 2 ; k++)
  74. if (output [k] != 0.0)
  75. { printf ("\n\nLine %d : output [%d] should be 0.0, is %f.\n", __LINE__, k, output [k]) ;
  76. exit (1) ;
  77. } ;
  78. /* Make sure that this function has been exported. */
  79. src_set_ratio (src_state, 1.0) ;
  80. /* Delete converter. */
  81. src_state = src_delete (src_state) ;
  82. puts ("ok") ;
  83. } /* process_reset_test */
  84. /*==============================================================================
  85. */
  86. typedef struct
  87. { int channels ;
  88. long count, total ;
  89. float *data ;
  90. } TEST_CB_DATA ;
  91. static long
  92. test_callback_func (void *cb_data, float **data)
  93. { TEST_CB_DATA *pcb_data ;
  94. long frames ;
  95. if ((pcb_data = cb_data) == NULL)
  96. return 0 ;
  97. if (data == NULL)
  98. return 0 ;
  99. if (pcb_data->total - pcb_data->count > 0)
  100. frames = pcb_data->total - pcb_data->count ;
  101. else
  102. frames = 0 ;
  103. *data = pcb_data->data + pcb_data->count ;
  104. pcb_data->count += frames ;
  105. return frames ;
  106. } /* test_callback_func */
  107. static void
  108. callback_reset_test (int converter)
  109. { static TEST_CB_DATA test_callback_data ;
  110. static float output [BUFFER_LEN] ;
  111. SRC_STATE *src_state ;
  112. double src_ratio = 1.1 ;
  113. long read_count, read_total ;
  114. int k, error ;
  115. printf ("\tcallback_reset_test (%-28s) ....... ", src_get_name (converter)) ;
  116. fflush (stdout) ;
  117. for (k = 0 ; k < ARRAY_LEN (data_one) ; k++)
  118. { data_one [k] = 1.0 ;
  119. data_zero [k] = 0.0 ;
  120. } ;
  121. if ((src_state = src_callback_new (test_callback_func, converter, 1, &error, &test_callback_data)) == NULL)
  122. { printf ("\n\nLine %d : %s\n\n", __LINE__, src_strerror (error)) ;
  123. exit (1) ;
  124. } ;
  125. /* Process a bunch of 1.0 valued samples. */
  126. test_callback_data.channels = 1 ;
  127. test_callback_data.count = 0 ;
  128. test_callback_data.total = ARRAY_LEN (data_one) ;
  129. test_callback_data.data = data_one ;
  130. read_total = 0 ;
  131. do
  132. { read_count = (ARRAY_LEN (output) - read_total > CB_READ_LEN) ? CB_READ_LEN : ARRAY_LEN (output) - read_total ;
  133. read_count = src_callback_read (src_state, src_ratio, read_count, output + read_total) ;
  134. read_total += read_count ;
  135. }
  136. while (read_count > 0) ;
  137. /* Check for errors. */
  138. if ((error = src_error (src_state)) != 0)
  139. { printf ("\n\nLine %d : %s\n\n", __LINE__, src_strerror (error)) ;
  140. exit (1) ;
  141. } ;
  142. /* Reset the state of the converter.*/
  143. src_reset (src_state) ;
  144. /* Process a bunch of 0.0 valued samples. */
  145. test_callback_data.channels = 1 ;
  146. test_callback_data.count = 0 ;
  147. test_callback_data.total = ARRAY_LEN (data_zero) ;
  148. test_callback_data.data = data_zero ;
  149. /* Now process some zero data. */
  150. read_total = 0 ;
  151. do
  152. { read_count = (ARRAY_LEN (output) - read_total > CB_READ_LEN) ? CB_READ_LEN : ARRAY_LEN (output) - read_total ;
  153. read_count = src_callback_read (src_state, src_ratio, read_count, output + read_total) ;
  154. read_total += read_count ;
  155. }
  156. while (read_count > 0) ;
  157. /* Check for errors. */
  158. if ((error = src_error (src_state)) != 0)
  159. { printf ("\n\nLine %d : %s\n\n", __LINE__, src_strerror (error)) ;
  160. exit (1) ;
  161. } ;
  162. /* Finally make sure that the output data is zero ie reset was sucessful. */
  163. for (k = 0 ; k < BUFFER_LEN / 2 ; k++)
  164. if (output [k] != 0.0)
  165. { printf ("\n\nLine %d : output [%d] should be 0.0, is %f.\n\n", __LINE__, k, output [k]) ;
  166. save_oct_float ("output.dat", data_one, ARRAY_LEN (data_one), output, ARRAY_LEN (output)) ;
  167. exit (1) ;
  168. } ;
  169. /* Make sure that this function has been exported. */
  170. src_set_ratio (src_state, 1.0) ;
  171. /* Delete converter. */
  172. src_state = src_delete (src_state) ;
  173. puts ("ok") ;
  174. } /* callback_reset_test */