annotate src/libsndfile-1.0.27/tests/ulaw_test.c @ 84:08ae793730bd

Add null config files
author Chris Cannam
date Mon, 02 Mar 2020 14:03:47 +0000
parents 1df64224f5ac
children
rev   line source
Chris@40 1 /*
Chris@40 2 ** Copyright (C) 1999-2012 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 #include "sfconfig.h"
Chris@40 20
Chris@40 21 #include <stdio.h>
Chris@40 22 #include <stdlib.h>
Chris@40 23 #include <string.h>
Chris@40 24
Chris@40 25 #if HAVE_UNISTD_H
Chris@40 26 #include <unistd.h>
Chris@40 27 #endif
Chris@40 28
Chris@40 29 #include <sndfile.h>
Chris@40 30
Chris@40 31 #include "utils.h"
Chris@40 32
Chris@40 33 #define BUFFER_SIZE (65536)
Chris@40 34
Chris@40 35 static unsigned char ulaw_encode (int sample) ;
Chris@40 36 static int ulaw_decode (unsigned int ulawbyte) ;
Chris@40 37
Chris@40 38 static short short_buffer [BUFFER_SIZE] ;
Chris@40 39 static unsigned char ulaw_buffer [BUFFER_SIZE] ;
Chris@40 40
Chris@40 41 int
Chris@40 42 main (void)
Chris@40 43 { SNDFILE *file ;
Chris@40 44 SF_INFO sfinfo ;
Chris@40 45 const char *filename ;
Chris@40 46 int k ;
Chris@40 47
Chris@40 48 print_test_name ("ulaw_test", "encoder") ;
Chris@40 49
Chris@40 50 filename = "test.raw" ;
Chris@40 51
Chris@40 52 sf_info_setup (&sfinfo, SF_FORMAT_RAW | SF_FORMAT_ULAW, 44100, 1) ;
Chris@40 53
Chris@40 54 if ((file = sf_open (filename, SFM_WRITE, &sfinfo)) == NULL)
Chris@40 55 { printf ("sf_open_write failed with error : ") ;
Chris@40 56 fflush (stdout) ;
Chris@40 57 puts (sf_strerror (NULL)) ;
Chris@40 58 exit (1) ;
Chris@40 59 } ;
Chris@40 60
Chris@40 61 /* Generate a file containing all possible 16 bit sample values
Chris@40 62 ** and write it to disk as ulaw encoded.frames.
Chris@40 63 */
Chris@40 64
Chris@40 65 for (k = 0 ; k < 0x10000 ; k++)
Chris@40 66 short_buffer [k] = k & 0xFFFF ;
Chris@40 67
Chris@40 68 sf_write_short (file, short_buffer, BUFFER_SIZE) ;
Chris@40 69 sf_close (file) ;
Chris@40 70
Chris@40 71 /* Now open that file and compare the ulaw encoded sample values
Chris@40 72 ** with what they should be.
Chris@40 73 */
Chris@40 74
Chris@40 75 if ((file = sf_open (filename, SFM_READ, &sfinfo)) == NULL)
Chris@40 76 { printf ("sf_open_write failed with error : ") ;
Chris@40 77 puts (sf_strerror (NULL)) ;
Chris@40 78 exit (1) ;
Chris@40 79 } ;
Chris@40 80
Chris@40 81 check_log_buffer_or_die (file, __LINE__) ;
Chris@40 82
Chris@40 83 if (sf_read_raw (file, ulaw_buffer, BUFFER_SIZE) != BUFFER_SIZE)
Chris@40 84 { printf ("sf_read_raw : ") ;
Chris@40 85 puts (sf_strerror (file)) ;
Chris@40 86 exit (1) ;
Chris@40 87 } ;
Chris@40 88
Chris@40 89 for (k = 0 ; k < 0x10000 ; k++)
Chris@40 90 if (ulaw_encode (short_buffer [k]) != ulaw_buffer [k])
Chris@40 91 { printf ("Encoder error : sample #%d (0x%02X should be 0x%02X)\n", k, ulaw_buffer [k], ulaw_encode (short_buffer [k])) ;
Chris@40 92 exit (1) ;
Chris@40 93 } ;
Chris@40 94
Chris@40 95 sf_close (file) ;
Chris@40 96
Chris@40 97 puts ("ok") ;
Chris@40 98
Chris@40 99 print_test_name ("ulaw_test", "decoder") ;
Chris@40 100
Chris@40 101 /* Now generate a file containing all possible 8 bit encoded
Chris@40 102 ** sample values and write it to disk as ulaw encoded.frames.
Chris@40 103 */
Chris@40 104
Chris@40 105 if (! (file = sf_open (filename, SFM_WRITE, &sfinfo)))
Chris@40 106 { printf ("sf_open_write failed with error : ") ;
Chris@40 107 puts (sf_strerror (NULL)) ;
Chris@40 108 exit (1) ;
Chris@40 109 } ;
Chris@40 110
Chris@40 111 for (k = 0 ; k < 256 ; k++)
Chris@40 112 ulaw_buffer [k] = k & 0xFF ;
Chris@40 113
Chris@40 114 sf_write_raw (file, ulaw_buffer, 256) ;
Chris@40 115 sf_close (file) ;
Chris@40 116
Chris@40 117 /* Now open that file and compare the ulaw decoded sample values
Chris@40 118 ** with what they should be.
Chris@40 119 */
Chris@40 120
Chris@40 121 if (! (file = sf_open (filename, SFM_READ, &sfinfo)))
Chris@40 122 { printf ("sf_open_write failed with error : ") ;
Chris@40 123 puts (sf_strerror (NULL)) ;
Chris@40 124 exit (1) ;
Chris@40 125 } ;
Chris@40 126
Chris@40 127 check_log_buffer_or_die (file, __LINE__) ;
Chris@40 128
Chris@40 129 if (sf_read_short (file, short_buffer, 256) != 256)
Chris@40 130 { printf ("sf_read_short : ") ;
Chris@40 131 puts (sf_strerror (file)) ;
Chris@40 132 exit (1) ;
Chris@40 133 } ;
Chris@40 134
Chris@40 135
Chris@40 136 for (k = 0 ; k < 256 ; k++)
Chris@40 137 if (short_buffer [k] != ulaw_decode (ulaw_buffer [k]))
Chris@40 138 { printf ("Decoder error : sample #%d (0x%04X should be 0x%04X)\n", k, short_buffer [k], ulaw_decode (ulaw_buffer [k])) ;
Chris@40 139 exit (1) ;
Chris@40 140 } ;
Chris@40 141
Chris@40 142 sf_close (file) ;
Chris@40 143
Chris@40 144 puts ("ok") ;
Chris@40 145
Chris@40 146 unlink (filename) ;
Chris@40 147
Chris@40 148 return 0 ;
Chris@40 149 } /* main */
Chris@40 150
Chris@40 151
Chris@40 152 /*=================================================================================
Chris@40 153 ** The following routines came from the sox-12.15 (Sound eXcahcnge) distribution.
Chris@40 154 **
Chris@40 155 ** This code is not compiled into libsndfile. It is only used to test the
Chris@40 156 ** libsndfile lookup tables for correctness.
Chris@40 157 **
Chris@40 158 ** I have included the original authors comments.
Chris@40 159 */
Chris@40 160
Chris@40 161 /*
Chris@40 162 ** This routine converts from linear to ulaw.
Chris@40 163 **
Chris@40 164 ** Craig Reese: IDA/Supercomputing Research Center
Chris@40 165 ** Joe Campbell: Department of Defense
Chris@40 166 ** 29 September 1989
Chris@40 167 **
Chris@40 168 ** References:
Chris@40 169 ** 1) CCITT Recommendation G.711 (very difficult to follow)
Chris@40 170 ** 2) "A New Digital Technique for Implementation of Any
Chris@40 171 ** Continuous PCM Companding Law," Villeret, Michel,
Chris@40 172 ** et al. 1973 IEEE Int. Conf. on Communications, Vol 1,
Chris@40 173 ** 1973, pg. 11.12-11.17
Chris@40 174 ** 3) MIL-STD-188-113,"Interoperability and Performance Standards
Chris@40 175 ** for Analog-to_Digital Conversion Techniques,"
Chris@40 176 ** 17 February 1987
Chris@40 177 **
Chris@40 178 ** Input: Signed 16 bit linear sample
Chris@40 179 ** Output: 8 bit ulaw sample
Chris@40 180 */
Chris@40 181
Chris@40 182 #define uBIAS 0x84 /* define the add-in bias for 16 bit.frames */
Chris@40 183 #define uCLIP 32635
Chris@40 184
Chris@40 185 static
Chris@40 186 unsigned char ulaw_encode (int sample)
Chris@40 187 { static int exp_lut [256] =
Chris@40 188 { 0, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3,
Chris@40 189 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
Chris@40 190 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
Chris@40 191 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
Chris@40 192 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
Chris@40 193 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
Chris@40 194 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
Chris@40 195 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
Chris@40 196 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
Chris@40 197 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
Chris@40 198 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
Chris@40 199 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
Chris@40 200 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
Chris@40 201 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
Chris@40 202 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
Chris@40 203 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7
Chris@40 204 } ;
Chris@40 205
Chris@40 206 int sign, exponent, mantissa ;
Chris@40 207 unsigned char ulawbyte ;
Chris@40 208
Chris@40 209 /* Get the sample into sign-magnitude. */
Chris@40 210 sign = (sample >> 8) & 0x80 ; /* set aside the sign */
Chris@40 211 if (sign != 0)
Chris@40 212 sample = -sample ; /* get magnitude */
Chris@40 213 if (sample > uCLIP)
Chris@40 214 sample = uCLIP ; /* clip the magnitude */
Chris@40 215
Chris@40 216 /* Convert from 16 bit linear to ulaw. */
Chris@40 217 sample = sample + uBIAS ;
Chris@40 218 exponent = exp_lut [(sample >> 7) & 0xFF] ;
Chris@40 219 mantissa = (sample >> (exponent + 3)) & 0x0F ;
Chris@40 220 ulawbyte = ~ (sign | (exponent << 4) | mantissa) ;
Chris@40 221
Chris@40 222 return ulawbyte ;
Chris@40 223 } /* ulaw_encode */
Chris@40 224
Chris@40 225
Chris@40 226 /*
Chris@40 227 ** This routine converts from ulaw to 16 bit linear.
Chris@40 228 **
Chris@40 229 ** Craig Reese: IDA/Supercomputing Research Center
Chris@40 230 ** 29 September 1989
Chris@40 231 **
Chris@40 232 ** References:
Chris@40 233 ** 1) CCITT Recommendation G.711 (very difficult to follow)
Chris@40 234 ** 2) MIL-STD-188-113,"Interoperability and Performance Standards
Chris@40 235 ** for Analog-to_Digital Conversion Techniques,"
Chris@40 236 ** 17 February 1987
Chris@40 237 **
Chris@40 238 ** Input: 8 bit ulaw sample
Chris@40 239 ** Output: signed 16 bit linear sample
Chris@40 240 */
Chris@40 241
Chris@40 242 static
Chris@40 243 int ulaw_decode (unsigned int ulawbyte)
Chris@40 244 { static int exp_lut [8] = { 0, 132, 396, 924, 1980, 4092, 8316, 16764 } ;
Chris@40 245 int sign, exponent, mantissa, sample ;
Chris@40 246
Chris@40 247 ulawbyte = ~ ulawbyte ;
Chris@40 248 sign = (ulawbyte & 0x80) ;
Chris@40 249 exponent = (ulawbyte >> 4) & 0x07 ;
Chris@40 250 mantissa = ulawbyte & 0x0F ;
Chris@40 251 sample = exp_lut [exponent] + (mantissa << (exponent + 3)) ;
Chris@40 252 if (sign != 0)
Chris@40 253 sample = -sample ;
Chris@40 254
Chris@40 255 return sample ;
Chris@40 256 } /* ulaw_decode */
Chris@40 257