Editing and cleaning of Makefiles of all levels.
[Faustine.git] / interpretor / lib / src / libsndfile-1.0.25 / tests / alaw_test.c
1 /*
2 ** Copyright (C) 1999-2011 Erik de Castro Lopo <erikd@mega-nerd.com>
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.
17 */
18
19 #include "sfconfig.h"
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24
25 #if HAVE_UNISTD_H
26 #include <unistd.h>
27 #endif
28
29 #include <sndfile.h>
30
31 #include "utils.h"
32
33 #define BUFFER_SIZE (65536)
34
35 static unsigned char alaw_encode (int sample) ;
36 static int alaw_decode (unsigned int alawbyte) ;
37
38 static short short_buffer [BUFFER_SIZE] ;
39 static unsigned char alaw_buffer [BUFFER_SIZE] ;
40
41 int
42 main (void)
43 { SNDFILE *file ;
44 SF_INFO sfinfo ;
45 const char *filename ;
46 int k ;
47
48 print_test_name ("alaw_test", "encoder") ;
49
50 filename = "test.raw" ;
51
52 sf_info_setup (&sfinfo, SF_FORMAT_RAW | SF_FORMAT_ALAW, 44100, 1) ;
53
54 if ((file = sf_open (filename, SFM_WRITE, &sfinfo)) == NULL)
55 { printf ("sf_open_write failed with error : ") ;
56 fflush (stdout) ;
57 puts (sf_strerror (NULL)) ;
58 exit (1) ;
59 } ;
60
61 /* Generate a file containing all possible 16 bit sample values
62 ** and write it to disk as alaw encoded.frames.
63 */
64
65 for (k = 0 ; k < 0x10000 ; k++)
66 short_buffer [k] = k & 0xFFFF ;
67
68 sf_write_short (file, short_buffer, BUFFER_SIZE) ;
69 sf_close (file) ;
70
71 /* Now open that file and compare the alaw encoded sample values
72 ** with what they should be.
73 */
74
75 if ((file = sf_open (filename, SFM_READ, &sfinfo)) == NULL)
76 { printf ("sf_open_write failed with error : ") ;
77 puts (sf_strerror (NULL)) ;
78 exit (1) ;
79 } ;
80
81 check_log_buffer_or_die (file, __LINE__) ;
82
83 if (sf_read_raw (file, alaw_buffer, BUFFER_SIZE) != BUFFER_SIZE)
84 { printf ("sf_read_raw : ") ;
85 puts (sf_strerror (file)) ;
86 exit (1) ;
87 } ;
88
89 for (k = 0 ; k < 0x10000 ; k++)
90 if (alaw_encode (short_buffer [k]) != alaw_buffer [k])
91 { printf ("Encoder error : sample #%d (0x%02X should be 0x%02X)\n", k, alaw_buffer [k], alaw_encode (short_buffer [k])) ;
92 exit (1) ;
93 } ;
94
95 sf_close (file) ;
96
97 puts ("ok") ;
98
99 print_test_name ("alaw_test", "decoder") ;
100 /* Now generate a file containing all possible 8 bit encoded
101 ** sample values and write it to disk as alaw encoded.frames.
102 */
103
104 if (! (file = sf_open (filename, SFM_WRITE, &sfinfo)))
105 { printf ("sf_open_write failed with error : ") ;
106 puts (sf_strerror (NULL)) ;
107 exit (1) ;
108 } ;
109
110 for (k = 0 ; k < 256 ; k++)
111 alaw_buffer [k] = k & 0xFF ;
112
113 sf_write_raw (file, alaw_buffer, 256) ;
114 sf_close (file) ;
115
116 /* Now open that file and compare the alaw decoded sample values
117 ** with what they should be.
118 */
119
120 if (! (file = sf_open (filename, SFM_READ, &sfinfo)))
121 { printf ("sf_open_write failed with error : ") ;
122 puts (sf_strerror (NULL)) ;
123 exit (1) ;
124 } ;
125
126 check_log_buffer_or_die (file, __LINE__) ;
127
128 if (sf_read_short (file, short_buffer, 256) != 256)
129 { printf ("sf_read_short : ") ;
130 puts (sf_strerror (file)) ;
131 exit (1) ;
132 } ;
133
134
135 for (k = 0 ; k < 256 ; k++)
136 if (short_buffer [k] != alaw_decode (alaw_buffer [k]))
137 { printf ("Decoder error : sample #%d (0x%02X should be 0x%02X)\n", k, short_buffer [k], alaw_decode (alaw_buffer [k])) ;
138 exit (1) ;
139 } ;
140
141 sf_close (file) ;
142
143 puts ("ok") ;
144
145 unlink (filename) ;
146
147 return 0 ;
148 } /* main */
149
150
151 /*=================================================================================
152 ** The following routines came from the sox-12.15 (Sound eXcahcnge) distribution.
153 **
154 ** This code is not compiled into libsndfile. It is only used to test the
155 ** libsndfile lookup tables for correctness.
156 **
157 ** I have included the original authors comments.
158 */
159
160 /*
161 ** A-law routines by Graeme W. Gill.
162 ** Date: 93/5/7
163 **
164 ** References:
165 ** 1) CCITT Recommendation G.711
166 **
167 */
168
169 #define ACLIP 31744
170
171 static
172 unsigned char alaw_encode (int sample)
173 { static int exp_lut [128] =
174 { 1, 1, 2, 2, 3, 3, 3, 3,
175 4, 4, 4, 4, 4, 4, 4, 4,
176 5, 5, 5, 5, 5, 5, 5, 5,
177 5, 5, 5, 5, 5, 5, 5, 5,
178 6, 6, 6, 6, 6, 6, 6, 6,
179 6, 6, 6, 6, 6, 6, 6, 6,
180 6, 6, 6, 6, 6, 6, 6, 6,
181 6, 6, 6, 6, 6, 6, 6, 6,
182 7, 7, 7, 7, 7, 7, 7, 7,
183 7, 7, 7, 7, 7, 7, 7, 7,
184 7, 7, 7, 7, 7, 7, 7, 7,
185 7, 7, 7, 7, 7, 7, 7, 7,
186 7, 7, 7, 7, 7, 7, 7, 7,
187 7, 7, 7, 7, 7, 7, 7, 7,
188 7, 7, 7, 7, 7, 7, 7, 7,
189 7, 7, 7, 7, 7, 7, 7, 7
190 } ;
191
192 int sign, exponent, mantissa ;
193 unsigned char Alawbyte ;
194
195 /* Get the sample into sign-magnitude. */
196 sign = ((~sample) >> 8) & 0x80 ; /* set aside the sign */
197 if (sign == 0)
198 sample = -sample ; /* get magnitude */
199 if (sample > ACLIP)
200 sample = ACLIP ; /* clip the magnitude */
201
202 /* Convert from 16 bit linear to ulaw. */
203 if (sample >= 256)
204 { exponent = exp_lut [(sample >> 8) & 0x7F] ;
205 mantissa = ( sample >> ( exponent + 3 ) ) & 0x0F ;
206 Alawbyte = ((exponent << 4) | mantissa) ;
207 }
208 else
209 Alawbyte = (sample >> 4) ;
210
211 Alawbyte ^= (sign ^ 0x55) ;
212
213 return Alawbyte ;
214 } /* alaw_encode */
215
216 static
217 int alaw_decode (unsigned int Alawbyte)
218 { static int exp_lut [8] = { 0, 264, 528, 1056, 2112, 4224, 8448, 16896 } ;
219 int sign, exponent, mantissa, sample ;
220
221 Alawbyte ^= 0x55 ;
222 sign = (Alawbyte & 0x80) ;
223 Alawbyte &= 0x7f ; /* get magnitude */
224 if (Alawbyte >= 16)
225 { exponent = (Alawbyte >> 4 ) & 0x07 ;
226 mantissa = Alawbyte & 0x0F ;
227 sample = exp_lut [exponent] + (mantissa << ( exponent + 3 )) ;
228 }
229 else
230 sample = (Alawbyte << 4) + 8 ;
231 if (sign == 0)
232 sample = -sample ;
233
234 return sample ;
235 } /* alaw_decode */
236