annotate src/libsndfile-1.0.27/tests/dither_test.c @ 40:1df64224f5ac

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