c2644f10ea1a0a965f6aacd6522014e5dd67b390
2 ** Copyright (C) 1999-2011 Erik de Castro Lopo <erikd@mega-nerd.com>
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.
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.
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.
33 #define BUFFER_SIZE (65536)
35 static unsigned char ulaw_encode (int sample
) ;
36 static int ulaw_decode (unsigned int ulawbyte
) ;
38 static short short_buffer
[BUFFER_SIZE
] ;
39 static unsigned char ulaw_buffer
[BUFFER_SIZE
] ;
45 const char *filename
;
48 print_test_name ("ulaw_test", "encoder") ;
50 filename
= "test.raw" ;
52 sf_info_setup (&sfinfo
, SF_FORMAT_RAW
| SF_FORMAT_ULAW
, 44100, 1) ;
54 if ((file
= sf_open (filename
, SFM_WRITE
, &sfinfo
)) == NULL
)
55 { printf ("sf_open_write failed with error : ") ;
57 puts (sf_strerror (NULL
)) ;
61 /* Generate a file containing all possible 16 bit sample values
62 ** and write it to disk as ulaw encoded.frames.
65 for (k
= 0 ; k
< 0x10000 ; k
++)
66 short_buffer
[k
] = k
& 0xFFFF ;
68 sf_write_short (file
, short_buffer
, BUFFER_SIZE
) ;
71 /* Now open that file and compare the ulaw encoded sample values
72 ** with what they should be.
75 if ((file
= sf_open (filename
, SFM_READ
, &sfinfo
)) == NULL
)
76 { printf ("sf_open_write failed with error : ") ;
77 puts (sf_strerror (NULL
)) ;
81 check_log_buffer_or_die (file
, __LINE__
) ;
83 if (sf_read_raw (file
, ulaw_buffer
, BUFFER_SIZE
) != BUFFER_SIZE
)
84 { printf ("sf_read_raw : ") ;
85 puts (sf_strerror (file
)) ;
89 for (k
= 0 ; k
< 0x10000 ; k
++)
90 if (ulaw_encode (short_buffer
[k
]) != ulaw_buffer
[k
])
91 { printf ("Encoder error : sample #%d (0x%02X should be 0x%02X)\n", k
, ulaw_buffer
[k
], ulaw_encode (short_buffer
[k
])) ;
99 print_test_name ("ulaw_test", "decoder") ;
101 /* Now generate a file containing all possible 8 bit encoded
102 ** sample values and write it to disk as ulaw encoded.frames.
105 if (! (file
= sf_open (filename
, SFM_WRITE
, &sfinfo
)))
106 { printf ("sf_open_write failed with error : ") ;
107 puts (sf_strerror (NULL
)) ;
111 for (k
= 0 ; k
< 256 ; k
++)
112 ulaw_buffer
[k
] = k
& 0xFF ;
114 sf_write_raw (file
, ulaw_buffer
, 256) ;
117 /* Now open that file and compare the ulaw decoded sample values
118 ** with what they should be.
121 if (! (file
= sf_open (filename
, SFM_READ
, &sfinfo
)))
122 { printf ("sf_open_write failed with error : ") ;
123 puts (sf_strerror (NULL
)) ;
127 check_log_buffer_or_die (file
, __LINE__
) ;
129 if (sf_read_short (file
, short_buffer
, 256) != 256)
130 { printf ("sf_read_short : ") ;
131 puts (sf_strerror (file
)) ;
136 for (k
= 0 ; k
< 256 ; k
++)
137 if (short_buffer
[k
] != ulaw_decode (ulaw_buffer
[k
]))
138 { printf ("Decoder error : sample #%d (0x%04X should be 0x%04X)\n", k
, short_buffer
[k
], ulaw_decode (ulaw_buffer
[k
])) ;
152 /*=================================================================================
153 ** The following routines came from the sox-12.15 (Sound eXcahcnge) distribution.
155 ** This code is not compiled into libsndfile. It is only used to test the
156 ** libsndfile lookup tables for correctness.
158 ** I have included the original authors comments.
162 ** This routine converts from linear to ulaw.
164 ** Craig Reese: IDA/Supercomputing Research Center
165 ** Joe Campbell: Department of Defense
169 ** 1) CCITT Recommendation G.711 (very difficult to follow)
170 ** 2) "A New Digital Technique for Implementation of Any
171 ** Continuous PCM Companding Law," Villeret, Michel,
172 ** et al. 1973 IEEE Int. Conf. on Communications, Vol 1,
173 ** 1973, pg. 11.12-11.17
174 ** 3) MIL-STD-188-113,"Interoperability and Performance Standards
175 ** for Analog-to_Digital Conversion Techniques,"
178 ** Input: Signed 16 bit linear sample
179 ** Output: 8 bit ulaw sample
182 #define uBIAS 0x84 /* define the add-in bias for 16 bit.frames */
186 unsigned char ulaw_encode (int sample
)
187 { static int exp_lut
[256] =
188 { 0, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3,
189 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
190 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
191 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
192 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
193 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
194 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
195 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
196 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
197 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
198 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
199 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
200 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
201 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
202 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
203 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7
206 int sign
, exponent
, mantissa
;
207 unsigned char ulawbyte
;
209 /* Get the sample into sign-magnitude. */
210 sign
= (sample
>> 8) & 0x80 ; /* set aside the sign */
212 sample
= -sample
; /* get magnitude */
213 if ( sample
> uCLIP
)
214 sample
= uCLIP
; /* clip the magnitude */
216 /* Convert from 16 bit linear to ulaw. */
217 sample
= sample
+ uBIAS
;
218 exponent
= exp_lut
[( sample
>> 7 ) & 0xFF] ;
219 mantissa
= (sample
>> ( exponent
+ 3 ) ) & 0x0F ;
220 ulawbyte
= ~ (sign
| ( exponent
<< 4 ) | mantissa
) ;
227 ** This routine converts from ulaw to 16 bit linear.
229 ** Craig Reese: IDA/Supercomputing Research Center
233 ** 1) CCITT Recommendation G.711 (very difficult to follow)
234 ** 2) MIL-STD-188-113,"Interoperability and Performance Standards
235 ** for Analog-to_Digital Conversion Techniques,"
238 ** Input: 8 bit ulaw sample
239 ** Output: signed 16 bit linear sample
243 int ulaw_decode (unsigned int ulawbyte
)
244 { static int exp_lut
[8] = { 0, 132, 396, 924, 1980, 4092, 8316, 16764 } ;
245 int sign
, exponent
, mantissa
, sample
;
247 ulawbyte
= ~ ulawbyte
;
248 sign
= (ulawbyte
& 0x80) ;
249 exponent
= (ulawbyte
>> 4) & 0x07 ;
250 mantissa
= ulawbyte
& 0x0F ;
251 sample
= exp_lut
[exponent
] + (mantissa
<< (exponent
+ 3)) ;