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