annotate src/libsndfile-1.0.25/tests/multi_file_test.c @ 0:c7265573341e

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