2 ** Copyright (C) 1999-2003 Erik de Castro Lopo <erikd@zip.com.au>
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.
28 #define BUFFER_SIZE (65536)
30 static unsigned char ulaw_encode (int sample
) ;
31 static int ulaw_decode (unsigned int ulawbyte
) ;
33 static short short_buffer
[BUFFER_SIZE
] ;
34 static unsigned char ulaw_buffer
[BUFFER_SIZE
] ;
43 filename
= "test.raw" ;
45 sfinfo
.format
= SF_FORMAT_RAW
| SF_FORMAT_ULAW
;
46 sfinfo
.samplerate
= 44100 ;
47 sfinfo
.frames
= 123456789 ;
50 if (! (file
= sf_open (filename
, SFM_WRITE
, &sfinfo
)))
51 { printf ("sf_open_write failed with error : ") ;
53 puts (sf_strerror (NULL
)) ;
57 /* Generate a file containing all possible 16 bit sample values
58 ** and write it to disk as ulaw encoded.frames.
61 for (k
= 0 ; k
< 0x10000 ; k
++)
62 short_buffer
[k
] = k
& 0xFFFF ;
64 sf_write_short (file
, short_buffer
, BUFFER_SIZE
) ;
67 /* Now open that file and compare the ulaw 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
)) ;
77 check_log_buffer_or_die (file
) ;
79 if (sf_read_raw (file
, ulaw_buffer
, BUFFER_SIZE
) != BUFFER_SIZE
)
80 { printf ("sf_read_raw : ") ;
81 puts (sf_strerror (file
)) ;
85 for (k
= 0 ; k
< 0x10000 ; k
++)
86 if (ulaw_encode (short_buffer
[k
]) != ulaw_buffer
[k
])
87 { printf ("Encoder error : sample #%d (0x%02X should be 0x%02X)\n", k
, ulaw_buffer
[k
], ulaw_encode (short_buffer
[k
])) ;
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 ulaw encoded.frames.
99 if (! (file
= sf_open (filename
, SFM_WRITE
, &sfinfo
)))
100 { printf ("sf_open_write failed with error : ") ;
101 puts (sf_strerror (NULL
)) ;
105 for (k
= 0 ; k
< 256 ; k
++)
106 ulaw_buffer
[k
] = k
& 0xFF ;
108 sf_write_raw (file
, ulaw_buffer
, 256) ;
111 /* Now open that file and compare the ulaw 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
)) ;
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
)) ;
130 for (k
= 0 ; k
< 256 ; k
++)
131 if (short_buffer
[k
] != ulaw_decode (ulaw_buffer
[k
]))
132 { printf ("Decoder error : sample #%d (0x%04X should be 0x%04X)\n", k
, short_buffer
[k
], ulaw_decode (ulaw_buffer
[k
])) ;
138 printf (" ulaw_test : decoder ... ok\n") ;
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 ** This routine converts from linear to ulaw.
158 ** Craig Reese: IDA/Supercomputing Research Center
159 ** Joe Campbell: Department of Defense
163 ** 1) CCITT Recommendation G.711 (very difficult to follow)
164 ** 2) "A New Digital Technique for Implementation of Any
165 ** Continuous PCM Companding Law," Villeret, Michel,
166 ** et al. 1973 IEEE Int. Conf. on Communications, Vol 1,
167 ** 1973, pg. 11.12-11.17
168 ** 3) MIL-STD-188-113,"Interoperability and Performance Standards
169 ** for Analog-to_Digital Conversion Techniques,"
172 ** Input: Signed 16 bit linear sample
173 ** Output: 8 bit ulaw sample
176 #define uBIAS 0x84 /* define the add-in bias for 16 bit.frames */
180 unsigned char ulaw_encode (int sample
)
181 { static int exp_lut
[256] = {0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,
182 4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
183 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
184 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
185 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
186 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
187 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
188 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
189 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
190 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
191 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
192 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
193 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
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 int sign
, exponent
, mantissa
;
198 unsigned char ulawbyte
;
200 /* Get the sample into sign-magnitude. */
201 sign
= (sample
>> 8) & 0x80 ; /* set aside the sign */
203 sample
= -sample
; /* get magnitude */
204 if ( sample
> uCLIP
)
205 sample
= uCLIP
; /* clip the magnitude */
207 /* Convert from 16 bit linear to ulaw. */
208 sample
= sample
+ uBIAS
;
209 exponent
= exp_lut
[( sample
>> 7 ) & 0xFF];
210 mantissa
= ( sample
>> ( exponent
+ 3 ) ) & 0x0F;
211 ulawbyte
= ~ ( sign
| ( exponent
<< 4 ) | mantissa
);
218 ** This routine converts from ulaw to 16 bit linear.
220 ** Craig Reese: IDA/Supercomputing Research Center
224 ** 1) CCITT Recommendation G.711 (very difficult to follow)
225 ** 2) MIL-STD-188-113,"Interoperability and Performance Standards
226 ** for Analog-to_Digital Conversion Techniques,"
229 ** Input: 8 bit ulaw sample
230 ** Output: signed 16 bit linear sample
234 int ulaw_decode (unsigned int ulawbyte
)
235 { static int exp_lut
[8] = { 0, 132, 396, 924, 1980, 4092, 8316, 16764 };
236 int sign
, exponent
, mantissa
, sample
;
238 ulawbyte
= ~ ulawbyte
;
239 sign
= (ulawbyte
& 0x80) ;
240 exponent
= (ulawbyte
>> 4) & 0x07 ;
241 mantissa
= ulawbyte
& 0x0F ;
242 sample
= exp_lut
[exponent
] + (mantissa
<< (exponent
+ 3)) ;