041f3980723ec72a857e1fa86e5b78ad93d24f6b
[Faustine.git] / interpretor / lib / src / libsndfile-1.0.25 / tests / multi_file_test.c
1 /*
2 ** Copyright (C) 1999-2011 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
24 #if HAVE_UNISTD_H
25 #include <unistd.h>
26 #endif
27
28 #if (HAVE_DECL_S_IRGRP == 0)
29 #include <sf_unistd.h>
30 #endif
31
32 #include <fcntl.h>
33 #include <math.h>
34 #include <string.h>
35 #include <errno.h>
36 #include <sys/stat.h>
37
38 #include <sndfile.h>
39
40 #include "utils.h"
41
42 #define DATA_LENGTH (512)
43
44 static void write_file_at_end (int fd, int filetype, int channels, int file_num) ;
45
46 static void multi_file_test (const char *filename, int *formats, int format_count) ;
47
48 static short data [DATA_LENGTH] ;
49
50 static int wav_formats [] =
51 { SF_FORMAT_WAV | SF_FORMAT_PCM_16,
52 SF_FORMAT_WAV | SF_FORMAT_PCM_24,
53 SF_FORMAT_WAV | SF_FORMAT_ULAW,
54 SF_FORMAT_WAV | SF_FORMAT_ALAW,
55 /* Lite remove start */
56 SF_FORMAT_WAV | SF_FORMAT_IMA_ADPCM,
57 SF_FORMAT_WAV | SF_FORMAT_MS_ADPCM,
58 /* Lite remove end */
59 /*-SF_FORMAT_WAV | SF_FORMAT_GSM610 Doesn't work yet. -*/
60 } ;
61
62 static int aiff_formats [] =
63 { SF_FORMAT_AIFF | SF_FORMAT_PCM_16,
64 SF_FORMAT_AIFF | SF_FORMAT_PCM_24,
65 SF_FORMAT_AIFF | SF_FORMAT_ULAW,
66 SF_FORMAT_AIFF | SF_FORMAT_ALAW
67 } ;
68
69 static int au_formats [] =
70 { SF_FORMAT_AU | SF_FORMAT_PCM_16,
71 SF_FORMAT_AU | SF_FORMAT_PCM_24,
72 SF_FORMAT_AU | SF_FORMAT_ULAW,
73 SF_FORMAT_AU | SF_FORMAT_ALAW
74 } ;
75
76 static int verbose = SF_FALSE ;
77
78 int
79 main (int argc, char **argv)
80 { int do_all = 0 ;
81 int test_count = 0 ;
82
83 if (argc == 3 && strcmp (argv [2], "-v") == 0)
84 { verbose = SF_TRUE ;
85 argc -- ;
86 } ;
87
88 if (argc != 2)
89 { printf ("Usage : %s <test>\n", argv [0]) ;
90 printf (" Where <test> is one of the following:\n") ;
91 printf (" wav - test WAV file functions (little endian)\n") ;
92 printf (" aiff - test AIFF file functions (big endian)\n") ;
93 printf (" au - test AU file functions\n") ;
94 #if 0
95 printf (" svx - test 8SVX/16SV file functions\n") ;
96 printf (" nist - test NIST Sphere file functions\n") ;
97 printf (" ircam - test IRCAM file functions\n") ;
98 printf (" voc - Create Voice file functions\n") ;
99 printf (" w64 - Sonic Foundry's W64 file functions\n") ;
100 #endif
101 printf (" all - perform all tests\n") ;
102 exit (1) ;
103 } ;
104
105 do_all = !strcmp (argv [1], "all") ;
106
107 if (do_all || ! strcmp (argv [1], "wav"))
108 { multi_file_test ("multi_wav.dat", wav_formats, ARRAY_LEN (wav_formats)) ;
109 test_count++ ;
110 } ;
111
112 if (do_all || ! strcmp (argv [1], "aiff"))
113 { multi_file_test ("multi_aiff.dat", aiff_formats, ARRAY_LEN (aiff_formats)) ;
114 test_count++ ;
115 } ;
116
117 if (do_all || ! strcmp (argv [1], "au"))
118 { multi_file_test ("multi_au.dat", au_formats, ARRAY_LEN (au_formats)) ;
119 test_count++ ;
120 } ;
121
122 return 0 ;
123 } /* main */
124
125 /*======================================================================================
126 */
127
128 static void
129 multi_file_test (const char *filename, int *formats, int format_count)
130 { SNDFILE *sndfile ;
131 SF_INFO sfinfo ;
132 SF_EMBED_FILE_INFO embed_info ;
133 sf_count_t filelen ;
134 int fd, k, file_count = 0 ;
135
136 print_test_name ("multi_file_test", filename) ;
137
138 unlink (filename) ;
139
140 if ((fd = open (filename, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR)) < 0)
141 { printf ("\n\nLine %d: open failed : %s\n", __LINE__, strerror (errno)) ;
142 exit (1) ;
143 } ;
144
145 k = write (fd, "1234", 4) ;
146
147 for (k = 0 ; k < format_count ; k++)
148 write_file_at_end (fd, formats [k], 2, k) ;
149
150 filelen = file_length_fd (fd) ;
151
152 embed_info.offset = 4 ;
153 embed_info.length = 0 ;
154
155
156 for (file_count = 1 ; embed_info.offset + embed_info.length < filelen ; file_count ++)
157 {
158 if (verbose)
159 { puts ("\n------------------------------------") ;
160 printf ("This offset : %ld\n", SF_COUNT_TO_LONG (embed_info.offset + embed_info.length)) ;
161 } ;
162
163 if (lseek (fd, embed_info.offset + embed_info.length, SEEK_SET) < 0)
164 { printf ("\n\nLine %d: lseek failed : %s\n", __LINE__, strerror (errno)) ;
165 exit (1) ;
166 } ;
167
168 memset (&sfinfo, 0, sizeof (sfinfo)) ;
169 if ((sndfile = sf_open_fd (fd, SFM_READ, &sfinfo, SF_FALSE)) == NULL)
170 { printf ("\n\nLine %d: sf_open_fd failed\n", __LINE__) ;
171 printf ("Embedded file number : %d offset : %ld\n", file_count, SF_COUNT_TO_LONG (embed_info.offset)) ;
172 puts (sf_strerror (sndfile)) ;
173 dump_log_buffer (sndfile) ;
174 exit (1) ;
175 } ;
176
177 sf_command (sndfile, SFC_GET_EMBED_FILE_INFO, &embed_info, sizeof (embed_info)) ;
178
179 sf_close (sndfile) ;
180
181 if (verbose)
182 printf ("\nNext offset : %ld\nNext length : %ld\n", SF_COUNT_TO_LONG (embed_info.offset), SF_COUNT_TO_LONG (embed_info.length)) ;
183 } ;
184
185 file_count -- ;
186
187 if (file_count != format_count)
188 { printf ("\n\nLine %d: file count (%d) not equal to %d.\n\n", __LINE__, file_count, format_count) ;
189 printf ("Embedded file number : %d\n", file_count) ;
190 exit (1) ;
191 } ;
192
193 close (fd) ;
194 unlink (filename) ;
195 printf ("ok\n") ;
196
197 return ;
198 } /* multi_file_test */
199
200 /*======================================================================================
201 */
202
203 static void
204 write_file_at_end (int fd, int filetype, int channels, int file_num)
205 { SNDFILE *sndfile ;
206 SF_INFO sfinfo ;
207
208 int frames, k ;
209
210 lseek (fd, 0, SEEK_END) ;
211
212 for (k = 0 ; k < DATA_LENGTH ; k++)
213 data [k] = k ;
214
215 frames = DATA_LENGTH / channels ;
216
217 sfinfo.format = filetype ;
218 sfinfo.channels = channels ;
219 sfinfo.samplerate = 44100 ;
220
221 if ((sndfile = sf_open_fd (fd, SFM_WRITE, &sfinfo, SF_FALSE)) == NULL)
222 { printf ("\n\nLine %d: sf_open_fd failed\n", __LINE__) ;
223 printf ("Embedded file number : %d\n", file_num) ;
224 puts (sf_strerror (sndfile)) ;
225 dump_log_buffer (sndfile) ;
226 exit (1) ;
227 } ;
228
229 if (sf_writef_short (sndfile, data, frames) != frames)
230 { printf ("\n\nLine %d: short write\n", __LINE__) ;
231 printf ("Embedded file number : %d\n", file_num) ;
232 exit (1) ;
233 } ;
234
235 sf_close (sndfile) ;
236 } /* write_file_at_end */
237