header_test.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  1. /*
  2. ** Copyright (C) 2001-2011 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 <sys/stat.h>
  23. #include <math.h>
  24. #if HAVE_UNISTD_H
  25. #include <unistd.h>
  26. #endif
  27. #if (HAVE_DECL_S_IRGRP == 0)
  28. #include <sf_unistd.h>
  29. #endif
  30. #if (defined (WIN32) || defined (_WIN32))
  31. #include <io.h>
  32. #include <direct.h>
  33. #endif
  34. #include <sndfile.h>
  35. #include "utils.h"
  36. #define BUFFER_LEN (1<<10)
  37. #define LOG_BUFFER_SIZE 1024
  38. static void update_header_test (const char *filename, int typemajor) ;
  39. static void update_seek_short_test (const char *filename, int filetype) ;
  40. static void update_seek_int_test (const char *filename, int filetype) ;
  41. static void update_seek_float_test (const char *filename, int filetype) ;
  42. static void update_seek_double_test (const char *filename, int filetype) ;
  43. static void extra_header_test (const char *filename, int filetype) ;
  44. static void header_shrink_test (const char *filename, int filetype) ;
  45. /* Force the start of this buffer to be double aligned. Sparc-solaris will
  46. ** choke if its not.
  47. */
  48. static int data_out [BUFFER_LEN] ;
  49. static int data_in [BUFFER_LEN] ;
  50. int
  51. main (int argc, char *argv [])
  52. { int do_all = 0 ;
  53. int test_count = 0 ;
  54. if (argc != 2)
  55. { printf ("Usage : %s <test>\n", argv [0]) ;
  56. printf (" Where <test> is one of the following:\n") ;
  57. printf (" wav - test WAV file peak chunk\n") ;
  58. printf (" aiff - test AIFF file PEAK chunk\n") ;
  59. printf (" all - perform all tests\n") ;
  60. exit (1) ;
  61. } ;
  62. do_all=!strcmp (argv [1], "all") ;
  63. if (do_all || ! strcmp (argv [1], "wav"))
  64. { update_header_test ("header.wav", SF_FORMAT_WAV) ;
  65. update_seek_short_test ("header_short.wav", SF_FORMAT_WAV) ;
  66. update_seek_int_test ("header_int.wav", SF_FORMAT_WAV) ;
  67. update_seek_float_test ("header_float.wav", SF_FORMAT_WAV) ;
  68. update_seek_double_test ("header_double.wav", SF_FORMAT_WAV) ;
  69. header_shrink_test ("header_shrink.wav", SF_FORMAT_WAV) ;
  70. extra_header_test ("extra.wav", SF_FORMAT_WAV) ;
  71. update_header_test ("header.wavex", SF_FORMAT_WAVEX) ;
  72. update_seek_short_test ("header_short.wavex", SF_FORMAT_WAVEX) ;
  73. update_seek_int_test ("header_int.wavex", SF_FORMAT_WAVEX) ;
  74. update_seek_float_test ("header_float.wavex", SF_FORMAT_WAVEX) ;
  75. update_seek_double_test ("header_double.wavex", SF_FORMAT_WAVEX) ;
  76. header_shrink_test ("header_shrink.wavex", SF_FORMAT_WAVEX) ;
  77. extra_header_test ("extra.wavex", SF_FORMAT_WAVEX) ;
  78. test_count++ ;
  79. } ;
  80. if (do_all || ! strcmp (argv [1], "aiff"))
  81. { update_header_test ("header.aiff", SF_FORMAT_AIFF) ;
  82. update_seek_short_test ("header_short.aiff", SF_FORMAT_AIFF) ;
  83. update_seek_int_test ("header_int.aiff", SF_FORMAT_AIFF) ;
  84. update_seek_float_test ("header_float.aiff", SF_FORMAT_AIFF) ;
  85. update_seek_double_test ("header_double.aiff", SF_FORMAT_AIFF) ;
  86. header_shrink_test ("header_shrink.wav", SF_FORMAT_AIFF) ;
  87. extra_header_test ("extra.aiff", SF_FORMAT_AIFF) ;
  88. test_count++ ;
  89. } ;
  90. if (do_all || ! strcmp (argv [1], "au"))
  91. { update_header_test ("header.au", SF_FORMAT_AU) ;
  92. update_seek_short_test ("header_short.au", SF_FORMAT_AU) ;
  93. update_seek_int_test ("header_int.au", SF_FORMAT_AU) ;
  94. update_seek_float_test ("header_float.au", SF_FORMAT_AU) ;
  95. update_seek_double_test ("header_double.au", SF_FORMAT_AU) ;
  96. test_count++ ;
  97. } ;
  98. if (do_all || ! strcmp (argv [1], "caf"))
  99. { update_header_test ("header.caf", SF_FORMAT_CAF) ;
  100. update_seek_short_test ("header_short.caf", SF_FORMAT_CAF) ;
  101. update_seek_int_test ("header_int.caf", SF_FORMAT_CAF) ;
  102. update_seek_float_test ("header_float.caf", SF_FORMAT_CAF) ;
  103. update_seek_double_test ("header_double.caf", SF_FORMAT_CAF) ;
  104. /* extra_header_test ("extra.caf", SF_FORMAT_CAF) ; */
  105. test_count++ ;
  106. } ;
  107. if (do_all || ! strcmp (argv [1], "nist"))
  108. { update_header_test ("header.nist", SF_FORMAT_NIST) ;
  109. update_seek_short_test ("header_short.nist", SF_FORMAT_NIST) ;
  110. update_seek_int_test ("header_int.nist", SF_FORMAT_NIST) ;
  111. test_count++ ;
  112. } ;
  113. if (do_all || ! strcmp (argv [1], "paf"))
  114. { update_header_test ("header.paf", SF_FORMAT_PAF) ;
  115. update_seek_short_test ("header_short.paf", SF_FORMAT_PAF) ;
  116. test_count++ ;
  117. } ;
  118. if (do_all || ! strcmp (argv [1], "ircam"))
  119. { update_header_test ("header.ircam", SF_FORMAT_IRCAM) ;
  120. update_seek_short_test ("header_short.ircam", SF_FORMAT_IRCAM) ;
  121. test_count++ ;
  122. } ;
  123. if (do_all || ! strcmp (argv [1], "w64"))
  124. { update_header_test ("header.w64", SF_FORMAT_W64) ;
  125. update_seek_short_test ("header_short.w64", SF_FORMAT_W64) ;
  126. update_seek_int_test ("header_int.w64", SF_FORMAT_W64) ;
  127. update_seek_float_test ("header_float.w64", SF_FORMAT_W64) ;
  128. update_seek_double_test ("header_double.w64", SF_FORMAT_W64) ;
  129. test_count++ ;
  130. } ;
  131. if (do_all || ! strcmp (argv [1], "rf64"))
  132. { update_header_test ("header.rf64", SF_FORMAT_RF64) ;
  133. update_seek_short_test ("header_short.rf64", SF_FORMAT_RF64) ;
  134. update_seek_int_test ("header_int.rf64", SF_FORMAT_RF64) ;
  135. update_seek_float_test ("header_float.rf64", SF_FORMAT_RF64) ;
  136. update_seek_double_test ("header_double.rf64", SF_FORMAT_RF64) ;
  137. test_count++ ;
  138. } ;
  139. if (do_all || ! strcmp (argv [1], "mat4"))
  140. { update_header_test ("header.mat4", SF_FORMAT_MAT4) ;
  141. update_seek_short_test ("header_short.mat4", SF_FORMAT_MAT4) ;
  142. update_seek_int_test ("header_int.mat4", SF_FORMAT_MAT4) ;
  143. update_seek_float_test ("header_float.mat4", SF_FORMAT_MAT4) ;
  144. update_seek_double_test ("header_double.mat4", SF_FORMAT_MAT4) ;
  145. test_count++ ;
  146. } ;
  147. if (do_all || ! strcmp (argv [1], "mat5"))
  148. { update_header_test ("header.mat5", SF_FORMAT_MAT5) ;
  149. update_seek_short_test ("header_short.mat5", SF_FORMAT_MAT5) ;
  150. update_seek_int_test ("header_int.mat5", SF_FORMAT_MAT5) ;
  151. update_seek_float_test ("header_float.mat5", SF_FORMAT_MAT5) ;
  152. update_seek_double_test ("header_double.mat5", SF_FORMAT_MAT5) ;
  153. test_count++ ;
  154. } ;
  155. if (do_all || ! strcmp (argv [1], "pvf"))
  156. { update_header_test ("header.pvf", SF_FORMAT_PVF) ;
  157. update_seek_short_test ("header_short.pvf", SF_FORMAT_PVF) ;
  158. test_count++ ;
  159. } ;
  160. if (do_all || ! strcmp (argv [1], "avr"))
  161. { update_header_test ("header.avr", SF_FORMAT_AVR) ;
  162. update_seek_short_test ("header_short.avr", SF_FORMAT_AVR) ;
  163. test_count++ ;
  164. } ;
  165. if (do_all || ! strcmp (argv [1], "htk"))
  166. { update_header_test ("header.htk", SF_FORMAT_HTK) ;
  167. update_seek_short_test ("header_short.htk", SF_FORMAT_HTK) ;
  168. test_count++ ;
  169. } ;
  170. if (do_all || ! strcmp (argv [1], "svx"))
  171. { update_header_test ("header.svx", SF_FORMAT_SVX) ;
  172. update_seek_short_test ("header_short.svx", SF_FORMAT_SVX) ;
  173. test_count++ ;
  174. } ;
  175. if (do_all || ! strcmp (argv [1], "voc"))
  176. { update_header_test ("header.voc", SF_FORMAT_VOC) ;
  177. /*-update_seek_short_test ("header_short.voc", SF_FORMAT_VOC) ;-*/
  178. test_count++ ;
  179. } ;
  180. if (do_all || ! strcmp (argv [1], "sds"))
  181. { update_header_test ("header.sds", SF_FORMAT_SDS) ;
  182. /*-update_seek_short_test ("header_short.sds", SF_FORMAT_SDS) ;-*/
  183. test_count++ ;
  184. } ;
  185. if (do_all || ! strcmp (argv [1], "mpc2k"))
  186. { update_header_test ("header.mpc", SF_FORMAT_MPC2K) ;
  187. update_seek_short_test ("header_short.mpc", SF_FORMAT_MPC2K) ;
  188. test_count++ ;
  189. } ;
  190. if (test_count == 0)
  191. { printf ("Mono : ************************************\n") ;
  192. printf ("Mono : * No '%s' test defined.\n", argv [1]) ;
  193. printf ("Mono : ************************************\n") ;
  194. return 1 ;
  195. } ;
  196. return 0 ;
  197. } /* main */
  198. /*============================================================================================
  199. ** Here are the test functions.
  200. */
  201. static void
  202. update_header_sub (const char *filename, int typemajor, int write_mode)
  203. { SNDFILE *outfile, *infile ;
  204. SF_INFO sfinfo ;
  205. int k ;
  206. sfinfo.samplerate = 44100 ;
  207. sfinfo.format = (typemajor | SF_FORMAT_PCM_16) ;
  208. sfinfo.channels = 1 ;
  209. sfinfo.frames = 0 ;
  210. outfile = test_open_file_or_die (filename, write_mode, &sfinfo, SF_TRUE, __LINE__) ;
  211. for (k = 0 ; k < BUFFER_LEN ; k++)
  212. data_out [k] = k + 1 ;
  213. test_write_int_or_die (outfile, 0, data_out, BUFFER_LEN, __LINE__) ;
  214. if (typemajor != SF_FORMAT_HTK)
  215. { /* The HTK header is not correct when the file is first written. */
  216. infile = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
  217. sf_close (infile) ;
  218. } ;
  219. sf_command (outfile, SFC_UPDATE_HEADER_NOW, NULL, 0) ;
  220. /*
  221. ** Open file and check log buffer for an error. If header update failed
  222. ** the the log buffer will contain errors.
  223. */
  224. infile = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
  225. check_log_buffer_or_die (infile, __LINE__) ;
  226. if (sfinfo.frames < BUFFER_LEN || sfinfo.frames > BUFFER_LEN + 50)
  227. { printf ("\n\nLine %d : Incorrect sample count (%ld should be %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), BUFFER_LEN) ;
  228. dump_log_buffer (infile) ;
  229. exit (1) ;
  230. } ;
  231. test_read_int_or_die (infile, 0, data_in, BUFFER_LEN, __LINE__) ;
  232. for (k = 0 ; k < BUFFER_LEN ; k++)
  233. if (data_out [k] != k + 1)
  234. printf ("Error : line %d\n", __LINE__) ;
  235. sf_close (infile) ;
  236. /* Set auto update on. */
  237. sf_command (outfile, SFC_SET_UPDATE_HEADER_AUTO, NULL, SF_TRUE) ;
  238. /* Write more data_out. */
  239. for (k = 0 ; k < BUFFER_LEN ; k++)
  240. data_out [k] = k + 2 ;
  241. test_write_int_or_die (outfile, 0, data_out, BUFFER_LEN, __LINE__) ;
  242. /* Open file again and make sure no errors in log buffer. */
  243. infile = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
  244. check_log_buffer_or_die (infile, __LINE__) ;
  245. if (sfinfo.frames < 2 * BUFFER_LEN || sfinfo.frames > 2 * BUFFER_LEN + 50)
  246. { printf ("\n\nLine %d : Incorrect sample count (%ld should be %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), 2 * BUFFER_LEN) ;
  247. dump_log_buffer (infile) ;
  248. exit (1) ;
  249. } ;
  250. sf_close (infile) ;
  251. sf_close (outfile) ;
  252. unlink (filename) ;
  253. } /* update_header_sub */
  254. static void
  255. update_header_test (const char *filename, int typemajor)
  256. {
  257. print_test_name ("update_header_test", filename) ;
  258. update_header_sub (filename, typemajor, SFM_WRITE) ;
  259. update_header_sub (filename, typemajor, SFM_RDWR) ;
  260. unlink (filename) ;
  261. puts ("ok") ;
  262. } /* update_header_test */
  263. /*==============================================================================
  264. */
  265. static void
  266. update_seek_short_test (const char *filename, int filetype)
  267. { SNDFILE *outfile, *infile ;
  268. SF_INFO sfinfo ;
  269. sf_count_t frames ;
  270. short buffer [8] ;
  271. int k ;
  272. print_test_name ("update_seek_short_test", filename) ;
  273. memset (buffer, 0, sizeof (buffer)) ;
  274. /* Create sound outfile with no data. */
  275. sfinfo.format = filetype | SF_FORMAT_PCM_16 ;
  276. sfinfo.samplerate = 48000 ;
  277. sfinfo.channels = 2 ;
  278. if (sf_format_check (&sfinfo) == SF_FALSE)
  279. sfinfo.channels = 1 ;
  280. outfile = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
  281. sf_close (outfile) ;
  282. /* Open again for read/write. */
  283. outfile = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, SF_TRUE, __LINE__) ;
  284. /*
  285. ** In auto header update mode, seeking to the end of the file with
  286. ** SEEK_SET will fail from the 2nd seek on. seeking to 0, SEEK_END
  287. ** will seek to 0 anyway
  288. */
  289. if (sf_command (outfile, SFC_SET_UPDATE_HEADER_AUTO, NULL, SF_TRUE) == 0)
  290. { printf ("\n\nError : sf_command (SFC_SET_UPDATE_HEADER_AUTO) return error : %s\n\n", sf_strerror (outfile)) ;
  291. exit (1) ;
  292. } ;
  293. /* Now write some frames. */
  294. frames = ARRAY_LEN (buffer) / sfinfo.channels ;
  295. for (k = 0 ; k < 6 ; k++)
  296. { test_seek_or_die (outfile, k * frames, SEEK_SET, k * frames, sfinfo.channels, __LINE__) ;
  297. test_seek_or_die (outfile, 0, SEEK_END, k * frames, sfinfo.channels, __LINE__) ;
  298. /* Open file again and make sure no errors in log buffer. */
  299. infile = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
  300. check_log_buffer_or_die (infile, __LINE__) ;
  301. sf_close (infile) ;
  302. if (sfinfo.frames != k * frames)
  303. { printf ("\n\nLine %d : Incorrect sample count (%ld should be %ld)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), SF_COUNT_TO_LONG (k + frames)) ;
  304. dump_log_buffer (infile) ;
  305. exit (1) ;
  306. } ;
  307. if ((k & 1) == 0)
  308. test_write_short_or_die (outfile, k, buffer, sfinfo.channels * frames, __LINE__) ;
  309. else
  310. test_writef_short_or_die (outfile, k, buffer, frames, __LINE__) ;
  311. } ;
  312. sf_close (outfile) ;
  313. unlink (filename) ;
  314. puts ("ok") ;
  315. return ;
  316. } /* update_seek_short_test */
  317. static void
  318. update_seek_int_test (const char *filename, int filetype)
  319. { SNDFILE *outfile, *infile ;
  320. SF_INFO sfinfo ;
  321. sf_count_t frames ;
  322. int buffer [8] ;
  323. int k ;
  324. print_test_name ("update_seek_int_test", filename) ;
  325. memset (buffer, 0, sizeof (buffer)) ;
  326. /* Create sound outfile with no data. */
  327. sfinfo.format = filetype | SF_FORMAT_PCM_32 ;
  328. sfinfo.samplerate = 48000 ;
  329. sfinfo.channels = 2 ;
  330. if (sf_format_check (&sfinfo) == SF_FALSE)
  331. sfinfo.channels = 1 ;
  332. outfile = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
  333. sf_close (outfile) ;
  334. /* Open again for read/write. */
  335. outfile = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, SF_TRUE, __LINE__) ;
  336. /*
  337. ** In auto header update mode, seeking to the end of the file with
  338. ** SEEK_SET will fail from the 2nd seek on. seeking to 0, SEEK_END
  339. ** will seek to 0 anyway
  340. */
  341. if (sf_command (outfile, SFC_SET_UPDATE_HEADER_AUTO, NULL, SF_TRUE) == 0)
  342. { printf ("\n\nError : sf_command (SFC_SET_UPDATE_HEADER_AUTO) return error : %s\n\n", sf_strerror (outfile)) ;
  343. exit (1) ;
  344. } ;
  345. /* Now write some frames. */
  346. frames = ARRAY_LEN (buffer) / sfinfo.channels ;
  347. for (k = 0 ; k < 6 ; k++)
  348. { test_seek_or_die (outfile, k * frames, SEEK_SET, k * frames, sfinfo.channels, __LINE__) ;
  349. test_seek_or_die (outfile, 0, SEEK_END, k * frames, sfinfo.channels, __LINE__) ;
  350. /* Open file again and make sure no errors in log buffer. */
  351. infile = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
  352. check_log_buffer_or_die (infile, __LINE__) ;
  353. sf_close (infile) ;
  354. if (sfinfo.frames != k * frames)
  355. { printf ("\n\nLine %d : Incorrect sample count (%ld should be %ld)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), SF_COUNT_TO_LONG (k + frames)) ;
  356. dump_log_buffer (infile) ;
  357. exit (1) ;
  358. } ;
  359. if ((k & 1) == 0)
  360. test_write_int_or_die (outfile, k, buffer, sfinfo.channels * frames, __LINE__) ;
  361. else
  362. test_writef_int_or_die (outfile, k, buffer, frames, __LINE__) ;
  363. } ;
  364. sf_close (outfile) ;
  365. unlink (filename) ;
  366. puts ("ok") ;
  367. return ;
  368. } /* update_seek_int_test */
  369. static void
  370. update_seek_float_test (const char *filename, int filetype)
  371. { SNDFILE *outfile, *infile ;
  372. SF_INFO sfinfo ;
  373. sf_count_t frames ;
  374. float buffer [8] ;
  375. int k ;
  376. print_test_name ("update_seek_float_test", filename) ;
  377. memset (buffer, 0, sizeof (buffer)) ;
  378. /* Create sound outfile with no data. */
  379. sfinfo.format = filetype | SF_FORMAT_FLOAT ;
  380. sfinfo.samplerate = 48000 ;
  381. sfinfo.channels = 2 ;
  382. if (sf_format_check (&sfinfo) == SF_FALSE)
  383. sfinfo.channels = 1 ;
  384. outfile = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
  385. sf_close (outfile) ;
  386. /* Open again for read/write. */
  387. outfile = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, SF_TRUE, __LINE__) ;
  388. /*
  389. ** In auto header update mode, seeking to the end of the file with
  390. ** SEEK_SET will fail from the 2nd seek on. seeking to 0, SEEK_END
  391. ** will seek to 0 anyway
  392. */
  393. if (sf_command (outfile, SFC_SET_UPDATE_HEADER_AUTO, NULL, SF_TRUE) == 0)
  394. { printf ("\n\nError : sf_command (SFC_SET_UPDATE_HEADER_AUTO) return error : %s\n\n", sf_strerror (outfile)) ;
  395. exit (1) ;
  396. } ;
  397. /* Now write some frames. */
  398. frames = ARRAY_LEN (buffer) / sfinfo.channels ;
  399. for (k = 0 ; k < 6 ; k++)
  400. { test_seek_or_die (outfile, k * frames, SEEK_SET, k * frames, sfinfo.channels, __LINE__) ;
  401. test_seek_or_die (outfile, 0, SEEK_END, k * frames, sfinfo.channels, __LINE__) ;
  402. /* Open file again and make sure no errors in log buffer. */
  403. infile = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
  404. check_log_buffer_or_die (infile, __LINE__) ;
  405. sf_close (infile) ;
  406. if (sfinfo.frames != k * frames)
  407. { printf ("\n\nLine %d : Incorrect sample count (%ld should be %ld)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), SF_COUNT_TO_LONG (k + frames)) ;
  408. dump_log_buffer (infile) ;
  409. exit (1) ;
  410. } ;
  411. if ((k & 1) == 0)
  412. test_write_float_or_die (outfile, k, buffer, sfinfo.channels * frames, __LINE__) ;
  413. else
  414. test_writef_float_or_die (outfile, k, buffer, frames, __LINE__) ;
  415. } ;
  416. sf_close (outfile) ;
  417. unlink (filename) ;
  418. puts ("ok") ;
  419. return ;
  420. } /* update_seek_float_test */
  421. static void
  422. update_seek_double_test (const char *filename, int filetype)
  423. { SNDFILE *outfile, *infile ;
  424. SF_INFO sfinfo ;
  425. sf_count_t frames ;
  426. double buffer [8] ;
  427. int k ;
  428. print_test_name ("update_seek_double_test", filename) ;
  429. memset (buffer, 0, sizeof (buffer)) ;
  430. /* Create sound outfile with no data. */
  431. sfinfo.format = filetype | SF_FORMAT_DOUBLE ;
  432. sfinfo.samplerate = 48000 ;
  433. sfinfo.channels = 2 ;
  434. if (sf_format_check (&sfinfo) == SF_FALSE)
  435. sfinfo.channels = 1 ;
  436. outfile = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
  437. sf_close (outfile) ;
  438. /* Open again for read/write. */
  439. outfile = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, SF_TRUE, __LINE__) ;
  440. /*
  441. ** In auto header update mode, seeking to the end of the file with
  442. ** SEEK_SET will fail from the 2nd seek on. seeking to 0, SEEK_END
  443. ** will seek to 0 anyway
  444. */
  445. if (sf_command (outfile, SFC_SET_UPDATE_HEADER_AUTO, NULL, SF_TRUE) == 0)
  446. { printf ("\n\nError : sf_command (SFC_SET_UPDATE_HEADER_AUTO) return error : %s\n\n", sf_strerror (outfile)) ;
  447. exit (1) ;
  448. } ;
  449. /* Now write some frames. */
  450. frames = ARRAY_LEN (buffer) / sfinfo.channels ;
  451. for (k = 0 ; k < 6 ; k++)
  452. { test_seek_or_die (outfile, k * frames, SEEK_SET, k * frames, sfinfo.channels, __LINE__) ;
  453. test_seek_or_die (outfile, 0, SEEK_END, k * frames, sfinfo.channels, __LINE__) ;
  454. /* Open file again and make sure no errors in log buffer. */
  455. infile = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
  456. check_log_buffer_or_die (infile, __LINE__) ;
  457. sf_close (infile) ;
  458. if (sfinfo.frames != k * frames)
  459. { printf ("\n\nLine %d : Incorrect sample count (%ld should be %ld)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), SF_COUNT_TO_LONG (k + frames)) ;
  460. dump_log_buffer (infile) ;
  461. exit (1) ;
  462. } ;
  463. if ((k & 1) == 0)
  464. test_write_double_or_die (outfile, k, buffer, sfinfo.channels * frames, __LINE__) ;
  465. else
  466. test_writef_double_or_die (outfile, k, buffer, frames, __LINE__) ;
  467. } ;
  468. sf_close (outfile) ;
  469. unlink (filename) ;
  470. puts ("ok") ;
  471. return ;
  472. } /* update_seek_double_test */
  473. static void
  474. header_shrink_test (const char *filename, int filetype)
  475. { SNDFILE *outfile, *infile ;
  476. SF_INFO sfinfo ;
  477. sf_count_t frames ;
  478. float buffer [8], bufferin [8] ;
  479. print_test_name ("header_shrink_test", filename) ;
  480. memset (&sfinfo, 0, sizeof (sfinfo)) ;
  481. sfinfo.samplerate = 44100 ;
  482. sfinfo.format = filetype | SF_FORMAT_FLOAT ;
  483. sfinfo.channels = 1 ;
  484. memset (buffer, 0xA0, sizeof (buffer)) ;
  485. /* Now write some frames. */
  486. frames = ARRAY_LEN (buffer) / sfinfo.channels ;
  487. /* Test the file with extra header data. */
  488. outfile = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_FALSE, __LINE__) ;
  489. sf_command (outfile, SFC_SET_ADD_PEAK_CHUNK, NULL, SF_TRUE) ;
  490. sf_command (outfile, SFC_UPDATE_HEADER_NOW, NULL, SF_FALSE) ;
  491. sf_command (outfile, SFC_SET_ADD_PEAK_CHUNK, NULL, SF_FALSE) ;
  492. test_writef_float_or_die (outfile, 0, buffer, frames, __LINE__) ;
  493. sf_close (outfile) ;
  494. /* Open again for read. */
  495. infile = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_FALSE, __LINE__) ;
  496. test_readf_float_or_die (infile, 0, bufferin, frames, __LINE__) ;
  497. sf_close (infile) ;
  498. compare_float_or_die (buffer, bufferin, frames, __LINE__) ;
  499. unlink (filename) ;
  500. puts ("ok") ;
  501. return ;
  502. } /* header_shrink_test */
  503. static void
  504. extra_header_test (const char *filename, int filetype)
  505. { SNDFILE *outfile, *infile ;
  506. SF_INFO sfinfo ;
  507. sf_count_t frames ;
  508. short buffer [8] ;
  509. int k = 0 ;
  510. print_test_name ("extra_header_test", filename) ;
  511. sfinfo.samplerate = 44100 ;
  512. sfinfo.format = (filetype | SF_FORMAT_PCM_16) ;
  513. sfinfo.channels = 1 ;
  514. memset (buffer, 0xA0, sizeof (buffer)) ;
  515. /* Now write some frames. */
  516. frames = ARRAY_LEN (buffer) / sfinfo.channels ;
  517. /* Test the file with extra header data. */
  518. outfile = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, 462) ;
  519. sf_set_string (outfile, SF_STR_TITLE, filename) ;
  520. test_writef_short_or_die (outfile, k, buffer, frames, 464) ;
  521. sf_set_string (outfile, SF_STR_COPYRIGHT, "(c) 1980 Erik") ;
  522. sf_close (outfile) ;
  523. #if 1
  524. /*
  525. ** Erik de Castro Lopo <erikd@mega-nerd.com> May 23 2004.
  526. **
  527. ** This file has extra string data in the header and therefore cannot
  528. ** currently be opened in SFM_RDWR mode. This is fixable, but its in
  529. ** a part of the code I don't want to fiddle with until the Ogg/Vorbis
  530. ** integration is done.
  531. */
  532. if ((infile = sf_open (filename, SFM_RDWR, &sfinfo)) != NULL)
  533. { printf ("\n\nError : should not be able to open this file in SFM_RDWR.\n\n") ;
  534. exit (1) ;
  535. } ;
  536. unlink (filename) ;
  537. puts ("ok") ;
  538. return ;
  539. #else
  540. hexdump_file (filename, 0, 100000) ;
  541. /* Open again for read/write. */
  542. outfile = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, 491) ;
  543. /*
  544. ** In auto header update mode, seeking to the end of the file with
  545. ** SEEK_SET will fail from the 2nd seek on. seeking to 0, SEEK_END
  546. ** will seek to 0 anyway
  547. */
  548. if (sf_command (outfile, SFC_SET_UPDATE_HEADER_AUTO, NULL, SF_TRUE) == 0)
  549. { printf ("\n\nError : sf_command (SFC_SET_UPDATE_HEADER_AUTO) return error : %s\n\n", sf_strerror (outfile)) ;
  550. exit (1) ;
  551. } ;
  552. /* Now write some frames. */
  553. frames = ARRAY_LEN (buffer) / sfinfo.channels ;
  554. for (k = 1 ; k < 6 ; k++)
  555. {
  556. printf ("\n*** pass %d\n", k) ;
  557. memset (buffer, 0xA0 + k, sizeof (buffer)) ;
  558. test_seek_or_die (outfile, k * frames, SEEK_SET, k * frames, sfinfo.channels, 512) ;
  559. test_seek_or_die (outfile, 0, SEEK_END, k * frames, sfinfo.channels, 513) ;
  560. /* Open file again and make sure no errors in log buffer. */
  561. if (0)
  562. { infile = test_open_file_or_die (filename, SFM_READ, &sfinfo, 517) ;
  563. check_log_buffer_or_die (infile, 518) ;
  564. sf_close (infile) ;
  565. } ;
  566. if (sfinfo.frames != k * frames)
  567. { printf ("\n\nLine %d : Incorrect sample count (%ld should be %ld)\n", 523, SF_COUNT_TO_LONG (sfinfo.frames), SF_COUNT_TO_LONG (k + frames)) ;
  568. dump_log_buffer (infile) ;
  569. exit (1) ;
  570. } ;
  571. if ((k & 1) == 0)
  572. test_write_short_or_die (outfile, k, buffer, sfinfo.channels * frames, 529) ;
  573. else
  574. test_writef_short_or_die (outfile, k, buffer, frames, 531) ;
  575. hexdump_file (filename, 0, 100000) ;
  576. } ;
  577. sf_close (outfile) ;
  578. unlink (filename) ;
  579. puts ("ok") ;
  580. return ;
  581. #endif
  582. } /* extra_header_test */