• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 ** Copyright (C) 1999-2013 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 Lesser General Public License as published by
6 ** the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
13 **
14 ** You should have received a copy of the GNU Lesser 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	<math.h>
22 #include	<limits.h>
23 
24 #include	"sndfile.h"
25 #include	"common.h"
26 
27 static sf_count_t alaw_read_alaw2s (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
28 static sf_count_t alaw_read_alaw2i (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
29 static sf_count_t alaw_read_alaw2f (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
30 static sf_count_t alaw_read_alaw2d (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
31 
32 static sf_count_t alaw_write_s2alaw (SF_PRIVATE *psf, const short *ptr, sf_count_t len) ;
33 static sf_count_t alaw_write_i2alaw (SF_PRIVATE *psf, const int *ptr, sf_count_t len) ;
34 static sf_count_t alaw_write_f2alaw (SF_PRIVATE *psf, const float *ptr, sf_count_t len) ;
35 static sf_count_t alaw_write_d2alaw (SF_PRIVATE *psf, const double *ptr, sf_count_t len) ;
36 
37 static void alaw2s_array (unsigned char *buffer, int count, short *ptr) ;
38 static void alaw2i_array (unsigned char *buffer, int count, int *ptr) ;
39 static void alaw2f_array (unsigned char *buffer, int count, float *ptr, float normfact) ;
40 static void alaw2d_array (unsigned char *buffer, int count, double *ptr, double normfact) ;
41 
42 static void s2alaw_array (const short *buffer, int count, unsigned char *ptr) ;
43 static void i2alaw_array (const int *buffer, int count, unsigned char *ptr) ;
44 static void f2alaw_array (const float *buffer, int count, unsigned char *ptr, float normfact) ;
45 static void d2alaw_array (const double *buffer, int count, unsigned char *ptr, double normfact) ;
46 
47 
48 int
alaw_init(SF_PRIVATE * psf)49 alaw_init (SF_PRIVATE *psf)
50 {
51 	if (psf->file.mode == SFM_READ || psf->file.mode == SFM_RDWR)
52 	{	psf->read_short		= alaw_read_alaw2s ;
53 		psf->read_int		= alaw_read_alaw2i ;
54 		psf->read_float		= alaw_read_alaw2f ;
55 		psf->read_double	= alaw_read_alaw2d ;
56 		} ;
57 
58 	if (psf->file.mode == SFM_WRITE || psf->file.mode == SFM_RDWR)
59 	{	psf->write_short	= alaw_write_s2alaw ;
60 		psf->write_int		= alaw_write_i2alaw ;
61 		psf->write_float	= alaw_write_f2alaw ;
62 		psf->write_double	= alaw_write_d2alaw ;
63 		} ;
64 
65 	psf->bytewidth = 1 ;
66 	psf->blockwidth = psf->sf.channels ;
67 
68 	if (psf->filelength > psf->dataoffset)
69 		psf->datalength = (psf->dataend) ? psf->dataend - psf->dataoffset : psf->filelength - psf->dataoffset ;
70 	else
71 		psf->datalength = 0 ;
72 
73 	psf->sf.frames = psf->blockwidth > 0 ? psf->datalength / psf->blockwidth : 0 ;
74 
75 	return 0 ;
76 } /* alaw_init */
77 
78 /*==============================================================================
79  *	Private static functions and data.
80  */
81 
82 static
83 short alaw_decode [256] =
84 {	-5504,	-5248,	-6016,	-5760,	-4480,	-4224,	-4992,	-4736,
85 	-7552,	-7296,	-8064,	-7808,	-6528,	-6272,	-7040,	-6784,
86 	-2752,	-2624,	-3008,	-2880,	-2240,	-2112,	-2496,	-2368,
87 	-3776,	-3648,	-4032,	-3904,	-3264,	-3136,	-3520,	-3392,
88 	-22016,	-20992,	-24064,	-23040,	-17920,	-16896,	-19968,	-18944,
89 	-30208,	-29184,	-32256,	-31232,	-26112,	-25088,	-28160,	-27136,
90 	-11008,	-10496,	-12032,	-11520,	-8960,	-8448,	-9984,	-9472,
91 	-15104,	-14592,	-16128,	-15616,	-13056,	-12544,	-14080,	-13568,
92 	-344,	-328,	-376,	-360,	-280,	-264,	-312,	-296,
93 	-472,	-456,	-504,	-488,	-408,	-392,	-440,	-424,
94 	-88,	-72,	-120,	-104,	-24,	-8,		-56,	-40,
95 	-216,	-200,	-248,	-232,	-152,	-136,	-184,	-168,
96 	-1376,	-1312,	-1504,	-1440,	-1120,	-1056,	-1248,	-1184,
97 	-1888,	-1824,	-2016,	-1952,	-1632,	-1568,	-1760,	-1696,
98 	-688,	-656,	-752,	-720,	-560,	-528,	-624,	-592,
99 	-944,	-912,	-1008,	-976,	-816,	-784,	-880,	-848,
100 	5504,	5248,	6016,	5760,	4480,	4224,	4992,	4736,
101 	7552,	7296,	8064,	7808,	6528,	6272,	7040,	6784,
102 	2752,	2624,	3008,	2880,	2240,	2112,	2496,	2368,
103 	3776,	3648,	4032,	3904,	3264,	3136,	3520,	3392,
104 	22016,	20992,	24064,	23040,	17920,	16896,	19968,	18944,
105 	30208,	29184,	32256,	31232,	26112,	25088,	28160,	27136,
106 	11008,	10496,	12032,	11520,	8960,	8448,	9984,	9472,
107 	15104,	14592,	16128,	15616,	13056,	12544,	14080,	13568,
108 	344,	328,	376,	360,	280,	264,	312,	296,
109 	472,	456,	504,	488,	408,	392,	440,	424,
110 	88,		72,		120,	104,	24,		8,		56,		40,
111 	216,	200,	248,	232,	152,	136,	184,	168,
112 	1376,	1312,	1504,	1440,	1120,	1056,	1248,	1184,
113 	1888,	1824,	2016,	1952,	1632,	1568,	1760,	1696,
114 	688,	656,	752,	720,	560,	528,	624,	592,
115 	944,	912,	1008,	976,	816,	784,	880,	848
116 } ; /* alaw_decode */
117 
118 static
119 unsigned char alaw_encode [2048 + 1] =
120 {	0xd5, 0xd4, 0xd7, 0xd6, 0xd1, 0xd0, 0xd3, 0xd2, 0xdd, 0xdc, 0xdf, 0xde,
121 	0xd9, 0xd8, 0xdb, 0xda, 0xc5, 0xc4, 0xc7, 0xc6, 0xc1, 0xc0, 0xc3, 0xc2,
122 	0xcd, 0xcc, 0xcf, 0xce, 0xc9, 0xc8, 0xcb, 0xca, 0xf5, 0xf5, 0xf4, 0xf4,
123 	0xf7, 0xf7, 0xf6, 0xf6, 0xf1, 0xf1, 0xf0, 0xf0, 0xf3, 0xf3, 0xf2, 0xf2,
124 	0xfd, 0xfd, 0xfc, 0xfc, 0xff, 0xff, 0xfe, 0xfe, 0xf9, 0xf9, 0xf8, 0xf8,
125 	0xfb, 0xfb, 0xfa, 0xfa, 0xe5, 0xe5, 0xe5, 0xe5, 0xe4, 0xe4, 0xe4, 0xe4,
126 	0xe7, 0xe7, 0xe7, 0xe7, 0xe6, 0xe6, 0xe6, 0xe6, 0xe1, 0xe1, 0xe1, 0xe1,
127 	0xe0, 0xe0, 0xe0, 0xe0, 0xe3, 0xe3, 0xe3, 0xe3, 0xe2, 0xe2, 0xe2, 0xe2,
128 	0xed, 0xed, 0xed, 0xed, 0xec, 0xec, 0xec, 0xec, 0xef, 0xef, 0xef, 0xef,
129 	0xee, 0xee, 0xee, 0xee, 0xe9, 0xe9, 0xe9, 0xe9, 0xe8, 0xe8, 0xe8, 0xe8,
130 	0xeb, 0xeb, 0xeb, 0xeb, 0xea, 0xea, 0xea, 0xea, 0x95, 0x95, 0x95, 0x95,
131 	0x95, 0x95, 0x95, 0x95, 0x94, 0x94, 0x94, 0x94, 0x94, 0x94, 0x94, 0x94,
132 	0x97, 0x97, 0x97, 0x97, 0x97, 0x97, 0x97, 0x97, 0x96, 0x96, 0x96, 0x96,
133 	0x96, 0x96, 0x96, 0x96, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91,
134 	0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x93, 0x93, 0x93, 0x93,
135 	0x93, 0x93, 0x93, 0x93, 0x92, 0x92, 0x92, 0x92, 0x92, 0x92, 0x92, 0x92,
136 	0x9d, 0x9d, 0x9d, 0x9d, 0x9d, 0x9d, 0x9d, 0x9d, 0x9c, 0x9c, 0x9c, 0x9c,
137 	0x9c, 0x9c, 0x9c, 0x9c, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f,
138 	0x9e, 0x9e, 0x9e, 0x9e, 0x9e, 0x9e, 0x9e, 0x9e, 0x99, 0x99, 0x99, 0x99,
139 	0x99, 0x99, 0x99, 0x99, 0x98, 0x98, 0x98, 0x98, 0x98, 0x98, 0x98, 0x98,
140 	0x9b, 0x9b, 0x9b, 0x9b, 0x9b, 0x9b, 0x9b, 0x9b, 0x9a, 0x9a, 0x9a, 0x9a,
141 	0x9a, 0x9a, 0x9a, 0x9a, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85,
142 	0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x84, 0x84, 0x84, 0x84,
143 	0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84,
144 	0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87,
145 	0x87, 0x87, 0x87, 0x87, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86,
146 	0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x81, 0x81, 0x81, 0x81,
147 	0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
148 	0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
149 	0x80, 0x80, 0x80, 0x80, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83,
150 	0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x82, 0x82, 0x82, 0x82,
151 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xa2, 0xa2, 0xa2, 0xa2, 0xa2,
248 	0xad, 0xad, 0xad, 0xad, 0xad, 0xad, 0xad, 0xad, 0xad, 0xad, 0xad, 0xad,
249 	0xad, 0xad, 0xad, 0xad, 0xad, 0xad, 0xad, 0xad, 0xad, 0xad, 0xad, 0xad,
250 	0xad, 0xad, 0xad, 0xad, 0xad, 0xad, 0xad, 0xad, 0xad, 0xad, 0xad, 0xad,
251 	0xad, 0xad, 0xad, 0xad, 0xad, 0xad, 0xad, 0xad, 0xad, 0xad, 0xad, 0xad,
252 	0xad, 0xad, 0xad, 0xad, 0xad, 0xad, 0xad, 0xad, 0xad, 0xad, 0xad, 0xad,
253 	0xad, 0xad, 0xad, 0xad, 0xac, 0xac, 0xac, 0xac, 0xac, 0xac, 0xac, 0xac,
254 	0xac, 0xac, 0xac, 0xac, 0xac, 0xac, 0xac, 0xac, 0xac, 0xac, 0xac, 0xac,
255 	0xac, 0xac, 0xac, 0xac, 0xac, 0xac, 0xac, 0xac, 0xac, 0xac, 0xac, 0xac,
256 	0xac, 0xac, 0xac, 0xac, 0xac, 0xac, 0xac, 0xac, 0xac, 0xac, 0xac, 0xac,
257 	0xac, 0xac, 0xac, 0xac, 0xac, 0xac, 0xac, 0xac, 0xac, 0xac, 0xac, 0xac,
258 	0xac, 0xac, 0xac, 0xac, 0xac, 0xac, 0xac, 0xac, 0xaf, 0xaf, 0xaf, 0xaf,
259 	0xaf, 0xaf, 0xaf, 0xaf, 0xaf, 0xaf, 0xaf, 0xaf, 0xaf, 0xaf, 0xaf, 0xaf,
260 	0xaf, 0xaf, 0xaf, 0xaf, 0xaf, 0xaf, 0xaf, 0xaf, 0xaf, 0xaf, 0xaf, 0xaf,
261 	0xaf, 0xaf, 0xaf, 0xaf, 0xaf, 0xaf, 0xaf, 0xaf, 0xaf, 0xaf, 0xaf, 0xaf,
262 	0xaf, 0xaf, 0xaf, 0xaf, 0xaf, 0xaf, 0xaf, 0xaf, 0xaf, 0xaf, 0xaf, 0xaf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8, 0xa8, 0xa8, 0xa8, 0xa8, 0xa8, 0xa8,
278 	0xa8, 0xa8, 0xa8, 0xa8, 0xa8, 0xa8, 0xa8, 0xa8, 0xa8, 0xa8, 0xa8, 0xa8,
279 	0xa8, 0xa8, 0xa8, 0xa8, 0xa8, 0xa8, 0xa8, 0xa8, 0xa8, 0xa8, 0xa8, 0xa8,
280 	0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab,
281 	0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab,
282 	0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab,
283 	0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab,
284 	0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab,
285 	0xab, 0xab, 0xab, 0xab, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
286 	0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
287 	0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
288 	0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
289 	0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
290 	0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa
291 } ; /* alaw_encode */
292 
293 static inline void
alaw2s_array(unsigned char * buffer,int count,short * ptr)294 alaw2s_array (unsigned char *buffer, int count, short *ptr)
295 {	while (--count >= 0)
296 		ptr [count] = alaw_decode [(int) buffer [count]] ;
297 } /* alaw2s_array */
298 
299 static inline void
alaw2i_array(unsigned char * buffer,int count,int * ptr)300 alaw2i_array (unsigned char *buffer, int count, int *ptr)
301 {	while (--count >= 0)
302 		ptr [count] = ((uint32_t) alaw_decode [(int) buffer [count]]) << 16 ;
303 } /* alaw2i_array */
304 
305 static inline void
alaw2f_array(unsigned char * buffer,int count,float * ptr,float normfact)306 alaw2f_array (unsigned char *buffer, int count, float *ptr, float normfact)
307 {	while (--count >= 0)
308 		ptr [count] = normfact * alaw_decode [(int) buffer [count]] ;
309 } /* alaw2f_array */
310 
311 static inline void
alaw2d_array(unsigned char * buffer,int count,double * ptr,double normfact)312 alaw2d_array (unsigned char *buffer, int count, double *ptr, double normfact)
313 {	while (--count >= 0)
314 		ptr [count] = normfact * alaw_decode [(int) buffer [count]] ;
315 } /* alaw2d_array */
316 
317 static inline void
s2alaw_array(const short * ptr,int count,unsigned char * buffer)318 s2alaw_array (const short *ptr, int count, unsigned char *buffer)
319 {	while (--count >= 0)
320 	{	if (ptr [count] >= 0)
321 			buffer [count] = alaw_encode [ptr [count] / 16] ;
322 		else
323 			buffer [count] = 0x7F & alaw_encode [ptr [count] / -16] ;
324 		} ;
325 } /* s2alaw_array */
326 
327 static inline void
i2alaw_array(const int * ptr,int count,unsigned char * buffer)328 i2alaw_array (const int *ptr, int count, unsigned char *buffer)
329 {	while (--count >= 0)
330 	{	if (ptr [count] == INT_MIN)
331 			buffer [count] = alaw_encode [INT_MAX >> (16 + 4)] ;
332 		else if (ptr [count] >= 0)
333 			buffer [count] = alaw_encode [ptr [count] >> (16 + 4)] ;
334 		else
335 			buffer [count] = 0x7F & alaw_encode [- ptr [count] >> (16 + 4)] ;
336 		} ;
337 } /* i2alaw_array */
338 
339 static inline void
f2alaw_array(const float * ptr,int count,unsigned char * buffer,float normfact)340 f2alaw_array (const float *ptr, int count, unsigned char *buffer, float normfact)
341 {	while (--count >= 0)
342 	{	if (ptr [count] >= 0)
343 			buffer [count] = alaw_encode [psf_lrintf (normfact * ptr [count])] ;
344 		else
345 			buffer [count] = 0x7F & alaw_encode [- psf_lrintf (normfact * ptr [count])] ;
346 		} ;
347 } /* f2alaw_array */
348 
349 static inline void
d2alaw_array(const double * ptr,int count,unsigned char * buffer,double normfact)350 d2alaw_array (const double *ptr, int count, unsigned char *buffer, double normfact)
351 {	while (--count >= 0)
352 	{	if (!isfinite (ptr [count]))
353 			buffer [count] = 0 ;
354 		else if (ptr [count] >= 0)
355 			buffer [count] = alaw_encode [psf_lrint (normfact * ptr [count])] ;
356 		else
357 			buffer [count] = 0x7F & alaw_encode [- psf_lrint (normfact * ptr [count])] ;
358 		} ;
359 } /* d2alaw_array */
360 
361 /*==============================================================================
362 */
363 
364 static sf_count_t
alaw_read_alaw2s(SF_PRIVATE * psf,short * ptr,sf_count_t len)365 alaw_read_alaw2s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
366 {	BUF_UNION	ubuf ;
367 	int			bufferlen, readcount ;
368 	sf_count_t	total = 0 ;
369 
370 	bufferlen = ARRAY_LEN (ubuf.ucbuf) ;
371 
372 	while (len > 0)
373 	{	if (len < bufferlen)
374 			bufferlen = (int) len ;
375 		readcount = psf_fread (ubuf.ucbuf, 1, bufferlen, psf) ;
376 		alaw2s_array (ubuf.ucbuf, readcount, ptr + total) ;
377 		total += readcount ;
378 		if (readcount < bufferlen)
379 			break ;
380 		len -= readcount ;
381 		} ;
382 
383 	return total ;
384 } /* alaw_read_alaw2s */
385 
386 static sf_count_t
alaw_read_alaw2i(SF_PRIVATE * psf,int * ptr,sf_count_t len)387 alaw_read_alaw2i (SF_PRIVATE *psf, int *ptr, sf_count_t len)
388 {	BUF_UNION	ubuf ;
389 	int			bufferlen, readcount ;
390 	sf_count_t	total = 0 ;
391 
392 	bufferlen = ARRAY_LEN (ubuf.ucbuf) ;
393 
394 	while (len > 0)
395 	{	if (len < bufferlen)
396 			bufferlen = (int) len ;
397 		readcount = psf_fread (ubuf.ucbuf, 1, bufferlen, psf) ;
398 		alaw2i_array (ubuf.ucbuf, readcount, ptr + total) ;
399 		total += readcount ;
400 		if (readcount < bufferlen)
401 			break ;
402 		len -= readcount ;
403 		} ;
404 
405 	return total ;
406 } /* alaw_read_alaw2i */
407 
408 static sf_count_t
alaw_read_alaw2f(SF_PRIVATE * psf,float * ptr,sf_count_t len)409 alaw_read_alaw2f (SF_PRIVATE *psf, float *ptr, sf_count_t len)
410 {	BUF_UNION	ubuf ;
411 	int			bufferlen, readcount ;
412 	sf_count_t	total = 0 ;
413 	float	normfact ;
414 
415 	normfact = (psf->norm_float == SF_TRUE) ? 1.0 / ((float) 0x8000) : 1.0 ;
416 
417 	bufferlen = ARRAY_LEN (ubuf.ucbuf) ;
418 
419 	while (len > 0)
420 	{	if (len < bufferlen)
421 			bufferlen = (int) len ;
422 		readcount = psf_fread (ubuf.ucbuf, 1, bufferlen, psf) ;
423 		alaw2f_array (ubuf.ucbuf, readcount, ptr + total, normfact) ;
424 		total += readcount ;
425 		if (readcount < bufferlen)
426 			break ;
427 		len -= readcount ;
428 		} ;
429 
430 	return total ;
431 } /* alaw_read_alaw2f */
432 
433 static sf_count_t
alaw_read_alaw2d(SF_PRIVATE * psf,double * ptr,sf_count_t len)434 alaw_read_alaw2d (SF_PRIVATE *psf, double *ptr, sf_count_t len)
435 {	BUF_UNION	ubuf ;
436 	int			bufferlen, readcount ;
437 	sf_count_t	total = 0 ;
438 	double	normfact ;
439 
440 	normfact = (psf->norm_double) ? 1.0 / ((double) 0x8000) : 1.0 ;
441 	bufferlen = ARRAY_LEN (ubuf.ucbuf) ;
442 
443 	while (len > 0)
444 	{	if (len < bufferlen)
445 			bufferlen = (int) len ;
446 		readcount = psf_fread (ubuf.ucbuf, 1, bufferlen, psf) ;
447 		alaw2d_array (ubuf.ucbuf, readcount, ptr + total, normfact) ;
448 		total += readcount ;
449 		if (readcount < bufferlen)
450 			break ;
451 		len -= readcount ;
452 		} ;
453 
454 	return total ;
455 } /* alaw_read_alaw2d */
456 
457 /*=============================================================================================
458 */
459 
460 static sf_count_t
alaw_write_s2alaw(SF_PRIVATE * psf,const short * ptr,sf_count_t len)461 alaw_write_s2alaw	(SF_PRIVATE *psf, const short *ptr, sf_count_t len)
462 {	BUF_UNION	ubuf ;
463 	int			bufferlen, writecount ;
464 	sf_count_t	total = 0 ;
465 
466 	bufferlen = ARRAY_LEN (ubuf.ucbuf) ;
467 
468 	while (len > 0)
469 	{	if (len < bufferlen)
470 			bufferlen = (int) len ;
471 		s2alaw_array (ptr + total, bufferlen, ubuf.ucbuf) ;
472 		writecount = psf_fwrite (ubuf.ucbuf, 1, bufferlen, psf) ;
473 		total += writecount ;
474 		if (writecount < bufferlen)
475 			break ;
476 		len -= writecount ;
477 		} ;
478 
479 	return total ;
480 } /* alaw_write_s2alaw */
481 
482 static sf_count_t
alaw_write_i2alaw(SF_PRIVATE * psf,const int * ptr,sf_count_t len)483 alaw_write_i2alaw	(SF_PRIVATE *psf, const int *ptr, sf_count_t len)
484 {	BUF_UNION	ubuf ;
485 	int			bufferlen, writecount ;
486 	sf_count_t	total = 0 ;
487 
488 	bufferlen = ARRAY_LEN (ubuf.ucbuf) ;
489 
490 	while (len > 0)
491 	{	if (len < bufferlen)
492 			bufferlen = (int) len ;
493 		i2alaw_array (ptr + total, bufferlen, ubuf.ucbuf) ;
494 		writecount = psf_fwrite (ubuf.ucbuf, 1, bufferlen, psf) ;
495 		total += writecount ;
496 		if (writecount < bufferlen)
497 			break ;
498 		len -= writecount ;
499 		} ;
500 
501 	return total ;
502 } /* alaw_write_i2alaw */
503 
504 static sf_count_t
alaw_write_f2alaw(SF_PRIVATE * psf,const float * ptr,sf_count_t len)505 alaw_write_f2alaw	(SF_PRIVATE *psf, const float *ptr, sf_count_t len)
506 {	BUF_UNION	ubuf ;
507 	int			bufferlen, writecount ;
508 	sf_count_t	total = 0 ;
509 	float	normfact ;
510 
511 	normfact = (psf->norm_float == SF_TRUE) ? (1.0 * 0x7FFF) / 16.0 : 1.0 / 16 ;
512 
513 	bufferlen = ARRAY_LEN (ubuf.ucbuf) ;
514 
515 	while (len > 0)
516 	{	if (len < bufferlen)
517 			bufferlen = (int) len ;
518 		f2alaw_array (ptr + total, bufferlen, ubuf.ucbuf, normfact) ;
519 		writecount = psf_fwrite (ubuf.ucbuf, 1, bufferlen, psf) ;
520 		total += writecount ;
521 		if (writecount < bufferlen)
522 			break ;
523 		len -= writecount ;
524 		} ;
525 
526 	return total ;
527 } /* alaw_write_f2alaw */
528 
529 static sf_count_t
alaw_write_d2alaw(SF_PRIVATE * psf,const double * ptr,sf_count_t len)530 alaw_write_d2alaw	(SF_PRIVATE *psf, const double *ptr, sf_count_t len)
531 {	BUF_UNION	ubuf ;
532 	int			bufferlen, writecount ;
533 	sf_count_t	total = 0 ;
534 	double	normfact ;
535 
536 	normfact = (psf->norm_double) ? (1.0 * 0x7FFF) / 16.0 : 1.0 / 16.0 ;
537 
538 	bufferlen = ARRAY_LEN (ubuf.ucbuf) ;
539 
540 	while (len > 0)
541 	{	if (len < bufferlen)
542 			bufferlen = (int) len ;
543 		d2alaw_array (ptr + total, bufferlen, ubuf.ucbuf, normfact) ;
544 		writecount = psf_fwrite (ubuf.ucbuf, 1, bufferlen, psf) ;
545 		total += writecount ;
546 		if (writecount < bufferlen)
547 			break ;
548 		len -= writecount ;
549 		} ;
550 
551 	return total ;
552 } /* alaw_write_d2alaw */
553 
554