utils.tpl 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897
  1. [+ AutoGen5 template h c +]
  2. /*
  3. ** Copyright (C) 2002-2012 Erik de Castro Lopo <erikd@mega-nerd.com>
  4. **
  5. ** This program is free software; you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation; either version 2 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program; if not, write to the Free Software
  17. ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. */
  19. /*
  20. ** Utility functions to make writing the test suite easier.
  21. **
  22. ** The .c and .h files were generated automagically with Autogen from
  23. ** the files utils.def and utils.tpl.
  24. */
  25. [+ CASE (suffix) +]
  26. [+ == h +]
  27. #ifdef __cplusplus
  28. extern "C" {
  29. #endif /* __cplusplus */
  30. #include <stdint.h>
  31. #include <stdarg.h>
  32. #define ARRAY_LEN(x) ((int) (sizeof (x)) / (sizeof ((x) [0])))
  33. #define SIGNED_SIZEOF(x) ((int64_t) (sizeof (x)))
  34. #define NOT(x) (! (x))
  35. #define PIPE_INDEX(x) ((x) + 500)
  36. #define PIPE_TEST_LEN 12345
  37. [+ FOR float_type
  38. +]void gen_windowed_sine_[+ (get "name") +] ([+ (get "name") +] *data, int len, double maximum) ;
  39. [+ ENDFOR float_type
  40. +]
  41. void create_short_sndfile (const char *filename, int format, int channels) ;
  42. void check_file_hash_or_die (const char *filename, uint64_t target_hash, int line_num) ;
  43. void print_test_name (const char *test, const char *filename) ;
  44. void dump_data_to_file (const char *filename, const void *data, unsigned int datalen) ;
  45. void write_mono_file (const char * filename, int format, int srate, float * output, int len) ;
  46. #ifdef __GNUC__
  47. static inline void
  48. exit_if_true (int test, const char *format, ...)
  49. #ifdef __USE_MINGW_ANSI_STDIO
  50. __attribute__ ((format (gnu_printf, 2, 3))) ;
  51. #else
  52. __attribute__ ((format (printf, 2, 3))) ;
  53. #endif
  54. #endif
  55. static inline void
  56. exit_if_true (int test, const char *format, ...)
  57. { if (test)
  58. { va_list argptr ;
  59. va_start (argptr, format) ;
  60. vprintf (format, argptr) ;
  61. va_end (argptr) ;
  62. exit (1) ;
  63. } ;
  64. } /* exit_if_true */
  65. /*
  66. ** Functions for saving two vectors of data in an ascii text file which
  67. ** can then be loaded into GNU octave for comparison.
  68. */
  69. [+ FOR io_type
  70. +]int oct_save_[+ (get "io_element") +] (const [+ (get "io_element") +] *a, const [+ (get "io_element") +] *b, int len) ;
  71. [+ ENDFOR io_type
  72. +]
  73. void delete_file (int format, const char *filename) ;
  74. void count_open_files (void) ;
  75. void increment_open_file_count (void) ;
  76. void check_open_file_count_or_die (int lineno) ;
  77. #ifdef SNDFILE_H
  78. static inline void
  79. sf_info_clear (SF_INFO * info)
  80. { memset (info, 0, sizeof (SF_INFO)) ;
  81. } /* sf_info_clear */
  82. static inline void
  83. sf_info_setup (SF_INFO * info, int format, int samplerate, int channels)
  84. { sf_info_clear (info) ;
  85. info->format = format ;
  86. info->samplerate = samplerate ;
  87. info->channels = channels ;
  88. } /* sf_info_setup */
  89. void dump_log_buffer (SNDFILE *file) ;
  90. void check_log_buffer_or_die (SNDFILE *file, int line_num) ;
  91. int string_in_log_buffer (SNDFILE *file, const char *s) ;
  92. void hexdump_file (const char * filename, sf_count_t offset, sf_count_t length) ;
  93. SNDFILE *test_open_file_or_die
  94. (const char *filename, int mode, SF_INFO *sfinfo, int allow_fd, int line_num) ;
  95. void test_read_write_position_or_die
  96. (SNDFILE *file, int line_num, int pass, sf_count_t read_pos, sf_count_t write_pos) ;
  97. void test_seek_or_die
  98. (SNDFILE *file, sf_count_t offset, int whence, sf_count_t new_pos, int channels, int line_num) ;
  99. [+ FOR read_op +]
  100. [+ FOR io_type
  101. +]void test_[+ (get "op_element") +]_[+ (get "io_element") +]_or_die
  102. (SNDFILE *file, int pass, [+ (get "io_element") +] *test, sf_count_t [+ (get "count_name") +], int line_num) ;
  103. [+ ENDFOR io_type +][+ ENDFOR read_op +]
  104. void
  105. test_read_raw_or_die (SNDFILE *file, int pass, void *test, sf_count_t items, int line_num) ;
  106. [+ FOR write_op +]
  107. [+ FOR io_type
  108. +]void test_[+ (get "op_element") +]_[+ (get "io_element") +]_or_die
  109. (SNDFILE *file, int pass, const [+ (get "io_element") +] *test, sf_count_t [+ (get "count_name") +], int line_num) ;
  110. [+ ENDFOR io_type +][+ ENDFOR write_op +]
  111. void
  112. test_write_raw_or_die (SNDFILE *file, int pass, const void *test, sf_count_t items, int line_num) ;
  113. [+ FOR io_type
  114. +]void compare_[+ (get "io_element") +]_or_die (const [+ (get "io_element") +] *expected, const [+ (get "io_element") +] *actual, unsigned count, int line_num) ;
  115. [+ ENDFOR io_type +]
  116. void gen_lowpass_signal_float (float *data, int len) ;
  117. sf_count_t file_length (const char * fname) ;
  118. sf_count_t file_length_fd (int fd) ;
  119. #endif
  120. #ifdef __cplusplus
  121. } /* extern "C" */
  122. #endif /* __cplusplus */
  123. [+ == c +]
  124. #include "sfconfig.h"
  125. #include <stdio.h>
  126. #include <stdlib.h>
  127. #include <inttypes.h>
  128. #if HAVE_UNISTD_H
  129. #include <unistd.h>
  130. #endif
  131. #if (HAVE_DECL_S_IRGRP == 0)
  132. #include <sf_unistd.h>
  133. #endif
  134. #include <errno.h>
  135. #include <string.h>
  136. #include <ctype.h>
  137. #include <math.h>
  138. #include <fcntl.h>
  139. #include <sys/stat.h>
  140. #include <sndfile.h>
  141. #include "utils.h"
  142. #ifndef M_PI
  143. #define M_PI 3.14159265358979323846264338
  144. #endif
  145. #define LOG_BUFFER_SIZE 2048
  146. /*
  147. ** Neat solution to the Win32/OS2 binary file flage requirement.
  148. ** If O_BINARY isn't already defined by the inclusion of the system
  149. ** headers, set it to zero.
  150. */
  151. #ifndef O_BINARY
  152. #define O_BINARY 0
  153. #endif
  154. [+ FOR float_type +]
  155. void
  156. gen_windowed_sine_[+ (get "name") +] ([+ (get "name") +] *data, int len, double maximum)
  157. { int k ;
  158. memset (data, 0, len * sizeof ([+ (get "name") +])) ;
  159. /*
  160. ** Choose a frequency of 1/32 so that it aligns perfectly with a DFT
  161. ** bucket to minimise spreading of energy over more than one bucket.
  162. ** Also do not want to make the frequency too high as some of the
  163. ** codecs (ie gsm610) have a quite severe high frequency roll off.
  164. */
  165. len /= 2 ;
  166. for (k = 0 ; k < len ; k++)
  167. { data [k] = sin (2.0 * k * M_PI * 1.0 / 32.0 + 0.4) ;
  168. /* Apply Hanning Window. */
  169. data [k] *= maximum * (0.5 - 0.5 * cos (2.0 * M_PI * k / ((len) - 1))) ;
  170. }
  171. return ;
  172. } /* gen_windowed_sine_[+ (get "name") +] */
  173. [+ ENDFOR float_type +]
  174. void
  175. create_short_sndfile (const char *filename, int format, int channels)
  176. { short data [2 * 3 * 4 * 5 * 6 * 7] = { 0, } ;
  177. SNDFILE *file ;
  178. SF_INFO sfinfo ;
  179. sfinfo.samplerate = 44100 ;
  180. sfinfo.channels = channels ;
  181. sfinfo.format = format ;
  182. if ((file = sf_open (filename, SFM_WRITE, &sfinfo)) == NULL)
  183. { printf ("Error (%s, %d) : sf_open failed : %s\n", __FILE__, __LINE__, sf_strerror (file)) ;
  184. exit (1) ;
  185. } ;
  186. sf_write_short (file, data, ARRAY_LEN (data)) ;
  187. sf_close (file) ;
  188. } /* create_short_sndfile */
  189. void
  190. check_file_hash_or_die (const char *filename, uint64_t target_hash, int line_num)
  191. { static unsigned char buf [4096] ;
  192. uint64_t cksum ;
  193. FILE *file ;
  194. int k, read_count ;
  195. memset (buf, 0, sizeof (buf)) ;
  196. /* The 'b' in the mode string means binary for Win32. */
  197. if ((file = fopen (filename, "rb")) == NULL)
  198. { printf ("\n\nLine %d: could not open file '%s'\n\n", line_num, filename) ;
  199. exit (1) ;
  200. } ;
  201. cksum = 0 ;
  202. while ((read_count = fread (buf, 1, sizeof (buf), file)))
  203. for (k = 0 ; k < read_count ; k++)
  204. cksum = cksum * 511 + buf [k] ;
  205. fclose (file) ;
  206. if (target_hash == 0)
  207. { printf (" 0x%016" PRIx64 "\n", cksum) ;
  208. return ;
  209. } ;
  210. if (cksum != target_hash)
  211. { printf ("\n\nLine %d: incorrect hash value 0x%016" PRIx64 " should be 0x%016" PRIx64 ".\n\n", line_num, cksum, target_hash) ;
  212. exit (1) ;
  213. } ;
  214. return ;
  215. } /* check_file_hash_or_die */
  216. void
  217. print_test_name (const char *test, const char *filename)
  218. { int count ;
  219. if (test == NULL)
  220. { printf (__FILE__ ": bad test of filename parameter.\n") ;
  221. exit (1) ;
  222. } ;
  223. if (filename == NULL || strlen (filename) == 0)
  224. { printf (" %-30s : ", test) ;
  225. count = 25 ;
  226. }
  227. else
  228. { printf (" %-30s : %s ", test, filename) ;
  229. count = 24 - strlen (filename) ;
  230. } ;
  231. while (count -- > 0)
  232. putchar ('.') ;
  233. putchar (' ') ;
  234. fflush (stdout) ;
  235. } /* print_test_name */
  236. void
  237. dump_data_to_file (const char *filename, const void *data, unsigned int datalen)
  238. { FILE *file ;
  239. if ((file = fopen (filename, "wb")) == NULL)
  240. { printf ("\n\nLine %d : could not open file : %s\n\n", __LINE__, filename) ;
  241. exit (1) ;
  242. } ;
  243. if (fwrite (data, 1, datalen, file) != datalen)
  244. { printf ("\n\nLine %d : fwrite failed.\n\n", __LINE__) ;
  245. exit (1) ;
  246. } ;
  247. fclose (file) ;
  248. } /* dump_data_to_file */
  249. /*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  250. */
  251. static char octfilename [] = "error.dat" ;
  252. [+ FOR io_type
  253. +]int
  254. oct_save_[+ (get "io_element") +] (const [+ (get "io_element") +] *a, const [+ (get "io_element") +] *b, int len)
  255. { FILE *file ;
  256. int k ;
  257. if (! (file = fopen (octfilename, "w")))
  258. return 1 ;
  259. fprintf (file, "# Not created by Octave\n") ;
  260. fprintf (file, "# name: a\n") ;
  261. fprintf (file, "# type: matrix\n") ;
  262. fprintf (file, "# rows: %d\n", len) ;
  263. fprintf (file, "# columns: 1\n") ;
  264. for (k = 0 ; k < len ; k++)
  265. fprintf (file, [+ (get "format_str") +] "\n", a [k]) ;
  266. fprintf (file, "# name: b\n") ;
  267. fprintf (file, "# type: matrix\n") ;
  268. fprintf (file, "# rows: %d\n", len) ;
  269. fprintf (file, "# columns: 1\n") ;
  270. for (k = 0 ; k < len ; k++)
  271. fprintf (file, [+ (get "format_str") +] "\n", b [k]) ;
  272. fclose (file) ;
  273. return 0 ;
  274. } /* oct_save_[+ (get "io_element") +] */
  275. [+ ENDFOR io_type
  276. +]
  277. void
  278. check_log_buffer_or_die (SNDFILE *file, int line_num)
  279. { static char buffer [LOG_BUFFER_SIZE] ;
  280. int count ;
  281. memset (buffer, 0, sizeof (buffer)) ;
  282. /* Get the log buffer data. */
  283. count = sf_command (file, SFC_GET_LOG_INFO, buffer, LOG_BUFFER_SIZE) ;
  284. if (LOG_BUFFER_SIZE - count < 2)
  285. { printf ("\n\nLine %d : Possible long log buffer.\n", line_num) ;
  286. exit (1) ;
  287. }
  288. /* Look for "Should" */
  289. if (strstr (buffer, "ould"))
  290. { printf ("\n\nLine %d : Log buffer contains `ould'. Dumping.\n", line_num) ;
  291. puts (buffer) ;
  292. exit (1) ;
  293. } ;
  294. /* Look for "**" */
  295. if (strstr (buffer, "*"))
  296. { printf ("\n\nLine %d : Log buffer contains `*'. Dumping.\n", line_num) ;
  297. puts (buffer) ;
  298. exit (1) ;
  299. } ;
  300. /* Look for "Should" */
  301. if (strstr (buffer, "nknown marker"))
  302. { printf ("\n\nLine %d : Log buffer contains `nknown marker'. Dumping.\n", line_num) ;
  303. puts (buffer) ;
  304. exit (1) ;
  305. } ;
  306. return ;
  307. } /* check_log_buffer_or_die */
  308. int
  309. string_in_log_buffer (SNDFILE *file, const char *s)
  310. { static char buffer [LOG_BUFFER_SIZE] ;
  311. int count ;
  312. memset (buffer, 0, sizeof (buffer)) ;
  313. /* Get the log buffer data. */
  314. count = sf_command (file, SFC_GET_LOG_INFO, buffer, LOG_BUFFER_SIZE) ;
  315. if (LOG_BUFFER_SIZE - count < 2)
  316. { printf ("Possible long log buffer.\n") ;
  317. exit (1) ;
  318. }
  319. /* Look for string */
  320. return strstr (buffer, s) ? SF_TRUE : SF_FALSE ;
  321. } /* string_in_log_buffer */
  322. void
  323. hexdump_file (const char * filename, sf_count_t offset, sf_count_t length)
  324. {
  325. FILE * file ;
  326. char buffer [16] ;
  327. int k, m, ch, readcount ;
  328. if (length > 1000000)
  329. { printf ("\n\nError : length (%" PRId64 ") too long.\n\n", offset) ;
  330. exit (1) ;
  331. } ;
  332. if ((file = fopen (filename, "r")) == NULL)
  333. { printf ("\n\nError : hexdump_file (%s) could not open file for read.\n\n", filename) ;
  334. exit (1) ;
  335. } ;
  336. if (fseek (file, offset, SEEK_SET) != 0)
  337. { printf ("\n\nError : fseek(file, %" PRId64 ", SEEK_SET) failed : %s\n\n", offset, strerror (errno)) ;
  338. exit (1) ;
  339. } ;
  340. puts ("\n\n") ;
  341. for (k = 0 ; k < length ; k+= sizeof (buffer))
  342. { readcount = fread (buffer, 1, sizeof (buffer), file) ;
  343. printf ("%08" PRIx64 " : ", offset + k) ;
  344. for (m = 0 ; m < readcount ; m++)
  345. printf ("%02x ", buffer [m] & 0xFF) ;
  346. for (m = readcount ; m < SIGNED_SIZEOF (buffer) ; m++)
  347. printf (" ") ;
  348. printf (" ") ;
  349. for (m = 0 ; m < readcount ; m++)
  350. { ch = isprint (buffer [m]) ? buffer [m] : '.' ;
  351. putchar (ch) ;
  352. } ;
  353. if (readcount < SIGNED_SIZEOF (buffer))
  354. break ;
  355. putchar ('\n') ;
  356. } ;
  357. puts ("\n") ;
  358. fclose (file) ;
  359. } /* hexdump_file */
  360. void
  361. dump_log_buffer (SNDFILE *file)
  362. { static char buffer [LOG_BUFFER_SIZE] ;
  363. memset (buffer, 0, sizeof (buffer)) ;
  364. /* Get the log buffer data. */
  365. sf_command (file, SFC_GET_LOG_INFO, buffer, LOG_BUFFER_SIZE) ;
  366. if (strlen (buffer) < 1)
  367. puts ("Log buffer empty.\n") ;
  368. else
  369. puts (buffer) ;
  370. return ;
  371. } /* dump_log_buffer */
  372. SNDFILE *
  373. test_open_file_or_die (const char *filename, int mode, SF_INFO *sfinfo, int allow_fd, int line_num)
  374. { static int count = 0 ;
  375. SNDFILE *file ;
  376. const char *modestr, *func_name ;
  377. int oflags = 0, omode = 0, err ;
  378. /*
  379. ** Need to test both sf_open() and sf_open_fd().
  380. ** Do so alternately.
  381. */
  382. switch (mode)
  383. { case SFM_READ :
  384. modestr = "SFM_READ" ;
  385. oflags = O_RDONLY | O_BINARY ;
  386. omode = 0 ;
  387. break ;
  388. case SFM_WRITE :
  389. modestr = "SFM_WRITE" ;
  390. oflags = O_WRONLY | O_CREAT | O_TRUNC | O_BINARY ;
  391. omode = S_IRUSR | S_IWUSR | S_IRGRP ;
  392. break ;
  393. case SFM_RDWR :
  394. modestr = "SFM_RDWR" ;
  395. oflags = O_RDWR | O_CREAT | O_BINARY ;
  396. omode = S_IRUSR | S_IWUSR | S_IRGRP ;
  397. break ;
  398. default :
  399. printf ("\n\nLine %d: Bad mode.\n", line_num) ;
  400. fflush (stdout) ;
  401. exit (1) ;
  402. } ;
  403. if (OS_IS_WIN32)
  404. { /* Windows does not understand and ignores the S_IRGRP flag, but Wine
  405. ** gives a run time warning message, so just clear it.
  406. */
  407. omode &= ~S_IRGRP ;
  408. } ;
  409. if (allow_fd && ((++count) & 1) == 1)
  410. { int fd ;
  411. /* Only use the three argument open() function if omode != 0. */
  412. fd = (omode == 0) ? open (filename, oflags) : open (filename, oflags, omode) ;
  413. if (fd < 0)
  414. { printf ("\n\n%s : open failed : %s\n", __func__, strerror (errno)) ;
  415. exit (1) ;
  416. } ;
  417. func_name = "sf_open_fd" ;
  418. file = sf_open_fd (fd, mode, sfinfo, SF_TRUE) ;
  419. }
  420. else
  421. { func_name = "sf_open" ;
  422. file = sf_open (filename, mode, sfinfo) ;
  423. } ;
  424. if (file == NULL)
  425. { printf ("\n\nLine %d: %s (%s) failed : %s\n\n", line_num, func_name, modestr, sf_strerror (NULL)) ;
  426. dump_log_buffer (file) ;
  427. exit (1) ;
  428. } ;
  429. err = sf_error (file) ;
  430. if (err != SF_ERR_NO_ERROR)
  431. { printf ("\n\nLine %d : sf_error : %s\n\n", line_num, sf_error_number (err)) ;
  432. dump_log_buffer (file) ;
  433. exit (1) ;
  434. } ;
  435. return file ;
  436. } /* test_open_file_or_die */
  437. void
  438. test_read_write_position_or_die (SNDFILE *file, int line_num, int pass, sf_count_t read_pos, sf_count_t write_pos)
  439. { sf_count_t pos ;
  440. /* Check the current read position. */
  441. if (read_pos >= 0 && (pos = sf_seek (file, 0, SEEK_CUR | SFM_READ)) != read_pos)
  442. { printf ("\n\nLine %d ", line_num) ;
  443. if (pass > 0)
  444. printf ("(pass %d): ", pass) ;
  445. printf ("Read position (%" PRId64 ") should be %" PRId64 ".\n", pos, read_pos) ;
  446. exit (1) ;
  447. } ;
  448. /* Check the current write position. */
  449. if (write_pos >= 0 && (pos = sf_seek (file, 0, SEEK_CUR | SFM_WRITE)) != write_pos)
  450. { printf ("\n\nLine %d", line_num) ;
  451. if (pass > 0)
  452. printf (" (pass %d)", pass) ;
  453. printf (" : Write position (%" PRId64 ") should be %" PRId64 ".\n", pos, write_pos) ;
  454. exit (1) ;
  455. } ;
  456. return ;
  457. } /* test_read_write_position */
  458. void
  459. test_seek_or_die (SNDFILE *file, sf_count_t offset, int whence, sf_count_t new_pos, int channels, int line_num)
  460. { sf_count_t position ;
  461. const char *channel_name, *whence_name ;
  462. switch (whence)
  463. { case SEEK_SET :
  464. whence_name = "SEEK_SET" ;
  465. break ;
  466. case SEEK_CUR :
  467. whence_name = "SEEK_CUR" ;
  468. break ;
  469. case SEEK_END :
  470. whence_name = "SEEK_END" ;
  471. break ;
  472. /* SFM_READ */
  473. case SEEK_SET | SFM_READ :
  474. whence_name = "SFM_READ | SEEK_SET" ;
  475. break ;
  476. case SEEK_CUR | SFM_READ :
  477. whence_name = "SFM_READ | SEEK_CUR" ;
  478. break ;
  479. case SEEK_END | SFM_READ :
  480. whence_name = "SFM_READ | SEEK_END" ;
  481. break ;
  482. /* SFM_WRITE */
  483. case SEEK_SET | SFM_WRITE :
  484. whence_name = "SFM_WRITE | SEEK_SET" ;
  485. break ;
  486. case SEEK_CUR | SFM_WRITE :
  487. whence_name = "SFM_WRITE | SEEK_CUR" ;
  488. break ;
  489. case SEEK_END | SFM_WRITE :
  490. whence_name = "SFM_WRITE | SEEK_END" ;
  491. break ;
  492. default :
  493. printf ("\n\nLine %d: bad whence parameter.\n", line_num) ;
  494. exit (1) ;
  495. } ;
  496. channel_name = (channels == 1) ? "Mono" : "Stereo" ;
  497. if ((position = sf_seek (file, offset, whence)) != new_pos)
  498. { printf ("\n\nLine %d : %s : sf_seek (file, %" PRId64 ", %s) returned %" PRId64 " (should be %" PRId64 ").\n\n",
  499. line_num, channel_name, offset, whence_name, position, new_pos) ;
  500. exit (1) ;
  501. } ;
  502. } /* test_seek_or_die */
  503. [+ FOR read_op +]
  504. [+ FOR io_type +]
  505. void
  506. test_[+ (get "op_element") +]_[+ (get "io_element") +]_or_die (SNDFILE *file, int pass, [+ (get "io_element") +] *test, sf_count_t [+ (get "count_name") +], int line_num)
  507. { sf_count_t count ;
  508. if ((count = sf_[+ (get "op_element") +]_[+ (get "io_element") +] (file, test, [+ (get "count_name") +])) != [+ (get "count_name") +])
  509. { printf ("\n\nLine %d", line_num) ;
  510. if (pass > 0)
  511. printf (" (pass %d)", pass) ;
  512. printf (" : sf_[+ (get "op_element") +]_[+ (get "io_element") +] failed with short [+ (get "op_element") +] (%" PRId64 " => %" PRId64 ").\n",
  513. [+ (get "count_name") +], count) ;
  514. fflush (stdout) ;
  515. puts (sf_strerror (file)) ;
  516. exit (1) ;
  517. } ;
  518. return ;
  519. } /* test_[+ (get "op_element") +]_[+ (get "io_element") +]_or_die */
  520. [+ ENDFOR io_type +][+ ENDFOR read_op +]
  521. void
  522. test_read_raw_or_die (SNDFILE *file, int pass, void *test, sf_count_t items, int line_num)
  523. { sf_count_t count ;
  524. if ((count = sf_read_raw (file, test, items)) != items)
  525. { printf ("\n\nLine %d", line_num) ;
  526. if (pass > 0)
  527. printf (" (pass %d)", pass) ;
  528. printf (" : sf_read_raw failed with short read (%" PRId64 " => %" PRId64 ").\n", items, count) ;
  529. fflush (stdout) ;
  530. puts (sf_strerror (file)) ;
  531. exit (1) ;
  532. } ;
  533. return ;
  534. } /* test_read_raw_or_die */
  535. [+ FOR write_op +]
  536. [+ FOR io_type +]
  537. void
  538. test_[+ (get "op_element") +]_[+ (get "io_element") +]_or_die (SNDFILE *file, int pass, const [+ (get "io_element") +] *test, sf_count_t [+ (get "count_name") +], int line_num)
  539. { sf_count_t count ;
  540. if ((count = sf_[+ (get "op_element") +]_[+ (get "io_element") +] (file, test, [+ (get "count_name") +])) != [+ (get "count_name") +])
  541. { printf ("\n\nLine %d", line_num) ;
  542. if (pass > 0)
  543. printf (" (pass %d)", pass) ;
  544. printf (" : sf_[+ (get "op_element") +]_[+ (get "io_element") +] failed with short [+ (get "op_element") +] (%" PRId64 " => %" PRId64 ").\n",
  545. [+ (get "count_name") +], count) ;
  546. fflush (stdout) ;
  547. puts (sf_strerror (file)) ;
  548. exit (1) ;
  549. } ;
  550. return ;
  551. } /* test_[+ (get "op_element") +]_[+ (get "io_element") +]_or_die */
  552. [+ ENDFOR io_type +][+ ENDFOR write_op +]
  553. void
  554. test_write_raw_or_die (SNDFILE *file, int pass, const void *test, sf_count_t items, int line_num)
  555. { sf_count_t count ;
  556. if ((count = sf_write_raw (file, test, items)) != items)
  557. { printf ("\n\nLine %d", line_num) ;
  558. if (pass > 0)
  559. printf (" (pass %d)", pass) ;
  560. printf (" : sf_write_raw failed with short write (%" PRId64 " => %" PRId64 ").\n", items, count) ;
  561. fflush (stdout) ;
  562. puts (sf_strerror (file)) ;
  563. exit (1) ;
  564. } ;
  565. return ;
  566. } /* test_write_raw_or_die */
  567. [+ FOR io_type
  568. +]void
  569. compare_[+ (get "io_element") +]_or_die (const [+ (get "io_element") +] *expected, const [+ (get "io_element") +] *actual, unsigned count, int line_num)
  570. {
  571. unsigned k ;
  572. for (k = 0 ; k < count ; k++)
  573. if (expected [k] != actual [k])
  574. { printf ("\n\nLine %d : Error at index %d, got " [+ (get "format_str") +] ", should be " [+ (get "format_str") +] ".\n\n", line_num, k, actual [k], expected [k]) ;
  575. exit (1) ;
  576. } ;
  577. return ;
  578. } /* compare_[+ (get "io_element") +]_or_die */
  579. [+ ENDFOR io_type +]
  580. void
  581. delete_file (int format, const char *filename)
  582. { char rsrc_name [512], *fname ;
  583. unlink (filename) ;
  584. if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_SD2)
  585. return ;
  586. /*
  587. ** Now try for a resource fork stored as a separate file.
  588. ** Grab the un-adulterated filename again.
  589. */
  590. snprintf (rsrc_name, sizeof (rsrc_name), "%s", filename) ;
  591. if ((fname = strrchr (rsrc_name, '/')) != NULL)
  592. fname ++ ;
  593. else if ((fname = strrchr (rsrc_name, '\\')) != NULL)
  594. fname ++ ;
  595. else
  596. fname = rsrc_name ;
  597. memmove (fname + 2, fname, strlen (fname) + 1) ;
  598. fname [0] = '.' ;
  599. fname [1] = '_' ;
  600. unlink (rsrc_name) ;
  601. } /* delete_file */
  602. static int allowed_open_files = -1 ;
  603. void
  604. count_open_files (void)
  605. {
  606. #if OS_IS_WIN32
  607. return ;
  608. #else
  609. int k, count = 0 ;
  610. struct stat statbuf ;
  611. if (allowed_open_files > 0)
  612. return ;
  613. for (k = 0 ; k < 1024 ; k++)
  614. if (fstat (k, &statbuf) == 0)
  615. count ++ ;
  616. allowed_open_files = count ;
  617. #endif
  618. } /* count_open_files */
  619. void
  620. increment_open_file_count (void)
  621. { allowed_open_files ++ ;
  622. } /* increment_open_file_count */
  623. void
  624. check_open_file_count_or_die (int lineno)
  625. {
  626. #if OS_IS_WIN32
  627. (void) lineno ;
  628. return ;
  629. #else
  630. int k, count = 0 ;
  631. struct stat statbuf ;
  632. if (allowed_open_files < 0)
  633. count_open_files () ;
  634. for (k = 0 ; k < 1024 ; k++)
  635. if (fstat (k, &statbuf) == 0)
  636. count ++ ;
  637. if (count > allowed_open_files)
  638. { printf ("\nLine %d : number of open files (%d) > allowed (%d).\n\n", lineno, count, allowed_open_files) ;
  639. exit (1) ;
  640. } ;
  641. #endif
  642. } /* check_open_file_count_or_die */
  643. void
  644. write_mono_file (const char * filename, int format, int srate, float * output, int len)
  645. { SNDFILE * file ;
  646. SF_INFO sfinfo ;
  647. memset (&sfinfo, 0, sizeof (sfinfo)) ;
  648. sfinfo.samplerate = srate ;
  649. sfinfo.channels = 1 ;
  650. sfinfo.format = format ;
  651. if ((file = sf_open (filename, SFM_WRITE, &sfinfo)) == NULL)
  652. { printf ("sf_open (%s) : %s\n", filename, sf_strerror (NULL)) ;
  653. exit (1) ;
  654. } ;
  655. sf_write_float (file, output, len) ;
  656. sf_close (file) ;
  657. } /* write_mono_file */
  658. void
  659. gen_lowpass_signal_float (float *data, int len)
  660. { int32_t value = 0x1243456 ;
  661. double sample, last_val = 0.0 ;
  662. int k ;
  663. for (k = 0 ; k < len ; k++)
  664. { /* Not a crypto quality RNG. */
  665. value = 11117 * value + 211231 ;
  666. value = 11117 * value + 211231 ;
  667. value = 11117 * value + 211231 ;
  668. sample = value / (0x7fffffff * 1.000001) ;
  669. sample = 0.2 * sample - 0.9 * last_val ;
  670. last_val = sample ;
  671. data [k] = 0.5 * (sample + sin (2.0 * k * M_PI * 1.0 / 32.0)) ;
  672. } ;
  673. } /* gen_lowpass_signal_float */
  674. /*
  675. ** Windows is fucked.
  676. ** If a file is opened R/W and data is written to it, then fstat will return
  677. ** the correct file length, but stat will return zero.
  678. */
  679. sf_count_t
  680. file_length (const char * fname)
  681. { struct stat data ;
  682. if (stat (fname, &data) != 0)
  683. return 0 ;
  684. return (sf_count_t) data.st_size ;
  685. } /* file_length */
  686. sf_count_t
  687. file_length_fd (int fd)
  688. { struct stat data ;
  689. memset (&data, 0, sizeof (data)) ;
  690. if (fstat (fd, &data) != 0)
  691. return 0 ;
  692. return (sf_count_t) data.st_size ;
  693. } /* file_length_fd */
  694. [+ ESAC +]