2 ** Copyright (C) 1999-2011 Erik de Castro Lopo <erikd@mega-nerd.com>
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.
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.
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.
28 #if (HAVE_DECL_S_IRGRP == 0)
29 #include <sf_unistd.h>
42 #define DATA_LENGTH (512)
44 static void write_file_at_end (int fd
, int filetype
, int channels
, int file_num
) ;
46 static void multi_file_test (const char *filename
, int *formats
, int format_count
) ;
48 static short data
[DATA_LENGTH
] ;
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
,
59 /*-SF_FORMAT_WAV | SF_FORMAT_GSM610 Doesn't work yet. -*/
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
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
76 static int verbose
= SF_FALSE
;
79 main (int argc
, char **argv
)
83 if (argc
== 3 && strcmp (argv
[2], "-v") == 0)
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") ;
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") ;
101 printf (" all - perform all tests\n") ;
105 do_all
= !strcmp (argv
[1], "all") ;
107 if (do_all
|| ! strcmp (argv
[1], "wav"))
108 { multi_file_test ("multi_wav.dat", wav_formats
, ARRAY_LEN (wav_formats
)) ;
112 if (do_all
|| ! strcmp (argv
[1], "aiff"))
113 { multi_file_test ("multi_aiff.dat", aiff_formats
, ARRAY_LEN (aiff_formats
)) ;
117 if (do_all
|| ! strcmp (argv
[1], "au"))
118 { multi_file_test ("multi_au.dat", au_formats
, ARRAY_LEN (au_formats
)) ;
125 /*======================================================================================
129 multi_file_test (const char *filename
, int *formats
, int format_count
)
132 SF_EMBED_FILE_INFO embed_info
;
134 int fd
, k
, file_count
= 0 ;
136 print_test_name ("multi_file_test", filename
) ;
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
)) ;
145 k
= write (fd
, "1234", 4) ;
147 for (k
= 0 ; k
< format_count
; k
++)
148 write_file_at_end (fd
, formats
[k
], 2, k
) ;
150 filelen
= file_length_fd (fd
) ;
152 embed_info
.offset
= 4 ;
153 embed_info
.length
= 0 ;
156 for (file_count
= 1 ; embed_info
.offset
+ embed_info
.length
< filelen
; file_count
++)
159 { puts ("\n------------------------------------") ;
160 printf ("This offset : %ld\n", SF_COUNT_TO_LONG (embed_info
.offset
+ embed_info
.length
)) ;
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
)) ;
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
) ;
177 sf_command (sndfile
, SFC_GET_EMBED_FILE_INFO
, &embed_info
, sizeof (embed_info
)) ;
182 printf ("\nNext offset : %ld\nNext length : %ld\n", SF_COUNT_TO_LONG (embed_info
.offset
), SF_COUNT_TO_LONG (embed_info
.length
)) ;
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
) ;
198 } /* multi_file_test */
200 /*======================================================================================
204 write_file_at_end (int fd
, int filetype
, int channels
, int file_num
)
210 lseek (fd
, 0, SEEK_END
) ;
212 for (k
= 0 ; k
< DATA_LENGTH
; k
++)
215 frames
= DATA_LENGTH
/ channels
;
217 sfinfo
.format
= filetype
;
218 sfinfo
.channels
= channels
;
219 sfinfo
.samplerate
= 44100 ;
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
) ;
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
) ;
236 } /* write_file_at_end */