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