Chris@0
|
1 /*
|
Chris@0
|
2 ** Copyright (C) 2001-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 #include <string.h>
|
Chris@0
|
24 #include <math.h>
|
Chris@0
|
25
|
Chris@0
|
26 #if HAVE_UNISTD_H
|
Chris@0
|
27 #include <unistd.h>
|
Chris@0
|
28 #endif
|
Chris@0
|
29
|
Chris@0
|
30 #include <sndfile.h>
|
Chris@0
|
31
|
Chris@0
|
32 #include "utils.h"
|
Chris@0
|
33
|
Chris@0
|
34 #define BUFFER_LEN (1<<16)
|
Chris@0
|
35
|
Chris@0
|
36 static void stdin_test (int typemajor, int count) ;
|
Chris@0
|
37
|
Chris@0
|
38 int
|
Chris@0
|
39 main (int argc, char *argv [])
|
Chris@0
|
40 { int do_all = 0, test_count = 0 ;
|
Chris@0
|
41
|
Chris@0
|
42 if (BUFFER_LEN < PIPE_TEST_LEN)
|
Chris@0
|
43 { fprintf (stderr, "Error : BUFFER_LEN < PIPE_TEST_LEN.\n\n") ;
|
Chris@0
|
44 exit (1) ;
|
Chris@0
|
45 } ;
|
Chris@0
|
46
|
Chris@0
|
47 if (argc != 2)
|
Chris@0
|
48 { fprintf (stderr, "This program cannot be run by itself. It needs\n") ;
|
Chris@0
|
49 fprintf (stderr, "to be run from the stdio_test program.\n") ;
|
Chris@0
|
50 exit (1) ;
|
Chris@0
|
51 } ;
|
Chris@0
|
52
|
Chris@0
|
53 do_all = ! strcmp (argv [1], "all") ;
|
Chris@0
|
54
|
Chris@0
|
55 if (do_all || ! strcmp (argv [1], "raw"))
|
Chris@0
|
56 { stdin_test (SF_FORMAT_RAW, PIPE_TEST_LEN) ;
|
Chris@0
|
57 test_count++ ;
|
Chris@0
|
58 } ;
|
Chris@0
|
59
|
Chris@0
|
60 if (do_all || ! strcmp (argv [1], "wav"))
|
Chris@0
|
61 { stdin_test (SF_FORMAT_WAV, PIPE_TEST_LEN) ;
|
Chris@0
|
62 test_count++ ;
|
Chris@0
|
63 } ;
|
Chris@0
|
64
|
Chris@0
|
65 if (do_all || ! strcmp (argv [1], "aiff"))
|
Chris@0
|
66 { stdin_test (SF_FORMAT_AIFF, PIPE_TEST_LEN) ;
|
Chris@0
|
67 test_count++ ;
|
Chris@0
|
68 } ;
|
Chris@0
|
69
|
Chris@0
|
70 if (do_all || ! strcmp (argv [1], "au"))
|
Chris@0
|
71 { stdin_test (SF_FORMAT_AU, PIPE_TEST_LEN) ;
|
Chris@0
|
72 test_count++ ;
|
Chris@0
|
73 } ;
|
Chris@0
|
74
|
Chris@0
|
75 if (do_all || ! strcmp (argv [1], "paf"))
|
Chris@0
|
76 { stdin_test (SF_FORMAT_PAF, PIPE_TEST_LEN) ;
|
Chris@0
|
77 test_count++ ;
|
Chris@0
|
78 } ;
|
Chris@0
|
79
|
Chris@0
|
80 if (do_all || ! strcmp (argv [1], "svx"))
|
Chris@0
|
81 { stdin_test (SF_FORMAT_SVX, PIPE_TEST_LEN) ;
|
Chris@0
|
82 test_count++ ;
|
Chris@0
|
83 } ;
|
Chris@0
|
84
|
Chris@0
|
85 if (do_all || ! strcmp (argv [1], "nist"))
|
Chris@0
|
86 { stdin_test (SF_FORMAT_NIST, PIPE_TEST_LEN) ;
|
Chris@0
|
87 test_count++ ;
|
Chris@0
|
88 } ;
|
Chris@0
|
89
|
Chris@0
|
90 if (do_all || ! strcmp (argv [1], "ircam"))
|
Chris@0
|
91 { stdin_test (SF_FORMAT_IRCAM, PIPE_TEST_LEN) ;
|
Chris@0
|
92 test_count++ ;
|
Chris@0
|
93 } ;
|
Chris@0
|
94
|
Chris@0
|
95 if (do_all || ! strcmp (argv [1], "voc"))
|
Chris@0
|
96 { stdin_test (SF_FORMAT_VOC, PIPE_TEST_LEN) ;
|
Chris@0
|
97 test_count++ ;
|
Chris@0
|
98 } ;
|
Chris@0
|
99
|
Chris@0
|
100 if (do_all || ! strcmp (argv [1], "w64"))
|
Chris@0
|
101 { stdin_test (SF_FORMAT_W64, PIPE_TEST_LEN) ;
|
Chris@0
|
102 test_count++ ;
|
Chris@0
|
103 } ;
|
Chris@0
|
104
|
Chris@0
|
105 if (do_all || ! strcmp (argv [1], "mat4"))
|
Chris@0
|
106 { stdin_test (SF_FORMAT_MAT4, PIPE_TEST_LEN) ;
|
Chris@0
|
107 test_count++ ;
|
Chris@0
|
108 } ;
|
Chris@0
|
109
|
Chris@0
|
110 if (do_all || ! strcmp (argv [1], "mat5"))
|
Chris@0
|
111 { stdin_test (SF_FORMAT_MAT5, PIPE_TEST_LEN) ;
|
Chris@0
|
112 test_count++ ;
|
Chris@0
|
113 } ;
|
Chris@0
|
114
|
Chris@0
|
115 if (do_all || ! strcmp (argv [1], "pvf"))
|
Chris@0
|
116 { stdin_test (SF_FORMAT_PVF, PIPE_TEST_LEN) ;
|
Chris@0
|
117 test_count++ ;
|
Chris@0
|
118 } ;
|
Chris@0
|
119
|
Chris@0
|
120 if (do_all || ! strcmp (argv [1], "htk"))
|
Chris@0
|
121 { stdin_test (SF_FORMAT_HTK, PIPE_TEST_LEN) ;
|
Chris@0
|
122 test_count++ ;
|
Chris@0
|
123 } ;
|
Chris@0
|
124
|
Chris@0
|
125 if (test_count == 0)
|
Chris@0
|
126 { fprintf (stderr, "************************************\n") ;
|
Chris@0
|
127 fprintf (stderr, "* No '%s' test defined.\n", argv [1]) ;
|
Chris@0
|
128 fprintf (stderr, "************************************\n") ;
|
Chris@0
|
129 return 1 ;
|
Chris@0
|
130 } ;
|
Chris@0
|
131
|
Chris@0
|
132 return 0 ;
|
Chris@0
|
133 } /* main */
|
Chris@0
|
134
|
Chris@0
|
135 static void
|
Chris@0
|
136 stdin_test (int typemajor, int count)
|
Chris@0
|
137 { static short data [BUFFER_LEN] ;
|
Chris@0
|
138
|
Chris@0
|
139 SNDFILE *file ;
|
Chris@0
|
140 SF_INFO sfinfo ;
|
Chris@0
|
141 int k, total, err ;
|
Chris@0
|
142
|
Chris@0
|
143 if (typemajor == SF_FORMAT_RAW)
|
Chris@0
|
144 { sfinfo.samplerate = 44100 ;
|
Chris@0
|
145 sfinfo.format = SF_FORMAT_RAW | SF_FORMAT_PCM_16 ;
|
Chris@0
|
146 sfinfo.channels = 1 ;
|
Chris@0
|
147 sfinfo.frames = 0 ;
|
Chris@0
|
148 }
|
Chris@0
|
149 else
|
Chris@0
|
150 memset (&sfinfo, 0, sizeof (sfinfo)) ;
|
Chris@0
|
151
|
Chris@0
|
152 if ((file = sf_open_fd (STDIN_FILENO, SFM_READ, &sfinfo, SF_TRUE)) == NULL)
|
Chris@0
|
153 { fprintf (stderr, "sf_open_fd failed with error : ") ;
|
Chris@0
|
154 puts (sf_strerror (NULL)) ;
|
Chris@0
|
155 dump_log_buffer (NULL) ;
|
Chris@0
|
156 exit (1) ;
|
Chris@0
|
157 } ;
|
Chris@0
|
158
|
Chris@0
|
159 err = sf_error (file) ;
|
Chris@0
|
160 if (err != SF_ERR_NO_ERROR)
|
Chris@0
|
161 { printf ("Line %d : unexpected error : %s\n", __LINE__, sf_error_number (err)) ;
|
Chris@0
|
162 exit (1) ;
|
Chris@0
|
163 } ;
|
Chris@0
|
164
|
Chris@0
|
165 if ((sfinfo.format & SF_FORMAT_TYPEMASK) != typemajor)
|
Chris@0
|
166 { fprintf (stderr, "\n\nError : File type doesn't match.\n") ;
|
Chris@0
|
167 exit (1) ;
|
Chris@0
|
168 } ;
|
Chris@0
|
169
|
Chris@0
|
170 if (sfinfo.samplerate != 44100)
|
Chris@0
|
171 { fprintf (stderr, "\n\nError : Sample rate (%d) should be 44100\n", sfinfo.samplerate) ;
|
Chris@0
|
172 exit (1) ;
|
Chris@0
|
173 } ;
|
Chris@0
|
174
|
Chris@0
|
175 if (sfinfo.channels != 1)
|
Chris@0
|
176 { fprintf (stderr, "\n\nError : Channels (%d) should be 1\n", sfinfo.channels) ;
|
Chris@0
|
177 exit (1) ;
|
Chris@0
|
178 } ;
|
Chris@0
|
179
|
Chris@0
|
180 if (sfinfo.frames < count)
|
Chris@0
|
181 { fprintf (stderr, "\n\nError : Sample count (%ld) should be %d\n", (long) sfinfo.frames, count) ;
|
Chris@0
|
182 exit (1) ;
|
Chris@0
|
183 } ;
|
Chris@0
|
184
|
Chris@0
|
185 total = 0 ;
|
Chris@0
|
186 while ((k = sf_read_short (file, data + total, BUFFER_LEN - total)) > 0)
|
Chris@0
|
187 total += k ;
|
Chris@0
|
188
|
Chris@0
|
189 if (total != count)
|
Chris@0
|
190 { fprintf (stderr, "\n\nError : Expected %d frames, read %d.\n", count, total) ;
|
Chris@0
|
191 exit (1) ;
|
Chris@0
|
192 } ;
|
Chris@0
|
193
|
Chris@0
|
194 for (k = 0 ; k < total ; k++)
|
Chris@0
|
195 if (data [k] != PIPE_INDEX (k))
|
Chris@0
|
196 { printf ("\n\nError : data [%d] == %d, should have been %d.\n\n", k, data [k], k) ;
|
Chris@0
|
197 exit (1) ;
|
Chris@0
|
198 } ;
|
Chris@0
|
199
|
Chris@0
|
200 sf_close (file) ;
|
Chris@0
|
201
|
Chris@0
|
202 return ;
|
Chris@0
|
203 } /* stdin_test */
|
Chris@0
|
204
|