• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 ** Copyright (C) 2007-2016 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 #if HAVE_UNISTD_H
25 #include <unistd.h>
26 #else
27 #include "sf_unistd.h"
28 #endif
29 
30 #include <math.h>
31 
32 #include	<sndfile.h>
33 
34 #include	"utils.h"
35 #include	"dft_cmp.h"
36 
37 #define	SAMPLE_RATE		16000
38 #define	DATA_LENGTH		(SAMPLE_RATE)
39 
40 static float data_out [DATA_LENGTH] ;
41 
42 static inline float
max_float(float a,float b)43 max_float (float a, float b)
44 {	return a > b ? a : b ;
45 } /* max_float */
46 
47 static void
vorbis_test(void)48 vorbis_test (void)
49 {	static float float_data [DFT_DATA_LENGTH] ;
50 	const char * filename = "vorbis_test.oga" ;
51 	SNDFILE * file ;
52 	SF_INFO sfinfo ;
53 	float max_abs = 0.0 ;
54 	unsigned k ;
55 
56 	print_test_name ("vorbis_test", filename) ;
57 
58 	/* Generate float data. */
59 	gen_windowed_sine_float (float_data, ARRAY_LEN (float_data), 1.0) ;
60 
61 	/* Set up output file type. */
62 	memset (&sfinfo, 0, sizeof (sfinfo)) ;
63 	sfinfo.format = SF_FORMAT_OGG | SF_FORMAT_VORBIS ;
64 	sfinfo.channels = 1 ;
65 	sfinfo.samplerate = SAMPLE_RATE ;
66 
67 	/* Write the output file. */
68 
69 	/*	The Vorbis encoder has a bug on PowerPC and X86-64 with sample rates
70 	**	<= 22050. Increasing the sample rate to 32000 avoids triggering it.
71 	**	See https://trac.xiph.org/ticket/1229
72 	*/
73 	if ((file = sf_open (filename, SFM_WRITE, &sfinfo)) == NULL)
74 	{	const char * errstr ;
75 
76 		errstr = sf_strerror (NULL) ;
77 		if (strstr (errstr, "Sample rate chosen is known to trigger a Vorbis") == NULL)
78 		{	printf ("Line %d: sf_open (SFM_WRITE) failed : %s\n", __LINE__, errstr) ;
79 			dump_log_buffer (NULL) ;
80 			exit (1) ;
81 			} ;
82 
83 		printf ("\n                                  Sample rate -> 32kHz    ") ;
84 		sfinfo.samplerate = 32000 ;
85 
86 		file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
87 		} ;
88 
89 	test_write_float_or_die (file, 0, float_data, ARRAY_LEN (float_data), __LINE__) ;
90 	sf_close (file) ;
91 
92 	memset (float_data, 0, sizeof (float_data)) ;
93 
94 	/* Read the file back in again. */
95 	memset (&sfinfo, 0, sizeof (sfinfo)) ;
96 	file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_FALSE, __LINE__) ;
97 	test_read_float_or_die (file, 0, float_data, ARRAY_LEN (float_data), __LINE__) ;
98 	sf_close (file) ;
99 
100 	for (k = 0 ; k < ARRAY_LEN (float_data) ; k ++)
101 		max_abs = max_float (max_abs, fabs (float_data [k])) ;
102 
103 	exit_if_true (max_abs > 1.023,
104 		"\n\nLine %d : max_abs %f should be < 1.023.\n\n", __LINE__, max_abs) ;
105 
106 	puts ("ok") ;
107 	unlink (filename) ;
108 } /* vorbis_test */
109 
110 static void
compression_size_test(int format,const char * filename)111 compression_size_test (int format, const char * filename)
112 {	/*
113 	**	Encode two files, one at quality 0.3 and one at quality 0.5 and then
114 	**	make sure that the quality 0.3 files is the smaller of the two.
115 	*/
116 	char q3_fname [64] ;
117 	char q6_fname [64] ;
118 	char test_name [64] ;
119 
120 	SNDFILE *q3_file, *q6_file ;
121 	SF_INFO sfinfo ;
122 	int q3_size, q6_size ;
123 	double quality ;
124 	int k ;
125 
126 	snprintf (q3_fname, sizeof (q3_fname), "q3_%s", filename) ;
127 	snprintf (q6_fname, sizeof (q6_fname), "q6_%s", filename) ;
128 
129 	snprintf (test_name, sizeof (test_name), "q[36]_%s", filename) ;
130 	print_test_name (__func__, test_name) ;
131 
132 	memset (&sfinfo, 0, sizeof (sfinfo)) ;
133 
134 	/* Set up output file type. */
135 	sfinfo.format = format ;
136 	sfinfo.channels = 1 ;
137 	sfinfo.samplerate = SAMPLE_RATE ;
138 
139 	/* Write the output file. */
140 	q3_file = test_open_file_or_die (q3_fname, SFM_WRITE, &sfinfo, SF_FALSE, __LINE__) ;
141 	q6_file = test_open_file_or_die (q6_fname, SFM_WRITE, &sfinfo, SF_FALSE, __LINE__) ;
142 
143 	quality = 0.3 ;
144 	sf_command (q3_file, SFC_SET_VBR_ENCODING_QUALITY, &quality, sizeof (quality)) ;
145 	quality = 0.6 ;
146 	sf_command (q6_file, SFC_SET_VBR_ENCODING_QUALITY, &quality, sizeof (quality)) ;
147 
148 	for (k = 0 ; k < 5 ; k++)
149 	{	gen_lowpass_signal_float (data_out, ARRAY_LEN (data_out)) ;
150 		test_write_float_or_die (q3_file, 0, data_out, ARRAY_LEN (data_out), __LINE__) ;
151 		test_write_float_or_die (q6_file, 0, data_out, ARRAY_LEN (data_out), __LINE__) ;
152 		} ;
153 
154 	sf_close (q3_file) ;
155 	sf_close (q6_file) ;
156 
157 	q3_size = file_length (q3_fname) ;
158 	q6_size = file_length (q6_fname) ;
159 
160 	exit_if_true (q3_size >= q6_size,
161 		"\n\nLine %d : q3 size (%d) >= q6 size (%d)\n\n", __LINE__, q3_size, q6_size) ;
162 
163 	puts ("ok") ;
164 	unlink (q3_fname) ;
165 	unlink (q6_fname) ;
166 } /* compression_size_test */
167 
168 
169 
170 int
main(int argc,char * argv[])171 main (int argc, char *argv [])
172 {	int all_tests = 0, tests = 0 ;
173 
174 	if (argc != 2)
175 	{	printf (
176 			"Usage : %s <test>\n"
177 			"    Where <test> is one of:\n"
178 			"        vorbis - test Ogg/Vorbis\n"
179 			"        flac   - test FLAC\n"
180 			"        opus   - test Opus\n"
181 			"        all    - perform all tests\n",
182 			argv [0]) ;
183 		exit (0) ;
184 		} ;
185 
186 	if (! HAVE_EXTERNAL_XIPH_LIBS)
187 	{	puts ("    No Ogg/Vorbis tests because Ogg/Vorbis support was not compiled in.") ;
188 		return 0 ;
189 	} ;
190 
191 	if (strcmp (argv [1], "all") == 0)
192 		all_tests = 1 ;
193 
194 	if (all_tests || strcmp (argv [1], "vorbis") == 0)
195 	{	vorbis_test () ;
196 		compression_size_test (SF_FORMAT_OGG | SF_FORMAT_VORBIS, "vorbis.oga") ;
197 		tests ++ ;
198 		} ;
199 
200 	if (all_tests || strcmp (argv [1], "flac") == 0)
201 	{	compression_size_test (SF_FORMAT_FLAC | SF_FORMAT_PCM_16, "pcm16.flac") ;
202 		tests ++ ;
203 		} ;
204 
205 	if (all_tests || strcmp (argv [1], "opus") == 0)
206 	{	compression_size_test (SF_FORMAT_OGG | SF_FORMAT_OPUS, "opus.opus") ;
207 		tests ++ ;
208 		} ;
209 
210 	return 0 ;
211 } /* main */
212