• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 ** Copyright (C) 2011-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 
25 #if HAVE_UNISTD_H
26 #include <unistd.h>
27 #else
28 #include "sf_unistd.h"
29 #endif
30 
31 #include "sndfile.h"
32 #include "utils.h"
33 
34 static void format_error_test (void) ;
35 static void format_combo_test (void) ;
36 
37 int
main(void)38 main (void)
39 {
40 	format_error_test () ;
41 	format_combo_test () ;
42 
43 	return 0 ;
44 } /* main */
45 
46 /*==============================================================================
47 */
48 
49 static void
format_error_test(void)50 format_error_test (void)
51 {	const char *filename = "format-error.wav" ;
52 	SNDFILE *file ;
53 	SF_INFO info ;
54 
55 	print_test_name (__func__, NULL) ;
56 
57 	memset (&info, 0, sizeof (info)) ;
58 	info.format = SF_FORMAT_WAV | SF_FORMAT_PCM_16 ;
59 	info.channels = 1 ;
60 	info.samplerate = 44100 ;
61 
62 	info.format = SF_FORMAT_WAV ;
63 	file = sf_open (filename, SFM_WRITE, &info) ;
64 	exit_if_true (file != NULL, "\n\nLine %d : Format should not be valid.\n\n", __LINE__) ;
65 	exit_if_true (
66 		strstr (sf_strerror (NULL), "minor format") == NULL,
67 		"\n\nLine %d : Error string should reference bad 'minor format'.\n\n", __LINE__
68 		) ;
69 
70 	info.format = SF_FORMAT_PCM_16 ;
71 	file = sf_open (filename, SFM_WRITE, &info) ;
72 	exit_if_true (file != NULL, "\n\nLine %d : Format should not be valid.\n\n", __LINE__) ;
73 	exit_if_true (
74 		strstr (sf_strerror (NULL), "major format") == NULL,
75 		"\n\nLine %d : Error string should reference bad 'major format'.\n\n", __LINE__
76 		) ;
77 
78 	unlink (filename) ;
79 	puts ("ok") ;
80 } /* format_error_test */
81 
82 static void
format_combo_test(void)83 format_combo_test (void)
84 {	int container_max, codec_max, cont, codec ;
85 
86 	print_test_name (__func__, NULL) ;
87 
88 	sf_command (NULL, SFC_GET_FORMAT_MAJOR_COUNT, &container_max, sizeof (container_max)) ;
89 	sf_command (NULL, SFC_GET_FORMAT_SUBTYPE_COUNT, &codec_max, sizeof (codec_max)) ;
90 
91 	for (cont = 0 ; cont < container_max + 10 ; cont ++)
92 	{	SF_FORMAT_INFO major_fmt_info ;
93 
94 		memset (&major_fmt_info, 0, sizeof (major_fmt_info)) ;
95 		major_fmt_info.format = cont ;
96 		(void) sf_command (NULL, SFC_GET_FORMAT_MAJOR, &major_fmt_info, sizeof (major_fmt_info)) ;
97 
98 		for (codec = 0 ; codec < codec_max + 10 ; codec ++)
99 		{	SF_FORMAT_INFO subtype_fmt_info ;
100 			SNDFILE * sndfile ;
101 			SF_INFO info ;
102 			char filename [128] ;
103 			int subtype_is_valid, check_is_valid ;
104 
105 			memset (&info, 0, sizeof (info)) ;
106 			memset (&subtype_fmt_info, 0, sizeof (subtype_fmt_info)) ;
107 			subtype_fmt_info.format = codec ;
108 			subtype_is_valid = sf_command (NULL, SFC_GET_FORMAT_SUBTYPE, &subtype_fmt_info, sizeof (subtype_fmt_info)) == 0 ;
109 
110 			/* Opus only works with a fixed set of sample rates. */
111 			if (subtype_fmt_info.format == SF_FORMAT_OPUS)
112 				sf_info_setup (&info, major_fmt_info.format | subtype_fmt_info.format, 24000, 1) ;
113 			else
114 				sf_info_setup (&info, major_fmt_info.format | subtype_fmt_info.format, 22050, 1) ;
115 
116 			check_is_valid = sf_format_check (&info) ;
117 
118 			exit_if_true (
119 				NOT (subtype_is_valid) && check_is_valid,
120 				"\n\nLine %d : Subtype is not valid but checks ok.\n",
121 				__LINE__
122 				) ;
123 
124 			/* Only have decode, not encode support for MPEG Layer I and II */
125 			if (subtype_fmt_info.format == SF_FORMAT_MPEG_LAYER_I ||
126 					subtype_fmt_info.format == SF_FORMAT_MPEG_LAYER_II)
127 				continue ;
128 
129 			/* MPEG Layer III in WAV is decode only currently */
130 			if (subtype_fmt_info.format == SF_FORMAT_MPEG_LAYER_III &&
131 					major_fmt_info.format == SF_FORMAT_WAV)
132 				continue ;
133 
134 			snprintf (filename, sizeof (filename), "format-check.%s", major_fmt_info.extension) ;
135 
136 			sndfile = sf_open (filename, SFM_WRITE, &info) ;
137 
138 			sf_close (sndfile) ;
139 			unlink (filename) ;
140 
141 			if (major_fmt_info.extension != NULL && strcmp (major_fmt_info.extension, "sd2") == 0)
142 			{	snprintf (filename, sizeof (filename), "._format-check.%s", major_fmt_info.extension) ;
143 				unlink (filename) ;
144 				} ;
145 
146 			exit_if_true (
147 				sndfile && NOT (check_is_valid),
148 				"\n\nError : Format was not valid but file opened correctly.\n"
149 				"    Container : %s\n"
150 				"    Codec     : %s\n\n",
151 				major_fmt_info.name, subtype_fmt_info.name
152 				) ;
153 
154 			exit_if_true (
155 				NOT (sndfile) && check_is_valid,
156 				"\n\nError : Format was valid but file failed to open.\n"
157 				"    Container : %s\n"
158 				"    Codec     : %s\n\n",
159 				major_fmt_info.name, subtype_fmt_info.name
160 				) ;
161 			} ;
162 		} ;
163 
164 	puts ("ok") ;
165 } /* format_combo_test */
166 
167