2 ** Copyright (C) 2007-2011 Erik de Castro Lopo <erikd@mega-nerd.com>
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.
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.
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.
33 #define SAMPLE_RATE 16000
34 #define DATA_LENGTH (SAMPLE_RATE / 8)
36 static float data_out
[DATA_LENGTH
] ;
39 max_float (float a
, float b
)
40 { return a
> b
? a
: b
;
45 { static float float_data
[DFT_DATA_LENGTH
] ;
46 const char * filename
= "vorbis_test.oga" ;
52 print_test_name ("vorbis_test", filename
) ;
54 /* Generate float data. */
55 gen_windowed_sine_float (float_data
, ARRAY_LEN (float_data
), 1.0) ;
57 /* Set up output file type. */
58 memset (&sfinfo
, 0, sizeof (sfinfo
)) ;
59 sfinfo
.format
= SF_FORMAT_OGG
| SF_FORMAT_VORBIS
;
61 sfinfo
.samplerate
= SAMPLE_RATE
;
63 /* Write the output file. */
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
69 if ((file
= sf_open (filename
, SFM_WRITE
, &sfinfo
)) == NULL
)
70 { const char * errstr
;
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
) ;
79 printf ("\n Sample rate -> 32kHz ") ;
80 sfinfo
.samplerate
= 32000 ;
82 file
= test_open_file_or_die (filename
, SFM_WRITE
, &sfinfo
, SF_TRUE
, __LINE__
) ;
85 test_write_float_or_die (file
, 0, float_data
, ARRAY_LEN (float_data
), __LINE__
) ;
88 memset (float_data
, 0, sizeof (float_data
)) ;
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__
) ;
96 for (k
= 0 ; k
< ARRAY_LEN (float_data
) ; k
++)
97 max_abs
= max_float (max_abs
, fabs (float_data
[k
])) ;
100 { printf ("\n\n Error : max_abs %f should be < 1.021.\n\n", max_abs
) ;
109 vorbis_quality_test (void)
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.
114 const char * q3_fname
= "q3_vorbis.oga" ;
115 const char * q5_fname
= "q5_vorbis.oga" ;
117 SNDFILE
*q3_file
, *q5_file
;
119 int q3_size
, q5_size
;
123 print_test_name (__func__
, "q[35]_vorbis.oga") ;
125 memset (&sfinfo
, 0, sizeof (sfinfo
)) ;
127 /* Set up output file type. */
128 sfinfo
.format
= SF_FORMAT_OGG
| SF_FORMAT_VORBIS
;
129 sfinfo
.channels
= 1 ;
130 sfinfo
.samplerate
= SAMPLE_RATE
;
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__
) ;
137 sf_command (q3_file
, SFC_SET_VBR_ENCODING_QUALITY
, &quality
, sizeof (quality
)) ;
139 sf_command (q5_file
, SFC_SET_VBR_ENCODING_QUALITY
, &quality
, sizeof (quality
)) ;
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__
) ;
150 q3_size
= file_length (q3_fname
) ;
151 q5_size
= file_length (q5_fname
) ;
153 if (q3_size
>= q5_size
)
154 { printf ("\n\nLine %d : q3 size (%d) >= q5 size (%d)\n\n", __LINE__
, q3_size
, q5_size
) ;
161 } /* vorbis_quality_test */
168 if (HAVE_EXTERNAL_LIBS
)
170 vorbis_quality_test () ;
173 puts (" No Ogg/Vorbis tests because Ogg/Vorbis support was not compiled in.") ;