From eb14a3babb5b3a30b6bc19a76074f319976d4943 Mon Sep 17 00:00:00 2001 From: Seungbae Shin Date: Thu, 15 Jun 2017 17:28:03 +0900 Subject: [PATCH] Fix build error 1. Add python to BuildRequire 2. Add autogen generated test files Change-Id: I6ae51fbdd9064bc16ff247d2fd4c21fe47a1b742 --- packaging/libsndfile.spec | 1 + tests/benchmark.c | 545 +++++++++++++ tests/floating_point_test.c | 722 +++++++++++++++++ tests/pipe_test.c | 525 ++++++++++++ tests/rdwr_test.c | 240 ++++++ tests/scale_clip_test.c | 1853 +++++++++++++++++++++++++++++++++++++++++++ tests/utils.c | 1173 +++++++++++++++++++++++++++ tests/utils.h | 201 +++++ 8 files changed, 5260 insertions(+) create mode 100644 tests/benchmark.c create mode 100644 tests/floating_point_test.c create mode 100644 tests/pipe_test.c create mode 100644 tests/rdwr_test.c create mode 100644 tests/scale_clip_test.c create mode 100644 tests/utils.c create mode 100644 tests/utils.h diff --git a/packaging/libsndfile.spec b/packaging/libsndfile.spec index b6f1ab7..82d5cba 100644 --- a/packaging/libsndfile.spec +++ b/packaging/libsndfile.spec @@ -6,6 +6,7 @@ Summary: C library for reading and writing sound files Group: Multimedia/Audio BuildRequires: gcc-c++ BuildRequires: libtool +BuildRequires: python BuildRequires: pkg-config BuildRequires: pkgconfig(ogg) BuildRequires: pkgconfig(vorbis) diff --git a/tests/benchmark.c b/tests/benchmark.c new file mode 100644 index 0000000..4983125 --- /dev/null +++ b/tests/benchmark.c @@ -0,0 +1,545 @@ +/* +** Copyright (C) 2002-2012 Erik de Castro Lopo +** +** This program is free software; you can redistribute it and/or modify +** it under the terms of the GNU General Public License as published by +** the Free Software Foundation; either version 2 of the License, or +** (at your option) any later version. +** +** This program is distributed in the hope that it will be useful, +** but WITHOUT ANY WARRANTY; without even the implied warranty of +** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +** GNU General Public License for more details. +** +** You should have received a copy of the GNU General Public License +** along with this program; if not, write to the Free Software +** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +*/ + +#include "sfconfig.h" + +#include +#include + +#if HAVE_UNISTD_H +#include +#endif + +#if (HAVE_DECL_S_IRGRP == 0) +#include +#endif + +#include +#include +#include +#include +#include + +#include + +#ifndef M_PI +#define M_PI 3.14159265358979323846264338 +#endif + +/* +** Neat solution to the Win32/OS2 binary file flage requirement. +** If O_BINARY isn't already defined by the inclusion of the system +** headers, set it to zero. +*/ +#ifndef O_BINARY +#define O_BINARY 0 +#endif + +#define WRITE_FLAGS (O_WRONLY | O_CREAT | O_TRUNC | O_BINARY) +#define READ_FLAGS (O_RDONLY | O_BINARY) + +#if (defined (WIN32) || defined (_WIN32) || defined (__OS2__)) + #define WRITE_PERMS 0777 +#else + #define WRITE_PERMS (S_IRUSR | S_IWUSR | S_IRGRP) +#endif + +#define BUFFER_SIZE (1 << 18) +#define BLOCK_COUNT (30) +#define TEST_DURATION (5) /* 5 Seconds. */ + +typedef struct +{ double write_rate ; + double read_rate ; +} PERF_STATS ; + +static void *data = NULL ; + +static void calc_raw_performance (PERF_STATS *stats) ; + +static void calc_short_performance (int format, double read_rate, double write_rate) ; +static void calc_int_performance (int format, double read_rate, double write_rate) ; +static void calc_float_performance (int format, double read_rate, double write_rate) ; + + +static int cpu_is_big_endian (void) ; + +static const char* get_subtype_str (int subtype) ; + +int +main (int argc, char *argv []) +{ PERF_STATS stats ; + char buffer [256] = "Benchmarking " ; + int format_major ; + + if (! (data = malloc (BUFFER_SIZE * sizeof (double)))) + { perror ("Error : malloc failed") ; + exit (1) ; + } ; + + sf_command (NULL, SFC_GET_LIB_VERSION, buffer + strlen (buffer), sizeof (buffer) - strlen (buffer)) ; + + puts (buffer) ; + memset (buffer, '-', strlen (buffer)) ; + puts (buffer) ; + printf ("Each test takes a little over %d seconds.\n\n", TEST_DURATION) ; + + calc_raw_performance (&stats) ; + + if (argc < 2 || strcmp ("--native-only", argv [1]) == 0) + { puts ("\nNative endian I/O :") ; + format_major = cpu_is_big_endian () ? SF_FORMAT_AIFF : SF_FORMAT_WAV ; + + calc_short_performance (format_major | SF_FORMAT_PCM_16, stats.read_rate, stats.write_rate) ; + calc_int_performance (format_major | SF_FORMAT_PCM_24, stats.read_rate, stats.write_rate) ; + calc_int_performance (format_major | SF_FORMAT_PCM_32, stats.read_rate, stats.write_rate) ; + calc_float_performance (format_major | SF_FORMAT_PCM_16, stats.read_rate, stats.write_rate) ; + calc_float_performance (format_major | SF_FORMAT_PCM_24, stats.read_rate, stats.write_rate) ; + calc_float_performance (format_major | SF_FORMAT_PCM_32, stats.read_rate, stats.write_rate) ; + calc_float_performance (format_major | SF_FORMAT_FLOAT , stats.read_rate, stats.write_rate) ; + } ; + + if (argc < 2 || strcmp ("--swap-only", argv [1]) == 0) + { puts ("\nEndian swapped I/O :") ; + format_major = cpu_is_big_endian () ? SF_FORMAT_WAV : SF_FORMAT_AIFF ; + + calc_short_performance (format_major | SF_FORMAT_PCM_16, stats.read_rate, stats.write_rate) ; + calc_int_performance (format_major | SF_FORMAT_PCM_24, stats.read_rate, stats.write_rate) ; + calc_int_performance (format_major | SF_FORMAT_PCM_32, stats.read_rate, stats.write_rate) ; + calc_float_performance (format_major | SF_FORMAT_PCM_16, stats.read_rate, stats.write_rate) ; + calc_float_performance (format_major | SF_FORMAT_PCM_24, stats.read_rate, stats.write_rate) ; + calc_float_performance (format_major | SF_FORMAT_PCM_32, stats.read_rate, stats.write_rate) ; + calc_float_performance (format_major | SF_FORMAT_FLOAT , stats.read_rate, stats.write_rate) ; + } ; + + puts ("") ; + + free (data) ; + + return 0 ; +} /* main */ + +/*============================================================================== +*/ + +static void +calc_raw_performance (PERF_STATS *stats) +{ clock_t start_clock, clock_time ; + int fd, k, byte_count, retval, op_count ; + const char *filename ; + + filename = "benchmark.dat" ; + + byte_count = BUFFER_SIZE * sizeof (short) ; + + /* Collect write stats */ + printf (" Raw write PCM_16 : ") ; + fflush (stdout) ; + + clock_time = 0 ; + op_count = 0 ; + start_clock = clock () ; + + while (clock_time < (CLOCKS_PER_SEC * TEST_DURATION)) + { if ((fd = open (filename, WRITE_FLAGS, WRITE_PERMS)) < 0) + { printf ("Error : not able to open file : %s\n", filename) ; + perror ("") ; + exit (1) ; + } ; + + for (k = 0 ; k < BLOCK_COUNT ; k++) + { if ((retval = write (fd, data, byte_count)) != byte_count) + { printf ("Error : write returned %d (should have been %d)\n", retval, byte_count) ; + exit (1) ; + } ; + } ; + + close (fd) ; + + clock_time = clock () - start_clock ; + op_count ++ ; + } ; + + stats->write_rate = (1.0 * BUFFER_SIZE) * BLOCK_COUNT * op_count ; + stats->write_rate *= (1.0 * CLOCKS_PER_SEC) / clock_time ; + printf ("%10.0f samples per sec\n", stats->write_rate) ; + + /* Collect read stats */ + printf (" Raw read PCM_16 : ") ; + fflush (stdout) ; + + clock_time = 0 ; + op_count = 0 ; + start_clock = clock () ; + + while (clock_time < (CLOCKS_PER_SEC * TEST_DURATION)) + { if ((fd = open (filename, READ_FLAGS)) < 0) + { printf ("Error : not able to open file : %s\n", filename) ; + perror ("") ; + exit (1) ; + } ; + + for (k = 0 ; k < BLOCK_COUNT ; k++) + { if ((retval = read (fd, data, byte_count)) != byte_count) + { printf ("Error : write returned %d (should have been %d)\n", retval, byte_count) ; + exit (1) ; + } ; + } ; + + close (fd) ; + + clock_time = clock () - start_clock ; + op_count ++ ; + } ; + + stats->read_rate = (1.0 * BUFFER_SIZE) * BLOCK_COUNT * op_count ; + stats->read_rate *= (1.0 * CLOCKS_PER_SEC) / clock_time ; + printf ("%10.0f samples per sec\n", stats->read_rate) ; + + unlink (filename) ; +} /* calc_raw_performance */ + +/*------------------------------------------------------------------------------ +*/ + +static void +calc_short_performance (int format, double read_rate, double write_rate) +{ SNDFILE *file ; + SF_INFO sfinfo ; + clock_t start_clock, clock_time ; + double performance ; + int k, item_count, retval, op_count ; + const char* subtype ; + short *short_data ; + const char *filename ; + + filename = "benchmark.dat" ; + subtype = get_subtype_str (format & SF_FORMAT_SUBMASK) ; + + short_data = data ; + item_count = BUFFER_SIZE ; + for (k = 0 ; k < item_count ; k++) + short_data [k] = 32700.0 * sin (2 * M_PI * k / 32000.0) ; + + /* Collect write stats */ + printf (" Write %-5s to %s : ", "short", subtype) ; + fflush (stdout) ; + + sfinfo.channels = 1 ; + sfinfo.format = format ; + sfinfo.frames = 1 ; + sfinfo.samplerate = 32000 ; + + clock_time = 0 ; + op_count = 0 ; + start_clock = clock () ; + + while (clock_time < (CLOCKS_PER_SEC * TEST_DURATION)) + { if (! (file = sf_open (filename, SFM_WRITE, &sfinfo))) + { printf ("Error : not able to open file : %s\n", filename) ; + perror ("") ; + exit (1) ; + } ; + + /* Turn off the addition of a PEAK chunk. */ + sf_command (file, SFC_SET_ADD_PEAK_CHUNK, NULL, SF_FALSE) ; + + for (k = 0 ; k < BLOCK_COUNT ; k++) + { if ((retval = sf_write_short (file, short_data, item_count)) != item_count) + { printf ("Error : sf_write_short returned %d (should have been %d)\n", retval, item_count) ; + exit (1) ; + } ; + } ; + + sf_close (file) ; + + clock_time = clock () - start_clock ; + op_count ++ ; + } ; + + performance = (1.0 * BUFFER_SIZE) * BLOCK_COUNT * op_count ; + performance *= (1.0 * CLOCKS_PER_SEC) / clock_time ; + printf ("%6.2f%% of raw write\n", 100.0 * performance / write_rate) ; + + /* Collect read stats */ + printf (" Read %-5s from %s : ", "short", subtype) ; + fflush (stdout) ; + + clock_time = 0 ; + op_count = 0 ; + start_clock = clock () ; + + while (clock_time < (CLOCKS_PER_SEC * TEST_DURATION)) + { if (! (file = sf_open (filename, SFM_READ, &sfinfo))) + { printf ("Error : not able to open file : %s\n", filename) ; + perror ("") ; + exit (1) ; + } ; + + for (k = 0 ; k < BLOCK_COUNT ; k++) + { if ((retval = sf_read_short (file, short_data, item_count)) != item_count) + { printf ("Error : write returned %d (should have been %d)\n", retval, item_count) ; + exit (1) ; + } ; + } ; + + sf_close (file) ; + + clock_time = clock () - start_clock ; + op_count ++ ; + } ; + + performance = (1.0 * item_count) * BLOCK_COUNT * op_count ; + performance *= (1.0 * CLOCKS_PER_SEC) / clock_time ; + printf ("%6.2f%% of raw read\n", 100.0 * performance / read_rate) ; + + unlink (filename) ; + +} /* calc_short_performance */ +static void +calc_int_performance (int format, double read_rate, double write_rate) +{ SNDFILE *file ; + SF_INFO sfinfo ; + clock_t start_clock, clock_time ; + double performance ; + int k, item_count, retval, op_count ; + const char* subtype ; + int *int_data ; + const char *filename ; + + filename = "benchmark.dat" ; + subtype = get_subtype_str (format & SF_FORMAT_SUBMASK) ; + + int_data = data ; + item_count = BUFFER_SIZE ; + for (k = 0 ; k < item_count ; k++) + int_data [k] = 32700.0 * (1 << 16) * sin (2 * M_PI * k / 32000.0) ; + + /* Collect write stats */ + printf (" Write %-5s to %s : ", "int", subtype) ; + fflush (stdout) ; + + sfinfo.channels = 1 ; + sfinfo.format = format ; + sfinfo.frames = 1 ; + sfinfo.samplerate = 32000 ; + + clock_time = 0 ; + op_count = 0 ; + start_clock = clock () ; + + while (clock_time < (CLOCKS_PER_SEC * TEST_DURATION)) + { if (! (file = sf_open (filename, SFM_WRITE, &sfinfo))) + { printf ("Error : not able to open file : %s\n", filename) ; + perror ("") ; + exit (1) ; + } ; + + /* Turn off the addition of a PEAK chunk. */ + sf_command (file, SFC_SET_ADD_PEAK_CHUNK, NULL, SF_FALSE) ; + + for (k = 0 ; k < BLOCK_COUNT ; k++) + { if ((retval = sf_write_int (file, int_data, item_count)) != item_count) + { printf ("Error : sf_write_short returned %d (should have been %d)\n", retval, item_count) ; + exit (1) ; + } ; + } ; + + sf_close (file) ; + + clock_time = clock () - start_clock ; + op_count ++ ; + } ; + + performance = (1.0 * BUFFER_SIZE) * BLOCK_COUNT * op_count ; + performance *= (1.0 * CLOCKS_PER_SEC) / clock_time ; + printf ("%6.2f%% of raw write\n", 100.0 * performance / write_rate) ; + + /* Collect read stats */ + printf (" Read %-5s from %s : ", "int", subtype) ; + fflush (stdout) ; + + clock_time = 0 ; + op_count = 0 ; + start_clock = clock () ; + + while (clock_time < (CLOCKS_PER_SEC * TEST_DURATION)) + { if (! (file = sf_open (filename, SFM_READ, &sfinfo))) + { printf ("Error : not able to open file : %s\n", filename) ; + perror ("") ; + exit (1) ; + } ; + + for (k = 0 ; k < BLOCK_COUNT ; k++) + { if ((retval = sf_read_int (file, int_data, item_count)) != item_count) + { printf ("Error : write returned %d (should have been %d)\n", retval, item_count) ; + exit (1) ; + } ; + } ; + + sf_close (file) ; + + clock_time = clock () - start_clock ; + op_count ++ ; + } ; + + performance = (1.0 * item_count) * BLOCK_COUNT * op_count ; + performance *= (1.0 * CLOCKS_PER_SEC) / clock_time ; + printf ("%6.2f%% of raw read\n", 100.0 * performance / read_rate) ; + + unlink (filename) ; + +} /* calc_int_performance */ +static void +calc_float_performance (int format, double read_rate, double write_rate) +{ SNDFILE *file ; + SF_INFO sfinfo ; + clock_t start_clock, clock_time ; + double performance ; + int k, item_count, retval, op_count ; + const char* subtype ; + float *float_data ; + const char *filename ; + + filename = "benchmark.dat" ; + subtype = get_subtype_str (format & SF_FORMAT_SUBMASK) ; + + float_data = data ; + item_count = BUFFER_SIZE ; + for (k = 0 ; k < item_count ; k++) + float_data [k] = 1.0 * sin (2 * M_PI * k / 32000.0) ; + + /* Collect write stats */ + printf (" Write %-5s to %s : ", "float", subtype) ; + fflush (stdout) ; + + sfinfo.channels = 1 ; + sfinfo.format = format ; + sfinfo.frames = 1 ; + sfinfo.samplerate = 32000 ; + + clock_time = 0 ; + op_count = 0 ; + start_clock = clock () ; + + while (clock_time < (CLOCKS_PER_SEC * TEST_DURATION)) + { if (! (file = sf_open (filename, SFM_WRITE, &sfinfo))) + { printf ("Error : not able to open file : %s\n", filename) ; + perror ("") ; + exit (1) ; + } ; + + /* Turn off the addition of a PEAK chunk. */ + sf_command (file, SFC_SET_ADD_PEAK_CHUNK, NULL, SF_FALSE) ; + + for (k = 0 ; k < BLOCK_COUNT ; k++) + { if ((retval = sf_write_float (file, float_data, item_count)) != item_count) + { printf ("Error : sf_write_short returned %d (should have been %d)\n", retval, item_count) ; + exit (1) ; + } ; + } ; + + sf_close (file) ; + + clock_time = clock () - start_clock ; + op_count ++ ; + } ; + + performance = (1.0 * BUFFER_SIZE) * BLOCK_COUNT * op_count ; + performance *= (1.0 * CLOCKS_PER_SEC) / clock_time ; + printf ("%6.2f%% of raw write\n", 100.0 * performance / write_rate) ; + + /* Collect read stats */ + printf (" Read %-5s from %s : ", "float", subtype) ; + fflush (stdout) ; + + clock_time = 0 ; + op_count = 0 ; + start_clock = clock () ; + + while (clock_time < (CLOCKS_PER_SEC * TEST_DURATION)) + { if (! (file = sf_open (filename, SFM_READ, &sfinfo))) + { printf ("Error : not able to open file : %s\n", filename) ; + perror ("") ; + exit (1) ; + } ; + + for (k = 0 ; k < BLOCK_COUNT ; k++) + { if ((retval = sf_read_float (file, float_data, item_count)) != item_count) + { printf ("Error : write returned %d (should have been %d)\n", retval, item_count) ; + exit (1) ; + } ; + } ; + + sf_close (file) ; + + clock_time = clock () - start_clock ; + op_count ++ ; + } ; + + performance = (1.0 * item_count) * BLOCK_COUNT * op_count ; + performance *= (1.0 * CLOCKS_PER_SEC) / clock_time ; + printf ("%6.2f%% of raw read\n", 100.0 * performance / read_rate) ; + + unlink (filename) ; + +} /* calc_float_performance */ + + +/*============================================================================== +*/ + +static int +cpu_is_big_endian (void) +{ unsigned char *cptr ; + int endtest ; + + endtest = 0x12345678 ; + + cptr = (unsigned char*) (&endtest) ; + + if (cptr [0] == 0x12 && cptr [1] == 0x34 && cptr [3] == 0x78) + return SF_TRUE ; + + return SF_FALSE ; +} /* cpu_is_big_endian */ + +static const char* +get_subtype_str (int subtype) +{ switch (subtype) + { case SF_FORMAT_PCM_16 : + return "PCM_16" ; + + case SF_FORMAT_PCM_24 : + return "PCM_24" ; + + case SF_FORMAT_PCM_32 : + return "PCM_32" ; + + case SF_FORMAT_FLOAT : + return "FLOAT " ; + + case SF_FORMAT_DOUBLE : + return "DOUBLE" ; + + default : break ; + } ; + + return "UNKNOWN" ; +} /* get_subtype_str */ + diff --git a/tests/floating_point_test.c b/tests/floating_point_test.c new file mode 100644 index 0000000..7ef0fad --- /dev/null +++ b/tests/floating_point_test.c @@ -0,0 +1,722 @@ +/* +** Copyright (C) 1999-2016 Erik de Castro Lopo +** +** This program is free software; you can redistribute it and/or modify +** it under the terms of the GNU General Public License as published by +** the Free Software Foundation; either version 2 of the License, or +** (at your option) any later version. +** +** This program is distributed in the hope that it will be useful, +** but WITHOUT ANY WARRANTY; without even the implied warranty of +** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +** GNU General Public License for more details. +** +** You should have received a copy of the GNU General Public License +** along with this program; if not, write to the Free Software +** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +*/ + +#include "sfconfig.h" + +#include +#include +#include +#include +#include + +#if HAVE_UNISTD_H +#include +#endif + +#include + +#include "dft_cmp.h" +#include "utils.h" + +#define SAMPLE_RATE 16000 + +static void float_scaled_test (const char *filename, int allow_exit, int replace_float, int filetype, double target_snr) ; +static void double_scaled_test (const char *filename, int allow_exit, int replace_float, int filetype, double target_snr) ; + +static void float_short_little_test (const char * filename) ; +static void float_short_big_test (const char * filename) ; +static void float_int_little_test (const char * filename) ; +static void float_int_big_test (const char * filename) ; +static void double_short_little_test (const char * filename) ; +static void double_short_big_test (const char * filename) ; +static void double_int_little_test (const char * filename) ; +static void double_int_big_test (const char * filename) ; + + +static double double_data [DFT_DATA_LENGTH] ; +static double double_test [DFT_DATA_LENGTH] ; + +static float float_data [DFT_DATA_LENGTH] ; +static float float_test [DFT_DATA_LENGTH] ; + +static double double_data [DFT_DATA_LENGTH] ; +static short short_data [DFT_DATA_LENGTH] ; +static int int_data [DFT_DATA_LENGTH] ; + +int +main (int argc, char *argv []) +{ int allow_exit = 1 ; + + if (argc == 2 && ! strstr (argv [1], "no-exit")) + allow_exit = 0 ; + +#if (HAVE_LRINTF == 0) + puts ("*** Cannot run this test on this platform because it lacks lrintf().") ; + exit (0) ; +#endif + + /* Float tests. */ + float_scaled_test ("float.raw", allow_exit, SF_FALSE, SF_ENDIAN_LITTLE | SF_FORMAT_RAW | SF_FORMAT_FLOAT, + OS_IS_OPENBSD ? -98.0 : -163.0) ; + + /* Test both signed and unsigned 8 bit files. */ + float_scaled_test ("pcm_s8.raw", allow_exit, SF_FALSE, SF_FORMAT_RAW | SF_FORMAT_PCM_S8, -39.0) ; + float_scaled_test ("pcm_u8.raw", allow_exit, SF_FALSE, SF_FORMAT_RAW | SF_FORMAT_PCM_U8, -39.0) ; + + float_scaled_test ("pcm_16.raw", allow_exit, SF_FALSE, SF_ENDIAN_BIG | SF_FORMAT_RAW | SF_FORMAT_PCM_16, -87.0) ; + float_scaled_test ("pcm_24.raw", allow_exit, SF_FALSE, SF_ENDIAN_LITTLE | SF_FORMAT_RAW | SF_FORMAT_PCM_24, -138.0) ; + float_scaled_test ("pcm_32.raw", allow_exit, SF_FALSE, SF_ENDIAN_BIG | SF_FORMAT_RAW | SF_FORMAT_PCM_32, -163.0) ; + + float_scaled_test ("ulaw.raw", allow_exit, SF_FALSE, SF_FORMAT_RAW | SF_FORMAT_ULAW, -50.0) ; + float_scaled_test ("alaw.raw", allow_exit, SF_FALSE, SF_FORMAT_RAW | SF_FORMAT_ALAW, -49.0) ; + + float_scaled_test ("ima_adpcm.wav", allow_exit, SF_FALSE, SF_FORMAT_WAV | SF_FORMAT_IMA_ADPCM, -47.0) ; + float_scaled_test ("ms_adpcm.wav" , allow_exit, SF_FALSE, SF_FORMAT_WAV | SF_FORMAT_MS_ADPCM, -40.0) ; + float_scaled_test ("gsm610.raw" , allow_exit, SF_FALSE, SF_FORMAT_RAW | SF_FORMAT_GSM610, -33.0) ; + + float_scaled_test ("g721_32.au", allow_exit, SF_FALSE, SF_FORMAT_AU | SF_FORMAT_G721_32, -32.3) ; + float_scaled_test ("g723_24.au", allow_exit, SF_FALSE, SF_FORMAT_AU | SF_FORMAT_G723_24, -32.3) ; + float_scaled_test ("g723_40.au", allow_exit, SF_FALSE, SF_FORMAT_AU | SF_FORMAT_G723_40, -40.0) ; + + /* PAF files do not use the same encoding method for 24 bit PCM data as other file + ** formats so we need to explicitly test it here. + */ + float_scaled_test ("le_paf_24.paf", allow_exit, SF_FALSE, SF_ENDIAN_LITTLE | SF_FORMAT_PAF | SF_FORMAT_PCM_24, -149.0) ; + float_scaled_test ("be_paf_24.paf", allow_exit, SF_FALSE, SF_ENDIAN_BIG | SF_FORMAT_PAF | SF_FORMAT_PCM_24, -149.0) ; + + float_scaled_test ("dwvw_12.raw", allow_exit, SF_FALSE, SF_FORMAT_RAW | SF_FORMAT_DWVW_12, -64.0) ; + float_scaled_test ("dwvw_16.raw", allow_exit, SF_FALSE, SF_FORMAT_RAW | SF_FORMAT_DWVW_16, -92.0) ; + float_scaled_test ("dwvw_24.raw", allow_exit, SF_FALSE, SF_FORMAT_RAW | SF_FORMAT_DWVW_24, -151.0) ; + + float_scaled_test ("adpcm.vox", allow_exit, SF_FALSE, SF_FORMAT_RAW | SF_FORMAT_VOX_ADPCM, -40.0) ; + + float_scaled_test ("dpcm_16.xi", allow_exit, SF_FALSE, SF_FORMAT_XI | SF_FORMAT_DPCM_16, -90.0) ; + float_scaled_test ("dpcm_8.xi" , allow_exit, SF_FALSE, SF_FORMAT_XI | SF_FORMAT_DPCM_8 , -41.0) ; + + float_scaled_test ("pcm_s8.sds", allow_exit, SF_FALSE, SF_FORMAT_SDS | SF_FORMAT_PCM_S8, -89.0) ; + float_scaled_test ("pcm_16.sds", allow_exit, SF_FALSE, SF_FORMAT_SDS | SF_FORMAT_PCM_16, -132.0) ; + float_scaled_test ("pcm_24.sds", allow_exit, SF_FALSE, SF_FORMAT_SDS | SF_FORMAT_PCM_24, -170.0) ; + + float_scaled_test ("alac_16.caf", allow_exit, SF_FALSE, SF_FORMAT_CAF | SF_FORMAT_ALAC_16, -90.0) ; + float_scaled_test ("alac_32.caf", allow_exit, SF_FALSE, SF_FORMAT_CAF | SF_FORMAT_ALAC_32, -76.0) ; + float_scaled_test ("alac_24.caf", allow_exit, SF_FALSE, SF_FORMAT_CAF | SF_FORMAT_ALAC_24, -153.0) ; + float_scaled_test ("alac_20.caf", allow_exit, SF_FALSE, SF_FORMAT_CAF | SF_FORMAT_ALAC_20, -125.0) ; + +#if HAVE_EXTERNAL_XIPH_LIBS + float_scaled_test ("flac_8.flac", allow_exit, SF_FALSE, SF_FORMAT_FLAC | SF_FORMAT_PCM_S8, -39.0) ; + float_scaled_test ("flac_16.flac", allow_exit, SF_FALSE, SF_FORMAT_FLAC | SF_FORMAT_PCM_16, -87.0) ; + float_scaled_test ("flac_24.flac", allow_exit, SF_FALSE, SF_FORMAT_FLAC | SF_FORMAT_PCM_24, -138.0) ; + + float_scaled_test ("vorbis.oga", allow_exit, SF_FALSE, SF_FORMAT_OGG | SF_FORMAT_VORBIS, -31.0) ; +#endif + + float_scaled_test ("replace_float.raw", allow_exit, SF_TRUE, SF_ENDIAN_LITTLE | SF_FORMAT_RAW | SF_FORMAT_FLOAT, -163.0) ; + + /*============================================================================== + ** Double tests. + */ + + double_scaled_test ("double.raw", allow_exit, SF_FALSE, SF_FORMAT_RAW | SF_FORMAT_DOUBLE, -201.0) ; + + /* Test both signed (AIFF) and unsigned (WAV) 8 bit files. */ + double_scaled_test ("pcm_s8.raw", allow_exit, SF_FALSE, SF_FORMAT_RAW | SF_FORMAT_PCM_S8, -39.0) ; + double_scaled_test ("pcm_u8.raw", allow_exit, SF_FALSE, SF_FORMAT_RAW | SF_FORMAT_PCM_U8, -39.0) ; + + double_scaled_test ("pcm_16.raw", allow_exit, SF_FALSE, SF_FORMAT_RAW | SF_FORMAT_PCM_16, -87.0) ; + double_scaled_test ("pcm_24.raw", allow_exit, SF_FALSE, SF_FORMAT_RAW | SF_FORMAT_PCM_24, -135.0) ; + double_scaled_test ("pcm_32.raw", allow_exit, SF_FALSE, SF_FORMAT_RAW | SF_FORMAT_PCM_32, -184.0) ; + + double_scaled_test ("ulaw.raw", allow_exit, SF_FALSE, SF_FORMAT_RAW | SF_FORMAT_ULAW, -50.0) ; + double_scaled_test ("alaw.raw", allow_exit, SF_FALSE, SF_FORMAT_RAW | SF_FORMAT_ALAW, -49.0) ; + + double_scaled_test ("ima_adpcm.wav", allow_exit, SF_FALSE, SF_FORMAT_WAV | SF_FORMAT_IMA_ADPCM, -47.0) ; + double_scaled_test ("ms_adpcm.wav" , allow_exit, SF_FALSE, SF_FORMAT_WAV | SF_FORMAT_MS_ADPCM, -40.0) ; + double_scaled_test ("gsm610.raw" , allow_exit, SF_FALSE, SF_FORMAT_RAW | SF_FORMAT_GSM610, -33.0) ; + + double_scaled_test ("g721_32.au", allow_exit, SF_FALSE, SF_FORMAT_AU | SF_FORMAT_G721_32, -32.3) ; + double_scaled_test ("g723_24.au", allow_exit, SF_FALSE, SF_FORMAT_AU | SF_FORMAT_G723_24, -32.3) ; + double_scaled_test ("g723_40.au", allow_exit, SF_FALSE, SF_FORMAT_AU | SF_FORMAT_G723_40, -40.0) ; + + /* 24 bit PCM PAF files tested here. */ + double_scaled_test ("be_paf_24.paf", allow_exit, SF_FALSE, SF_ENDIAN_BIG | SF_FORMAT_PAF | SF_FORMAT_PCM_24, -151.0) ; + double_scaled_test ("le_paf_24.paf", allow_exit, SF_FALSE, SF_ENDIAN_LITTLE | SF_FORMAT_PAF | SF_FORMAT_PCM_24, -151.0) ; + + double_scaled_test ("dwvw_12.raw", allow_exit, SF_FALSE, SF_FORMAT_RAW | SF_FORMAT_DWVW_12, -64.0) ; + double_scaled_test ("dwvw_16.raw", allow_exit, SF_FALSE, SF_FORMAT_RAW | SF_FORMAT_DWVW_16, -92.0) ; + double_scaled_test ("dwvw_24.raw", allow_exit, SF_FALSE, SF_FORMAT_RAW | SF_FORMAT_DWVW_24, -151.0) ; + + double_scaled_test ("adpcm.vox" , allow_exit, SF_FALSE, SF_FORMAT_RAW | SF_FORMAT_VOX_ADPCM, -40.0) ; + + double_scaled_test ("dpcm_16.xi", allow_exit, SF_FALSE, SF_FORMAT_XI | SF_FORMAT_DPCM_16, -90.0) ; + double_scaled_test ("dpcm_8.xi" , allow_exit, SF_FALSE, SF_FORMAT_XI | SF_FORMAT_DPCM_8 , -41.0) ; + + double_scaled_test ("pcm_s8.sds", allow_exit, SF_FALSE, SF_FORMAT_SDS | SF_FORMAT_PCM_S8, -89.0) ; + double_scaled_test ("pcm_16.sds", allow_exit, SF_FALSE, SF_FORMAT_SDS | SF_FORMAT_PCM_16, -132.0) ; + double_scaled_test ("pcm_24.sds", allow_exit, SF_FALSE, SF_FORMAT_SDS | SF_FORMAT_PCM_24, -180.0) ; + + double_scaled_test ("alac_16.caf", allow_exit, SF_FALSE, SF_FORMAT_CAF | SF_FORMAT_ALAC_16, -90.0) ; + double_scaled_test ("alac_20.caf", allow_exit, SF_FALSE, SF_FORMAT_CAF | SF_FORMAT_ALAC_20, -125.0) ; + double_scaled_test ("alac_24.caf", allow_exit, SF_FALSE, SF_FORMAT_CAF | SF_FORMAT_ALAC_24, -153.0) ; + double_scaled_test ("alac_32.caf", allow_exit, SF_FALSE, SF_FORMAT_CAF | SF_FORMAT_ALAC_32, -186.0) ; + +#if HAVE_EXTERNAL_XIPH_LIBS + double_scaled_test ("flac_8.flac", allow_exit, SF_FALSE, SF_FORMAT_FLAC | SF_FORMAT_PCM_S8, -39.0) ; + double_scaled_test ("flac_16.flac", allow_exit, SF_FALSE, SF_FORMAT_FLAC | SF_FORMAT_PCM_16, -87.0) ; + double_scaled_test ("flac_24.flac", allow_exit, SF_FALSE, SF_FORMAT_FLAC | SF_FORMAT_PCM_24, -138.0) ; + + double_scaled_test ("vorbis.oga", allow_exit, SF_FALSE, SF_FORMAT_OGG | SF_FORMAT_VORBIS, -29.0) ; +#endif + + double_scaled_test ("replace_double.raw", allow_exit, SF_TRUE, SF_FORMAT_RAW | SF_FORMAT_DOUBLE, -201.0) ; + + putchar ('\n') ; + /* Float int tests. */ + float_short_little_test ("float_short_little.au") ; + float_short_big_test ("float_short_big.au") ; + float_int_little_test ("float_int_little.au") ; + float_int_big_test ("float_int_big.au") ; + double_short_little_test ("double_short_little.au") ; + double_short_big_test ("double_short_big.au") ; + double_int_little_test ("double_int_little.au") ; + double_int_big_test ("double_int_big.au") ; + + + return 0 ; +} /* main */ + +/*============================================================================================ + * Here are the test functions. + */ + +static void +float_scaled_test (const char *filename, int allow_exit, int replace_float, int filetype, double target_snr) +{ SNDFILE *file ; + SF_INFO sfinfo ; + double snr ; + int byterate ; + + print_test_name ("float_scaled_test", filename) ; + + gen_windowed_sine_float (float_data, DFT_DATA_LENGTH, 0.9999) ; + + sfinfo.samplerate = SAMPLE_RATE ; + sfinfo.frames = DFT_DATA_LENGTH ; + sfinfo.channels = 1 ; + sfinfo.format = filetype ; + + file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ; + sf_command (file, SFC_TEST_IEEE_FLOAT_REPLACE, NULL, replace_float) ; + + test_write_float_or_die (file, 0, float_data, DFT_DATA_LENGTH, __LINE__) ; + + sf_close (file) ; + + memset (float_test, 0, sizeof (float_test)) ; + + file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ; + sf_command (file, SFC_TEST_IEEE_FLOAT_REPLACE, NULL, replace_float) ; + + exit_if_true (sfinfo.format != filetype, "\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, filetype, sfinfo.format) ; + exit_if_true (sfinfo.frames < DFT_DATA_LENGTH, "\n\nLine %d: Incorrect number of frames in file (too short). (%" PRId64 " should be %d)\n", __LINE__, sfinfo.frames, DFT_DATA_LENGTH) ; + exit_if_true (sfinfo.channels != 1, "\n\nLine %d: Incorrect number of channels in file.\n", __LINE__) ; + + check_log_buffer_or_die (file, __LINE__) ; + + test_read_float_or_die (file, 0, float_test, DFT_DATA_LENGTH, __LINE__) ; + + byterate = sf_current_byterate (file) ; + exit_if_true (byterate <= 0, "\n\nLine %d: byterate is zero.\n", __LINE__) ; + + sf_close (file) ; + + snr = dft_cmp_float (__LINE__, float_data, float_test, DFT_DATA_LENGTH, target_snr, allow_exit) ; + + exit_if_true (snr > target_snr, "% 6.1fdB SNR\n\n Error : should be better than % 6.1fdB\n\n", snr, target_snr) ; + + printf ("% 6.1fdB SNR ... ok\n", snr) ; + + unlink (filename) ; + + return ; +} /* float_scaled_test */ + +static void +double_scaled_test (const char *filename, int allow_exit, int replace_float, int filetype, double target_snr) +{ SNDFILE *file ; + SF_INFO sfinfo ; + double snr ; + int byterate ; + + print_test_name ("double_scaled_test", filename) ; + + gen_windowed_sine_double (double_data, DFT_DATA_LENGTH, 0.9999) ; + + sfinfo.samplerate = SAMPLE_RATE ; + sfinfo.frames = DFT_DATA_LENGTH ; + sfinfo.channels = 1 ; + sfinfo.format = filetype ; + + file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ; + sf_command (file, SFC_TEST_IEEE_FLOAT_REPLACE, NULL, replace_float) ; + + test_write_double_or_die (file, 0, double_data, DFT_DATA_LENGTH, __LINE__) ; + + sf_close (file) ; + + memset (double_test, 0, sizeof (double_test)) ; + + file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ; + sf_command (file, SFC_TEST_IEEE_FLOAT_REPLACE, NULL, replace_float) ; + + exit_if_true (sfinfo.format != filetype, "\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, filetype, sfinfo.format) ; + exit_if_true (sfinfo.frames < DFT_DATA_LENGTH, "\n\nLine %d: Incorrect number of frames in file (too short). (%" PRId64 " should be %d)\n", __LINE__, sfinfo.frames, DFT_DATA_LENGTH) ; + exit_if_true (sfinfo.channels != 1, "\n\nLine %d: Incorrect number of channels in file.\n", __LINE__) ; + + check_log_buffer_or_die (file, __LINE__) ; + + test_read_double_or_die (file, 0, double_test, DFT_DATA_LENGTH, __LINE__) ; + + byterate = sf_current_byterate (file) ; + exit_if_true (byterate <= 0, "\n\nLine %d: byterate is zero.\n", __LINE__) ; + + sf_close (file) ; + + snr = dft_cmp_double (__LINE__, double_data, double_test, DFT_DATA_LENGTH, target_snr, allow_exit) ; + + exit_if_true (snr > target_snr, "% 6.1fdB SNR\n\n Error : should be better than % 6.1fdB\n\n", snr, target_snr) ; + + printf ("% 6.1fdB SNR ... ok\n", snr) ; + + unlink (filename) ; + + return ; +} /* double_scaled_test */ + +/*============================================================================== +*/ + + +static void +float_short_little_test (const char * filename) +{ SNDFILE *file ; + SF_INFO sfinfo ; + int max ; + unsigned k ; + + print_test_name ("float_short_little_test", filename) ; + + gen_windowed_sine_float (float_data, ARRAY_LEN (float_data), 0.9999) ; + + sfinfo.samplerate = SAMPLE_RATE ; + sfinfo.frames = ARRAY_LEN (short_data) ; + sfinfo.channels = 1 ; + sfinfo.format = SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_FLOAT ; + + file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ; + test_write_float_or_die (file, 0, float_data, ARRAY_LEN (float_data), __LINE__) ; + sf_close (file) ; + + file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ; + + if (sfinfo.frames != ARRAY_LEN (float_data)) + { printf ("\n\nLine %d: Incorrect number of frames in file (too short). (%" PRId64 " should be %d)\n", __LINE__, sfinfo.frames, DFT_DATA_LENGTH) ; + exit (1) ; + } ; + + if (sfinfo.channels != 1) + { printf ("\n\nLine %d: Incorrect number of channels in file.\n", __LINE__) ; + exit (1) ; + } ; + + sf_command (file, SFC_SET_SCALE_FLOAT_INT_READ, NULL, SF_TRUE) ; + + test_read_short_or_die (file, 0, short_data, ARRAY_LEN (short_data), __LINE__) ; + sf_close (file) ; + + max = 0 ; + for (k = 0 ; k < ARRAY_LEN (short_data) ; k++) + if (abs (short_data [k]) > max) + max = abs (short_data [k]) ; + + if (1.0 * abs (max - 0x7FFF) / 0x7FFF > 0.01) + { printf ("\n\nLine %d: Bad maximum (%d should be %d).\n\n", __LINE__, max, 0x7FFF) ; + exit (1) ; + } ; + + unlink (filename) ; + puts ("ok") ; +} /* float_short_little_test */ + +static void +float_short_big_test (const char * filename) +{ SNDFILE *file ; + SF_INFO sfinfo ; + int max ; + unsigned k ; + + print_test_name ("float_short_big_test", filename) ; + + gen_windowed_sine_float (float_data, ARRAY_LEN (float_data), 0.9999) ; + + sfinfo.samplerate = SAMPLE_RATE ; + sfinfo.frames = ARRAY_LEN (short_data) ; + sfinfo.channels = 1 ; + sfinfo.format = SF_ENDIAN_BIG | SF_FORMAT_AU | SF_FORMAT_FLOAT ; + + file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ; + test_write_float_or_die (file, 0, float_data, ARRAY_LEN (float_data), __LINE__) ; + sf_close (file) ; + + file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ; + + if (sfinfo.frames != ARRAY_LEN (float_data)) + { printf ("\n\nLine %d: Incorrect number of frames in file (too short). (%" PRId64 " should be %d)\n", __LINE__, sfinfo.frames, DFT_DATA_LENGTH) ; + exit (1) ; + } ; + + if (sfinfo.channels != 1) + { printf ("\n\nLine %d: Incorrect number of channels in file.\n", __LINE__) ; + exit (1) ; + } ; + + sf_command (file, SFC_SET_SCALE_FLOAT_INT_READ, NULL, SF_TRUE) ; + + test_read_short_or_die (file, 0, short_data, ARRAY_LEN (short_data), __LINE__) ; + sf_close (file) ; + + max = 0 ; + for (k = 0 ; k < ARRAY_LEN (short_data) ; k++) + if (abs (short_data [k]) > max) + max = abs (short_data [k]) ; + + if (1.0 * abs (max - 0x7FFF) / 0x7FFF > 0.01) + { printf ("\n\nLine %d: Bad maximum (%d should be %d).\n\n", __LINE__, max, 0x7FFF) ; + exit (1) ; + } ; + + unlink (filename) ; + puts ("ok") ; +} /* float_short_big_test */ + +static void +float_int_little_test (const char * filename) +{ SNDFILE *file ; + SF_INFO sfinfo ; + int max ; + unsigned k ; + + print_test_name ("float_int_little_test", filename) ; + + gen_windowed_sine_float (float_data, ARRAY_LEN (float_data), 0.9999) ; + + sfinfo.samplerate = SAMPLE_RATE ; + sfinfo.frames = ARRAY_LEN (int_data) ; + sfinfo.channels = 1 ; + sfinfo.format = SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_FLOAT ; + + file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ; + test_write_float_or_die (file, 0, float_data, ARRAY_LEN (float_data), __LINE__) ; + sf_close (file) ; + + file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ; + + if (sfinfo.frames != ARRAY_LEN (float_data)) + { printf ("\n\nLine %d: Incorrect number of frames in file (too short). (%" PRId64 " should be %d)\n", __LINE__, sfinfo.frames, DFT_DATA_LENGTH) ; + exit (1) ; + } ; + + if (sfinfo.channels != 1) + { printf ("\n\nLine %d: Incorrect number of channels in file.\n", __LINE__) ; + exit (1) ; + } ; + + sf_command (file, SFC_SET_SCALE_FLOAT_INT_READ, NULL, SF_TRUE) ; + + test_read_int_or_die (file, 0, int_data, ARRAY_LEN (int_data), __LINE__) ; + sf_close (file) ; + + max = 0 ; + for (k = 0 ; k < ARRAY_LEN (int_data) ; k++) + if (abs (int_data [k]) > max) + max = abs (int_data [k]) ; + + if (1.0 * abs (max - 0x7FFFFFFF) / 0x7FFFFFFF > 0.01) + { printf ("\n\nLine %d: Bad maximum (%d should be %d).\n\n", __LINE__, max, 0x7FFFFFFF) ; + exit (1) ; + } ; + + unlink (filename) ; + puts ("ok") ; +} /* float_int_little_test */ + +static void +float_int_big_test (const char * filename) +{ SNDFILE *file ; + SF_INFO sfinfo ; + int max ; + unsigned k ; + + print_test_name ("float_int_big_test", filename) ; + + gen_windowed_sine_float (float_data, ARRAY_LEN (float_data), 0.9999) ; + + sfinfo.samplerate = SAMPLE_RATE ; + sfinfo.frames = ARRAY_LEN (int_data) ; + sfinfo.channels = 1 ; + sfinfo.format = SF_ENDIAN_BIG | SF_FORMAT_AU | SF_FORMAT_FLOAT ; + + file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ; + test_write_float_or_die (file, 0, float_data, ARRAY_LEN (float_data), __LINE__) ; + sf_close (file) ; + + file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ; + + if (sfinfo.frames != ARRAY_LEN (float_data)) + { printf ("\n\nLine %d: Incorrect number of frames in file (too short). (%" PRId64 " should be %d)\n", __LINE__, sfinfo.frames, DFT_DATA_LENGTH) ; + exit (1) ; + } ; + + if (sfinfo.channels != 1) + { printf ("\n\nLine %d: Incorrect number of channels in file.\n", __LINE__) ; + exit (1) ; + } ; + + sf_command (file, SFC_SET_SCALE_FLOAT_INT_READ, NULL, SF_TRUE) ; + + test_read_int_or_die (file, 0, int_data, ARRAY_LEN (int_data), __LINE__) ; + sf_close (file) ; + + max = 0 ; + for (k = 0 ; k < ARRAY_LEN (int_data) ; k++) + if (abs (int_data [k]) > max) + max = abs (int_data [k]) ; + + if (1.0 * abs (max - 0x7FFFFFFF) / 0x7FFFFFFF > 0.01) + { printf ("\n\nLine %d: Bad maximum (%d should be %d).\n\n", __LINE__, max, 0x7FFFFFFF) ; + exit (1) ; + } ; + + unlink (filename) ; + puts ("ok") ; +} /* float_int_big_test */ + +static void +double_short_little_test (const char * filename) +{ SNDFILE *file ; + SF_INFO sfinfo ; + int max ; + unsigned k ; + + print_test_name ("double_short_little_test", filename) ; + + gen_windowed_sine_double (double_data, ARRAY_LEN (double_data), 0.9999) ; + + sfinfo.samplerate = SAMPLE_RATE ; + sfinfo.frames = ARRAY_LEN (short_data) ; + sfinfo.channels = 1 ; + sfinfo.format = SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_DOUBLE ; + + file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ; + test_write_double_or_die (file, 0, double_data, ARRAY_LEN (double_data), __LINE__) ; + sf_close (file) ; + + file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ; + + if (sfinfo.frames != ARRAY_LEN (double_data)) + { printf ("\n\nLine %d: Incorrect number of frames in file (too short). (%" PRId64 " should be %d)\n", __LINE__, sfinfo.frames, DFT_DATA_LENGTH) ; + exit (1) ; + } ; + + if (sfinfo.channels != 1) + { printf ("\n\nLine %d: Incorrect number of channels in file.\n", __LINE__) ; + exit (1) ; + } ; + + sf_command (file, SFC_SET_SCALE_FLOAT_INT_READ, NULL, SF_TRUE) ; + + test_read_short_or_die (file, 0, short_data, ARRAY_LEN (short_data), __LINE__) ; + sf_close (file) ; + + max = 0 ; + for (k = 0 ; k < ARRAY_LEN (short_data) ; k++) + if (abs (short_data [k]) > max) + max = abs (short_data [k]) ; + + if (1.0 * abs (max - 0x7FFF) / 0x7FFF > 0.01) + { printf ("\n\nLine %d: Bad maximum (%d should be %d).\n\n", __LINE__, max, 0x7FFF) ; + exit (1) ; + } ; + + unlink (filename) ; + puts ("ok") ; +} /* double_short_little_test */ + +static void +double_short_big_test (const char * filename) +{ SNDFILE *file ; + SF_INFO sfinfo ; + int max ; + unsigned k ; + + print_test_name ("double_short_big_test", filename) ; + + gen_windowed_sine_double (double_data, ARRAY_LEN (double_data), 0.9999) ; + + sfinfo.samplerate = SAMPLE_RATE ; + sfinfo.frames = ARRAY_LEN (short_data) ; + sfinfo.channels = 1 ; + sfinfo.format = SF_ENDIAN_BIG | SF_FORMAT_AU | SF_FORMAT_DOUBLE ; + + file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ; + test_write_double_or_die (file, 0, double_data, ARRAY_LEN (double_data), __LINE__) ; + sf_close (file) ; + + file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ; + + if (sfinfo.frames != ARRAY_LEN (double_data)) + { printf ("\n\nLine %d: Incorrect number of frames in file (too short). (%" PRId64 " should be %d)\n", __LINE__, sfinfo.frames, DFT_DATA_LENGTH) ; + exit (1) ; + } ; + + if (sfinfo.channels != 1) + { printf ("\n\nLine %d: Incorrect number of channels in file.\n", __LINE__) ; + exit (1) ; + } ; + + sf_command (file, SFC_SET_SCALE_FLOAT_INT_READ, NULL, SF_TRUE) ; + + test_read_short_or_die (file, 0, short_data, ARRAY_LEN (short_data), __LINE__) ; + sf_close (file) ; + + max = 0 ; + for (k = 0 ; k < ARRAY_LEN (short_data) ; k++) + if (abs (short_data [k]) > max) + max = abs (short_data [k]) ; + + if (1.0 * abs (max - 0x7FFF) / 0x7FFF > 0.01) + { printf ("\n\nLine %d: Bad maximum (%d should be %d).\n\n", __LINE__, max, 0x7FFF) ; + exit (1) ; + } ; + + unlink (filename) ; + puts ("ok") ; +} /* double_short_big_test */ + +static void +double_int_little_test (const char * filename) +{ SNDFILE *file ; + SF_INFO sfinfo ; + int max ; + unsigned k ; + + print_test_name ("double_int_little_test", filename) ; + + gen_windowed_sine_double (double_data, ARRAY_LEN (double_data), 0.9999) ; + + sfinfo.samplerate = SAMPLE_RATE ; + sfinfo.frames = ARRAY_LEN (int_data) ; + sfinfo.channels = 1 ; + sfinfo.format = SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_DOUBLE ; + + file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ; + test_write_double_or_die (file, 0, double_data, ARRAY_LEN (double_data), __LINE__) ; + sf_close (file) ; + + file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ; + + if (sfinfo.frames != ARRAY_LEN (double_data)) + { printf ("\n\nLine %d: Incorrect number of frames in file (too short). (%" PRId64 " should be %d)\n", __LINE__, sfinfo.frames, DFT_DATA_LENGTH) ; + exit (1) ; + } ; + + if (sfinfo.channels != 1) + { printf ("\n\nLine %d: Incorrect number of channels in file.\n", __LINE__) ; + exit (1) ; + } ; + + sf_command (file, SFC_SET_SCALE_FLOAT_INT_READ, NULL, SF_TRUE) ; + + test_read_int_or_die (file, 0, int_data, ARRAY_LEN (int_data), __LINE__) ; + sf_close (file) ; + + max = 0 ; + for (k = 0 ; k < ARRAY_LEN (int_data) ; k++) + if (abs (int_data [k]) > max) + max = abs (int_data [k]) ; + + if (1.0 * abs (max - 0x7FFFFFFF) / 0x7FFFFFFF > 0.01) + { printf ("\n\nLine %d: Bad maximum (%d should be %d).\n\n", __LINE__, max, 0x7FFFFFFF) ; + exit (1) ; + } ; + + unlink (filename) ; + puts ("ok") ; +} /* double_int_little_test */ + +static void +double_int_big_test (const char * filename) +{ SNDFILE *file ; + SF_INFO sfinfo ; + int max ; + unsigned k ; + + print_test_name ("double_int_big_test", filename) ; + + gen_windowed_sine_double (double_data, ARRAY_LEN (double_data), 0.9999) ; + + sfinfo.samplerate = SAMPLE_RATE ; + sfinfo.frames = ARRAY_LEN (int_data) ; + sfinfo.channels = 1 ; + sfinfo.format = SF_ENDIAN_BIG | SF_FORMAT_AU | SF_FORMAT_DOUBLE ; + + file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ; + test_write_double_or_die (file, 0, double_data, ARRAY_LEN (double_data), __LINE__) ; + sf_close (file) ; + + file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ; + + if (sfinfo.frames != ARRAY_LEN (double_data)) + { printf ("\n\nLine %d: Incorrect number of frames in file (too short). (%" PRId64 " should be %d)\n", __LINE__, sfinfo.frames, DFT_DATA_LENGTH) ; + exit (1) ; + } ; + + if (sfinfo.channels != 1) + { printf ("\n\nLine %d: Incorrect number of channels in file.\n", __LINE__) ; + exit (1) ; + } ; + + sf_command (file, SFC_SET_SCALE_FLOAT_INT_READ, NULL, SF_TRUE) ; + + test_read_int_or_die (file, 0, int_data, ARRAY_LEN (int_data), __LINE__) ; + sf_close (file) ; + + max = 0 ; + for (k = 0 ; k < ARRAY_LEN (int_data) ; k++) + if (abs (int_data [k]) > max) + max = abs (int_data [k]) ; + + if (1.0 * abs (max - 0x7FFFFFFF) / 0x7FFFFFFF > 0.01) + { printf ("\n\nLine %d: Bad maximum (%d should be %d).\n\n", __LINE__, max, 0x7FFFFFFF) ; + exit (1) ; + } ; + + unlink (filename) ; + puts ("ok") ; +} /* double_int_big_test */ + + diff --git a/tests/pipe_test.c b/tests/pipe_test.c new file mode 100644 index 0000000..fe28313 --- /dev/null +++ b/tests/pipe_test.c @@ -0,0 +1,525 @@ +/* +** Copyright (C) 2001-2012 Erik de Castro Lopo +** +** This program is free software; you can redistribute it and/or modify +** it under the terms of the GNU General Public License as published by +** the Free Software Foundation; either version 2 of the License, or +** (at your option) any later version. +** +** This program is distributed in the hope that it will be useful, +** but WITHOUT ANY WARRANTY; without even the implied warranty of +** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +** GNU General Public License for more details. +** +** You should have received a copy of the GNU General Public License +** along with this program; if not, write to the Free Software +** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +*/ + +/*========================================================================== +** This is a test program which tests reading from and writing to pipes. +*/ + +#include "sfconfig.h" + +#include +#include +#include + +#if (OS_IS_WIN32 || defined __OS2__ || HAVE_PIPE == 0 || HAVE_WAITPID == 0) + +int +main (void) +{ + puts (" pipe_test : this test doesn't work on this OS.") ; + return 0 ; +} /* main */ + +#else + +#if HAVE_UNISTD_H +#include +#endif + +#include +#include +#include +#include + +#include + +#include "utils.h" + +typedef struct +{ int format ; + const char *ext ; +} FILETYPE ; + +static int file_exists (const char *filename) ; +static void useek_pipe_rw_test (int filetype, const char *ext) ; +static void pipe_read_test (int filetype, const char *ext) ; +static void pipe_write_test (const char *ext) ; +static void pipe_test_others (FILETYPE*, FILETYPE*) ; + +static FILETYPE read_write_types [] = +{ { SF_FORMAT_RAW , "raw" }, + { SF_FORMAT_AU , "au" }, + /* Lite remove start */ + { SF_FORMAT_PAF , "paf" }, + { SF_FORMAT_IRCAM , "ircam" }, + { SF_FORMAT_PVF , "pvf" }, + /* Lite remove end */ + { 0 , NULL } +} ; + +static FILETYPE read_only_types [] = +{ { SF_FORMAT_RAW , "raw" }, + { SF_FORMAT_AU , "au" }, + { SF_FORMAT_AIFF , "aiff" }, + { SF_FORMAT_WAV , "wav" }, + { SF_FORMAT_W64 , "w64" }, + /* Lite remove start */ + { SF_FORMAT_PAF , "paf" }, + { SF_FORMAT_NIST , "nist" }, + { SF_FORMAT_IRCAM , "ircam" }, + { SF_FORMAT_MAT4 , "mat4" }, + { SF_FORMAT_MAT5 , "mat5" }, + { SF_FORMAT_SVX , "svx" }, + { SF_FORMAT_PVF , "pvf" }, + /* Lite remove end */ + { 0 , NULL } +} ; + +int +main (void) +{ int k ; + + if (file_exists ("libsndfile.spec.in")) + exit_if_true (chdir ("tests") != 0, "\n Error : chdir ('tests') failed.\n") ; + + for (k = 0 ; read_only_types [k].format ; k++) + pipe_read_test (read_only_types [k].format, read_only_types [k].ext) ; + + for (k = 0 ; read_write_types [k].format ; k++) + pipe_write_test (read_write_types [k].ext) ; + + for (k = 0 ; read_write_types [k].format ; k++) + useek_pipe_rw_test (read_write_types [k].format, read_write_types [k].ext) ; + + if (0) + pipe_test_others (read_write_types, read_only_types) ; + + return 0 ; +} /* main */ + +/*============================================================================== +*/ + +static void +pipe_read_test (int filetype, const char *ext) +{ static short data [PIPE_TEST_LEN] ; + static char buffer [256] ; + static char filename [256] ; + + SNDFILE *outfile ; + SF_INFO sfinfo ; + int k, retval ; + + snprintf (filename, sizeof (filename), "pipe_in.%s", ext) ; + print_test_name ("pipe_read_test", filename) ; + + sfinfo.format = filetype | SF_FORMAT_PCM_16 ; + sfinfo.channels = 1 ; + sfinfo.samplerate = 44100 ; + + for (k = 0 ; k < PIPE_TEST_LEN ; k++) + data [k] = PIPE_INDEX (k) ; + + outfile = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ; + test_writef_short_or_die (outfile, 0, data, PIPE_TEST_LEN, __LINE__) ; + sf_close (outfile) ; + + snprintf (buffer, sizeof (buffer), "cat %s | ./stdin_test %s ", filename, ext) ; + if ((retval = system (buffer)) != 0) + { retval = WEXITSTATUS (retval) ; + printf ("\n\n Line %d : pipe test returned error for file type \"%s\".\n\n", __LINE__, ext) ; + exit (retval) ; + } ; + + unlink (filename) ; + puts ("ok") ; + + return ; +} /* pipe_read_test */ + +static void +pipe_write_test (const char *ext) +{ static char buffer [256] ; + + int retval ; + + print_test_name ("pipe_write_test", ext) ; + + snprintf (buffer, sizeof (buffer), "./stdout_test %s | ./stdin_test %s ", ext, ext) ; + if ((retval = system (buffer))) + { retval = WEXITSTATUS (retval) ; + printf ("\n\n Line %d : pipe test returned error file type \"%s\".\n\n", __LINE__, ext) ; + exit (retval) ; + } ; + + puts ("ok") ; + + return ; +} /* pipe_write_test */ + +/*============================================================================== +*/ + + +static void +useek_pipe_rw_short (const char * ext, SF_INFO * psfinfo_write, SF_INFO * psfinfo_read) +{ static short buffer [PIPE_TEST_LEN] ; + static short data [PIPE_TEST_LEN] ; + SNDFILE *outfile ; + SNDFILE *infile_piped ; + + int k, status = 0 ; + int pipefd [2] ; + pid_t pida ; + + for (k = 0 ; k < PIPE_TEST_LEN ; k++) + data [k] = PIPE_INDEX (k) ; + + /* + ** Create the pipe. + */ + exit_if_true (pipe (pipefd) != 0, "\n\n%s %d : pipe failed : %s\n", __func__, __LINE__, strerror (errno)) ; + + /* + ** Attach the write end of the pipe to be written to. + */ + if ((outfile = sf_open_fd (pipefd [1], SFM_WRITE, psfinfo_write, SF_TRUE)) == NULL) + { printf ("\n\n%s %d : unable to create unseekable pipe for write type \"%s\".\n", __func__, __LINE__, ext) ; + printf ("\t%s\n\n", sf_strerror (outfile)) ; + exit (1) ; + } ; + + if (sf_error (outfile) != SF_ERR_NO_ERROR) + { printf ("\n\n%s %d : unable to open unseekable pipe for write type \"%s\".\n\n", __func__, __LINE__, ext) ; + exit (1) ; + } ; + + /* + ** Attach the read end of the pipe to be read from. + */ + if ((infile_piped = sf_open_fd (pipefd [0], SFM_READ, psfinfo_read, SF_TRUE)) == NULL) + { printf ("\n\n%s %d : unable to create unseekable pipe for read type. \"%s\".\n\n", __func__, __LINE__, ext) ; + exit (1) ; + } ; + + if (sf_error (infile_piped) != SF_ERR_NO_ERROR) + { printf ("\n\n%s %d : unable to open unseekable pipe for read type \"%s\".\n\n", __func__, __LINE__, ext) ; + exit (1) ; + } ; + + /* Fork a child process that will write directly into the pipe. */ + if ((pida = fork ()) == 0) /* child process */ + { test_writef_short_or_die (outfile, 0, data, PIPE_TEST_LEN, __LINE__) ; + exit (0) ; + } ; + + /* In the parent process, read from the pipe and compare what is read + ** to what is written, if they match everything went as planned. + */ + test_readf_short_or_die (infile_piped, 0, buffer, PIPE_TEST_LEN, __LINE__) ; + if (memcmp (buffer, data, sizeof (buffer)) != 0) + { printf ("\n\n%s %d : unseekable pipe test failed for file type \"%s\".\n\n", __func__, __LINE__, ext) ; + exit (1) ; + } ; + + /* Wait for the child process to return. */ + waitpid (pida, &status, 0) ; + status = WEXITSTATUS (status) ; + sf_close (outfile) ; + sf_close (infile_piped) ; + + if (status != 0) + { printf ("\n\n%s %d : status of child process is %d for file type %s.\n\n", __func__, __LINE__, status, ext) ; + exit (1) ; + } ; + + return ; +} /* useek_pipe_rw_short */ + + +static void +useek_pipe_rw_float (const char * ext, SF_INFO * psfinfo_write, SF_INFO * psfinfo_read) +{ static float buffer [PIPE_TEST_LEN] ; + static float data [PIPE_TEST_LEN] ; + SNDFILE *outfile ; + SNDFILE *infile_piped ; + + int k, status = 0 ; + int pipefd [2] ; + pid_t pida ; + + for (k = 0 ; k < PIPE_TEST_LEN ; k++) + data [k] = PIPE_INDEX (k) ; + + /* + ** Create the pipe. + */ + exit_if_true (pipe (pipefd) != 0, "\n\n%s %d : pipe failed : %s\n", __func__, __LINE__, strerror (errno)) ; + + /* + ** Attach the write end of the pipe to be written to. + */ + if ((outfile = sf_open_fd (pipefd [1], SFM_WRITE, psfinfo_write, SF_TRUE)) == NULL) + { printf ("\n\n%s %d : unable to create unseekable pipe for write type \"%s\".\n", __func__, __LINE__, ext) ; + printf ("\t%s\n\n", sf_strerror (outfile)) ; + exit (1) ; + } ; + + if (sf_error (outfile) != SF_ERR_NO_ERROR) + { printf ("\n\n%s %d : unable to open unseekable pipe for write type \"%s\".\n\n", __func__, __LINE__, ext) ; + exit (1) ; + } ; + + /* + ** Attach the read end of the pipe to be read from. + */ + if ((infile_piped = sf_open_fd (pipefd [0], SFM_READ, psfinfo_read, SF_TRUE)) == NULL) + { printf ("\n\n%s %d : unable to create unseekable pipe for read type. \"%s\".\n\n", __func__, __LINE__, ext) ; + exit (1) ; + } ; + + if (sf_error (infile_piped) != SF_ERR_NO_ERROR) + { printf ("\n\n%s %d : unable to open unseekable pipe for read type \"%s\".\n\n", __func__, __LINE__, ext) ; + exit (1) ; + } ; + + /* Fork a child process that will write directly into the pipe. */ + if ((pida = fork ()) == 0) /* child process */ + { test_writef_float_or_die (outfile, 0, data, PIPE_TEST_LEN, __LINE__) ; + exit (0) ; + } ; + + /* In the parent process, read from the pipe and compare what is read + ** to what is written, if they match everything went as planned. + */ + test_readf_float_or_die (infile_piped, 0, buffer, PIPE_TEST_LEN, __LINE__) ; + if (memcmp (buffer, data, sizeof (buffer)) != 0) + { printf ("\n\n%s %d : unseekable pipe test failed for file type \"%s\".\n\n", __func__, __LINE__, ext) ; + exit (1) ; + } ; + + /* Wait for the child process to return. */ + waitpid (pida, &status, 0) ; + status = WEXITSTATUS (status) ; + sf_close (outfile) ; + sf_close (infile_piped) ; + + if (status != 0) + { printf ("\n\n%s %d : status of child process is %d for file type %s.\n\n", __func__, __LINE__, status, ext) ; + exit (1) ; + } ; + + return ; +} /* useek_pipe_rw_float */ + + +static void +useek_pipe_rw_double (const char * ext, SF_INFO * psfinfo_write, SF_INFO * psfinfo_read) +{ static double buffer [PIPE_TEST_LEN] ; + static double data [PIPE_TEST_LEN] ; + SNDFILE *outfile ; + SNDFILE *infile_piped ; + + int k, status = 0 ; + int pipefd [2] ; + pid_t pida ; + + for (k = 0 ; k < PIPE_TEST_LEN ; k++) + data [k] = PIPE_INDEX (k) ; + + /* + ** Create the pipe. + */ + exit_if_true (pipe (pipefd) != 0, "\n\n%s %d : pipe failed : %s\n", __func__, __LINE__, strerror (errno)) ; + + /* + ** Attach the write end of the pipe to be written to. + */ + if ((outfile = sf_open_fd (pipefd [1], SFM_WRITE, psfinfo_write, SF_TRUE)) == NULL) + { printf ("\n\n%s %d : unable to create unseekable pipe for write type \"%s\".\n", __func__, __LINE__, ext) ; + printf ("\t%s\n\n", sf_strerror (outfile)) ; + exit (1) ; + } ; + + if (sf_error (outfile) != SF_ERR_NO_ERROR) + { printf ("\n\n%s %d : unable to open unseekable pipe for write type \"%s\".\n\n", __func__, __LINE__, ext) ; + exit (1) ; + } ; + + /* + ** Attach the read end of the pipe to be read from. + */ + if ((infile_piped = sf_open_fd (pipefd [0], SFM_READ, psfinfo_read, SF_TRUE)) == NULL) + { printf ("\n\n%s %d : unable to create unseekable pipe for read type. \"%s\".\n\n", __func__, __LINE__, ext) ; + exit (1) ; + } ; + + if (sf_error (infile_piped) != SF_ERR_NO_ERROR) + { printf ("\n\n%s %d : unable to open unseekable pipe for read type \"%s\".\n\n", __func__, __LINE__, ext) ; + exit (1) ; + } ; + + /* Fork a child process that will write directly into the pipe. */ + if ((pida = fork ()) == 0) /* child process */ + { test_writef_double_or_die (outfile, 0, data, PIPE_TEST_LEN, __LINE__) ; + exit (0) ; + } ; + + /* In the parent process, read from the pipe and compare what is read + ** to what is written, if they match everything went as planned. + */ + test_readf_double_or_die (infile_piped, 0, buffer, PIPE_TEST_LEN, __LINE__) ; + if (memcmp (buffer, data, sizeof (buffer)) != 0) + { printf ("\n\n%s %d : unseekable pipe test failed for file type \"%s\".\n\n", __func__, __LINE__, ext) ; + exit (1) ; + } ; + + /* Wait for the child process to return. */ + waitpid (pida, &status, 0) ; + status = WEXITSTATUS (status) ; + sf_close (outfile) ; + sf_close (infile_piped) ; + + if (status != 0) + { printf ("\n\n%s %d : status of child process is %d for file type %s.\n\n", __func__, __LINE__, status, ext) ; + exit (1) ; + } ; + + return ; +} /* useek_pipe_rw_double */ + + + + +static void +useek_pipe_rw_test (int filetype, const char *ext) +{ SF_INFO sfinfo_write ; + SF_INFO sfinfo_read ; + + print_test_name ("useek_pipe_rw_test", ext) ; + + /* + ** Setup the INFO structures for the filetype we will be + ** working with. + */ + sfinfo_write.format = filetype | SF_FORMAT_PCM_16 ; + sfinfo_write.channels = 1 ; + sfinfo_write.samplerate = 44100 ; + + + sfinfo_read.format = 0 ; + if (filetype == SF_FORMAT_RAW) + { sfinfo_read.format = filetype | SF_FORMAT_PCM_16 ; + sfinfo_read.channels = 1 ; + sfinfo_read.samplerate = 44100 ; + } ; + + useek_pipe_rw_short (ext, &sfinfo_write, &sfinfo_read) ; + + sfinfo_read.format = sfinfo_write.format = filetype | SF_FORMAT_FLOAT ; + if (sf_format_check (&sfinfo_read) != 0) + useek_pipe_rw_float (ext, &sfinfo_write, &sfinfo_read) ; + + sfinfo_read.format = sfinfo_write.format = filetype | SF_FORMAT_DOUBLE ; + if (sf_format_check (&sfinfo_read) != 0) + useek_pipe_rw_double (ext, &sfinfo_write, &sfinfo_read) ; + + puts ("ok") ; + return ; +} /* useek_pipe_rw_test */ + + + +static void +pipe_test_others (FILETYPE* list1, FILETYPE* list2) +{ SF_FORMAT_INFO info ; + int k, m, major_count, in_list ; + + print_test_name ("pipe_test_others", "") ; + + sf_command (NULL, SFC_GET_FORMAT_MAJOR_COUNT, &major_count, sizeof (int)) ; + + for (k = 0 ; k < major_count ; k++) + { info.format = k ; + + sf_command (NULL, SFC_GET_FORMAT_MAJOR, &info, sizeof (info)) ; + + in_list = SF_FALSE ; + for (m = 0 ; list1 [m].format ; m++) + if (info.format == list1 [m].format) + in_list = SF_TRUE ; + + for (m = 0 ; list2 [m].format ; m++) + if (info.format == list2 [m].format) + in_list = SF_TRUE ; + + if (in_list) + continue ; + + printf ("%s %x\n", info.name, info.format) ; + + if (1) + { static short data [PIPE_TEST_LEN] ; + static char buffer [256] ; + static const char *filename = "pipe_in.dat" ; + + SNDFILE *outfile ; + SF_INFO sfinfo ; + int retval ; + + sfinfo.format = info.format | SF_FORMAT_PCM_16 ; + sfinfo.channels = 1 ; + sfinfo.samplerate = 44100 ; + + outfile = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ; + test_writef_short_or_die (outfile, 0, data, PIPE_TEST_LEN, __LINE__) ; + sf_close (outfile) ; + + snprintf (buffer, sizeof (buffer), "cat %s | ./stdin_test %s %d ", filename, info.extension, PIPE_TEST_LEN) ; + if ((retval = system (buffer)) == 0) + { retval = WEXITSTATUS (retval) ; + printf ("\n\n Line %d : pipe test should have returned error file type \"%s\" but didn't.\n\n", __LINE__, info.name) ; + exit (1) ; + } ; + + unlink (filename) ; + } ; + } ; + + + puts ("ok") ; + + return ; +} /* pipe_test_others */ + + +/*============================================================================== +*/ + +static int +file_exists (const char *filename) +{ struct stat buf ; + + if (stat (filename, &buf)) + return 0 ; + + return 1 ; +} /* file_exists */ + +#endif + diff --git a/tests/rdwr_test.c b/tests/rdwr_test.c new file mode 100644 index 0000000..def53e2 --- /dev/null +++ b/tests/rdwr_test.c @@ -0,0 +1,240 @@ +/* +** Copyright (C) 2010-2012 Erik de Castro Lopo +** +** This program is free software ; you can redistribute it and/or modify +** it under the terms of the GNU General Public License as published by +** the Free Software Foundation ; either version 2 of the License, or +** (at your option) any later version. +** +** This program is distributed in the hope that it will be useful, +** but WITHOUT ANY WARRANTY ; without even the implied warranty of +** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +** GNU General Public License for more details. +** +** You should have received a copy of the GNU General Public License +** along with this program ; if not, write to the Free Software +** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +*/ + +#include "sfconfig.h" + +#include +#include +#include + +#include +#include + +#if HAVE_UNISTD_H +#include +#endif + +#if (HAVE_DECL_S_IRGRP == 0) +#include +#endif + +#if (defined (WIN32) || defined (_WIN32)) +#include +#include +#endif + +#include + +#include "utils.h" + +static void rdwr_short_test (const char *filename) ; +static void rdwr_int_test (const char *filename) ; +static void rdwr_float_test (const char *filename) ; +static void rdwr_double_test (const char *filename) ; +static void rdwr_raw_test (const char *filename) ; + + +int +main (void) +{ + rdwr_short_test ("rdwr_short.wav") ; + rdwr_int_test ("rdwr_int.wav") ; + rdwr_float_test ("rdwr_float.wav") ; + rdwr_double_test ("rdwr_double.wav") ; + rdwr_raw_test ("rdwr_raw.wav") ; + + return 0 ; +} /* main */ + + +/*============================================================================================ +** Here are the test functions. +*/ + +static void +rdwr_short_test (const char *filename) +{ SNDFILE *file ; + SF_INFO sfinfo ; + sf_count_t frames ; + short buffer [160] ; + + print_test_name ("rdwr_short_test", filename) ; + + memset (buffer, 0, sizeof (buffer)) ; + + /* Create sound file with no data. */ + sfinfo.format = SF_FORMAT_WAV | SF_FORMAT_PCM_16 ; + sfinfo.samplerate = 16000 ; + sfinfo.channels = 1 ; + + unlink (filename) ; + + frames = ARRAY_LEN (buffer) ; + + /* Open again for read/write. */ + file = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, SF_TRUE, __LINE__) ; + + test_write_short_or_die (file, 0, buffer, frames, __LINE__) ; + + test_read_short_or_die (file, 0, buffer, frames, __LINE__) ; + + sf_close (file) ; + unlink (filename) ; + + puts ("ok") ; + return ; +} /* rdwr_short_test */ + +static void +rdwr_int_test (const char *filename) +{ SNDFILE *file ; + SF_INFO sfinfo ; + sf_count_t frames ; + int buffer [160] ; + + print_test_name ("rdwr_int_test", filename) ; + + memset (buffer, 0, sizeof (buffer)) ; + + /* Create sound file with no data. */ + sfinfo.format = SF_FORMAT_WAV | SF_FORMAT_PCM_32 ; + sfinfo.samplerate = 16000 ; + sfinfo.channels = 1 ; + + unlink (filename) ; + + frames = ARRAY_LEN (buffer) ; + + /* Open again for read/write. */ + file = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, SF_TRUE, __LINE__) ; + + test_write_int_or_die (file, 0, buffer, frames, __LINE__) ; + + test_read_int_or_die (file, 0, buffer, frames, __LINE__) ; + + sf_close (file) ; + unlink (filename) ; + + puts ("ok") ; + return ; +} /* rdwr_int_test */ + +static void +rdwr_float_test (const char *filename) +{ SNDFILE *file ; + SF_INFO sfinfo ; + sf_count_t frames ; + float buffer [160] ; + + print_test_name ("rdwr_float_test", filename) ; + + memset (buffer, 0, sizeof (buffer)) ; + + /* Create sound file with no data. */ + sfinfo.format = SF_FORMAT_WAV | SF_FORMAT_FLOAT ; + sfinfo.samplerate = 16000 ; + sfinfo.channels = 1 ; + + unlink (filename) ; + + frames = ARRAY_LEN (buffer) ; + + /* Open again for read/write. */ + file = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, SF_TRUE, __LINE__) ; + + test_write_float_or_die (file, 0, buffer, frames, __LINE__) ; + + test_read_float_or_die (file, 0, buffer, frames, __LINE__) ; + + sf_close (file) ; + unlink (filename) ; + + puts ("ok") ; + return ; +} /* rdwr_float_test */ + +static void +rdwr_double_test (const char *filename) +{ SNDFILE *file ; + SF_INFO sfinfo ; + sf_count_t frames ; + double buffer [160] ; + + print_test_name ("rdwr_double_test", filename) ; + + memset (buffer, 0, sizeof (buffer)) ; + + /* Create sound file with no data. */ + sfinfo.format = SF_FORMAT_WAV | SF_FORMAT_DOUBLE ; + sfinfo.samplerate = 16000 ; + sfinfo.channels = 1 ; + + unlink (filename) ; + + frames = ARRAY_LEN (buffer) ; + + /* Open again for read/write. */ + file = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, SF_TRUE, __LINE__) ; + + test_write_double_or_die (file, 0, buffer, frames, __LINE__) ; + + test_read_double_or_die (file, 0, buffer, frames, __LINE__) ; + + sf_close (file) ; + unlink (filename) ; + + puts ("ok") ; + return ; +} /* rdwr_double_test */ + +static void +rdwr_raw_test (const char *filename) +{ SNDFILE *file ; + SF_INFO sfinfo ; + sf_count_t frames ; + unsigned char buffer [160] ; + + print_test_name ("rdwr_raw_test", filename) ; + + memset (buffer, 0, sizeof (buffer)) ; + + /* Create sound file with no data. */ + sfinfo.format = SF_FORMAT_WAV | SF_FORMAT_PCM_U8 ; + sfinfo.samplerate = 16000 ; + sfinfo.channels = 1 ; + + unlink (filename) ; + + frames = ARRAY_LEN (buffer) ; + + /* Open again for read/write. */ + file = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, SF_TRUE, __LINE__) ; + + test_write_raw_or_die (file, 0, buffer, frames, __LINE__) ; + + test_read_raw_or_die (file, 0, buffer, frames, __LINE__) ; + + sf_close (file) ; + unlink (filename) ; + + puts ("ok") ; + return ; +} /* rdwr_raw_test */ + + + diff --git a/tests/scale_clip_test.c b/tests/scale_clip_test.c new file mode 100644 index 0000000..defca24 --- /dev/null +++ b/tests/scale_clip_test.c @@ -0,0 +1,1853 @@ +/* +** Copyright (C) 1999-2012 Erik de Castro Lopo +** +** This program is free software; you can redistribute it and/or modify +** it under the terms of the GNU General Public License as published by +** the Free Software Foundation; either version 2 of the License, or +** (at your option) any later version. +** +** This program is distributed in the hope that it will be useful, +** but WITHOUT ANY WARRANTY; without even the implied warranty of +** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +** GNU General Public License for more details. +** +** You should have received a copy of the GNU General Public License +** along with this program; if not, write to the Free Software +** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +*/ + +#include "sfconfig.h" + +#include +#include +#include +#include +#include + +#if HAVE_UNISTD_H +#include +#endif + +#include + +#include "utils.h" + +#ifndef M_PI +#define M_PI 3.14159265358979323846264338 +#endif + +#define HALF_BUFFER_SIZE (1 << 12) +#define BUFFER_SIZE (2 * HALF_BUFFER_SIZE) + +#define SINE_AMP 1.1 +#define MAX_ERROR 0.0202 + + +static void flt_scale_clip_test_16 (const char *filename, int filetype, float maxval) ; +static void flt_scale_clip_test_24 (const char *filename, int filetype, float maxval) ; +static void flt_scale_clip_test_32 (const char *filename, int filetype, float maxval) ; +static void flt_scale_clip_test_08 (const char *filename, int filetype, float maxval) ; + +static void dbl_scale_clip_test_16 (const char *filename, int filetype, float maxval) ; +static void dbl_scale_clip_test_24 (const char *filename, int filetype, float maxval) ; +static void dbl_scale_clip_test_32 (const char *filename, int filetype, float maxval) ; +static void dbl_scale_clip_test_08 (const char *filename, int filetype, float maxval) ; + + + +static void flt_short_clip_read_test (const char *filename, int filetype) ; +static void flt_int_clip_read_test (const char *filename, int filetype) ; + +static void dbl_short_clip_read_test (const char *filename, int filetype) ; +static void dbl_int_clip_read_test (const char *filename, int filetype) ; + + + +static void short_flt_scale_write_test (const char *filename, int filetype) ; +static void short_dbl_scale_write_test (const char *filename, int filetype) ; + +static void int_flt_scale_write_test (const char *filename, int filetype) ; +static void int_dbl_scale_write_test (const char *filename, int filetype) ; + + +typedef union +{ double dbl [BUFFER_SIZE] ; + float flt [BUFFER_SIZE] ; + int i [BUFFER_SIZE] ; + short s [BUFFER_SIZE] ; +} BUFFER ; + +/* Data buffer. */ +static BUFFER buffer_out ; +static BUFFER buffer_in ; + +int +main (void) +{ + flt_scale_clip_test_08 ("scale_clip_s8.au", SF_FORMAT_AU | SF_FORMAT_PCM_S8, 1.0 * 0x80) ; + flt_scale_clip_test_08 ("scale_clip_u8.wav", SF_FORMAT_WAV | SF_FORMAT_PCM_U8, 1.0 * 0x80) ; + + dbl_scale_clip_test_08 ("scale_clip_s8.au", SF_FORMAT_AU | SF_FORMAT_PCM_S8, 1.0 * 0x80) ; + dbl_scale_clip_test_08 ("scale_clip_u8.wav", SF_FORMAT_WAV | SF_FORMAT_PCM_U8, 1.0 * 0x80) ; + + /* + ** Now use SF_FORMAT_AU where possible because it allows both + ** big and little endian files. + */ + + flt_scale_clip_test_16 ("scale_clip_be16.au", SF_ENDIAN_BIG | SF_FORMAT_AU | SF_FORMAT_PCM_16, 1.0 * 0x8000) ; + flt_scale_clip_test_16 ("scale_clip_le16.au", SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_PCM_16, 1.0 * 0x8000) ; + flt_scale_clip_test_24 ("scale_clip_be24.au", SF_ENDIAN_BIG | SF_FORMAT_AU | SF_FORMAT_PCM_24, 1.0 * 0x800000) ; + flt_scale_clip_test_24 ("scale_clip_le24.au", SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_PCM_24, 1.0 * 0x800000) ; + flt_scale_clip_test_32 ("scale_clip_be32.au", SF_ENDIAN_BIG | SF_FORMAT_AU | SF_FORMAT_PCM_32, 1.0 * 0x80000000) ; + flt_scale_clip_test_32 ("scale_clip_le32.au", SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_PCM_32, 1.0 * 0x80000000) ; + + dbl_scale_clip_test_16 ("scale_clip_be16.au", SF_ENDIAN_BIG | SF_FORMAT_AU | SF_FORMAT_PCM_16, 1.0 * 0x8000) ; + dbl_scale_clip_test_16 ("scale_clip_le16.au", SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_PCM_16, 1.0 * 0x8000) ; + dbl_scale_clip_test_24 ("scale_clip_be24.au", SF_ENDIAN_BIG | SF_FORMAT_AU | SF_FORMAT_PCM_24, 1.0 * 0x800000) ; + dbl_scale_clip_test_24 ("scale_clip_le24.au", SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_PCM_24, 1.0 * 0x800000) ; + dbl_scale_clip_test_32 ("scale_clip_be32.au", SF_ENDIAN_BIG | SF_FORMAT_AU | SF_FORMAT_PCM_32, 1.0 * 0x80000000) ; + dbl_scale_clip_test_32 ("scale_clip_le32.au", SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_PCM_32, 1.0 * 0x80000000) ; + + flt_short_clip_read_test ("flt_short.au" , SF_ENDIAN_BIG | SF_FORMAT_AU | SF_FORMAT_FLOAT) ; + flt_int_clip_read_test ("flt_int.au" , SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_FLOAT) ; + dbl_short_clip_read_test ("dbl_short.au" , SF_ENDIAN_BIG | SF_FORMAT_AU | SF_FORMAT_DOUBLE) ; + dbl_int_clip_read_test ("dbl_int.au" , SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_DOUBLE) ; + + short_flt_scale_write_test ("short_flt.au" , SF_ENDIAN_BIG | SF_FORMAT_AU | SF_FORMAT_FLOAT) ; + int_flt_scale_write_test ("int_flt.au" , SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_FLOAT) ; + short_dbl_scale_write_test ("short_dbl.au" , SF_ENDIAN_BIG | SF_FORMAT_AU | SF_FORMAT_DOUBLE) ; + int_dbl_scale_write_test ("int_dbl.au" , SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_DOUBLE) ; + + return 0 ; +} /* main */ + +/*============================================================================================ +** Here are the test functions. +*/ + + +static void +flt_scale_clip_test_16 (const char *filename, int filetype, float maxval) +{ SNDFILE *file ; + SF_INFO sfinfo ; + int k ; + float *data_out, *data_in ; + double diff, clip_max_diff ; + + print_test_name ("flt_scale_clip_test_16", filename) ; + + data_out = buffer_out.flt ; + data_in = buffer_in.flt ; + + for (k = 0 ; k < HALF_BUFFER_SIZE ; k++) + { data_out [k] = 1.2 * sin (2 * M_PI * k / HALF_BUFFER_SIZE) ; + data_out [k + HALF_BUFFER_SIZE] = data_out [k] * maxval ; + } ; + + sfinfo.samplerate = 44100 ; + sfinfo.frames = 123456789 ; /* Wrong length. Library should correct this on sf_close. */ + sfinfo.channels = 1 ; + sfinfo.format = filetype ; + + /* + ** Write two versions of the data: + ** normalized and clipped + ** un-normalized and clipped. + */ + + file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ; + sf_command (file, SFC_SET_CLIPPING, NULL, SF_TRUE) ; + test_write_float_or_die (file, 0, data_out, HALF_BUFFER_SIZE, __LINE__) ; + sf_command (file, SFC_SET_NORM_FLOAT, NULL, SF_FALSE) ; + test_write_float_or_die (file, 0, data_out + HALF_BUFFER_SIZE, HALF_BUFFER_SIZE, __LINE__) ; + sf_close (file) ; + + memset (&buffer_in, 0, sizeof (buffer_in)) ; + + file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ; + + sfinfo.format &= (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK) ; + + if (sfinfo.format != (filetype & (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK))) + { printf ("\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n\n", __LINE__, filetype, sfinfo.format) ; + exit (1) ; + } ; + + if (sfinfo.frames != BUFFER_SIZE) + { printf ("\n\nLine %d: Incorrect number of frames in file (%d => %" PRId64 ").\n\n", __LINE__, BUFFER_SIZE, sfinfo.frames) ; + exit (1) ; + } ; + + if (sfinfo.channels != 1) + { printf ("\n\nLine %d: Incorrect number of channels in file.\n\n", __LINE__) ; + exit (1) ; + } ; + + check_log_buffer_or_die (file, __LINE__) ; + + test_read_float_or_die (file, 0, data_in, HALF_BUFFER_SIZE, __LINE__) ; + sf_command (file, SFC_SET_NORM_FLOAT, NULL, SF_FALSE) ; + test_read_float_or_die (file, 0, data_in + HALF_BUFFER_SIZE, HALF_BUFFER_SIZE, __LINE__) ; + sf_close (file) ; + + /* Check normalized version. */ + clip_max_diff = 0.0 ; + for (k = 0 ; k < HALF_BUFFER_SIZE ; k++) + { if (fabs (data_in [k]) > 1.0) + { printf ("\n\nLine %d: Input sample %d/%d (%f) has not been clipped.\n\n", __LINE__, k, BUFFER_SIZE, data_in [k]) ; + exit (1) ; + } ; + + if (data_out [k] * data_in [k] < 0.0) + { printf ("\n\nLine %d: Data wrap around at index %d/%d.\n\n", __LINE__, k, BUFFER_SIZE) ; + exit (1) ; + } ; + + if (fabs (data_out [k]) > 1.0) + continue ; + + diff = fabs (data_out [k] - data_in [k]) ; + if (diff > clip_max_diff) + clip_max_diff = diff ; + } ; + + if (clip_max_diff < 1e-20) + { printf ("\n\nLine %d: Clipping difference (%e) too small (normalized).\n\n", __LINE__, clip_max_diff) ; + exit (1) ; + } ; + + if (clip_max_diff > 1.0 / 0x8000) + { printf ("\n\nLine %d: Clipping difference (%e) too large (normalized).\n\n", __LINE__, clip_max_diff) ; + exit (1) ; + } ; + + /* Check the un-normalized data. */ + clip_max_diff = 0.0 ; + for (k = HALF_BUFFER_SIZE ; k < BUFFER_SIZE ; k++) + { if (fabs (data_in [k]) > maxval) + { printf ("\n\nLine %d: Input sample %d/%d (%f) has not been clipped.\n\n", __LINE__, k, BUFFER_SIZE, data_in [k]) ; + exit (1) ; + } ; + + if (data_out [k] * data_in [k] < 0.0) + { printf ("\n\nLine %d: Data wrap around at index %d/%d.\n\n", __LINE__, k, BUFFER_SIZE) ; + exit (1) ; + } ; + + if (fabs (data_out [k]) > maxval) + continue ; + + diff = fabs (data_out [k] - data_in [k]) ; + if (diff > clip_max_diff) + clip_max_diff = diff ; + } ; + + if (clip_max_diff < 1e-20) + { printf ("\n\nLine %d: Clipping difference (%e) too small (un-normalized).\n\n", __LINE__, clip_max_diff) ; + exit (1) ; + } ; + + if (clip_max_diff > 1.0) + { printf ("\n\nLine %d: Clipping difference (%e) too large (un-normalised).\n\n", __LINE__, clip_max_diff) ; + exit (1) ; + } ; + + printf ("ok\n") ; + unlink (filename) ; +} /* flt_scale_clip_test_16 */ + +static void +flt_scale_clip_test_24 (const char *filename, int filetype, float maxval) +{ SNDFILE *file ; + SF_INFO sfinfo ; + int k ; + float *data_out, *data_in ; + double diff, clip_max_diff ; + + print_test_name ("flt_scale_clip_test_24", filename) ; + + data_out = buffer_out.flt ; + data_in = buffer_in.flt ; + + for (k = 0 ; k < HALF_BUFFER_SIZE ; k++) + { data_out [k] = 1.2 * sin (2 * M_PI * k / HALF_BUFFER_SIZE) ; + data_out [k + HALF_BUFFER_SIZE] = data_out [k] * maxval ; + } ; + + sfinfo.samplerate = 44100 ; + sfinfo.frames = 123456789 ; /* Wrong length. Library should correct this on sf_close. */ + sfinfo.channels = 1 ; + sfinfo.format = filetype ; + + /* + ** Write two versions of the data: + ** normalized and clipped + ** un-normalized and clipped. + */ + + file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ; + sf_command (file, SFC_SET_CLIPPING, NULL, SF_TRUE) ; + test_write_float_or_die (file, 0, data_out, HALF_BUFFER_SIZE, __LINE__) ; + sf_command (file, SFC_SET_NORM_FLOAT, NULL, SF_FALSE) ; + test_write_float_or_die (file, 0, data_out + HALF_BUFFER_SIZE, HALF_BUFFER_SIZE, __LINE__) ; + sf_close (file) ; + + memset (&buffer_in, 0, sizeof (buffer_in)) ; + + file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ; + + sfinfo.format &= (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK) ; + + if (sfinfo.format != (filetype & (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK))) + { printf ("\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n\n", __LINE__, filetype, sfinfo.format) ; + exit (1) ; + } ; + + if (sfinfo.frames != BUFFER_SIZE) + { printf ("\n\nLine %d: Incorrect number of frames in file (%d => %" PRId64 ").\n\n", __LINE__, BUFFER_SIZE, sfinfo.frames) ; + exit (1) ; + } ; + + if (sfinfo.channels != 1) + { printf ("\n\nLine %d: Incorrect number of channels in file.\n\n", __LINE__) ; + exit (1) ; + } ; + + check_log_buffer_or_die (file, __LINE__) ; + + test_read_float_or_die (file, 0, data_in, HALF_BUFFER_SIZE, __LINE__) ; + sf_command (file, SFC_SET_NORM_FLOAT, NULL, SF_FALSE) ; + test_read_float_or_die (file, 0, data_in + HALF_BUFFER_SIZE, HALF_BUFFER_SIZE, __LINE__) ; + sf_close (file) ; + + /* Check normalized version. */ + clip_max_diff = 0.0 ; + for (k = 0 ; k < HALF_BUFFER_SIZE ; k++) + { if (fabs (data_in [k]) > 1.0) + { printf ("\n\nLine %d: Input sample %d/%d (%f) has not been clipped.\n\n", __LINE__, k, BUFFER_SIZE, data_in [k]) ; + exit (1) ; + } ; + + if (data_out [k] * data_in [k] < 0.0) + { printf ("\n\nLine %d: Data wrap around at index %d/%d.\n\n", __LINE__, k, BUFFER_SIZE) ; + exit (1) ; + } ; + + if (fabs (data_out [k]) > 1.0) + continue ; + + diff = fabs (data_out [k] - data_in [k]) ; + if (diff > clip_max_diff) + clip_max_diff = diff ; + } ; + + if (clip_max_diff < 1e-20) + { printf ("\n\nLine %d: Clipping difference (%e) too small (normalized).\n\n", __LINE__, clip_max_diff) ; + exit (1) ; + } ; + + if (clip_max_diff > 1.0 / 0x800000) + { printf ("\n\nLine %d: Clipping difference (%e) too large (normalized).\n\n", __LINE__, clip_max_diff) ; + exit (1) ; + } ; + + /* Check the un-normalized data. */ + clip_max_diff = 0.0 ; + for (k = HALF_BUFFER_SIZE ; k < BUFFER_SIZE ; k++) + { if (fabs (data_in [k]) > maxval) + { printf ("\n\nLine %d: Input sample %d/%d (%f) has not been clipped.\n\n", __LINE__, k, BUFFER_SIZE, data_in [k]) ; + exit (1) ; + } ; + + if (data_out [k] * data_in [k] < 0.0) + { printf ("\n\nLine %d: Data wrap around at index %d/%d.\n\n", __LINE__, k, BUFFER_SIZE) ; + exit (1) ; + } ; + + if (fabs (data_out [k]) > maxval) + continue ; + + diff = fabs (data_out [k] - data_in [k]) ; + if (diff > clip_max_diff) + clip_max_diff = diff ; + } ; + + if (clip_max_diff < 1e-20) + { printf ("\n\nLine %d: Clipping difference (%e) too small (un-normalized).\n\n", __LINE__, clip_max_diff) ; + exit (1) ; + } ; + + if (clip_max_diff > 1.0) + { printf ("\n\nLine %d: Clipping difference (%e) too large (un-normalised).\n\n", __LINE__, clip_max_diff) ; + exit (1) ; + } ; + + printf ("ok\n") ; + unlink (filename) ; +} /* flt_scale_clip_test_24 */ + +static void +flt_scale_clip_test_32 (const char *filename, int filetype, float maxval) +{ SNDFILE *file ; + SF_INFO sfinfo ; + int k ; + float *data_out, *data_in ; + double diff, clip_max_diff ; + + print_test_name ("flt_scale_clip_test_32", filename) ; + + data_out = buffer_out.flt ; + data_in = buffer_in.flt ; + + for (k = 0 ; k < HALF_BUFFER_SIZE ; k++) + { data_out [k] = 1.2 * sin (2 * M_PI * k / HALF_BUFFER_SIZE) ; + data_out [k + HALF_BUFFER_SIZE] = data_out [k] * maxval ; + } ; + + sfinfo.samplerate = 44100 ; + sfinfo.frames = 123456789 ; /* Wrong length. Library should correct this on sf_close. */ + sfinfo.channels = 1 ; + sfinfo.format = filetype ; + + /* + ** Write two versions of the data: + ** normalized and clipped + ** un-normalized and clipped. + */ + + file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ; + sf_command (file, SFC_SET_CLIPPING, NULL, SF_TRUE) ; + test_write_float_or_die (file, 0, data_out, HALF_BUFFER_SIZE, __LINE__) ; + sf_command (file, SFC_SET_NORM_FLOAT, NULL, SF_FALSE) ; + test_write_float_or_die (file, 0, data_out + HALF_BUFFER_SIZE, HALF_BUFFER_SIZE, __LINE__) ; + sf_close (file) ; + + memset (&buffer_in, 0, sizeof (buffer_in)) ; + + file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ; + + sfinfo.format &= (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK) ; + + if (sfinfo.format != (filetype & (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK))) + { printf ("\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n\n", __LINE__, filetype, sfinfo.format) ; + exit (1) ; + } ; + + if (sfinfo.frames != BUFFER_SIZE) + { printf ("\n\nLine %d: Incorrect number of frames in file (%d => %" PRId64 ").\n\n", __LINE__, BUFFER_SIZE, sfinfo.frames) ; + exit (1) ; + } ; + + if (sfinfo.channels != 1) + { printf ("\n\nLine %d: Incorrect number of channels in file.\n\n", __LINE__) ; + exit (1) ; + } ; + + check_log_buffer_or_die (file, __LINE__) ; + + test_read_float_or_die (file, 0, data_in, HALF_BUFFER_SIZE, __LINE__) ; + sf_command (file, SFC_SET_NORM_FLOAT, NULL, SF_FALSE) ; + test_read_float_or_die (file, 0, data_in + HALF_BUFFER_SIZE, HALF_BUFFER_SIZE, __LINE__) ; + sf_close (file) ; + + /* Check normalized version. */ + clip_max_diff = 0.0 ; + for (k = 0 ; k < HALF_BUFFER_SIZE ; k++) + { if (fabs (data_in [k]) > 1.0) + { printf ("\n\nLine %d: Input sample %d/%d (%f) has not been clipped.\n\n", __LINE__, k, BUFFER_SIZE, data_in [k]) ; + exit (1) ; + } ; + + if (data_out [k] * data_in [k] < 0.0) + { printf ("\n\nLine %d: Data wrap around at index %d/%d.\n\n", __LINE__, k, BUFFER_SIZE) ; + exit (1) ; + } ; + + if (fabs (data_out [k]) > 1.0) + continue ; + + diff = fabs (data_out [k] - data_in [k]) ; + if (diff > clip_max_diff) + clip_max_diff = diff ; + } ; + + if (clip_max_diff < 1e-20) + { printf ("\n\nLine %d: Clipping difference (%e) too small (normalized).\n\n", __LINE__, clip_max_diff) ; + exit (1) ; + } ; + + if (clip_max_diff > 1.0 / 0x80000000) + { printf ("\n\nLine %d: Clipping difference (%e) too large (normalized).\n\n", __LINE__, clip_max_diff) ; + exit (1) ; + } ; + + /* Check the un-normalized data. */ + clip_max_diff = 0.0 ; + for (k = HALF_BUFFER_SIZE ; k < BUFFER_SIZE ; k++) + { if (fabs (data_in [k]) > maxval) + { printf ("\n\nLine %d: Input sample %d/%d (%f) has not been clipped.\n\n", __LINE__, k, BUFFER_SIZE, data_in [k]) ; + exit (1) ; + } ; + + if (data_out [k] * data_in [k] < 0.0) + { printf ("\n\nLine %d: Data wrap around at index %d/%d.\n\n", __LINE__, k, BUFFER_SIZE) ; + exit (1) ; + } ; + + if (fabs (data_out [k]) > maxval) + continue ; + + diff = fabs (data_out [k] - data_in [k]) ; + if (diff > clip_max_diff) + clip_max_diff = diff ; + } ; + + if (clip_max_diff < 1e-20) + { printf ("\n\nLine %d: Clipping difference (%e) too small (un-normalized).\n\n", __LINE__, clip_max_diff) ; + exit (1) ; + } ; + + if (clip_max_diff > 1.0) + { printf ("\n\nLine %d: Clipping difference (%e) too large (un-normalised).\n\n", __LINE__, clip_max_diff) ; + exit (1) ; + } ; + + printf ("ok\n") ; + unlink (filename) ; +} /* flt_scale_clip_test_32 */ + +static void +flt_scale_clip_test_08 (const char *filename, int filetype, float maxval) +{ SNDFILE *file ; + SF_INFO sfinfo ; + int k ; + float *data_out, *data_in ; + double diff, clip_max_diff ; + + print_test_name ("flt_scale_clip_test_08", filename) ; + + data_out = buffer_out.flt ; + data_in = buffer_in.flt ; + + for (k = 0 ; k < HALF_BUFFER_SIZE ; k++) + { data_out [k] = 1.2 * sin (2 * M_PI * k / HALF_BUFFER_SIZE) ; + data_out [k + HALF_BUFFER_SIZE] = data_out [k] * maxval ; + } ; + + sfinfo.samplerate = 44100 ; + sfinfo.frames = 123456789 ; /* Wrong length. Library should correct this on sf_close. */ + sfinfo.channels = 1 ; + sfinfo.format = filetype ; + + /* + ** Write two versions of the data: + ** normalized and clipped + ** un-normalized and clipped. + */ + + file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ; + sf_command (file, SFC_SET_CLIPPING, NULL, SF_TRUE) ; + test_write_float_or_die (file, 0, data_out, HALF_BUFFER_SIZE, __LINE__) ; + sf_command (file, SFC_SET_NORM_FLOAT, NULL, SF_FALSE) ; + test_write_float_or_die (file, 0, data_out + HALF_BUFFER_SIZE, HALF_BUFFER_SIZE, __LINE__) ; + sf_close (file) ; + + memset (&buffer_in, 0, sizeof (buffer_in)) ; + + file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ; + + sfinfo.format &= (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK) ; + + if (sfinfo.format != (filetype & (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK))) + { printf ("\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n\n", __LINE__, filetype, sfinfo.format) ; + exit (1) ; + } ; + + if (sfinfo.frames != BUFFER_SIZE) + { printf ("\n\nLine %d: Incorrect number of frames in file (%d => %" PRId64 ").\n\n", __LINE__, BUFFER_SIZE, sfinfo.frames) ; + exit (1) ; + } ; + + if (sfinfo.channels != 1) + { printf ("\n\nLine %d: Incorrect number of channels in file.\n\n", __LINE__) ; + exit (1) ; + } ; + + check_log_buffer_or_die (file, __LINE__) ; + + test_read_float_or_die (file, 0, data_in, HALF_BUFFER_SIZE, __LINE__) ; + sf_command (file, SFC_SET_NORM_FLOAT, NULL, SF_FALSE) ; + test_read_float_or_die (file, 0, data_in + HALF_BUFFER_SIZE, HALF_BUFFER_SIZE, __LINE__) ; + sf_close (file) ; + + /* Check normalized version. */ + clip_max_diff = 0.0 ; + for (k = 0 ; k < HALF_BUFFER_SIZE ; k++) + { if (fabs (data_in [k]) > 1.0) + { printf ("\n\nLine %d: Input sample %d/%d (%f) has not been clipped.\n\n", __LINE__, k, BUFFER_SIZE, data_in [k]) ; + exit (1) ; + } ; + + if (data_out [k] * data_in [k] < 0.0) + { printf ("\n\nLine %d: Data wrap around at index %d/%d.\n\n", __LINE__, k, BUFFER_SIZE) ; + exit (1) ; + } ; + + if (fabs (data_out [k]) > 1.0) + continue ; + + diff = fabs (data_out [k] - data_in [k]) ; + if (diff > clip_max_diff) + clip_max_diff = diff ; + } ; + + if (clip_max_diff < 1e-20) + { printf ("\n\nLine %d: Clipping difference (%e) too small (normalized).\n\n", __LINE__, clip_max_diff) ; + exit (1) ; + } ; + + if (clip_max_diff > 1.0 / 0x80) + { printf ("\n\nLine %d: Clipping difference (%e) too large (normalized).\n\n", __LINE__, clip_max_diff) ; + exit (1) ; + } ; + + /* Check the un-normalized data. */ + clip_max_diff = 0.0 ; + for (k = HALF_BUFFER_SIZE ; k < BUFFER_SIZE ; k++) + { if (fabs (data_in [k]) > maxval) + { printf ("\n\nLine %d: Input sample %d/%d (%f) has not been clipped.\n\n", __LINE__, k, BUFFER_SIZE, data_in [k]) ; + exit (1) ; + } ; + + if (data_out [k] * data_in [k] < 0.0) + { printf ("\n\nLine %d: Data wrap around at index %d/%d.\n\n", __LINE__, k, BUFFER_SIZE) ; + exit (1) ; + } ; + + if (fabs (data_out [k]) > maxval) + continue ; + + diff = fabs (data_out [k] - data_in [k]) ; + if (diff > clip_max_diff) + clip_max_diff = diff ; + } ; + + if (clip_max_diff < 1e-20) + { printf ("\n\nLine %d: Clipping difference (%e) too small (un-normalized).\n\n", __LINE__, clip_max_diff) ; + exit (1) ; + } ; + + if (clip_max_diff > 1.0) + { printf ("\n\nLine %d: Clipping difference (%e) too large (un-normalised).\n\n", __LINE__, clip_max_diff) ; + exit (1) ; + } ; + + printf ("ok\n") ; + unlink (filename) ; +} /* flt_scale_clip_test_08 */ + + + +static void +dbl_scale_clip_test_16 (const char *filename, int filetype, float maxval) +{ SNDFILE *file ; + SF_INFO sfinfo ; + int k ; + double *data_out, *data_in ; + double diff, clip_max_diff ; + + print_test_name ("dbl_scale_clip_test_16", filename) ; + + data_out = buffer_out.dbl ; + data_in = buffer_in.dbl ; + + for (k = 0 ; k < HALF_BUFFER_SIZE ; k++) + { data_out [k] = 1.2 * sin (2 * M_PI * k / HALF_BUFFER_SIZE) ; + data_out [k + HALF_BUFFER_SIZE] = data_out [k] * maxval ; + } ; + + sfinfo.samplerate = 44100 ; + sfinfo.frames = 123456789 ; /* Wrong length. Library should correct this on sf_close. */ + sfinfo.channels = 1 ; + sfinfo.format = filetype ; + + /* + ** Write two versions of the data: + ** normalized and clipped + ** un-normalized and clipped. + */ + + file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ; + sf_command (file, SFC_SET_CLIPPING, NULL, SF_TRUE) ; + test_write_double_or_die (file, 0, data_out, HALF_BUFFER_SIZE, __LINE__) ; + sf_command (file, SFC_SET_NORM_DOUBLE, NULL, SF_FALSE) ; + test_write_double_or_die (file, 0, data_out + HALF_BUFFER_SIZE, HALF_BUFFER_SIZE, __LINE__) ; + sf_close (file) ; + + memset (&buffer_in, 0, sizeof (buffer_in)) ; + + file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ; + + sfinfo.format &= (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK) ; + + if (sfinfo.format != (filetype & (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK))) + { printf ("\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n\n", __LINE__, filetype, sfinfo.format) ; + exit (1) ; + } ; + + if (sfinfo.frames != BUFFER_SIZE) + { printf ("\n\nLine %d: Incorrect number of frames in file (%d => %" PRId64 ").\n\n", __LINE__, BUFFER_SIZE, sfinfo.frames) ; + exit (1) ; + } ; + + if (sfinfo.channels != 1) + { printf ("\n\nLine %d: Incorrect number of channels in file.\n\n", __LINE__) ; + exit (1) ; + } ; + + check_log_buffer_or_die (file, __LINE__) ; + + test_read_double_or_die (file, 0, data_in, HALF_BUFFER_SIZE, __LINE__) ; + sf_command (file, SFC_SET_NORM_DOUBLE, NULL, SF_FALSE) ; + test_read_double_or_die (file, 0, data_in + HALF_BUFFER_SIZE, HALF_BUFFER_SIZE, __LINE__) ; + sf_close (file) ; + + /* Check normalized version. */ + clip_max_diff = 0.0 ; + for (k = 0 ; k < HALF_BUFFER_SIZE ; k++) + { if (fabs (data_in [k]) > 1.0) + { printf ("\n\nLine %d: Input sample %d/%d (%f) has not been clipped.\n\n", __LINE__, k, BUFFER_SIZE, data_in [k]) ; + exit (1) ; + } ; + + if (data_out [k] * data_in [k] < 0.0) + { printf ("\n\nLine %d: Data wrap around at index %d/%d.\n\n", __LINE__, k, BUFFER_SIZE) ; + exit (1) ; + } ; + + if (fabs (data_out [k]) > 1.0) + continue ; + + diff = fabs (data_out [k] - data_in [k]) ; + if (diff > clip_max_diff) + clip_max_diff = diff ; + } ; + + if (clip_max_diff < 1e-20) + { printf ("\n\nLine %d: Clipping difference (%e) too small (normalized).\n\n", __LINE__, clip_max_diff) ; + exit (1) ; + } ; + + if (clip_max_diff > 1.0 / 0x8000) + { printf ("\n\nLine %d: Clipping difference (%e) too large (normalized).\n\n", __LINE__, clip_max_diff) ; + exit (1) ; + } ; + + /* Check the un-normalized data. */ + clip_max_diff = 0.0 ; + for (k = HALF_BUFFER_SIZE ; k < BUFFER_SIZE ; k++) + { if (fabs (data_in [k]) > maxval) + { printf ("\n\nLine %d: Input sample %d/%d (%f) has not been clipped.\n\n", __LINE__, k, BUFFER_SIZE, data_in [k]) ; + exit (1) ; + } ; + + if (data_out [k] * data_in [k] < 0.0) + { printf ("\n\nLine %d: Data wrap around at index %d/%d.\n\n", __LINE__, k, BUFFER_SIZE) ; + exit (1) ; + } ; + + if (fabs (data_out [k]) > maxval) + continue ; + + diff = fabs (data_out [k] - data_in [k]) ; + if (diff > clip_max_diff) + clip_max_diff = diff ; + } ; + + if (clip_max_diff < 1e-20) + { printf ("\n\nLine %d: Clipping difference (%e) too small (un-normalized).\n\n", __LINE__, clip_max_diff) ; + exit (1) ; + } ; + + if (clip_max_diff > 1.0) + { printf ("\n\nLine %d: Clipping difference (%e) too large (un-normalised).\n\n", __LINE__, clip_max_diff) ; + exit (1) ; + } ; + + printf ("ok\n") ; + unlink (filename) ; +} /* dbl_scale_clip_test_16 */ + +static void +dbl_scale_clip_test_24 (const char *filename, int filetype, float maxval) +{ SNDFILE *file ; + SF_INFO sfinfo ; + int k ; + double *data_out, *data_in ; + double diff, clip_max_diff ; + + print_test_name ("dbl_scale_clip_test_24", filename) ; + + data_out = buffer_out.dbl ; + data_in = buffer_in.dbl ; + + for (k = 0 ; k < HALF_BUFFER_SIZE ; k++) + { data_out [k] = 1.2 * sin (2 * M_PI * k / HALF_BUFFER_SIZE) ; + data_out [k + HALF_BUFFER_SIZE] = data_out [k] * maxval ; + } ; + + sfinfo.samplerate = 44100 ; + sfinfo.frames = 123456789 ; /* Wrong length. Library should correct this on sf_close. */ + sfinfo.channels = 1 ; + sfinfo.format = filetype ; + + /* + ** Write two versions of the data: + ** normalized and clipped + ** un-normalized and clipped. + */ + + file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ; + sf_command (file, SFC_SET_CLIPPING, NULL, SF_TRUE) ; + test_write_double_or_die (file, 0, data_out, HALF_BUFFER_SIZE, __LINE__) ; + sf_command (file, SFC_SET_NORM_DOUBLE, NULL, SF_FALSE) ; + test_write_double_or_die (file, 0, data_out + HALF_BUFFER_SIZE, HALF_BUFFER_SIZE, __LINE__) ; + sf_close (file) ; + + memset (&buffer_in, 0, sizeof (buffer_in)) ; + + file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ; + + sfinfo.format &= (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK) ; + + if (sfinfo.format != (filetype & (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK))) + { printf ("\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n\n", __LINE__, filetype, sfinfo.format) ; + exit (1) ; + } ; + + if (sfinfo.frames != BUFFER_SIZE) + { printf ("\n\nLine %d: Incorrect number of frames in file (%d => %" PRId64 ").\n\n", __LINE__, BUFFER_SIZE, sfinfo.frames) ; + exit (1) ; + } ; + + if (sfinfo.channels != 1) + { printf ("\n\nLine %d: Incorrect number of channels in file.\n\n", __LINE__) ; + exit (1) ; + } ; + + check_log_buffer_or_die (file, __LINE__) ; + + test_read_double_or_die (file, 0, data_in, HALF_BUFFER_SIZE, __LINE__) ; + sf_command (file, SFC_SET_NORM_DOUBLE, NULL, SF_FALSE) ; + test_read_double_or_die (file, 0, data_in + HALF_BUFFER_SIZE, HALF_BUFFER_SIZE, __LINE__) ; + sf_close (file) ; + + /* Check normalized version. */ + clip_max_diff = 0.0 ; + for (k = 0 ; k < HALF_BUFFER_SIZE ; k++) + { if (fabs (data_in [k]) > 1.0) + { printf ("\n\nLine %d: Input sample %d/%d (%f) has not been clipped.\n\n", __LINE__, k, BUFFER_SIZE, data_in [k]) ; + exit (1) ; + } ; + + if (data_out [k] * data_in [k] < 0.0) + { printf ("\n\nLine %d: Data wrap around at index %d/%d.\n\n", __LINE__, k, BUFFER_SIZE) ; + exit (1) ; + } ; + + if (fabs (data_out [k]) > 1.0) + continue ; + + diff = fabs (data_out [k] - data_in [k]) ; + if (diff > clip_max_diff) + clip_max_diff = diff ; + } ; + + if (clip_max_diff < 1e-20) + { printf ("\n\nLine %d: Clipping difference (%e) too small (normalized).\n\n", __LINE__, clip_max_diff) ; + exit (1) ; + } ; + + if (clip_max_diff > 1.0 / 0x800000) + { printf ("\n\nLine %d: Clipping difference (%e) too large (normalized).\n\n", __LINE__, clip_max_diff) ; + exit (1) ; + } ; + + /* Check the un-normalized data. */ + clip_max_diff = 0.0 ; + for (k = HALF_BUFFER_SIZE ; k < BUFFER_SIZE ; k++) + { if (fabs (data_in [k]) > maxval) + { printf ("\n\nLine %d: Input sample %d/%d (%f) has not been clipped.\n\n", __LINE__, k, BUFFER_SIZE, data_in [k]) ; + exit (1) ; + } ; + + if (data_out [k] * data_in [k] < 0.0) + { printf ("\n\nLine %d: Data wrap around at index %d/%d.\n\n", __LINE__, k, BUFFER_SIZE) ; + exit (1) ; + } ; + + if (fabs (data_out [k]) > maxval) + continue ; + + diff = fabs (data_out [k] - data_in [k]) ; + if (diff > clip_max_diff) + clip_max_diff = diff ; + } ; + + if (clip_max_diff < 1e-20) + { printf ("\n\nLine %d: Clipping difference (%e) too small (un-normalized).\n\n", __LINE__, clip_max_diff) ; + exit (1) ; + } ; + + if (clip_max_diff > 1.0) + { printf ("\n\nLine %d: Clipping difference (%e) too large (un-normalised).\n\n", __LINE__, clip_max_diff) ; + exit (1) ; + } ; + + printf ("ok\n") ; + unlink (filename) ; +} /* dbl_scale_clip_test_24 */ + +static void +dbl_scale_clip_test_32 (const char *filename, int filetype, float maxval) +{ SNDFILE *file ; + SF_INFO sfinfo ; + int k ; + double *data_out, *data_in ; + double diff, clip_max_diff ; + + print_test_name ("dbl_scale_clip_test_32", filename) ; + + data_out = buffer_out.dbl ; + data_in = buffer_in.dbl ; + + for (k = 0 ; k < HALF_BUFFER_SIZE ; k++) + { data_out [k] = 1.2 * sin (2 * M_PI * k / HALF_BUFFER_SIZE) ; + data_out [k + HALF_BUFFER_SIZE] = data_out [k] * maxval ; + } ; + + sfinfo.samplerate = 44100 ; + sfinfo.frames = 123456789 ; /* Wrong length. Library should correct this on sf_close. */ + sfinfo.channels = 1 ; + sfinfo.format = filetype ; + + /* + ** Write two versions of the data: + ** normalized and clipped + ** un-normalized and clipped. + */ + + file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ; + sf_command (file, SFC_SET_CLIPPING, NULL, SF_TRUE) ; + test_write_double_or_die (file, 0, data_out, HALF_BUFFER_SIZE, __LINE__) ; + sf_command (file, SFC_SET_NORM_DOUBLE, NULL, SF_FALSE) ; + test_write_double_or_die (file, 0, data_out + HALF_BUFFER_SIZE, HALF_BUFFER_SIZE, __LINE__) ; + sf_close (file) ; + + memset (&buffer_in, 0, sizeof (buffer_in)) ; + + file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ; + + sfinfo.format &= (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK) ; + + if (sfinfo.format != (filetype & (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK))) + { printf ("\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n\n", __LINE__, filetype, sfinfo.format) ; + exit (1) ; + } ; + + if (sfinfo.frames != BUFFER_SIZE) + { printf ("\n\nLine %d: Incorrect number of frames in file (%d => %" PRId64 ").\n\n", __LINE__, BUFFER_SIZE, sfinfo.frames) ; + exit (1) ; + } ; + + if (sfinfo.channels != 1) + { printf ("\n\nLine %d: Incorrect number of channels in file.\n\n", __LINE__) ; + exit (1) ; + } ; + + check_log_buffer_or_die (file, __LINE__) ; + + test_read_double_or_die (file, 0, data_in, HALF_BUFFER_SIZE, __LINE__) ; + sf_command (file, SFC_SET_NORM_DOUBLE, NULL, SF_FALSE) ; + test_read_double_or_die (file, 0, data_in + HALF_BUFFER_SIZE, HALF_BUFFER_SIZE, __LINE__) ; + sf_close (file) ; + + /* Check normalized version. */ + clip_max_diff = 0.0 ; + for (k = 0 ; k < HALF_BUFFER_SIZE ; k++) + { if (fabs (data_in [k]) > 1.0) + { printf ("\n\nLine %d: Input sample %d/%d (%f) has not been clipped.\n\n", __LINE__, k, BUFFER_SIZE, data_in [k]) ; + exit (1) ; + } ; + + if (data_out [k] * data_in [k] < 0.0) + { printf ("\n\nLine %d: Data wrap around at index %d/%d.\n\n", __LINE__, k, BUFFER_SIZE) ; + exit (1) ; + } ; + + if (fabs (data_out [k]) > 1.0) + continue ; + + diff = fabs (data_out [k] - data_in [k]) ; + if (diff > clip_max_diff) + clip_max_diff = diff ; + } ; + + if (clip_max_diff < 1e-20) + { printf ("\n\nLine %d: Clipping difference (%e) too small (normalized).\n\n", __LINE__, clip_max_diff) ; + exit (1) ; + } ; + + if (clip_max_diff > 1.0 / 0x80000000) + { printf ("\n\nLine %d: Clipping difference (%e) too large (normalized).\n\n", __LINE__, clip_max_diff) ; + exit (1) ; + } ; + + /* Check the un-normalized data. */ + clip_max_diff = 0.0 ; + for (k = HALF_BUFFER_SIZE ; k < BUFFER_SIZE ; k++) + { if (fabs (data_in [k]) > maxval) + { printf ("\n\nLine %d: Input sample %d/%d (%f) has not been clipped.\n\n", __LINE__, k, BUFFER_SIZE, data_in [k]) ; + exit (1) ; + } ; + + if (data_out [k] * data_in [k] < 0.0) + { printf ("\n\nLine %d: Data wrap around at index %d/%d.\n\n", __LINE__, k, BUFFER_SIZE) ; + exit (1) ; + } ; + + if (fabs (data_out [k]) > maxval) + continue ; + + diff = fabs (data_out [k] - data_in [k]) ; + if (diff > clip_max_diff) + clip_max_diff = diff ; + } ; + + if (clip_max_diff < 1e-20) + { printf ("\n\nLine %d: Clipping difference (%e) too small (un-normalized).\n\n", __LINE__, clip_max_diff) ; + exit (1) ; + } ; + + if (clip_max_diff > 1.0) + { printf ("\n\nLine %d: Clipping difference (%e) too large (un-normalised).\n\n", __LINE__, clip_max_diff) ; + exit (1) ; + } ; + + printf ("ok\n") ; + unlink (filename) ; +} /* dbl_scale_clip_test_32 */ + +static void +dbl_scale_clip_test_08 (const char *filename, int filetype, float maxval) +{ SNDFILE *file ; + SF_INFO sfinfo ; + int k ; + double *data_out, *data_in ; + double diff, clip_max_diff ; + + print_test_name ("dbl_scale_clip_test_08", filename) ; + + data_out = buffer_out.dbl ; + data_in = buffer_in.dbl ; + + for (k = 0 ; k < HALF_BUFFER_SIZE ; k++) + { data_out [k] = 1.2 * sin (2 * M_PI * k / HALF_BUFFER_SIZE) ; + data_out [k + HALF_BUFFER_SIZE] = data_out [k] * maxval ; + } ; + + sfinfo.samplerate = 44100 ; + sfinfo.frames = 123456789 ; /* Wrong length. Library should correct this on sf_close. */ + sfinfo.channels = 1 ; + sfinfo.format = filetype ; + + /* + ** Write two versions of the data: + ** normalized and clipped + ** un-normalized and clipped. + */ + + file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ; + sf_command (file, SFC_SET_CLIPPING, NULL, SF_TRUE) ; + test_write_double_or_die (file, 0, data_out, HALF_BUFFER_SIZE, __LINE__) ; + sf_command (file, SFC_SET_NORM_DOUBLE, NULL, SF_FALSE) ; + test_write_double_or_die (file, 0, data_out + HALF_BUFFER_SIZE, HALF_BUFFER_SIZE, __LINE__) ; + sf_close (file) ; + + memset (&buffer_in, 0, sizeof (buffer_in)) ; + + file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ; + + sfinfo.format &= (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK) ; + + if (sfinfo.format != (filetype & (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK))) + { printf ("\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n\n", __LINE__, filetype, sfinfo.format) ; + exit (1) ; + } ; + + if (sfinfo.frames != BUFFER_SIZE) + { printf ("\n\nLine %d: Incorrect number of frames in file (%d => %" PRId64 ").\n\n", __LINE__, BUFFER_SIZE, sfinfo.frames) ; + exit (1) ; + } ; + + if (sfinfo.channels != 1) + { printf ("\n\nLine %d: Incorrect number of channels in file.\n\n", __LINE__) ; + exit (1) ; + } ; + + check_log_buffer_or_die (file, __LINE__) ; + + test_read_double_or_die (file, 0, data_in, HALF_BUFFER_SIZE, __LINE__) ; + sf_command (file, SFC_SET_NORM_DOUBLE, NULL, SF_FALSE) ; + test_read_double_or_die (file, 0, data_in + HALF_BUFFER_SIZE, HALF_BUFFER_SIZE, __LINE__) ; + sf_close (file) ; + + /* Check normalized version. */ + clip_max_diff = 0.0 ; + for (k = 0 ; k < HALF_BUFFER_SIZE ; k++) + { if (fabs (data_in [k]) > 1.0) + { printf ("\n\nLine %d: Input sample %d/%d (%f) has not been clipped.\n\n", __LINE__, k, BUFFER_SIZE, data_in [k]) ; + exit (1) ; + } ; + + if (data_out [k] * data_in [k] < 0.0) + { printf ("\n\nLine %d: Data wrap around at index %d/%d.\n\n", __LINE__, k, BUFFER_SIZE) ; + exit (1) ; + } ; + + if (fabs (data_out [k]) > 1.0) + continue ; + + diff = fabs (data_out [k] - data_in [k]) ; + if (diff > clip_max_diff) + clip_max_diff = diff ; + } ; + + if (clip_max_diff < 1e-20) + { printf ("\n\nLine %d: Clipping difference (%e) too small (normalized).\n\n", __LINE__, clip_max_diff) ; + exit (1) ; + } ; + + if (clip_max_diff > 1.0 / 0x80) + { printf ("\n\nLine %d: Clipping difference (%e) too large (normalized).\n\n", __LINE__, clip_max_diff) ; + exit (1) ; + } ; + + /* Check the un-normalized data. */ + clip_max_diff = 0.0 ; + for (k = HALF_BUFFER_SIZE ; k < BUFFER_SIZE ; k++) + { if (fabs (data_in [k]) > maxval) + { printf ("\n\nLine %d: Input sample %d/%d (%f) has not been clipped.\n\n", __LINE__, k, BUFFER_SIZE, data_in [k]) ; + exit (1) ; + } ; + + if (data_out [k] * data_in [k] < 0.0) + { printf ("\n\nLine %d: Data wrap around at index %d/%d.\n\n", __LINE__, k, BUFFER_SIZE) ; + exit (1) ; + } ; + + if (fabs (data_out [k]) > maxval) + continue ; + + diff = fabs (data_out [k] - data_in [k]) ; + if (diff > clip_max_diff) + clip_max_diff = diff ; + } ; + + if (clip_max_diff < 1e-20) + { printf ("\n\nLine %d: Clipping difference (%e) too small (un-normalized).\n\n", __LINE__, clip_max_diff) ; + exit (1) ; + } ; + + if (clip_max_diff > 1.0) + { printf ("\n\nLine %d: Clipping difference (%e) too large (un-normalised).\n\n", __LINE__, clip_max_diff) ; + exit (1) ; + } ; + + printf ("ok\n") ; + unlink (filename) ; +} /* dbl_scale_clip_test_08 */ + + + + +/*============================================================================== +*/ + + +static void flt_short_clip_read_test (const char *filename, int filetype) +{ SNDFILE *file ; + SF_INFO sfinfo ; + float *data_out ; + short *data_in, max_value ; + int k ; + + print_test_name ("flt_short_clip_read_test", filename) ; + + data_out = buffer_out.flt ; + data_in = buffer_in.s ; + + for (k = 0 ; k < BUFFER_SIZE ; k++) + data_out [k] = 0.995 * sin (4 * M_PI * k / BUFFER_SIZE) ; + data_out [BUFFER_SIZE / 8] = 1.0 ; + data_out [3 * BUFFER_SIZE / 8] = -1.000000001 ; + data_out [5 * BUFFER_SIZE / 8] = 1.0 ; + data_out [7 * BUFFER_SIZE / 8] = -1.000000001 ; + + memset (&sfinfo, 0, sizeof (sfinfo)) ; + sfinfo.samplerate = 44100 ; + sfinfo.frames = 123456789 ; /* Wrong length. Library should correct this on sf_close. */ + sfinfo.channels = 1 ; + sfinfo.format = filetype ; + + /* Save unclipped data to the file. */ + file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ; + test_write_float_or_die (file, 0, data_out, BUFFER_SIZE, __LINE__) ; + sf_close (file) ; + + memset (&sfinfo, 0, sizeof (sfinfo)) ; + + file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ; + sf_command (file, SFC_SET_SCALE_FLOAT_INT_READ, NULL, SF_TRUE) ; + + sfinfo.format &= (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK) ; + + if (sfinfo.format != (filetype & (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK))) + { printf ("\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n\n", __LINE__, filetype, sfinfo.format) ; + exit (1) ; + } ; + + if (sfinfo.frames != BUFFER_SIZE) + { printf ("\n\nLine %d: Incorrect number of frames in file (%d => %" PRId64 ").\n\n", __LINE__, BUFFER_SIZE, sfinfo.frames) ; + exit (1) ; + } ; + + if (sfinfo.channels != 1) + { printf ("\n\nLine %d: Incorrect number of channels in file.\n\n", __LINE__) ; + exit (1) ; + } ; + + check_log_buffer_or_die (file, __LINE__) ; + + sf_command (file, SFC_SET_CLIPPING, NULL, SF_TRUE) ; + test_read_short_or_die (file, 0, data_in, BUFFER_SIZE, __LINE__) ; + /*-sf_command (file, SFC_SET_NORM_FLOAT, NULL, SF_FALSE) ;-*/ + sf_close (file) ; + + /* Check the first half. */ + max_value = 0 ; + for (k = 0 ; k < sfinfo.frames ; k++) + { /* Check if data_out has different sign from data_in. */ + if ((data_out [k] < 0.0 && data_in [k] > 0) || (data_out [k] > 0.0 && data_in [k] < 0)) + { printf ("\n\nLine %d: Data wrap around at index %d/%d (%f -> %d).\n\n", __LINE__, k, BUFFER_SIZE, data_out [k], data_in [k]) ; + exit (1) ; + } ; + max_value = (max_value > abs (data_in [k])) ? max_value : abs (data_in [k]) ; + } ; + + unlink (filename) ; + puts ("ok") ; +} /* flt_short_clip_read_test */ +static void flt_int_clip_read_test (const char *filename, int filetype) +{ SNDFILE *file ; + SF_INFO sfinfo ; + float *data_out ; + int *data_in, max_value ; + int k ; + + print_test_name ("flt_int_clip_read_test", filename) ; + + data_out = buffer_out.flt ; + data_in = buffer_in.i ; + + for (k = 0 ; k < BUFFER_SIZE ; k++) + data_out [k] = 0.995 * sin (4 * M_PI * k / BUFFER_SIZE) ; + data_out [BUFFER_SIZE / 8] = 1.0 ; + data_out [3 * BUFFER_SIZE / 8] = -1.000000001 ; + data_out [5 * BUFFER_SIZE / 8] = 1.0 ; + data_out [7 * BUFFER_SIZE / 8] = -1.000000001 ; + + memset (&sfinfo, 0, sizeof (sfinfo)) ; + sfinfo.samplerate = 44100 ; + sfinfo.frames = 123456789 ; /* Wrong length. Library should correct this on sf_close. */ + sfinfo.channels = 1 ; + sfinfo.format = filetype ; + + /* Save unclipped data to the file. */ + file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ; + test_write_float_or_die (file, 0, data_out, BUFFER_SIZE, __LINE__) ; + sf_close (file) ; + + memset (&sfinfo, 0, sizeof (sfinfo)) ; + + file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ; + sf_command (file, SFC_SET_SCALE_FLOAT_INT_READ, NULL, SF_TRUE) ; + + sfinfo.format &= (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK) ; + + if (sfinfo.format != (filetype & (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK))) + { printf ("\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n\n", __LINE__, filetype, sfinfo.format) ; + exit (1) ; + } ; + + if (sfinfo.frames != BUFFER_SIZE) + { printf ("\n\nLine %d: Incorrect number of frames in file (%d => %" PRId64 ").\n\n", __LINE__, BUFFER_SIZE, sfinfo.frames) ; + exit (1) ; + } ; + + if (sfinfo.channels != 1) + { printf ("\n\nLine %d: Incorrect number of channels in file.\n\n", __LINE__) ; + exit (1) ; + } ; + + check_log_buffer_or_die (file, __LINE__) ; + + sf_command (file, SFC_SET_CLIPPING, NULL, SF_TRUE) ; + test_read_int_or_die (file, 0, data_in, BUFFER_SIZE, __LINE__) ; + /*-sf_command (file, SFC_SET_NORM_FLOAT, NULL, SF_FALSE) ;-*/ + sf_close (file) ; + + /* Check the first half. */ + max_value = 0 ; + for (k = 0 ; k < sfinfo.frames ; k++) + { /* Check if data_out has different sign from data_in. */ + if ((data_out [k] < 0.0 && data_in [k] > 0) || (data_out [k] > 0.0 && data_in [k] < 0)) + { printf ("\n\nLine %d: Data wrap around at index %d/%d (%f -> %d).\n\n", __LINE__, k, BUFFER_SIZE, data_out [k], data_in [k]) ; + exit (1) ; + } ; + max_value = (max_value > abs (data_in [k])) ? max_value : abs (data_in [k]) ; + } ; + + unlink (filename) ; + puts ("ok") ; +} /* flt_int_clip_read_test */ + +static void dbl_short_clip_read_test (const char *filename, int filetype) +{ SNDFILE *file ; + SF_INFO sfinfo ; + double *data_out ; + short *data_in, max_value ; + int k ; + + print_test_name ("dbl_short_clip_read_test", filename) ; + + data_out = buffer_out.dbl ; + data_in = buffer_in.s ; + + for (k = 0 ; k < BUFFER_SIZE ; k++) + data_out [k] = 0.995 * sin (4 * M_PI * k / BUFFER_SIZE) ; + data_out [BUFFER_SIZE / 8] = 1.0 ; + data_out [3 * BUFFER_SIZE / 8] = -1.000000001 ; + data_out [5 * BUFFER_SIZE / 8] = 1.0 ; + data_out [7 * BUFFER_SIZE / 8] = -1.000000001 ; + + memset (&sfinfo, 0, sizeof (sfinfo)) ; + sfinfo.samplerate = 44100 ; + sfinfo.frames = 123456789 ; /* Wrong length. Library should correct this on sf_close. */ + sfinfo.channels = 1 ; + sfinfo.format = filetype ; + + /* Save unclipped data to the file. */ + file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ; + test_write_double_or_die (file, 0, data_out, BUFFER_SIZE, __LINE__) ; + sf_close (file) ; + + memset (&sfinfo, 0, sizeof (sfinfo)) ; + + file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ; + sf_command (file, SFC_SET_SCALE_FLOAT_INT_READ, NULL, SF_TRUE) ; + + sfinfo.format &= (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK) ; + + if (sfinfo.format != (filetype & (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK))) + { printf ("\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n\n", __LINE__, filetype, sfinfo.format) ; + exit (1) ; + } ; + + if (sfinfo.frames != BUFFER_SIZE) + { printf ("\n\nLine %d: Incorrect number of frames in file (%d => %" PRId64 ").\n\n", __LINE__, BUFFER_SIZE, sfinfo.frames) ; + exit (1) ; + } ; + + if (sfinfo.channels != 1) + { printf ("\n\nLine %d: Incorrect number of channels in file.\n\n", __LINE__) ; + exit (1) ; + } ; + + check_log_buffer_or_die (file, __LINE__) ; + + sf_command (file, SFC_SET_CLIPPING, NULL, SF_TRUE) ; + test_read_short_or_die (file, 0, data_in, BUFFER_SIZE, __LINE__) ; + /*-sf_command (file, SFC_SET_NORM_DOUBLE, NULL, SF_FALSE) ;-*/ + sf_close (file) ; + + /* Check the first half. */ + max_value = 0 ; + for (k = 0 ; k < sfinfo.frames ; k++) + { /* Check if data_out has different sign from data_in. */ + if ((data_out [k] < 0.0 && data_in [k] > 0) || (data_out [k] > 0.0 && data_in [k] < 0)) + { printf ("\n\nLine %d: Data wrap around at index %d/%d (%f -> %d).\n\n", __LINE__, k, BUFFER_SIZE, data_out [k], data_in [k]) ; + exit (1) ; + } ; + max_value = (max_value > abs (data_in [k])) ? max_value : abs (data_in [k]) ; + } ; + + unlink (filename) ; + puts ("ok") ; +} /* dbl_short_clip_read_test */ +static void dbl_int_clip_read_test (const char *filename, int filetype) +{ SNDFILE *file ; + SF_INFO sfinfo ; + double *data_out ; + int *data_in, max_value ; + int k ; + + print_test_name ("dbl_int_clip_read_test", filename) ; + + data_out = buffer_out.dbl ; + data_in = buffer_in.i ; + + for (k = 0 ; k < BUFFER_SIZE ; k++) + data_out [k] = 0.995 * sin (4 * M_PI * k / BUFFER_SIZE) ; + data_out [BUFFER_SIZE / 8] = 1.0 ; + data_out [3 * BUFFER_SIZE / 8] = -1.000000001 ; + data_out [5 * BUFFER_SIZE / 8] = 1.0 ; + data_out [7 * BUFFER_SIZE / 8] = -1.000000001 ; + + memset (&sfinfo, 0, sizeof (sfinfo)) ; + sfinfo.samplerate = 44100 ; + sfinfo.frames = 123456789 ; /* Wrong length. Library should correct this on sf_close. */ + sfinfo.channels = 1 ; + sfinfo.format = filetype ; + + /* Save unclipped data to the file. */ + file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ; + test_write_double_or_die (file, 0, data_out, BUFFER_SIZE, __LINE__) ; + sf_close (file) ; + + memset (&sfinfo, 0, sizeof (sfinfo)) ; + + file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ; + sf_command (file, SFC_SET_SCALE_FLOAT_INT_READ, NULL, SF_TRUE) ; + + sfinfo.format &= (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK) ; + + if (sfinfo.format != (filetype & (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK))) + { printf ("\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n\n", __LINE__, filetype, sfinfo.format) ; + exit (1) ; + } ; + + if (sfinfo.frames != BUFFER_SIZE) + { printf ("\n\nLine %d: Incorrect number of frames in file (%d => %" PRId64 ").\n\n", __LINE__, BUFFER_SIZE, sfinfo.frames) ; + exit (1) ; + } ; + + if (sfinfo.channels != 1) + { printf ("\n\nLine %d: Incorrect number of channels in file.\n\n", __LINE__) ; + exit (1) ; + } ; + + check_log_buffer_or_die (file, __LINE__) ; + + sf_command (file, SFC_SET_CLIPPING, NULL, SF_TRUE) ; + test_read_int_or_die (file, 0, data_in, BUFFER_SIZE, __LINE__) ; + /*-sf_command (file, SFC_SET_NORM_DOUBLE, NULL, SF_FALSE) ;-*/ + sf_close (file) ; + + /* Check the first half. */ + max_value = 0 ; + for (k = 0 ; k < sfinfo.frames ; k++) + { /* Check if data_out has different sign from data_in. */ + if ((data_out [k] < 0.0 && data_in [k] > 0) || (data_out [k] > 0.0 && data_in [k] < 0)) + { printf ("\n\nLine %d: Data wrap around at index %d/%d (%f -> %d).\n\n", __LINE__, k, BUFFER_SIZE, data_out [k], data_in [k]) ; + exit (1) ; + } ; + max_value = (max_value > abs (data_in [k])) ? max_value : abs (data_in [k]) ; + } ; + + unlink (filename) ; + puts ("ok") ; +} /* dbl_int_clip_read_test */ + + +/*============================================================================== +*/ + + +static void short_flt_scale_write_test (const char *filename, int filetype) +{ SNDFILE *file ; + SF_INFO sfinfo ; + short *data_out ; + float *data_in, max_value ; + int k ; + + print_test_name ("short_flt_clip_write_test", filename) ; + + data_out = buffer_out.s ; + data_in = buffer_in.flt ; + + for (k = 0 ; k < BUFFER_SIZE ; k++) + data_out [k] = lrintf (0x7FFFF * 0.995 * sin (4 * M_PI * k / BUFFER_SIZE)) ; + + memset (&sfinfo, 0, sizeof (sfinfo)) ; + sfinfo.samplerate = 44100 ; + sfinfo.frames = 123456789 ; /* Wrong length. Library should correct this on sf_close. */ + sfinfo.channels = 1 ; + sfinfo.format = filetype ; + + file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ; + test_write_short_or_die (file, 0, data_out, BUFFER_SIZE, __LINE__) ; + sf_command (file, SFC_SET_SCALE_INT_FLOAT_WRITE, NULL, SF_TRUE) ; + test_write_short_or_die (file, 0, data_out, BUFFER_SIZE, __LINE__) ; + sf_command (file, SFC_SET_SCALE_INT_FLOAT_WRITE, NULL, SF_FALSE) ; + test_write_short_or_die (file, 0, data_out, BUFFER_SIZE, __LINE__) ; + sf_close (file) ; + + memset (&sfinfo, 0, sizeof (sfinfo)) ; + + file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ; + + sfinfo.format &= (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK) ; + + if (sfinfo.format != (filetype & (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK))) + { printf ("\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n\n", __LINE__, filetype, sfinfo.format) ; + exit (1) ; + } ; + + if (sfinfo.frames != 3 * BUFFER_SIZE) + { printf ("\n\nLine %d: Incorrect number of frames in file (%d => %" PRId64 ").\n\n", __LINE__, 3 * BUFFER_SIZE, sfinfo.frames) ; + exit (1) ; + } ; + + if (sfinfo.channels != 1) + { printf ("\n\nLine %d: Incorrect number of channels in file.\n\n", __LINE__) ; + exit (1) ; + } ; + + check_log_buffer_or_die (file, __LINE__) ; + + /* Check the first section. */ + test_read_float_or_die (file, 0, data_in, BUFFER_SIZE, __LINE__) ; + + max_value = 0.0 ; + for (k = 0 ; k < BUFFER_SIZE ; k++) + max_value = (max_value > fabs (data_in [k])) ? max_value : fabs (data_in [k]) ; + + if (max_value < 1000.0) + { printf ("\n\nLine %d: Max value (%f) < 1000.0.\n\n", __LINE__, max_value) ; + exit (1) ; + } ; + + /* Check the second section. */ + test_read_float_or_die (file, 0, data_in, BUFFER_SIZE, __LINE__) ; + + max_value = 0.0 ; + for (k = 0 ; k < BUFFER_SIZE ; k++) + max_value = (max_value > fabs (data_in [k])) ? max_value : fabs (data_in [k]) ; + + if (max_value > 1.0) + { printf ("\n\nLine %d: Max value (%f) > 1.0.\n\n", __LINE__, max_value) ; + exit (1) ; + } ; + + /* Check the third section. */ + test_read_float_or_die (file, 0, data_in, BUFFER_SIZE, __LINE__) ; + + max_value = 0.0 ; + for (k = 0 ; k < BUFFER_SIZE ; k++) + max_value = (max_value > fabs (data_in [k])) ? max_value : fabs (data_in [k]) ; + + if (max_value < 1000.0) + { printf ("\n\nLine %d: Max value (%f) < 1000.0.\n\n", __LINE__, max_value) ; + exit (1) ; + } ; + + sf_close (file) ; + + unlink (filename) ; + puts ("ok") ; +} /* short_flt_scale_write_test */ +static void short_dbl_scale_write_test (const char *filename, int filetype) +{ SNDFILE *file ; + SF_INFO sfinfo ; + short *data_out ; + double *data_in, max_value ; + int k ; + + print_test_name ("short_dbl_clip_write_test", filename) ; + + data_out = buffer_out.s ; + data_in = buffer_in.dbl ; + + for (k = 0 ; k < BUFFER_SIZE ; k++) + data_out [k] = lrint (0x7FFFF * 0.995 * sin (4 * M_PI * k / BUFFER_SIZE)) ; + + memset (&sfinfo, 0, sizeof (sfinfo)) ; + sfinfo.samplerate = 44100 ; + sfinfo.frames = 123456789 ; /* Wrong length. Library should correct this on sf_close. */ + sfinfo.channels = 1 ; + sfinfo.format = filetype ; + + file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ; + test_write_short_or_die (file, 0, data_out, BUFFER_SIZE, __LINE__) ; + sf_command (file, SFC_SET_SCALE_INT_FLOAT_WRITE, NULL, SF_TRUE) ; + test_write_short_or_die (file, 0, data_out, BUFFER_SIZE, __LINE__) ; + sf_command (file, SFC_SET_SCALE_INT_FLOAT_WRITE, NULL, SF_FALSE) ; + test_write_short_or_die (file, 0, data_out, BUFFER_SIZE, __LINE__) ; + sf_close (file) ; + + memset (&sfinfo, 0, sizeof (sfinfo)) ; + + file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ; + + sfinfo.format &= (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK) ; + + if (sfinfo.format != (filetype & (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK))) + { printf ("\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n\n", __LINE__, filetype, sfinfo.format) ; + exit (1) ; + } ; + + if (sfinfo.frames != 3 * BUFFER_SIZE) + { printf ("\n\nLine %d: Incorrect number of frames in file (%d => %" PRId64 ").\n\n", __LINE__, 3 * BUFFER_SIZE, sfinfo.frames) ; + exit (1) ; + } ; + + if (sfinfo.channels != 1) + { printf ("\n\nLine %d: Incorrect number of channels in file.\n\n", __LINE__) ; + exit (1) ; + } ; + + check_log_buffer_or_die (file, __LINE__) ; + + /* Check the first section. */ + test_read_double_or_die (file, 0, data_in, BUFFER_SIZE, __LINE__) ; + + max_value = 0.0 ; + for (k = 0 ; k < BUFFER_SIZE ; k++) + max_value = (max_value > fabs (data_in [k])) ? max_value : fabs (data_in [k]) ; + + if (max_value < 1000.0) + { printf ("\n\nLine %d: Max value (%f) < 1000.0.\n\n", __LINE__, max_value) ; + exit (1) ; + } ; + + /* Check the second section. */ + test_read_double_or_die (file, 0, data_in, BUFFER_SIZE, __LINE__) ; + + max_value = 0.0 ; + for (k = 0 ; k < BUFFER_SIZE ; k++) + max_value = (max_value > fabs (data_in [k])) ? max_value : fabs (data_in [k]) ; + + if (max_value > 1.0) + { printf ("\n\nLine %d: Max value (%f) > 1.0.\n\n", __LINE__, max_value) ; + exit (1) ; + } ; + + /* Check the third section. */ + test_read_double_or_die (file, 0, data_in, BUFFER_SIZE, __LINE__) ; + + max_value = 0.0 ; + for (k = 0 ; k < BUFFER_SIZE ; k++) + max_value = (max_value > fabs (data_in [k])) ? max_value : fabs (data_in [k]) ; + + if (max_value < 1000.0) + { printf ("\n\nLine %d: Max value (%f) < 1000.0.\n\n", __LINE__, max_value) ; + exit (1) ; + } ; + + sf_close (file) ; + + unlink (filename) ; + puts ("ok") ; +} /* short_dbl_scale_write_test */ + +static void int_flt_scale_write_test (const char *filename, int filetype) +{ SNDFILE *file ; + SF_INFO sfinfo ; + int *data_out ; + float *data_in, max_value ; + int k ; + + print_test_name ("int_flt_clip_write_test", filename) ; + + data_out = buffer_out.i ; + data_in = buffer_in.flt ; + + for (k = 0 ; k < BUFFER_SIZE ; k++) + data_out [k] = lrintf (0x7FFFFFFF * 0.995 * sin (4 * M_PI * k / BUFFER_SIZE)) ; + + memset (&sfinfo, 0, sizeof (sfinfo)) ; + sfinfo.samplerate = 44100 ; + sfinfo.frames = 123456789 ; /* Wrong length. Library should correct this on sf_close. */ + sfinfo.channels = 1 ; + sfinfo.format = filetype ; + + file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ; + test_write_int_or_die (file, 0, data_out, BUFFER_SIZE, __LINE__) ; + sf_command (file, SFC_SET_SCALE_INT_FLOAT_WRITE, NULL, SF_TRUE) ; + test_write_int_or_die (file, 0, data_out, BUFFER_SIZE, __LINE__) ; + sf_command (file, SFC_SET_SCALE_INT_FLOAT_WRITE, NULL, SF_FALSE) ; + test_write_int_or_die (file, 0, data_out, BUFFER_SIZE, __LINE__) ; + sf_close (file) ; + + memset (&sfinfo, 0, sizeof (sfinfo)) ; + + file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ; + + sfinfo.format &= (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK) ; + + if (sfinfo.format != (filetype & (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK))) + { printf ("\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n\n", __LINE__, filetype, sfinfo.format) ; + exit (1) ; + } ; + + if (sfinfo.frames != 3 * BUFFER_SIZE) + { printf ("\n\nLine %d: Incorrect number of frames in file (%d => %" PRId64 ").\n\n", __LINE__, 3 * BUFFER_SIZE, sfinfo.frames) ; + exit (1) ; + } ; + + if (sfinfo.channels != 1) + { printf ("\n\nLine %d: Incorrect number of channels in file.\n\n", __LINE__) ; + exit (1) ; + } ; + + check_log_buffer_or_die (file, __LINE__) ; + + /* Check the first section. */ + test_read_float_or_die (file, 0, data_in, BUFFER_SIZE, __LINE__) ; + + max_value = 0.0 ; + for (k = 0 ; k < BUFFER_SIZE ; k++) + max_value = (max_value > fabs (data_in [k])) ? max_value : fabs (data_in [k]) ; + + if (max_value < 1000.0) + { printf ("\n\nLine %d: Max value (%f) < 1000.0.\n\n", __LINE__, max_value) ; + exit (1) ; + } ; + + /* Check the second section. */ + test_read_float_or_die (file, 0, data_in, BUFFER_SIZE, __LINE__) ; + + max_value = 0.0 ; + for (k = 0 ; k < BUFFER_SIZE ; k++) + max_value = (max_value > fabs (data_in [k])) ? max_value : fabs (data_in [k]) ; + + if (max_value > 1.0) + { printf ("\n\nLine %d: Max value (%f) > 1.0.\n\n", __LINE__, max_value) ; + exit (1) ; + } ; + + /* Check the third section. */ + test_read_float_or_die (file, 0, data_in, BUFFER_SIZE, __LINE__) ; + + max_value = 0.0 ; + for (k = 0 ; k < BUFFER_SIZE ; k++) + max_value = (max_value > fabs (data_in [k])) ? max_value : fabs (data_in [k]) ; + + if (max_value < 1000.0) + { printf ("\n\nLine %d: Max value (%f) < 1000.0.\n\n", __LINE__, max_value) ; + exit (1) ; + } ; + + sf_close (file) ; + + unlink (filename) ; + puts ("ok") ; +} /* int_flt_scale_write_test */ +static void int_dbl_scale_write_test (const char *filename, int filetype) +{ SNDFILE *file ; + SF_INFO sfinfo ; + int *data_out ; + double *data_in, max_value ; + int k ; + + print_test_name ("int_dbl_clip_write_test", filename) ; + + data_out = buffer_out.i ; + data_in = buffer_in.dbl ; + + for (k = 0 ; k < BUFFER_SIZE ; k++) + data_out [k] = lrint (0x7FFFFFFF * 0.995 * sin (4 * M_PI * k / BUFFER_SIZE)) ; + + memset (&sfinfo, 0, sizeof (sfinfo)) ; + sfinfo.samplerate = 44100 ; + sfinfo.frames = 123456789 ; /* Wrong length. Library should correct this on sf_close. */ + sfinfo.channels = 1 ; + sfinfo.format = filetype ; + + file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ; + test_write_int_or_die (file, 0, data_out, BUFFER_SIZE, __LINE__) ; + sf_command (file, SFC_SET_SCALE_INT_FLOAT_WRITE, NULL, SF_TRUE) ; + test_write_int_or_die (file, 0, data_out, BUFFER_SIZE, __LINE__) ; + sf_command (file, SFC_SET_SCALE_INT_FLOAT_WRITE, NULL, SF_FALSE) ; + test_write_int_or_die (file, 0, data_out, BUFFER_SIZE, __LINE__) ; + sf_close (file) ; + + memset (&sfinfo, 0, sizeof (sfinfo)) ; + + file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ; + + sfinfo.format &= (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK) ; + + if (sfinfo.format != (filetype & (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK))) + { printf ("\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n\n", __LINE__, filetype, sfinfo.format) ; + exit (1) ; + } ; + + if (sfinfo.frames != 3 * BUFFER_SIZE) + { printf ("\n\nLine %d: Incorrect number of frames in file (%d => %" PRId64 ").\n\n", __LINE__, 3 * BUFFER_SIZE, sfinfo.frames) ; + exit (1) ; + } ; + + if (sfinfo.channels != 1) + { printf ("\n\nLine %d: Incorrect number of channels in file.\n\n", __LINE__) ; + exit (1) ; + } ; + + check_log_buffer_or_die (file, __LINE__) ; + + /* Check the first section. */ + test_read_double_or_die (file, 0, data_in, BUFFER_SIZE, __LINE__) ; + + max_value = 0.0 ; + for (k = 0 ; k < BUFFER_SIZE ; k++) + max_value = (max_value > fabs (data_in [k])) ? max_value : fabs (data_in [k]) ; + + if (max_value < 1000.0) + { printf ("\n\nLine %d: Max value (%f) < 1000.0.\n\n", __LINE__, max_value) ; + exit (1) ; + } ; + + /* Check the second section. */ + test_read_double_or_die (file, 0, data_in, BUFFER_SIZE, __LINE__) ; + + max_value = 0.0 ; + for (k = 0 ; k < BUFFER_SIZE ; k++) + max_value = (max_value > fabs (data_in [k])) ? max_value : fabs (data_in [k]) ; + + if (max_value > 1.0) + { printf ("\n\nLine %d: Max value (%f) > 1.0.\n\n", __LINE__, max_value) ; + exit (1) ; + } ; + + /* Check the third section. */ + test_read_double_or_die (file, 0, data_in, BUFFER_SIZE, __LINE__) ; + + max_value = 0.0 ; + for (k = 0 ; k < BUFFER_SIZE ; k++) + max_value = (max_value > fabs (data_in [k])) ? max_value : fabs (data_in [k]) ; + + if (max_value < 1000.0) + { printf ("\n\nLine %d: Max value (%f) < 1000.0.\n\n", __LINE__, max_value) ; + exit (1) ; + } ; + + sf_close (file) ; + + unlink (filename) ; + puts ("ok") ; +} /* int_dbl_scale_write_test */ + + + diff --git a/tests/utils.c b/tests/utils.c new file mode 100644 index 0000000..7a3e4e3 --- /dev/null +++ b/tests/utils.c @@ -0,0 +1,1173 @@ +/* +** Copyright (C) 2002-2016 Erik de Castro Lopo +** +** This program is free software; you can redistribute it and/or modify +** it under the terms of the GNU General Public License as published by +** the Free Software Foundation; either version 2 of the License, or +** (at your option) any later version. +** +** This program is distributed in the hope that it will be useful, +** but WITHOUT ANY WARRANTY; without even the implied warranty of +** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +** GNU General Public License for more details. +** +** You should have received a copy of the GNU General Public License +** along with this program; if not, write to the Free Software +** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +*/ + +/* +** Utility functions to make writing the test suite easier. +** +** The .c and .h files were generated automagically with Autogen from +** the files utils.def and utils.tpl. +*/ + + + +#include "sfconfig.h" + +#include +#include +#include + +#if HAVE_UNISTD_H +#include +#endif + +#if (HAVE_DECL_S_IRGRP == 0) +#include +#endif + +#include +#include +#include +#include +#include +#include + +#include + +#include "utils.h" + +#ifndef M_PI +#define M_PI 3.14159265358979323846264338 +#endif + +#define LOG_BUFFER_SIZE 2048 + +/* +** Neat solution to the Win32/OS2 binary file flage requirement. +** If O_BINARY isn't already defined by the inclusion of the system +** headers, set it to zero. +*/ +#ifndef O_BINARY +#define O_BINARY 0 +#endif + + +void +gen_windowed_sine_float (float *data, int len, double maximum) +{ int k ; + + memset (data, 0, len * sizeof (float)) ; + + len = (5 * len) / 6 ; + + for (k = 0 ; k < len ; k++) + { data [k] = sin (2.0 * k * M_PI * 1.0 / 32.0 + 0.4) ; + + /* Apply Hanning Window. */ + data [k] *= maximum * (0.5 - 0.5 * cos (2.0 * M_PI * k / ((len) - 1))) ; + } + + return ; +} /* gen_windowed_sine_float */ + +void +gen_windowed_sine_double (double *data, int len, double maximum) +{ int k ; + + memset (data, 0, len * sizeof (double)) ; + + len = (5 * len) / 6 ; + + for (k = 0 ; k < len ; k++) + { data [k] = sin (2.0 * k * M_PI * 1.0 / 32.0 + 0.4) ; + + /* Apply Hanning Window. */ + data [k] *= maximum * (0.5 - 0.5 * cos (2.0 * M_PI * k / ((len) - 1))) ; + } + + return ; +} /* gen_windowed_sine_double */ + + +void +create_short_sndfile (const char *filename, int format, int channels) +{ short data [2 * 3 * 4 * 5 * 6 * 7] = { 0, } ; + SNDFILE *file ; + SF_INFO sfinfo ; + + sfinfo.samplerate = 44100 ; + sfinfo.channels = channels ; + sfinfo.format = format ; + + if ((file = sf_open (filename, SFM_WRITE, &sfinfo)) == NULL) + { printf ("Error (%s, %d) : sf_open failed : %s\n", __FILE__, __LINE__, sf_strerror (file)) ; + exit (1) ; + } ; + + sf_write_short (file, data, ARRAY_LEN (data)) ; + + sf_close (file) ; +} /* create_short_sndfile */ + +void +check_file_hash_or_die (const char *filename, uint64_t target_hash, int line_num) +{ static unsigned char buf [4096] ; + uint64_t cksum ; + FILE *file ; + int k, read_count ; + + memset (buf, 0, sizeof (buf)) ; + + /* The 'b' in the mode string means binary for Win32. */ + if ((file = fopen (filename, "rb")) == NULL) + { printf ("\n\nLine %d: could not open file '%s'\n\n", line_num, filename) ; + exit (1) ; + } ; + + cksum = 0 ; + + while ((read_count = fread (buf, 1, sizeof (buf), file))) + for (k = 0 ; k < read_count ; k++) + cksum = (cksum * 511 + buf [k]) & 0xfffffffffffff ; + + fclose (file) ; + + if (target_hash == 0) + { printf (" 0x%" PRIx64 "\n", cksum) ; + return ; + } ; + + if (cksum != target_hash) + { printf ("\n\nLine %d: incorrect hash value 0x%" PRIx64 " should be 0x%" PRIx64 ".\n\n", line_num, cksum, target_hash) ; + exit (1) ; + } ; + + return ; +} /* check_file_hash_or_die */ + +void +print_test_name (const char *test, const char *filename) +{ int count ; + + if (test == NULL) + { printf (__FILE__ ": bad test of filename parameter.\n") ; + exit (1) ; + } ; + + if (filename == NULL || strlen (filename) == 0) + { printf (" %-30s : ", test) ; + count = 25 ; + } + else + { printf (" %-30s : %s ", test, filename) ; + count = 24 - strlen (filename) ; + } ; + + while (count -- > 0) + putchar ('.') ; + putchar (' ') ; + + fflush (stdout) ; +} /* print_test_name */ + +void +dump_data_to_file (const char *filename, const void *data, unsigned int datalen) +{ FILE *file ; + + if ((file = fopen (filename, "wb")) == NULL) + { printf ("\n\nLine %d : could not open file : %s\n\n", __LINE__, filename) ; + exit (1) ; + } ; + + if (fwrite (data, 1, datalen, file) != datalen) + { printf ("\n\nLine %d : fwrite failed.\n\n", __LINE__) ; + exit (1) ; + } ; + + fclose (file) ; + +} /* dump_data_to_file */ + +/*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- +*/ + +static char octfilename [] = "error.dat" ; + +int +oct_save_short (const short *a, const short *b, int len) +{ FILE *file ; + int k ; + + if (! (file = fopen (octfilename, "w"))) + return 1 ; + + fprintf (file, "# Not created by Octave\n") ; + + fprintf (file, "# name: a\n") ; + fprintf (file, "# type: matrix\n") ; + fprintf (file, "# rows: %d\n", len) ; + fprintf (file, "# columns: 1\n") ; + + for (k = 0 ; k < len ; k++) + fprintf (file, "% d" "\n", a [k]) ; + + fprintf (file, "# name: b\n") ; + fprintf (file, "# type: matrix\n") ; + fprintf (file, "# rows: %d\n", len) ; + fprintf (file, "# columns: 1\n") ; + + for (k = 0 ; k < len ; k++) + fprintf (file, "% d" "\n", b [k]) ; + + fclose (file) ; + return 0 ; +} /* oct_save_short */ +int +oct_save_int (const int *a, const int *b, int len) +{ FILE *file ; + int k ; + + if (! (file = fopen (octfilename, "w"))) + return 1 ; + + fprintf (file, "# Not created by Octave\n") ; + + fprintf (file, "# name: a\n") ; + fprintf (file, "# type: matrix\n") ; + fprintf (file, "# rows: %d\n", len) ; + fprintf (file, "# columns: 1\n") ; + + for (k = 0 ; k < len ; k++) + fprintf (file, "% d" "\n", a [k]) ; + + fprintf (file, "# name: b\n") ; + fprintf (file, "# type: matrix\n") ; + fprintf (file, "# rows: %d\n", len) ; + fprintf (file, "# columns: 1\n") ; + + for (k = 0 ; k < len ; k++) + fprintf (file, "% d" "\n", b [k]) ; + + fclose (file) ; + return 0 ; +} /* oct_save_int */ +int +oct_save_float (const float *a, const float *b, int len) +{ FILE *file ; + int k ; + + if (! (file = fopen (octfilename, "w"))) + return 1 ; + + fprintf (file, "# Not created by Octave\n") ; + + fprintf (file, "# name: a\n") ; + fprintf (file, "# type: matrix\n") ; + fprintf (file, "# rows: %d\n", len) ; + fprintf (file, "# columns: 1\n") ; + + for (k = 0 ; k < len ; k++) + fprintf (file, "% g" "\n", a [k]) ; + + fprintf (file, "# name: b\n") ; + fprintf (file, "# type: matrix\n") ; + fprintf (file, "# rows: %d\n", len) ; + fprintf (file, "# columns: 1\n") ; + + for (k = 0 ; k < len ; k++) + fprintf (file, "% g" "\n", b [k]) ; + + fclose (file) ; + return 0 ; +} /* oct_save_float */ +int +oct_save_double (const double *a, const double *b, int len) +{ FILE *file ; + int k ; + + if (! (file = fopen (octfilename, "w"))) + return 1 ; + + fprintf (file, "# Not created by Octave\n") ; + + fprintf (file, "# name: a\n") ; + fprintf (file, "# type: matrix\n") ; + fprintf (file, "# rows: %d\n", len) ; + fprintf (file, "# columns: 1\n") ; + + for (k = 0 ; k < len ; k++) + fprintf (file, "% g" "\n", a [k]) ; + + fprintf (file, "# name: b\n") ; + fprintf (file, "# type: matrix\n") ; + fprintf (file, "# rows: %d\n", len) ; + fprintf (file, "# columns: 1\n") ; + + for (k = 0 ; k < len ; k++) + fprintf (file, "% g" "\n", b [k]) ; + + fclose (file) ; + return 0 ; +} /* oct_save_double */ + + +void +check_log_buffer_or_die (SNDFILE *file, int line_num) +{ static char buffer [LOG_BUFFER_SIZE] ; + int count ; + + memset (buffer, 0, sizeof (buffer)) ; + + /* Get the log buffer data. */ + count = sf_command (file, SFC_GET_LOG_INFO, buffer, LOG_BUFFER_SIZE) ; + + if (LOG_BUFFER_SIZE - count < 2) + { printf ("\n\nLine %d : Possible long log buffer.\n", line_num) ; + exit (1) ; + } + + /* Look for "Should" */ + if (strstr (buffer, "ould")) + { printf ("\n\nLine %d : Log buffer contains `ould'. Dumping.\n", line_num) ; + puts (buffer) ; + exit (1) ; + } ; + + /* Look for "**" */ + if (strstr (buffer, "*")) + { printf ("\n\nLine %d : Log buffer contains `*'. Dumping.\n", line_num) ; + puts (buffer) ; + exit (1) ; + } ; + + /* Look for "Should" */ + if (strstr (buffer, "nknown marker")) + { printf ("\n\nLine %d : Log buffer contains `nknown marker'. Dumping.\n", line_num) ; + puts (buffer) ; + exit (1) ; + } ; + + return ; +} /* check_log_buffer_or_die */ + +int +string_in_log_buffer (SNDFILE *file, const char *s) +{ static char buffer [LOG_BUFFER_SIZE] ; + int count ; + + memset (buffer, 0, sizeof (buffer)) ; + + /* Get the log buffer data. */ + count = sf_command (file, SFC_GET_LOG_INFO, buffer, LOG_BUFFER_SIZE) ; + + if (LOG_BUFFER_SIZE - count < 2) + { printf ("Possible long log buffer.\n") ; + exit (1) ; + } + + /* Look for string */ + return strstr (buffer, s) ? SF_TRUE : SF_FALSE ; +} /* string_in_log_buffer */ + +void +hexdump_file (const char * filename, sf_count_t offset, sf_count_t length) +{ + FILE * file ; + char buffer [16] ; + int k, m, ch, readcount ; + + if (length > 1000000) + { printf ("\n\nError : length (%" PRId64 ") too long.\n\n", offset) ; + exit (1) ; + } ; + + if ((file = fopen (filename, "r")) == NULL) + { printf ("\n\nError : hexdump_file (%s) could not open file for read.\n\n", filename) ; + exit (1) ; + } ; + + if (fseek (file, offset, SEEK_SET) != 0) + { printf ("\n\nError : fseek(file, %" PRId64 ", SEEK_SET) failed : %s\n\n", offset, strerror (errno)) ; + exit (1) ; + } ; + + puts ("\n\n") ; + + for (k = 0 ; k < length ; k+= sizeof (buffer)) + { readcount = fread (buffer, 1, sizeof (buffer), file) ; + + printf ("%08" PRIx64 " : ", offset + k) ; + + for (m = 0 ; m < readcount ; m++) + printf ("%02x ", buffer [m] & 0xFF) ; + + for (m = readcount ; m < SIGNED_SIZEOF (buffer) ; m++) + printf (" ") ; + + printf (" ") ; + for (m = 0 ; m < readcount ; m++) + { ch = isprint (buffer [m]) ? buffer [m] : '.' ; + putchar (ch) ; + } ; + + if (readcount < SIGNED_SIZEOF (buffer)) + break ; + + putchar ('\n') ; + } ; + + puts ("\n") ; + + fclose (file) ; +} /* hexdump_file */ + +void +dump_log_buffer (SNDFILE *file) +{ static char buffer [LOG_BUFFER_SIZE] ; + + memset (buffer, 0, sizeof (buffer)) ; + + /* Get the log buffer data. */ + sf_command (file, SFC_GET_LOG_INFO, buffer, LOG_BUFFER_SIZE) ; + + if (strlen (buffer) < 1) + puts ("Log buffer empty.\n") ; + else + puts (buffer) ; + + return ; +} /* dump_log_buffer */ + +void +test_sf_format_or_die (const SF_INFO *info, int line_num) +{ int res ; + + if ((res = sf_format_check (info)) != 1) + { printf ("\n\nLine %d : sf_format_check returned error (%d)\n\n", line_num,res) ; + exit (1) ; + } ; + + return ; +} /* test_sf_format_or_die */ + +SNDFILE * +test_open_file_or_die (const char *filename, int mode, SF_INFO *sfinfo, int allow_fd, int line_num) +{ static int count = 0 ; + + SNDFILE *file ; + const char *modestr, *func_name ; + int oflags = 0, omode = 0, err ; + + /* + ** Need to test both sf_open() and sf_open_fd(). + ** Do so alternately. + */ + switch (mode) + { case SFM_READ : + modestr = "SFM_READ" ; + oflags = O_RDONLY | O_BINARY ; + omode = 0 ; + break ; + + case SFM_WRITE : + modestr = "SFM_WRITE" ; + oflags = O_WRONLY | O_CREAT | O_TRUNC | O_BINARY ; + omode = S_IRUSR | S_IWUSR | S_IRGRP ; + break ; + + case SFM_RDWR : + modestr = "SFM_RDWR" ; + oflags = O_RDWR | O_CREAT | O_BINARY ; + omode = S_IRUSR | S_IWUSR | S_IRGRP ; + break ; + default : + printf ("\n\nLine %d: Bad mode.\n", line_num) ; + fflush (stdout) ; + exit (1) ; + } ; + + if (OS_IS_WIN32) + { /* Windows does not understand and ignores the S_IRGRP flag, but Wine + ** gives a run time warning message, so just clear it. + */ + omode &= ~S_IRGRP ; + } ; + + if (allow_fd && ((++count) & 1) == 1) + { int fd ; + + /* Only use the three argument open() function if omode != 0. */ + fd = (omode == 0) ? open (filename, oflags) : open (filename, oflags, omode) ; + + if (fd < 0) + { printf ("\n\n%s : open failed : %s\n", __func__, strerror (errno)) ; + exit (1) ; + } ; + + func_name = "sf_open_fd" ; + file = sf_open_fd (fd, mode, sfinfo, SF_TRUE) ; + } + else + { func_name = "sf_open" ; + file = sf_open (filename, mode, sfinfo) ; + } ; + + if (file == NULL) + { printf ("\n\nLine %d: %s (%s) failed : %s\n\n", line_num, func_name, modestr, sf_strerror (NULL)) ; + dump_log_buffer (file) ; + exit (1) ; + } ; + + err = sf_error (file) ; + if (err != SF_ERR_NO_ERROR) + { printf ("\n\nLine %d : sf_error : %s\n\n", line_num, sf_error_number (err)) ; + dump_log_buffer (file) ; + exit (1) ; + } ; + + return file ; +} /* test_open_file_or_die */ + +void +test_read_write_position_or_die (SNDFILE *file, int line_num, int pass, sf_count_t read_pos, sf_count_t write_pos) +{ sf_count_t pos ; + + /* Check the current read position. */ + if (read_pos >= 0 && (pos = sf_seek (file, 0, SEEK_CUR | SFM_READ)) != read_pos) + { printf ("\n\nLine %d ", line_num) ; + if (pass > 0) + printf ("(pass %d): ", pass) ; + printf ("Read position (%" PRId64 ") should be %" PRId64 ".\n", pos, read_pos) ; + exit (1) ; + } ; + + /* Check the current write position. */ + if (write_pos >= 0 && (pos = sf_seek (file, 0, SEEK_CUR | SFM_WRITE)) != write_pos) + { printf ("\n\nLine %d", line_num) ; + if (pass > 0) + printf (" (pass %d)", pass) ; + printf (" : Write position (%" PRId64 ") should be %" PRId64 ".\n", pos, write_pos) ; + exit (1) ; + } ; + + return ; +} /* test_read_write_position */ + +void +test_seek_or_die (SNDFILE *file, sf_count_t offset, int whence, sf_count_t new_pos, int channels, int line_num) +{ sf_count_t position ; + const char *channel_name, *whence_name ; + + switch (whence) + { case SEEK_SET : + whence_name = "SEEK_SET" ; + break ; + case SEEK_CUR : + whence_name = "SEEK_CUR" ; + break ; + case SEEK_END : + whence_name = "SEEK_END" ; + break ; + + /* SFM_READ */ + case SEEK_SET | SFM_READ : + whence_name = "SFM_READ | SEEK_SET" ; + break ; + case SEEK_CUR | SFM_READ : + whence_name = "SFM_READ | SEEK_CUR" ; + break ; + case SEEK_END | SFM_READ : + whence_name = "SFM_READ | SEEK_END" ; + break ; + + /* SFM_WRITE */ + case SEEK_SET | SFM_WRITE : + whence_name = "SFM_WRITE | SEEK_SET" ; + break ; + case SEEK_CUR | SFM_WRITE : + whence_name = "SFM_WRITE | SEEK_CUR" ; + break ; + case SEEK_END | SFM_WRITE : + whence_name = "SFM_WRITE | SEEK_END" ; + break ; + + default : + printf ("\n\nLine %d: bad whence parameter.\n", line_num) ; + exit (1) ; + } ; + + channel_name = (channels == 1) ? "Mono" : "Stereo" ; + + if ((position = sf_seek (file, offset, whence)) != new_pos) + { printf ("\n\nLine %d : %s : sf_seek (file, %" PRId64 ", %s) returned %" PRId64 " (should be %" PRId64 ").\n\n", + line_num, channel_name, offset, whence_name, position, new_pos) ; + exit (1) ; + } ; + +} /* test_seek_or_die */ + + + +void +test_read_short_or_die (SNDFILE *file, int pass, short *test, sf_count_t items, int line_num) +{ sf_count_t count ; + + if ((count = sf_read_short (file, test, items)) != items) + { printf ("\n\nLine %d", line_num) ; + if (pass > 0) + printf (" (pass %d)", pass) ; + printf (" : sf_read_short failed with short read (%" PRId64 " => %" PRId64 ").\n", + items, count) ; + fflush (stdout) ; + puts (sf_strerror (file)) ; + exit (1) ; + } ; + + return ; +} /* test_read_short_or_die */ + +void +test_read_int_or_die (SNDFILE *file, int pass, int *test, sf_count_t items, int line_num) +{ sf_count_t count ; + + if ((count = sf_read_int (file, test, items)) != items) + { printf ("\n\nLine %d", line_num) ; + if (pass > 0) + printf (" (pass %d)", pass) ; + printf (" : sf_read_int failed with short read (%" PRId64 " => %" PRId64 ").\n", + items, count) ; + fflush (stdout) ; + puts (sf_strerror (file)) ; + exit (1) ; + } ; + + return ; +} /* test_read_int_or_die */ + +void +test_read_float_or_die (SNDFILE *file, int pass, float *test, sf_count_t items, int line_num) +{ sf_count_t count ; + + if ((count = sf_read_float (file, test, items)) != items) + { printf ("\n\nLine %d", line_num) ; + if (pass > 0) + printf (" (pass %d)", pass) ; + printf (" : sf_read_float failed with short read (%" PRId64 " => %" PRId64 ").\n", + items, count) ; + fflush (stdout) ; + puts (sf_strerror (file)) ; + exit (1) ; + } ; + + return ; +} /* test_read_float_or_die */ + +void +test_read_double_or_die (SNDFILE *file, int pass, double *test, sf_count_t items, int line_num) +{ sf_count_t count ; + + if ((count = sf_read_double (file, test, items)) != items) + { printf ("\n\nLine %d", line_num) ; + if (pass > 0) + printf (" (pass %d)", pass) ; + printf (" : sf_read_double failed with short read (%" PRId64 " => %" PRId64 ").\n", + items, count) ; + fflush (stdout) ; + puts (sf_strerror (file)) ; + exit (1) ; + } ; + + return ; +} /* test_read_double_or_die */ + + +void +test_readf_short_or_die (SNDFILE *file, int pass, short *test, sf_count_t frames, int line_num) +{ sf_count_t count ; + + if ((count = sf_readf_short (file, test, frames)) != frames) + { printf ("\n\nLine %d", line_num) ; + if (pass > 0) + printf (" (pass %d)", pass) ; + printf (" : sf_readf_short failed with short readf (%" PRId64 " => %" PRId64 ").\n", + frames, count) ; + fflush (stdout) ; + puts (sf_strerror (file)) ; + exit (1) ; + } ; + + return ; +} /* test_readf_short_or_die */ + +void +test_readf_int_or_die (SNDFILE *file, int pass, int *test, sf_count_t frames, int line_num) +{ sf_count_t count ; + + if ((count = sf_readf_int (file, test, frames)) != frames) + { printf ("\n\nLine %d", line_num) ; + if (pass > 0) + printf (" (pass %d)", pass) ; + printf (" : sf_readf_int failed with short readf (%" PRId64 " => %" PRId64 ").\n", + frames, count) ; + fflush (stdout) ; + puts (sf_strerror (file)) ; + exit (1) ; + } ; + + return ; +} /* test_readf_int_or_die */ + +void +test_readf_float_or_die (SNDFILE *file, int pass, float *test, sf_count_t frames, int line_num) +{ sf_count_t count ; + + if ((count = sf_readf_float (file, test, frames)) != frames) + { printf ("\n\nLine %d", line_num) ; + if (pass > 0) + printf (" (pass %d)", pass) ; + printf (" : sf_readf_float failed with short readf (%" PRId64 " => %" PRId64 ").\n", + frames, count) ; + fflush (stdout) ; + puts (sf_strerror (file)) ; + exit (1) ; + } ; + + return ; +} /* test_readf_float_or_die */ + +void +test_readf_double_or_die (SNDFILE *file, int pass, double *test, sf_count_t frames, int line_num) +{ sf_count_t count ; + + if ((count = sf_readf_double (file, test, frames)) != frames) + { printf ("\n\nLine %d", line_num) ; + if (pass > 0) + printf (" (pass %d)", pass) ; + printf (" : sf_readf_double failed with short readf (%" PRId64 " => %" PRId64 ").\n", + frames, count) ; + fflush (stdout) ; + puts (sf_strerror (file)) ; + exit (1) ; + } ; + + return ; +} /* test_readf_double_or_die */ + + +void +test_read_raw_or_die (SNDFILE *file, int pass, void *test, sf_count_t items, int line_num) +{ sf_count_t count ; + + if ((count = sf_read_raw (file, test, items)) != items) + { printf ("\n\nLine %d", line_num) ; + if (pass > 0) + printf (" (pass %d)", pass) ; + printf (" : sf_read_raw failed with short read (%" PRId64 " => %" PRId64 ").\n", items, count) ; + fflush (stdout) ; + puts (sf_strerror (file)) ; + exit (1) ; + } ; + + return ; +} /* test_read_raw_or_die */ + + + +void +test_write_short_or_die (SNDFILE *file, int pass, const short *test, sf_count_t items, int line_num) +{ sf_count_t count ; + + if ((count = sf_write_short (file, test, items)) != items) + { printf ("\n\nLine %d", line_num) ; + if (pass > 0) + printf (" (pass %d)", pass) ; + printf (" : sf_write_short failed with short write (%" PRId64 " => %" PRId64 ").\n", + items, count) ; + fflush (stdout) ; + puts (sf_strerror (file)) ; + exit (1) ; + } ; + + return ; +} /* test_write_short_or_die */ + +void +test_write_int_or_die (SNDFILE *file, int pass, const int *test, sf_count_t items, int line_num) +{ sf_count_t count ; + + if ((count = sf_write_int (file, test, items)) != items) + { printf ("\n\nLine %d", line_num) ; + if (pass > 0) + printf (" (pass %d)", pass) ; + printf (" : sf_write_int failed with short write (%" PRId64 " => %" PRId64 ").\n", + items, count) ; + fflush (stdout) ; + puts (sf_strerror (file)) ; + exit (1) ; + } ; + + return ; +} /* test_write_int_or_die */ + +void +test_write_float_or_die (SNDFILE *file, int pass, const float *test, sf_count_t items, int line_num) +{ sf_count_t count ; + + if ((count = sf_write_float (file, test, items)) != items) + { printf ("\n\nLine %d", line_num) ; + if (pass > 0) + printf (" (pass %d)", pass) ; + printf (" : sf_write_float failed with short write (%" PRId64 " => %" PRId64 ").\n", + items, count) ; + fflush (stdout) ; + puts (sf_strerror (file)) ; + exit (1) ; + } ; + + return ; +} /* test_write_float_or_die */ + +void +test_write_double_or_die (SNDFILE *file, int pass, const double *test, sf_count_t items, int line_num) +{ sf_count_t count ; + + if ((count = sf_write_double (file, test, items)) != items) + { printf ("\n\nLine %d", line_num) ; + if (pass > 0) + printf (" (pass %d)", pass) ; + printf (" : sf_write_double failed with short write (%" PRId64 " => %" PRId64 ").\n", + items, count) ; + fflush (stdout) ; + puts (sf_strerror (file)) ; + exit (1) ; + } ; + + return ; +} /* test_write_double_or_die */ + + +void +test_writef_short_or_die (SNDFILE *file, int pass, const short *test, sf_count_t frames, int line_num) +{ sf_count_t count ; + + if ((count = sf_writef_short (file, test, frames)) != frames) + { printf ("\n\nLine %d", line_num) ; + if (pass > 0) + printf (" (pass %d)", pass) ; + printf (" : sf_writef_short failed with short writef (%" PRId64 " => %" PRId64 ").\n", + frames, count) ; + fflush (stdout) ; + puts (sf_strerror (file)) ; + exit (1) ; + } ; + + return ; +} /* test_writef_short_or_die */ + +void +test_writef_int_or_die (SNDFILE *file, int pass, const int *test, sf_count_t frames, int line_num) +{ sf_count_t count ; + + if ((count = sf_writef_int (file, test, frames)) != frames) + { printf ("\n\nLine %d", line_num) ; + if (pass > 0) + printf (" (pass %d)", pass) ; + printf (" : sf_writef_int failed with short writef (%" PRId64 " => %" PRId64 ").\n", + frames, count) ; + fflush (stdout) ; + puts (sf_strerror (file)) ; + exit (1) ; + } ; + + return ; +} /* test_writef_int_or_die */ + +void +test_writef_float_or_die (SNDFILE *file, int pass, const float *test, sf_count_t frames, int line_num) +{ sf_count_t count ; + + if ((count = sf_writef_float (file, test, frames)) != frames) + { printf ("\n\nLine %d", line_num) ; + if (pass > 0) + printf (" (pass %d)", pass) ; + printf (" : sf_writef_float failed with short writef (%" PRId64 " => %" PRId64 ").\n", + frames, count) ; + fflush (stdout) ; + puts (sf_strerror (file)) ; + exit (1) ; + } ; + + return ; +} /* test_writef_float_or_die */ + +void +test_writef_double_or_die (SNDFILE *file, int pass, const double *test, sf_count_t frames, int line_num) +{ sf_count_t count ; + + if ((count = sf_writef_double (file, test, frames)) != frames) + { printf ("\n\nLine %d", line_num) ; + if (pass > 0) + printf (" (pass %d)", pass) ; + printf (" : sf_writef_double failed with short writef (%" PRId64 " => %" PRId64 ").\n", + frames, count) ; + fflush (stdout) ; + puts (sf_strerror (file)) ; + exit (1) ; + } ; + + return ; +} /* test_writef_double_or_die */ + + +void +test_write_raw_or_die (SNDFILE *file, int pass, const void *test, sf_count_t items, int line_num) +{ sf_count_t count ; + + if ((count = sf_write_raw (file, test, items)) != items) + { printf ("\n\nLine %d", line_num) ; + if (pass > 0) + printf (" (pass %d)", pass) ; + printf (" : sf_write_raw failed with short write (%" PRId64 " => %" PRId64 ").\n", items, count) ; + fflush (stdout) ; + puts (sf_strerror (file)) ; + exit (1) ; + } ; + + return ; +} /* test_write_raw_or_die */ + + +void +compare_short_or_die (const short *expected, const short *actual, unsigned count, int line_num) +{ + unsigned k ; + + for (k = 0 ; k < count ; k++) + if (expected [k] != actual [k]) + { printf ("\n\nLine %d : Error at index %d, got " "% d" ", should be " "% d" ".\n\n", line_num, k, actual [k], expected [k]) ; + exit (1) ; + } ; + + return ; +} /* compare_short_or_die */ +void +compare_int_or_die (const int *expected, const int *actual, unsigned count, int line_num) +{ + unsigned k ; + + for (k = 0 ; k < count ; k++) + if (expected [k] != actual [k]) + { printf ("\n\nLine %d : Error at index %d, got " "% d" ", should be " "% d" ".\n\n", line_num, k, actual [k], expected [k]) ; + exit (1) ; + } ; + + return ; +} /* compare_int_or_die */ +void +compare_float_or_die (const float *expected, const float *actual, unsigned count, int line_num) +{ + unsigned k ; + + for (k = 0 ; k < count ; k++) + if (expected [k] != actual [k]) + { printf ("\n\nLine %d : Error at index %d, got " "% g" ", should be " "% g" ".\n\n", line_num, k, actual [k], expected [k]) ; + exit (1) ; + } ; + + return ; +} /* compare_float_or_die */ +void +compare_double_or_die (const double *expected, const double *actual, unsigned count, int line_num) +{ + unsigned k ; + + for (k = 0 ; k < count ; k++) + if (expected [k] != actual [k]) + { printf ("\n\nLine %d : Error at index %d, got " "% g" ", should be " "% g" ".\n\n", line_num, k, actual [k], expected [k]) ; + exit (1) ; + } ; + + return ; +} /* compare_double_or_die */ + + + +void +delete_file (int format, const char *filename) +{ char rsrc_name [512], *fname ; + + unlink (filename) ; + + if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_SD2) + return ; + + /* + ** Now try for a resource fork stored as a separate file. + ** Grab the un-adulterated filename again. + */ + snprintf (rsrc_name, sizeof (rsrc_name), "%s", filename) ; + + if ((fname = strrchr (rsrc_name, '/')) != NULL) + fname ++ ; + else if ((fname = strrchr (rsrc_name, '\\')) != NULL) + fname ++ ; + else + fname = rsrc_name ; + + memmove (fname + 2, fname, strlen (fname) + 1) ; + fname [0] = '.' ; + fname [1] = '_' ; + + unlink (rsrc_name) ; +} /* delete_file */ + +int +truncate_file_to_zero (const char * fname) +{ FILE * file ; + + if ((file = fopen (fname, "w")) == NULL) + return errno ; + fclose (file) ; + + return 0 ; +} /* truncate_file_to_zero */ + +static int allowed_open_files = -1 ; + +void +count_open_files (void) +{ +#if OS_IS_WIN32 + return ; +#else + int k, count = 0 ; + struct stat statbuf ; + + if (allowed_open_files > 0) + return ; + + for (k = 0 ; k < 1024 ; k++) + if (fstat (k, &statbuf) == 0) + count ++ ; + + allowed_open_files = count ; +#endif +} /* count_open_files */ + +void +increment_open_file_count (void) +{ allowed_open_files ++ ; +} /* increment_open_file_count */ + +void +check_open_file_count_or_die (int lineno) +{ +#if OS_IS_WIN32 + (void) lineno ; + return ; +#else + int k, count = 0 ; + struct stat statbuf ; + + if (allowed_open_files < 0) + count_open_files () ; + + for (k = 0 ; k < 1024 ; k++) + if (fstat (k, &statbuf) == 0) + count ++ ; + + if (count > allowed_open_files) + { printf ("\nLine %d : number of open files (%d) > allowed (%d).\n\n", lineno, count, allowed_open_files) ; + exit (1) ; + } ; +#endif +} /* check_open_file_count_or_die */ + +void +write_mono_file (const char * filename, int format, int srate, float * output, int len) +{ SNDFILE * file ; + SF_INFO sfinfo ; + + memset (&sfinfo, 0, sizeof (sfinfo)) ; + + sfinfo.samplerate = srate ; + sfinfo.channels = 1 ; + sfinfo.format = format ; + + if ((file = sf_open (filename, SFM_WRITE, &sfinfo)) == NULL) + { printf ("sf_open (%s) : %s\n", filename, sf_strerror (NULL)) ; + exit (1) ; + } ; + + sf_write_float (file, output, len) ; + + sf_close (file) ; +} /* write_mono_file */ + +void +gen_lowpass_signal_float (float *data, int len) +{ int64_t value = 0x1243456 ; + double sample, last_val = 0.0 ; + int k ; + + for (k = 0 ; k < len ; k++) + { /* Not a crypto quality RNG. */ + value = (11117 * value + 211231) & 0xffffffff ; + value = (11117 * value + 211231) & 0xffffffff ; + value = (11117 * value + 211231) & 0xffffffff ; + + sample = value / (0x7fffffff * 1.000001) ; + sample = 0.2 * sample - 0.9 * last_val ; + + last_val = sample ; + + data [k] = 0.5 * (sample + sin (2.0 * k * M_PI * 1.0 / 32.0)) ; + } ; + +} /* gen_lowpass_signal_float */ + + +/* +** Windows is fucked. +** If a file is opened R/W and data is written to it, then fstat will return +** the correct file length, but stat will return zero. +*/ + +sf_count_t +file_length (const char * fname) +{ struct stat data ; + + if (stat (fname, &data) != 0) + return 0 ; + + return (sf_count_t) data.st_size ; +} /* file_length */ + +sf_count_t +file_length_fd (int fd) +{ struct stat data ; + + memset (&data, 0, sizeof (data)) ; + if (fstat (fd, &data) != 0) + return 0 ; + + return (sf_count_t) data.st_size ; +} /* file_length_fd */ + + + + diff --git a/tests/utils.h b/tests/utils.h new file mode 100644 index 0000000..f8d0d3f --- /dev/null +++ b/tests/utils.h @@ -0,0 +1,201 @@ +/* +** Copyright (C) 2002-2016 Erik de Castro Lopo +** +** This program is free software; you can redistribute it and/or modify +** it under the terms of the GNU General Public License as published by +** the Free Software Foundation; either version 2 of the License, or +** (at your option) any later version. +** +** This program is distributed in the hope that it will be useful, +** but WITHOUT ANY WARRANTY; without even the implied warranty of +** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +** GNU General Public License for more details. +** +** You should have received a copy of the GNU General Public License +** along with this program; if not, write to the Free Software +** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +*/ + +/* +** Utility functions to make writing the test suite easier. +** +** The .c and .h files were generated automagically with Autogen from +** the files utils.def and utils.tpl. +*/ + + + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +#include +#include + +#define ARRAY_LEN(x) ((int) (sizeof (x)) / (sizeof ((x) [0]))) +#define SIGNED_SIZEOF(x) ((int64_t) (sizeof (x))) +#define NOT(x) (! (x)) +#define ABS(x) ((x) >= 0 ? (x) : -(x)) + +#define PIPE_INDEX(x) ((x) + 500) +#define PIPE_TEST_LEN 12345 + + +void gen_windowed_sine_float (float *data, int len, double maximum) ; +void gen_windowed_sine_double (double *data, int len, double maximum) ; + + +void create_short_sndfile (const char *filename, int format, int channels) ; + +void check_file_hash_or_die (const char *filename, uint64_t target_hash, int line_num) ; + +void print_test_name (const char *test, const char *filename) ; + +void dump_data_to_file (const char *filename, const void *data, unsigned int datalen) ; + +void write_mono_file (const char * filename, int format, int srate, float * output, int len) ; + +#ifdef __GNUC__ +static inline void +exit_if_true (int test, const char *format, ...) +#if (defined (__USE_MINGW_ANSI_STDIO) && __USE_MINGW_ANSI_STDIO) + __attribute__ ((format (gnu_printf, 2, 3))) ; +#else + __attribute__ ((format (printf, 2, 3))) ; +#endif +#endif + +static inline void +exit_if_true (int test, const char *format, ...) +{ if (test) + { va_list argptr ; + va_start (argptr, format) ; + vprintf (format, argptr) ; + va_end (argptr) ; + exit (1) ; + } ; +} /* exit_if_true */ + +static inline int32_t +arith_shift_left (int32_t x, int shift) +{ return (int32_t) (((uint32_t) x) << shift) ; +} /* arith_shift_left */ + +/* +** Functions for saving two vectors of data in an ascii text file which +** can then be loaded into GNU octave for comparison. +*/ + +int oct_save_short (const short *a, const short *b, int len) ; +int oct_save_int (const int *a, const int *b, int len) ; +int oct_save_float (const float *a, const float *b, int len) ; +int oct_save_double (const double *a, const double *b, int len) ; + + +void delete_file (int format, const char *filename) ; + +int truncate_file_to_zero (const char *fname) ; + +void count_open_files (void) ; +void increment_open_file_count (void) ; +void check_open_file_count_or_die (int lineno) ; + +#ifdef SNDFILE_H + +static inline void +sf_info_clear (SF_INFO * info) +{ memset (info, 0, sizeof (SF_INFO)) ; +} /* sf_info_clear */ + +static inline void +sf_info_setup (SF_INFO * info, int format, int samplerate, int channels) +{ sf_info_clear (info) ; + + info->format = format ; + info->samplerate = samplerate ; + info->channels = channels ; +} /* sf_info_setup */ + + +void dump_log_buffer (SNDFILE *file) ; +void check_log_buffer_or_die (SNDFILE *file, int line_num) ; +int string_in_log_buffer (SNDFILE *file, const char *s) ; +void hexdump_file (const char * filename, sf_count_t offset, sf_count_t length) ; + +void test_sf_format_or_die (const SF_INFO *info, int line_num) ; + +SNDFILE *test_open_file_or_die + (const char *filename, int mode, SF_INFO *sfinfo, int allow_fd, int line_num) ; + +void test_read_write_position_or_die + (SNDFILE *file, int line_num, int pass, sf_count_t read_pos, sf_count_t write_pos) ; + +void test_seek_or_die + (SNDFILE *file, sf_count_t offset, int whence, sf_count_t new_pos, int channels, int line_num) ; + + +void test_read_short_or_die + (SNDFILE *file, int pass, short *test, sf_count_t items, int line_num) ; +void test_read_int_or_die + (SNDFILE *file, int pass, int *test, sf_count_t items, int line_num) ; +void test_read_float_or_die + (SNDFILE *file, int pass, float *test, sf_count_t items, int line_num) ; +void test_read_double_or_die + (SNDFILE *file, int pass, double *test, sf_count_t items, int line_num) ; + +void test_readf_short_or_die + (SNDFILE *file, int pass, short *test, sf_count_t frames, int line_num) ; +void test_readf_int_or_die + (SNDFILE *file, int pass, int *test, sf_count_t frames, int line_num) ; +void test_readf_float_or_die + (SNDFILE *file, int pass, float *test, sf_count_t frames, int line_num) ; +void test_readf_double_or_die + (SNDFILE *file, int pass, double *test, sf_count_t frames, int line_num) ; + + +void +test_read_raw_or_die (SNDFILE *file, int pass, void *test, sf_count_t items, int line_num) ; + + +void test_write_short_or_die + (SNDFILE *file, int pass, const short *test, sf_count_t items, int line_num) ; +void test_write_int_or_die + (SNDFILE *file, int pass, const int *test, sf_count_t items, int line_num) ; +void test_write_float_or_die + (SNDFILE *file, int pass, const float *test, sf_count_t items, int line_num) ; +void test_write_double_or_die + (SNDFILE *file, int pass, const double *test, sf_count_t items, int line_num) ; + +void test_writef_short_or_die + (SNDFILE *file, int pass, const short *test, sf_count_t frames, int line_num) ; +void test_writef_int_or_die + (SNDFILE *file, int pass, const int *test, sf_count_t frames, int line_num) ; +void test_writef_float_or_die + (SNDFILE *file, int pass, const float *test, sf_count_t frames, int line_num) ; +void test_writef_double_or_die + (SNDFILE *file, int pass, const double *test, sf_count_t frames, int line_num) ; + + +void +test_write_raw_or_die (SNDFILE *file, int pass, const void *test, sf_count_t items, int line_num) ; + +void compare_short_or_die (const short *expected, const short *actual, unsigned count, int line_num) ; +void compare_int_or_die (const int *expected, const int *actual, unsigned count, int line_num) ; +void compare_float_or_die (const float *expected, const float *actual, unsigned count, int line_num) ; +void compare_double_or_die (const double *expected, const double *actual, unsigned count, int line_num) ; + + + +void gen_lowpass_signal_float (float *data, int len) ; + +sf_count_t file_length (const char * fname) ; +sf_count_t file_length_fd (int fd) ; + +#endif + +#ifdef __cplusplus +} /* extern "C" */ +#endif /* __cplusplus */ + + + -- 2.7.4