annotate src/libsndfile-1.0.25/tests/dither_test.c @ 0:c7265573341e

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