annotate src/libsndfile-1.0.25/tests/alaw_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 alaw_encode (int sample) ;
cannam@85 36 static int alaw_decode (unsigned int alawbyte) ;
cannam@85 37
cannam@85 38 static short short_buffer [BUFFER_SIZE] ;
cannam@85 39 static unsigned char alaw_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 ("alaw_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_ALAW, 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 alaw 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 alaw 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, alaw_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 (alaw_encode (short_buffer [k]) != alaw_buffer [k])
cannam@85 91 { printf ("Encoder error : sample #%d (0x%02X should be 0x%02X)\n", k, alaw_buffer [k], alaw_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 ("alaw_test", "decoder") ;
cannam@85 100 /* Now generate a file containing all possible 8 bit encoded
cannam@85 101 ** sample values and write it to disk as alaw encoded.frames.
cannam@85 102 */
cannam@85 103
cannam@85 104 if (! (file = sf_open (filename, SFM_WRITE, &sfinfo)))
cannam@85 105 { printf ("sf_open_write failed with error : ") ;
cannam@85 106 puts (sf_strerror (NULL)) ;
cannam@85 107 exit (1) ;
cannam@85 108 } ;
cannam@85 109
cannam@85 110 for (k = 0 ; k < 256 ; k++)
cannam@85 111 alaw_buffer [k] = k & 0xFF ;
cannam@85 112
cannam@85 113 sf_write_raw (file, alaw_buffer, 256) ;
cannam@85 114 sf_close (file) ;
cannam@85 115
cannam@85 116 /* Now open that file and compare the alaw decoded sample values
cannam@85 117 ** with what they should be.
cannam@85 118 */
cannam@85 119
cannam@85 120 if (! (file = sf_open (filename, SFM_READ, &sfinfo)))
cannam@85 121 { printf ("sf_open_write failed with error : ") ;
cannam@85 122 puts (sf_strerror (NULL)) ;
cannam@85 123 exit (1) ;
cannam@85 124 } ;
cannam@85 125
cannam@85 126 check_log_buffer_or_die (file, __LINE__) ;
cannam@85 127
cannam@85 128 if (sf_read_short (file, short_buffer, 256) != 256)
cannam@85 129 { printf ("sf_read_short : ") ;
cannam@85 130 puts (sf_strerror (file)) ;
cannam@85 131 exit (1) ;
cannam@85 132 } ;
cannam@85 133
cannam@85 134
cannam@85 135 for (k = 0 ; k < 256 ; k++)
cannam@85 136 if (short_buffer [k] != alaw_decode (alaw_buffer [k]))
cannam@85 137 { printf ("Decoder error : sample #%d (0x%02X should be 0x%02X)\n", k, short_buffer [k], alaw_decode (alaw_buffer [k])) ;
cannam@85 138 exit (1) ;
cannam@85 139 } ;
cannam@85 140
cannam@85 141 sf_close (file) ;
cannam@85 142
cannam@85 143 puts ("ok") ;
cannam@85 144
cannam@85 145 unlink (filename) ;
cannam@85 146
cannam@85 147 return 0 ;
cannam@85 148 } /* main */
cannam@85 149
cannam@85 150
cannam@85 151 /*=================================================================================
cannam@85 152 ** The following routines came from the sox-12.15 (Sound eXcahcnge) distribution.
cannam@85 153 **
cannam@85 154 ** This code is not compiled into libsndfile. It is only used to test the
cannam@85 155 ** libsndfile lookup tables for correctness.
cannam@85 156 **
cannam@85 157 ** I have included the original authors comments.
cannam@85 158 */
cannam@85 159
cannam@85 160 /*
cannam@85 161 ** A-law routines by Graeme W. Gill.
cannam@85 162 ** Date: 93/5/7
cannam@85 163 **
cannam@85 164 ** References:
cannam@85 165 ** 1) CCITT Recommendation G.711
cannam@85 166 **
cannam@85 167 */
cannam@85 168
cannam@85 169 #define ACLIP 31744
cannam@85 170
cannam@85 171 static
cannam@85 172 unsigned char alaw_encode (int sample)
cannam@85 173 { static int exp_lut [128] =
cannam@85 174 { 1, 1, 2, 2, 3, 3, 3, 3,
cannam@85 175 4, 4, 4, 4, 4, 4, 4, 4,
cannam@85 176 5, 5, 5, 5, 5, 5, 5, 5,
cannam@85 177 5, 5, 5, 5, 5, 5, 5, 5,
cannam@85 178 6, 6, 6, 6, 6, 6, 6, 6,
cannam@85 179 6, 6, 6, 6, 6, 6, 6, 6,
cannam@85 180 6, 6, 6, 6, 6, 6, 6, 6,
cannam@85 181 6, 6, 6, 6, 6, 6, 6, 6,
cannam@85 182 7, 7, 7, 7, 7, 7, 7, 7,
cannam@85 183 7, 7, 7, 7, 7, 7, 7, 7,
cannam@85 184 7, 7, 7, 7, 7, 7, 7, 7,
cannam@85 185 7, 7, 7, 7, 7, 7, 7, 7,
cannam@85 186 7, 7, 7, 7, 7, 7, 7, 7,
cannam@85 187 7, 7, 7, 7, 7, 7, 7, 7,
cannam@85 188 7, 7, 7, 7, 7, 7, 7, 7,
cannam@85 189 7, 7, 7, 7, 7, 7, 7, 7
cannam@85 190 } ;
cannam@85 191
cannam@85 192 int sign, exponent, mantissa ;
cannam@85 193 unsigned char Alawbyte ;
cannam@85 194
cannam@85 195 /* Get the sample into sign-magnitude. */
cannam@85 196 sign = ((~sample) >> 8) & 0x80 ; /* set aside the sign */
cannam@85 197 if (sign == 0)
cannam@85 198 sample = -sample ; /* get magnitude */
cannam@85 199 if (sample > ACLIP)
cannam@85 200 sample = ACLIP ; /* clip the magnitude */
cannam@85 201
cannam@85 202 /* Convert from 16 bit linear to ulaw. */
cannam@85 203 if (sample >= 256)
cannam@85 204 { exponent = exp_lut [(sample >> 8) & 0x7F] ;
cannam@85 205 mantissa = ( sample >> ( exponent + 3 ) ) & 0x0F ;
cannam@85 206 Alawbyte = ((exponent << 4) | mantissa) ;
cannam@85 207 }
cannam@85 208 else
cannam@85 209 Alawbyte = (sample >> 4) ;
cannam@85 210
cannam@85 211 Alawbyte ^= (sign ^ 0x55) ;
cannam@85 212
cannam@85 213 return Alawbyte ;
cannam@85 214 } /* alaw_encode */
cannam@85 215
cannam@85 216 static
cannam@85 217 int alaw_decode (unsigned int Alawbyte)
cannam@85 218 { static int exp_lut [8] = { 0, 264, 528, 1056, 2112, 4224, 8448, 16896 } ;
cannam@85 219 int sign, exponent, mantissa, sample ;
cannam@85 220
cannam@85 221 Alawbyte ^= 0x55 ;
cannam@85 222 sign = (Alawbyte & 0x80) ;
cannam@85 223 Alawbyte &= 0x7f ; /* get magnitude */
cannam@85 224 if (Alawbyte >= 16)
cannam@85 225 { exponent = (Alawbyte >> 4 ) & 0x07 ;
cannam@85 226 mantissa = Alawbyte & 0x0F ;
cannam@85 227 sample = exp_lut [exponent] + (mantissa << ( exponent + 3 )) ;
cannam@85 228 }
cannam@85 229 else
cannam@85 230 sample = (Alawbyte << 4) + 8 ;
cannam@85 231 if (sign == 0)
cannam@85 232 sample = -sample ;
cannam@85 233
cannam@85 234 return sample ;
cannam@85 235 } /* alaw_decode */
cannam@85 236