2
** Copyright (C) 1999-2004 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.
32
#define BUFFER_SIZE (65536)
34
static unsigned char ulaw_encode (int sample) ;
35
static int ulaw_decode (unsigned int ulawbyte) ;
37
static short short_buffer [BUFFER_SIZE] ;
38
static unsigned char ulaw_buffer [BUFFER_SIZE] ;
44
const char *filename ;
47
filename = "test.raw" ;
49
sfinfo.format = SF_FORMAT_RAW | SF_FORMAT_ULAW ;
50
sfinfo.samplerate = 44100 ;
51
sfinfo.frames = 123456789 ;
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])) ;
97
printf (" ulaw_test : encoder ... ok\n") ;
99
/* Now generate a file containing all possible 8 bit encoded
100
** sample values and write it to disk as ulaw encoded.frames.
103
if (! (file = sf_open (filename, SFM_WRITE, &sfinfo)))
104
{ printf ("sf_open_write failed with error : ") ;
105
puts (sf_strerror (NULL)) ;
109
for (k = 0 ; k < 256 ; k++)
110
ulaw_buffer [k] = k & 0xFF ;
112
sf_write_raw (file, ulaw_buffer, 256) ;
115
/* Now open that file and compare the ulaw decoded sample values
116
** with what they should be.
119
if (! (file = sf_open (filename, SFM_READ, &sfinfo)))
120
{ printf ("sf_open_write failed with error : ") ;
121
puts (sf_strerror (NULL)) ;
125
check_log_buffer_or_die (file, __LINE__) ;
127
if (sf_read_short (file, short_buffer, 256) != 256)
128
{ printf ("sf_read_short : ") ;
129
puts (sf_strerror (file)) ;
134
for (k = 0 ; k < 256 ; k++)
135
if (short_buffer [k] != ulaw_decode (ulaw_buffer [k]))
136
{ printf ("Decoder error : sample #%d (0x%04X should be 0x%04X)\n", k, short_buffer [k], ulaw_decode (ulaw_buffer [k])) ;
142
printf (" ulaw_test : decoder ... ok\n") ;
150
/*=================================================================================
151
** The following routines came from the sox-12.15 (Sound eXcahcnge) distribution.
153
** This code is not compiled into libsndfile. It is only used to test the
154
** libsndfile lookup tables for correctness.
156
** I have included the original authors comments.
160
** This routine converts from linear to ulaw.
162
** Craig Reese: IDA/Supercomputing Research Center
163
** Joe Campbell: Department of Defense
167
** 1) CCITT Recommendation G.711 (very difficult to follow)
168
** 2) "A New Digital Technique for Implementation of Any
169
** Continuous PCM Companding Law," Villeret, Michel,
170
** et al. 1973 IEEE Int. Conf. on Communications, Vol 1,
171
** 1973, pg. 11.12-11.17
172
** 3) MIL-STD-188-113,"Interoperability and Performance Standards
173
** for Analog-to_Digital Conversion Techniques,"
176
** Input: Signed 16 bit linear sample
177
** Output: 8 bit ulaw sample
180
#define uBIAS 0x84 /* define the add-in bias for 16 bit.frames */
184
unsigned char ulaw_encode (int sample)
185
{ static int exp_lut [256] =
186
{ 0, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3,
187
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
188
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
189
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
190
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
191
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
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
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
195
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
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
204
int sign, exponent, mantissa ;
205
unsigned char ulawbyte ;
207
/* Get the sample into sign-magnitude. */
208
sign = (sample >> 8) & 0x80 ; /* set aside the sign */
210
sample = -sample ; /* get magnitude */
211
if ( sample > uCLIP )
212
sample = uCLIP ; /* clip the magnitude */
214
/* Convert from 16 bit linear to ulaw. */
215
sample = sample + uBIAS ;
216
exponent = exp_lut [( sample >> 7 ) & 0xFF] ;
217
mantissa = (sample >> ( exponent + 3 ) ) & 0x0F ;
218
ulawbyte = ~ (sign | ( exponent << 4 ) | mantissa) ;
225
** This routine converts from ulaw to 16 bit linear.
227
** Craig Reese: IDA/Supercomputing Research Center
231
** 1) CCITT Recommendation G.711 (very difficult to follow)
232
** 2) MIL-STD-188-113,"Interoperability and Performance Standards
233
** for Analog-to_Digital Conversion Techniques,"
236
** Input: 8 bit ulaw sample
237
** Output: signed 16 bit linear sample
241
int ulaw_decode (unsigned int ulawbyte)
242
{ static int exp_lut [8] = { 0, 132, 396, 924, 1980, 4092, 8316, 16764 } ;
243
int sign, exponent, mantissa, sample ;
245
ulawbyte = ~ ulawbyte ;
246
sign = (ulawbyte & 0x80) ;
247
exponent = (ulawbyte >> 4) & 0x07 ;
248
mantissa = ulawbyte & 0x0F ;
249
sample = exp_lut [exponent] + (mantissa << (exponent + 3)) ;
257
** Do not edit or modify anything in this comment block.
258
** The arch-tag line is a file identity tag for the GNU Arch
259
** revision control system.
261
** arch-tag: e764cee1-5a9a-480b-a4ca-34d9b57dea6f