• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 ** Copyright (C) 2008-2017 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 <errno.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 static void major_format_test (void) ;
37 static void subtype_format_test (void) ;
38 static void simple_format_test (void) ;
39 static void flac_subset_test (void) ;
40 
41 int
main(void)42 main (void)
43 {
44 	major_format_test () ;
45 	subtype_format_test () ;
46 	simple_format_test () ;
47 
48 	if (HAVE_EXTERNAL_XIPH_LIBS)
49 		flac_subset_test () ;
50 
51 	return 0 ;
52 } /* main */
53 
54 static void
major_format_test(void)55 major_format_test (void)
56 {	SF_FORMAT_INFO	info ;
57 	int have_ogg = 0, have_flac = 0 ;
58 	int m, major_count ;
59 
60 	print_test_name (__func__, NULL) ;
61 
62 	sf_command (NULL, SFC_GET_FORMAT_MAJOR_COUNT, &major_count, sizeof (int)) ;
63 
64 	for (m = 0 ; m < major_count ; m++)
65 	{	info.format = m ;
66 		sf_command (NULL, SFC_GET_FORMAT_MAJOR, &info, sizeof (info)) ;
67 
68 		have_flac = info.format == SF_FORMAT_FLAC ? 1 : have_flac ;
69 		have_ogg = info.format == SF_FORMAT_OGG ? 1 : have_ogg ;
70 		} ;
71 
72 	if (HAVE_EXTERNAL_XIPH_LIBS)
73 	{	exit_if_true (have_flac == 0, "\n\nLine %d : FLAC should be available.\n\n", __LINE__) ;
74 		exit_if_true (have_ogg == 0, "\n\nLine %d : Ogg/Vorbis should be available.\n\n", __LINE__) ;
75 		}
76 	else
77 	{	exit_if_true (have_flac, "\n\nLine %d : FLAC should not be available.\n\n", __LINE__) ;
78 		exit_if_true (have_ogg, "\n\nLine %d : Ogg/Vorbis should not be available.\n\n", __LINE__) ;
79 		} ;
80 
81 	puts ("ok") ;
82 } /* major_format_test */
83 
84 static void
subtype_format_test(void)85 subtype_format_test (void)
86 {	SF_FORMAT_INFO	info ;
87 	int have_vorbis = 0 , have_opus = 0 ;
88 	int s, subtype_count ;
89 
90 	print_test_name (__func__, NULL) ;
91 
92 	sf_command (NULL, SFC_GET_FORMAT_SUBTYPE_COUNT, &subtype_count, sizeof (int)) ;
93 
94 	for (s = 0 ; s < subtype_count ; s++)
95 	{	info.format = s ;
96 		sf_command (NULL, SFC_GET_FORMAT_SUBTYPE, &info, sizeof (info)) ;
97 
98 		have_vorbis = info.format == SF_FORMAT_VORBIS ? 1 : have_vorbis ;
99 		have_opus = info.format == SF_FORMAT_OPUS ? 1 : have_opus ;
100 		} ;
101 
102 	if (HAVE_EXTERNAL_XIPH_LIBS)
103 	{	exit_if_true (have_vorbis == 0, "\n\nLine %d : Ogg/Vorbis should be available.\n\n", __LINE__) ;
104 		exit_if_true (have_opus == 0, "\n\nLine %d : Ogg/Opus should be available.\n\n", __LINE__) ;
105 		}
106 	else
107 	{	exit_if_true (have_vorbis, "\n\nLine %d : Ogg/Vorbis should not be available.\n\n", __LINE__) ;
108 		exit_if_true (have_opus, "\n\nLine %d : Ogg/Opus should not be available.\n\n", __LINE__) ;
109 		} ;
110 
111 	puts ("ok") ;
112 } /* subtype_format_test */
113 
114 static void
simple_format_test(void)115 simple_format_test (void)
116 {	SF_FORMAT_INFO	info ;
117 	int have_flac = 0, have_ogg = 0, have_vorbis = 0, have_opus = 0 ;
118 	int s, simple_count ;
119 
120 	print_test_name (__func__, NULL) ;
121 
122 	sf_command (NULL, SFC_GET_SIMPLE_FORMAT_COUNT, &simple_count, sizeof (int)) ;
123 
124 	for (s = 0 ; s < simple_count ; s++)
125 	{	info.format = s ;
126 		sf_command (NULL, SFC_GET_SIMPLE_FORMAT, &info, sizeof (info)) ;
127 
128 		switch (info.format & SF_FORMAT_TYPEMASK)
129 		{	case SF_FORMAT_FLAC :
130 				have_flac = 1 ;
131 				break ;
132 
133 			case SF_FORMAT_OGG :
134 				have_ogg = 1 ;
135 				break ;
136 
137 			default :
138 				break ;
139 			} ;
140 
141 		switch (info.format & SF_FORMAT_SUBMASK)
142 		{	case SF_FORMAT_VORBIS :
143 				have_vorbis = 1 ;
144 				break ;
145 
146 			case SF_FORMAT_OPUS :
147 				have_opus = 1 ;
148 				break ;
149 
150 			default :
151 				break ;
152 			} ;
153 
154 		} ;
155 
156 	if (HAVE_EXTERNAL_XIPH_LIBS)
157 	{	exit_if_true (have_flac == 0, "\n\nLine %d : FLAC should be available.\n\n", __LINE__) ;
158 		exit_if_true (have_ogg == 0, "\n\nLine %d : Ogg/Vorbis should be available.\n\n", __LINE__) ;
159 		exit_if_true (have_vorbis == 0, "\n\nLine %d : Ogg/Vorbis should be available.\n\n", __LINE__) ;
160 		exit_if_true (have_opus == 0, "\n\nLine %d : Ogg/Opus should be available.\n\n", __LINE__) ;
161 		}
162 	else
163 	{	exit_if_true (have_flac, "\n\nLine %d : FLAC should not be available.\n\n", __LINE__) ;
164 		exit_if_true (have_ogg, "\n\nLine %d : Ogg/Vorbis should not be available.\n\n", __LINE__) ;
165 		exit_if_true (have_vorbis, "\n\nLine %d : Ogg/Vorbis should not be available.\n\n", __LINE__) ;
166 		exit_if_true (have_opus, "\n\nLine %d : Ogg/Opus should not be available.\n\n", __LINE__) ;
167 		} ;
168 
169 	puts ("ok") ;
170 } /* simple_format_test */
171 
172 static void
flac_subset_test(void)173 flac_subset_test (void)
174 {	float whatever [256] ;
175 	SNDFILE	*file ;
176 	SF_INFO	sfinfo ;
177 	sf_count_t rc ;
178 	int		samplerate ;
179 	const char *filename = "subset_test.flac" ;
180 
181 	/* For some formats (like FLAC) the headers are written *just* before the
182 	** first bit of audio data. This test makes sure errors in that process
183 	** are caught.
184 	*/
185 
186 	print_test_name (__func__, NULL) ;
187 
188 	for (samplerate = 65536 ; samplerate < 655350 ; samplerate *= 4)
189 	{	sfinfo.samplerate	= samplerate ;
190 		sfinfo.channels		= 1 ;
191 		sfinfo.frames		= 0 ;
192 		sfinfo.format = SF_FORMAT_FLAC | SF_FORMAT_PCM_16 ;
193 
194 		file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
195 		rc = sf_write_float (file, whatever, ARRAY_LEN (whatever)) ;
196 		unlink (filename) ;
197 		exit_if_true (rc != 0, "\n\nLine %d : return code (%d) should be 0.\n\n", __LINE__, (int) rc) ;
198 
199 		sf_close (file) ;
200 		} ;
201 
202 	puts ("ok") ;
203 } /* flac_subset_test */
204 
205