r663: This commit was generated by cvs2svn to compensate for changes in r662,
[cinelerra_cv.git] / libsndfile / tests / alaw_test.c
blob5fb22a9f7be43448c2bb2e4b257a57ca5ba1ec41
1 /*
2 ** Copyright (C) 1999-2003 Erik de Castro Lopo <erikd@zip.com.au>
3 **
4 ** This program is free software; you can redistribute it and/or modify
5 ** it under the terms of the GNU General Public License as published by
6 ** the Free Software Foundation; either version 2 of the License, or
7 ** (at your option) any later version.
8 **
9 ** This program is distributed in the hope that it will be useful,
10 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 ** GNU General Public License for more details.
13 **
14 ** You should have received a copy of the GNU General Public License
15 ** along with this program; if not, write to the Free Software
16 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 #include <stdio.h>
22 #include <unistd.h>
24 #include <sndfile.h>
26 #include "utils.h"
28 #define BUFFER_SIZE (65536)
30 static unsigned char alaw_encode (int sample) ;
31 static int alaw_decode (unsigned int alawbyte) ;
33 static short short_buffer [BUFFER_SIZE] ;
34 static unsigned char alaw_buffer [BUFFER_SIZE] ;
36 int
37 main (void)
38 { SNDFILE *file ;
39 SF_INFO sfinfo ;
40 char *filename ;
41 int k ;
43 filename = "test.raw" ;
45 sfinfo.format = SF_FORMAT_RAW | SF_FORMAT_ALAW ;
46 sfinfo.samplerate = 44100 ;
47 sfinfo.frames = 123456789 ; /* Wrong length. Library should correct this on sf_close. */
48 sfinfo.channels = 1 ;
50 if (! (file = sf_open (filename, SFM_WRITE, &sfinfo)))
51 { printf ("sf_open_write failed with error : ") ;
52 fflush (stdout) ;
53 puts (sf_strerror (NULL)) ;
54 exit (1) ;
55 } ;
57 /* Generate a file containing all possible 16 bit sample values
58 ** and write it to disk as alaw encoded.frames.
61 for (k = 0 ; k < 0x10000 ; k++)
62 short_buffer [k] = k & 0xFFFF ;
64 sf_write_short (file, short_buffer, BUFFER_SIZE) ;
65 sf_close (file) ;
67 /* Now open that file and compare the alaw encoded sample values
68 ** with what they should be.
71 if (! (file = sf_open (filename, SFM_READ, &sfinfo)))
72 { printf ("sf_open_write failed with error : ") ;
73 puts (sf_strerror (NULL)) ;
74 exit (1) ;
75 } ;
77 check_log_buffer_or_die (file) ;
79 if (sf_read_raw (file, alaw_buffer, BUFFER_SIZE) != BUFFER_SIZE)
80 { printf ("sf_read_raw : ") ;
81 puts (sf_strerror (file)) ;
82 exit (1) ;
83 } ;
85 for (k = 0 ; k < 0x10000 ; k++)
86 if (alaw_encode (short_buffer [k]) != alaw_buffer [k])
87 { printf ("Encoder error : sample #%d (0x%02X should be 0x%02X)\n", k, alaw_buffer [k], alaw_encode (short_buffer [k])) ;
88 exit (1) ;
89 } ;
91 sf_close (file) ;
93 printf (" alaw_test : encoder ... ok\n") ;
95 /* Now generate a file containing all possible 8 bit encoded
96 ** sample values and write it to disk as alaw encoded.frames.
99 if (! (file = sf_open (filename, SFM_WRITE, &sfinfo)))
100 { printf ("sf_open_write failed with error : ") ;
101 puts (sf_strerror (NULL)) ;
102 exit (1) ;
105 for (k = 0 ; k < 256 ; k++)
106 alaw_buffer [k] = k & 0xFF ;
108 sf_write_raw (file, alaw_buffer, 256) ;
109 sf_close (file) ;
111 /* Now open that file and compare the alaw decoded sample values
112 ** with what they should be.
115 if (! (file = sf_open (filename, SFM_READ, &sfinfo)))
116 { printf ("sf_open_write failed with error : ") ;
117 puts (sf_strerror (NULL)) ;
118 exit (1) ;
121 check_log_buffer_or_die (file) ;
123 if (sf_read_short (file, short_buffer, 256) != 256)
124 { printf ("sf_read_short : ") ;
125 puts (sf_strerror (file)) ;
126 exit (1) ;
130 for (k = 0 ; k < 256 ; k++)
131 if (short_buffer [k] != alaw_decode (alaw_buffer [k]))
132 { printf ("Decoder error : sample #%d (0x%02X should be 0x%02X)\n", k, short_buffer [k], alaw_decode (alaw_buffer [k])) ;
133 exit (1) ;
136 sf_close (file) ;
138 printf (" alaw_test : decoder ... ok\n") ;
140 unlink (filename) ;
142 return 0 ;
143 } /* main */
146 /*=================================================================================
147 ** The following routines came from the sox-12.15 (Sound eXcahcnge) distribution.
149 ** This code is not compiled into libsndfile. It is only used to test the
150 ** libsndfile lookup tables for correctness.
152 ** I have included the original authors comments.
156 ** A-law routines by Graeme W. Gill.
157 ** Date: 93/5/7
159 ** References:
160 ** 1) CCITT Recommendation G.711
164 #define ACLIP 31744
166 static
167 unsigned char alaw_encode (int sample)
168 { static int exp_lut[128] = {1,1,2,2,3,3,3,3,
169 4,4,4,4,4,4,4,4,
170 5,5,5,5,5,5,5,5,
171 5,5,5,5,5,5,5,5,
172 6,6,6,6,6,6,6,6,
173 6,6,6,6,6,6,6,6,
174 6,6,6,6,6,6,6,6,
175 6,6,6,6,6,6,6,6,
176 7,7,7,7,7,7,7,7,
177 7,7,7,7,7,7,7,7,
178 7,7,7,7,7,7,7,7,
179 7,7,7,7,7,7,7,7,
180 7,7,7,7,7,7,7,7,
181 7,7,7,7,7,7,7,7,
182 7,7,7,7,7,7,7,7,
183 7,7,7,7,7,7,7,7};
185 int sign, exponent, mantissa;
186 unsigned char Alawbyte;
188 /* Get the sample into sign-magnitude. */
189 sign = ((~sample) >> 8) & 0x80 ; /* set aside the sign */
190 if (sign == 0)
191 sample = -sample ; /* get magnitude */
192 if (sample > ACLIP)
193 sample = ACLIP ; /* clip the magnitude */
195 /* Convert from 16 bit linear to ulaw. */
196 if (sample >= 256)
197 { exponent = exp_lut [(sample >> 8) & 0x7F] ;
198 mantissa = ( sample >> ( exponent + 3 ) ) & 0x0F ;
199 Alawbyte = ((exponent << 4) | mantissa) ;
201 else
202 Alawbyte = (sample >> 4) ;
204 Alawbyte ^= (sign ^ 0x55) ;
206 return Alawbyte ;
207 } /* alaw_encode */
209 static
210 int alaw_decode (unsigned int Alawbyte)
211 { static int exp_lut[8] = { 0, 264, 528, 1056, 2112, 4224, 8448, 16896 } ;
212 int sign, exponent, mantissa, sample ;
214 Alawbyte ^= 0x55 ;
215 sign = ( Alawbyte & 0x80 ) ;
216 Alawbyte &= 0x7f ; /* get magnitude */
217 if (Alawbyte >= 16)
218 { exponent = (Alawbyte >> 4 ) & 0x07 ;
219 mantissa = Alawbyte & 0x0F ;
220 sample = exp_lut[exponent] + (mantissa << ( exponent + 3 )) ;
222 else
223 sample = (Alawbyte << 4) + 8 ;
224 if (sign == 0)
225 sample = -sample ;
227 return sample ;
228 } /* alaw_decode */