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