peak_chunk_test.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. /*
  2. ** Copyright (C) 2001-2016 Erik de Castro Lopo <[email protected]>
  3. **
  4. ** This program is free software; you can redistribute it and/or modify
  5. ** it under the terms of the GNU General Public License as published by
  6. ** the Free Software Foundation; either version 2 of the License, or
  7. ** (at your option) any later version.
  8. **
  9. ** This program is distributed in the hope that it will be useful,
  10. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ** GNU General Public License for more details.
  13. **
  14. ** You should have received a copy of the GNU General Public License
  15. ** along with this program; if not, write to the Free Software
  16. ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. */
  18. #include "sfconfig.h"
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <math.h>
  23. #include <inttypes.h>
  24. #if HAVE_UNISTD_H
  25. #include <unistd.h>
  26. #else
  27. #include "sf_unistd.h"
  28. #endif
  29. #include <sndfile.h>
  30. #include "utils.h"
  31. #define BUFFER_LEN (1 << 15)
  32. #define LOG_BUFFER_SIZE 1024
  33. static void test_float_peak (const char *filename, int filetype) ;
  34. static void read_write_peak_test (const char *filename, int filetype) ;
  35. static void check_logged_peaks (char *buffer) ;
  36. /* Force the start of this buffer to be double aligned. Sparc-solaris will
  37. ** choke if its not.
  38. */
  39. static double data [BUFFER_LEN] ;
  40. static char log_buffer [LOG_BUFFER_SIZE] ;
  41. int
  42. main (int argc, char *argv [])
  43. { int do_all = 0 ;
  44. int test_count = 0 ;
  45. if (argc != 2)
  46. { printf ("Usage : %s <test>\n", argv [0]) ;
  47. printf (" Where <test> is one of the following:\n") ;
  48. printf (" aiff - test AIFF file PEAK chunk\n") ;
  49. printf (" caf - test CAF file PEAK chunk\n") ;
  50. printf (" wav - test WAV file peak chunk\n") ;
  51. printf (" all - perform all tests\n") ;
  52. exit (1) ;
  53. } ;
  54. do_all = ! strcmp (argv [1], "all") ;
  55. if (do_all || ! strcmp (argv [1], "wav"))
  56. { test_float_peak ("peak_float.wav", SF_FORMAT_WAV | SF_FORMAT_FLOAT) ;
  57. test_float_peak ("peak_float.wavex", SF_FORMAT_WAVEX | SF_FORMAT_FLOAT) ;
  58. test_float_peak ("peak_float.rifx", SF_ENDIAN_BIG | SF_FORMAT_WAV | SF_FORMAT_FLOAT) ;
  59. read_write_peak_test ("rw_peak.wav", SF_FORMAT_WAV | SF_FORMAT_FLOAT) ;
  60. read_write_peak_test ("rw_peak.wavex", SF_FORMAT_WAVEX | SF_FORMAT_FLOAT) ;
  61. test_count++ ;
  62. } ;
  63. if (do_all || ! strcmp (argv [1], "aiff"))
  64. { test_float_peak ("peak_float.aiff", SF_FORMAT_AIFF | SF_FORMAT_FLOAT) ;
  65. read_write_peak_test ("rw_peak.aiff", SF_FORMAT_AIFF | SF_FORMAT_FLOAT) ;
  66. test_count++ ;
  67. } ;
  68. if (do_all || ! strcmp (argv [1], "caf"))
  69. { test_float_peak ("peak_float.caf", SF_FORMAT_CAF | SF_FORMAT_FLOAT) ;
  70. read_write_peak_test ("rw_peak.caf", SF_FORMAT_CAF | SF_FORMAT_FLOAT) ;
  71. test_count++ ;
  72. } ;
  73. if (do_all || ! strcmp (argv [1], "rf64"))
  74. { test_float_peak ("peak_float.rf64", SF_FORMAT_RF64 | SF_FORMAT_FLOAT) ;
  75. read_write_peak_test ("rw_peak.rf64", SF_FORMAT_RF64 | SF_FORMAT_FLOAT) ;
  76. test_count++ ;
  77. } ;
  78. if (test_count == 0)
  79. { printf ("Mono : ************************************\n") ;
  80. printf ("Mono : * No '%s' test defined.\n", argv [1]) ;
  81. printf ("Mono : ************************************\n") ;
  82. return 1 ;
  83. } ;
  84. return 0 ;
  85. } /* main */
  86. /*============================================================================================
  87. ** Here are the test functions.
  88. */
  89. static void
  90. test_float_peak (const char *filename, int filetype)
  91. { SNDFILE *file ;
  92. SF_INFO sfinfo ;
  93. int k, frames, count ;
  94. print_test_name ("test_float_peak", filename) ;
  95. memset (&sfinfo, 0, sizeof (sfinfo)) ;
  96. sfinfo.samplerate = 44100 ;
  97. sfinfo.format = filetype ;
  98. sfinfo.channels = 4 ;
  99. sfinfo.frames = 0 ;
  100. frames = BUFFER_LEN / sfinfo.channels ;
  101. /* Create some random data with a peak value of 0.66. */
  102. for (k = 0 ; k < BUFFER_LEN ; k++)
  103. data [k] = (rand () % 2000) / 3000.0 ;
  104. /* Insert some larger peaks a know locations. */
  105. data [4 * (frames / 8) + 0] = (frames / 8) * 0.01 ; /* First channel */
  106. data [4 * (frames / 6) + 1] = (frames / 6) * 0.01 ; /* Second channel */
  107. data [4 * (frames / 4) + 2] = (frames / 4) * 0.01 ; /* Third channel */
  108. data [4 * (frames / 2) + 3] = (frames / 2) * 0.01 ; /* Fourth channel */
  109. /* Write a file with PEAK chunks. */
  110. file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, 0, __LINE__) ;
  111. /* Try to confuse the header writer by adding a removing the PEAK chunk. */
  112. sf_command (file, SFC_SET_ADD_PEAK_CHUNK, NULL, SF_TRUE) ;
  113. sf_command (file, SFC_SET_ADD_PEAK_CHUNK, NULL, SF_FALSE) ;
  114. sf_command (file, SFC_SET_ADD_PEAK_CHUNK, NULL, SF_TRUE) ;
  115. /* Write the data in four passed. The data is designed so that peaks will
  116. ** be written in the different calls to sf_write_double ().
  117. */
  118. for (count = 0 ; count < 4 ; count ++)
  119. test_write_double_or_die (file, 0, data + count * BUFFER_LEN / 4, BUFFER_LEN / 4, BUFFER_LEN / 4) ;
  120. sf_close (file) ;
  121. file = test_open_file_or_die (filename, SFM_READ, &sfinfo, 0, __LINE__) ;
  122. if (sfinfo.format != filetype)
  123. { printf ("\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, filetype, sfinfo.format) ;
  124. exit (1) ;
  125. } ;
  126. if (sfinfo.frames != frames)
  127. { printf ("\n\nLine %d: Incorrect number of frames in file. (%d => %ld)\n", __LINE__, frames, (long) sfinfo.frames) ;
  128. exit (1) ;
  129. } ;
  130. if (sfinfo.channels != 4)
  131. { printf ("\n\nLine %d: Incorrect number of channels in file.\n", __LINE__) ;
  132. exit (1) ;
  133. } ;
  134. /* Check these two commands. */
  135. if (sf_command (file, SFC_GET_SIGNAL_MAX, data, sizeof (double)) == SF_FALSE)
  136. { printf ("\n\nLine %d: Command should have returned SF_TRUE.\n", __LINE__) ;
  137. exit (1) ;
  138. } ;
  139. if (fabs (data [0] - (frames / 2) * 0.01) > 0.01)
  140. { printf ("\n\nLine %d: Bad peak value (%f should be %f) for command SFC_GET_SIGNAL_MAX.\n", __LINE__, data [0], (frames / 2) * 0.01) ;
  141. exit (1) ;
  142. } ;
  143. if (sf_command (file, SFC_GET_MAX_ALL_CHANNELS, data, sizeof (double) * sfinfo.channels) == SF_FALSE)
  144. { printf ("\n\nLine %d: Command should have returned SF_TRUE.\n", __LINE__) ;
  145. exit (1) ;
  146. } ;
  147. if (fabs (data [3] - (frames / 2) * 0.01) > 0.01)
  148. { printf ("\n\nLine %d: Bad peak value (%f should be %f) for command SFC_GET_MAX_ALL_CHANNELS.\n", __LINE__, data [0], (frames / 2) * 0.01) ;
  149. exit (1) ;
  150. } ;
  151. /* Get the log buffer data. */
  152. log_buffer [0] = 0 ;
  153. sf_command (file, SFC_GET_LOG_INFO, log_buffer, LOG_BUFFER_SIZE) ;
  154. if (strlen (log_buffer) == 0)
  155. { printf ("\n\nLine %d: Empty log buffer,\n", __LINE__) ;
  156. exit (1) ;
  157. } ;
  158. check_logged_peaks (log_buffer) ;
  159. sf_close (file) ;
  160. /* Write a file ***without*** PEAK chunks. */
  161. file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, 0, __LINE__) ;
  162. /* Try to confuse the header writer by adding a removing the PEAK chunk. */
  163. sf_command (file, SFC_SET_ADD_PEAK_CHUNK, NULL, SF_FALSE) ;
  164. sf_command (file, SFC_SET_ADD_PEAK_CHUNK, NULL, SF_TRUE) ;
  165. sf_command (file, SFC_SET_ADD_PEAK_CHUNK, NULL, SF_FALSE) ;
  166. /* Write the data in four passed. The data is designed so that peaks will
  167. ** be written in the different calls to sf_write_double ().
  168. */
  169. for (count = 0 ; count < 4 ; count ++)
  170. test_write_double_or_die (file, 0, data + count * BUFFER_LEN / 4, BUFFER_LEN / 4, BUFFER_LEN / 4) ;
  171. sf_close (file) ;
  172. file = test_open_file_or_die (filename, SFM_READ, &sfinfo, 0, __LINE__) ;
  173. /* Check these two commands. */
  174. if (sf_command (file, SFC_GET_SIGNAL_MAX, data, sizeof (double)))
  175. { printf ("\n\nLine %d: Command should have returned SF_FALSE.\n", __LINE__) ;
  176. exit (1) ;
  177. } ;
  178. if (sf_command (file, SFC_GET_MAX_ALL_CHANNELS, data, sizeof (double) * sfinfo.channels))
  179. { printf ("\n\nLine %d: Command should have returned SF_FALSE.\n", __LINE__) ;
  180. exit (1) ;
  181. } ;
  182. /* Get the log buffer data. */
  183. log_buffer [0] = 0 ;
  184. sf_command (file, SFC_GET_LOG_INFO, log_buffer, LOG_BUFFER_SIZE) ;
  185. if (strlen (log_buffer) == 0)
  186. { printf ("\n\nLine %d: Empty log buffer,\n", __LINE__) ;
  187. exit (1) ;
  188. } ;
  189. if (strstr (log_buffer, "PEAK :") != NULL)
  190. { printf ("\n\nLine %d: Should not have a PEAK chunk in this file.\n\n", __LINE__) ;
  191. puts (log_buffer) ;
  192. exit (1) ;
  193. } ;
  194. sf_close (file) ;
  195. unlink (filename) ;
  196. printf ("ok\n") ;
  197. } /* test_float_peak */
  198. static void
  199. check_logged_peaks (char *buffer)
  200. { char *cptr ;
  201. int k, chan, channel_count, position ;
  202. float value ;
  203. if (strstr (buffer, "should") || strstr (buffer, "*"))
  204. { printf ("\n\nLine %d: Something wrong in buffer. Dumping.\n", __LINE__) ;
  205. puts (buffer) ;
  206. exit (1) ;
  207. } ;
  208. channel_count = 0 ;
  209. cptr = strstr (buffer, "Channels") ;
  210. if (cptr && sscanf (cptr, "Channels : %d", &k) == 1)
  211. channel_count = k ;
  212. else if (cptr && sscanf (cptr, "Channels / frame : %d", &k) == 1)
  213. channel_count = k ;
  214. else
  215. { printf ("\n\nLine %d: Couldn't find channel count.\n", __LINE__) ;
  216. exit (1) ;
  217. } ;
  218. if (channel_count != 4)
  219. { printf ("\n\nLine %d: Wrong channel count (4 ->%d).\n", __LINE__, channel_count) ;
  220. exit (1) ;
  221. } ;
  222. if (! (cptr = strstr (buffer, "Ch Position Value")))
  223. { printf ("\n\nLine %d: Can't find PEAK data.\n", __LINE__) ;
  224. exit (1) ;
  225. } ;
  226. for (k = 0 ; k < channel_count ; k++)
  227. { if (! (cptr = strchr (cptr, '\n')))
  228. { printf ("\n\nLine %d: Got lost.\n", __LINE__) ;
  229. exit (1) ;
  230. } ;
  231. if (sscanf (cptr, "%d %d %f", &chan, &position, &value) != 3)
  232. { printf ("\n\nLine %d: sscanf failed.\n", __LINE__) ;
  233. exit (1) ;
  234. } ;
  235. if (position == 0)
  236. { printf ("\n\nLine %d: peak position for channel %d should not be at offset 0.\n", __LINE__, chan) ;
  237. printf ("%s", buffer) ;
  238. exit (1) ;
  239. } ;
  240. if (chan != k || fabs ((position) * 0.01 - value) > 1e-6)
  241. { printf ("\n\nLine %d: Error : peak value incorrect!\n", __LINE__) ;
  242. printf ("%s", buffer) ;
  243. printf ("\n\nLine %d: %d %f %f\n", __LINE__, chan, position * 0.01, value) ;
  244. exit (1) ;
  245. } ;
  246. cptr ++ ; /* Move past current newline. */
  247. } ;
  248. } /* check_logged_peaks */
  249. static void
  250. read_write_peak_test (const char *filename, int filetype)
  251. { SNDFILE *file ;
  252. SF_INFO sfinfo ;
  253. double small_data [10], max_peak = 0.0 ;
  254. unsigned k ;
  255. print_test_name (__func__, filename) ;
  256. for (k = 0 ; k < ARRAY_LEN (small_data) ; k ++)
  257. small_data [k] = 0.1 ;
  258. sfinfo.samplerate = 44100 ;
  259. sfinfo.channels = 2 ;
  260. sfinfo.format = filetype ;
  261. sfinfo.frames = 0 ;
  262. /* Open the file, add peak chunk and write samples with value 0.1. */
  263. file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_FALSE, __LINE__) ;
  264. sf_command (file, SFC_SET_ADD_PEAK_CHUNK, NULL, SF_TRUE) ;
  265. test_write_double_or_die (file, 0, small_data, ARRAY_LEN (small_data), __LINE__) ;
  266. sf_close (file) ;
  267. /* Open the fiel RDWR, write sample valied 1.25. */
  268. file = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, SF_FALSE, __LINE__) ;
  269. for (k = 0 ; k < ARRAY_LEN (small_data) ; k ++)
  270. small_data [k] = 1.0 ;
  271. test_write_double_or_die (file, 0, small_data, ARRAY_LEN (small_data), __LINE__) ;
  272. sf_command (file, SFC_GET_SIGNAL_MAX, &max_peak, sizeof (max_peak)) ;
  273. sf_close (file) ;
  274. exit_if_true (max_peak < 0.1, "\n\nLine %d : max peak (%5.3f) should not be 0.1.\n\n", __LINE__, max_peak) ;
  275. /* Open the file and test the values written to the PEAK chunk. */
  276. file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_FALSE, __LINE__) ;
  277. exit_if_true (sfinfo.channels * sfinfo.frames != 2 * ARRAY_LEN (small_data),
  278. "Line %d : frame count is %" PRId64 ", should be %zd\n", __LINE__, sfinfo.frames, 2 * ARRAY_LEN (small_data)) ;
  279. sf_command (file, SFC_GET_SIGNAL_MAX, &max_peak, sizeof (double)) ;
  280. sf_close (file) ;
  281. exit_if_true (max_peak < 1.0, "\n\nLine %d : max peak (%5.3f) should be 1.0.\n\n", __LINE__, max_peak) ;
  282. unlink (filename) ;
  283. puts ("ok") ;
  284. } /* read_write_peak_test */