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