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