annotate src/libsndfile-1.0.27/tests/dither_test.c @ 127:7867fa7e1b6b

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