annotate src/libsndfile-1.0.27/tests/multi_file_test.c @ 83:ae30d91d2ffe

Replace these with versions built using an older toolset (so as to avoid ABI compatibilities when linking on Ubuntu 14.04 for packaging purposes)
author Chris Cannam
date Fri, 07 Feb 2020 11:51:13 +0000
parents 1df64224f5ac
children
rev   line source
Chris@40 1 /*
Chris@40 2 ** Copyright (C) 1999-2012 Erik de Castro Lopo <erikd@mega-nerd.com>
Chris@40 3 **
Chris@40 4 ** This program is free software; you can redistribute it and/or modify
Chris@40 5 ** it under the terms of the GNU General Public License as published by
Chris@40 6 ** the Free Software Foundation; either version 2 of the License, or
Chris@40 7 ** (at your option) any later version.
Chris@40 8 **
Chris@40 9 ** This program is distributed in the hope that it will be useful,
Chris@40 10 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@40 11 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@40 12 ** GNU General Public License for more details.
Chris@40 13 **
Chris@40 14 ** You should have received a copy of the GNU General Public License
Chris@40 15 ** along with this program; if not, write to the Free Software
Chris@40 16 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Chris@40 17 */
Chris@40 18
Chris@40 19 #include "sfconfig.h"
Chris@40 20
Chris@40 21 #include <stdio.h>
Chris@40 22 #include <stdlib.h>
Chris@40 23 #include <string.h>
Chris@40 24 #include <math.h>
Chris@40 25 #include <inttypes.h>
Chris@40 26
Chris@40 27 #if HAVE_UNISTD_H
Chris@40 28 #include <unistd.h>
Chris@40 29 #endif
Chris@40 30
Chris@40 31 #if (HAVE_DECL_S_IRGRP == 0)
Chris@40 32 #include <sf_unistd.h>
Chris@40 33 #endif
Chris@40 34
Chris@40 35 #include <fcntl.h>
Chris@40 36 #include <errno.h>
Chris@40 37 #include <sys/stat.h>
Chris@40 38
Chris@40 39 #include <sndfile.h>
Chris@40 40
Chris@40 41 #include "utils.h"
Chris@40 42
Chris@40 43 #define DATA_LENGTH (512)
Chris@40 44
Chris@40 45 static void write_file_at_end (int fd, int filetype, int channels, int file_num) ;
Chris@40 46
Chris@40 47 static void multi_file_test (const char *filename, int *formats, int format_count) ;
Chris@40 48
Chris@40 49 static short data [DATA_LENGTH] ;
Chris@40 50
Chris@40 51 static int wav_formats [] =
Chris@40 52 { SF_FORMAT_WAV | SF_FORMAT_PCM_16,
Chris@40 53 SF_FORMAT_WAV | SF_FORMAT_PCM_24,
Chris@40 54 SF_FORMAT_WAV | SF_FORMAT_ULAW,
Chris@40 55 SF_FORMAT_WAV | SF_FORMAT_ALAW,
Chris@40 56 /* Lite remove start */
Chris@40 57 SF_FORMAT_WAV | SF_FORMAT_IMA_ADPCM,
Chris@40 58 SF_FORMAT_WAV | SF_FORMAT_MS_ADPCM,
Chris@40 59 /* Lite remove end */
Chris@40 60 /*-SF_FORMAT_WAV | SF_FORMAT_GSM610 Doesn't work yet. -*/
Chris@40 61 } ;
Chris@40 62
Chris@40 63 static int aiff_formats [] =
Chris@40 64 { SF_FORMAT_AIFF | SF_FORMAT_PCM_16,
Chris@40 65 SF_FORMAT_AIFF | SF_FORMAT_PCM_24,
Chris@40 66 SF_FORMAT_AIFF | SF_FORMAT_ULAW,
Chris@40 67 SF_FORMAT_AIFF | SF_FORMAT_ALAW
Chris@40 68 } ;
Chris@40 69
Chris@40 70 static int au_formats [] =
Chris@40 71 { SF_FORMAT_AU | SF_FORMAT_PCM_16,
Chris@40 72 SF_FORMAT_AU | SF_FORMAT_PCM_24,
Chris@40 73 SF_FORMAT_AU | SF_FORMAT_ULAW,
Chris@40 74 SF_FORMAT_AU | SF_FORMAT_ALAW
Chris@40 75 } ;
Chris@40 76
Chris@40 77 static int verbose = SF_FALSE ;
Chris@40 78
Chris@40 79 int
Chris@40 80 main (int argc, char **argv)
Chris@40 81 { int do_all = 0 ;
Chris@40 82 int test_count = 0 ;
Chris@40 83
Chris@40 84 if (argc == 3 && strcmp (argv [2], "-v") == 0)
Chris@40 85 { verbose = SF_TRUE ;
Chris@40 86 argc -- ;
Chris@40 87 } ;
Chris@40 88
Chris@40 89 if (argc != 2)
Chris@40 90 { printf ("Usage : %s <test>\n", argv [0]) ;
Chris@40 91 printf (" Where <test> is one of the following:\n") ;
Chris@40 92 printf (" wav - test WAV file functions (little endian)\n") ;
Chris@40 93 printf (" aiff - test AIFF file functions (big endian)\n") ;
Chris@40 94 printf (" au - test AU file functions\n") ;
Chris@40 95 #if 0
Chris@40 96 printf (" svx - test 8SVX/16SV file functions\n") ;
Chris@40 97 printf (" nist - test NIST Sphere file functions\n") ;
Chris@40 98 printf (" ircam - test IRCAM file functions\n") ;
Chris@40 99 printf (" voc - Create Voice file functions\n") ;
Chris@40 100 printf (" w64 - Sonic Foundry's W64 file functions\n") ;
Chris@40 101 #endif
Chris@40 102 printf (" all - perform all tests\n") ;
Chris@40 103 exit (1) ;
Chris@40 104 } ;
Chris@40 105
Chris@40 106 do_all = !strcmp (argv [1], "all") ;
Chris@40 107
Chris@40 108 if (do_all || ! strcmp (argv [1], "wav"))
Chris@40 109 { multi_file_test ("multi_wav.dat", wav_formats, ARRAY_LEN (wav_formats)) ;
Chris@40 110 test_count++ ;
Chris@40 111 } ;
Chris@40 112
Chris@40 113 if (do_all || ! strcmp (argv [1], "aiff"))
Chris@40 114 { multi_file_test ("multi_aiff.dat", aiff_formats, ARRAY_LEN (aiff_formats)) ;
Chris@40 115 test_count++ ;
Chris@40 116 } ;
Chris@40 117
Chris@40 118 if (do_all || ! strcmp (argv [1], "au"))
Chris@40 119 { multi_file_test ("multi_au.dat", au_formats, ARRAY_LEN (au_formats)) ;
Chris@40 120 test_count++ ;
Chris@40 121 } ;
Chris@40 122
Chris@40 123 return 0 ;
Chris@40 124 } /* main */
Chris@40 125
Chris@40 126 /*======================================================================================
Chris@40 127 */
Chris@40 128
Chris@40 129 static void
Chris@40 130 multi_file_test (const char *filename, int *formats, int format_count)
Chris@40 131 { SNDFILE *sndfile ;
Chris@40 132 SF_INFO sfinfo ;
Chris@40 133 SF_EMBED_FILE_INFO embed_info ;
Chris@40 134 sf_count_t filelen ;
Chris@40 135 int fd, k, file_count = 0 ;
Chris@40 136
Chris@40 137 print_test_name ("multi_file_test", filename) ;
Chris@40 138
Chris@40 139 unlink (filename) ;
Chris@40 140
Chris@40 141 if ((fd = open (filename, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR)) < 0)
Chris@40 142 { printf ("\n\nLine %d: open failed : %s\n", __LINE__, strerror (errno)) ;
Chris@40 143 exit (1) ;
Chris@40 144 } ;
Chris@40 145
Chris@40 146 k = write (fd, "1234", 4) ;
Chris@40 147
Chris@40 148 for (k = 0 ; k < format_count ; k++)
Chris@40 149 write_file_at_end (fd, formats [k], 2, k) ;
Chris@40 150
Chris@40 151 filelen = file_length_fd (fd) ;
Chris@40 152
Chris@40 153 embed_info.offset = 4 ;
Chris@40 154 embed_info.length = 0 ;
Chris@40 155
Chris@40 156
Chris@40 157 for (file_count = 1 ; embed_info.offset + embed_info.length < filelen ; file_count ++)
Chris@40 158 {
Chris@40 159 if (verbose)
Chris@40 160 { puts ("\n------------------------------------") ;
Chris@40 161 printf ("This offset : %" PRId64 "\n", embed_info.offset + embed_info.length) ;
Chris@40 162 } ;
Chris@40 163
Chris@40 164 if (lseek (fd, embed_info.offset + embed_info.length, SEEK_SET) < 0)
Chris@40 165 { printf ("\n\nLine %d: lseek failed : %s\n", __LINE__, strerror (errno)) ;
Chris@40 166 exit (1) ;
Chris@40 167 } ;
Chris@40 168
Chris@40 169 memset (&sfinfo, 0, sizeof (sfinfo)) ;
Chris@40 170 if ((sndfile = sf_open_fd (fd, SFM_READ, &sfinfo, SF_FALSE)) == NULL)
Chris@40 171 { printf ("\n\nLine %d: sf_open_fd failed\n", __LINE__) ;
Chris@40 172 printf ("Embedded file number : %d offset : %" PRId64 "\n", file_count, embed_info.offset) ;
Chris@40 173 puts (sf_strerror (sndfile)) ;
Chris@40 174 dump_log_buffer (sndfile) ;
Chris@40 175 exit (1) ;
Chris@40 176 } ;
Chris@40 177
Chris@40 178 sf_command (sndfile, SFC_GET_EMBED_FILE_INFO, &embed_info, sizeof (embed_info)) ;
Chris@40 179
Chris@40 180 sf_close (sndfile) ;
Chris@40 181
Chris@40 182 if (verbose)
Chris@40 183 printf ("\nNext offset : %" PRId64 "\nNext length : %" PRId64 "\n", embed_info.offset, embed_info.length) ;
Chris@40 184 } ;
Chris@40 185
Chris@40 186 file_count -- ;
Chris@40 187
Chris@40 188 if (file_count != format_count)
Chris@40 189 { printf ("\n\nLine %d: file count (%d) not equal to %d.\n\n", __LINE__, file_count, format_count) ;
Chris@40 190 printf ("Embedded file number : %d\n", file_count) ;
Chris@40 191 exit (1) ;
Chris@40 192 } ;
Chris@40 193
Chris@40 194 close (fd) ;
Chris@40 195 unlink (filename) ;
Chris@40 196 printf ("ok\n") ;
Chris@40 197
Chris@40 198 return ;
Chris@40 199 } /* multi_file_test */
Chris@40 200
Chris@40 201 /*======================================================================================
Chris@40 202 */
Chris@40 203
Chris@40 204 static void
Chris@40 205 write_file_at_end (int fd, int filetype, int channels, int file_num)
Chris@40 206 { SNDFILE *sndfile ;
Chris@40 207 SF_INFO sfinfo ;
Chris@40 208
Chris@40 209 int frames, k ;
Chris@40 210
Chris@40 211 lseek (fd, 0, SEEK_END) ;
Chris@40 212
Chris@40 213 for (k = 0 ; k < DATA_LENGTH ; k++)
Chris@40 214 data [k] = k ;
Chris@40 215
Chris@40 216 frames = DATA_LENGTH / channels ;
Chris@40 217
Chris@40 218 sfinfo.format = filetype ;
Chris@40 219 sfinfo.channels = channels ;
Chris@40 220 sfinfo.samplerate = 44100 ;
Chris@40 221
Chris@40 222 if ((sndfile = sf_open_fd (fd, SFM_WRITE, &sfinfo, SF_FALSE)) == NULL)
Chris@40 223 { printf ("\n\nLine %d: sf_open_fd failed\n", __LINE__) ;
Chris@40 224 printf ("Embedded file number : %d\n", file_num) ;
Chris@40 225 puts (sf_strerror (sndfile)) ;
Chris@40 226 dump_log_buffer (sndfile) ;
Chris@40 227 exit (1) ;
Chris@40 228 } ;
Chris@40 229
Chris@40 230 if (sf_writef_short (sndfile, data, frames) != frames)
Chris@40 231 { printf ("\n\nLine %d: short write\n", __LINE__) ;
Chris@40 232 printf ("Embedded file number : %d\n", file_num) ;
Chris@40 233 exit (1) ;
Chris@40 234 } ;
Chris@40 235
Chris@40 236 sf_close (sndfile) ;
Chris@40 237 } /* write_file_at_end */
Chris@40 238