annotate src/libsndfile-1.0.25/tests/write_read_test.c @ 169:223a55898ab9 tip default

Add null config files
author Chris Cannam <cannam@all-day-breakfast.com>
date Mon, 02 Mar 2020 14:03:47 +0000
parents 545efbb81310
children
rev   line source
cannam@85 1 /*
cannam@85 2 ** Copyright (C) 1999-2011 Erik de Castro Lopo <erikd@mega-nerd.com>
cannam@85 3 **
cannam@85 4 ** This program is free software; you can redistribute it and/or modify
cannam@85 5 ** it under the terms of the GNU General Public License as published by
cannam@85 6 ** the Free Software Foundation; either version 2 of the License, or
cannam@85 7 ** (at your option) any later version.
cannam@85 8 **
cannam@85 9 ** This program is distributed in the hope that it will be useful,
cannam@85 10 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
cannam@85 11 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
cannam@85 12 ** GNU General Public License for more details.
cannam@85 13 **
cannam@85 14 ** You should have received a copy of the GNU General Public License
cannam@85 15 ** along with this program; if not, write to the Free Software
cannam@85 16 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
cannam@85 17 */
cannam@85 18
cannam@85 19 #include "sfconfig.h"
cannam@85 20
cannam@85 21 #include <stdio.h>
cannam@85 22 #include <stdlib.h>
cannam@85 23 #include <string.h>
cannam@85 24
cannam@85 25 #if HAVE_UNISTD_H
cannam@85 26 #include <unistd.h>
cannam@85 27 #endif
cannam@85 28
cannam@85 29 #include <math.h>
cannam@85 30
cannam@85 31 #if (defined (WIN32) || defined (_WIN32))
cannam@85 32 #include <fcntl.h>
cannam@85 33 static int truncate (const char *filename, int ignored) ;
cannam@85 34 #endif
cannam@85 35
cannam@85 36 #include <sndfile.h>
cannam@85 37
cannam@85 38 #include "utils.h"
cannam@85 39 #include "generate.h"
cannam@85 40
cannam@85 41 #define SAMPLE_RATE 11025
cannam@85 42 #define DATA_LENGTH (1<<12)
cannam@85 43
cannam@85 44 #define SILLY_WRITE_COUNT (234)
cannam@85 45
cannam@85 46 static void pcm_test_char (const char *str, int format, int long_file_okz) ;
cannam@85 47 static void pcm_test_short (const char *str, int format, int long_file_okz) ;
cannam@85 48 static void pcm_test_24bit (const char *str, int format, int long_file_okz) ;
cannam@85 49 static void pcm_test_int (const char *str, int format, int long_file_okz) ;
cannam@85 50 static void pcm_test_float (const char *str, int format, int long_file_okz) ;
cannam@85 51 static void pcm_test_double (const char *str, int format, int long_file_okz) ;
cannam@85 52
cannam@85 53 static void empty_file_test (const char *filename, int format) ;
cannam@85 54
cannam@85 55 typedef union
cannam@85 56 { double d [DATA_LENGTH] ;
cannam@85 57 float f [DATA_LENGTH] ;
cannam@85 58 int i [DATA_LENGTH] ;
cannam@85 59 short s [DATA_LENGTH] ;
cannam@85 60 char c [DATA_LENGTH] ;
cannam@85 61 } BUFFER ;
cannam@85 62
cannam@85 63 static BUFFER orig_data ;
cannam@85 64 static BUFFER test_data ;
cannam@85 65
cannam@85 66 int
cannam@85 67 main (int argc, char **argv)
cannam@85 68 { int do_all = 0 ;
cannam@85 69 int test_count = 0 ;
cannam@85 70
cannam@85 71 count_open_files () ;
cannam@85 72
cannam@85 73 if (argc != 2)
cannam@85 74 { printf ("Usage : %s <test>\n", argv [0]) ;
cannam@85 75 printf (" Where <test> is one of the following:\n") ;
cannam@85 76 printf (" wav - test WAV file functions (little endian)\n") ;
cannam@85 77 printf (" aiff - test AIFF file functions (big endian)\n") ;
cannam@85 78 printf (" au - test AU file functions\n") ;
cannam@85 79 printf (" avr - test AVR file functions\n") ;
cannam@85 80 printf (" caf - test CAF file functions\n") ;
cannam@85 81 printf (" raw - test RAW header-less PCM file functions\n") ;
cannam@85 82 printf (" paf - test PAF file functions\n") ;
cannam@85 83 printf (" svx - test 8SVX/16SV file functions\n") ;
cannam@85 84 printf (" nist - test NIST Sphere file functions\n") ;
cannam@85 85 printf (" ircam - test IRCAM file functions\n") ;
cannam@85 86 printf (" voc - Create Voice file functions\n") ;
cannam@85 87 printf (" w64 - Sonic Foundry's W64 file functions\n") ;
cannam@85 88 printf (" flac - test FLAC file functions\n") ;
cannam@85 89 printf (" mpc2k - test MPC 2000 file functions\n") ;
cannam@85 90 printf (" rf64 - test RF64 file functions\n") ;
cannam@85 91 printf (" all - perform all tests\n") ;
cannam@85 92 exit (1) ;
cannam@85 93 } ;
cannam@85 94
cannam@85 95 do_all = !strcmp (argv [1], "all") ;
cannam@85 96
cannam@85 97 if (do_all || ! strcmp (argv [1], "wav"))
cannam@85 98 { pcm_test_char ("char.wav" , SF_FORMAT_WAV | SF_FORMAT_PCM_U8, SF_FALSE) ;
cannam@85 99 pcm_test_short ("short.wav" , SF_FORMAT_WAV | SF_FORMAT_PCM_16, SF_FALSE) ;
cannam@85 100 pcm_test_24bit ("24bit.wav" , SF_FORMAT_WAV | SF_FORMAT_PCM_24, SF_FALSE) ;
cannam@85 101 pcm_test_int ("int.wav" , SF_FORMAT_WAV | SF_FORMAT_PCM_32, SF_FALSE) ;
cannam@85 102
cannam@85 103 pcm_test_char ("char.rifx" , SF_ENDIAN_BIG | SF_FORMAT_WAV | SF_FORMAT_PCM_U8, SF_FALSE) ;
cannam@85 104 pcm_test_short ("short.rifx" , SF_ENDIAN_BIG | SF_FORMAT_WAV | SF_FORMAT_PCM_16, SF_FALSE) ;
cannam@85 105 pcm_test_24bit ("24bit.rifx" , SF_ENDIAN_BIG | SF_FORMAT_WAV | SF_FORMAT_PCM_24, SF_FALSE) ;
cannam@85 106 pcm_test_int ("int.rifx" , SF_ENDIAN_BIG | SF_FORMAT_WAV | SF_FORMAT_PCM_32, SF_FALSE) ;
cannam@85 107
cannam@85 108 pcm_test_24bit ("24bit.wavex" , SF_FORMAT_WAVEX | SF_FORMAT_PCM_24, SF_FALSE) ;
cannam@85 109 pcm_test_int ("int.wavex" , SF_FORMAT_WAVEX | SF_FORMAT_PCM_32, SF_FALSE) ;
cannam@85 110
cannam@85 111 /* Lite remove start */
cannam@85 112 pcm_test_float ("float.wav" , SF_FORMAT_WAV | SF_FORMAT_FLOAT , SF_FALSE) ;
cannam@85 113 pcm_test_double ("double.wav" , SF_FORMAT_WAV | SF_FORMAT_DOUBLE, SF_FALSE) ;
cannam@85 114
cannam@85 115 pcm_test_float ("float.rifx" , SF_ENDIAN_BIG | SF_FORMAT_WAV | SF_FORMAT_FLOAT , SF_FALSE) ;
cannam@85 116 pcm_test_double ("double.rifx" , SF_ENDIAN_BIG | SF_FORMAT_WAV | SF_FORMAT_DOUBLE, SF_FALSE) ;
cannam@85 117
cannam@85 118 pcm_test_float ("float.wavex" , SF_FORMAT_WAVEX | SF_FORMAT_FLOAT , SF_FALSE) ;
cannam@85 119 pcm_test_double ("double.wavex" , SF_FORMAT_WAVEX | SF_FORMAT_DOUBLE, SF_FALSE) ;
cannam@85 120 /* Lite remove end */
cannam@85 121
cannam@85 122 empty_file_test ("empty_char.wav", SF_FORMAT_WAV | SF_FORMAT_PCM_U8) ;
cannam@85 123 empty_file_test ("empty_short.wav", SF_FORMAT_WAV | SF_FORMAT_PCM_16) ;
cannam@85 124 empty_file_test ("empty_float.wav", SF_FORMAT_WAV | SF_FORMAT_FLOAT) ;
cannam@85 125
cannam@85 126 test_count++ ;
cannam@85 127 } ;
cannam@85 128
cannam@85 129 if (do_all || ! strcmp (argv [1], "aiff"))
cannam@85 130 { pcm_test_char ("char_u8.aiff" , SF_FORMAT_AIFF | SF_FORMAT_PCM_U8, SF_FALSE) ;
cannam@85 131 pcm_test_char ("char_s8.aiff" , SF_FORMAT_AIFF | SF_FORMAT_PCM_S8, SF_FALSE) ;
cannam@85 132 pcm_test_short ("short.aiff" , SF_FORMAT_AIFF | SF_FORMAT_PCM_16, SF_FALSE) ;
cannam@85 133 pcm_test_24bit ("24bit.aiff" , SF_FORMAT_AIFF | SF_FORMAT_PCM_24, SF_FALSE) ;
cannam@85 134 pcm_test_int ("int.aiff" , SF_FORMAT_AIFF | SF_FORMAT_PCM_32, SF_FALSE) ;
cannam@85 135
cannam@85 136 pcm_test_short ("short_sowt.aifc" , SF_ENDIAN_LITTLE | SF_FORMAT_AIFF | SF_FORMAT_PCM_16, SF_FALSE) ;
cannam@85 137 pcm_test_24bit ("24bit_sowt.aifc" , SF_ENDIAN_LITTLE | SF_FORMAT_AIFF | SF_FORMAT_PCM_24, SF_FALSE) ;
cannam@85 138 pcm_test_int ("int_sowt.aifc" , SF_ENDIAN_LITTLE | SF_FORMAT_AIFF | SF_FORMAT_PCM_32, SF_FALSE) ;
cannam@85 139
cannam@85 140 pcm_test_short ("short_twos.aifc" , SF_ENDIAN_BIG | SF_FORMAT_AIFF | SF_FORMAT_PCM_16, SF_FALSE) ;
cannam@85 141 pcm_test_24bit ("24bit_twos.aifc" , SF_ENDIAN_BIG | SF_FORMAT_AIFF | SF_FORMAT_PCM_24, SF_FALSE) ;
cannam@85 142 pcm_test_int ("int_twos.aifc" , SF_ENDIAN_BIG | SF_FORMAT_AIFF | SF_FORMAT_PCM_32, SF_FALSE) ;
cannam@85 143
cannam@85 144 /* Lite remove start */
cannam@85 145 pcm_test_short ("dwvw16.aifc", SF_FORMAT_AIFF | SF_FORMAT_DWVW_16, SF_TRUE) ;
cannam@85 146 pcm_test_24bit ("dwvw24.aifc", SF_FORMAT_AIFF | SF_FORMAT_DWVW_24, SF_TRUE) ;
cannam@85 147
cannam@85 148 pcm_test_float ("float.aifc" , SF_FORMAT_AIFF | SF_FORMAT_FLOAT , SF_FALSE) ;
cannam@85 149 pcm_test_double ("double.aifc" , SF_FORMAT_AIFF | SF_FORMAT_DOUBLE, SF_FALSE) ;
cannam@85 150 /* Lite remove end */
cannam@85 151
cannam@85 152 empty_file_test ("empty_char.aiff", SF_FORMAT_AIFF | SF_FORMAT_PCM_U8) ;
cannam@85 153 empty_file_test ("empty_short.aiff", SF_FORMAT_AIFF | SF_FORMAT_PCM_16) ;
cannam@85 154 empty_file_test ("empty_float.aiff", SF_FORMAT_AIFF | SF_FORMAT_FLOAT) ;
cannam@85 155
cannam@85 156 test_count++ ;
cannam@85 157 } ;
cannam@85 158
cannam@85 159 if (do_all || ! strcmp (argv [1], "au"))
cannam@85 160 { pcm_test_char ("char.au" , SF_FORMAT_AU | SF_FORMAT_PCM_S8, SF_FALSE) ;
cannam@85 161 pcm_test_short ("short.au" , SF_FORMAT_AU | SF_FORMAT_PCM_16, SF_FALSE) ;
cannam@85 162 pcm_test_24bit ("24bit.au" , SF_FORMAT_AU | SF_FORMAT_PCM_24, SF_FALSE) ;
cannam@85 163 pcm_test_int ("int.au" , SF_FORMAT_AU | SF_FORMAT_PCM_32, SF_FALSE) ;
cannam@85 164 /* Lite remove start */
cannam@85 165 pcm_test_float ("float.au" , SF_FORMAT_AU | SF_FORMAT_FLOAT , SF_FALSE) ;
cannam@85 166 pcm_test_double ("double.au", SF_FORMAT_AU | SF_FORMAT_DOUBLE, SF_FALSE) ;
cannam@85 167 /* Lite remove end */
cannam@85 168
cannam@85 169 pcm_test_char ("char_le.au" , SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_PCM_S8, SF_FALSE) ;
cannam@85 170 pcm_test_short ("short_le.au" , SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_PCM_16, SF_FALSE) ;
cannam@85 171 pcm_test_24bit ("24bit_le.au" , SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_PCM_24, SF_FALSE) ;
cannam@85 172 pcm_test_int ("int_le.au" , SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_PCM_32, SF_FALSE) ;
cannam@85 173 /* Lite remove start */
cannam@85 174 pcm_test_float ("float_le.au" , SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_FLOAT , SF_FALSE) ;
cannam@85 175 pcm_test_double ("double_le.au" , SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_DOUBLE, SF_FALSE) ;
cannam@85 176 /* Lite remove end */
cannam@85 177 test_count++ ;
cannam@85 178 } ;
cannam@85 179
cannam@85 180 if (do_all || ! strcmp (argv [1], "caf"))
cannam@85 181 { pcm_test_char ("char.caf" , SF_FORMAT_CAF | SF_FORMAT_PCM_S8, SF_FALSE) ;
cannam@85 182 pcm_test_short ("short.caf" , SF_FORMAT_CAF | SF_FORMAT_PCM_16, SF_FALSE) ;
cannam@85 183 pcm_test_24bit ("24bit.caf" , SF_FORMAT_CAF | SF_FORMAT_PCM_24, SF_FALSE) ;
cannam@85 184 pcm_test_int ("int.caf" , SF_FORMAT_CAF | SF_FORMAT_PCM_32, SF_FALSE) ;
cannam@85 185 /* Lite remove start */
cannam@85 186 pcm_test_float ("float.caf" , SF_FORMAT_CAF | SF_FORMAT_FLOAT , SF_FALSE) ;
cannam@85 187 pcm_test_double ("double.caf" , SF_FORMAT_CAF | SF_FORMAT_DOUBLE, SF_FALSE) ;
cannam@85 188 /* Lite remove end */
cannam@85 189
cannam@85 190 pcm_test_short ("short_le.caf" , SF_ENDIAN_LITTLE | SF_FORMAT_CAF | SF_FORMAT_PCM_16, SF_FALSE) ;
cannam@85 191 pcm_test_24bit ("24bit_le.caf" , SF_ENDIAN_LITTLE | SF_FORMAT_CAF | SF_FORMAT_PCM_24, SF_FALSE) ;
cannam@85 192 pcm_test_int ("int_le.caf" , SF_ENDIAN_LITTLE | SF_FORMAT_CAF | SF_FORMAT_PCM_32, SF_FALSE) ;
cannam@85 193 /* Lite remove start */
cannam@85 194 pcm_test_float ("float_le.caf" , SF_ENDIAN_LITTLE | SF_FORMAT_CAF | SF_FORMAT_FLOAT , SF_FALSE) ;
cannam@85 195 pcm_test_double ("double_le.caf", SF_ENDIAN_LITTLE | SF_FORMAT_CAF | SF_FORMAT_DOUBLE, SF_FALSE) ;
cannam@85 196 /* Lite remove end */
cannam@85 197 test_count++ ;
cannam@85 198 } ;
cannam@85 199
cannam@85 200 if (do_all || ! strcmp (argv [1], "raw"))
cannam@85 201 { pcm_test_char ("char_s8.raw" , SF_FORMAT_RAW | SF_FORMAT_PCM_S8, SF_FALSE) ;
cannam@85 202 pcm_test_char ("char_u8.raw" , SF_FORMAT_RAW | SF_FORMAT_PCM_U8, SF_FALSE) ;
cannam@85 203
cannam@85 204 pcm_test_short ("short_le.raw" , SF_ENDIAN_LITTLE | SF_FORMAT_RAW | SF_FORMAT_PCM_16, SF_FALSE) ;
cannam@85 205 pcm_test_short ("short_be.raw" , SF_ENDIAN_BIG | SF_FORMAT_RAW | SF_FORMAT_PCM_16, SF_FALSE) ;
cannam@85 206 pcm_test_24bit ("24bit_le.raw" , SF_ENDIAN_LITTLE | SF_FORMAT_RAW | SF_FORMAT_PCM_24, SF_FALSE) ;
cannam@85 207 pcm_test_24bit ("24bit_be.raw" , SF_ENDIAN_BIG | SF_FORMAT_RAW | SF_FORMAT_PCM_24, SF_FALSE) ;
cannam@85 208 pcm_test_int ("int_le.raw" , SF_ENDIAN_LITTLE | SF_FORMAT_RAW | SF_FORMAT_PCM_32, SF_FALSE) ;
cannam@85 209 pcm_test_int ("int_be.raw" , SF_ENDIAN_BIG | SF_FORMAT_RAW | SF_FORMAT_PCM_32, SF_FALSE) ;
cannam@85 210
cannam@85 211 /* Lite remove start */
cannam@85 212 pcm_test_float ("float_le.raw" , SF_ENDIAN_LITTLE | SF_FORMAT_RAW | SF_FORMAT_FLOAT , SF_FALSE) ;
cannam@85 213 pcm_test_float ("float_be.raw" , SF_ENDIAN_BIG | SF_FORMAT_RAW | SF_FORMAT_FLOAT , SF_FALSE) ;
cannam@85 214
cannam@85 215 pcm_test_double ("double_le.raw", SF_ENDIAN_LITTLE | SF_FORMAT_RAW | SF_FORMAT_DOUBLE, SF_FALSE) ;
cannam@85 216 pcm_test_double ("double_be.raw", SF_ENDIAN_BIG | SF_FORMAT_RAW | SF_FORMAT_DOUBLE, SF_FALSE) ;
cannam@85 217 /* Lite remove end */
cannam@85 218 test_count++ ;
cannam@85 219 } ;
cannam@85 220
cannam@85 221 /* Lite remove start */
cannam@85 222 if (do_all || ! strcmp (argv [1], "paf"))
cannam@85 223 { pcm_test_char ("char_le.paf", SF_ENDIAN_LITTLE | SF_FORMAT_PAF | SF_FORMAT_PCM_S8, SF_FALSE) ;
cannam@85 224 pcm_test_char ("char_be.paf", SF_ENDIAN_BIG | SF_FORMAT_PAF | SF_FORMAT_PCM_S8, SF_FALSE) ;
cannam@85 225 pcm_test_short ("short_le.paf", SF_ENDIAN_LITTLE | SF_FORMAT_PAF | SF_FORMAT_PCM_16, SF_FALSE) ;
cannam@85 226 pcm_test_short ("short_be.paf", SF_ENDIAN_BIG | SF_FORMAT_PAF | SF_FORMAT_PCM_16, SF_FALSE) ;
cannam@85 227 pcm_test_24bit ("24bit_le.paf", SF_ENDIAN_LITTLE | SF_FORMAT_PAF | SF_FORMAT_PCM_24, SF_TRUE) ;
cannam@85 228 pcm_test_24bit ("24bit_be.paf", SF_ENDIAN_BIG | SF_FORMAT_PAF | SF_FORMAT_PCM_24, SF_TRUE) ;
cannam@85 229 test_count++ ;
cannam@85 230 } ;
cannam@85 231
cannam@85 232 if (do_all || ! strcmp (argv [1], "svx"))
cannam@85 233 { pcm_test_char ("char.svx" , SF_FORMAT_SVX | SF_FORMAT_PCM_S8, SF_FALSE) ;
cannam@85 234 pcm_test_short ("short.svx", SF_FORMAT_SVX | SF_FORMAT_PCM_16, SF_FALSE) ;
cannam@85 235
cannam@85 236 empty_file_test ("empty_char.svx", SF_FORMAT_SVX | SF_FORMAT_PCM_S8) ;
cannam@85 237 empty_file_test ("empty_short.svx", SF_FORMAT_SVX | SF_FORMAT_PCM_16) ;
cannam@85 238
cannam@85 239 test_count++ ;
cannam@85 240 } ;
cannam@85 241
cannam@85 242 if (do_all || ! strcmp (argv [1], "nist"))
cannam@85 243 { pcm_test_short ("short_le.nist", SF_ENDIAN_LITTLE | SF_FORMAT_NIST | SF_FORMAT_PCM_16, SF_FALSE) ;
cannam@85 244 pcm_test_short ("short_be.nist", SF_ENDIAN_BIG | SF_FORMAT_NIST | SF_FORMAT_PCM_16, SF_FALSE) ;
cannam@85 245 pcm_test_24bit ("24bit_le.nist", SF_ENDIAN_LITTLE | SF_FORMAT_NIST | SF_FORMAT_PCM_24, SF_FALSE) ;
cannam@85 246 pcm_test_24bit ("24bit_be.nist", SF_ENDIAN_BIG | SF_FORMAT_NIST | SF_FORMAT_PCM_24, SF_FALSE) ;
cannam@85 247 pcm_test_int ("int_le.nist" , SF_ENDIAN_LITTLE | SF_FORMAT_NIST | SF_FORMAT_PCM_32, SF_FALSE) ;
cannam@85 248 pcm_test_int ("int_be.nist" , SF_ENDIAN_BIG | SF_FORMAT_NIST | SF_FORMAT_PCM_32, SF_FALSE) ;
cannam@85 249
cannam@85 250 test_count++ ;
cannam@85 251 } ;
cannam@85 252
cannam@85 253 if (do_all || ! strcmp (argv [1], "ircam"))
cannam@85 254 { pcm_test_short ("short_be.ircam" , SF_ENDIAN_BIG | SF_FORMAT_IRCAM | SF_FORMAT_PCM_16, SF_FALSE) ;
cannam@85 255 pcm_test_short ("short_le.ircam" , SF_ENDIAN_LITTLE | SF_FORMAT_IRCAM | SF_FORMAT_PCM_16, SF_FALSE) ;
cannam@85 256 pcm_test_int ("int_be.ircam" , SF_ENDIAN_BIG | SF_FORMAT_IRCAM | SF_FORMAT_PCM_32, SF_FALSE) ;
cannam@85 257 pcm_test_int ("int_le.ircam" , SF_ENDIAN_LITTLE | SF_FORMAT_IRCAM | SF_FORMAT_PCM_32, SF_FALSE) ;
cannam@85 258 pcm_test_float ("float_be.ircam" , SF_ENDIAN_BIG | SF_FORMAT_IRCAM | SF_FORMAT_FLOAT , SF_FALSE) ;
cannam@85 259 pcm_test_float ("float_le.ircam" , SF_ENDIAN_LITTLE | SF_FORMAT_IRCAM | SF_FORMAT_FLOAT , SF_FALSE) ;
cannam@85 260
cannam@85 261 test_count++ ;
cannam@85 262 } ;
cannam@85 263
cannam@85 264 if (do_all || ! strcmp (argv [1], "voc"))
cannam@85 265 { pcm_test_char ("char.voc" , SF_FORMAT_VOC | SF_FORMAT_PCM_U8, SF_FALSE) ;
cannam@85 266 pcm_test_short ("short.voc", SF_FORMAT_VOC | SF_FORMAT_PCM_16, SF_FALSE) ;
cannam@85 267
cannam@85 268 test_count++ ;
cannam@85 269 } ;
cannam@85 270
cannam@85 271 if (do_all || ! strcmp (argv [1], "mat4"))
cannam@85 272 { pcm_test_short ("short_be.mat4" , SF_ENDIAN_BIG | SF_FORMAT_MAT4 | SF_FORMAT_PCM_16, SF_FALSE) ;
cannam@85 273 pcm_test_short ("short_le.mat4" , SF_ENDIAN_LITTLE | SF_FORMAT_MAT4 | SF_FORMAT_PCM_16, SF_FALSE) ;
cannam@85 274 pcm_test_int ("int_be.mat4" , SF_ENDIAN_BIG | SF_FORMAT_MAT4 | SF_FORMAT_PCM_32, SF_FALSE) ;
cannam@85 275 pcm_test_int ("int_le.mat4" , SF_ENDIAN_LITTLE | SF_FORMAT_MAT4 | SF_FORMAT_PCM_32, SF_FALSE) ;
cannam@85 276 pcm_test_float ("float_be.mat4" , SF_ENDIAN_BIG | SF_FORMAT_MAT4 | SF_FORMAT_FLOAT , SF_FALSE) ;
cannam@85 277 pcm_test_float ("float_le.mat4" , SF_ENDIAN_LITTLE | SF_FORMAT_MAT4 | SF_FORMAT_FLOAT , SF_FALSE) ;
cannam@85 278 pcm_test_double ("double_be.mat4" , SF_ENDIAN_BIG | SF_FORMAT_MAT4 | SF_FORMAT_DOUBLE, SF_FALSE) ;
cannam@85 279 pcm_test_double ("double_le.mat4" , SF_ENDIAN_LITTLE | SF_FORMAT_MAT4 | SF_FORMAT_DOUBLE, SF_FALSE) ;
cannam@85 280
cannam@85 281 empty_file_test ("empty_short.mat4", SF_FORMAT_MAT4 | SF_FORMAT_PCM_16) ;
cannam@85 282 empty_file_test ("empty_float.mat4", SF_FORMAT_MAT4 | SF_FORMAT_FLOAT) ;
cannam@85 283 test_count++ ;
cannam@85 284 } ;
cannam@85 285
cannam@85 286 if (do_all || ! strcmp (argv [1], "mat5"))
cannam@85 287 { pcm_test_char ("char_be.mat5" , SF_ENDIAN_BIG | SF_FORMAT_MAT5 | SF_FORMAT_PCM_U8, SF_FALSE) ;
cannam@85 288 pcm_test_char ("char_le.mat5" , SF_ENDIAN_LITTLE | SF_FORMAT_MAT5 | SF_FORMAT_PCM_U8, SF_FALSE) ;
cannam@85 289 pcm_test_short ("short_be.mat5" , SF_ENDIAN_BIG | SF_FORMAT_MAT5 | SF_FORMAT_PCM_16, SF_FALSE) ;
cannam@85 290 pcm_test_short ("short_le.mat5" , SF_ENDIAN_LITTLE | SF_FORMAT_MAT5 | SF_FORMAT_PCM_16, SF_FALSE) ;
cannam@85 291 pcm_test_int ("int_be.mat5" , SF_ENDIAN_BIG | SF_FORMAT_MAT5 | SF_FORMAT_PCM_32, SF_FALSE) ;
cannam@85 292 pcm_test_int ("int_le.mat5" , SF_ENDIAN_LITTLE | SF_FORMAT_MAT5 | SF_FORMAT_PCM_32, SF_FALSE) ;
cannam@85 293 pcm_test_float ("float_be.mat5" , SF_ENDIAN_BIG | SF_FORMAT_MAT5 | SF_FORMAT_FLOAT , SF_FALSE) ;
cannam@85 294 pcm_test_float ("float_le.mat5" , SF_ENDIAN_LITTLE | SF_FORMAT_MAT5 | SF_FORMAT_FLOAT , SF_FALSE) ;
cannam@85 295 pcm_test_double ("double_be.mat5" , SF_ENDIAN_BIG | SF_FORMAT_MAT5 | SF_FORMAT_DOUBLE, SF_FALSE) ;
cannam@85 296 pcm_test_double ("double_le.mat5" , SF_ENDIAN_LITTLE | SF_FORMAT_MAT5 | SF_FORMAT_DOUBLE, SF_FALSE) ;
cannam@85 297
cannam@85 298 increment_open_file_count () ;
cannam@85 299
cannam@85 300 empty_file_test ("empty_char.mat5", SF_FORMAT_MAT5 | SF_FORMAT_PCM_U8) ;
cannam@85 301 empty_file_test ("empty_short.mat5", SF_FORMAT_MAT5 | SF_FORMAT_PCM_16) ;
cannam@85 302 empty_file_test ("empty_float.mat5", SF_FORMAT_MAT5 | SF_FORMAT_FLOAT) ;
cannam@85 303
cannam@85 304 test_count++ ;
cannam@85 305 } ;
cannam@85 306
cannam@85 307 if (do_all || ! strcmp (argv [1], "pvf"))
cannam@85 308 { pcm_test_char ("char.pvf" , SF_FORMAT_PVF | SF_FORMAT_PCM_S8, SF_FALSE) ;
cannam@85 309 pcm_test_short ("short.pvf", SF_FORMAT_PVF | SF_FORMAT_PCM_16, SF_FALSE) ;
cannam@85 310 pcm_test_int ("int.pvf" , SF_FORMAT_PVF | SF_FORMAT_PCM_32, SF_FALSE) ;
cannam@85 311 test_count++ ;
cannam@85 312 } ;
cannam@85 313
cannam@85 314 if (do_all || ! strcmp (argv [1], "htk"))
cannam@85 315 { pcm_test_short ("short.htk", SF_FORMAT_HTK | SF_FORMAT_PCM_16, SF_FALSE) ;
cannam@85 316 test_count++ ;
cannam@85 317 } ;
cannam@85 318
cannam@85 319 if (do_all || ! strcmp (argv [1], "mpc2k"))
cannam@85 320 { pcm_test_short ("short.mpc", SF_FORMAT_MPC2K | SF_FORMAT_PCM_16, SF_FALSE) ;
cannam@85 321 test_count++ ;
cannam@85 322 } ;
cannam@85 323
cannam@85 324 if (do_all || ! strcmp (argv [1], "avr"))
cannam@85 325 { pcm_test_char ("char_u8.avr" , SF_FORMAT_AVR | SF_FORMAT_PCM_U8, SF_FALSE) ;
cannam@85 326 pcm_test_char ("char_s8.avr" , SF_FORMAT_AVR | SF_FORMAT_PCM_S8, SF_FALSE) ;
cannam@85 327 pcm_test_short ("short.avr" , SF_FORMAT_AVR | SF_FORMAT_PCM_16, SF_FALSE) ;
cannam@85 328 test_count++ ;
cannam@85 329 } ;
cannam@85 330 /* Lite remove end */
cannam@85 331
cannam@85 332 if (do_all || ! strcmp (argv [1], "w64"))
cannam@85 333 { pcm_test_char ("char.w64" , SF_FORMAT_W64 | SF_FORMAT_PCM_U8, SF_FALSE) ;
cannam@85 334 pcm_test_short ("short.w64" , SF_FORMAT_W64 | SF_FORMAT_PCM_16, SF_FALSE) ;
cannam@85 335 pcm_test_24bit ("24bit.w64" , SF_FORMAT_W64 | SF_FORMAT_PCM_24, SF_FALSE) ;
cannam@85 336 pcm_test_int ("int.w64" , SF_FORMAT_W64 | SF_FORMAT_PCM_32, SF_FALSE) ;
cannam@85 337 /* Lite remove start */
cannam@85 338 pcm_test_float ("float.w64" , SF_FORMAT_W64 | SF_FORMAT_FLOAT , SF_FALSE) ;
cannam@85 339 pcm_test_double ("double.w64" , SF_FORMAT_W64 | SF_FORMAT_DOUBLE, SF_FALSE) ;
cannam@85 340 /* Lite remove end */
cannam@85 341
cannam@85 342 empty_file_test ("empty_char.w64", SF_FORMAT_W64 | SF_FORMAT_PCM_U8) ;
cannam@85 343 empty_file_test ("empty_short.w64", SF_FORMAT_W64 | SF_FORMAT_PCM_16) ;
cannam@85 344 empty_file_test ("empty_float.w64", SF_FORMAT_W64 | SF_FORMAT_FLOAT) ;
cannam@85 345
cannam@85 346 test_count++ ;
cannam@85 347 } ;
cannam@85 348
cannam@85 349 if (do_all || ! strcmp (argv [1], "sds"))
cannam@85 350 { pcm_test_char ("char.sds" , SF_FORMAT_SDS | SF_FORMAT_PCM_S8, SF_FALSE) ;
cannam@85 351 pcm_test_short ("short.sds" , SF_FORMAT_SDS | SF_FORMAT_PCM_16, SF_FALSE) ;
cannam@85 352 pcm_test_24bit ("24bit.sds" , SF_FORMAT_SDS | SF_FORMAT_PCM_24, SF_FALSE) ;
cannam@85 353
cannam@85 354 empty_file_test ("empty_char.sds", SF_FORMAT_SDS | SF_FORMAT_PCM_S8) ;
cannam@85 355 empty_file_test ("empty_short.sds", SF_FORMAT_SDS | SF_FORMAT_PCM_16) ;
cannam@85 356
cannam@85 357 test_count++ ;
cannam@85 358 } ;
cannam@85 359
cannam@85 360 if (do_all || ! strcmp (argv [1], "sd2"))
cannam@85 361 { pcm_test_char ("char.sd2" , SF_FORMAT_SD2 | SF_FORMAT_PCM_S8, SF_TRUE) ;
cannam@85 362 pcm_test_short ("short.sd2" , SF_FORMAT_SD2 | SF_FORMAT_PCM_16, SF_TRUE) ;
cannam@85 363 pcm_test_24bit ("24bit.sd2" , SF_FORMAT_SD2 | SF_FORMAT_PCM_24, SF_TRUE) ;
cannam@85 364 test_count++ ;
cannam@85 365 } ;
cannam@85 366
cannam@85 367 if (do_all || ! strcmp (argv [1], "flac"))
cannam@85 368 { if (HAVE_EXTERNAL_LIBS)
cannam@85 369 { pcm_test_char ("char.flac" , SF_FORMAT_FLAC | SF_FORMAT_PCM_S8, SF_TRUE) ;
cannam@85 370 pcm_test_short ("short.flac" , SF_FORMAT_FLAC | SF_FORMAT_PCM_16, SF_TRUE) ;
cannam@85 371 pcm_test_24bit ("24bit.flac" , SF_FORMAT_FLAC | SF_FORMAT_PCM_24, SF_TRUE) ;
cannam@85 372 }
cannam@85 373 else
cannam@85 374 puts (" No FLAC tests because FLAC support was not compiled in.") ;
cannam@85 375 test_count++ ;
cannam@85 376 } ;
cannam@85 377
cannam@85 378 if (do_all || ! strcmp (argv [1], "rf64"))
cannam@85 379 { pcm_test_char ("char.rf64" , SF_FORMAT_RF64 | SF_FORMAT_PCM_U8, SF_FALSE) ;
cannam@85 380 pcm_test_short ("short.rf64" , SF_FORMAT_RF64 | SF_FORMAT_PCM_16, SF_FALSE) ;
cannam@85 381 pcm_test_24bit ("24bit.rf64" , SF_FORMAT_RF64 | SF_FORMAT_PCM_24, SF_FALSE) ;
cannam@85 382 pcm_test_int ("int.rf64" , SF_FORMAT_RF64 | SF_FORMAT_PCM_32, SF_FALSE) ;
cannam@85 383
cannam@85 384 /* Lite remove start */
cannam@85 385 pcm_test_float ("float.rf64" , SF_FORMAT_RF64 | SF_FORMAT_FLOAT , SF_FALSE) ;
cannam@85 386 pcm_test_double ("double.rf64" , SF_FORMAT_RF64 | SF_FORMAT_DOUBLE, SF_FALSE) ;
cannam@85 387 empty_file_test ("empty_char.rf64", SF_FORMAT_RF64 | SF_FORMAT_PCM_U8) ;
cannam@85 388 empty_file_test ("empty_short.rf64", SF_FORMAT_RF64 | SF_FORMAT_PCM_16) ;
cannam@85 389 empty_file_test ("empty_float.rf64", SF_FORMAT_RF64 | SF_FORMAT_FLOAT) ;
cannam@85 390 /* Lite remove end */
cannam@85 391
cannam@85 392 test_count++ ;
cannam@85 393 } ;
cannam@85 394
cannam@85 395 if (test_count == 0)
cannam@85 396 { printf ("Mono : ************************************\n") ;
cannam@85 397 printf ("Mono : * No '%s' test defined.\n", argv [1]) ;
cannam@85 398 printf ("Mono : ************************************\n") ;
cannam@85 399 return 1 ;
cannam@85 400 } ;
cannam@85 401
cannam@85 402 /* Only open file descriptors should be stdin, stdout and stderr. */
cannam@85 403 check_open_file_count_or_die (__LINE__) ;
cannam@85 404
cannam@85 405 return 0 ;
cannam@85 406 } /* main */
cannam@85 407
cannam@85 408 /*============================================================================================
cannam@85 409 ** Helper functions and macros.
cannam@85 410 */
cannam@85 411
cannam@85 412 static void create_short_file (const char *filename) ;
cannam@85 413
cannam@85 414 #define CHAR_ERROR(x,y) (abs ((x) - (y)) > 255)
cannam@85 415 #define INT_ERROR(x,y) (((x) - (y)) != 0)
cannam@85 416 #define TRIBYTE_ERROR(x,y) (abs ((x) - (y)) > 255)
cannam@85 417 #define FLOAT_ERROR(x,y) (fabs ((x) - (y)) > 1e-5)
cannam@85 418
cannam@85 419 #define CONVERT_DATA(k,len,new,orig) \
cannam@85 420 { for ((k) = 0 ; (k) < (len) ; (k) ++) \
cannam@85 421 (new) [k] = (orig) [k] ; \
cannam@85 422 }
cannam@85 423
cannam@85 424
cannam@85 425 /*======================================================================================
cannam@85 426 */
cannam@85 427
cannam@85 428 static void mono_char_test (const char *filename, int format, int long_file_ok, int allow_fd) ;
cannam@85 429 static void stereo_char_test (const char *filename, int format, int long_file_ok, int allow_fd) ;
cannam@85 430 static void mono_rdwr_char_test (const char *filename, int format, int long_file_ok, int allow_fd) ;
cannam@85 431 static void new_rdwr_char_test (const char *filename, int format, int allow_fd) ;
cannam@85 432 static void multi_seek_test (const char * filename, int format) ;
cannam@85 433 static void write_seek_extend_test (const char * filename, int format) ;
cannam@85 434
cannam@85 435 static void
cannam@85 436 pcm_test_char (const char *filename, int format, int long_file_ok)
cannam@85 437 { SF_INFO sfinfo ;
cannam@85 438 short *orig ;
cannam@85 439 int k, allow_fd ;
cannam@85 440
cannam@85 441 /* Sd2 files cannot be opened from an existing file descriptor. */
cannam@85 442 allow_fd = ((format & SF_FORMAT_TYPEMASK) == SF_FORMAT_SD2) ? SF_FALSE : SF_TRUE ;
cannam@85 443
cannam@85 444 print_test_name ("pcm_test_char", filename) ;
cannam@85 445
cannam@85 446 sfinfo.samplerate = 44100 ;
cannam@85 447 sfinfo.frames = SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */
cannam@85 448 sfinfo.channels = 1 ;
cannam@85 449 sfinfo.format = format ;
cannam@85 450
cannam@85 451 gen_windowed_sine_double (orig_data.d, DATA_LENGTH, 32000.0) ;
cannam@85 452
cannam@85 453 orig = orig_data.s ;
cannam@85 454
cannam@85 455 /* Make this a macro so gdb steps over it in one go. */
cannam@85 456 CONVERT_DATA (k, DATA_LENGTH, orig, orig_data.d) ;
cannam@85 457
cannam@85 458 /* Some test broken out here. */
cannam@85 459
cannam@85 460 mono_char_test (filename, format, long_file_ok, allow_fd) ;
cannam@85 461
cannam@85 462 /* Sub format DWVW does not allow seeking. */
cannam@85 463 if ((format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_16 ||
cannam@85 464 (format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_24)
cannam@85 465 { unlink (filename) ;
cannam@85 466 printf ("no seek : ok\n") ;
cannam@85 467 return ;
cannam@85 468 } ;
cannam@85 469
cannam@85 470 if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_FLAC)
cannam@85 471 mono_rdwr_char_test (filename, format, long_file_ok, allow_fd) ;
cannam@85 472
cannam@85 473 /* If the format doesn't support stereo we're done. */
cannam@85 474 sfinfo.channels = 2 ;
cannam@85 475 if (sf_format_check (&sfinfo) == 0)
cannam@85 476 { unlink (filename) ;
cannam@85 477 puts ("no stereo : ok") ;
cannam@85 478 return ;
cannam@85 479 } ;
cannam@85 480
cannam@85 481 stereo_char_test (filename, format, long_file_ok, allow_fd) ;
cannam@85 482
cannam@85 483 /* New read/write test. Not sure if this is needed yet. */
cannam@85 484
cannam@85 485 if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_PAF &&
cannam@85 486 (format & SF_FORMAT_TYPEMASK) != SF_FORMAT_VOC &&
cannam@85 487 (format & SF_FORMAT_TYPEMASK) != SF_FORMAT_FLAC)
cannam@85 488 new_rdwr_char_test (filename, format, allow_fd) ;
cannam@85 489
cannam@85 490 delete_file (format, filename) ;
cannam@85 491
cannam@85 492 puts ("ok") ;
cannam@85 493 return ;
cannam@85 494 } /* pcm_test_char */
cannam@85 495
cannam@85 496 static void
cannam@85 497 mono_char_test (const char *filename, int format, int long_file_ok, int allow_fd)
cannam@85 498 { SNDFILE *file ;
cannam@85 499 SF_INFO sfinfo ;
cannam@85 500 short *orig, *test ;
cannam@85 501 sf_count_t count ;
cannam@85 502 int k, items ;
cannam@85 503
cannam@85 504 sfinfo.samplerate = 44100 ;
cannam@85 505 sfinfo.frames = SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */
cannam@85 506 sfinfo.channels = 1 ;
cannam@85 507 sfinfo.format = format ;
cannam@85 508
cannam@85 509 orig = orig_data.s ;
cannam@85 510 test = test_data.s ;
cannam@85 511
cannam@85 512 items = DATA_LENGTH ;
cannam@85 513
cannam@85 514 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, allow_fd, __LINE__) ;
cannam@85 515
cannam@85 516 sf_set_string (file, SF_STR_ARTIST, "Your name here") ;
cannam@85 517
cannam@85 518 test_write_short_or_die (file, 0, orig, items, __LINE__) ;
cannam@85 519 sf_write_sync (file) ;
cannam@85 520 test_write_short_or_die (file, 0, orig, items, __LINE__) ;
cannam@85 521 sf_write_sync (file) ;
cannam@85 522
cannam@85 523 /* Add non-audio data after the audio. */
cannam@85 524 sf_set_string (file, SF_STR_COPYRIGHT, "Copyright (c) 2003") ;
cannam@85 525
cannam@85 526 sf_close (file) ;
cannam@85 527
cannam@85 528 memset (test, 0, items * sizeof (short)) ;
cannam@85 529
cannam@85 530 if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_RAW)
cannam@85 531 memset (&sfinfo, 0, sizeof (sfinfo)) ;
cannam@85 532
cannam@85 533 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, allow_fd, __LINE__) ;
cannam@85 534
cannam@85 535 if (sfinfo.format != format)
cannam@85 536 { printf ("\n\nLine %d : Mono : Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, format, sfinfo.format) ;
cannam@85 537 exit (1) ;
cannam@85 538 } ;
cannam@85 539
cannam@85 540 if (sfinfo.frames < 2 * items)
cannam@85 541 { printf ("\n\nLine %d : Mono : Incorrect number of frames in file (too short). (%ld should be %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), items) ;
cannam@85 542 exit (1) ;
cannam@85 543 } ;
cannam@85 544
cannam@85 545 if (! long_file_ok && sfinfo.frames > 2 * items)
cannam@85 546 { printf ("\n\nLine %d : Mono : Incorrect number of frames in file (too long). (%ld should be %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), items) ;
cannam@85 547 exit (1) ;
cannam@85 548 } ;
cannam@85 549
cannam@85 550 if (sfinfo.channels != 1)
cannam@85 551 { printf ("\n\nLine %d : Mono : Incorrect number of channels in file.\n", __LINE__) ;
cannam@85 552 exit (1) ;
cannam@85 553 } ;
cannam@85 554
cannam@85 555 check_log_buffer_or_die (file, __LINE__) ;
cannam@85 556
cannam@85 557 test_read_short_or_die (file, 0, test, items, __LINE__) ;
cannam@85 558 for (k = 0 ; k < items ; k++)
cannam@85 559 if (CHAR_ERROR (orig [k], test [k]))
cannam@85 560 { printf ("\n\nLine %d: Mono : Incorrect sample A (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
cannam@85 561 oct_save_short (orig, test, items) ;
cannam@85 562 exit (1) ;
cannam@85 563 } ;
cannam@85 564
cannam@85 565 /* Seek to start of file. */
cannam@85 566 test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
cannam@85 567
cannam@85 568 test_read_short_or_die (file, 0, test, 4, __LINE__) ;
cannam@85 569 for (k = 0 ; k < 4 ; k++)
cannam@85 570 if (CHAR_ERROR (orig [k], test [k]))
cannam@85 571 { printf ("\n\nLine %d : Mono : Incorrect sample A (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
cannam@85 572 exit (1) ;
cannam@85 573 } ;
cannam@85 574
cannam@85 575 if ((format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_16 ||
cannam@85 576 (format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_24)
cannam@85 577 { sf_close (file) ;
cannam@85 578 unlink (filename) ;
cannam@85 579 printf ("no seek : ") ;
cannam@85 580 return ;
cannam@85 581 } ;
cannam@85 582
cannam@85 583 /* Seek to offset from start of file. */
cannam@85 584 test_seek_or_die (file, items + 10, SEEK_SET, items + 10, sfinfo.channels, __LINE__) ;
cannam@85 585
cannam@85 586 test_read_short_or_die (file, 0, test + 10, 4, __LINE__) ;
cannam@85 587 for (k = 10 ; k < 14 ; k++)
cannam@85 588 if (CHAR_ERROR (orig [k], test [k]))
cannam@85 589 { printf ("\n\nLine %d : Mono : Incorrect sample A (#%d : 0x%X => 0x%X).\n", __LINE__, k, test [k], orig [k]) ;
cannam@85 590 exit (1) ;
cannam@85 591 } ;
cannam@85 592
cannam@85 593 /* Seek to offset from current position. */
cannam@85 594 test_seek_or_die (file, 6, SEEK_CUR, items + 20, sfinfo.channels, __LINE__) ;
cannam@85 595
cannam@85 596 test_read_short_or_die (file, 0, test + 20, 4, __LINE__) ;
cannam@85 597 for (k = 20 ; k < 24 ; k++)
cannam@85 598 if (CHAR_ERROR (orig [k], test [k]))
cannam@85 599 { printf ("\n\nLine %d : Mono : Incorrect sample A (#%d : 0x%X => 0x%X).\n", __LINE__, k, test [k], orig [k]) ;
cannam@85 600 exit (1) ;
cannam@85 601 } ;
cannam@85 602
cannam@85 603 /* Seek to offset from end of file. */
cannam@85 604 test_seek_or_die (file, -1 * (sfinfo.frames - 10), SEEK_END, 10, sfinfo.channels, __LINE__) ;
cannam@85 605
cannam@85 606 test_read_short_or_die (file, 0, test + 10, 4, __LINE__) ;
cannam@85 607 for (k = 10 ; k < 14 ; k++)
cannam@85 608 if (CHAR_ERROR (orig [k], test [k]))
cannam@85 609 { printf ("\n\nLine %d : Mono : Incorrect sample D (#%d : 0x%X => 0x%X).\n", __LINE__, k, test [k], orig [k]) ;
cannam@85 610 exit (1) ;
cannam@85 611 } ;
cannam@85 612
cannam@85 613 /* Check read past end of file followed by sf_seek (sndfile, 0, SEEK_CUR). */
cannam@85 614 test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
cannam@85 615
cannam@85 616 count = 0 ;
cannam@85 617 while (count < sfinfo.frames)
cannam@85 618 count += sf_read_short (file, test, 311) ;
cannam@85 619
cannam@85 620 /* Check that no error has occurred. */
cannam@85 621 if (sf_error (file))
cannam@85 622 { printf ("\n\nLine %d : Mono : error where there shouldn't have been one.\n", __LINE__) ;
cannam@85 623 puts (sf_strerror (file)) ;
cannam@85 624 exit (1) ;
cannam@85 625 } ;
cannam@85 626
cannam@85 627 /* Check that we haven't read beyond EOF. */
cannam@85 628 if (count > sfinfo.frames)
cannam@85 629 { printf ("\n\nLines %d : read past end of file (%ld should be %ld)\n", __LINE__, (long) count, (long) sfinfo.frames) ;
cannam@85 630 exit (1) ;
cannam@85 631 } ;
cannam@85 632
cannam@85 633 test_seek_or_die (file, 0, SEEK_CUR, sfinfo.frames, sfinfo.channels, __LINE__) ;
cannam@85 634
cannam@85 635 sf_close (file) ;
cannam@85 636
cannam@85 637 multi_seek_test (filename, format) ;
cannam@85 638 write_seek_extend_test (filename, format) ;
cannam@85 639
cannam@85 640 } /* mono_char_test */
cannam@85 641
cannam@85 642 static void
cannam@85 643 stereo_char_test (const char *filename, int format, int long_file_ok, int allow_fd)
cannam@85 644 { SNDFILE *file ;
cannam@85 645 SF_INFO sfinfo ;
cannam@85 646 short *orig, *test ;
cannam@85 647 int k, items, frames ;
cannam@85 648
cannam@85 649 sfinfo.samplerate = 44100 ;
cannam@85 650 sfinfo.frames = SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */
cannam@85 651 sfinfo.channels = 2 ;
cannam@85 652 sfinfo.format = format ;
cannam@85 653
cannam@85 654 gen_windowed_sine_double (orig_data.d, DATA_LENGTH, 32000.0) ;
cannam@85 655
cannam@85 656 orig = orig_data.s ;
cannam@85 657 test = test_data.s ;
cannam@85 658
cannam@85 659 /* Make this a macro so gdb steps over it in one go. */
cannam@85 660 CONVERT_DATA (k, DATA_LENGTH, orig, orig_data.d) ;
cannam@85 661
cannam@85 662 items = DATA_LENGTH ;
cannam@85 663 frames = items / sfinfo.channels ;
cannam@85 664
cannam@85 665 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, allow_fd, __LINE__) ;
cannam@85 666
cannam@85 667 sf_set_string (file, SF_STR_ARTIST, "Your name here") ;
cannam@85 668
cannam@85 669 test_writef_short_or_die (file, 0, orig, frames, __LINE__) ;
cannam@85 670
cannam@85 671 sf_set_string (file, SF_STR_COPYRIGHT, "Copyright (c) 2003") ;
cannam@85 672
cannam@85 673 sf_close (file) ;
cannam@85 674
cannam@85 675 memset (test, 0, items * sizeof (short)) ;
cannam@85 676
cannam@85 677 if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_RAW)
cannam@85 678 memset (&sfinfo, 0, sizeof (sfinfo)) ;
cannam@85 679
cannam@85 680 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, allow_fd, __LINE__) ;
cannam@85 681
cannam@85 682 if (sfinfo.format != format)
cannam@85 683 { printf ("\n\nLine %d : Stereo : Returned format incorrect (0x%08X => 0x%08X).\n",
cannam@85 684 __LINE__, format, sfinfo.format) ;
cannam@85 685 exit (1) ;
cannam@85 686 } ;
cannam@85 687
cannam@85 688 if (sfinfo.frames < frames)
cannam@85 689 { printf ("\n\nLine %d : Stereo : Incorrect number of frames in file (too short). (%ld should be %d)\n",
cannam@85 690 __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), frames) ;
cannam@85 691 exit (1) ;
cannam@85 692 } ;
cannam@85 693
cannam@85 694 if (! long_file_ok && sfinfo.frames > frames)
cannam@85 695 { printf ("\n\nLine %d : Stereo : Incorrect number of frames in file (too long). (%ld should be %d)\n",
cannam@85 696 __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), frames) ;
cannam@85 697 exit (1) ;
cannam@85 698 } ;
cannam@85 699
cannam@85 700 if (sfinfo.channels != 2)
cannam@85 701 { printf ("\n\nLine %d : Stereo : Incorrect number of channels in file.\n", __LINE__) ;
cannam@85 702 exit (1) ;
cannam@85 703 } ;
cannam@85 704
cannam@85 705 check_log_buffer_or_die (file, __LINE__) ;
cannam@85 706
cannam@85 707 test_readf_short_or_die (file, 0, test, frames, __LINE__) ;
cannam@85 708 for (k = 0 ; k < items ; k++)
cannam@85 709 if (CHAR_ERROR (test [k], orig [k]))
cannam@85 710 { printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
cannam@85 711 exit (1) ;
cannam@85 712 } ;
cannam@85 713
cannam@85 714 /* Seek to start of file. */
cannam@85 715 test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
cannam@85 716
cannam@85 717 test_readf_short_or_die (file, 0, test, 2, __LINE__) ;
cannam@85 718 for (k = 0 ; k < 4 ; k++)
cannam@85 719 if (CHAR_ERROR (test [k], orig [k]))
cannam@85 720 { printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
cannam@85 721 exit (1) ;
cannam@85 722 } ;
cannam@85 723
cannam@85 724 /* Seek to offset from start of file. */
cannam@85 725 test_seek_or_die (file, 10, SEEK_SET, 10, sfinfo.channels, __LINE__) ;
cannam@85 726
cannam@85 727 /* Check for errors here. */
cannam@85 728 if (sf_error (file))
cannam@85 729 { printf ("Line %d: Should NOT return an error.\n", __LINE__) ;
cannam@85 730 puts (sf_strerror (file)) ;
cannam@85 731 exit (1) ;
cannam@85 732 } ;
cannam@85 733
cannam@85 734 if (sf_read_short (file, test, 1) > 0)
cannam@85 735 { printf ("Line %d: Should return 0.\n", __LINE__) ;
cannam@85 736 exit (1) ;
cannam@85 737 } ;
cannam@85 738
cannam@85 739 if (! sf_error (file))
cannam@85 740 { printf ("Line %d: Should return an error.\n", __LINE__) ;
cannam@85 741 exit (1) ;
cannam@85 742 } ;
cannam@85 743 /*-----------------------*/
cannam@85 744
cannam@85 745 test_readf_short_or_die (file, 0, test + 10, 2, __LINE__) ;
cannam@85 746 for (k = 20 ; k < 24 ; k++)
cannam@85 747 if (CHAR_ERROR (test [k], orig [k]))
cannam@85 748 { printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
cannam@85 749 exit (1) ;
cannam@85 750 } ;
cannam@85 751
cannam@85 752 /* Seek to offset from current position. */
cannam@85 753 test_seek_or_die (file, 8, SEEK_CUR, 20, sfinfo.channels, __LINE__) ;
cannam@85 754
cannam@85 755 test_readf_short_or_die (file, 0, test + 20, 2, __LINE__) ;
cannam@85 756 for (k = 40 ; k < 44 ; k++)
cannam@85 757 if (CHAR_ERROR (test [k], orig [k]))
cannam@85 758 { printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
cannam@85 759 exit (1) ;
cannam@85 760 } ;
cannam@85 761
cannam@85 762 /* Seek to offset from end of file. */
cannam@85 763 test_seek_or_die (file, -1 * (sfinfo.frames - 10), SEEK_END, 10, sfinfo.channels, __LINE__) ;
cannam@85 764
cannam@85 765 test_readf_short_or_die (file, 0, test + 20, 2, __LINE__) ;
cannam@85 766 for (k = 20 ; k < 24 ; k++)
cannam@85 767 if (CHAR_ERROR (test [k], orig [k]))
cannam@85 768 { printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
cannam@85 769 exit (1) ;
cannam@85 770 } ;
cannam@85 771
cannam@85 772 sf_close (file) ;
cannam@85 773 } /* stereo_char_test */
cannam@85 774
cannam@85 775 static void
cannam@85 776 mono_rdwr_char_test (const char *filename, int format, int long_file_ok, int allow_fd)
cannam@85 777 { SNDFILE *file ;
cannam@85 778 SF_INFO sfinfo ;
cannam@85 779 short *orig, *test ;
cannam@85 780 int k, pass ;
cannam@85 781
cannam@85 782 orig = orig_data.s ;
cannam@85 783 test = test_data.s ;
cannam@85 784
cannam@85 785 sfinfo.samplerate = SAMPLE_RATE ;
cannam@85 786 sfinfo.frames = DATA_LENGTH ;
cannam@85 787 sfinfo.channels = 1 ;
cannam@85 788 sfinfo.format = format ;
cannam@85 789
cannam@85 790 if ((format & SF_FORMAT_TYPEMASK) == SF_FORMAT_RAW
cannam@85 791 || (format & SF_FORMAT_TYPEMASK) == SF_FORMAT_AU
cannam@85 792 || (format & SF_FORMAT_TYPEMASK) == SF_FORMAT_SD2)
cannam@85 793 unlink (filename) ;
cannam@85 794 else
cannam@85 795 { /* Create a short file. */
cannam@85 796 create_short_file (filename) ;
cannam@85 797
cannam@85 798 /* Opening a already existing short file (ie invalid header) RDWR is disallowed.
cannam@85 799 ** If this returns a valif pointer sf_open() screwed up.
cannam@85 800 */
cannam@85 801 if ((file = sf_open (filename, SFM_RDWR, &sfinfo)))
cannam@85 802 { printf ("\n\nLine %d: sf_open should (SFM_RDWR) have failed but didn't.\n", __LINE__) ;
cannam@85 803 exit (1) ;
cannam@85 804 } ;
cannam@85 805
cannam@85 806 /* Truncate the file to zero bytes. */
cannam@85 807 if (truncate (filename, 0) < 0)
cannam@85 808 { printf ("\n\nLine %d: truncate (%s) failed", __LINE__, filename) ;
cannam@85 809 perror (NULL) ;
cannam@85 810 exit (1) ;
cannam@85 811 } ;
cannam@85 812 } ;
cannam@85 813
cannam@85 814 /* Opening a zero length file RDWR is allowed, but the SF_INFO struct must contain
cannam@85 815 ** all the usual data required when opening the file in WRITE mode.
cannam@85 816 */
cannam@85 817 sfinfo.samplerate = SAMPLE_RATE ;
cannam@85 818 sfinfo.frames = DATA_LENGTH ;
cannam@85 819 sfinfo.channels = 1 ;
cannam@85 820 sfinfo.format = format ;
cannam@85 821
cannam@85 822 file = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, allow_fd, __LINE__) ;
cannam@85 823
cannam@85 824 /* Do 3 writes followed by reads. After each, check the data and the current
cannam@85 825 ** read and write offsets.
cannam@85 826 */
cannam@85 827 for (pass = 1 ; pass <= 3 ; pass ++)
cannam@85 828 { orig [20] = pass * 2 ;
cannam@85 829
cannam@85 830 /* Write some data. */
cannam@85 831 test_write_short_or_die (file, pass, orig, DATA_LENGTH, __LINE__) ;
cannam@85 832
cannam@85 833 test_read_write_position_or_die (file, __LINE__, pass, (pass - 1) * DATA_LENGTH, pass * DATA_LENGTH) ;
cannam@85 834
cannam@85 835 /* Read what we just wrote. */
cannam@85 836 test_read_short_or_die (file, 0, test, DATA_LENGTH, __LINE__) ;
cannam@85 837
cannam@85 838 /* Check the data. */
cannam@85 839 for (k = 0 ; k < DATA_LENGTH ; k++)
cannam@85 840 if (CHAR_ERROR (orig [k], test [k]))
cannam@85 841 { printf ("\n\nLine %d (pass %d) A : Error at sample %d (0x%X => 0x%X).\n", __LINE__, pass, k, orig [k], test [k]) ;
cannam@85 842 oct_save_short (orig, test, DATA_LENGTH) ;
cannam@85 843 exit (1) ;
cannam@85 844 } ;
cannam@85 845
cannam@85 846 test_read_write_position_or_die (file, __LINE__, pass, pass * DATA_LENGTH, pass * DATA_LENGTH) ;
cannam@85 847 } ; /* for (pass ...) */
cannam@85 848
cannam@85 849 sf_close (file) ;
cannam@85 850
cannam@85 851 /* Open the file again to check the data. */
cannam@85 852 file = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, allow_fd, __LINE__) ;
cannam@85 853
cannam@85 854 if (sfinfo.format != format)
cannam@85 855 { printf ("\n\nLine %d : Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, format, sfinfo.format) ;
cannam@85 856 exit (1) ;
cannam@85 857 } ;
cannam@85 858
cannam@85 859 if (sfinfo.frames < 3 * DATA_LENGTH)
cannam@85 860 { printf ("\n\nLine %d : Not enough frames in file. (%ld < %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), 3 * DATA_LENGTH ) ;
cannam@85 861 exit (1) ;
cannam@85 862 }
cannam@85 863
cannam@85 864 if (! long_file_ok && sfinfo.frames != 3 * DATA_LENGTH)
cannam@85 865 { printf ("\n\nLine %d : Incorrect number of frames in file. (%ld should be %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), 3 * DATA_LENGTH ) ;
cannam@85 866 exit (1) ;
cannam@85 867 } ;
cannam@85 868
cannam@85 869 if (sfinfo.channels != 1)
cannam@85 870 { printf ("\n\nLine %d : Incorrect number of channels in file.\n", __LINE__) ;
cannam@85 871 exit (1) ;
cannam@85 872 } ;
cannam@85 873
cannam@85 874 if (! long_file_ok)
cannam@85 875 test_read_write_position_or_die (file, __LINE__, 0, 0, 3 * DATA_LENGTH) ;
cannam@85 876 else
cannam@85 877 test_seek_or_die (file, 3 * DATA_LENGTH, SFM_WRITE | SEEK_SET, 3 * DATA_LENGTH, sfinfo.channels, __LINE__) ;
cannam@85 878
cannam@85 879 for (pass = 1 ; pass <= 3 ; pass ++)
cannam@85 880 { orig [20] = pass * 2 ;
cannam@85 881
cannam@85 882 test_read_write_position_or_die (file, __LINE__, pass, (pass - 1) * DATA_LENGTH, 3 * DATA_LENGTH) ;
cannam@85 883
cannam@85 884 /* Read what we just wrote. */
cannam@85 885 test_read_short_or_die (file, pass, test, DATA_LENGTH, __LINE__) ;
cannam@85 886
cannam@85 887 /* Check the data. */
cannam@85 888 for (k = 0 ; k < DATA_LENGTH ; k++)
cannam@85 889 if (CHAR_ERROR (orig [k], test [k]))
cannam@85 890 { printf ("\n\nLine %d (pass %d) B : Error at sample %d (0x%X => 0x%X).\n", __LINE__, pass, k, orig [k], test [k]) ;
cannam@85 891 oct_save_short (orig, test, DATA_LENGTH) ;
cannam@85 892 exit (1) ;
cannam@85 893 } ;
cannam@85 894
cannam@85 895 } ; /* for (pass ...) */
cannam@85 896
cannam@85 897 sf_close (file) ;
cannam@85 898 } /* mono_rdwr_short_test */
cannam@85 899
cannam@85 900 static void
cannam@85 901 new_rdwr_char_test (const char *filename, int format, int allow_fd)
cannam@85 902 { SNDFILE *wfile, *rwfile ;
cannam@85 903 SF_INFO sfinfo ;
cannam@85 904 short *orig, *test ;
cannam@85 905 int items, frames ;
cannam@85 906
cannam@85 907 orig = orig_data.s ;
cannam@85 908 test = test_data.s ;
cannam@85 909
cannam@85 910 sfinfo.samplerate = 44100 ;
cannam@85 911 sfinfo.frames = SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */
cannam@85 912 sfinfo.channels = 2 ;
cannam@85 913 sfinfo.format = format ;
cannam@85 914
cannam@85 915 items = DATA_LENGTH ;
cannam@85 916 frames = items / sfinfo.channels ;
cannam@85 917
cannam@85 918 wfile = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, allow_fd, __LINE__) ;
cannam@85 919 sf_command (wfile, SFC_SET_UPDATE_HEADER_AUTO, NULL, SF_TRUE) ;
cannam@85 920 test_writef_short_or_die (wfile, 1, orig, frames, __LINE__) ;
cannam@85 921 sf_write_sync (wfile) ;
cannam@85 922 test_writef_short_or_die (wfile, 2, orig, frames, __LINE__) ;
cannam@85 923 sf_write_sync (wfile) ;
cannam@85 924
cannam@85 925 rwfile = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, allow_fd, __LINE__) ;
cannam@85 926 if (sfinfo.frames != 2 * frames)
cannam@85 927 { printf ("\n\nLine %d : incorrect number of frames in file (%ld should be %d)\n\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), 2 * frames) ;
cannam@85 928 exit (1) ;
cannam@85 929 } ;
cannam@85 930
cannam@85 931 test_writef_short_or_die (wfile, 3, orig, frames, __LINE__) ;
cannam@85 932
cannam@85 933 test_readf_short_or_die (rwfile, 1, test, frames, __LINE__) ;
cannam@85 934 test_readf_short_or_die (rwfile, 2, test, frames, __LINE__) ;
cannam@85 935
cannam@85 936 sf_close (wfile) ;
cannam@85 937 sf_close (rwfile) ;
cannam@85 938 } /* new_rdwr_char_test */
cannam@85 939
cannam@85 940
cannam@85 941 /*======================================================================================
cannam@85 942 */
cannam@85 943
cannam@85 944 static void mono_short_test (const char *filename, int format, int long_file_ok, int allow_fd) ;
cannam@85 945 static void stereo_short_test (const char *filename, int format, int long_file_ok, int allow_fd) ;
cannam@85 946 static void mono_rdwr_short_test (const char *filename, int format, int long_file_ok, int allow_fd) ;
cannam@85 947 static void new_rdwr_short_test (const char *filename, int format, int allow_fd) ;
cannam@85 948 static void multi_seek_test (const char * filename, int format) ;
cannam@85 949 static void write_seek_extend_test (const char * filename, int format) ;
cannam@85 950
cannam@85 951 static void
cannam@85 952 pcm_test_short (const char *filename, int format, int long_file_ok)
cannam@85 953 { SF_INFO sfinfo ;
cannam@85 954 short *orig ;
cannam@85 955 int k, allow_fd ;
cannam@85 956
cannam@85 957 /* Sd2 files cannot be opened from an existing file descriptor. */
cannam@85 958 allow_fd = ((format & SF_FORMAT_TYPEMASK) == SF_FORMAT_SD2) ? SF_FALSE : SF_TRUE ;
cannam@85 959
cannam@85 960 print_test_name ("pcm_test_short", filename) ;
cannam@85 961
cannam@85 962 sfinfo.samplerate = 44100 ;
cannam@85 963 sfinfo.frames = SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */
cannam@85 964 sfinfo.channels = 1 ;
cannam@85 965 sfinfo.format = format ;
cannam@85 966
cannam@85 967 gen_windowed_sine_double (orig_data.d, DATA_LENGTH, 32000.0) ;
cannam@85 968
cannam@85 969 orig = orig_data.s ;
cannam@85 970
cannam@85 971 /* Make this a macro so gdb steps over it in one go. */
cannam@85 972 CONVERT_DATA (k, DATA_LENGTH, orig, orig_data.d) ;
cannam@85 973
cannam@85 974 /* Some test broken out here. */
cannam@85 975
cannam@85 976 mono_short_test (filename, format, long_file_ok, allow_fd) ;
cannam@85 977
cannam@85 978 /* Sub format DWVW does not allow seeking. */
cannam@85 979 if ((format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_16 ||
cannam@85 980 (format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_24)
cannam@85 981 { unlink (filename) ;
cannam@85 982 printf ("no seek : ok\n") ;
cannam@85 983 return ;
cannam@85 984 } ;
cannam@85 985
cannam@85 986 if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_FLAC)
cannam@85 987 mono_rdwr_short_test (filename, format, long_file_ok, allow_fd) ;
cannam@85 988
cannam@85 989 /* If the format doesn't support stereo we're done. */
cannam@85 990 sfinfo.channels = 2 ;
cannam@85 991 if (sf_format_check (&sfinfo) == 0)
cannam@85 992 { unlink (filename) ;
cannam@85 993 puts ("no stereo : ok") ;
cannam@85 994 return ;
cannam@85 995 } ;
cannam@85 996
cannam@85 997 stereo_short_test (filename, format, long_file_ok, allow_fd) ;
cannam@85 998
cannam@85 999 /* New read/write test. Not sure if this is needed yet. */
cannam@85 1000
cannam@85 1001 if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_PAF &&
cannam@85 1002 (format & SF_FORMAT_TYPEMASK) != SF_FORMAT_VOC &&
cannam@85 1003 (format & SF_FORMAT_TYPEMASK) != SF_FORMAT_FLAC)
cannam@85 1004 new_rdwr_short_test (filename, format, allow_fd) ;
cannam@85 1005
cannam@85 1006 delete_file (format, filename) ;
cannam@85 1007
cannam@85 1008 puts ("ok") ;
cannam@85 1009 return ;
cannam@85 1010 } /* pcm_test_short */
cannam@85 1011
cannam@85 1012 static void
cannam@85 1013 mono_short_test (const char *filename, int format, int long_file_ok, int allow_fd)
cannam@85 1014 { SNDFILE *file ;
cannam@85 1015 SF_INFO sfinfo ;
cannam@85 1016 short *orig, *test ;
cannam@85 1017 sf_count_t count ;
cannam@85 1018 int k, items ;
cannam@85 1019
cannam@85 1020 sfinfo.samplerate = 44100 ;
cannam@85 1021 sfinfo.frames = SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */
cannam@85 1022 sfinfo.channels = 1 ;
cannam@85 1023 sfinfo.format = format ;
cannam@85 1024
cannam@85 1025 orig = orig_data.s ;
cannam@85 1026 test = test_data.s ;
cannam@85 1027
cannam@85 1028 items = DATA_LENGTH ;
cannam@85 1029
cannam@85 1030 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, allow_fd, __LINE__) ;
cannam@85 1031
cannam@85 1032 sf_set_string (file, SF_STR_ARTIST, "Your name here") ;
cannam@85 1033
cannam@85 1034 test_write_short_or_die (file, 0, orig, items, __LINE__) ;
cannam@85 1035 sf_write_sync (file) ;
cannam@85 1036 test_write_short_or_die (file, 0, orig, items, __LINE__) ;
cannam@85 1037 sf_write_sync (file) ;
cannam@85 1038
cannam@85 1039 /* Add non-audio data after the audio. */
cannam@85 1040 sf_set_string (file, SF_STR_COPYRIGHT, "Copyright (c) 2003") ;
cannam@85 1041
cannam@85 1042 sf_close (file) ;
cannam@85 1043
cannam@85 1044 memset (test, 0, items * sizeof (short)) ;
cannam@85 1045
cannam@85 1046 if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_RAW)
cannam@85 1047 memset (&sfinfo, 0, sizeof (sfinfo)) ;
cannam@85 1048
cannam@85 1049 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, allow_fd, __LINE__) ;
cannam@85 1050
cannam@85 1051 if (sfinfo.format != format)
cannam@85 1052 { printf ("\n\nLine %d : Mono : Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, format, sfinfo.format) ;
cannam@85 1053 exit (1) ;
cannam@85 1054 } ;
cannam@85 1055
cannam@85 1056 if (sfinfo.frames < 2 * items)
cannam@85 1057 { printf ("\n\nLine %d : Mono : Incorrect number of frames in file (too short). (%ld should be %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), items) ;
cannam@85 1058 exit (1) ;
cannam@85 1059 } ;
cannam@85 1060
cannam@85 1061 if (! long_file_ok && sfinfo.frames > 2 * items)
cannam@85 1062 { printf ("\n\nLine %d : Mono : Incorrect number of frames in file (too long). (%ld should be %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), items) ;
cannam@85 1063 exit (1) ;
cannam@85 1064 } ;
cannam@85 1065
cannam@85 1066 if (sfinfo.channels != 1)
cannam@85 1067 { printf ("\n\nLine %d : Mono : Incorrect number of channels in file.\n", __LINE__) ;
cannam@85 1068 exit (1) ;
cannam@85 1069 } ;
cannam@85 1070
cannam@85 1071 check_log_buffer_or_die (file, __LINE__) ;
cannam@85 1072
cannam@85 1073 test_read_short_or_die (file, 0, test, items, __LINE__) ;
cannam@85 1074 for (k = 0 ; k < items ; k++)
cannam@85 1075 if (INT_ERROR (orig [k], test [k]))
cannam@85 1076 { printf ("\n\nLine %d: Mono : Incorrect sample A (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
cannam@85 1077 oct_save_short (orig, test, items) ;
cannam@85 1078 exit (1) ;
cannam@85 1079 } ;
cannam@85 1080
cannam@85 1081 /* Seek to start of file. */
cannam@85 1082 test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
cannam@85 1083
cannam@85 1084 test_read_short_or_die (file, 0, test, 4, __LINE__) ;
cannam@85 1085 for (k = 0 ; k < 4 ; k++)
cannam@85 1086 if (INT_ERROR (orig [k], test [k]))
cannam@85 1087 { printf ("\n\nLine %d : Mono : Incorrect sample A (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
cannam@85 1088 exit (1) ;
cannam@85 1089 } ;
cannam@85 1090
cannam@85 1091 if ((format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_16 ||
cannam@85 1092 (format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_24)
cannam@85 1093 { sf_close (file) ;
cannam@85 1094 unlink (filename) ;
cannam@85 1095 printf ("no seek : ") ;
cannam@85 1096 return ;
cannam@85 1097 } ;
cannam@85 1098
cannam@85 1099 /* Seek to offset from start of file. */
cannam@85 1100 test_seek_or_die (file, items + 10, SEEK_SET, items + 10, sfinfo.channels, __LINE__) ;
cannam@85 1101
cannam@85 1102 test_read_short_or_die (file, 0, test + 10, 4, __LINE__) ;
cannam@85 1103 for (k = 10 ; k < 14 ; k++)
cannam@85 1104 if (INT_ERROR (orig [k], test [k]))
cannam@85 1105 { printf ("\n\nLine %d : Mono : Incorrect sample A (#%d : 0x%X => 0x%X).\n", __LINE__, k, test [k], orig [k]) ;
cannam@85 1106 exit (1) ;
cannam@85 1107 } ;
cannam@85 1108
cannam@85 1109 /* Seek to offset from current position. */
cannam@85 1110 test_seek_or_die (file, 6, SEEK_CUR, items + 20, sfinfo.channels, __LINE__) ;
cannam@85 1111
cannam@85 1112 test_read_short_or_die (file, 0, test + 20, 4, __LINE__) ;
cannam@85 1113 for (k = 20 ; k < 24 ; k++)
cannam@85 1114 if (INT_ERROR (orig [k], test [k]))
cannam@85 1115 { printf ("\n\nLine %d : Mono : Incorrect sample A (#%d : 0x%X => 0x%X).\n", __LINE__, k, test [k], orig [k]) ;
cannam@85 1116 exit (1) ;
cannam@85 1117 } ;
cannam@85 1118
cannam@85 1119 /* Seek to offset from end of file. */
cannam@85 1120 test_seek_or_die (file, -1 * (sfinfo.frames - 10), SEEK_END, 10, sfinfo.channels, __LINE__) ;
cannam@85 1121
cannam@85 1122 test_read_short_or_die (file, 0, test + 10, 4, __LINE__) ;
cannam@85 1123 for (k = 10 ; k < 14 ; k++)
cannam@85 1124 if (INT_ERROR (orig [k], test [k]))
cannam@85 1125 { printf ("\n\nLine %d : Mono : Incorrect sample D (#%d : 0x%X => 0x%X).\n", __LINE__, k, test [k], orig [k]) ;
cannam@85 1126 exit (1) ;
cannam@85 1127 } ;
cannam@85 1128
cannam@85 1129 /* Check read past end of file followed by sf_seek (sndfile, 0, SEEK_CUR). */
cannam@85 1130 test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
cannam@85 1131
cannam@85 1132 count = 0 ;
cannam@85 1133 while (count < sfinfo.frames)
cannam@85 1134 count += sf_read_short (file, test, 311) ;
cannam@85 1135
cannam@85 1136 /* Check that no error has occurred. */
cannam@85 1137 if (sf_error (file))
cannam@85 1138 { printf ("\n\nLine %d : Mono : error where there shouldn't have been one.\n", __LINE__) ;
cannam@85 1139 puts (sf_strerror (file)) ;
cannam@85 1140 exit (1) ;
cannam@85 1141 } ;
cannam@85 1142
cannam@85 1143 /* Check that we haven't read beyond EOF. */
cannam@85 1144 if (count > sfinfo.frames)
cannam@85 1145 { printf ("\n\nLines %d : read past end of file (%ld should be %ld)\n", __LINE__, (long) count, (long) sfinfo.frames) ;
cannam@85 1146 exit (1) ;
cannam@85 1147 } ;
cannam@85 1148
cannam@85 1149 test_seek_or_die (file, 0, SEEK_CUR, sfinfo.frames, sfinfo.channels, __LINE__) ;
cannam@85 1150
cannam@85 1151 sf_close (file) ;
cannam@85 1152
cannam@85 1153 multi_seek_test (filename, format) ;
cannam@85 1154 write_seek_extend_test (filename, format) ;
cannam@85 1155
cannam@85 1156 } /* mono_short_test */
cannam@85 1157
cannam@85 1158 static void
cannam@85 1159 stereo_short_test (const char *filename, int format, int long_file_ok, int allow_fd)
cannam@85 1160 { SNDFILE *file ;
cannam@85 1161 SF_INFO sfinfo ;
cannam@85 1162 short *orig, *test ;
cannam@85 1163 int k, items, frames ;
cannam@85 1164
cannam@85 1165 sfinfo.samplerate = 44100 ;
cannam@85 1166 sfinfo.frames = SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */
cannam@85 1167 sfinfo.channels = 2 ;
cannam@85 1168 sfinfo.format = format ;
cannam@85 1169
cannam@85 1170 gen_windowed_sine_double (orig_data.d, DATA_LENGTH, 32000.0) ;
cannam@85 1171
cannam@85 1172 orig = orig_data.s ;
cannam@85 1173 test = test_data.s ;
cannam@85 1174
cannam@85 1175 /* Make this a macro so gdb steps over it in one go. */
cannam@85 1176 CONVERT_DATA (k, DATA_LENGTH, orig, orig_data.d) ;
cannam@85 1177
cannam@85 1178 items = DATA_LENGTH ;
cannam@85 1179 frames = items / sfinfo.channels ;
cannam@85 1180
cannam@85 1181 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, allow_fd, __LINE__) ;
cannam@85 1182
cannam@85 1183 sf_set_string (file, SF_STR_ARTIST, "Your name here") ;
cannam@85 1184
cannam@85 1185 test_writef_short_or_die (file, 0, orig, frames, __LINE__) ;
cannam@85 1186
cannam@85 1187 sf_set_string (file, SF_STR_COPYRIGHT, "Copyright (c) 2003") ;
cannam@85 1188
cannam@85 1189 sf_close (file) ;
cannam@85 1190
cannam@85 1191 memset (test, 0, items * sizeof (short)) ;
cannam@85 1192
cannam@85 1193 if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_RAW)
cannam@85 1194 memset (&sfinfo, 0, sizeof (sfinfo)) ;
cannam@85 1195
cannam@85 1196 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, allow_fd, __LINE__) ;
cannam@85 1197
cannam@85 1198 if (sfinfo.format != format)
cannam@85 1199 { printf ("\n\nLine %d : Stereo : Returned format incorrect (0x%08X => 0x%08X).\n",
cannam@85 1200 __LINE__, format, sfinfo.format) ;
cannam@85 1201 exit (1) ;
cannam@85 1202 } ;
cannam@85 1203
cannam@85 1204 if (sfinfo.frames < frames)
cannam@85 1205 { printf ("\n\nLine %d : Stereo : Incorrect number of frames in file (too short). (%ld should be %d)\n",
cannam@85 1206 __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), frames) ;
cannam@85 1207 exit (1) ;
cannam@85 1208 } ;
cannam@85 1209
cannam@85 1210 if (! long_file_ok && sfinfo.frames > frames)
cannam@85 1211 { printf ("\n\nLine %d : Stereo : Incorrect number of frames in file (too long). (%ld should be %d)\n",
cannam@85 1212 __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), frames) ;
cannam@85 1213 exit (1) ;
cannam@85 1214 } ;
cannam@85 1215
cannam@85 1216 if (sfinfo.channels != 2)
cannam@85 1217 { printf ("\n\nLine %d : Stereo : Incorrect number of channels in file.\n", __LINE__) ;
cannam@85 1218 exit (1) ;
cannam@85 1219 } ;
cannam@85 1220
cannam@85 1221 check_log_buffer_or_die (file, __LINE__) ;
cannam@85 1222
cannam@85 1223 test_readf_short_or_die (file, 0, test, frames, __LINE__) ;
cannam@85 1224 for (k = 0 ; k < items ; k++)
cannam@85 1225 if (INT_ERROR (test [k], orig [k]))
cannam@85 1226 { printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
cannam@85 1227 exit (1) ;
cannam@85 1228 } ;
cannam@85 1229
cannam@85 1230 /* Seek to start of file. */
cannam@85 1231 test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
cannam@85 1232
cannam@85 1233 test_readf_short_or_die (file, 0, test, 2, __LINE__) ;
cannam@85 1234 for (k = 0 ; k < 4 ; k++)
cannam@85 1235 if (INT_ERROR (test [k], orig [k]))
cannam@85 1236 { printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
cannam@85 1237 exit (1) ;
cannam@85 1238 } ;
cannam@85 1239
cannam@85 1240 /* Seek to offset from start of file. */
cannam@85 1241 test_seek_or_die (file, 10, SEEK_SET, 10, sfinfo.channels, __LINE__) ;
cannam@85 1242
cannam@85 1243 /* Check for errors here. */
cannam@85 1244 if (sf_error (file))
cannam@85 1245 { printf ("Line %d: Should NOT return an error.\n", __LINE__) ;
cannam@85 1246 puts (sf_strerror (file)) ;
cannam@85 1247 exit (1) ;
cannam@85 1248 } ;
cannam@85 1249
cannam@85 1250 if (sf_read_short (file, test, 1) > 0)
cannam@85 1251 { printf ("Line %d: Should return 0.\n", __LINE__) ;
cannam@85 1252 exit (1) ;
cannam@85 1253 } ;
cannam@85 1254
cannam@85 1255 if (! sf_error (file))
cannam@85 1256 { printf ("Line %d: Should return an error.\n", __LINE__) ;
cannam@85 1257 exit (1) ;
cannam@85 1258 } ;
cannam@85 1259 /*-----------------------*/
cannam@85 1260
cannam@85 1261 test_readf_short_or_die (file, 0, test + 10, 2, __LINE__) ;
cannam@85 1262 for (k = 20 ; k < 24 ; k++)
cannam@85 1263 if (INT_ERROR (test [k], orig [k]))
cannam@85 1264 { printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
cannam@85 1265 exit (1) ;
cannam@85 1266 } ;
cannam@85 1267
cannam@85 1268 /* Seek to offset from current position. */
cannam@85 1269 test_seek_or_die (file, 8, SEEK_CUR, 20, sfinfo.channels, __LINE__) ;
cannam@85 1270
cannam@85 1271 test_readf_short_or_die (file, 0, test + 20, 2, __LINE__) ;
cannam@85 1272 for (k = 40 ; k < 44 ; k++)
cannam@85 1273 if (INT_ERROR (test [k], orig [k]))
cannam@85 1274 { printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
cannam@85 1275 exit (1) ;
cannam@85 1276 } ;
cannam@85 1277
cannam@85 1278 /* Seek to offset from end of file. */
cannam@85 1279 test_seek_or_die (file, -1 * (sfinfo.frames - 10), SEEK_END, 10, sfinfo.channels, __LINE__) ;
cannam@85 1280
cannam@85 1281 test_readf_short_or_die (file, 0, test + 20, 2, __LINE__) ;
cannam@85 1282 for (k = 20 ; k < 24 ; k++)
cannam@85 1283 if (INT_ERROR (test [k], orig [k]))
cannam@85 1284 { printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
cannam@85 1285 exit (1) ;
cannam@85 1286 } ;
cannam@85 1287
cannam@85 1288 sf_close (file) ;
cannam@85 1289 } /* stereo_short_test */
cannam@85 1290
cannam@85 1291 static void
cannam@85 1292 mono_rdwr_short_test (const char *filename, int format, int long_file_ok, int allow_fd)
cannam@85 1293 { SNDFILE *file ;
cannam@85 1294 SF_INFO sfinfo ;
cannam@85 1295 short *orig, *test ;
cannam@85 1296 int k, pass ;
cannam@85 1297
cannam@85 1298 orig = orig_data.s ;
cannam@85 1299 test = test_data.s ;
cannam@85 1300
cannam@85 1301 sfinfo.samplerate = SAMPLE_RATE ;
cannam@85 1302 sfinfo.frames = DATA_LENGTH ;
cannam@85 1303 sfinfo.channels = 1 ;
cannam@85 1304 sfinfo.format = format ;
cannam@85 1305
cannam@85 1306 if ((format & SF_FORMAT_TYPEMASK) == SF_FORMAT_RAW
cannam@85 1307 || (format & SF_FORMAT_TYPEMASK) == SF_FORMAT_AU
cannam@85 1308 || (format & SF_FORMAT_TYPEMASK) == SF_FORMAT_SD2)
cannam@85 1309 unlink (filename) ;
cannam@85 1310 else
cannam@85 1311 { /* Create a short file. */
cannam@85 1312 create_short_file (filename) ;
cannam@85 1313
cannam@85 1314 /* Opening a already existing short file (ie invalid header) RDWR is disallowed.
cannam@85 1315 ** If this returns a valif pointer sf_open() screwed up.
cannam@85 1316 */
cannam@85 1317 if ((file = sf_open (filename, SFM_RDWR, &sfinfo)))
cannam@85 1318 { printf ("\n\nLine %d: sf_open should (SFM_RDWR) have failed but didn't.\n", __LINE__) ;
cannam@85 1319 exit (1) ;
cannam@85 1320 } ;
cannam@85 1321
cannam@85 1322 /* Truncate the file to zero bytes. */
cannam@85 1323 if (truncate (filename, 0) < 0)
cannam@85 1324 { printf ("\n\nLine %d: truncate (%s) failed", __LINE__, filename) ;
cannam@85 1325 perror (NULL) ;
cannam@85 1326 exit (1) ;
cannam@85 1327 } ;
cannam@85 1328 } ;
cannam@85 1329
cannam@85 1330 /* Opening a zero length file RDWR is allowed, but the SF_INFO struct must contain
cannam@85 1331 ** all the usual data required when opening the file in WRITE mode.
cannam@85 1332 */
cannam@85 1333 sfinfo.samplerate = SAMPLE_RATE ;
cannam@85 1334 sfinfo.frames = DATA_LENGTH ;
cannam@85 1335 sfinfo.channels = 1 ;
cannam@85 1336 sfinfo.format = format ;
cannam@85 1337
cannam@85 1338 file = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, allow_fd, __LINE__) ;
cannam@85 1339
cannam@85 1340 /* Do 3 writes followed by reads. After each, check the data and the current
cannam@85 1341 ** read and write offsets.
cannam@85 1342 */
cannam@85 1343 for (pass = 1 ; pass <= 3 ; pass ++)
cannam@85 1344 { orig [20] = pass * 2 ;
cannam@85 1345
cannam@85 1346 /* Write some data. */
cannam@85 1347 test_write_short_or_die (file, pass, orig, DATA_LENGTH, __LINE__) ;
cannam@85 1348
cannam@85 1349 test_read_write_position_or_die (file, __LINE__, pass, (pass - 1) * DATA_LENGTH, pass * DATA_LENGTH) ;
cannam@85 1350
cannam@85 1351 /* Read what we just wrote. */
cannam@85 1352 test_read_short_or_die (file, 0, test, DATA_LENGTH, __LINE__) ;
cannam@85 1353
cannam@85 1354 /* Check the data. */
cannam@85 1355 for (k = 0 ; k < DATA_LENGTH ; k++)
cannam@85 1356 if (INT_ERROR (orig [k], test [k]))
cannam@85 1357 { printf ("\n\nLine %d (pass %d) A : Error at sample %d (0x%X => 0x%X).\n", __LINE__, pass, k, orig [k], test [k]) ;
cannam@85 1358 oct_save_short (orig, test, DATA_LENGTH) ;
cannam@85 1359 exit (1) ;
cannam@85 1360 } ;
cannam@85 1361
cannam@85 1362 test_read_write_position_or_die (file, __LINE__, pass, pass * DATA_LENGTH, pass * DATA_LENGTH) ;
cannam@85 1363 } ; /* for (pass ...) */
cannam@85 1364
cannam@85 1365 sf_close (file) ;
cannam@85 1366
cannam@85 1367 /* Open the file again to check the data. */
cannam@85 1368 file = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, allow_fd, __LINE__) ;
cannam@85 1369
cannam@85 1370 if (sfinfo.format != format)
cannam@85 1371 { printf ("\n\nLine %d : Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, format, sfinfo.format) ;
cannam@85 1372 exit (1) ;
cannam@85 1373 } ;
cannam@85 1374
cannam@85 1375 if (sfinfo.frames < 3 * DATA_LENGTH)
cannam@85 1376 { printf ("\n\nLine %d : Not enough frames in file. (%ld < %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), 3 * DATA_LENGTH ) ;
cannam@85 1377 exit (1) ;
cannam@85 1378 }
cannam@85 1379
cannam@85 1380 if (! long_file_ok && sfinfo.frames != 3 * DATA_LENGTH)
cannam@85 1381 { printf ("\n\nLine %d : Incorrect number of frames in file. (%ld should be %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), 3 * DATA_LENGTH ) ;
cannam@85 1382 exit (1) ;
cannam@85 1383 } ;
cannam@85 1384
cannam@85 1385 if (sfinfo.channels != 1)
cannam@85 1386 { printf ("\n\nLine %d : Incorrect number of channels in file.\n", __LINE__) ;
cannam@85 1387 exit (1) ;
cannam@85 1388 } ;
cannam@85 1389
cannam@85 1390 if (! long_file_ok)
cannam@85 1391 test_read_write_position_or_die (file, __LINE__, 0, 0, 3 * DATA_LENGTH) ;
cannam@85 1392 else
cannam@85 1393 test_seek_or_die (file, 3 * DATA_LENGTH, SFM_WRITE | SEEK_SET, 3 * DATA_LENGTH, sfinfo.channels, __LINE__) ;
cannam@85 1394
cannam@85 1395 for (pass = 1 ; pass <= 3 ; pass ++)
cannam@85 1396 { orig [20] = pass * 2 ;
cannam@85 1397
cannam@85 1398 test_read_write_position_or_die (file, __LINE__, pass, (pass - 1) * DATA_LENGTH, 3 * DATA_LENGTH) ;
cannam@85 1399
cannam@85 1400 /* Read what we just wrote. */
cannam@85 1401 test_read_short_or_die (file, pass, test, DATA_LENGTH, __LINE__) ;
cannam@85 1402
cannam@85 1403 /* Check the data. */
cannam@85 1404 for (k = 0 ; k < DATA_LENGTH ; k++)
cannam@85 1405 if (INT_ERROR (orig [k], test [k]))
cannam@85 1406 { printf ("\n\nLine %d (pass %d) B : Error at sample %d (0x%X => 0x%X).\n", __LINE__, pass, k, orig [k], test [k]) ;
cannam@85 1407 oct_save_short (orig, test, DATA_LENGTH) ;
cannam@85 1408 exit (1) ;
cannam@85 1409 } ;
cannam@85 1410
cannam@85 1411 } ; /* for (pass ...) */
cannam@85 1412
cannam@85 1413 sf_close (file) ;
cannam@85 1414 } /* mono_rdwr_short_test */
cannam@85 1415
cannam@85 1416 static void
cannam@85 1417 new_rdwr_short_test (const char *filename, int format, int allow_fd)
cannam@85 1418 { SNDFILE *wfile, *rwfile ;
cannam@85 1419 SF_INFO sfinfo ;
cannam@85 1420 short *orig, *test ;
cannam@85 1421 int items, frames ;
cannam@85 1422
cannam@85 1423 orig = orig_data.s ;
cannam@85 1424 test = test_data.s ;
cannam@85 1425
cannam@85 1426 sfinfo.samplerate = 44100 ;
cannam@85 1427 sfinfo.frames = SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */
cannam@85 1428 sfinfo.channels = 2 ;
cannam@85 1429 sfinfo.format = format ;
cannam@85 1430
cannam@85 1431 items = DATA_LENGTH ;
cannam@85 1432 frames = items / sfinfo.channels ;
cannam@85 1433
cannam@85 1434 wfile = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, allow_fd, __LINE__) ;
cannam@85 1435 sf_command (wfile, SFC_SET_UPDATE_HEADER_AUTO, NULL, SF_TRUE) ;
cannam@85 1436 test_writef_short_or_die (wfile, 1, orig, frames, __LINE__) ;
cannam@85 1437 sf_write_sync (wfile) ;
cannam@85 1438 test_writef_short_or_die (wfile, 2, orig, frames, __LINE__) ;
cannam@85 1439 sf_write_sync (wfile) ;
cannam@85 1440
cannam@85 1441 rwfile = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, allow_fd, __LINE__) ;
cannam@85 1442 if (sfinfo.frames != 2 * frames)
cannam@85 1443 { printf ("\n\nLine %d : incorrect number of frames in file (%ld should be %d)\n\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), 2 * frames) ;
cannam@85 1444 exit (1) ;
cannam@85 1445 } ;
cannam@85 1446
cannam@85 1447 test_writef_short_or_die (wfile, 3, orig, frames, __LINE__) ;
cannam@85 1448
cannam@85 1449 test_readf_short_or_die (rwfile, 1, test, frames, __LINE__) ;
cannam@85 1450 test_readf_short_or_die (rwfile, 2, test, frames, __LINE__) ;
cannam@85 1451
cannam@85 1452 sf_close (wfile) ;
cannam@85 1453 sf_close (rwfile) ;
cannam@85 1454 } /* new_rdwr_short_test */
cannam@85 1455
cannam@85 1456
cannam@85 1457 /*======================================================================================
cannam@85 1458 */
cannam@85 1459
cannam@85 1460 static void mono_24bit_test (const char *filename, int format, int long_file_ok, int allow_fd) ;
cannam@85 1461 static void stereo_24bit_test (const char *filename, int format, int long_file_ok, int allow_fd) ;
cannam@85 1462 static void mono_rdwr_24bit_test (const char *filename, int format, int long_file_ok, int allow_fd) ;
cannam@85 1463 static void new_rdwr_24bit_test (const char *filename, int format, int allow_fd) ;
cannam@85 1464 static void multi_seek_test (const char * filename, int format) ;
cannam@85 1465 static void write_seek_extend_test (const char * filename, int format) ;
cannam@85 1466
cannam@85 1467 static void
cannam@85 1468 pcm_test_24bit (const char *filename, int format, int long_file_ok)
cannam@85 1469 { SF_INFO sfinfo ;
cannam@85 1470 int *orig ;
cannam@85 1471 int k, allow_fd ;
cannam@85 1472
cannam@85 1473 /* Sd2 files cannot be opened from an existing file descriptor. */
cannam@85 1474 allow_fd = ((format & SF_FORMAT_TYPEMASK) == SF_FORMAT_SD2) ? SF_FALSE : SF_TRUE ;
cannam@85 1475
cannam@85 1476 print_test_name ("pcm_test_24bit", filename) ;
cannam@85 1477
cannam@85 1478 sfinfo.samplerate = 44100 ;
cannam@85 1479 sfinfo.frames = SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */
cannam@85 1480 sfinfo.channels = 1 ;
cannam@85 1481 sfinfo.format = format ;
cannam@85 1482
cannam@85 1483 gen_windowed_sine_double (orig_data.d, DATA_LENGTH, (1.0 * 0x7F000000)) ;
cannam@85 1484
cannam@85 1485 orig = orig_data.i ;
cannam@85 1486
cannam@85 1487 /* Make this a macro so gdb steps over it in one go. */
cannam@85 1488 CONVERT_DATA (k, DATA_LENGTH, orig, orig_data.d) ;
cannam@85 1489
cannam@85 1490 /* Some test broken out here. */
cannam@85 1491
cannam@85 1492 mono_24bit_test (filename, format, long_file_ok, allow_fd) ;
cannam@85 1493
cannam@85 1494 /* Sub format DWVW does not allow seeking. */
cannam@85 1495 if ((format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_16 ||
cannam@85 1496 (format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_24)
cannam@85 1497 { unlink (filename) ;
cannam@85 1498 printf ("no seek : ok\n") ;
cannam@85 1499 return ;
cannam@85 1500 } ;
cannam@85 1501
cannam@85 1502 if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_FLAC)
cannam@85 1503 mono_rdwr_24bit_test (filename, format, long_file_ok, allow_fd) ;
cannam@85 1504
cannam@85 1505 /* If the format doesn't support stereo we're done. */
cannam@85 1506 sfinfo.channels = 2 ;
cannam@85 1507 if (sf_format_check (&sfinfo) == 0)
cannam@85 1508 { unlink (filename) ;
cannam@85 1509 puts ("no stereo : ok") ;
cannam@85 1510 return ;
cannam@85 1511 } ;
cannam@85 1512
cannam@85 1513 stereo_24bit_test (filename, format, long_file_ok, allow_fd) ;
cannam@85 1514
cannam@85 1515 /* New read/write test. Not sure if this is needed yet. */
cannam@85 1516
cannam@85 1517 if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_PAF &&
cannam@85 1518 (format & SF_FORMAT_TYPEMASK) != SF_FORMAT_VOC &&
cannam@85 1519 (format & SF_FORMAT_TYPEMASK) != SF_FORMAT_FLAC)
cannam@85 1520 new_rdwr_24bit_test (filename, format, allow_fd) ;
cannam@85 1521
cannam@85 1522 delete_file (format, filename) ;
cannam@85 1523
cannam@85 1524 puts ("ok") ;
cannam@85 1525 return ;
cannam@85 1526 } /* pcm_test_24bit */
cannam@85 1527
cannam@85 1528 static void
cannam@85 1529 mono_24bit_test (const char *filename, int format, int long_file_ok, int allow_fd)
cannam@85 1530 { SNDFILE *file ;
cannam@85 1531 SF_INFO sfinfo ;
cannam@85 1532 int *orig, *test ;
cannam@85 1533 sf_count_t count ;
cannam@85 1534 int k, items ;
cannam@85 1535
cannam@85 1536 sfinfo.samplerate = 44100 ;
cannam@85 1537 sfinfo.frames = SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */
cannam@85 1538 sfinfo.channels = 1 ;
cannam@85 1539 sfinfo.format = format ;
cannam@85 1540
cannam@85 1541 orig = orig_data.i ;
cannam@85 1542 test = test_data.i ;
cannam@85 1543
cannam@85 1544 items = DATA_LENGTH ;
cannam@85 1545
cannam@85 1546 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, allow_fd, __LINE__) ;
cannam@85 1547
cannam@85 1548 sf_set_string (file, SF_STR_ARTIST, "Your name here") ;
cannam@85 1549
cannam@85 1550 test_write_int_or_die (file, 0, orig, items, __LINE__) ;
cannam@85 1551 sf_write_sync (file) ;
cannam@85 1552 test_write_int_or_die (file, 0, orig, items, __LINE__) ;
cannam@85 1553 sf_write_sync (file) ;
cannam@85 1554
cannam@85 1555 /* Add non-audio data after the audio. */
cannam@85 1556 sf_set_string (file, SF_STR_COPYRIGHT, "Copyright (c) 2003") ;
cannam@85 1557
cannam@85 1558 sf_close (file) ;
cannam@85 1559
cannam@85 1560 memset (test, 0, items * sizeof (int)) ;
cannam@85 1561
cannam@85 1562 if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_RAW)
cannam@85 1563 memset (&sfinfo, 0, sizeof (sfinfo)) ;
cannam@85 1564
cannam@85 1565 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, allow_fd, __LINE__) ;
cannam@85 1566
cannam@85 1567 if (sfinfo.format != format)
cannam@85 1568 { printf ("\n\nLine %d : Mono : Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, format, sfinfo.format) ;
cannam@85 1569 exit (1) ;
cannam@85 1570 } ;
cannam@85 1571
cannam@85 1572 if (sfinfo.frames < 2 * items)
cannam@85 1573 { printf ("\n\nLine %d : Mono : Incorrect number of frames in file (too short). (%ld should be %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), items) ;
cannam@85 1574 exit (1) ;
cannam@85 1575 } ;
cannam@85 1576
cannam@85 1577 if (! long_file_ok && sfinfo.frames > 2 * items)
cannam@85 1578 { printf ("\n\nLine %d : Mono : Incorrect number of frames in file (too long). (%ld should be %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), items) ;
cannam@85 1579 exit (1) ;
cannam@85 1580 } ;
cannam@85 1581
cannam@85 1582 if (sfinfo.channels != 1)
cannam@85 1583 { printf ("\n\nLine %d : Mono : Incorrect number of channels in file.\n", __LINE__) ;
cannam@85 1584 exit (1) ;
cannam@85 1585 } ;
cannam@85 1586
cannam@85 1587 check_log_buffer_or_die (file, __LINE__) ;
cannam@85 1588
cannam@85 1589 test_read_int_or_die (file, 0, test, items, __LINE__) ;
cannam@85 1590 for (k = 0 ; k < items ; k++)
cannam@85 1591 if (TRIBYTE_ERROR (orig [k], test [k]))
cannam@85 1592 { printf ("\n\nLine %d: Mono : Incorrect sample A (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
cannam@85 1593 oct_save_int (orig, test, items) ;
cannam@85 1594 exit (1) ;
cannam@85 1595 } ;
cannam@85 1596
cannam@85 1597 /* Seek to start of file. */
cannam@85 1598 test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
cannam@85 1599
cannam@85 1600 test_read_int_or_die (file, 0, test, 4, __LINE__) ;
cannam@85 1601 for (k = 0 ; k < 4 ; k++)
cannam@85 1602 if (TRIBYTE_ERROR (orig [k], test [k]))
cannam@85 1603 { printf ("\n\nLine %d : Mono : Incorrect sample A (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
cannam@85 1604 exit (1) ;
cannam@85 1605 } ;
cannam@85 1606
cannam@85 1607 if ((format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_16 ||
cannam@85 1608 (format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_24)
cannam@85 1609 { sf_close (file) ;
cannam@85 1610 unlink (filename) ;
cannam@85 1611 printf ("no seek : ") ;
cannam@85 1612 return ;
cannam@85 1613 } ;
cannam@85 1614
cannam@85 1615 /* Seek to offset from start of file. */
cannam@85 1616 test_seek_or_die (file, items + 10, SEEK_SET, items + 10, sfinfo.channels, __LINE__) ;
cannam@85 1617
cannam@85 1618 test_read_int_or_die (file, 0, test + 10, 4, __LINE__) ;
cannam@85 1619 for (k = 10 ; k < 14 ; k++)
cannam@85 1620 if (TRIBYTE_ERROR (orig [k], test [k]))
cannam@85 1621 { printf ("\n\nLine %d : Mono : Incorrect sample A (#%d : 0x%X => 0x%X).\n", __LINE__, k, test [k], orig [k]) ;
cannam@85 1622 exit (1) ;
cannam@85 1623 } ;
cannam@85 1624
cannam@85 1625 /* Seek to offset from current position. */
cannam@85 1626 test_seek_or_die (file, 6, SEEK_CUR, items + 20, sfinfo.channels, __LINE__) ;
cannam@85 1627
cannam@85 1628 test_read_int_or_die (file, 0, test + 20, 4, __LINE__) ;
cannam@85 1629 for (k = 20 ; k < 24 ; k++)
cannam@85 1630 if (TRIBYTE_ERROR (orig [k], test [k]))
cannam@85 1631 { printf ("\n\nLine %d : Mono : Incorrect sample A (#%d : 0x%X => 0x%X).\n", __LINE__, k, test [k], orig [k]) ;
cannam@85 1632 exit (1) ;
cannam@85 1633 } ;
cannam@85 1634
cannam@85 1635 /* Seek to offset from end of file. */
cannam@85 1636 test_seek_or_die (file, -1 * (sfinfo.frames - 10), SEEK_END, 10, sfinfo.channels, __LINE__) ;
cannam@85 1637
cannam@85 1638 test_read_int_or_die (file, 0, test + 10, 4, __LINE__) ;
cannam@85 1639 for (k = 10 ; k < 14 ; k++)
cannam@85 1640 if (TRIBYTE_ERROR (orig [k], test [k]))
cannam@85 1641 { printf ("\n\nLine %d : Mono : Incorrect sample D (#%d : 0x%X => 0x%X).\n", __LINE__, k, test [k], orig [k]) ;
cannam@85 1642 exit (1) ;
cannam@85 1643 } ;
cannam@85 1644
cannam@85 1645 /* Check read past end of file followed by sf_seek (sndfile, 0, SEEK_CUR). */
cannam@85 1646 test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
cannam@85 1647
cannam@85 1648 count = 0 ;
cannam@85 1649 while (count < sfinfo.frames)
cannam@85 1650 count += sf_read_int (file, test, 311) ;
cannam@85 1651
cannam@85 1652 /* Check that no error has occurred. */
cannam@85 1653 if (sf_error (file))
cannam@85 1654 { printf ("\n\nLine %d : Mono : error where there shouldn't have been one.\n", __LINE__) ;
cannam@85 1655 puts (sf_strerror (file)) ;
cannam@85 1656 exit (1) ;
cannam@85 1657 } ;
cannam@85 1658
cannam@85 1659 /* Check that we haven't read beyond EOF. */
cannam@85 1660 if (count > sfinfo.frames)
cannam@85 1661 { printf ("\n\nLines %d : read past end of file (%ld should be %ld)\n", __LINE__, (long) count, (long) sfinfo.frames) ;
cannam@85 1662 exit (1) ;
cannam@85 1663 } ;
cannam@85 1664
cannam@85 1665 test_seek_or_die (file, 0, SEEK_CUR, sfinfo.frames, sfinfo.channels, __LINE__) ;
cannam@85 1666
cannam@85 1667 sf_close (file) ;
cannam@85 1668
cannam@85 1669 multi_seek_test (filename, format) ;
cannam@85 1670 write_seek_extend_test (filename, format) ;
cannam@85 1671
cannam@85 1672 } /* mono_24bit_test */
cannam@85 1673
cannam@85 1674 static void
cannam@85 1675 stereo_24bit_test (const char *filename, int format, int long_file_ok, int allow_fd)
cannam@85 1676 { SNDFILE *file ;
cannam@85 1677 SF_INFO sfinfo ;
cannam@85 1678 int *orig, *test ;
cannam@85 1679 int k, items, frames ;
cannam@85 1680
cannam@85 1681 sfinfo.samplerate = 44100 ;
cannam@85 1682 sfinfo.frames = SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */
cannam@85 1683 sfinfo.channels = 2 ;
cannam@85 1684 sfinfo.format = format ;
cannam@85 1685
cannam@85 1686 gen_windowed_sine_double (orig_data.d, DATA_LENGTH, (1.0 * 0x7F000000)) ;
cannam@85 1687
cannam@85 1688 orig = orig_data.i ;
cannam@85 1689 test = test_data.i ;
cannam@85 1690
cannam@85 1691 /* Make this a macro so gdb steps over it in one go. */
cannam@85 1692 CONVERT_DATA (k, DATA_LENGTH, orig, orig_data.d) ;
cannam@85 1693
cannam@85 1694 items = DATA_LENGTH ;
cannam@85 1695 frames = items / sfinfo.channels ;
cannam@85 1696
cannam@85 1697 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, allow_fd, __LINE__) ;
cannam@85 1698
cannam@85 1699 sf_set_string (file, SF_STR_ARTIST, "Your name here") ;
cannam@85 1700
cannam@85 1701 test_writef_int_or_die (file, 0, orig, frames, __LINE__) ;
cannam@85 1702
cannam@85 1703 sf_set_string (file, SF_STR_COPYRIGHT, "Copyright (c) 2003") ;
cannam@85 1704
cannam@85 1705 sf_close (file) ;
cannam@85 1706
cannam@85 1707 memset (test, 0, items * sizeof (int)) ;
cannam@85 1708
cannam@85 1709 if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_RAW)
cannam@85 1710 memset (&sfinfo, 0, sizeof (sfinfo)) ;
cannam@85 1711
cannam@85 1712 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, allow_fd, __LINE__) ;
cannam@85 1713
cannam@85 1714 if (sfinfo.format != format)
cannam@85 1715 { printf ("\n\nLine %d : Stereo : Returned format incorrect (0x%08X => 0x%08X).\n",
cannam@85 1716 __LINE__, format, sfinfo.format) ;
cannam@85 1717 exit (1) ;
cannam@85 1718 } ;
cannam@85 1719
cannam@85 1720 if (sfinfo.frames < frames)
cannam@85 1721 { printf ("\n\nLine %d : Stereo : Incorrect number of frames in file (too short). (%ld should be %d)\n",
cannam@85 1722 __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), frames) ;
cannam@85 1723 exit (1) ;
cannam@85 1724 } ;
cannam@85 1725
cannam@85 1726 if (! long_file_ok && sfinfo.frames > frames)
cannam@85 1727 { printf ("\n\nLine %d : Stereo : Incorrect number of frames in file (too long). (%ld should be %d)\n",
cannam@85 1728 __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), frames) ;
cannam@85 1729 exit (1) ;
cannam@85 1730 } ;
cannam@85 1731
cannam@85 1732 if (sfinfo.channels != 2)
cannam@85 1733 { printf ("\n\nLine %d : Stereo : Incorrect number of channels in file.\n", __LINE__) ;
cannam@85 1734 exit (1) ;
cannam@85 1735 } ;
cannam@85 1736
cannam@85 1737 check_log_buffer_or_die (file, __LINE__) ;
cannam@85 1738
cannam@85 1739 test_readf_int_or_die (file, 0, test, frames, __LINE__) ;
cannam@85 1740 for (k = 0 ; k < items ; k++)
cannam@85 1741 if (TRIBYTE_ERROR (test [k], orig [k]))
cannam@85 1742 { printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
cannam@85 1743 exit (1) ;
cannam@85 1744 } ;
cannam@85 1745
cannam@85 1746 /* Seek to start of file. */
cannam@85 1747 test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
cannam@85 1748
cannam@85 1749 test_readf_int_or_die (file, 0, test, 2, __LINE__) ;
cannam@85 1750 for (k = 0 ; k < 4 ; k++)
cannam@85 1751 if (TRIBYTE_ERROR (test [k], orig [k]))
cannam@85 1752 { printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
cannam@85 1753 exit (1) ;
cannam@85 1754 } ;
cannam@85 1755
cannam@85 1756 /* Seek to offset from start of file. */
cannam@85 1757 test_seek_or_die (file, 10, SEEK_SET, 10, sfinfo.channels, __LINE__) ;
cannam@85 1758
cannam@85 1759 /* Check for errors here. */
cannam@85 1760 if (sf_error (file))
cannam@85 1761 { printf ("Line %d: Should NOT return an error.\n", __LINE__) ;
cannam@85 1762 puts (sf_strerror (file)) ;
cannam@85 1763 exit (1) ;
cannam@85 1764 } ;
cannam@85 1765
cannam@85 1766 if (sf_read_int (file, test, 1) > 0)
cannam@85 1767 { printf ("Line %d: Should return 0.\n", __LINE__) ;
cannam@85 1768 exit (1) ;
cannam@85 1769 } ;
cannam@85 1770
cannam@85 1771 if (! sf_error (file))
cannam@85 1772 { printf ("Line %d: Should return an error.\n", __LINE__) ;
cannam@85 1773 exit (1) ;
cannam@85 1774 } ;
cannam@85 1775 /*-----------------------*/
cannam@85 1776
cannam@85 1777 test_readf_int_or_die (file, 0, test + 10, 2, __LINE__) ;
cannam@85 1778 for (k = 20 ; k < 24 ; k++)
cannam@85 1779 if (TRIBYTE_ERROR (test [k], orig [k]))
cannam@85 1780 { printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
cannam@85 1781 exit (1) ;
cannam@85 1782 } ;
cannam@85 1783
cannam@85 1784 /* Seek to offset from current position. */
cannam@85 1785 test_seek_or_die (file, 8, SEEK_CUR, 20, sfinfo.channels, __LINE__) ;
cannam@85 1786
cannam@85 1787 test_readf_int_or_die (file, 0, test + 20, 2, __LINE__) ;
cannam@85 1788 for (k = 40 ; k < 44 ; k++)
cannam@85 1789 if (TRIBYTE_ERROR (test [k], orig [k]))
cannam@85 1790 { printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
cannam@85 1791 exit (1) ;
cannam@85 1792 } ;
cannam@85 1793
cannam@85 1794 /* Seek to offset from end of file. */
cannam@85 1795 test_seek_or_die (file, -1 * (sfinfo.frames - 10), SEEK_END, 10, sfinfo.channels, __LINE__) ;
cannam@85 1796
cannam@85 1797 test_readf_int_or_die (file, 0, test + 20, 2, __LINE__) ;
cannam@85 1798 for (k = 20 ; k < 24 ; k++)
cannam@85 1799 if (TRIBYTE_ERROR (test [k], orig [k]))
cannam@85 1800 { printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
cannam@85 1801 exit (1) ;
cannam@85 1802 } ;
cannam@85 1803
cannam@85 1804 sf_close (file) ;
cannam@85 1805 } /* stereo_24bit_test */
cannam@85 1806
cannam@85 1807 static void
cannam@85 1808 mono_rdwr_24bit_test (const char *filename, int format, int long_file_ok, int allow_fd)
cannam@85 1809 { SNDFILE *file ;
cannam@85 1810 SF_INFO sfinfo ;
cannam@85 1811 int *orig, *test ;
cannam@85 1812 int k, pass ;
cannam@85 1813
cannam@85 1814 orig = orig_data.i ;
cannam@85 1815 test = test_data.i ;
cannam@85 1816
cannam@85 1817 sfinfo.samplerate = SAMPLE_RATE ;
cannam@85 1818 sfinfo.frames = DATA_LENGTH ;
cannam@85 1819 sfinfo.channels = 1 ;
cannam@85 1820 sfinfo.format = format ;
cannam@85 1821
cannam@85 1822 if ((format & SF_FORMAT_TYPEMASK) == SF_FORMAT_RAW
cannam@85 1823 || (format & SF_FORMAT_TYPEMASK) == SF_FORMAT_AU
cannam@85 1824 || (format & SF_FORMAT_TYPEMASK) == SF_FORMAT_SD2)
cannam@85 1825 unlink (filename) ;
cannam@85 1826 else
cannam@85 1827 { /* Create a short file. */
cannam@85 1828 create_short_file (filename) ;
cannam@85 1829
cannam@85 1830 /* Opening a already existing short file (ie invalid header) RDWR is disallowed.
cannam@85 1831 ** If this returns a valif pointer sf_open() screwed up.
cannam@85 1832 */
cannam@85 1833 if ((file = sf_open (filename, SFM_RDWR, &sfinfo)))
cannam@85 1834 { printf ("\n\nLine %d: sf_open should (SFM_RDWR) have failed but didn't.\n", __LINE__) ;
cannam@85 1835 exit (1) ;
cannam@85 1836 } ;
cannam@85 1837
cannam@85 1838 /* Truncate the file to zero bytes. */
cannam@85 1839 if (truncate (filename, 0) < 0)
cannam@85 1840 { printf ("\n\nLine %d: truncate (%s) failed", __LINE__, filename) ;
cannam@85 1841 perror (NULL) ;
cannam@85 1842 exit (1) ;
cannam@85 1843 } ;
cannam@85 1844 } ;
cannam@85 1845
cannam@85 1846 /* Opening a zero length file RDWR is allowed, but the SF_INFO struct must contain
cannam@85 1847 ** all the usual data required when opening the file in WRITE mode.
cannam@85 1848 */
cannam@85 1849 sfinfo.samplerate = SAMPLE_RATE ;
cannam@85 1850 sfinfo.frames = DATA_LENGTH ;
cannam@85 1851 sfinfo.channels = 1 ;
cannam@85 1852 sfinfo.format = format ;
cannam@85 1853
cannam@85 1854 file = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, allow_fd, __LINE__) ;
cannam@85 1855
cannam@85 1856 /* Do 3 writes followed by reads. After each, check the data and the current
cannam@85 1857 ** read and write offsets.
cannam@85 1858 */
cannam@85 1859 for (pass = 1 ; pass <= 3 ; pass ++)
cannam@85 1860 { orig [20] = pass * 2 ;
cannam@85 1861
cannam@85 1862 /* Write some data. */
cannam@85 1863 test_write_int_or_die (file, pass, orig, DATA_LENGTH, __LINE__) ;
cannam@85 1864
cannam@85 1865 test_read_write_position_or_die (file, __LINE__, pass, (pass - 1) * DATA_LENGTH, pass * DATA_LENGTH) ;
cannam@85 1866
cannam@85 1867 /* Read what we just wrote. */
cannam@85 1868 test_read_int_or_die (file, 0, test, DATA_LENGTH, __LINE__) ;
cannam@85 1869
cannam@85 1870 /* Check the data. */
cannam@85 1871 for (k = 0 ; k < DATA_LENGTH ; k++)
cannam@85 1872 if (TRIBYTE_ERROR (orig [k], test [k]))
cannam@85 1873 { printf ("\n\nLine %d (pass %d) A : Error at sample %d (0x%X => 0x%X).\n", __LINE__, pass, k, orig [k], test [k]) ;
cannam@85 1874 oct_save_int (orig, test, DATA_LENGTH) ;
cannam@85 1875 exit (1) ;
cannam@85 1876 } ;
cannam@85 1877
cannam@85 1878 test_read_write_position_or_die (file, __LINE__, pass, pass * DATA_LENGTH, pass * DATA_LENGTH) ;
cannam@85 1879 } ; /* for (pass ...) */
cannam@85 1880
cannam@85 1881 sf_close (file) ;
cannam@85 1882
cannam@85 1883 /* Open the file again to check the data. */
cannam@85 1884 file = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, allow_fd, __LINE__) ;
cannam@85 1885
cannam@85 1886 if (sfinfo.format != format)
cannam@85 1887 { printf ("\n\nLine %d : Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, format, sfinfo.format) ;
cannam@85 1888 exit (1) ;
cannam@85 1889 } ;
cannam@85 1890
cannam@85 1891 if (sfinfo.frames < 3 * DATA_LENGTH)
cannam@85 1892 { printf ("\n\nLine %d : Not enough frames in file. (%ld < %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), 3 * DATA_LENGTH ) ;
cannam@85 1893 exit (1) ;
cannam@85 1894 }
cannam@85 1895
cannam@85 1896 if (! long_file_ok && sfinfo.frames != 3 * DATA_LENGTH)
cannam@85 1897 { printf ("\n\nLine %d : Incorrect number of frames in file. (%ld should be %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), 3 * DATA_LENGTH ) ;
cannam@85 1898 exit (1) ;
cannam@85 1899 } ;
cannam@85 1900
cannam@85 1901 if (sfinfo.channels != 1)
cannam@85 1902 { printf ("\n\nLine %d : Incorrect number of channels in file.\n", __LINE__) ;
cannam@85 1903 exit (1) ;
cannam@85 1904 } ;
cannam@85 1905
cannam@85 1906 if (! long_file_ok)
cannam@85 1907 test_read_write_position_or_die (file, __LINE__, 0, 0, 3 * DATA_LENGTH) ;
cannam@85 1908 else
cannam@85 1909 test_seek_or_die (file, 3 * DATA_LENGTH, SFM_WRITE | SEEK_SET, 3 * DATA_LENGTH, sfinfo.channels, __LINE__) ;
cannam@85 1910
cannam@85 1911 for (pass = 1 ; pass <= 3 ; pass ++)
cannam@85 1912 { orig [20] = pass * 2 ;
cannam@85 1913
cannam@85 1914 test_read_write_position_or_die (file, __LINE__, pass, (pass - 1) * DATA_LENGTH, 3 * DATA_LENGTH) ;
cannam@85 1915
cannam@85 1916 /* Read what we just wrote. */
cannam@85 1917 test_read_int_or_die (file, pass, test, DATA_LENGTH, __LINE__) ;
cannam@85 1918
cannam@85 1919 /* Check the data. */
cannam@85 1920 for (k = 0 ; k < DATA_LENGTH ; k++)
cannam@85 1921 if (TRIBYTE_ERROR (orig [k], test [k]))
cannam@85 1922 { printf ("\n\nLine %d (pass %d) B : Error at sample %d (0x%X => 0x%X).\n", __LINE__, pass, k, orig [k], test [k]) ;
cannam@85 1923 oct_save_int (orig, test, DATA_LENGTH) ;
cannam@85 1924 exit (1) ;
cannam@85 1925 } ;
cannam@85 1926
cannam@85 1927 } ; /* for (pass ...) */
cannam@85 1928
cannam@85 1929 sf_close (file) ;
cannam@85 1930 } /* mono_rdwr_int_test */
cannam@85 1931
cannam@85 1932 static void
cannam@85 1933 new_rdwr_24bit_test (const char *filename, int format, int allow_fd)
cannam@85 1934 { SNDFILE *wfile, *rwfile ;
cannam@85 1935 SF_INFO sfinfo ;
cannam@85 1936 int *orig, *test ;
cannam@85 1937 int items, frames ;
cannam@85 1938
cannam@85 1939 orig = orig_data.i ;
cannam@85 1940 test = test_data.i ;
cannam@85 1941
cannam@85 1942 sfinfo.samplerate = 44100 ;
cannam@85 1943 sfinfo.frames = SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */
cannam@85 1944 sfinfo.channels = 2 ;
cannam@85 1945 sfinfo.format = format ;
cannam@85 1946
cannam@85 1947 items = DATA_LENGTH ;
cannam@85 1948 frames = items / sfinfo.channels ;
cannam@85 1949
cannam@85 1950 wfile = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, allow_fd, __LINE__) ;
cannam@85 1951 sf_command (wfile, SFC_SET_UPDATE_HEADER_AUTO, NULL, SF_TRUE) ;
cannam@85 1952 test_writef_int_or_die (wfile, 1, orig, frames, __LINE__) ;
cannam@85 1953 sf_write_sync (wfile) ;
cannam@85 1954 test_writef_int_or_die (wfile, 2, orig, frames, __LINE__) ;
cannam@85 1955 sf_write_sync (wfile) ;
cannam@85 1956
cannam@85 1957 rwfile = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, allow_fd, __LINE__) ;
cannam@85 1958 if (sfinfo.frames != 2 * frames)
cannam@85 1959 { printf ("\n\nLine %d : incorrect number of frames in file (%ld should be %d)\n\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), 2 * frames) ;
cannam@85 1960 exit (1) ;
cannam@85 1961 } ;
cannam@85 1962
cannam@85 1963 test_writef_int_or_die (wfile, 3, orig, frames, __LINE__) ;
cannam@85 1964
cannam@85 1965 test_readf_int_or_die (rwfile, 1, test, frames, __LINE__) ;
cannam@85 1966 test_readf_int_or_die (rwfile, 2, test, frames, __LINE__) ;
cannam@85 1967
cannam@85 1968 sf_close (wfile) ;
cannam@85 1969 sf_close (rwfile) ;
cannam@85 1970 } /* new_rdwr_24bit_test */
cannam@85 1971
cannam@85 1972
cannam@85 1973 /*======================================================================================
cannam@85 1974 */
cannam@85 1975
cannam@85 1976 static void mono_int_test (const char *filename, int format, int long_file_ok, int allow_fd) ;
cannam@85 1977 static void stereo_int_test (const char *filename, int format, int long_file_ok, int allow_fd) ;
cannam@85 1978 static void mono_rdwr_int_test (const char *filename, int format, int long_file_ok, int allow_fd) ;
cannam@85 1979 static void new_rdwr_int_test (const char *filename, int format, int allow_fd) ;
cannam@85 1980 static void multi_seek_test (const char * filename, int format) ;
cannam@85 1981 static void write_seek_extend_test (const char * filename, int format) ;
cannam@85 1982
cannam@85 1983 static void
cannam@85 1984 pcm_test_int (const char *filename, int format, int long_file_ok)
cannam@85 1985 { SF_INFO sfinfo ;
cannam@85 1986 int *orig ;
cannam@85 1987 int k, allow_fd ;
cannam@85 1988
cannam@85 1989 /* Sd2 files cannot be opened from an existing file descriptor. */
cannam@85 1990 allow_fd = ((format & SF_FORMAT_TYPEMASK) == SF_FORMAT_SD2) ? SF_FALSE : SF_TRUE ;
cannam@85 1991
cannam@85 1992 print_test_name ("pcm_test_int", filename) ;
cannam@85 1993
cannam@85 1994 sfinfo.samplerate = 44100 ;
cannam@85 1995 sfinfo.frames = SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */
cannam@85 1996 sfinfo.channels = 1 ;
cannam@85 1997 sfinfo.format = format ;
cannam@85 1998
cannam@85 1999 gen_windowed_sine_double (orig_data.d, DATA_LENGTH, (1.0 * 0x7F000000)) ;
cannam@85 2000
cannam@85 2001 orig = orig_data.i ;
cannam@85 2002
cannam@85 2003 /* Make this a macro so gdb steps over it in one go. */
cannam@85 2004 CONVERT_DATA (k, DATA_LENGTH, orig, orig_data.d) ;
cannam@85 2005
cannam@85 2006 /* Some test broken out here. */
cannam@85 2007
cannam@85 2008 mono_int_test (filename, format, long_file_ok, allow_fd) ;
cannam@85 2009
cannam@85 2010 /* Sub format DWVW does not allow seeking. */
cannam@85 2011 if ((format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_16 ||
cannam@85 2012 (format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_24)
cannam@85 2013 { unlink (filename) ;
cannam@85 2014 printf ("no seek : ok\n") ;
cannam@85 2015 return ;
cannam@85 2016 } ;
cannam@85 2017
cannam@85 2018 if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_FLAC)
cannam@85 2019 mono_rdwr_int_test (filename, format, long_file_ok, allow_fd) ;
cannam@85 2020
cannam@85 2021 /* If the format doesn't support stereo we're done. */
cannam@85 2022 sfinfo.channels = 2 ;
cannam@85 2023 if (sf_format_check (&sfinfo) == 0)
cannam@85 2024 { unlink (filename) ;
cannam@85 2025 puts ("no stereo : ok") ;
cannam@85 2026 return ;
cannam@85 2027 } ;
cannam@85 2028
cannam@85 2029 stereo_int_test (filename, format, long_file_ok, allow_fd) ;
cannam@85 2030
cannam@85 2031 /* New read/write test. Not sure if this is needed yet. */
cannam@85 2032
cannam@85 2033 if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_PAF &&
cannam@85 2034 (format & SF_FORMAT_TYPEMASK) != SF_FORMAT_VOC &&
cannam@85 2035 (format & SF_FORMAT_TYPEMASK) != SF_FORMAT_FLAC)
cannam@85 2036 new_rdwr_int_test (filename, format, allow_fd) ;
cannam@85 2037
cannam@85 2038 delete_file (format, filename) ;
cannam@85 2039
cannam@85 2040 puts ("ok") ;
cannam@85 2041 return ;
cannam@85 2042 } /* pcm_test_int */
cannam@85 2043
cannam@85 2044 static void
cannam@85 2045 mono_int_test (const char *filename, int format, int long_file_ok, int allow_fd)
cannam@85 2046 { SNDFILE *file ;
cannam@85 2047 SF_INFO sfinfo ;
cannam@85 2048 int *orig, *test ;
cannam@85 2049 sf_count_t count ;
cannam@85 2050 int k, items ;
cannam@85 2051
cannam@85 2052 sfinfo.samplerate = 44100 ;
cannam@85 2053 sfinfo.frames = SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */
cannam@85 2054 sfinfo.channels = 1 ;
cannam@85 2055 sfinfo.format = format ;
cannam@85 2056
cannam@85 2057 orig = orig_data.i ;
cannam@85 2058 test = test_data.i ;
cannam@85 2059
cannam@85 2060 items = DATA_LENGTH ;
cannam@85 2061
cannam@85 2062 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, allow_fd, __LINE__) ;
cannam@85 2063
cannam@85 2064 sf_set_string (file, SF_STR_ARTIST, "Your name here") ;
cannam@85 2065
cannam@85 2066 test_write_int_or_die (file, 0, orig, items, __LINE__) ;
cannam@85 2067 sf_write_sync (file) ;
cannam@85 2068 test_write_int_or_die (file, 0, orig, items, __LINE__) ;
cannam@85 2069 sf_write_sync (file) ;
cannam@85 2070
cannam@85 2071 /* Add non-audio data after the audio. */
cannam@85 2072 sf_set_string (file, SF_STR_COPYRIGHT, "Copyright (c) 2003") ;
cannam@85 2073
cannam@85 2074 sf_close (file) ;
cannam@85 2075
cannam@85 2076 memset (test, 0, items * sizeof (int)) ;
cannam@85 2077
cannam@85 2078 if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_RAW)
cannam@85 2079 memset (&sfinfo, 0, sizeof (sfinfo)) ;
cannam@85 2080
cannam@85 2081 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, allow_fd, __LINE__) ;
cannam@85 2082
cannam@85 2083 if (sfinfo.format != format)
cannam@85 2084 { printf ("\n\nLine %d : Mono : Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, format, sfinfo.format) ;
cannam@85 2085 exit (1) ;
cannam@85 2086 } ;
cannam@85 2087
cannam@85 2088 if (sfinfo.frames < 2 * items)
cannam@85 2089 { printf ("\n\nLine %d : Mono : Incorrect number of frames in file (too short). (%ld should be %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), items) ;
cannam@85 2090 exit (1) ;
cannam@85 2091 } ;
cannam@85 2092
cannam@85 2093 if (! long_file_ok && sfinfo.frames > 2 * items)
cannam@85 2094 { printf ("\n\nLine %d : Mono : Incorrect number of frames in file (too long). (%ld should be %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), items) ;
cannam@85 2095 exit (1) ;
cannam@85 2096 } ;
cannam@85 2097
cannam@85 2098 if (sfinfo.channels != 1)
cannam@85 2099 { printf ("\n\nLine %d : Mono : Incorrect number of channels in file.\n", __LINE__) ;
cannam@85 2100 exit (1) ;
cannam@85 2101 } ;
cannam@85 2102
cannam@85 2103 check_log_buffer_or_die (file, __LINE__) ;
cannam@85 2104
cannam@85 2105 test_read_int_or_die (file, 0, test, items, __LINE__) ;
cannam@85 2106 for (k = 0 ; k < items ; k++)
cannam@85 2107 if (INT_ERROR (orig [k], test [k]))
cannam@85 2108 { printf ("\n\nLine %d: Mono : Incorrect sample A (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
cannam@85 2109 oct_save_int (orig, test, items) ;
cannam@85 2110 exit (1) ;
cannam@85 2111 } ;
cannam@85 2112
cannam@85 2113 /* Seek to start of file. */
cannam@85 2114 test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
cannam@85 2115
cannam@85 2116 test_read_int_or_die (file, 0, test, 4, __LINE__) ;
cannam@85 2117 for (k = 0 ; k < 4 ; k++)
cannam@85 2118 if (INT_ERROR (orig [k], test [k]))
cannam@85 2119 { printf ("\n\nLine %d : Mono : Incorrect sample A (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
cannam@85 2120 exit (1) ;
cannam@85 2121 } ;
cannam@85 2122
cannam@85 2123 if ((format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_16 ||
cannam@85 2124 (format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_24)
cannam@85 2125 { sf_close (file) ;
cannam@85 2126 unlink (filename) ;
cannam@85 2127 printf ("no seek : ") ;
cannam@85 2128 return ;
cannam@85 2129 } ;
cannam@85 2130
cannam@85 2131 /* Seek to offset from start of file. */
cannam@85 2132 test_seek_or_die (file, items + 10, SEEK_SET, items + 10, sfinfo.channels, __LINE__) ;
cannam@85 2133
cannam@85 2134 test_read_int_or_die (file, 0, test + 10, 4, __LINE__) ;
cannam@85 2135 for (k = 10 ; k < 14 ; k++)
cannam@85 2136 if (INT_ERROR (orig [k], test [k]))
cannam@85 2137 { printf ("\n\nLine %d : Mono : Incorrect sample A (#%d : 0x%X => 0x%X).\n", __LINE__, k, test [k], orig [k]) ;
cannam@85 2138 exit (1) ;
cannam@85 2139 } ;
cannam@85 2140
cannam@85 2141 /* Seek to offset from current position. */
cannam@85 2142 test_seek_or_die (file, 6, SEEK_CUR, items + 20, sfinfo.channels, __LINE__) ;
cannam@85 2143
cannam@85 2144 test_read_int_or_die (file, 0, test + 20, 4, __LINE__) ;
cannam@85 2145 for (k = 20 ; k < 24 ; k++)
cannam@85 2146 if (INT_ERROR (orig [k], test [k]))
cannam@85 2147 { printf ("\n\nLine %d : Mono : Incorrect sample A (#%d : 0x%X => 0x%X).\n", __LINE__, k, test [k], orig [k]) ;
cannam@85 2148 exit (1) ;
cannam@85 2149 } ;
cannam@85 2150
cannam@85 2151 /* Seek to offset from end of file. */
cannam@85 2152 test_seek_or_die (file, -1 * (sfinfo.frames - 10), SEEK_END, 10, sfinfo.channels, __LINE__) ;
cannam@85 2153
cannam@85 2154 test_read_int_or_die (file, 0, test + 10, 4, __LINE__) ;
cannam@85 2155 for (k = 10 ; k < 14 ; k++)
cannam@85 2156 if (INT_ERROR (orig [k], test [k]))
cannam@85 2157 { printf ("\n\nLine %d : Mono : Incorrect sample D (#%d : 0x%X => 0x%X).\n", __LINE__, k, test [k], orig [k]) ;
cannam@85 2158 exit (1) ;
cannam@85 2159 } ;
cannam@85 2160
cannam@85 2161 /* Check read past end of file followed by sf_seek (sndfile, 0, SEEK_CUR). */
cannam@85 2162 test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
cannam@85 2163
cannam@85 2164 count = 0 ;
cannam@85 2165 while (count < sfinfo.frames)
cannam@85 2166 count += sf_read_int (file, test, 311) ;
cannam@85 2167
cannam@85 2168 /* Check that no error has occurred. */
cannam@85 2169 if (sf_error (file))
cannam@85 2170 { printf ("\n\nLine %d : Mono : error where there shouldn't have been one.\n", __LINE__) ;
cannam@85 2171 puts (sf_strerror (file)) ;
cannam@85 2172 exit (1) ;
cannam@85 2173 } ;
cannam@85 2174
cannam@85 2175 /* Check that we haven't read beyond EOF. */
cannam@85 2176 if (count > sfinfo.frames)
cannam@85 2177 { printf ("\n\nLines %d : read past end of file (%ld should be %ld)\n", __LINE__, (long) count, (long) sfinfo.frames) ;
cannam@85 2178 exit (1) ;
cannam@85 2179 } ;
cannam@85 2180
cannam@85 2181 test_seek_or_die (file, 0, SEEK_CUR, sfinfo.frames, sfinfo.channels, __LINE__) ;
cannam@85 2182
cannam@85 2183 sf_close (file) ;
cannam@85 2184
cannam@85 2185 multi_seek_test (filename, format) ;
cannam@85 2186 write_seek_extend_test (filename, format) ;
cannam@85 2187
cannam@85 2188 } /* mono_int_test */
cannam@85 2189
cannam@85 2190 static void
cannam@85 2191 stereo_int_test (const char *filename, int format, int long_file_ok, int allow_fd)
cannam@85 2192 { SNDFILE *file ;
cannam@85 2193 SF_INFO sfinfo ;
cannam@85 2194 int *orig, *test ;
cannam@85 2195 int k, items, frames ;
cannam@85 2196
cannam@85 2197 sfinfo.samplerate = 44100 ;
cannam@85 2198 sfinfo.frames = SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */
cannam@85 2199 sfinfo.channels = 2 ;
cannam@85 2200 sfinfo.format = format ;
cannam@85 2201
cannam@85 2202 gen_windowed_sine_double (orig_data.d, DATA_LENGTH, (1.0 * 0x7F000000)) ;
cannam@85 2203
cannam@85 2204 orig = orig_data.i ;
cannam@85 2205 test = test_data.i ;
cannam@85 2206
cannam@85 2207 /* Make this a macro so gdb steps over it in one go. */
cannam@85 2208 CONVERT_DATA (k, DATA_LENGTH, orig, orig_data.d) ;
cannam@85 2209
cannam@85 2210 items = DATA_LENGTH ;
cannam@85 2211 frames = items / sfinfo.channels ;
cannam@85 2212
cannam@85 2213 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, allow_fd, __LINE__) ;
cannam@85 2214
cannam@85 2215 sf_set_string (file, SF_STR_ARTIST, "Your name here") ;
cannam@85 2216
cannam@85 2217 test_writef_int_or_die (file, 0, orig, frames, __LINE__) ;
cannam@85 2218
cannam@85 2219 sf_set_string (file, SF_STR_COPYRIGHT, "Copyright (c) 2003") ;
cannam@85 2220
cannam@85 2221 sf_close (file) ;
cannam@85 2222
cannam@85 2223 memset (test, 0, items * sizeof (int)) ;
cannam@85 2224
cannam@85 2225 if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_RAW)
cannam@85 2226 memset (&sfinfo, 0, sizeof (sfinfo)) ;
cannam@85 2227
cannam@85 2228 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, allow_fd, __LINE__) ;
cannam@85 2229
cannam@85 2230 if (sfinfo.format != format)
cannam@85 2231 { printf ("\n\nLine %d : Stereo : Returned format incorrect (0x%08X => 0x%08X).\n",
cannam@85 2232 __LINE__, format, sfinfo.format) ;
cannam@85 2233 exit (1) ;
cannam@85 2234 } ;
cannam@85 2235
cannam@85 2236 if (sfinfo.frames < frames)
cannam@85 2237 { printf ("\n\nLine %d : Stereo : Incorrect number of frames in file (too short). (%ld should be %d)\n",
cannam@85 2238 __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), frames) ;
cannam@85 2239 exit (1) ;
cannam@85 2240 } ;
cannam@85 2241
cannam@85 2242 if (! long_file_ok && sfinfo.frames > frames)
cannam@85 2243 { printf ("\n\nLine %d : Stereo : Incorrect number of frames in file (too long). (%ld should be %d)\n",
cannam@85 2244 __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), frames) ;
cannam@85 2245 exit (1) ;
cannam@85 2246 } ;
cannam@85 2247
cannam@85 2248 if (sfinfo.channels != 2)
cannam@85 2249 { printf ("\n\nLine %d : Stereo : Incorrect number of channels in file.\n", __LINE__) ;
cannam@85 2250 exit (1) ;
cannam@85 2251 } ;
cannam@85 2252
cannam@85 2253 check_log_buffer_or_die (file, __LINE__) ;
cannam@85 2254
cannam@85 2255 test_readf_int_or_die (file, 0, test, frames, __LINE__) ;
cannam@85 2256 for (k = 0 ; k < items ; k++)
cannam@85 2257 if (INT_ERROR (test [k], orig [k]))
cannam@85 2258 { printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
cannam@85 2259 exit (1) ;
cannam@85 2260 } ;
cannam@85 2261
cannam@85 2262 /* Seek to start of file. */
cannam@85 2263 test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
cannam@85 2264
cannam@85 2265 test_readf_int_or_die (file, 0, test, 2, __LINE__) ;
cannam@85 2266 for (k = 0 ; k < 4 ; k++)
cannam@85 2267 if (INT_ERROR (test [k], orig [k]))
cannam@85 2268 { printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
cannam@85 2269 exit (1) ;
cannam@85 2270 } ;
cannam@85 2271
cannam@85 2272 /* Seek to offset from start of file. */
cannam@85 2273 test_seek_or_die (file, 10, SEEK_SET, 10, sfinfo.channels, __LINE__) ;
cannam@85 2274
cannam@85 2275 /* Check for errors here. */
cannam@85 2276 if (sf_error (file))
cannam@85 2277 { printf ("Line %d: Should NOT return an error.\n", __LINE__) ;
cannam@85 2278 puts (sf_strerror (file)) ;
cannam@85 2279 exit (1) ;
cannam@85 2280 } ;
cannam@85 2281
cannam@85 2282 if (sf_read_int (file, test, 1) > 0)
cannam@85 2283 { printf ("Line %d: Should return 0.\n", __LINE__) ;
cannam@85 2284 exit (1) ;
cannam@85 2285 } ;
cannam@85 2286
cannam@85 2287 if (! sf_error (file))
cannam@85 2288 { printf ("Line %d: Should return an error.\n", __LINE__) ;
cannam@85 2289 exit (1) ;
cannam@85 2290 } ;
cannam@85 2291 /*-----------------------*/
cannam@85 2292
cannam@85 2293 test_readf_int_or_die (file, 0, test + 10, 2, __LINE__) ;
cannam@85 2294 for (k = 20 ; k < 24 ; k++)
cannam@85 2295 if (INT_ERROR (test [k], orig [k]))
cannam@85 2296 { printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
cannam@85 2297 exit (1) ;
cannam@85 2298 } ;
cannam@85 2299
cannam@85 2300 /* Seek to offset from current position. */
cannam@85 2301 test_seek_or_die (file, 8, SEEK_CUR, 20, sfinfo.channels, __LINE__) ;
cannam@85 2302
cannam@85 2303 test_readf_int_or_die (file, 0, test + 20, 2, __LINE__) ;
cannam@85 2304 for (k = 40 ; k < 44 ; k++)
cannam@85 2305 if (INT_ERROR (test [k], orig [k]))
cannam@85 2306 { printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
cannam@85 2307 exit (1) ;
cannam@85 2308 } ;
cannam@85 2309
cannam@85 2310 /* Seek to offset from end of file. */
cannam@85 2311 test_seek_or_die (file, -1 * (sfinfo.frames - 10), SEEK_END, 10, sfinfo.channels, __LINE__) ;
cannam@85 2312
cannam@85 2313 test_readf_int_or_die (file, 0, test + 20, 2, __LINE__) ;
cannam@85 2314 for (k = 20 ; k < 24 ; k++)
cannam@85 2315 if (INT_ERROR (test [k], orig [k]))
cannam@85 2316 { printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
cannam@85 2317 exit (1) ;
cannam@85 2318 } ;
cannam@85 2319
cannam@85 2320 sf_close (file) ;
cannam@85 2321 } /* stereo_int_test */
cannam@85 2322
cannam@85 2323 static void
cannam@85 2324 mono_rdwr_int_test (const char *filename, int format, int long_file_ok, int allow_fd)
cannam@85 2325 { SNDFILE *file ;
cannam@85 2326 SF_INFO sfinfo ;
cannam@85 2327 int *orig, *test ;
cannam@85 2328 int k, pass ;
cannam@85 2329
cannam@85 2330 orig = orig_data.i ;
cannam@85 2331 test = test_data.i ;
cannam@85 2332
cannam@85 2333 sfinfo.samplerate = SAMPLE_RATE ;
cannam@85 2334 sfinfo.frames = DATA_LENGTH ;
cannam@85 2335 sfinfo.channels = 1 ;
cannam@85 2336 sfinfo.format = format ;
cannam@85 2337
cannam@85 2338 if ((format & SF_FORMAT_TYPEMASK) == SF_FORMAT_RAW
cannam@85 2339 || (format & SF_FORMAT_TYPEMASK) == SF_FORMAT_AU
cannam@85 2340 || (format & SF_FORMAT_TYPEMASK) == SF_FORMAT_SD2)
cannam@85 2341 unlink (filename) ;
cannam@85 2342 else
cannam@85 2343 { /* Create a short file. */
cannam@85 2344 create_short_file (filename) ;
cannam@85 2345
cannam@85 2346 /* Opening a already existing short file (ie invalid header) RDWR is disallowed.
cannam@85 2347 ** If this returns a valif pointer sf_open() screwed up.
cannam@85 2348 */
cannam@85 2349 if ((file = sf_open (filename, SFM_RDWR, &sfinfo)))
cannam@85 2350 { printf ("\n\nLine %d: sf_open should (SFM_RDWR) have failed but didn't.\n", __LINE__) ;
cannam@85 2351 exit (1) ;
cannam@85 2352 } ;
cannam@85 2353
cannam@85 2354 /* Truncate the file to zero bytes. */
cannam@85 2355 if (truncate (filename, 0) < 0)
cannam@85 2356 { printf ("\n\nLine %d: truncate (%s) failed", __LINE__, filename) ;
cannam@85 2357 perror (NULL) ;
cannam@85 2358 exit (1) ;
cannam@85 2359 } ;
cannam@85 2360 } ;
cannam@85 2361
cannam@85 2362 /* Opening a zero length file RDWR is allowed, but the SF_INFO struct must contain
cannam@85 2363 ** all the usual data required when opening the file in WRITE mode.
cannam@85 2364 */
cannam@85 2365 sfinfo.samplerate = SAMPLE_RATE ;
cannam@85 2366 sfinfo.frames = DATA_LENGTH ;
cannam@85 2367 sfinfo.channels = 1 ;
cannam@85 2368 sfinfo.format = format ;
cannam@85 2369
cannam@85 2370 file = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, allow_fd, __LINE__) ;
cannam@85 2371
cannam@85 2372 /* Do 3 writes followed by reads. After each, check the data and the current
cannam@85 2373 ** read and write offsets.
cannam@85 2374 */
cannam@85 2375 for (pass = 1 ; pass <= 3 ; pass ++)
cannam@85 2376 { orig [20] = pass * 2 ;
cannam@85 2377
cannam@85 2378 /* Write some data. */
cannam@85 2379 test_write_int_or_die (file, pass, orig, DATA_LENGTH, __LINE__) ;
cannam@85 2380
cannam@85 2381 test_read_write_position_or_die (file, __LINE__, pass, (pass - 1) * DATA_LENGTH, pass * DATA_LENGTH) ;
cannam@85 2382
cannam@85 2383 /* Read what we just wrote. */
cannam@85 2384 test_read_int_or_die (file, 0, test, DATA_LENGTH, __LINE__) ;
cannam@85 2385
cannam@85 2386 /* Check the data. */
cannam@85 2387 for (k = 0 ; k < DATA_LENGTH ; k++)
cannam@85 2388 if (INT_ERROR (orig [k], test [k]))
cannam@85 2389 { printf ("\n\nLine %d (pass %d) A : Error at sample %d (0x%X => 0x%X).\n", __LINE__, pass, k, orig [k], test [k]) ;
cannam@85 2390 oct_save_int (orig, test, DATA_LENGTH) ;
cannam@85 2391 exit (1) ;
cannam@85 2392 } ;
cannam@85 2393
cannam@85 2394 test_read_write_position_or_die (file, __LINE__, pass, pass * DATA_LENGTH, pass * DATA_LENGTH) ;
cannam@85 2395 } ; /* for (pass ...) */
cannam@85 2396
cannam@85 2397 sf_close (file) ;
cannam@85 2398
cannam@85 2399 /* Open the file again to check the data. */
cannam@85 2400 file = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, allow_fd, __LINE__) ;
cannam@85 2401
cannam@85 2402 if (sfinfo.format != format)
cannam@85 2403 { printf ("\n\nLine %d : Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, format, sfinfo.format) ;
cannam@85 2404 exit (1) ;
cannam@85 2405 } ;
cannam@85 2406
cannam@85 2407 if (sfinfo.frames < 3 * DATA_LENGTH)
cannam@85 2408 { printf ("\n\nLine %d : Not enough frames in file. (%ld < %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), 3 * DATA_LENGTH ) ;
cannam@85 2409 exit (1) ;
cannam@85 2410 }
cannam@85 2411
cannam@85 2412 if (! long_file_ok && sfinfo.frames != 3 * DATA_LENGTH)
cannam@85 2413 { printf ("\n\nLine %d : Incorrect number of frames in file. (%ld should be %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), 3 * DATA_LENGTH ) ;
cannam@85 2414 exit (1) ;
cannam@85 2415 } ;
cannam@85 2416
cannam@85 2417 if (sfinfo.channels != 1)
cannam@85 2418 { printf ("\n\nLine %d : Incorrect number of channels in file.\n", __LINE__) ;
cannam@85 2419 exit (1) ;
cannam@85 2420 } ;
cannam@85 2421
cannam@85 2422 if (! long_file_ok)
cannam@85 2423 test_read_write_position_or_die (file, __LINE__, 0, 0, 3 * DATA_LENGTH) ;
cannam@85 2424 else
cannam@85 2425 test_seek_or_die (file, 3 * DATA_LENGTH, SFM_WRITE | SEEK_SET, 3 * DATA_LENGTH, sfinfo.channels, __LINE__) ;
cannam@85 2426
cannam@85 2427 for (pass = 1 ; pass <= 3 ; pass ++)
cannam@85 2428 { orig [20] = pass * 2 ;
cannam@85 2429
cannam@85 2430 test_read_write_position_or_die (file, __LINE__, pass, (pass - 1) * DATA_LENGTH, 3 * DATA_LENGTH) ;
cannam@85 2431
cannam@85 2432 /* Read what we just wrote. */
cannam@85 2433 test_read_int_or_die (file, pass, test, DATA_LENGTH, __LINE__) ;
cannam@85 2434
cannam@85 2435 /* Check the data. */
cannam@85 2436 for (k = 0 ; k < DATA_LENGTH ; k++)
cannam@85 2437 if (INT_ERROR (orig [k], test [k]))
cannam@85 2438 { printf ("\n\nLine %d (pass %d) B : Error at sample %d (0x%X => 0x%X).\n", __LINE__, pass, k, orig [k], test [k]) ;
cannam@85 2439 oct_save_int (orig, test, DATA_LENGTH) ;
cannam@85 2440 exit (1) ;
cannam@85 2441 } ;
cannam@85 2442
cannam@85 2443 } ; /* for (pass ...) */
cannam@85 2444
cannam@85 2445 sf_close (file) ;
cannam@85 2446 } /* mono_rdwr_int_test */
cannam@85 2447
cannam@85 2448 static void
cannam@85 2449 new_rdwr_int_test (const char *filename, int format, int allow_fd)
cannam@85 2450 { SNDFILE *wfile, *rwfile ;
cannam@85 2451 SF_INFO sfinfo ;
cannam@85 2452 int *orig, *test ;
cannam@85 2453 int items, frames ;
cannam@85 2454
cannam@85 2455 orig = orig_data.i ;
cannam@85 2456 test = test_data.i ;
cannam@85 2457
cannam@85 2458 sfinfo.samplerate = 44100 ;
cannam@85 2459 sfinfo.frames = SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */
cannam@85 2460 sfinfo.channels = 2 ;
cannam@85 2461 sfinfo.format = format ;
cannam@85 2462
cannam@85 2463 items = DATA_LENGTH ;
cannam@85 2464 frames = items / sfinfo.channels ;
cannam@85 2465
cannam@85 2466 wfile = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, allow_fd, __LINE__) ;
cannam@85 2467 sf_command (wfile, SFC_SET_UPDATE_HEADER_AUTO, NULL, SF_TRUE) ;
cannam@85 2468 test_writef_int_or_die (wfile, 1, orig, frames, __LINE__) ;
cannam@85 2469 sf_write_sync (wfile) ;
cannam@85 2470 test_writef_int_or_die (wfile, 2, orig, frames, __LINE__) ;
cannam@85 2471 sf_write_sync (wfile) ;
cannam@85 2472
cannam@85 2473 rwfile = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, allow_fd, __LINE__) ;
cannam@85 2474 if (sfinfo.frames != 2 * frames)
cannam@85 2475 { printf ("\n\nLine %d : incorrect number of frames in file (%ld should be %d)\n\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), 2 * frames) ;
cannam@85 2476 exit (1) ;
cannam@85 2477 } ;
cannam@85 2478
cannam@85 2479 test_writef_int_or_die (wfile, 3, orig, frames, __LINE__) ;
cannam@85 2480
cannam@85 2481 test_readf_int_or_die (rwfile, 1, test, frames, __LINE__) ;
cannam@85 2482 test_readf_int_or_die (rwfile, 2, test, frames, __LINE__) ;
cannam@85 2483
cannam@85 2484 sf_close (wfile) ;
cannam@85 2485 sf_close (rwfile) ;
cannam@85 2486 } /* new_rdwr_int_test */
cannam@85 2487
cannam@85 2488
cannam@85 2489 /*======================================================================================
cannam@85 2490 */
cannam@85 2491
cannam@85 2492 static void mono_float_test (const char *filename, int format, int long_file_ok, int allow_fd) ;
cannam@85 2493 static void stereo_float_test (const char *filename, int format, int long_file_ok, int allow_fd) ;
cannam@85 2494 static void mono_rdwr_float_test (const char *filename, int format, int long_file_ok, int allow_fd) ;
cannam@85 2495 static void new_rdwr_float_test (const char *filename, int format, int allow_fd) ;
cannam@85 2496 static void multi_seek_test (const char * filename, int format) ;
cannam@85 2497 static void write_seek_extend_test (const char * filename, int format) ;
cannam@85 2498
cannam@85 2499 static void
cannam@85 2500 pcm_test_float (const char *filename, int format, int long_file_ok)
cannam@85 2501 { SF_INFO sfinfo ;
cannam@85 2502 float *orig ;
cannam@85 2503 int k, allow_fd ;
cannam@85 2504
cannam@85 2505 /* Sd2 files cannot be opened from an existing file descriptor. */
cannam@85 2506 allow_fd = ((format & SF_FORMAT_TYPEMASK) == SF_FORMAT_SD2) ? SF_FALSE : SF_TRUE ;
cannam@85 2507
cannam@85 2508 print_test_name ("pcm_test_float", filename) ;
cannam@85 2509
cannam@85 2510 sfinfo.samplerate = 44100 ;
cannam@85 2511 sfinfo.frames = SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */
cannam@85 2512 sfinfo.channels = 1 ;
cannam@85 2513 sfinfo.format = format ;
cannam@85 2514
cannam@85 2515 gen_windowed_sine_double (orig_data.d, DATA_LENGTH, 1.0) ;
cannam@85 2516
cannam@85 2517 orig = orig_data.f ;
cannam@85 2518
cannam@85 2519 /* Make this a macro so gdb steps over it in one go. */
cannam@85 2520 CONVERT_DATA (k, DATA_LENGTH, orig, orig_data.d) ;
cannam@85 2521
cannam@85 2522 /* Some test broken out here. */
cannam@85 2523
cannam@85 2524 mono_float_test (filename, format, long_file_ok, allow_fd) ;
cannam@85 2525
cannam@85 2526 /* Sub format DWVW does not allow seeking. */
cannam@85 2527 if ((format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_16 ||
cannam@85 2528 (format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_24)
cannam@85 2529 { unlink (filename) ;
cannam@85 2530 printf ("no seek : ok\n") ;
cannam@85 2531 return ;
cannam@85 2532 } ;
cannam@85 2533
cannam@85 2534 if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_FLAC)
cannam@85 2535 mono_rdwr_float_test (filename, format, long_file_ok, allow_fd) ;
cannam@85 2536
cannam@85 2537 /* If the format doesn't support stereo we're done. */
cannam@85 2538 sfinfo.channels = 2 ;
cannam@85 2539 if (sf_format_check (&sfinfo) == 0)
cannam@85 2540 { unlink (filename) ;
cannam@85 2541 puts ("no stereo : ok") ;
cannam@85 2542 return ;
cannam@85 2543 } ;
cannam@85 2544
cannam@85 2545 stereo_float_test (filename, format, long_file_ok, allow_fd) ;
cannam@85 2546
cannam@85 2547 /* New read/write test. Not sure if this is needed yet. */
cannam@85 2548
cannam@85 2549 if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_PAF &&
cannam@85 2550 (format & SF_FORMAT_TYPEMASK) != SF_FORMAT_VOC &&
cannam@85 2551 (format & SF_FORMAT_TYPEMASK) != SF_FORMAT_FLAC)
cannam@85 2552 new_rdwr_float_test (filename, format, allow_fd) ;
cannam@85 2553
cannam@85 2554 delete_file (format, filename) ;
cannam@85 2555
cannam@85 2556 puts ("ok") ;
cannam@85 2557 return ;
cannam@85 2558 } /* pcm_test_float */
cannam@85 2559
cannam@85 2560 static void
cannam@85 2561 mono_float_test (const char *filename, int format, int long_file_ok, int allow_fd)
cannam@85 2562 { SNDFILE *file ;
cannam@85 2563 SF_INFO sfinfo ;
cannam@85 2564 float *orig, *test ;
cannam@85 2565 sf_count_t count ;
cannam@85 2566 int k, items ;
cannam@85 2567
cannam@85 2568 sfinfo.samplerate = 44100 ;
cannam@85 2569 sfinfo.frames = SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */
cannam@85 2570 sfinfo.channels = 1 ;
cannam@85 2571 sfinfo.format = format ;
cannam@85 2572
cannam@85 2573 orig = orig_data.f ;
cannam@85 2574 test = test_data.f ;
cannam@85 2575
cannam@85 2576 items = DATA_LENGTH ;
cannam@85 2577
cannam@85 2578 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, allow_fd, __LINE__) ;
cannam@85 2579
cannam@85 2580 sf_set_string (file, SF_STR_ARTIST, "Your name here") ;
cannam@85 2581
cannam@85 2582 test_write_float_or_die (file, 0, orig, items, __LINE__) ;
cannam@85 2583 sf_write_sync (file) ;
cannam@85 2584 test_write_float_or_die (file, 0, orig, items, __LINE__) ;
cannam@85 2585 sf_write_sync (file) ;
cannam@85 2586
cannam@85 2587 /* Add non-audio data after the audio. */
cannam@85 2588 sf_set_string (file, SF_STR_COPYRIGHT, "Copyright (c) 2003") ;
cannam@85 2589
cannam@85 2590 sf_close (file) ;
cannam@85 2591
cannam@85 2592 memset (test, 0, items * sizeof (float)) ;
cannam@85 2593
cannam@85 2594 if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_RAW)
cannam@85 2595 memset (&sfinfo, 0, sizeof (sfinfo)) ;
cannam@85 2596
cannam@85 2597 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, allow_fd, __LINE__) ;
cannam@85 2598
cannam@85 2599 if (sfinfo.format != format)
cannam@85 2600 { printf ("\n\nLine %d : Mono : Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, format, sfinfo.format) ;
cannam@85 2601 exit (1) ;
cannam@85 2602 } ;
cannam@85 2603
cannam@85 2604 if (sfinfo.frames < 2 * items)
cannam@85 2605 { printf ("\n\nLine %d : Mono : Incorrect number of frames in file (too short). (%ld should be %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), items) ;
cannam@85 2606 exit (1) ;
cannam@85 2607 } ;
cannam@85 2608
cannam@85 2609 if (! long_file_ok && sfinfo.frames > 2 * items)
cannam@85 2610 { printf ("\n\nLine %d : Mono : Incorrect number of frames in file (too long). (%ld should be %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), items) ;
cannam@85 2611 exit (1) ;
cannam@85 2612 } ;
cannam@85 2613
cannam@85 2614 if (sfinfo.channels != 1)
cannam@85 2615 { printf ("\n\nLine %d : Mono : Incorrect number of channels in file.\n", __LINE__) ;
cannam@85 2616 exit (1) ;
cannam@85 2617 } ;
cannam@85 2618
cannam@85 2619 check_log_buffer_or_die (file, __LINE__) ;
cannam@85 2620
cannam@85 2621 test_read_float_or_die (file, 0, test, items, __LINE__) ;
cannam@85 2622 for (k = 0 ; k < items ; k++)
cannam@85 2623 if (FLOAT_ERROR (orig [k], test [k]))
cannam@85 2624 { printf ("\n\nLine %d: Mono : Incorrect sample A (#%d : %g => %g).\n", __LINE__, k, orig [k], test [k]) ;
cannam@85 2625 oct_save_float (orig, test, items) ;
cannam@85 2626 exit (1) ;
cannam@85 2627 } ;
cannam@85 2628
cannam@85 2629 /* Seek to start of file. */
cannam@85 2630 test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
cannam@85 2631
cannam@85 2632 test_read_float_or_die (file, 0, test, 4, __LINE__) ;
cannam@85 2633 for (k = 0 ; k < 4 ; k++)
cannam@85 2634 if (FLOAT_ERROR (orig [k], test [k]))
cannam@85 2635 { printf ("\n\nLine %d : Mono : Incorrect sample A (#%d : %g => %g).\n", __LINE__, k, orig [k], test [k]) ;
cannam@85 2636 exit (1) ;
cannam@85 2637 } ;
cannam@85 2638
cannam@85 2639 if ((format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_16 ||
cannam@85 2640 (format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_24)
cannam@85 2641 { sf_close (file) ;
cannam@85 2642 unlink (filename) ;
cannam@85 2643 printf ("no seek : ") ;
cannam@85 2644 return ;
cannam@85 2645 } ;
cannam@85 2646
cannam@85 2647 /* Seek to offset from start of file. */
cannam@85 2648 test_seek_or_die (file, items + 10, SEEK_SET, items + 10, sfinfo.channels, __LINE__) ;
cannam@85 2649
cannam@85 2650 test_read_float_or_die (file, 0, test + 10, 4, __LINE__) ;
cannam@85 2651 for (k = 10 ; k < 14 ; k++)
cannam@85 2652 if (FLOAT_ERROR (orig [k], test [k]))
cannam@85 2653 { printf ("\n\nLine %d : Mono : Incorrect sample A (#%d : %g => %g).\n", __LINE__, k, test [k], orig [k]) ;
cannam@85 2654 exit (1) ;
cannam@85 2655 } ;
cannam@85 2656
cannam@85 2657 /* Seek to offset from current position. */
cannam@85 2658 test_seek_or_die (file, 6, SEEK_CUR, items + 20, sfinfo.channels, __LINE__) ;
cannam@85 2659
cannam@85 2660 test_read_float_or_die (file, 0, test + 20, 4, __LINE__) ;
cannam@85 2661 for (k = 20 ; k < 24 ; k++)
cannam@85 2662 if (FLOAT_ERROR (orig [k], test [k]))
cannam@85 2663 { printf ("\n\nLine %d : Mono : Incorrect sample A (#%d : %g => %g).\n", __LINE__, k, test [k], orig [k]) ;
cannam@85 2664 exit (1) ;
cannam@85 2665 } ;
cannam@85 2666
cannam@85 2667 /* Seek to offset from end of file. */
cannam@85 2668 test_seek_or_die (file, -1 * (sfinfo.frames - 10), SEEK_END, 10, sfinfo.channels, __LINE__) ;
cannam@85 2669
cannam@85 2670 test_read_float_or_die (file, 0, test + 10, 4, __LINE__) ;
cannam@85 2671 for (k = 10 ; k < 14 ; k++)
cannam@85 2672 if (FLOAT_ERROR (orig [k], test [k]))
cannam@85 2673 { printf ("\n\nLine %d : Mono : Incorrect sample D (#%d : %g => %g).\n", __LINE__, k, test [k], orig [k]) ;
cannam@85 2674 exit (1) ;
cannam@85 2675 } ;
cannam@85 2676
cannam@85 2677 /* Check read past end of file followed by sf_seek (sndfile, 0, SEEK_CUR). */
cannam@85 2678 test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
cannam@85 2679
cannam@85 2680 count = 0 ;
cannam@85 2681 while (count < sfinfo.frames)
cannam@85 2682 count += sf_read_float (file, test, 311) ;
cannam@85 2683
cannam@85 2684 /* Check that no error has occurred. */
cannam@85 2685 if (sf_error (file))
cannam@85 2686 { printf ("\n\nLine %d : Mono : error where there shouldn't have been one.\n", __LINE__) ;
cannam@85 2687 puts (sf_strerror (file)) ;
cannam@85 2688 exit (1) ;
cannam@85 2689 } ;
cannam@85 2690
cannam@85 2691 /* Check that we haven't read beyond EOF. */
cannam@85 2692 if (count > sfinfo.frames)
cannam@85 2693 { printf ("\n\nLines %d : read past end of file (%ld should be %ld)\n", __LINE__, (long) count, (long) sfinfo.frames) ;
cannam@85 2694 exit (1) ;
cannam@85 2695 } ;
cannam@85 2696
cannam@85 2697 test_seek_or_die (file, 0, SEEK_CUR, sfinfo.frames, sfinfo.channels, __LINE__) ;
cannam@85 2698
cannam@85 2699 sf_close (file) ;
cannam@85 2700
cannam@85 2701 multi_seek_test (filename, format) ;
cannam@85 2702 write_seek_extend_test (filename, format) ;
cannam@85 2703
cannam@85 2704 } /* mono_float_test */
cannam@85 2705
cannam@85 2706 static void
cannam@85 2707 stereo_float_test (const char *filename, int format, int long_file_ok, int allow_fd)
cannam@85 2708 { SNDFILE *file ;
cannam@85 2709 SF_INFO sfinfo ;
cannam@85 2710 float *orig, *test ;
cannam@85 2711 int k, items, frames ;
cannam@85 2712
cannam@85 2713 sfinfo.samplerate = 44100 ;
cannam@85 2714 sfinfo.frames = SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */
cannam@85 2715 sfinfo.channels = 2 ;
cannam@85 2716 sfinfo.format = format ;
cannam@85 2717
cannam@85 2718 gen_windowed_sine_double (orig_data.d, DATA_LENGTH, 1.0) ;
cannam@85 2719
cannam@85 2720 orig = orig_data.f ;
cannam@85 2721 test = test_data.f ;
cannam@85 2722
cannam@85 2723 /* Make this a macro so gdb steps over it in one go. */
cannam@85 2724 CONVERT_DATA (k, DATA_LENGTH, orig, orig_data.d) ;
cannam@85 2725
cannam@85 2726 items = DATA_LENGTH ;
cannam@85 2727 frames = items / sfinfo.channels ;
cannam@85 2728
cannam@85 2729 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, allow_fd, __LINE__) ;
cannam@85 2730
cannam@85 2731 sf_set_string (file, SF_STR_ARTIST, "Your name here") ;
cannam@85 2732
cannam@85 2733 test_writef_float_or_die (file, 0, orig, frames, __LINE__) ;
cannam@85 2734
cannam@85 2735 sf_set_string (file, SF_STR_COPYRIGHT, "Copyright (c) 2003") ;
cannam@85 2736
cannam@85 2737 sf_close (file) ;
cannam@85 2738
cannam@85 2739 memset (test, 0, items * sizeof (float)) ;
cannam@85 2740
cannam@85 2741 if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_RAW)
cannam@85 2742 memset (&sfinfo, 0, sizeof (sfinfo)) ;
cannam@85 2743
cannam@85 2744 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, allow_fd, __LINE__) ;
cannam@85 2745
cannam@85 2746 if (sfinfo.format != format)
cannam@85 2747 { printf ("\n\nLine %d : Stereo : Returned format incorrect (0x%08X => 0x%08X).\n",
cannam@85 2748 __LINE__, format, sfinfo.format) ;
cannam@85 2749 exit (1) ;
cannam@85 2750 } ;
cannam@85 2751
cannam@85 2752 if (sfinfo.frames < frames)
cannam@85 2753 { printf ("\n\nLine %d : Stereo : Incorrect number of frames in file (too short). (%ld should be %d)\n",
cannam@85 2754 __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), frames) ;
cannam@85 2755 exit (1) ;
cannam@85 2756 } ;
cannam@85 2757
cannam@85 2758 if (! long_file_ok && sfinfo.frames > frames)
cannam@85 2759 { printf ("\n\nLine %d : Stereo : Incorrect number of frames in file (too long). (%ld should be %d)\n",
cannam@85 2760 __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), frames) ;
cannam@85 2761 exit (1) ;
cannam@85 2762 } ;
cannam@85 2763
cannam@85 2764 if (sfinfo.channels != 2)
cannam@85 2765 { printf ("\n\nLine %d : Stereo : Incorrect number of channels in file.\n", __LINE__) ;
cannam@85 2766 exit (1) ;
cannam@85 2767 } ;
cannam@85 2768
cannam@85 2769 check_log_buffer_or_die (file, __LINE__) ;
cannam@85 2770
cannam@85 2771 test_readf_float_or_die (file, 0, test, frames, __LINE__) ;
cannam@85 2772 for (k = 0 ; k < items ; k++)
cannam@85 2773 if (FLOAT_ERROR (test [k], orig [k]))
cannam@85 2774 { printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : %g => %g).\n", __LINE__, k, orig [k], test [k]) ;
cannam@85 2775 exit (1) ;
cannam@85 2776 } ;
cannam@85 2777
cannam@85 2778 /* Seek to start of file. */
cannam@85 2779 test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
cannam@85 2780
cannam@85 2781 test_readf_float_or_die (file, 0, test, 2, __LINE__) ;
cannam@85 2782 for (k = 0 ; k < 4 ; k++)
cannam@85 2783 if (FLOAT_ERROR (test [k], orig [k]))
cannam@85 2784 { printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : %g => %g).\n", __LINE__, k, orig [k], test [k]) ;
cannam@85 2785 exit (1) ;
cannam@85 2786 } ;
cannam@85 2787
cannam@85 2788 /* Seek to offset from start of file. */
cannam@85 2789 test_seek_or_die (file, 10, SEEK_SET, 10, sfinfo.channels, __LINE__) ;
cannam@85 2790
cannam@85 2791 /* Check for errors here. */
cannam@85 2792 if (sf_error (file))
cannam@85 2793 { printf ("Line %d: Should NOT return an error.\n", __LINE__) ;
cannam@85 2794 puts (sf_strerror (file)) ;
cannam@85 2795 exit (1) ;
cannam@85 2796 } ;
cannam@85 2797
cannam@85 2798 if (sf_read_float (file, test, 1) > 0)
cannam@85 2799 { printf ("Line %d: Should return 0.\n", __LINE__) ;
cannam@85 2800 exit (1) ;
cannam@85 2801 } ;
cannam@85 2802
cannam@85 2803 if (! sf_error (file))
cannam@85 2804 { printf ("Line %d: Should return an error.\n", __LINE__) ;
cannam@85 2805 exit (1) ;
cannam@85 2806 } ;
cannam@85 2807 /*-----------------------*/
cannam@85 2808
cannam@85 2809 test_readf_float_or_die (file, 0, test + 10, 2, __LINE__) ;
cannam@85 2810 for (k = 20 ; k < 24 ; k++)
cannam@85 2811 if (FLOAT_ERROR (test [k], orig [k]))
cannam@85 2812 { printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : %g => %g).\n", __LINE__, k, orig [k], test [k]) ;
cannam@85 2813 exit (1) ;
cannam@85 2814 } ;
cannam@85 2815
cannam@85 2816 /* Seek to offset from current position. */
cannam@85 2817 test_seek_or_die (file, 8, SEEK_CUR, 20, sfinfo.channels, __LINE__) ;
cannam@85 2818
cannam@85 2819 test_readf_float_or_die (file, 0, test + 20, 2, __LINE__) ;
cannam@85 2820 for (k = 40 ; k < 44 ; k++)
cannam@85 2821 if (FLOAT_ERROR (test [k], orig [k]))
cannam@85 2822 { printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : %g => %g).\n", __LINE__, k, orig [k], test [k]) ;
cannam@85 2823 exit (1) ;
cannam@85 2824 } ;
cannam@85 2825
cannam@85 2826 /* Seek to offset from end of file. */
cannam@85 2827 test_seek_or_die (file, -1 * (sfinfo.frames - 10), SEEK_END, 10, sfinfo.channels, __LINE__) ;
cannam@85 2828
cannam@85 2829 test_readf_float_or_die (file, 0, test + 20, 2, __LINE__) ;
cannam@85 2830 for (k = 20 ; k < 24 ; k++)
cannam@85 2831 if (FLOAT_ERROR (test [k], orig [k]))
cannam@85 2832 { printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : %g => %g).\n", __LINE__, k, orig [k], test [k]) ;
cannam@85 2833 exit (1) ;
cannam@85 2834 } ;
cannam@85 2835
cannam@85 2836 sf_close (file) ;
cannam@85 2837 } /* stereo_float_test */
cannam@85 2838
cannam@85 2839 static void
cannam@85 2840 mono_rdwr_float_test (const char *filename, int format, int long_file_ok, int allow_fd)
cannam@85 2841 { SNDFILE *file ;
cannam@85 2842 SF_INFO sfinfo ;
cannam@85 2843 float *orig, *test ;
cannam@85 2844 int k, pass ;
cannam@85 2845
cannam@85 2846 orig = orig_data.f ;
cannam@85 2847 test = test_data.f ;
cannam@85 2848
cannam@85 2849 sfinfo.samplerate = SAMPLE_RATE ;
cannam@85 2850 sfinfo.frames = DATA_LENGTH ;
cannam@85 2851 sfinfo.channels = 1 ;
cannam@85 2852 sfinfo.format = format ;
cannam@85 2853
cannam@85 2854 if ((format & SF_FORMAT_TYPEMASK) == SF_FORMAT_RAW
cannam@85 2855 || (format & SF_FORMAT_TYPEMASK) == SF_FORMAT_AU
cannam@85 2856 || (format & SF_FORMAT_TYPEMASK) == SF_FORMAT_SD2)
cannam@85 2857 unlink (filename) ;
cannam@85 2858 else
cannam@85 2859 { /* Create a short file. */
cannam@85 2860 create_short_file (filename) ;
cannam@85 2861
cannam@85 2862 /* Opening a already existing short file (ie invalid header) RDWR is disallowed.
cannam@85 2863 ** If this returns a valif pointer sf_open() screwed up.
cannam@85 2864 */
cannam@85 2865 if ((file = sf_open (filename, SFM_RDWR, &sfinfo)))
cannam@85 2866 { printf ("\n\nLine %d: sf_open should (SFM_RDWR) have failed but didn't.\n", __LINE__) ;
cannam@85 2867 exit (1) ;
cannam@85 2868 } ;
cannam@85 2869
cannam@85 2870 /* Truncate the file to zero bytes. */
cannam@85 2871 if (truncate (filename, 0) < 0)
cannam@85 2872 { printf ("\n\nLine %d: truncate (%s) failed", __LINE__, filename) ;
cannam@85 2873 perror (NULL) ;
cannam@85 2874 exit (1) ;
cannam@85 2875 } ;
cannam@85 2876 } ;
cannam@85 2877
cannam@85 2878 /* Opening a zero length file RDWR is allowed, but the SF_INFO struct must contain
cannam@85 2879 ** all the usual data required when opening the file in WRITE mode.
cannam@85 2880 */
cannam@85 2881 sfinfo.samplerate = SAMPLE_RATE ;
cannam@85 2882 sfinfo.frames = DATA_LENGTH ;
cannam@85 2883 sfinfo.channels = 1 ;
cannam@85 2884 sfinfo.format = format ;
cannam@85 2885
cannam@85 2886 file = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, allow_fd, __LINE__) ;
cannam@85 2887
cannam@85 2888 /* Do 3 writes followed by reads. After each, check the data and the current
cannam@85 2889 ** read and write offsets.
cannam@85 2890 */
cannam@85 2891 for (pass = 1 ; pass <= 3 ; pass ++)
cannam@85 2892 { orig [20] = pass * 2 ;
cannam@85 2893
cannam@85 2894 /* Write some data. */
cannam@85 2895 test_write_float_or_die (file, pass, orig, DATA_LENGTH, __LINE__) ;
cannam@85 2896
cannam@85 2897 test_read_write_position_or_die (file, __LINE__, pass, (pass - 1) * DATA_LENGTH, pass * DATA_LENGTH) ;
cannam@85 2898
cannam@85 2899 /* Read what we just wrote. */
cannam@85 2900 test_read_float_or_die (file, 0, test, DATA_LENGTH, __LINE__) ;
cannam@85 2901
cannam@85 2902 /* Check the data. */
cannam@85 2903 for (k = 0 ; k < DATA_LENGTH ; k++)
cannam@85 2904 if (FLOAT_ERROR (orig [k], test [k]))
cannam@85 2905 { printf ("\n\nLine %d (pass %d) A : Error at sample %d (%g => %g).\n", __LINE__, pass, k, orig [k], test [k]) ;
cannam@85 2906 oct_save_float (orig, test, DATA_LENGTH) ;
cannam@85 2907 exit (1) ;
cannam@85 2908 } ;
cannam@85 2909
cannam@85 2910 test_read_write_position_or_die (file, __LINE__, pass, pass * DATA_LENGTH, pass * DATA_LENGTH) ;
cannam@85 2911 } ; /* for (pass ...) */
cannam@85 2912
cannam@85 2913 sf_close (file) ;
cannam@85 2914
cannam@85 2915 /* Open the file again to check the data. */
cannam@85 2916 file = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, allow_fd, __LINE__) ;
cannam@85 2917
cannam@85 2918 if (sfinfo.format != format)
cannam@85 2919 { printf ("\n\nLine %d : Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, format, sfinfo.format) ;
cannam@85 2920 exit (1) ;
cannam@85 2921 } ;
cannam@85 2922
cannam@85 2923 if (sfinfo.frames < 3 * DATA_LENGTH)
cannam@85 2924 { printf ("\n\nLine %d : Not enough frames in file. (%ld < %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), 3 * DATA_LENGTH ) ;
cannam@85 2925 exit (1) ;
cannam@85 2926 }
cannam@85 2927
cannam@85 2928 if (! long_file_ok && sfinfo.frames != 3 * DATA_LENGTH)
cannam@85 2929 { printf ("\n\nLine %d : Incorrect number of frames in file. (%ld should be %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), 3 * DATA_LENGTH ) ;
cannam@85 2930 exit (1) ;
cannam@85 2931 } ;
cannam@85 2932
cannam@85 2933 if (sfinfo.channels != 1)
cannam@85 2934 { printf ("\n\nLine %d : Incorrect number of channels in file.\n", __LINE__) ;
cannam@85 2935 exit (1) ;
cannam@85 2936 } ;
cannam@85 2937
cannam@85 2938 if (! long_file_ok)
cannam@85 2939 test_read_write_position_or_die (file, __LINE__, 0, 0, 3 * DATA_LENGTH) ;
cannam@85 2940 else
cannam@85 2941 test_seek_or_die (file, 3 * DATA_LENGTH, SFM_WRITE | SEEK_SET, 3 * DATA_LENGTH, sfinfo.channels, __LINE__) ;
cannam@85 2942
cannam@85 2943 for (pass = 1 ; pass <= 3 ; pass ++)
cannam@85 2944 { orig [20] = pass * 2 ;
cannam@85 2945
cannam@85 2946 test_read_write_position_or_die (file, __LINE__, pass, (pass - 1) * DATA_LENGTH, 3 * DATA_LENGTH) ;
cannam@85 2947
cannam@85 2948 /* Read what we just wrote. */
cannam@85 2949 test_read_float_or_die (file, pass, test, DATA_LENGTH, __LINE__) ;
cannam@85 2950
cannam@85 2951 /* Check the data. */
cannam@85 2952 for (k = 0 ; k < DATA_LENGTH ; k++)
cannam@85 2953 if (FLOAT_ERROR (orig [k], test [k]))
cannam@85 2954 { printf ("\n\nLine %d (pass %d) B : Error at sample %d (%g => %g).\n", __LINE__, pass, k, orig [k], test [k]) ;
cannam@85 2955 oct_save_float (orig, test, DATA_LENGTH) ;
cannam@85 2956 exit (1) ;
cannam@85 2957 } ;
cannam@85 2958
cannam@85 2959 } ; /* for (pass ...) */
cannam@85 2960
cannam@85 2961 sf_close (file) ;
cannam@85 2962 } /* mono_rdwr_float_test */
cannam@85 2963
cannam@85 2964 static void
cannam@85 2965 new_rdwr_float_test (const char *filename, int format, int allow_fd)
cannam@85 2966 { SNDFILE *wfile, *rwfile ;
cannam@85 2967 SF_INFO sfinfo ;
cannam@85 2968 float *orig, *test ;
cannam@85 2969 int items, frames ;
cannam@85 2970
cannam@85 2971 orig = orig_data.f ;
cannam@85 2972 test = test_data.f ;
cannam@85 2973
cannam@85 2974 sfinfo.samplerate = 44100 ;
cannam@85 2975 sfinfo.frames = SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */
cannam@85 2976 sfinfo.channels = 2 ;
cannam@85 2977 sfinfo.format = format ;
cannam@85 2978
cannam@85 2979 items = DATA_LENGTH ;
cannam@85 2980 frames = items / sfinfo.channels ;
cannam@85 2981
cannam@85 2982 wfile = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, allow_fd, __LINE__) ;
cannam@85 2983 sf_command (wfile, SFC_SET_UPDATE_HEADER_AUTO, NULL, SF_TRUE) ;
cannam@85 2984 test_writef_float_or_die (wfile, 1, orig, frames, __LINE__) ;
cannam@85 2985 sf_write_sync (wfile) ;
cannam@85 2986 test_writef_float_or_die (wfile, 2, orig, frames, __LINE__) ;
cannam@85 2987 sf_write_sync (wfile) ;
cannam@85 2988
cannam@85 2989 rwfile = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, allow_fd, __LINE__) ;
cannam@85 2990 if (sfinfo.frames != 2 * frames)
cannam@85 2991 { printf ("\n\nLine %d : incorrect number of frames in file (%ld should be %d)\n\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), 2 * frames) ;
cannam@85 2992 exit (1) ;
cannam@85 2993 } ;
cannam@85 2994
cannam@85 2995 test_writef_float_or_die (wfile, 3, orig, frames, __LINE__) ;
cannam@85 2996
cannam@85 2997 test_readf_float_or_die (rwfile, 1, test, frames, __LINE__) ;
cannam@85 2998 test_readf_float_or_die (rwfile, 2, test, frames, __LINE__) ;
cannam@85 2999
cannam@85 3000 sf_close (wfile) ;
cannam@85 3001 sf_close (rwfile) ;
cannam@85 3002 } /* new_rdwr_float_test */
cannam@85 3003
cannam@85 3004
cannam@85 3005 /*======================================================================================
cannam@85 3006 */
cannam@85 3007
cannam@85 3008 static void mono_double_test (const char *filename, int format, int long_file_ok, int allow_fd) ;
cannam@85 3009 static void stereo_double_test (const char *filename, int format, int long_file_ok, int allow_fd) ;
cannam@85 3010 static void mono_rdwr_double_test (const char *filename, int format, int long_file_ok, int allow_fd) ;
cannam@85 3011 static void new_rdwr_double_test (const char *filename, int format, int allow_fd) ;
cannam@85 3012 static void multi_seek_test (const char * filename, int format) ;
cannam@85 3013 static void write_seek_extend_test (const char * filename, int format) ;
cannam@85 3014
cannam@85 3015 static void
cannam@85 3016 pcm_test_double (const char *filename, int format, int long_file_ok)
cannam@85 3017 { SF_INFO sfinfo ;
cannam@85 3018 double *orig ;
cannam@85 3019 int k, allow_fd ;
cannam@85 3020
cannam@85 3021 /* Sd2 files cannot be opened from an existing file descriptor. */
cannam@85 3022 allow_fd = ((format & SF_FORMAT_TYPEMASK) == SF_FORMAT_SD2) ? SF_FALSE : SF_TRUE ;
cannam@85 3023
cannam@85 3024 print_test_name ("pcm_test_double", filename) ;
cannam@85 3025
cannam@85 3026 sfinfo.samplerate = 44100 ;
cannam@85 3027 sfinfo.frames = SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */
cannam@85 3028 sfinfo.channels = 1 ;
cannam@85 3029 sfinfo.format = format ;
cannam@85 3030
cannam@85 3031 gen_windowed_sine_double (orig_data.d, DATA_LENGTH, 1.0) ;
cannam@85 3032
cannam@85 3033 orig = orig_data.d ;
cannam@85 3034
cannam@85 3035 /* Make this a macro so gdb steps over it in one go. */
cannam@85 3036 CONVERT_DATA (k, DATA_LENGTH, orig, orig_data.d) ;
cannam@85 3037
cannam@85 3038 /* Some test broken out here. */
cannam@85 3039
cannam@85 3040 mono_double_test (filename, format, long_file_ok, allow_fd) ;
cannam@85 3041
cannam@85 3042 /* Sub format DWVW does not allow seeking. */
cannam@85 3043 if ((format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_16 ||
cannam@85 3044 (format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_24)
cannam@85 3045 { unlink (filename) ;
cannam@85 3046 printf ("no seek : ok\n") ;
cannam@85 3047 return ;
cannam@85 3048 } ;
cannam@85 3049
cannam@85 3050 if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_FLAC)
cannam@85 3051 mono_rdwr_double_test (filename, format, long_file_ok, allow_fd) ;
cannam@85 3052
cannam@85 3053 /* If the format doesn't support stereo we're done. */
cannam@85 3054 sfinfo.channels = 2 ;
cannam@85 3055 if (sf_format_check (&sfinfo) == 0)
cannam@85 3056 { unlink (filename) ;
cannam@85 3057 puts ("no stereo : ok") ;
cannam@85 3058 return ;
cannam@85 3059 } ;
cannam@85 3060
cannam@85 3061 stereo_double_test (filename, format, long_file_ok, allow_fd) ;
cannam@85 3062
cannam@85 3063 /* New read/write test. Not sure if this is needed yet. */
cannam@85 3064
cannam@85 3065 if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_PAF &&
cannam@85 3066 (format & SF_FORMAT_TYPEMASK) != SF_FORMAT_VOC &&
cannam@85 3067 (format & SF_FORMAT_TYPEMASK) != SF_FORMAT_FLAC)
cannam@85 3068 new_rdwr_double_test (filename, format, allow_fd) ;
cannam@85 3069
cannam@85 3070 delete_file (format, filename) ;
cannam@85 3071
cannam@85 3072 puts ("ok") ;
cannam@85 3073 return ;
cannam@85 3074 } /* pcm_test_double */
cannam@85 3075
cannam@85 3076 static void
cannam@85 3077 mono_double_test (const char *filename, int format, int long_file_ok, int allow_fd)
cannam@85 3078 { SNDFILE *file ;
cannam@85 3079 SF_INFO sfinfo ;
cannam@85 3080 double *orig, *test ;
cannam@85 3081 sf_count_t count ;
cannam@85 3082 int k, items ;
cannam@85 3083
cannam@85 3084 sfinfo.samplerate = 44100 ;
cannam@85 3085 sfinfo.frames = SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */
cannam@85 3086 sfinfo.channels = 1 ;
cannam@85 3087 sfinfo.format = format ;
cannam@85 3088
cannam@85 3089 orig = orig_data.d ;
cannam@85 3090 test = test_data.d ;
cannam@85 3091
cannam@85 3092 items = DATA_LENGTH ;
cannam@85 3093
cannam@85 3094 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, allow_fd, __LINE__) ;
cannam@85 3095
cannam@85 3096 sf_set_string (file, SF_STR_ARTIST, "Your name here") ;
cannam@85 3097
cannam@85 3098 test_write_double_or_die (file, 0, orig, items, __LINE__) ;
cannam@85 3099 sf_write_sync (file) ;
cannam@85 3100 test_write_double_or_die (file, 0, orig, items, __LINE__) ;
cannam@85 3101 sf_write_sync (file) ;
cannam@85 3102
cannam@85 3103 /* Add non-audio data after the audio. */
cannam@85 3104 sf_set_string (file, SF_STR_COPYRIGHT, "Copyright (c) 2003") ;
cannam@85 3105
cannam@85 3106 sf_close (file) ;
cannam@85 3107
cannam@85 3108 memset (test, 0, items * sizeof (double)) ;
cannam@85 3109
cannam@85 3110 if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_RAW)
cannam@85 3111 memset (&sfinfo, 0, sizeof (sfinfo)) ;
cannam@85 3112
cannam@85 3113 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, allow_fd, __LINE__) ;
cannam@85 3114
cannam@85 3115 if (sfinfo.format != format)
cannam@85 3116 { printf ("\n\nLine %d : Mono : Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, format, sfinfo.format) ;
cannam@85 3117 exit (1) ;
cannam@85 3118 } ;
cannam@85 3119
cannam@85 3120 if (sfinfo.frames < 2 * items)
cannam@85 3121 { printf ("\n\nLine %d : Mono : Incorrect number of frames in file (too short). (%ld should be %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), items) ;
cannam@85 3122 exit (1) ;
cannam@85 3123 } ;
cannam@85 3124
cannam@85 3125 if (! long_file_ok && sfinfo.frames > 2 * items)
cannam@85 3126 { printf ("\n\nLine %d : Mono : Incorrect number of frames in file (too long). (%ld should be %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), items) ;
cannam@85 3127 exit (1) ;
cannam@85 3128 } ;
cannam@85 3129
cannam@85 3130 if (sfinfo.channels != 1)
cannam@85 3131 { printf ("\n\nLine %d : Mono : Incorrect number of channels in file.\n", __LINE__) ;
cannam@85 3132 exit (1) ;
cannam@85 3133 } ;
cannam@85 3134
cannam@85 3135 check_log_buffer_or_die (file, __LINE__) ;
cannam@85 3136
cannam@85 3137 test_read_double_or_die (file, 0, test, items, __LINE__) ;
cannam@85 3138 for (k = 0 ; k < items ; k++)
cannam@85 3139 if (FLOAT_ERROR (orig [k], test [k]))
cannam@85 3140 { printf ("\n\nLine %d: Mono : Incorrect sample A (#%d : %g => %g).\n", __LINE__, k, orig [k], test [k]) ;
cannam@85 3141 oct_save_double (orig, test, items) ;
cannam@85 3142 exit (1) ;
cannam@85 3143 } ;
cannam@85 3144
cannam@85 3145 /* Seek to start of file. */
cannam@85 3146 test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
cannam@85 3147
cannam@85 3148 test_read_double_or_die (file, 0, test, 4, __LINE__) ;
cannam@85 3149 for (k = 0 ; k < 4 ; k++)
cannam@85 3150 if (FLOAT_ERROR (orig [k], test [k]))
cannam@85 3151 { printf ("\n\nLine %d : Mono : Incorrect sample A (#%d : %g => %g).\n", __LINE__, k, orig [k], test [k]) ;
cannam@85 3152 exit (1) ;
cannam@85 3153 } ;
cannam@85 3154
cannam@85 3155 if ((format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_16 ||
cannam@85 3156 (format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_24)
cannam@85 3157 { sf_close (file) ;
cannam@85 3158 unlink (filename) ;
cannam@85 3159 printf ("no seek : ") ;
cannam@85 3160 return ;
cannam@85 3161 } ;
cannam@85 3162
cannam@85 3163 /* Seek to offset from start of file. */
cannam@85 3164 test_seek_or_die (file, items + 10, SEEK_SET, items + 10, sfinfo.channels, __LINE__) ;
cannam@85 3165
cannam@85 3166 test_read_double_or_die (file, 0, test + 10, 4, __LINE__) ;
cannam@85 3167 for (k = 10 ; k < 14 ; k++)
cannam@85 3168 if (FLOAT_ERROR (orig [k], test [k]))
cannam@85 3169 { printf ("\n\nLine %d : Mono : Incorrect sample A (#%d : %g => %g).\n", __LINE__, k, test [k], orig [k]) ;
cannam@85 3170 exit (1) ;
cannam@85 3171 } ;
cannam@85 3172
cannam@85 3173 /* Seek to offset from current position. */
cannam@85 3174 test_seek_or_die (file, 6, SEEK_CUR, items + 20, sfinfo.channels, __LINE__) ;
cannam@85 3175
cannam@85 3176 test_read_double_or_die (file, 0, test + 20, 4, __LINE__) ;
cannam@85 3177 for (k = 20 ; k < 24 ; k++)
cannam@85 3178 if (FLOAT_ERROR (orig [k], test [k]))
cannam@85 3179 { printf ("\n\nLine %d : Mono : Incorrect sample A (#%d : %g => %g).\n", __LINE__, k, test [k], orig [k]) ;
cannam@85 3180 exit (1) ;
cannam@85 3181 } ;
cannam@85 3182
cannam@85 3183 /* Seek to offset from end of file. */
cannam@85 3184 test_seek_or_die (file, -1 * (sfinfo.frames - 10), SEEK_END, 10, sfinfo.channels, __LINE__) ;
cannam@85 3185
cannam@85 3186 test_read_double_or_die (file, 0, test + 10, 4, __LINE__) ;
cannam@85 3187 for (k = 10 ; k < 14 ; k++)
cannam@85 3188 if (FLOAT_ERROR (orig [k], test [k]))
cannam@85 3189 { printf ("\n\nLine %d : Mono : Incorrect sample D (#%d : %g => %g).\n", __LINE__, k, test [k], orig [k]) ;
cannam@85 3190 exit (1) ;
cannam@85 3191 } ;
cannam@85 3192
cannam@85 3193 /* Check read past end of file followed by sf_seek (sndfile, 0, SEEK_CUR). */
cannam@85 3194 test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
cannam@85 3195
cannam@85 3196 count = 0 ;
cannam@85 3197 while (count < sfinfo.frames)
cannam@85 3198 count += sf_read_double (file, test, 311) ;
cannam@85 3199
cannam@85 3200 /* Check that no error has occurred. */
cannam@85 3201 if (sf_error (file))
cannam@85 3202 { printf ("\n\nLine %d : Mono : error where there shouldn't have been one.\n", __LINE__) ;
cannam@85 3203 puts (sf_strerror (file)) ;
cannam@85 3204 exit (1) ;
cannam@85 3205 } ;
cannam@85 3206
cannam@85 3207 /* Check that we haven't read beyond EOF. */
cannam@85 3208 if (count > sfinfo.frames)
cannam@85 3209 { printf ("\n\nLines %d : read past end of file (%ld should be %ld)\n", __LINE__, (long) count, (long) sfinfo.frames) ;
cannam@85 3210 exit (1) ;
cannam@85 3211 } ;
cannam@85 3212
cannam@85 3213 test_seek_or_die (file, 0, SEEK_CUR, sfinfo.frames, sfinfo.channels, __LINE__) ;
cannam@85 3214
cannam@85 3215 sf_close (file) ;
cannam@85 3216
cannam@85 3217 multi_seek_test (filename, format) ;
cannam@85 3218 write_seek_extend_test (filename, format) ;
cannam@85 3219
cannam@85 3220 } /* mono_double_test */
cannam@85 3221
cannam@85 3222 static void
cannam@85 3223 stereo_double_test (const char *filename, int format, int long_file_ok, int allow_fd)
cannam@85 3224 { SNDFILE *file ;
cannam@85 3225 SF_INFO sfinfo ;
cannam@85 3226 double *orig, *test ;
cannam@85 3227 int k, items, frames ;
cannam@85 3228
cannam@85 3229 sfinfo.samplerate = 44100 ;
cannam@85 3230 sfinfo.frames = SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */
cannam@85 3231 sfinfo.channels = 2 ;
cannam@85 3232 sfinfo.format = format ;
cannam@85 3233
cannam@85 3234 gen_windowed_sine_double (orig_data.d, DATA_LENGTH, 1.0) ;
cannam@85 3235
cannam@85 3236 orig = orig_data.d ;
cannam@85 3237 test = test_data.d ;
cannam@85 3238
cannam@85 3239 /* Make this a macro so gdb steps over it in one go. */
cannam@85 3240 CONVERT_DATA (k, DATA_LENGTH, orig, orig_data.d) ;
cannam@85 3241
cannam@85 3242 items = DATA_LENGTH ;
cannam@85 3243 frames = items / sfinfo.channels ;
cannam@85 3244
cannam@85 3245 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, allow_fd, __LINE__) ;
cannam@85 3246
cannam@85 3247 sf_set_string (file, SF_STR_ARTIST, "Your name here") ;
cannam@85 3248
cannam@85 3249 test_writef_double_or_die (file, 0, orig, frames, __LINE__) ;
cannam@85 3250
cannam@85 3251 sf_set_string (file, SF_STR_COPYRIGHT, "Copyright (c) 2003") ;
cannam@85 3252
cannam@85 3253 sf_close (file) ;
cannam@85 3254
cannam@85 3255 memset (test, 0, items * sizeof (double)) ;
cannam@85 3256
cannam@85 3257 if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_RAW)
cannam@85 3258 memset (&sfinfo, 0, sizeof (sfinfo)) ;
cannam@85 3259
cannam@85 3260 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, allow_fd, __LINE__) ;
cannam@85 3261
cannam@85 3262 if (sfinfo.format != format)
cannam@85 3263 { printf ("\n\nLine %d : Stereo : Returned format incorrect (0x%08X => 0x%08X).\n",
cannam@85 3264 __LINE__, format, sfinfo.format) ;
cannam@85 3265 exit (1) ;
cannam@85 3266 } ;
cannam@85 3267
cannam@85 3268 if (sfinfo.frames < frames)
cannam@85 3269 { printf ("\n\nLine %d : Stereo : Incorrect number of frames in file (too short). (%ld should be %d)\n",
cannam@85 3270 __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), frames) ;
cannam@85 3271 exit (1) ;
cannam@85 3272 } ;
cannam@85 3273
cannam@85 3274 if (! long_file_ok && sfinfo.frames > frames)
cannam@85 3275 { printf ("\n\nLine %d : Stereo : Incorrect number of frames in file (too long). (%ld should be %d)\n",
cannam@85 3276 __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), frames) ;
cannam@85 3277 exit (1) ;
cannam@85 3278 } ;
cannam@85 3279
cannam@85 3280 if (sfinfo.channels != 2)
cannam@85 3281 { printf ("\n\nLine %d : Stereo : Incorrect number of channels in file.\n", __LINE__) ;
cannam@85 3282 exit (1) ;
cannam@85 3283 } ;
cannam@85 3284
cannam@85 3285 check_log_buffer_or_die (file, __LINE__) ;
cannam@85 3286
cannam@85 3287 test_readf_double_or_die (file, 0, test, frames, __LINE__) ;
cannam@85 3288 for (k = 0 ; k < items ; k++)
cannam@85 3289 if (FLOAT_ERROR (test [k], orig [k]))
cannam@85 3290 { printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : %g => %g).\n", __LINE__, k, orig [k], test [k]) ;
cannam@85 3291 exit (1) ;
cannam@85 3292 } ;
cannam@85 3293
cannam@85 3294 /* Seek to start of file. */
cannam@85 3295 test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
cannam@85 3296
cannam@85 3297 test_readf_double_or_die (file, 0, test, 2, __LINE__) ;
cannam@85 3298 for (k = 0 ; k < 4 ; k++)
cannam@85 3299 if (FLOAT_ERROR (test [k], orig [k]))
cannam@85 3300 { printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : %g => %g).\n", __LINE__, k, orig [k], test [k]) ;
cannam@85 3301 exit (1) ;
cannam@85 3302 } ;
cannam@85 3303
cannam@85 3304 /* Seek to offset from start of file. */
cannam@85 3305 test_seek_or_die (file, 10, SEEK_SET, 10, sfinfo.channels, __LINE__) ;
cannam@85 3306
cannam@85 3307 /* Check for errors here. */
cannam@85 3308 if (sf_error (file))
cannam@85 3309 { printf ("Line %d: Should NOT return an error.\n", __LINE__) ;
cannam@85 3310 puts (sf_strerror (file)) ;
cannam@85 3311 exit (1) ;
cannam@85 3312 } ;
cannam@85 3313
cannam@85 3314 if (sf_read_double (file, test, 1) > 0)
cannam@85 3315 { printf ("Line %d: Should return 0.\n", __LINE__) ;
cannam@85 3316 exit (1) ;
cannam@85 3317 } ;
cannam@85 3318
cannam@85 3319 if (! sf_error (file))
cannam@85 3320 { printf ("Line %d: Should return an error.\n", __LINE__) ;
cannam@85 3321 exit (1) ;
cannam@85 3322 } ;
cannam@85 3323 /*-----------------------*/
cannam@85 3324
cannam@85 3325 test_readf_double_or_die (file, 0, test + 10, 2, __LINE__) ;
cannam@85 3326 for (k = 20 ; k < 24 ; k++)
cannam@85 3327 if (FLOAT_ERROR (test [k], orig [k]))
cannam@85 3328 { printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : %g => %g).\n", __LINE__, k, orig [k], test [k]) ;
cannam@85 3329 exit (1) ;
cannam@85 3330 } ;
cannam@85 3331
cannam@85 3332 /* Seek to offset from current position. */
cannam@85 3333 test_seek_or_die (file, 8, SEEK_CUR, 20, sfinfo.channels, __LINE__) ;
cannam@85 3334
cannam@85 3335 test_readf_double_or_die (file, 0, test + 20, 2, __LINE__) ;
cannam@85 3336 for (k = 40 ; k < 44 ; k++)
cannam@85 3337 if (FLOAT_ERROR (test [k], orig [k]))
cannam@85 3338 { printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : %g => %g).\n", __LINE__, k, orig [k], test [k]) ;
cannam@85 3339 exit (1) ;
cannam@85 3340 } ;
cannam@85 3341
cannam@85 3342 /* Seek to offset from end of file. */
cannam@85 3343 test_seek_or_die (file, -1 * (sfinfo.frames - 10), SEEK_END, 10, sfinfo.channels, __LINE__) ;
cannam@85 3344
cannam@85 3345 test_readf_double_or_die (file, 0, test + 20, 2, __LINE__) ;
cannam@85 3346 for (k = 20 ; k < 24 ; k++)
cannam@85 3347 if (FLOAT_ERROR (test [k], orig [k]))
cannam@85 3348 { printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : %g => %g).\n", __LINE__, k, orig [k], test [k]) ;
cannam@85 3349 exit (1) ;
cannam@85 3350 } ;
cannam@85 3351
cannam@85 3352 sf_close (file) ;
cannam@85 3353 } /* stereo_double_test */
cannam@85 3354
cannam@85 3355 static void
cannam@85 3356 mono_rdwr_double_test (const char *filename, int format, int long_file_ok, int allow_fd)
cannam@85 3357 { SNDFILE *file ;
cannam@85 3358 SF_INFO sfinfo ;
cannam@85 3359 double *orig, *test ;
cannam@85 3360 int k, pass ;
cannam@85 3361
cannam@85 3362 orig = orig_data.d ;
cannam@85 3363 test = test_data.d ;
cannam@85 3364
cannam@85 3365 sfinfo.samplerate = SAMPLE_RATE ;
cannam@85 3366 sfinfo.frames = DATA_LENGTH ;
cannam@85 3367 sfinfo.channels = 1 ;
cannam@85 3368 sfinfo.format = format ;
cannam@85 3369
cannam@85 3370 if ((format & SF_FORMAT_TYPEMASK) == SF_FORMAT_RAW
cannam@85 3371 || (format & SF_FORMAT_TYPEMASK) == SF_FORMAT_AU
cannam@85 3372 || (format & SF_FORMAT_TYPEMASK) == SF_FORMAT_SD2)
cannam@85 3373 unlink (filename) ;
cannam@85 3374 else
cannam@85 3375 { /* Create a short file. */
cannam@85 3376 create_short_file (filename) ;
cannam@85 3377
cannam@85 3378 /* Opening a already existing short file (ie invalid header) RDWR is disallowed.
cannam@85 3379 ** If this returns a valif pointer sf_open() screwed up.
cannam@85 3380 */
cannam@85 3381 if ((file = sf_open (filename, SFM_RDWR, &sfinfo)))
cannam@85 3382 { printf ("\n\nLine %d: sf_open should (SFM_RDWR) have failed but didn't.\n", __LINE__) ;
cannam@85 3383 exit (1) ;
cannam@85 3384 } ;
cannam@85 3385
cannam@85 3386 /* Truncate the file to zero bytes. */
cannam@85 3387 if (truncate (filename, 0) < 0)
cannam@85 3388 { printf ("\n\nLine %d: truncate (%s) failed", __LINE__, filename) ;
cannam@85 3389 perror (NULL) ;
cannam@85 3390 exit (1) ;
cannam@85 3391 } ;
cannam@85 3392 } ;
cannam@85 3393
cannam@85 3394 /* Opening a zero length file RDWR is allowed, but the SF_INFO struct must contain
cannam@85 3395 ** all the usual data required when opening the file in WRITE mode.
cannam@85 3396 */
cannam@85 3397 sfinfo.samplerate = SAMPLE_RATE ;
cannam@85 3398 sfinfo.frames = DATA_LENGTH ;
cannam@85 3399 sfinfo.channels = 1 ;
cannam@85 3400 sfinfo.format = format ;
cannam@85 3401
cannam@85 3402 file = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, allow_fd, __LINE__) ;
cannam@85 3403
cannam@85 3404 /* Do 3 writes followed by reads. After each, check the data and the current
cannam@85 3405 ** read and write offsets.
cannam@85 3406 */
cannam@85 3407 for (pass = 1 ; pass <= 3 ; pass ++)
cannam@85 3408 { orig [20] = pass * 2 ;
cannam@85 3409
cannam@85 3410 /* Write some data. */
cannam@85 3411 test_write_double_or_die (file, pass, orig, DATA_LENGTH, __LINE__) ;
cannam@85 3412
cannam@85 3413 test_read_write_position_or_die (file, __LINE__, pass, (pass - 1) * DATA_LENGTH, pass * DATA_LENGTH) ;
cannam@85 3414
cannam@85 3415 /* Read what we just wrote. */
cannam@85 3416 test_read_double_or_die (file, 0, test, DATA_LENGTH, __LINE__) ;
cannam@85 3417
cannam@85 3418 /* Check the data. */
cannam@85 3419 for (k = 0 ; k < DATA_LENGTH ; k++)
cannam@85 3420 if (FLOAT_ERROR (orig [k], test [k]))
cannam@85 3421 { printf ("\n\nLine %d (pass %d) A : Error at sample %d (%g => %g).\n", __LINE__, pass, k, orig [k], test [k]) ;
cannam@85 3422 oct_save_double (orig, test, DATA_LENGTH) ;
cannam@85 3423 exit (1) ;
cannam@85 3424 } ;
cannam@85 3425
cannam@85 3426 test_read_write_position_or_die (file, __LINE__, pass, pass * DATA_LENGTH, pass * DATA_LENGTH) ;
cannam@85 3427 } ; /* for (pass ...) */
cannam@85 3428
cannam@85 3429 sf_close (file) ;
cannam@85 3430
cannam@85 3431 /* Open the file again to check the data. */
cannam@85 3432 file = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, allow_fd, __LINE__) ;
cannam@85 3433
cannam@85 3434 if (sfinfo.format != format)
cannam@85 3435 { printf ("\n\nLine %d : Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, format, sfinfo.format) ;
cannam@85 3436 exit (1) ;
cannam@85 3437 } ;
cannam@85 3438
cannam@85 3439 if (sfinfo.frames < 3 * DATA_LENGTH)
cannam@85 3440 { printf ("\n\nLine %d : Not enough frames in file. (%ld < %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), 3 * DATA_LENGTH ) ;
cannam@85 3441 exit (1) ;
cannam@85 3442 }
cannam@85 3443
cannam@85 3444 if (! long_file_ok && sfinfo.frames != 3 * DATA_LENGTH)
cannam@85 3445 { printf ("\n\nLine %d : Incorrect number of frames in file. (%ld should be %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), 3 * DATA_LENGTH ) ;
cannam@85 3446 exit (1) ;
cannam@85 3447 } ;
cannam@85 3448
cannam@85 3449 if (sfinfo.channels != 1)
cannam@85 3450 { printf ("\n\nLine %d : Incorrect number of channels in file.\n", __LINE__) ;
cannam@85 3451 exit (1) ;
cannam@85 3452 } ;
cannam@85 3453
cannam@85 3454 if (! long_file_ok)
cannam@85 3455 test_read_write_position_or_die (file, __LINE__, 0, 0, 3 * DATA_LENGTH) ;
cannam@85 3456 else
cannam@85 3457 test_seek_or_die (file, 3 * DATA_LENGTH, SFM_WRITE | SEEK_SET, 3 * DATA_LENGTH, sfinfo.channels, __LINE__) ;
cannam@85 3458
cannam@85 3459 for (pass = 1 ; pass <= 3 ; pass ++)
cannam@85 3460 { orig [20] = pass * 2 ;
cannam@85 3461
cannam@85 3462 test_read_write_position_or_die (file, __LINE__, pass, (pass - 1) * DATA_LENGTH, 3 * DATA_LENGTH) ;
cannam@85 3463
cannam@85 3464 /* Read what we just wrote. */
cannam@85 3465 test_read_double_or_die (file, pass, test, DATA_LENGTH, __LINE__) ;
cannam@85 3466
cannam@85 3467 /* Check the data. */
cannam@85 3468 for (k = 0 ; k < DATA_LENGTH ; k++)
cannam@85 3469 if (FLOAT_ERROR (orig [k], test [k]))
cannam@85 3470 { printf ("\n\nLine %d (pass %d) B : Error at sample %d (%g => %g).\n", __LINE__, pass, k, orig [k], test [k]) ;
cannam@85 3471 oct_save_double (orig, test, DATA_LENGTH) ;
cannam@85 3472 exit (1) ;
cannam@85 3473 } ;
cannam@85 3474
cannam@85 3475 } ; /* for (pass ...) */
cannam@85 3476
cannam@85 3477 sf_close (file) ;
cannam@85 3478 } /* mono_rdwr_double_test */
cannam@85 3479
cannam@85 3480 static void
cannam@85 3481 new_rdwr_double_test (const char *filename, int format, int allow_fd)
cannam@85 3482 { SNDFILE *wfile, *rwfile ;
cannam@85 3483 SF_INFO sfinfo ;
cannam@85 3484 double *orig, *test ;
cannam@85 3485 int items, frames ;
cannam@85 3486
cannam@85 3487 orig = orig_data.d ;
cannam@85 3488 test = test_data.d ;
cannam@85 3489
cannam@85 3490 sfinfo.samplerate = 44100 ;
cannam@85 3491 sfinfo.frames = SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */
cannam@85 3492 sfinfo.channels = 2 ;
cannam@85 3493 sfinfo.format = format ;
cannam@85 3494
cannam@85 3495 items = DATA_LENGTH ;
cannam@85 3496 frames = items / sfinfo.channels ;
cannam@85 3497
cannam@85 3498 wfile = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, allow_fd, __LINE__) ;
cannam@85 3499 sf_command (wfile, SFC_SET_UPDATE_HEADER_AUTO, NULL, SF_TRUE) ;
cannam@85 3500 test_writef_double_or_die (wfile, 1, orig, frames, __LINE__) ;
cannam@85 3501 sf_write_sync (wfile) ;
cannam@85 3502 test_writef_double_or_die (wfile, 2, orig, frames, __LINE__) ;
cannam@85 3503 sf_write_sync (wfile) ;
cannam@85 3504
cannam@85 3505 rwfile = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, allow_fd, __LINE__) ;
cannam@85 3506 if (sfinfo.frames != 2 * frames)
cannam@85 3507 { printf ("\n\nLine %d : incorrect number of frames in file (%ld should be %d)\n\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), 2 * frames) ;
cannam@85 3508 exit (1) ;
cannam@85 3509 } ;
cannam@85 3510
cannam@85 3511 test_writef_double_or_die (wfile, 3, orig, frames, __LINE__) ;
cannam@85 3512
cannam@85 3513 test_readf_double_or_die (rwfile, 1, test, frames, __LINE__) ;
cannam@85 3514 test_readf_double_or_die (rwfile, 2, test, frames, __LINE__) ;
cannam@85 3515
cannam@85 3516 sf_close (wfile) ;
cannam@85 3517 sf_close (rwfile) ;
cannam@85 3518 } /* new_rdwr_double_test */
cannam@85 3519
cannam@85 3520
cannam@85 3521
cannam@85 3522 /*----------------------------------------------------------------------------------------
cannam@85 3523 */
cannam@85 3524
cannam@85 3525 static void
cannam@85 3526 empty_file_test (const char *filename, int format)
cannam@85 3527 { SNDFILE *file ;
cannam@85 3528 SF_INFO info ;
cannam@85 3529 int allow_fd ;
cannam@85 3530
cannam@85 3531 /* Sd2 files cannot be opened from an existing file descriptor. */
cannam@85 3532 allow_fd = ((format & SF_FORMAT_TYPEMASK) == SF_FORMAT_SD2) ? SF_FALSE : SF_TRUE ;
cannam@85 3533
cannam@85 3534 print_test_name ("empty_file_test", filename) ;
cannam@85 3535
cannam@85 3536 unlink (filename) ;
cannam@85 3537
cannam@85 3538 info.samplerate = 48000 ;
cannam@85 3539 info.channels = 2 ;
cannam@85 3540 info.format = format ;
cannam@85 3541
cannam@85 3542 if (sf_format_check (&info) == SF_FALSE)
cannam@85 3543 { info.channels = 1 ;
cannam@85 3544 if (sf_format_check (&info) == SF_FALSE)
cannam@85 3545 { puts ("invalid file format") ;
cannam@85 3546 return ;
cannam@85 3547 } ;
cannam@85 3548 } ;
cannam@85 3549
cannam@85 3550 /* Create an empty file. */
cannam@85 3551 file = test_open_file_or_die (filename, SFM_WRITE, &info, allow_fd, __LINE__) ;
cannam@85 3552 sf_close (file) ;
cannam@85 3553
cannam@85 3554 /* Open for read and check the length. */
cannam@85 3555 file = test_open_file_or_die (filename, SFM_READ, &info, allow_fd, __LINE__) ;
cannam@85 3556
cannam@85 3557 if (SF_COUNT_TO_LONG (info.frames) != 0)
cannam@85 3558 { printf ("\n\nError : frame count (%ld) should be zero.\n", SF_COUNT_TO_LONG (info.frames)) ;
cannam@85 3559 exit (1) ;
cannam@85 3560 } ;
cannam@85 3561
cannam@85 3562 sf_close (file) ;
cannam@85 3563
cannam@85 3564 /* Open for read/write and check the length. */
cannam@85 3565 file = test_open_file_or_die (filename, SFM_RDWR, &info, allow_fd, __LINE__) ;
cannam@85 3566
cannam@85 3567 if (SF_COUNT_TO_LONG (info.frames) != 0)
cannam@85 3568 { printf ("\n\nError : frame count (%ld) should be zero.\n", SF_COUNT_TO_LONG (info.frames)) ;
cannam@85 3569 exit (1) ;
cannam@85 3570 } ;
cannam@85 3571
cannam@85 3572 sf_close (file) ;
cannam@85 3573
cannam@85 3574 /* Open for read and check the length. */
cannam@85 3575 file = test_open_file_or_die (filename, SFM_READ, &info, allow_fd, __LINE__) ;
cannam@85 3576
cannam@85 3577 if (SF_COUNT_TO_LONG (info.frames) != 0)
cannam@85 3578 { printf ("\n\nError : frame count (%ld) should be zero.\n", SF_COUNT_TO_LONG (info.frames)) ;
cannam@85 3579 exit (1) ;
cannam@85 3580 } ;
cannam@85 3581
cannam@85 3582 sf_close (file) ;
cannam@85 3583
cannam@85 3584 check_open_file_count_or_die (__LINE__) ;
cannam@85 3585
cannam@85 3586 unlink (filename) ;
cannam@85 3587 puts ("ok") ;
cannam@85 3588
cannam@85 3589 return ;
cannam@85 3590 } /* empty_file_test */
cannam@85 3591
cannam@85 3592
cannam@85 3593 /*----------------------------------------------------------------------------------------
cannam@85 3594 */
cannam@85 3595
cannam@85 3596 static void
cannam@85 3597 create_short_file (const char *filename)
cannam@85 3598 { FILE *file ;
cannam@85 3599
cannam@85 3600 if (! (file = fopen (filename, "w")))
cannam@85 3601 { printf ("create_short_file : fopen (%s, \"w\") failed.", filename) ;
cannam@85 3602 fflush (stdout) ;
cannam@85 3603 perror (NULL) ;
cannam@85 3604 exit (1) ;
cannam@85 3605 } ;
cannam@85 3606
cannam@85 3607 fprintf (file, "This is the file data.\n") ;
cannam@85 3608
cannam@85 3609 fclose (file) ;
cannam@85 3610 } /* create_short_file */
cannam@85 3611
cannam@85 3612 #if (defined (WIN32) || defined (__WIN32))
cannam@85 3613
cannam@85 3614 /* Win32 does not have truncate (nor does it have the POSIX function ftruncate).
cannam@85 3615 ** Hack somethng up here to over come this. This function can only truncate to a
cannam@85 3616 ** length of zero.
cannam@85 3617 */
cannam@85 3618
cannam@85 3619 static int
cannam@85 3620 truncate (const char *filename, int ignored)
cannam@85 3621 { int fd ;
cannam@85 3622
cannam@85 3623 ignored = 0 ;
cannam@85 3624
cannam@85 3625 if ((fd = open (filename, O_RDWR | O_TRUNC | O_BINARY)) < 0)
cannam@85 3626 return 0 ;
cannam@85 3627
cannam@85 3628 close (fd) ;
cannam@85 3629
cannam@85 3630 return 0 ;
cannam@85 3631 } /* truncate */
cannam@85 3632
cannam@85 3633 #endif
cannam@85 3634
cannam@85 3635 static void
cannam@85 3636 multi_seek_test (const char * filename, int format)
cannam@85 3637 { SNDFILE * file ;
cannam@85 3638 SF_INFO info ;
cannam@85 3639 sf_count_t pos ;
cannam@85 3640 int k ;
cannam@85 3641
cannam@85 3642 /* This test doesn't work on the following. */
cannam@85 3643 switch (format & SF_FORMAT_TYPEMASK)
cannam@85 3644 { case SF_FORMAT_RAW :
cannam@85 3645 return ;
cannam@85 3646
cannam@85 3647 default :
cannam@85 3648 break ;
cannam@85 3649 } ;
cannam@85 3650
cannam@85 3651 memset (&info, 0, sizeof (info)) ;
cannam@85 3652
cannam@85 3653 generate_file (filename, format, 88200) ;
cannam@85 3654
cannam@85 3655 file = test_open_file_or_die (filename, SFM_READ, &info, SF_FALSE, __LINE__) ;
cannam@85 3656
cannam@85 3657 for (k = 0 ; k < 10 ; k++)
cannam@85 3658 { pos = info.frames / (k + 2) ;
cannam@85 3659 test_seek_or_die (file, pos, SEEK_SET, pos, info.channels, __LINE__) ;
cannam@85 3660 } ;
cannam@85 3661
cannam@85 3662 sf_close (file) ;
cannam@85 3663 } /* multi_seek_test */
cannam@85 3664
cannam@85 3665 static void
cannam@85 3666 write_seek_extend_test (const char * filename, int format)
cannam@85 3667 { SNDFILE * file ;
cannam@85 3668 SF_INFO info ;
cannam@85 3669 short *orig, *test ;
cannam@85 3670 unsigned items, k ;
cannam@85 3671
cannam@85 3672 /* This test doesn't work on the following. */
cannam@85 3673 switch (format & SF_FORMAT_TYPEMASK)
cannam@85 3674 { case SF_FORMAT_FLAC :
cannam@85 3675 case SF_FORMAT_HTK :
cannam@85 3676 case SF_FORMAT_PAF :
cannam@85 3677 case SF_FORMAT_SDS :
cannam@85 3678 case SF_FORMAT_SVX :
cannam@85 3679 return ;
cannam@85 3680
cannam@85 3681 default :
cannam@85 3682 break ;
cannam@85 3683 } ;
cannam@85 3684
cannam@85 3685 memset (&info, 0, sizeof (info)) ;
cannam@85 3686
cannam@85 3687 info.samplerate = 48000 ;
cannam@85 3688 info.channels = 1 ;
cannam@85 3689 info.format = format ;
cannam@85 3690
cannam@85 3691 items = 512 ;
cannam@85 3692 exit_if_true (items > ARRAY_LEN (orig_data.s), "Line %d : Bad assumption.\n", __LINE__) ;
cannam@85 3693
cannam@85 3694 orig = orig_data.s ;
cannam@85 3695 test = test_data.s ;
cannam@85 3696
cannam@85 3697 for (k = 0 ; k < ARRAY_LEN (orig_data.s) ; k++)
cannam@85 3698 orig [k] = 0x3fff ;
cannam@85 3699
cannam@85 3700 file = test_open_file_or_die (filename, SFM_WRITE, &info, SF_FALSE, __LINE__) ;
cannam@85 3701 test_write_short_or_die (file, 0, orig, items, __LINE__) ;
cannam@85 3702
cannam@85 3703 /* Extend the file using a seek. */
cannam@85 3704 test_seek_or_die (file, 2 * items, SEEK_SET, 2 * items, info.channels, __LINE__) ;
cannam@85 3705
cannam@85 3706 test_writef_short_or_die (file, 0, orig, items, __LINE__) ;
cannam@85 3707 sf_close (file) ;
cannam@85 3708
cannam@85 3709 file = test_open_file_or_die (filename, SFM_READ, &info, SF_FALSE, __LINE__) ;
cannam@85 3710 test_read_short_or_die (file, 0, test, 3 * items, __LINE__) ;
cannam@85 3711 sf_close (file) ;
cannam@85 3712
cannam@85 3713 /* Can't do these formats due to scaling. */
cannam@85 3714 switch (format & SF_FORMAT_SUBMASK)
cannam@85 3715 { case SF_FORMAT_PCM_S8 :
cannam@85 3716 case SF_FORMAT_PCM_U8 :
cannam@85 3717 return ;
cannam@85 3718 default :
cannam@85 3719 break ;
cannam@85 3720 } ;
cannam@85 3721
cannam@85 3722 for (k = 0 ; k < items ; k++)
cannam@85 3723 { exit_if_true (test [k] != 0x3fff, "Line %d : test [%d] == %d, should be 0x3fff.\n", __LINE__, k, test [k]) ;
cannam@85 3724 exit_if_true (test [items + k] != 0, "Line %d : test [%d] == %d, should be 0.\n", __LINE__, items + k, test [items + k]) ;
cannam@85 3725 exit_if_true (test [2 * items + k] != 0x3fff, "Line %d : test [%d] == %d, should be 0x3fff.\n", __LINE__, 2 * items + k, test [2 * items + k]) ;
cannam@85 3726 } ;
cannam@85 3727
cannam@85 3728 return ;
cannam@85 3729 } /* write_seek_extend_test */
cannam@85 3730
cannam@85 3731