Add input csv files of examples to track list.
[Faustine.git] / interpreter / lib / src / libsndfile-1.0.25 / tests / vorbis_test.c
1 /*
2 ** Copyright (C) 2007-2011 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 / 8)
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 if (max_abs > 1.021)
100 { printf ("\n\n Error : max_abs %f should be < 1.021.\n\n", max_abs) ;
101 exit (1) ;
102 } ;
103
104 puts ("ok") ;
105 unlink (filename) ;
106 } /* vorbis_test */
107
108 static void
109 vorbis_quality_test (void)
110 { /*
111 ** Encode two files, one at quality 0.3 and one at quality 0.5 and then
112 ** make sure that the quality 0.3 files is the smaller of the two.
113 */
114 const char * q3_fname = "q3_vorbis.oga" ;
115 const char * q5_fname = "q5_vorbis.oga" ;
116
117 SNDFILE *q3_file, *q5_file ;
118 SF_INFO sfinfo ;
119 int q3_size, q5_size ;
120 double quality ;
121 int k ;
122
123 print_test_name (__func__, "q[35]_vorbis.oga") ;
124
125 memset (&sfinfo, 0, sizeof (sfinfo)) ;
126
127 /* Set up output file type. */
128 sfinfo.format = SF_FORMAT_OGG | SF_FORMAT_VORBIS ;
129 sfinfo.channels = 1 ;
130 sfinfo.samplerate = SAMPLE_RATE ;
131
132 /* Write the output file. */
133 q3_file = test_open_file_or_die (q3_fname, SFM_WRITE, &sfinfo, SF_FALSE, __LINE__) ;
134 q5_file = test_open_file_or_die (q5_fname, SFM_WRITE, &sfinfo, SF_FALSE, __LINE__) ;
135
136 quality = 0.3 ;
137 sf_command (q3_file, SFC_SET_VBR_ENCODING_QUALITY, &quality, sizeof (quality)) ;
138 quality = 0.5 ;
139 sf_command (q5_file, SFC_SET_VBR_ENCODING_QUALITY, &quality, sizeof (quality)) ;
140
141 for (k = 0 ; k < 5 ; k++)
142 { gen_lowpass_noise_float (data_out, ARRAY_LEN (data_out)) ;
143 test_write_float_or_die (q3_file, 0, data_out, ARRAY_LEN (data_out), __LINE__) ;
144 test_write_float_or_die (q5_file, 0, data_out, ARRAY_LEN (data_out), __LINE__) ;
145 } ;
146
147 sf_close (q3_file) ;
148 sf_close (q5_file) ;
149
150 q3_size = file_length (q3_fname) ;
151 q5_size = file_length (q5_fname) ;
152
153 if (q3_size >= q5_size)
154 { printf ("\n\nLine %d : q3 size (%d) >= q5 size (%d)\n\n", __LINE__, q3_size, q5_size) ;
155 exit (1) ;
156 } ;
157
158 puts ("ok") ;
159 unlink (q3_fname) ;
160 unlink (q5_fname) ;
161 } /* vorbis_quality_test */
162
163
164
165 int
166 main (void)
167 {
168 if (HAVE_EXTERNAL_LIBS)
169 { vorbis_test () ;
170 vorbis_quality_test () ;
171 }
172 else
173 puts (" No Ogg/Vorbis tests because Ogg/Vorbis support was not compiled in.") ;
174
175 return 0 ;
176 } /* main */