annotate src/libsndfile-1.0.25/tests/dither_test.c @ 85:545efbb81310

Import initial set of sources
author Chris Cannam <cannam@all-day-breakfast.com>
date Mon, 18 Mar 2013 14:12:14 +0000
parents
children
rev   line source
cannam@85 1 /*
cannam@85 2 ** Copyright (C) 2003-2011 Erik de Castro Lopo <erikd@mega-nerd.com>
cannam@85 3 **
cannam@85 4 ** This program is free software; you can redistribute it and/or modify
cannam@85 5 ** it under the terms of the GNU General Public License as published by
cannam@85 6 ** the Free Software Foundation; either version 2 of the License, or
cannam@85 7 ** (at your option) any later version.
cannam@85 8 **
cannam@85 9 ** This program is distributed in the hope that it will be useful,
cannam@85 10 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
cannam@85 11 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
cannam@85 12 ** GNU General Public License for more details.
cannam@85 13 **
cannam@85 14 ** You should have received a copy of the GNU General Public License
cannam@85 15 ** along with this program; if not, write to the Free Software
cannam@85 16 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
cannam@85 17 */
cannam@85 18
cannam@85 19
cannam@85 20
cannam@85 21 #include <stdio.h>
cannam@85 22 #include <stdlib.h>
cannam@85 23 #include <unistd.h>
cannam@85 24 #include <string.h>
cannam@85 25 #include <math.h>
cannam@85 26
cannam@85 27 #include <sndfile.h>
cannam@85 28
cannam@85 29 #include "utils.h"
cannam@85 30
cannam@85 31 #define BUFFER_LEN (1<<16)
cannam@85 32 #define LOG_BUFFER_SIZE 1024
cannam@85 33
cannam@85 34 static void dither_test (const char *filename, int filetype) ;
cannam@85 35
cannam@85 36 /* Force the start of this buffer to be double aligned. Sparc-solaris will
cannam@85 37 ** choke if its not.
cannam@85 38 */
cannam@85 39 static short data_out [BUFFER_LEN] ;
cannam@85 40
cannam@85 41 int
cannam@85 42 main (int argc, char *argv [])
cannam@85 43 { int do_all = 0 ;
cannam@85 44 int test_count = 0 ;
cannam@85 45
cannam@85 46 if (argc != 2)
cannam@85 47 { printf ("Usage : %s <test>\n", argv [0]) ;
cannam@85 48 printf (" Where <test> is one of the following:\n") ;
cannam@85 49 printf (" wav - test WAV file peak chunk\n") ;
cannam@85 50 printf (" aiff - test AIFF file PEAK chunk\n") ;
cannam@85 51 printf (" all - perform all tests\n") ;
cannam@85 52 exit (1) ;
cannam@85 53 } ;
cannam@85 54
cannam@85 55 do_all=!strcmp (argv [1], "all") ;
cannam@85 56
cannam@85 57 if (do_all || ! strcmp (argv [1], "wav"))
cannam@85 58 { dither_test ("dither.wav", SF_FORMAT_WAV | SF_FORMAT_PCM_U8) ;
cannam@85 59 test_count++ ;
cannam@85 60 } ;
cannam@85 61
cannam@85 62 if (do_all || ! strcmp (argv [1], "aiff"))
cannam@85 63 { dither_test ("dither.aiff", SF_FORMAT_AIFF | SF_FORMAT_PCM_S8) ;
cannam@85 64 test_count++ ;
cannam@85 65 } ;
cannam@85 66
cannam@85 67 if (do_all || ! strcmp (argv [1], "au"))
cannam@85 68 { dither_test ("dither.au", SF_FORMAT_AU | SF_FORMAT_PCM_S8) ;
cannam@85 69 test_count++ ;
cannam@85 70 } ;
cannam@85 71
cannam@85 72 if (do_all || ! strcmp (argv [1], "svx"))
cannam@85 73 { dither_test ("dither.svx", SF_FORMAT_SVX | SF_FORMAT_PCM_S8) ;
cannam@85 74 test_count++ ;
cannam@85 75 } ;
cannam@85 76
cannam@85 77 if (do_all || ! strcmp (argv [1], "nist"))
cannam@85 78 { dither_test ("dither.nist", SF_FORMAT_NIST | SF_FORMAT_PCM_S8) ;
cannam@85 79 test_count++ ;
cannam@85 80 } ;
cannam@85 81
cannam@85 82 if (do_all || ! strcmp (argv [1], "paf"))
cannam@85 83 { dither_test ("dither.paf", SF_FORMAT_PAF | SF_FORMAT_PCM_S8) ;
cannam@85 84 test_count++ ;
cannam@85 85 } ;
cannam@85 86
cannam@85 87 if (do_all || ! strcmp (argv [1], "ircam"))
cannam@85 88 { dither_test ("dither.ircam", SF_FORMAT_IRCAM | SF_FORMAT_PCM_S8) ;
cannam@85 89 test_count++ ;
cannam@85 90 } ;
cannam@85 91
cannam@85 92 if (do_all || ! strcmp (argv [1], "voc"))
cannam@85 93 { dither_test ("dither.voc", SF_FORMAT_VOC | SF_FORMAT_PCM_S8) ;
cannam@85 94 test_count++ ;
cannam@85 95 } ;
cannam@85 96
cannam@85 97 if (do_all || ! strcmp (argv [1], "w64"))
cannam@85 98 { dither_test ("dither.w64", SF_FORMAT_W64 | SF_FORMAT_PCM_S8) ;
cannam@85 99 test_count++ ;
cannam@85 100 } ;
cannam@85 101
cannam@85 102 if (do_all || ! strcmp (argv [1], "mat4"))
cannam@85 103 { dither_test ("dither.mat4", SF_FORMAT_MAT4 | SF_FORMAT_PCM_S8) ;
cannam@85 104 test_count++ ;
cannam@85 105 } ;
cannam@85 106
cannam@85 107 if (do_all || ! strcmp (argv [1], "mat5"))
cannam@85 108 { dither_test ("dither.mat5", SF_FORMAT_MAT5 | SF_FORMAT_PCM_S8) ;
cannam@85 109 test_count++ ;
cannam@85 110 } ;
cannam@85 111
cannam@85 112 if (do_all || ! strcmp (argv [1], "pvf"))
cannam@85 113 { dither_test ("dither.pvf", SF_FORMAT_PVF | SF_FORMAT_PCM_S8) ;
cannam@85 114 test_count++ ;
cannam@85 115 } ;
cannam@85 116
cannam@85 117 if (test_count == 0)
cannam@85 118 { printf ("Mono : ************************************\n") ;
cannam@85 119 printf ("Mono : * No '%s' test defined.\n", argv [1]) ;
cannam@85 120 printf ("Mono : ************************************\n") ;
cannam@85 121 return 1 ;
cannam@85 122 } ;
cannam@85 123
cannam@85 124 return 0 ;
cannam@85 125 } /* main */
cannam@85 126
cannam@85 127
cannam@85 128 /*============================================================================================
cannam@85 129 ** Here are the test functions.
cannam@85 130 */
cannam@85 131
cannam@85 132 static void
cannam@85 133 dither_test (const char *filename, int filetype)
cannam@85 134 { SNDFILE *file ;
cannam@85 135 SF_INFO sfinfo ;
cannam@85 136 SF_DITHER_INFO dither ;
cannam@85 137
cannam@85 138 filetype = filetype ;
cannam@85 139
cannam@85 140 print_test_name ("dither_test", filename) ;
cannam@85 141
cannam@85 142 sfinfo.samplerate = 44100 ;
cannam@85 143 sfinfo.format = filetype ;
cannam@85 144 sfinfo.channels = 1 ;
cannam@85 145 sfinfo.frames = 0 ;
cannam@85 146
cannam@85 147 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
cannam@85 148
cannam@85 149 /* Check for old version of the dither API. */
cannam@85 150 if (sf_command (file, SFC_SET_DITHER_ON_WRITE, NULL, SF_TRUE) == 0)
cannam@85 151 { printf ("\n\nLine %d: Should have an error here but don't.\n\n", __LINE__) ;
cannam@85 152 exit (1) ;
cannam@85 153 } ;
cannam@85 154
cannam@85 155 memset (&dither, 0, sizeof (dither)) ;
cannam@85 156 dither.type = SFD_WHITE ;
cannam@85 157 dither.level = 0 ;
cannam@85 158
cannam@85 159 if (sf_command (file, SFC_SET_DITHER_ON_WRITE, &dither, sizeof (dither)) != 0)
cannam@85 160 { printf ("\n\nLine %d: sf_command (SFC_SET_DITHER_ON_WRITE) returned error : %s\n\n",
cannam@85 161 __LINE__, sf_strerror (file)) ;
cannam@85 162 exit (1) ;
cannam@85 163 } ;
cannam@85 164
cannam@85 165 /* Write data to file. */
cannam@85 166 test_write_short_or_die (file, 0, data_out, BUFFER_LEN, __LINE__) ;
cannam@85 167 test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
cannam@85 168
cannam@85 169 sf_close (file) ;
cannam@85 170
cannam@85 171 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
cannam@85 172
cannam@85 173 if (sfinfo.frames != BUFFER_LEN)
cannam@85 174 { printf ("\n\nLine %d: Bad frame count %d (should be %d)\n\n", __LINE__, (int) sfinfo.frames, BUFFER_LEN) ;
cannam@85 175 } ;
cannam@85 176
cannam@85 177 sf_close (file) ;
cannam@85 178 /*-unlink (filename) ;-*/
cannam@85 179
cannam@85 180 puts ("ok") ;
cannam@85 181 } /* dither_test */
cannam@85 182