2007-08-21 04:18:52 +00:00
|
|
|
/*
|
2012-06-19 09:53:39 +00:00
|
|
|
** Copyright (C) 2007-2012 Erik de Castro Lopo <erikd@mega-nerd.com>
|
2007-08-21 04:18:52 +00:00
|
|
|
**
|
|
|
|
** This program is free software; you can redistribute it and/or modify
|
|
|
|
** it under the terms of the GNU General Public License as published by
|
|
|
|
** the Free Software Foundation; either version 2 of the License, or
|
|
|
|
** (at your option) any later version.
|
|
|
|
**
|
|
|
|
** This program is distributed in the hope that it will be useful,
|
|
|
|
** but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
** GNU General Public License for more details.
|
|
|
|
**
|
|
|
|
** You should have received a copy of the GNU General Public License
|
|
|
|
** along with this program; if not, write to the Free Software
|
|
|
|
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "sfconfig.h"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <math.h>
|
|
|
|
|
|
|
|
#include <sndfile.h>
|
|
|
|
|
|
|
|
#include "utils.h"
|
|
|
|
#include "dft_cmp.h"
|
|
|
|
|
2007-08-21 09:50:43 +00:00
|
|
|
#define SAMPLE_RATE 16000
|
2012-06-20 11:57:28 +00:00
|
|
|
#define DATA_LENGTH (SAMPLE_RATE)
|
2007-11-03 10:48:56 +00:00
|
|
|
|
|
|
|
static float data_out [DATA_LENGTH] ;
|
2007-08-21 04:18:52 +00:00
|
|
|
|
|
|
|
static inline float
|
|
|
|
max_float (float a, float b)
|
|
|
|
{ return a > b ? a : b ;
|
|
|
|
} /* max_float */
|
|
|
|
|
|
|
|
static void
|
|
|
|
vorbis_test (void)
|
|
|
|
{ static float float_data [DFT_DATA_LENGTH] ;
|
2007-08-21 10:24:14 +00:00
|
|
|
const char * filename = "vorbis_test.oga" ;
|
2007-08-21 04:18:52 +00:00
|
|
|
SNDFILE * file ;
|
|
|
|
SF_INFO sfinfo ;
|
|
|
|
float max_abs = 0.0 ;
|
|
|
|
unsigned k ;
|
|
|
|
|
|
|
|
print_test_name ("vorbis_test", filename) ;
|
|
|
|
|
|
|
|
/* Generate float data. */
|
2007-08-21 09:50:43 +00:00
|
|
|
gen_windowed_sine_float (float_data, ARRAY_LEN (float_data), 1.0) ;
|
2007-08-21 04:18:52 +00:00
|
|
|
|
|
|
|
/* Set up output file type. */
|
|
|
|
memset (&sfinfo, 0, sizeof (sfinfo)) ;
|
|
|
|
sfinfo.format = SF_FORMAT_OGG | SF_FORMAT_VORBIS ;
|
|
|
|
sfinfo.channels = 1 ;
|
|
|
|
sfinfo.samplerate = SAMPLE_RATE ;
|
|
|
|
|
|
|
|
/* Write the output file. */
|
2007-08-22 11:30:58 +00:00
|
|
|
|
|
|
|
/* The Vorbis encoder has a bug on PowerPC and X86-64 with sample rates
|
|
|
|
** <= 22050. Increasing the sample rate to 32000 avoids triggering it.
|
|
|
|
** See https://trac.xiph.org/ticket/1229
|
|
|
|
*/
|
|
|
|
if ((file = sf_open (filename, SFM_WRITE, &sfinfo)) == NULL)
|
|
|
|
{ const char * errstr ;
|
2008-04-19 09:19:24 +00:00
|
|
|
|
2007-08-22 11:30:58 +00:00
|
|
|
errstr = sf_strerror (NULL) ;
|
|
|
|
if (strstr (errstr, "Sample rate chosen is known to trigger a Vorbis") == NULL)
|
2008-04-27 08:29:31 +00:00
|
|
|
{ printf ("Line %d: sf_open (SFM_WRITE) failed : %s\n", __LINE__, errstr) ;
|
2007-08-22 11:30:58 +00:00
|
|
|
dump_log_buffer (NULL) ;
|
|
|
|
exit (1) ;
|
|
|
|
} ;
|
|
|
|
|
|
|
|
printf ("\n Sample rate -> 32kHz ") ;
|
|
|
|
sfinfo.samplerate = 32000 ;
|
|
|
|
|
|
|
|
file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
|
|
|
|
} ;
|
|
|
|
|
2007-08-21 04:18:52 +00:00
|
|
|
test_write_float_or_die (file, 0, float_data, ARRAY_LEN (float_data), __LINE__) ;
|
|
|
|
sf_close (file) ;
|
|
|
|
|
|
|
|
memset (float_data, 0, sizeof (float_data)) ;
|
|
|
|
|
|
|
|
/* Read the file back in again. */
|
|
|
|
memset (&sfinfo, 0, sizeof (sfinfo)) ;
|
|
|
|
file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_FALSE, __LINE__) ;
|
|
|
|
test_read_float_or_die (file, 0, float_data, ARRAY_LEN (float_data), __LINE__) ;
|
|
|
|
sf_close (file) ;
|
|
|
|
|
|
|
|
for (k = 0 ; k < ARRAY_LEN (float_data) ; k ++)
|
|
|
|
max_abs = max_float (max_abs, fabs (float_data [k])) ;
|
|
|
|
|
2009-03-04 00:44:42 +00:00
|
|
|
if (max_abs > 1.021)
|
|
|
|
{ printf ("\n\n Error : max_abs %f should be < 1.021.\n\n", max_abs) ;
|
2007-08-21 04:18:52 +00:00
|
|
|
exit (1) ;
|
|
|
|
} ;
|
|
|
|
|
|
|
|
puts ("ok") ;
|
|
|
|
unlink (filename) ;
|
|
|
|
} /* vorbis_test */
|
|
|
|
|
2007-11-03 10:48:56 +00:00
|
|
|
static void
|
2012-06-19 09:53:39 +00:00
|
|
|
compression_size_test (int format, const char * filename)
|
2007-11-03 10:48:56 +00:00
|
|
|
{ /*
|
|
|
|
** Encode two files, one at quality 0.3 and one at quality 0.5 and then
|
|
|
|
** make sure that the quality 0.3 files is the smaller of the two.
|
|
|
|
*/
|
2012-06-19 09:53:39 +00:00
|
|
|
char q3_fname [64] ;
|
2012-06-20 11:57:28 +00:00
|
|
|
char q6_fname [64] ;
|
2012-06-19 09:53:39 +00:00
|
|
|
char test_name [64] ;
|
2007-11-03 10:48:56 +00:00
|
|
|
|
2012-06-20 11:57:28 +00:00
|
|
|
SNDFILE *q3_file, *q6_file ;
|
2007-11-03 10:48:56 +00:00
|
|
|
SF_INFO sfinfo ;
|
2012-06-20 11:57:28 +00:00
|
|
|
int q3_size, q6_size ;
|
2007-11-03 10:48:56 +00:00
|
|
|
double quality ;
|
|
|
|
int k ;
|
|
|
|
|
2012-06-19 09:53:39 +00:00
|
|
|
snprintf (q3_fname, sizeof (q3_fname), "q3_%s", filename) ;
|
2012-06-20 11:57:28 +00:00
|
|
|
snprintf (q6_fname, sizeof (q6_fname), "q6_%s", filename) ;
|
2012-06-19 09:53:39 +00:00
|
|
|
|
2012-06-20 11:57:28 +00:00
|
|
|
snprintf (test_name, sizeof (test_name), "q[36]_%s", filename) ;
|
2012-06-19 09:53:39 +00:00
|
|
|
print_test_name (__func__, test_name) ;
|
2007-11-03 10:48:56 +00:00
|
|
|
|
|
|
|
memset (&sfinfo, 0, sizeof (sfinfo)) ;
|
|
|
|
|
|
|
|
/* Set up output file type. */
|
2012-06-19 09:53:39 +00:00
|
|
|
sfinfo.format = format ;
|
2007-11-03 10:48:56 +00:00
|
|
|
sfinfo.channels = 1 ;
|
|
|
|
sfinfo.samplerate = SAMPLE_RATE ;
|
|
|
|
|
|
|
|
/* Write the output file. */
|
|
|
|
q3_file = test_open_file_or_die (q3_fname, SFM_WRITE, &sfinfo, SF_FALSE, __LINE__) ;
|
2012-06-20 11:57:28 +00:00
|
|
|
q6_file = test_open_file_or_die (q6_fname, SFM_WRITE, &sfinfo, SF_FALSE, __LINE__) ;
|
2007-11-03 10:48:56 +00:00
|
|
|
|
|
|
|
quality = 0.3 ;
|
2007-11-04 04:39:12 +00:00
|
|
|
sf_command (q3_file, SFC_SET_VBR_ENCODING_QUALITY, &quality, sizeof (quality)) ;
|
2012-06-20 11:57:28 +00:00
|
|
|
quality = 0.6 ;
|
|
|
|
sf_command (q6_file, SFC_SET_VBR_ENCODING_QUALITY, &quality, sizeof (quality)) ;
|
2007-11-03 10:48:56 +00:00
|
|
|
|
|
|
|
for (k = 0 ; k < 5 ; k++)
|
2012-06-20 11:57:28 +00:00
|
|
|
{ gen_lowpass_signal_float (data_out, ARRAY_LEN (data_out)) ;
|
2007-11-03 10:48:56 +00:00
|
|
|
test_write_float_or_die (q3_file, 0, data_out, ARRAY_LEN (data_out), __LINE__) ;
|
2012-06-20 11:57:28 +00:00
|
|
|
test_write_float_or_die (q6_file, 0, data_out, ARRAY_LEN (data_out), __LINE__) ;
|
2007-11-03 10:48:56 +00:00
|
|
|
} ;
|
|
|
|
|
|
|
|
sf_close (q3_file) ;
|
2012-06-20 11:57:28 +00:00
|
|
|
sf_close (q6_file) ;
|
2007-11-03 10:48:56 +00:00
|
|
|
|
|
|
|
q3_size = file_length (q3_fname) ;
|
2012-06-20 11:57:28 +00:00
|
|
|
q6_size = file_length (q6_fname) ;
|
2007-11-03 10:48:56 +00:00
|
|
|
|
2012-06-20 11:57:28 +00:00
|
|
|
if (q3_size >= q6_size)
|
|
|
|
{ printf ("\n\nLine %d : q3 size (%d) >= q5 size (%d)\n\n", __LINE__, q3_size, q6_size) ;
|
2007-11-03 10:48:56 +00:00
|
|
|
exit (1) ;
|
|
|
|
} ;
|
|
|
|
|
|
|
|
puts ("ok") ;
|
|
|
|
unlink (q3_fname) ;
|
2012-06-20 11:57:28 +00:00
|
|
|
unlink (q6_fname) ;
|
2012-06-19 09:53:39 +00:00
|
|
|
} /* compression_size_test */
|
2007-11-03 10:48:56 +00:00
|
|
|
|
|
|
|
|
2007-08-21 04:18:52 +00:00
|
|
|
|
|
|
|
int
|
2012-06-20 11:57:28 +00:00
|
|
|
main (int argc, char *argv [])
|
|
|
|
{ int all_tests = 0, tests = 0 ;
|
|
|
|
|
|
|
|
if (argc != 2)
|
|
|
|
{ printf (
|
|
|
|
"Usage : %s <test>\n"
|
|
|
|
" Where <test> is one of:\n"
|
|
|
|
" vorbis - test Ogg/Vorbis\n"
|
|
|
|
" flac - test FLAC\n"
|
|
|
|
" all - perform all tests\n",
|
|
|
|
argv [0]) ;
|
|
|
|
exit (0) ;
|
|
|
|
} ;
|
|
|
|
|
2012-06-19 09:53:39 +00:00
|
|
|
if (! HAVE_EXTERNAL_LIBS)
|
|
|
|
{ puts (" No Ogg/Vorbis tests because Ogg/Vorbis support was not compiled in.") ;
|
|
|
|
return 0 ;
|
|
|
|
} ;
|
|
|
|
|
2012-06-20 11:57:28 +00:00
|
|
|
if (strcmp (argv [1], "all") == 0)
|
|
|
|
all_tests = 1 ;
|
|
|
|
|
|
|
|
if (all_tests || strcmp (argv [1], "vorbis") == 0)
|
|
|
|
{ vorbis_test () ;
|
|
|
|
compression_size_test (SF_FORMAT_OGG | SF_FORMAT_VORBIS, "vorbis.oga") ;
|
|
|
|
tests ++ ;
|
|
|
|
} ;
|
|
|
|
|
|
|
|
if (all_tests || strcmp (argv [1], "flac") == 0)
|
|
|
|
{ compression_size_test (SF_FORMAT_FLAC | SF_FORMAT_PCM_16, "pcm16.flac") ;
|
|
|
|
tests ++ ;
|
|
|
|
} ;
|
2007-08-21 04:18:52 +00:00
|
|
|
|
|
|
|
return 0 ;
|
|
|
|
} /* main */
|