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