comparison src/libsndfile-1.0.27/tests/compression_size_test.c @ 40:1df64224f5ac

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