annotate src/libsndfile-1.0.25/tests/ulaw_test.c @ 124:e3d5853d5918

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