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