2
0

peak_chunk_test.c 11 KB

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