Chris@40: [+ AutoGen5 template h c +] Chris@40: /* Chris@40: ** Copyright (C) 2002-2016 Erik de Castro Lopo Chris@40: ** Chris@40: ** This program is free software; you can redistribute it and/or modify Chris@40: ** it under the terms of the GNU General Public License as published by Chris@40: ** the Free Software Foundation; either version 2 of the License, or Chris@40: ** (at your option) any later version. Chris@40: ** Chris@40: ** This program is distributed in the hope that it will be useful, Chris@40: ** but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@40: ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@40: ** GNU General Public License for more details. Chris@40: ** Chris@40: ** You should have received a copy of the GNU General Public License Chris@40: ** along with this program; if not, write to the Free Software Chris@40: ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. Chris@40: */ Chris@40: Chris@40: /* Chris@40: ** Utility functions to make writing the test suite easier. Chris@40: ** Chris@40: ** The .c and .h files were generated automagically with Autogen from Chris@40: ** the files utils.def and utils.tpl. Chris@40: */ Chris@40: Chris@40: [+ CASE (suffix) +] Chris@40: [+ == h +] Chris@40: Chris@40: #ifdef __cplusplus Chris@40: extern "C" { Chris@40: #endif /* __cplusplus */ Chris@40: Chris@40: #include Chris@40: #include Chris@40: Chris@40: #define ARRAY_LEN(x) ((int) (sizeof (x)) / (sizeof ((x) [0]))) Chris@40: #define SIGNED_SIZEOF(x) ((int64_t) (sizeof (x))) Chris@40: #define NOT(x) (! (x)) Chris@40: #define ABS(x) ((x) >= 0 ? (x) : -(x)) Chris@40: Chris@40: #define PIPE_INDEX(x) ((x) + 500) Chris@40: #define PIPE_TEST_LEN 12345 Chris@40: Chris@40: Chris@40: [+ FOR float_type Chris@40: +]void gen_windowed_sine_[+ (get "name") +] ([+ (get "name") +] *data, int len, double maximum) ; Chris@40: [+ ENDFOR float_type Chris@40: +] Chris@40: Chris@40: void create_short_sndfile (const char *filename, int format, int channels) ; Chris@40: Chris@40: void check_file_hash_or_die (const char *filename, uint64_t target_hash, int line_num) ; Chris@40: Chris@40: void print_test_name (const char *test, const char *filename) ; Chris@40: Chris@40: void dump_data_to_file (const char *filename, const void *data, unsigned int datalen) ; Chris@40: Chris@40: void write_mono_file (const char * filename, int format, int srate, float * output, int len) ; Chris@40: Chris@40: #ifdef __GNUC__ Chris@40: static inline void Chris@40: exit_if_true (int test, const char *format, ...) Chris@40: #if (defined (__USE_MINGW_ANSI_STDIO) && __USE_MINGW_ANSI_STDIO) Chris@40: __attribute__ ((format (gnu_printf, 2, 3))) ; Chris@40: #else Chris@40: __attribute__ ((format (printf, 2, 3))) ; Chris@40: #endif Chris@40: #endif Chris@40: Chris@40: static inline void Chris@40: exit_if_true (int test, const char *format, ...) Chris@40: { if (test) Chris@40: { va_list argptr ; Chris@40: va_start (argptr, format) ; Chris@40: vprintf (format, argptr) ; Chris@40: va_end (argptr) ; Chris@40: exit (1) ; Chris@40: } ; Chris@40: } /* exit_if_true */ Chris@40: Chris@40: static inline int32_t Chris@40: arith_shift_left (int32_t x, int shift) Chris@40: { return (int32_t) (((uint32_t) x) << shift) ; Chris@40: } /* arith_shift_left */ Chris@40: Chris@40: /* Chris@40: ** Functions for saving two vectors of data in an ascii text file which Chris@40: ** can then be loaded into GNU octave for comparison. Chris@40: */ Chris@40: Chris@40: [+ FOR io_type Chris@40: +]int oct_save_[+ (get "io_element") +] (const [+ (get "io_element") +] *a, const [+ (get "io_element") +] *b, int len) ; Chris@40: [+ ENDFOR io_type Chris@40: +] Chris@40: Chris@40: void delete_file (int format, const char *filename) ; Chris@40: Chris@40: void count_open_files (void) ; Chris@40: void increment_open_file_count (void) ; Chris@40: void check_open_file_count_or_die (int lineno) ; Chris@40: Chris@40: #ifdef SNDFILE_H Chris@40: Chris@40: static inline void Chris@40: sf_info_clear (SF_INFO * info) Chris@40: { memset (info, 0, sizeof (SF_INFO)) ; Chris@40: } /* sf_info_clear */ Chris@40: Chris@40: static inline void Chris@40: sf_info_setup (SF_INFO * info, int format, int samplerate, int channels) Chris@40: { sf_info_clear (info) ; Chris@40: Chris@40: info->format = format ; Chris@40: info->samplerate = samplerate ; Chris@40: info->channels = channels ; Chris@40: } /* sf_info_setup */ Chris@40: Chris@40: Chris@40: void dump_log_buffer (SNDFILE *file) ; Chris@40: void check_log_buffer_or_die (SNDFILE *file, int line_num) ; Chris@40: int string_in_log_buffer (SNDFILE *file, const char *s) ; Chris@40: void hexdump_file (const char * filename, sf_count_t offset, sf_count_t length) ; Chris@40: Chris@40: void test_sf_format_or_die (const SF_INFO *info, int line_num) ; Chris@40: Chris@40: SNDFILE *test_open_file_or_die Chris@40: (const char *filename, int mode, SF_INFO *sfinfo, int allow_fd, int line_num) ; Chris@40: Chris@40: void test_read_write_position_or_die Chris@40: (SNDFILE *file, int line_num, int pass, sf_count_t read_pos, sf_count_t write_pos) ; Chris@40: Chris@40: void test_seek_or_die Chris@40: (SNDFILE *file, sf_count_t offset, int whence, sf_count_t new_pos, int channels, int line_num) ; Chris@40: Chris@40: [+ FOR read_op +] Chris@40: [+ FOR io_type Chris@40: +]void test_[+ (get "op_element") +]_[+ (get "io_element") +]_or_die Chris@40: (SNDFILE *file, int pass, [+ (get "io_element") +] *test, sf_count_t [+ (get "count_name") +], int line_num) ; Chris@40: [+ ENDFOR io_type +][+ ENDFOR read_op +] Chris@40: Chris@40: void Chris@40: test_read_raw_or_die (SNDFILE *file, int pass, void *test, sf_count_t items, int line_num) ; Chris@40: Chris@40: [+ FOR write_op +] Chris@40: [+ FOR io_type Chris@40: +]void test_[+ (get "op_element") +]_[+ (get "io_element") +]_or_die Chris@40: (SNDFILE *file, int pass, const [+ (get "io_element") +] *test, sf_count_t [+ (get "count_name") +], int line_num) ; Chris@40: [+ ENDFOR io_type +][+ ENDFOR write_op +] Chris@40: Chris@40: void Chris@40: test_write_raw_or_die (SNDFILE *file, int pass, const void *test, sf_count_t items, int line_num) ; Chris@40: Chris@40: [+ FOR io_type Chris@40: +]void compare_[+ (get "io_element") +]_or_die (const [+ (get "io_element") +] *expected, const [+ (get "io_element") +] *actual, unsigned count, int line_num) ; Chris@40: [+ ENDFOR io_type +] Chris@40: Chris@40: Chris@40: void gen_lowpass_signal_float (float *data, int len) ; Chris@40: Chris@40: sf_count_t file_length (const char * fname) ; Chris@40: sf_count_t file_length_fd (int fd) ; Chris@40: Chris@40: #endif Chris@40: Chris@40: #ifdef __cplusplus Chris@40: } /* extern "C" */ Chris@40: #endif /* __cplusplus */ Chris@40: Chris@40: [+ == c +] Chris@40: Chris@40: #include "sfconfig.h" Chris@40: Chris@40: #include Chris@40: #include Chris@40: #include Chris@40: Chris@40: #if HAVE_UNISTD_H Chris@40: #include Chris@40: #endif Chris@40: Chris@40: #if (HAVE_DECL_S_IRGRP == 0) Chris@40: #include Chris@40: #endif Chris@40: Chris@40: #include Chris@40: #include Chris@40: #include Chris@40: #include Chris@40: #include Chris@40: #include Chris@40: Chris@40: #include Chris@40: Chris@40: #include "utils.h" Chris@40: Chris@40: #ifndef M_PI Chris@40: #define M_PI 3.14159265358979323846264338 Chris@40: #endif Chris@40: Chris@40: #define LOG_BUFFER_SIZE 2048 Chris@40: Chris@40: /* Chris@40: ** Neat solution to the Win32/OS2 binary file flage requirement. Chris@40: ** If O_BINARY isn't already defined by the inclusion of the system Chris@40: ** headers, set it to zero. Chris@40: */ Chris@40: #ifndef O_BINARY Chris@40: #define O_BINARY 0 Chris@40: #endif Chris@40: Chris@40: [+ FOR float_type +] Chris@40: void Chris@40: gen_windowed_sine_[+ (get "name") +] ([+ (get "name") +] *data, int len, double maximum) Chris@40: { int k ; Chris@40: Chris@40: memset (data, 0, len * sizeof ([+ (get "name") +])) ; Chris@40: Chris@40: len = (5 * len) / 6 ; Chris@40: Chris@40: for (k = 0 ; k < len ; k++) Chris@40: { data [k] = sin (2.0 * k * M_PI * 1.0 / 32.0 + 0.4) ; Chris@40: Chris@40: /* Apply Hanning Window. */ Chris@40: data [k] *= maximum * (0.5 - 0.5 * cos (2.0 * M_PI * k / ((len) - 1))) ; Chris@40: } Chris@40: Chris@40: return ; Chris@40: } /* gen_windowed_sine_[+ (get "name") +] */ Chris@40: [+ ENDFOR float_type +] Chris@40: Chris@40: void Chris@40: create_short_sndfile (const char *filename, int format, int channels) Chris@40: { short data [2 * 3 * 4 * 5 * 6 * 7] = { 0, } ; Chris@40: SNDFILE *file ; Chris@40: SF_INFO sfinfo ; Chris@40: Chris@40: sfinfo.samplerate = 44100 ; Chris@40: sfinfo.channels = channels ; Chris@40: sfinfo.format = format ; Chris@40: Chris@40: if ((file = sf_open (filename, SFM_WRITE, &sfinfo)) == NULL) Chris@40: { printf ("Error (%s, %d) : sf_open failed : %s\n", __FILE__, __LINE__, sf_strerror (file)) ; Chris@40: exit (1) ; Chris@40: } ; Chris@40: Chris@40: sf_write_short (file, data, ARRAY_LEN (data)) ; Chris@40: Chris@40: sf_close (file) ; Chris@40: } /* create_short_sndfile */ Chris@40: Chris@40: void Chris@40: check_file_hash_or_die (const char *filename, uint64_t target_hash, int line_num) Chris@40: { static unsigned char buf [4096] ; Chris@40: uint64_t cksum ; Chris@40: FILE *file ; Chris@40: int k, read_count ; Chris@40: Chris@40: memset (buf, 0, sizeof (buf)) ; Chris@40: Chris@40: /* The 'b' in the mode string means binary for Win32. */ Chris@40: if ((file = fopen (filename, "rb")) == NULL) Chris@40: { printf ("\n\nLine %d: could not open file '%s'\n\n", line_num, filename) ; Chris@40: exit (1) ; Chris@40: } ; Chris@40: Chris@40: cksum = 0 ; Chris@40: Chris@40: while ((read_count = fread (buf, 1, sizeof (buf), file))) Chris@40: for (k = 0 ; k < read_count ; k++) Chris@40: cksum = (cksum * 511 + buf [k]) & 0xfffffffffffff ; Chris@40: Chris@40: fclose (file) ; Chris@40: Chris@40: if (target_hash == 0) Chris@40: { printf (" 0x%" PRIx64 "\n", cksum) ; Chris@40: return ; Chris@40: } ; Chris@40: Chris@40: if (cksum != target_hash) Chris@40: { printf ("\n\nLine %d: incorrect hash value 0x%" PRIx64 " should be 0x%" PRIx64 ".\n\n", line_num, cksum, target_hash) ; Chris@40: exit (1) ; Chris@40: } ; Chris@40: Chris@40: return ; Chris@40: } /* check_file_hash_or_die */ Chris@40: Chris@40: void Chris@40: print_test_name (const char *test, const char *filename) Chris@40: { int count ; Chris@40: Chris@40: if (test == NULL) Chris@40: { printf (__FILE__ ": bad test of filename parameter.\n") ; Chris@40: exit (1) ; Chris@40: } ; Chris@40: Chris@40: if (filename == NULL || strlen (filename) == 0) Chris@40: { printf (" %-30s : ", test) ; Chris@40: count = 25 ; Chris@40: } Chris@40: else Chris@40: { printf (" %-30s : %s ", test, filename) ; Chris@40: count = 24 - strlen (filename) ; Chris@40: } ; Chris@40: Chris@40: while (count -- > 0) Chris@40: putchar ('.') ; Chris@40: putchar (' ') ; Chris@40: Chris@40: fflush (stdout) ; Chris@40: } /* print_test_name */ Chris@40: Chris@40: void Chris@40: dump_data_to_file (const char *filename, const void *data, unsigned int datalen) Chris@40: { FILE *file ; Chris@40: Chris@40: if ((file = fopen (filename, "wb")) == NULL) Chris@40: { printf ("\n\nLine %d : could not open file : %s\n\n", __LINE__, filename) ; Chris@40: exit (1) ; Chris@40: } ; Chris@40: Chris@40: if (fwrite (data, 1, datalen, file) != datalen) Chris@40: { printf ("\n\nLine %d : fwrite failed.\n\n", __LINE__) ; Chris@40: exit (1) ; Chris@40: } ; Chris@40: Chris@40: fclose (file) ; Chris@40: Chris@40: } /* dump_data_to_file */ Chris@40: Chris@40: /*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- Chris@40: */ Chris@40: Chris@40: static char octfilename [] = "error.dat" ; Chris@40: Chris@40: [+ FOR io_type Chris@40: +]int Chris@40: oct_save_[+ (get "io_element") +] (const [+ (get "io_element") +] *a, const [+ (get "io_element") +] *b, int len) Chris@40: { FILE *file ; Chris@40: int k ; Chris@40: Chris@40: if (! (file = fopen (octfilename, "w"))) Chris@40: return 1 ; Chris@40: Chris@40: fprintf (file, "# Not created by Octave\n") ; Chris@40: Chris@40: fprintf (file, "# name: a\n") ; Chris@40: fprintf (file, "# type: matrix\n") ; Chris@40: fprintf (file, "# rows: %d\n", len) ; Chris@40: fprintf (file, "# columns: 1\n") ; Chris@40: Chris@40: for (k = 0 ; k < len ; k++) Chris@40: fprintf (file, [+ (get "format_str") +] "\n", a [k]) ; Chris@40: Chris@40: fprintf (file, "# name: b\n") ; Chris@40: fprintf (file, "# type: matrix\n") ; Chris@40: fprintf (file, "# rows: %d\n", len) ; Chris@40: fprintf (file, "# columns: 1\n") ; Chris@40: Chris@40: for (k = 0 ; k < len ; k++) Chris@40: fprintf (file, [+ (get "format_str") +] "\n", b [k]) ; Chris@40: Chris@40: fclose (file) ; Chris@40: return 0 ; Chris@40: } /* oct_save_[+ (get "io_element") +] */ Chris@40: [+ ENDFOR io_type Chris@40: +] Chris@40: Chris@40: void Chris@40: check_log_buffer_or_die (SNDFILE *file, int line_num) Chris@40: { static char buffer [LOG_BUFFER_SIZE] ; Chris@40: int count ; Chris@40: Chris@40: memset (buffer, 0, sizeof (buffer)) ; Chris@40: Chris@40: /* Get the log buffer data. */ Chris@40: count = sf_command (file, SFC_GET_LOG_INFO, buffer, LOG_BUFFER_SIZE) ; Chris@40: Chris@40: if (LOG_BUFFER_SIZE - count < 2) Chris@40: { printf ("\n\nLine %d : Possible long log buffer.\n", line_num) ; Chris@40: exit (1) ; Chris@40: } Chris@40: Chris@40: /* Look for "Should" */ Chris@40: if (strstr (buffer, "ould")) Chris@40: { printf ("\n\nLine %d : Log buffer contains `ould'. Dumping.\n", line_num) ; Chris@40: puts (buffer) ; Chris@40: exit (1) ; Chris@40: } ; Chris@40: Chris@40: /* Look for "**" */ Chris@40: if (strstr (buffer, "*")) Chris@40: { printf ("\n\nLine %d : Log buffer contains `*'. Dumping.\n", line_num) ; Chris@40: puts (buffer) ; Chris@40: exit (1) ; Chris@40: } ; Chris@40: Chris@40: /* Look for "Should" */ Chris@40: if (strstr (buffer, "nknown marker")) Chris@40: { printf ("\n\nLine %d : Log buffer contains `nknown marker'. Dumping.\n", line_num) ; Chris@40: puts (buffer) ; Chris@40: exit (1) ; Chris@40: } ; Chris@40: Chris@40: return ; Chris@40: } /* check_log_buffer_or_die */ Chris@40: Chris@40: int Chris@40: string_in_log_buffer (SNDFILE *file, const char *s) Chris@40: { static char buffer [LOG_BUFFER_SIZE] ; Chris@40: int count ; Chris@40: Chris@40: memset (buffer, 0, sizeof (buffer)) ; Chris@40: Chris@40: /* Get the log buffer data. */ Chris@40: count = sf_command (file, SFC_GET_LOG_INFO, buffer, LOG_BUFFER_SIZE) ; Chris@40: Chris@40: if (LOG_BUFFER_SIZE - count < 2) Chris@40: { printf ("Possible long log buffer.\n") ; Chris@40: exit (1) ; Chris@40: } Chris@40: Chris@40: /* Look for string */ Chris@40: return strstr (buffer, s) ? SF_TRUE : SF_FALSE ; Chris@40: } /* string_in_log_buffer */ Chris@40: Chris@40: void Chris@40: hexdump_file (const char * filename, sf_count_t offset, sf_count_t length) Chris@40: { Chris@40: FILE * file ; Chris@40: char buffer [16] ; Chris@40: int k, m, ch, readcount ; Chris@40: Chris@40: if (length > 1000000) Chris@40: { printf ("\n\nError : length (%" PRId64 ") too long.\n\n", offset) ; Chris@40: exit (1) ; Chris@40: } ; Chris@40: Chris@40: if ((file = fopen (filename, "r")) == NULL) Chris@40: { printf ("\n\nError : hexdump_file (%s) could not open file for read.\n\n", filename) ; Chris@40: exit (1) ; Chris@40: } ; Chris@40: Chris@40: if (fseek (file, offset, SEEK_SET) != 0) Chris@40: { printf ("\n\nError : fseek(file, %" PRId64 ", SEEK_SET) failed : %s\n\n", offset, strerror (errno)) ; Chris@40: exit (1) ; Chris@40: } ; Chris@40: Chris@40: puts ("\n\n") ; Chris@40: Chris@40: for (k = 0 ; k < length ; k+= sizeof (buffer)) Chris@40: { readcount = fread (buffer, 1, sizeof (buffer), file) ; Chris@40: Chris@40: printf ("%08" PRIx64 " : ", offset + k) ; Chris@40: Chris@40: for (m = 0 ; m < readcount ; m++) Chris@40: printf ("%02x ", buffer [m] & 0xFF) ; Chris@40: Chris@40: for (m = readcount ; m < SIGNED_SIZEOF (buffer) ; m++) Chris@40: printf (" ") ; Chris@40: Chris@40: printf (" ") ; Chris@40: for (m = 0 ; m < readcount ; m++) Chris@40: { ch = isprint (buffer [m]) ? buffer [m] : '.' ; Chris@40: putchar (ch) ; Chris@40: } ; Chris@40: Chris@40: if (readcount < SIGNED_SIZEOF (buffer)) Chris@40: break ; Chris@40: Chris@40: putchar ('\n') ; Chris@40: } ; Chris@40: Chris@40: puts ("\n") ; Chris@40: Chris@40: fclose (file) ; Chris@40: } /* hexdump_file */ Chris@40: Chris@40: void Chris@40: dump_log_buffer (SNDFILE *file) Chris@40: { static char buffer [LOG_BUFFER_SIZE] ; Chris@40: Chris@40: memset (buffer, 0, sizeof (buffer)) ; Chris@40: Chris@40: /* Get the log buffer data. */ Chris@40: sf_command (file, SFC_GET_LOG_INFO, buffer, LOG_BUFFER_SIZE) ; Chris@40: Chris@40: if (strlen (buffer) < 1) Chris@40: puts ("Log buffer empty.\n") ; Chris@40: else Chris@40: puts (buffer) ; Chris@40: Chris@40: return ; Chris@40: } /* dump_log_buffer */ Chris@40: Chris@40: void Chris@40: test_sf_format_or_die (const SF_INFO *info, int line_num) Chris@40: { int res ; Chris@40: Chris@40: if ((res = sf_format_check (info)) != 1) Chris@40: { printf ("\n\nLine %d : sf_format_check returned error (%d)\n\n", line_num,res) ; Chris@40: exit (1) ; Chris@40: } ; Chris@40: Chris@40: return ; Chris@40: } /* test_sf_format_or_die */ Chris@40: Chris@40: SNDFILE * Chris@40: test_open_file_or_die (const char *filename, int mode, SF_INFO *sfinfo, int allow_fd, int line_num) Chris@40: { static int count = 0 ; Chris@40: Chris@40: SNDFILE *file ; Chris@40: const char *modestr, *func_name ; Chris@40: int oflags = 0, omode = 0, err ; Chris@40: Chris@40: /* Chris@40: ** Need to test both sf_open() and sf_open_fd(). Chris@40: ** Do so alternately. Chris@40: */ Chris@40: switch (mode) Chris@40: { case SFM_READ : Chris@40: modestr = "SFM_READ" ; Chris@40: oflags = O_RDONLY | O_BINARY ; Chris@40: omode = 0 ; Chris@40: break ; Chris@40: Chris@40: case SFM_WRITE : Chris@40: modestr = "SFM_WRITE" ; Chris@40: oflags = O_WRONLY | O_CREAT | O_TRUNC | O_BINARY ; Chris@40: omode = S_IRUSR | S_IWUSR | S_IRGRP ; Chris@40: break ; Chris@40: Chris@40: case SFM_RDWR : Chris@40: modestr = "SFM_RDWR" ; Chris@40: oflags = O_RDWR | O_CREAT | O_BINARY ; Chris@40: omode = S_IRUSR | S_IWUSR | S_IRGRP ; Chris@40: break ; Chris@40: default : Chris@40: printf ("\n\nLine %d: Bad mode.\n", line_num) ; Chris@40: fflush (stdout) ; Chris@40: exit (1) ; Chris@40: } ; Chris@40: Chris@40: if (OS_IS_WIN32) Chris@40: { /* Windows does not understand and ignores the S_IRGRP flag, but Wine Chris@40: ** gives a run time warning message, so just clear it. Chris@40: */ Chris@40: omode &= ~S_IRGRP ; Chris@40: } ; Chris@40: Chris@40: if (allow_fd && ((++count) & 1) == 1) Chris@40: { int fd ; Chris@40: Chris@40: /* Only use the three argument open() function if omode != 0. */ Chris@40: fd = (omode == 0) ? open (filename, oflags) : open (filename, oflags, omode) ; Chris@40: Chris@40: if (fd < 0) Chris@40: { printf ("\n\n%s : open failed : %s\n", __func__, strerror (errno)) ; Chris@40: exit (1) ; Chris@40: } ; Chris@40: Chris@40: func_name = "sf_open_fd" ; Chris@40: file = sf_open_fd (fd, mode, sfinfo, SF_TRUE) ; Chris@40: } Chris@40: else Chris@40: { func_name = "sf_open" ; Chris@40: file = sf_open (filename, mode, sfinfo) ; Chris@40: } ; Chris@40: Chris@40: if (file == NULL) Chris@40: { printf ("\n\nLine %d: %s (%s) failed : %s\n\n", line_num, func_name, modestr, sf_strerror (NULL)) ; Chris@40: dump_log_buffer (file) ; Chris@40: exit (1) ; Chris@40: } ; Chris@40: Chris@40: err = sf_error (file) ; Chris@40: if (err != SF_ERR_NO_ERROR) Chris@40: { printf ("\n\nLine %d : sf_error : %s\n\n", line_num, sf_error_number (err)) ; Chris@40: dump_log_buffer (file) ; Chris@40: exit (1) ; Chris@40: } ; Chris@40: Chris@40: return file ; Chris@40: } /* test_open_file_or_die */ Chris@40: Chris@40: void Chris@40: test_read_write_position_or_die (SNDFILE *file, int line_num, int pass, sf_count_t read_pos, sf_count_t write_pos) Chris@40: { sf_count_t pos ; Chris@40: Chris@40: /* Check the current read position. */ Chris@40: if (read_pos >= 0 && (pos = sf_seek (file, 0, SEEK_CUR | SFM_READ)) != read_pos) Chris@40: { printf ("\n\nLine %d ", line_num) ; Chris@40: if (pass > 0) Chris@40: printf ("(pass %d): ", pass) ; Chris@40: printf ("Read position (%" PRId64 ") should be %" PRId64 ".\n", pos, read_pos) ; Chris@40: exit (1) ; Chris@40: } ; Chris@40: Chris@40: /* Check the current write position. */ Chris@40: if (write_pos >= 0 && (pos = sf_seek (file, 0, SEEK_CUR | SFM_WRITE)) != write_pos) Chris@40: { printf ("\n\nLine %d", line_num) ; Chris@40: if (pass > 0) Chris@40: printf (" (pass %d)", pass) ; Chris@40: printf (" : Write position (%" PRId64 ") should be %" PRId64 ".\n", pos, write_pos) ; Chris@40: exit (1) ; Chris@40: } ; Chris@40: Chris@40: return ; Chris@40: } /* test_read_write_position */ Chris@40: Chris@40: void Chris@40: test_seek_or_die (SNDFILE *file, sf_count_t offset, int whence, sf_count_t new_pos, int channels, int line_num) Chris@40: { sf_count_t position ; Chris@40: const char *channel_name, *whence_name ; Chris@40: Chris@40: switch (whence) Chris@40: { case SEEK_SET : Chris@40: whence_name = "SEEK_SET" ; Chris@40: break ; Chris@40: case SEEK_CUR : Chris@40: whence_name = "SEEK_CUR" ; Chris@40: break ; Chris@40: case SEEK_END : Chris@40: whence_name = "SEEK_END" ; Chris@40: break ; Chris@40: Chris@40: /* SFM_READ */ Chris@40: case SEEK_SET | SFM_READ : Chris@40: whence_name = "SFM_READ | SEEK_SET" ; Chris@40: break ; Chris@40: case SEEK_CUR | SFM_READ : Chris@40: whence_name = "SFM_READ | SEEK_CUR" ; Chris@40: break ; Chris@40: case SEEK_END | SFM_READ : Chris@40: whence_name = "SFM_READ | SEEK_END" ; Chris@40: break ; Chris@40: Chris@40: /* SFM_WRITE */ Chris@40: case SEEK_SET | SFM_WRITE : Chris@40: whence_name = "SFM_WRITE | SEEK_SET" ; Chris@40: break ; Chris@40: case SEEK_CUR | SFM_WRITE : Chris@40: whence_name = "SFM_WRITE | SEEK_CUR" ; Chris@40: break ; Chris@40: case SEEK_END | SFM_WRITE : Chris@40: whence_name = "SFM_WRITE | SEEK_END" ; Chris@40: break ; Chris@40: Chris@40: default : Chris@40: printf ("\n\nLine %d: bad whence parameter.\n", line_num) ; Chris@40: exit (1) ; Chris@40: } ; Chris@40: Chris@40: channel_name = (channels == 1) ? "Mono" : "Stereo" ; Chris@40: Chris@40: if ((position = sf_seek (file, offset, whence)) != new_pos) Chris@40: { printf ("\n\nLine %d : %s : sf_seek (file, %" PRId64 ", %s) returned %" PRId64 " (should be %" PRId64 ").\n\n", Chris@40: line_num, channel_name, offset, whence_name, position, new_pos) ; Chris@40: exit (1) ; Chris@40: } ; Chris@40: Chris@40: } /* test_seek_or_die */ Chris@40: Chris@40: [+ FOR read_op +] Chris@40: [+ FOR io_type +] Chris@40: void Chris@40: 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) Chris@40: { sf_count_t count ; Chris@40: Chris@40: if ((count = sf_[+ (get "op_element") +]_[+ (get "io_element") +] (file, test, [+ (get "count_name") +])) != [+ (get "count_name") +]) Chris@40: { printf ("\n\nLine %d", line_num) ; Chris@40: if (pass > 0) Chris@40: printf (" (pass %d)", pass) ; Chris@40: printf (" : sf_[+ (get "op_element") +]_[+ (get "io_element") +] failed with short [+ (get "op_element") +] (%" PRId64 " => %" PRId64 ").\n", Chris@40: [+ (get "count_name") +], count) ; Chris@40: fflush (stdout) ; Chris@40: puts (sf_strerror (file)) ; Chris@40: exit (1) ; Chris@40: } ; Chris@40: Chris@40: return ; Chris@40: } /* test_[+ (get "op_element") +]_[+ (get "io_element") +]_or_die */ Chris@40: [+ ENDFOR io_type +][+ ENDFOR read_op +] Chris@40: Chris@40: void Chris@40: test_read_raw_or_die (SNDFILE *file, int pass, void *test, sf_count_t items, int line_num) Chris@40: { sf_count_t count ; Chris@40: Chris@40: if ((count = sf_read_raw (file, test, items)) != items) Chris@40: { printf ("\n\nLine %d", line_num) ; Chris@40: if (pass > 0) Chris@40: printf (" (pass %d)", pass) ; Chris@40: printf (" : sf_read_raw failed with short read (%" PRId64 " => %" PRId64 ").\n", items, count) ; Chris@40: fflush (stdout) ; Chris@40: puts (sf_strerror (file)) ; Chris@40: exit (1) ; Chris@40: } ; Chris@40: Chris@40: return ; Chris@40: } /* test_read_raw_or_die */ Chris@40: Chris@40: [+ FOR write_op +] Chris@40: [+ FOR io_type +] Chris@40: void Chris@40: 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) Chris@40: { sf_count_t count ; Chris@40: Chris@40: if ((count = sf_[+ (get "op_element") +]_[+ (get "io_element") +] (file, test, [+ (get "count_name") +])) != [+ (get "count_name") +]) Chris@40: { printf ("\n\nLine %d", line_num) ; Chris@40: if (pass > 0) Chris@40: printf (" (pass %d)", pass) ; Chris@40: printf (" : sf_[+ (get "op_element") +]_[+ (get "io_element") +] failed with short [+ (get "op_element") +] (%" PRId64 " => %" PRId64 ").\n", Chris@40: [+ (get "count_name") +], count) ; Chris@40: fflush (stdout) ; Chris@40: puts (sf_strerror (file)) ; Chris@40: exit (1) ; Chris@40: } ; Chris@40: Chris@40: return ; Chris@40: } /* test_[+ (get "op_element") +]_[+ (get "io_element") +]_or_die */ Chris@40: [+ ENDFOR io_type +][+ ENDFOR write_op +] Chris@40: Chris@40: void Chris@40: test_write_raw_or_die (SNDFILE *file, int pass, const void *test, sf_count_t items, int line_num) Chris@40: { sf_count_t count ; Chris@40: Chris@40: if ((count = sf_write_raw (file, test, items)) != items) Chris@40: { printf ("\n\nLine %d", line_num) ; Chris@40: if (pass > 0) Chris@40: printf (" (pass %d)", pass) ; Chris@40: printf (" : sf_write_raw failed with short write (%" PRId64 " => %" PRId64 ").\n", items, count) ; Chris@40: fflush (stdout) ; Chris@40: puts (sf_strerror (file)) ; Chris@40: exit (1) ; Chris@40: } ; Chris@40: Chris@40: return ; Chris@40: } /* test_write_raw_or_die */ Chris@40: Chris@40: Chris@40: [+ FOR io_type Chris@40: +]void Chris@40: compare_[+ (get "io_element") +]_or_die (const [+ (get "io_element") +] *expected, const [+ (get "io_element") +] *actual, unsigned count, int line_num) Chris@40: { Chris@40: unsigned k ; Chris@40: Chris@40: for (k = 0 ; k < count ; k++) Chris@40: if (expected [k] != actual [k]) Chris@40: { 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]) ; Chris@40: exit (1) ; Chris@40: } ; Chris@40: Chris@40: return ; Chris@40: } /* compare_[+ (get "io_element") +]_or_die */ Chris@40: [+ ENDFOR io_type +] Chris@40: Chris@40: Chris@40: void Chris@40: delete_file (int format, const char *filename) Chris@40: { char rsrc_name [512], *fname ; Chris@40: Chris@40: unlink (filename) ; Chris@40: Chris@40: if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_SD2) Chris@40: return ; Chris@40: Chris@40: /* Chris@40: ** Now try for a resource fork stored as a separate file. Chris@40: ** Grab the un-adulterated filename again. Chris@40: */ Chris@40: snprintf (rsrc_name, sizeof (rsrc_name), "%s", filename) ; Chris@40: Chris@40: if ((fname = strrchr (rsrc_name, '/')) != NULL) Chris@40: fname ++ ; Chris@40: else if ((fname = strrchr (rsrc_name, '\\')) != NULL) Chris@40: fname ++ ; Chris@40: else Chris@40: fname = rsrc_name ; Chris@40: Chris@40: memmove (fname + 2, fname, strlen (fname) + 1) ; Chris@40: fname [0] = '.' ; Chris@40: fname [1] = '_' ; Chris@40: Chris@40: unlink (rsrc_name) ; Chris@40: } /* delete_file */ Chris@40: Chris@40: static int allowed_open_files = -1 ; Chris@40: Chris@40: void Chris@40: count_open_files (void) Chris@40: { Chris@40: #if OS_IS_WIN32 Chris@40: return ; Chris@40: #else Chris@40: int k, count = 0 ; Chris@40: struct stat statbuf ; Chris@40: Chris@40: if (allowed_open_files > 0) Chris@40: return ; Chris@40: Chris@40: for (k = 0 ; k < 1024 ; k++) Chris@40: if (fstat (k, &statbuf) == 0) Chris@40: count ++ ; Chris@40: Chris@40: allowed_open_files = count ; Chris@40: #endif Chris@40: } /* count_open_files */ Chris@40: Chris@40: void Chris@40: increment_open_file_count (void) Chris@40: { allowed_open_files ++ ; Chris@40: } /* increment_open_file_count */ Chris@40: Chris@40: void Chris@40: check_open_file_count_or_die (int lineno) Chris@40: { Chris@40: #if OS_IS_WIN32 Chris@40: (void) lineno ; Chris@40: return ; Chris@40: #else Chris@40: int k, count = 0 ; Chris@40: struct stat statbuf ; Chris@40: Chris@40: if (allowed_open_files < 0) Chris@40: count_open_files () ; Chris@40: Chris@40: for (k = 0 ; k < 1024 ; k++) Chris@40: if (fstat (k, &statbuf) == 0) Chris@40: count ++ ; Chris@40: Chris@40: if (count > allowed_open_files) Chris@40: { printf ("\nLine %d : number of open files (%d) > allowed (%d).\n\n", lineno, count, allowed_open_files) ; Chris@40: exit (1) ; Chris@40: } ; Chris@40: #endif Chris@40: } /* check_open_file_count_or_die */ Chris@40: Chris@40: void Chris@40: write_mono_file (const char * filename, int format, int srate, float * output, int len) Chris@40: { SNDFILE * file ; Chris@40: SF_INFO sfinfo ; Chris@40: Chris@40: memset (&sfinfo, 0, sizeof (sfinfo)) ; Chris@40: Chris@40: sfinfo.samplerate = srate ; Chris@40: sfinfo.channels = 1 ; Chris@40: sfinfo.format = format ; Chris@40: Chris@40: if ((file = sf_open (filename, SFM_WRITE, &sfinfo)) == NULL) Chris@40: { printf ("sf_open (%s) : %s\n", filename, sf_strerror (NULL)) ; Chris@40: exit (1) ; Chris@40: } ; Chris@40: Chris@40: sf_write_float (file, output, len) ; Chris@40: Chris@40: sf_close (file) ; Chris@40: } /* write_mono_file */ Chris@40: Chris@40: void Chris@40: gen_lowpass_signal_float (float *data, int len) Chris@40: { int64_t value = 0x1243456 ; Chris@40: double sample, last_val = 0.0 ; Chris@40: int k ; Chris@40: Chris@40: for (k = 0 ; k < len ; k++) Chris@40: { /* Not a crypto quality RNG. */ Chris@40: value = (11117 * value + 211231) & 0xffffffff ; Chris@40: value = (11117 * value + 211231) & 0xffffffff ; Chris@40: value = (11117 * value + 211231) & 0xffffffff ; Chris@40: Chris@40: sample = value / (0x7fffffff * 1.000001) ; Chris@40: sample = 0.2 * sample - 0.9 * last_val ; Chris@40: Chris@40: last_val = sample ; Chris@40: Chris@40: data [k] = 0.5 * (sample + sin (2.0 * k * M_PI * 1.0 / 32.0)) ; Chris@40: } ; Chris@40: Chris@40: } /* gen_lowpass_signal_float */ Chris@40: Chris@40: Chris@40: /* Chris@40: ** Windows is fucked. Chris@40: ** If a file is opened R/W and data is written to it, then fstat will return Chris@40: ** the correct file length, but stat will return zero. Chris@40: */ Chris@40: Chris@40: sf_count_t Chris@40: file_length (const char * fname) Chris@40: { struct stat data ; Chris@40: Chris@40: if (stat (fname, &data) != 0) Chris@40: return 0 ; Chris@40: Chris@40: return (sf_count_t) data.st_size ; Chris@40: } /* file_length */ Chris@40: Chris@40: sf_count_t Chris@40: file_length_fd (int fd) Chris@40: { struct stat data ; Chris@40: Chris@40: memset (&data, 0, sizeof (data)) ; Chris@40: if (fstat (fd, &data) != 0) Chris@40: return 0 ; Chris@40: Chris@40: return (sf_count_t) data.st_size ; Chris@40: } /* file_length_fd */ Chris@40: Chris@40: Chris@40: [+ ESAC +] Chris@40: