1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
|
/*
** Copyright (C) 2015-2016 Erik de Castro Lopo <erikd@mega-nerd.com>
**
** 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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <inttypes.h>
#if HAVE_UNISTD_H
#include <unistd.h>
#else
#include "sf_unistd.h"
#endif
#include <sndfile.h>
#include "dft_cmp.h"
#include "utils.h"
#define BUFFER_LENGTH 10000
#define SAMPLE_RATE 44010
static void short_lrw_test (const char *filename, int filetype, const short * output, int out_len) ;
static void int_lrw_test (const char *filename, int filetype, const int * output, int out_len) ;
static void float_lrw_test (const char *filename, int filetype, const float * output, int out_len) ;
static void double_lrw_test (const char *filename, int filetype, const double * output, int out_len) ;
static short short_data [BUFFER_LENGTH] ;
static int int_data [BUFFER_LENGTH] ;
static float float_data [BUFFER_LENGTH] ;
static double double_data [BUFFER_LENGTH] ;
int
main (int argc, char *argv [])
{ int do_all ;
size_t k ;
if (argc != 2)
{ printf ("Usage : %s <test>\n", argv [0]) ;
printf (" Where <test> is one of the following:\n") ;
printf (" alac - test CAF/ALAC file functions\n") ;
printf (" all - perform all tests\n") ;
exit (1) ;
} ;
for (k = 0 ; k < ARRAY_LEN (short_data) ; k++)
{ int value = k / 32 ;
int_data [k] = (value & 1 ? -1 : 1) * value ;
short_data [k] = int_data [k] ;
float_data [k] = int_data [k] / 32000.0f ;
double_data [k] = int_data [k] / 32000.0 ;
}
do_all = ! strcmp (argv [1], "all") ;
if (do_all || strcmp (argv [1], "alac") == 0)
{ short_lrw_test ("alac.caf", SF_FORMAT_CAF | SF_FORMAT_ALAC_16, short_data, ARRAY_LEN (short_data)) ;
int_lrw_test ("alac.caf", SF_FORMAT_CAF | SF_FORMAT_ALAC_32, int_data, ARRAY_LEN (int_data)) ;
float_lrw_test ("alac.caf", SF_FORMAT_CAF | SF_FORMAT_ALAC_32, float_data, ARRAY_LEN (float_data)) ;
double_lrw_test ("alac.caf", SF_FORMAT_CAF | SF_FORMAT_ALAC_32, double_data, ARRAY_LEN (double_data)) ;
} ;
return 0 ;
} /* main */
/*============================================================================================
* Here are the test functions.
*/
static void
short_lrw_test (const char *filename, int filetype, const short * output, int out_len)
{ SNDFILE *file ;
SF_INFO sfinfo ;
int k ;
short input [BUFFER_LENGTH] ;
print_test_name ("short_lrw_test", filename) ;
exit_if_true (BUFFER_LENGTH > out_len, "\n\nLine %d: Bad array length.\n", __LINE__) ;
sfinfo.samplerate = SAMPLE_RATE ;
sfinfo.frames = out_len ;
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, output, out_len, __LINE__) ;
sf_close (file) ;
memset (input, 0, sizeof (input)) ;
file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
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 < out_len, "\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_short_or_die (file, 0, input, out_len, __LINE__) ;
sf_close (file) ;
for (k = 0 ; k < out_len ; k++)
exit_if_true (input [k] != output [k],
"\n\nLine: %d: Error on input %d, expected %d, got %d\n", __LINE__, k, output [k], input [k]) ;
puts ("ok") ;
unlink (filename) ;
return ;
} /* short_lrw_test */
static void
int_lrw_test (const char *filename, int filetype, const int * output, int out_len)
{ SNDFILE *file ;
SF_INFO sfinfo ;
int k ;
int input [BUFFER_LENGTH] ;
print_test_name ("int_lrw_test", filename) ;
exit_if_true (BUFFER_LENGTH > out_len, "\n\nLine %d: Bad array length.\n", __LINE__) ;
sfinfo.samplerate = SAMPLE_RATE ;
sfinfo.frames = out_len ;
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, output, out_len, __LINE__) ;
sf_close (file) ;
memset (input, 0, sizeof (input)) ;
file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
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 < out_len, "\n\nLine %d: Incorrect number of frames in file (too int). (%" 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_int_or_die (file, 0, input, out_len, __LINE__) ;
sf_close (file) ;
for (k = 0 ; k < out_len ; k++)
exit_if_true (input [k] != output [k],
"\n\nLine: %d: Error on input %d, expected %d, got %d\n", __LINE__, k, output [k], input [k]) ;
puts ("ok") ;
unlink (filename) ;
return ;
} /* int_lrw_test */
static void
float_lrw_test (const char *filename, int filetype, const float * output, int out_len)
{ SNDFILE *file ;
SF_INFO sfinfo ;
int k ;
float input [BUFFER_LENGTH] ;
print_test_name ("float_lrw_test", filename) ;
exit_if_true (BUFFER_LENGTH > out_len, "\n\nLine %d: Bad array length.\n", __LINE__) ;
sfinfo.samplerate = SAMPLE_RATE ;
sfinfo.frames = out_len ;
sfinfo.channels = 1 ;
sfinfo.format = filetype ;
file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
test_write_float_or_die (file, 0, output, out_len, __LINE__) ;
sf_close (file) ;
memset (input, 0, sizeof (input)) ;
file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
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 < out_len, "\n\nLine %d: Incorrect number of frames in file (too float). (%" 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, input, out_len, __LINE__) ;
sf_close (file) ;
for (k = 0 ; k < out_len ; k++)
exit_if_true (fabs (input [k] - output [k]) > 0.00001,
"\n\nLine: %d: Error on input %d, expected %f, got %f\n", __LINE__, k, output [k], input [k]) ;
puts ("ok") ;
unlink (filename) ;
return ;
} /* float_lrw_test */
static void
double_lrw_test (const char *filename, int filetype, const double * output, int out_len)
{ SNDFILE *file ;
SF_INFO sfinfo ;
int k ;
double input [BUFFER_LENGTH] ;
print_test_name ("double_lrw_test", filename) ;
exit_if_true (BUFFER_LENGTH > out_len, "\n\nLine %d: Bad array length.\n", __LINE__) ;
sfinfo.samplerate = SAMPLE_RATE ;
sfinfo.frames = out_len ;
sfinfo.channels = 1 ;
sfinfo.format = filetype ;
file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
test_write_double_or_die (file, 0, output, out_len, __LINE__) ;
sf_close (file) ;
memset (input, 0, sizeof (input)) ;
file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
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 < out_len, "\n\nLine %d: Incorrect number of frames in file (too double). (%" 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, input, out_len, __LINE__) ;
sf_close (file) ;
for (k = 0 ; k < out_len ; k++)
exit_if_true (fabs (input [k] - output [k]) > 0.00001,
"\n\nLine: %d: Error on input %d, expected %f, got %f\n", __LINE__, k, output [k], input [k]) ;
puts ("ok") ;
unlink (filename) ;
return ;
} /* double_lrw_test */
|