• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 ** Copyright (C) 2003-2017 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 <stdio.h>
22 #include <stdlib.h>
23 #include <fcntl.h>
24 #include <string.h>
25 #include <ctype.h>
26 #include <math.h>
27 
28 #include "sndfile.h"
29 #include "sfendian.h"
30 #include "common.h"
31 
32 #define	MAX_XI_SAMPLES	16
33 
34 /*------------------------------------------------------------------------------
35 ** Private static functions and tyepdefs.
36 */
37 
38 typedef struct
39 {	/* Warning, this filename is NOT nul terminated. */
40 	char	filename [22] ;
41 	char	software [20] ;
42 	char	sample_name [22] ;
43 
44 	int		loop_begin, loop_end ;
45 	int		sample_flags ;
46 
47 	/* Data for encoder and decoder. */
48 	short	last_16 ;
49 } XI_PRIVATE ;
50 
51 static int	xi_close		(SF_PRIVATE *psf) ;
52 static int	xi_write_header (SF_PRIVATE *psf, int calc_length) ;
53 static int	xi_read_header	(SF_PRIVATE *psf) ;
54 static int	dpcm_init 		(SF_PRIVATE *psf) ;
55 
56 
57 static sf_count_t	dpcm_seek (SF_PRIVATE *psf, int mode, sf_count_t offset) ;
58 
59 /*------------------------------------------------------------------------------
60 ** Public function.
61 */
62 
63 int
xi_open(SF_PRIVATE * psf)64 xi_open	(SF_PRIVATE *psf)
65 {	XI_PRIVATE *pxi ;
66 	int		subformat, error = 0 ;
67 
68 	if (psf->is_pipe)
69 		return SFE_XI_NO_PIPE ;
70 
71 	if (psf->codec_data)
72 		pxi = psf->codec_data ;
73 	else if ((pxi = calloc (1, sizeof (XI_PRIVATE))) == NULL)
74 		return SFE_MALLOC_FAILED ;
75 
76 	psf->codec_data = pxi ;
77 
78 	if (psf->file.mode == SFM_READ || (psf->file.mode == SFM_RDWR && psf->filelength > 0))
79 	{	if ((error = xi_read_header (psf)))
80 			return error ;
81 		} ;
82 
83 	subformat = SF_CODEC (psf->sf.format) ;
84 
85 	if (psf->file.mode == SFM_WRITE || psf->file.mode == SFM_RDWR)
86 	{	if ((SF_CONTAINER (psf->sf.format)) != SF_FORMAT_XI)
87 			return	SFE_BAD_OPEN_FORMAT ;
88 
89 		psf->endian = SF_ENDIAN_LITTLE ;
90 		psf->sf.channels = 1 ; /* Always mono */
91 		psf->sf.samplerate = 44100 ; /* Always */
92 
93 		/* Set up default instrument and software name. */
94 		memcpy (pxi->filename, "Default Name            ", sizeof (pxi->filename)) ;
95 		memcpy (pxi->software, PACKAGE_NAME "-" PACKAGE_VERSION "               ", sizeof (pxi->software)) ;
96 
97 		memset (pxi->sample_name, 0, sizeof (pxi->sample_name)) ;
98 		snprintf (pxi->sample_name, sizeof (pxi->sample_name), "%s", "Sample #1") ;
99 
100 		pxi->sample_flags = (subformat == SF_FORMAT_DPCM_16) ? 16 : 0 ;
101 
102 		if (xi_write_header (psf, SF_FALSE))
103 			return psf->error ;
104 
105 		psf->write_header = xi_write_header ;
106 		} ;
107 
108 	psf->container_close = xi_close ;
109 	psf->seek = dpcm_seek ;
110 
111 	psf->sf.seekable = SF_FALSE ;
112 
113 	psf->blockwidth = psf->bytewidth * psf->sf.channels ;
114 
115 	switch (subformat)
116 	{	case SF_FORMAT_DPCM_8 :		/* 8-bit differential PCM. */
117 		case SF_FORMAT_DPCM_16 :	/* 16-bit differential PCM. */
118 				error = dpcm_init (psf) ;
119 				break ;
120 
121 		default : break ;
122 		} ;
123 
124 	return error ;
125 } /* xi_open */
126 
127 /*------------------------------------------------------------------------------
128 */
129 
130 static int
xi_close(SF_PRIVATE * UNUSED (psf))131 xi_close	(SF_PRIVATE * UNUSED (psf))
132 {
133 	return 0 ;
134 } /* xi_close */
135 
136 /*==============================================================================
137 */
138 
139 static sf_count_t dpcm_read_dsc2s (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
140 static sf_count_t dpcm_read_dsc2i (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
141 static sf_count_t dpcm_read_dsc2f (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
142 static sf_count_t dpcm_read_dsc2d (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
143 
144 static sf_count_t dpcm_write_s2dsc (SF_PRIVATE *psf, const short *ptr, sf_count_t len) ;
145 static sf_count_t dpcm_write_i2dsc (SF_PRIVATE *psf, const int *ptr, sf_count_t len) ;
146 static sf_count_t dpcm_write_f2dsc (SF_PRIVATE *psf, const float *ptr, sf_count_t len) ;
147 static sf_count_t dpcm_write_d2dsc (SF_PRIVATE *psf, const double *ptr, sf_count_t len) ;
148 
149 static sf_count_t dpcm_read_dles2s (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
150 static sf_count_t dpcm_read_dles2i (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
151 static sf_count_t dpcm_read_dles2f (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
152 static sf_count_t dpcm_read_dles2d (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
153 
154 static sf_count_t dpcm_write_s2dles (SF_PRIVATE *psf, const short *ptr, sf_count_t len) ;
155 static sf_count_t dpcm_write_i2dles (SF_PRIVATE *psf, const int *ptr, sf_count_t len) ;
156 static sf_count_t dpcm_write_f2dles (SF_PRIVATE *psf, const float *ptr, sf_count_t len) ;
157 static sf_count_t dpcm_write_d2dles (SF_PRIVATE *psf, const double *ptr, sf_count_t len) ;
158 
159 static int
dpcm_init(SF_PRIVATE * psf)160 dpcm_init (SF_PRIVATE *psf)
161 {	if (psf->bytewidth == 0 || psf->sf.channels == 0)
162 		return SFE_INTERNAL ;
163 
164 	psf->blockwidth = psf->bytewidth * psf->sf.channels ;
165 
166 	if (psf->file.mode == SFM_READ || psf->file.mode == SFM_RDWR)
167 	{	switch (psf->bytewidth)
168 		{	case 1 :
169 					psf->read_short		= dpcm_read_dsc2s ;
170 					psf->read_int		= dpcm_read_dsc2i ;
171 					psf->read_float		= dpcm_read_dsc2f ;
172 					psf->read_double	= dpcm_read_dsc2d ;
173 					break ;
174 			case 2 :
175 					psf->read_short		= dpcm_read_dles2s ;
176 					psf->read_int		= dpcm_read_dles2i ;
177 					psf->read_float		= dpcm_read_dles2f ;
178 					psf->read_double	= dpcm_read_dles2d ;
179 					break ;
180 			default :
181 				psf_log_printf (psf, "dpcm_init() returning SFE_UNIMPLEMENTED\n") ;
182 				return SFE_UNIMPLEMENTED ;
183 			} ;
184 		} ;
185 
186 	if (psf->file.mode == SFM_WRITE || psf->file.mode == SFM_RDWR)
187 	{	switch (psf->bytewidth)
188 		{	case 1 :
189 					psf->write_short	= dpcm_write_s2dsc ;
190 					psf->write_int		= dpcm_write_i2dsc ;
191 					psf->write_float	= dpcm_write_f2dsc ;
192 					psf->write_double	= dpcm_write_d2dsc ;
193 					break ;
194 			case 2 :
195 					psf->write_short	= dpcm_write_s2dles ;
196 					psf->write_int		= dpcm_write_i2dles ;
197 					psf->write_float	= dpcm_write_f2dles ;
198 					psf->write_double	= dpcm_write_d2dles ;
199 					break ;
200 			default :
201 				psf_log_printf (psf, "dpcm_init() returning SFE_UNIMPLEMENTED\n") ;
202 				return SFE_UNIMPLEMENTED ;
203 			} ;
204 		} ;
205 
206 	psf->filelength = psf_get_filelen (psf) ;
207 	psf->datalength = (psf->dataend) ? psf->dataend - psf->dataoffset :
208 							psf->filelength - psf->dataoffset ;
209 	psf->sf.frames = psf->datalength / psf->blockwidth ;
210 
211 	return 0 ;
212 } /* dpcm_init */
213 
214 /*==============================================================================
215 */
216 
217 static sf_count_t
dpcm_seek(SF_PRIVATE * psf,int mode,sf_count_t offset)218 dpcm_seek (SF_PRIVATE *psf, int mode, sf_count_t offset)
219 {	BUF_UNION	ubuf ;
220 	XI_PRIVATE	*pxi ;
221 	int			total, bufferlen, len ;
222 
223 	if ((pxi = psf->codec_data) == NULL)
224 		return SFE_INTERNAL ;
225 
226 	if (psf->datalength < 0 || psf->dataoffset < 0)
227 	{	psf->error = SFE_BAD_SEEK ;
228 		return	PSF_SEEK_ERROR ;
229 		} ;
230 
231 	if (offset == 0)
232 	{	psf_fseek (psf, psf->dataoffset, SEEK_SET) ;
233 		pxi->last_16 = 0 ;
234 		return 0 ;
235 		} ;
236 
237 	if (offset < 0 || offset > psf->sf.frames)
238 	{	psf->error = SFE_BAD_SEEK ;
239 		return	PSF_SEEK_ERROR ;
240 		} ;
241 
242 	if (mode != SFM_READ)
243 	{	/* What to do about write??? */
244 		psf->error = SFE_BAD_SEEK ;
245 		return	PSF_SEEK_ERROR ;
246 		} ;
247 
248 	psf_fseek (psf, psf->dataoffset, SEEK_SET) ;
249 
250 	if ((SF_CODEC (psf->sf.format)) == SF_FORMAT_DPCM_16)
251 	{	total = offset ;
252 		bufferlen = ARRAY_LEN (ubuf.sbuf) ;
253 		while (total > 0)
254 		{	len = (total > bufferlen) ? bufferlen : total ;
255 			total -= dpcm_read_dles2s (psf, ubuf.sbuf, len) ;
256 			} ;
257 		}
258 	else
259 	{	total = offset ;
260 		bufferlen = ARRAY_LEN (ubuf.sbuf) ;
261 		while (total > 0)
262 		{	len = (total > bufferlen) ? bufferlen : total ;
263 			total -= dpcm_read_dsc2s (psf, ubuf.sbuf, len) ;
264 			} ;
265 		} ;
266 
267 	return offset ;
268 } /* dpcm_seek */
269 
270 
271 static int
xi_write_header(SF_PRIVATE * psf,int UNUSED (calc_length))272 xi_write_header (SF_PRIVATE *psf, int UNUSED (calc_length))
273 {	XI_PRIVATE	*pxi ;
274 	sf_count_t	current ;
275 	const char	*string ;
276 
277 	if ((pxi = psf->codec_data) == NULL)
278 		return SFE_INTERNAL ;
279 
280 	current = psf_ftell (psf) ;
281 
282 	/* Reset the current header length to zero. */
283 	psf->header.ptr [0] = 0 ;
284 	psf->header.indx = 0 ;
285 	psf_fseek (psf, 0, SEEK_SET) ;
286 
287 	string = "Extended Instrument: " ;
288 	psf_binheader_writef (psf, "b", BHWv (string), BHWz (strlen (string))) ;
289 	psf_binheader_writef (psf, "b1", BHWv (pxi->filename), BHWz (sizeof (pxi->filename)), BHW1 (0x1A)) ;
290 
291 	/* Write software version and two byte XI version. */
292 	psf_binheader_writef (psf, "eb2", BHWv (pxi->software), BHWz (sizeof (pxi->software)), BHW2 ((1 << 8) + 2)) ;
293 
294 	/*
295 	** Jump note numbers (96), volume envelope (48), pan envelope (48),
296 	** volume points (1), pan points (1)
297 	*/
298 	psf_binheader_writef (psf, "z", BHWz ((size_t) (96 + 48 + 48 + 1 + 1))) ;
299 
300 	/* Jump volume loop (3 bytes), pan loop (3), envelope flags (3), vibrato (3)
301 	** fade out (2), 22 unknown bytes, and then write sample_count (2 bytes).
302 	*/
303 	psf_binheader_writef (psf, "ez2z2", BHWz ((size_t) (4 * 3)), BHW2 (0x1234), BHWz (22), BHW2 (1)) ;
304 
305 	pxi->loop_begin = 0 ;
306 	pxi->loop_end = 0 ;
307 
308 	psf_binheader_writef (psf, "et844", BHW8 (psf->sf.frames), BHW4 (pxi->loop_begin), BHW4 (pxi->loop_end)) ;
309 
310 	/* volume, fine tune, flags, pan, note, namelen */
311 	psf_binheader_writef (psf, "111111", BHW1 (128), BHW1 (0), BHW1 (pxi->sample_flags), BHW1 (128), BHW1 (0), BHW1 (strlen (pxi->sample_name))) ;
312 
313 	psf_binheader_writef (psf, "b", BHWv (pxi->sample_name), BHWz (sizeof (pxi->sample_name))) ;
314 
315 
316 
317 
318 
319 	/* Header construction complete so write it out. */
320 	psf_fwrite (psf->header.ptr, psf->header.indx, 1, psf) ;
321 
322 	if (psf->error)
323 		return psf->error ;
324 
325 	psf->dataoffset = psf->header.indx ;
326 
327 	if (current > 0)
328 		psf_fseek (psf, current, SEEK_SET) ;
329 
330 	return psf->error ;
331 } /* xi_write_header */
332 
333 static int
xi_read_header(SF_PRIVATE * psf)334 xi_read_header (SF_PRIVATE *psf)
335 {	char	buffer [64], name [32] ;
336 	short	version, fade_out, sample_count ;
337 	int		k, loop_begin, loop_end ;
338 	int 	sample_sizes [MAX_XI_SAMPLES] ;
339 
340 	psf_binheader_readf (psf, "pb", 0, buffer, 21) ;
341 
342 	memset (sample_sizes, 0, sizeof (sample_sizes)) ;
343 
344 	buffer [20] = 0 ;
345 	if (strcmp (buffer, "Extended Instrument:") != 0)
346 		return SFE_XI_BAD_HEADER ;
347 
348 	memset (buffer, 0, sizeof (buffer)) ;
349 	psf_binheader_readf (psf, "b", buffer, 23) ;
350 
351 	if (buffer [22] != 0x1A)
352 		return SFE_XI_BAD_HEADER ;
353 
354 	buffer [22] = 0 ;
355 	for (k = 21 ; k >= 0 && buffer [k] == ' ' ; k --)
356 		buffer [k] = 0 ;
357 
358 	psf_log_printf (psf, "Extended Instrument : %s\n", buffer) ;
359 	psf_store_string (psf, SF_STR_TITLE, buffer) ;
360 
361 	psf_binheader_readf (psf, "be2", buffer, 20, &version) ;
362 	buffer [19] = 0 ;
363 	for (k = 18 ; k >= 0 && buffer [k] == ' ' ; k --)
364 		buffer [k] = 0 ;
365 
366 	psf_log_printf (psf, "Software : %s\nVersion  : %d.%02d\n", buffer, version / 256, version % 256) ;
367 	psf_store_string (psf, SF_STR_SOFTWARE, buffer) ;
368 
369 	/* Jump note numbers (96), volume envelope (48), pan envelope (48),
370 	** volume points (1), pan points (1)
371 	*/
372 	psf_binheader_readf (psf, "j", 96 + 48 + 48 + 1 + 1) ;
373 
374 	psf_binheader_readf (psf, "b", buffer, 12) ;
375 	psf_log_printf (psf, "Volume Loop\n  sustain : %u\n  begin   : %u\n  end     : %u\n",
376 						buffer [0], buffer [1], buffer [2]) ;
377 	psf_log_printf (psf, "Pan Loop\n  sustain : %u\n  begin   : %u\n  end     : %u\n",
378 						buffer [3], buffer [4], buffer [5]) ;
379 	psf_log_printf (psf, "Envelope Flags\n  volume  : 0x%X\n  pan     : 0x%X\n",
380 				buffer [6] & 0xFF, buffer [7] & 0xFF) ;
381 
382 	psf_log_printf (psf, "Vibrato\n  type    : %u\n  sweep   : %u\n  depth   : %u\n  rate    : %u\n",
383 				buffer [8], buffer [9], buffer [10], buffer [11]) ;
384 
385 	/*
386 	** Read fade_out then jump reserved (2 bytes) and ???? (20 bytes) and
387 	** sample_count.
388 	*/
389 	psf_binheader_readf (psf, "e2j2", &fade_out, 2 + 20, &sample_count) ;
390 	psf_log_printf (psf, "Fade out  : %d\n", fade_out) ;
391 
392 	/* XI file can contain up to 16 samples. */
393 	if (sample_count > MAX_XI_SAMPLES)
394 		return SFE_XI_EXCESS_SAMPLES ;
395 
396 	if (psf->instrument == NULL && (psf->instrument = psf_instrument_alloc ()) == NULL)
397 		return SFE_MALLOC_FAILED ;
398 
399 	psf->instrument->basenote = 0 ;
400 	/* Log all data for each sample. */
401 	for (k = 0 ; k < sample_count ; k++)
402 	{	psf_binheader_readf (psf, "e444", &(sample_sizes [k]), &loop_begin, &loop_end) ;
403 
404 		/* Read 5 know bytes, 1 unknown byte and 22 name bytes. */
405 		psf_binheader_readf (psf, "bb", buffer, 6, name, 22) ;
406 		name [21] = 0 ;
407 
408 		psf_log_printf (psf, "Sample #%d\n  name    : %s\n", k + 1, name) ;
409 
410 		psf_log_printf (psf, "  size    : %d\n", sample_sizes [k]) ;
411 
412 
413 
414 		psf_log_printf (psf, "  loop\n    begin : %d\n    end   : %d\n", loop_begin, loop_end) ;
415 
416 		psf_log_printf (psf, "  volume  : %u\n  f. tune : %d\n  flags   : 0x%02X ",
417 					buffer [0] & 0xFF, buffer [1] & 0xFF, buffer [2] & 0xFF) ;
418 
419 		psf_log_printf (psf, " (") ;
420 		if (buffer [2] & 1)
421 			psf_log_printf (psf, " Loop") ;
422 		if (buffer [2] & 2)
423 			psf_log_printf (psf, " PingPong") ;
424 		psf_log_printf (psf, (buffer [2] & 16) ? " 16bit" : " 8bit") ;
425 		psf_log_printf (psf, " )\n") ;
426 
427 		psf_log_printf (psf, "  pan     : %u\n  note    : %d\n  namelen : %d\n",
428 					buffer [3] & 0xFF, buffer [4], buffer [5]) ;
429 
430 		psf->instrument->basenote = buffer [4] ;
431 		if (buffer [2] & 1)
432 		{	psf->instrument->loop_count = 1 ;
433 			psf->instrument->loops [0].mode = (buffer [2] & 2) ? SF_LOOP_ALTERNATING : SF_LOOP_FORWARD ;
434 			psf->instrument->loops [0].start = loop_begin ;
435 			psf->instrument->loops [0].end = loop_end ;
436 			} ;
437 
438 		if (k != 0)
439 			continue ;
440 
441 		if (buffer [2] & 16)
442 		{	psf->sf.format = SF_FORMAT_XI | SF_FORMAT_DPCM_16 ;
443 			psf->bytewidth = 2 ;
444 			}
445 		else
446 		{	psf->sf.format = SF_FORMAT_XI | SF_FORMAT_DPCM_8 ;
447 			psf->bytewidth = 1 ;
448 			} ;
449 		} ;
450 
451 	while (sample_count > 1 && sample_sizes [sample_count - 1] == 0)
452 		sample_count -- ;
453 
454 	/* Currently, we can only handle 1 sample per file. */
455 
456 	if (sample_count > 2)
457 	{	psf_log_printf (psf, "*** Sample count is less than 16 but more than 1.\n") ;
458 		psf_log_printf (psf, "  sample count : %d    sample_sizes [%d] : %d\n",
459 						sample_count, sample_count - 1, sample_sizes [sample_count - 1]) ;
460 		return SFE_XI_EXCESS_SAMPLES ;
461 		} ;
462 
463 	psf->datalength = sample_sizes [0] ;
464 
465 	psf->dataoffset = psf_ftell (psf) ;
466 	if (psf->dataoffset < 0)
467 	{	psf_log_printf (psf, "*** Bad Data Offset : %D\n", psf->dataoffset) ;
468 		return SFE_BAD_OFFSET ;
469 		} ;
470 	psf_log_printf (psf, "Data Offset : %D\n", psf->dataoffset) ;
471 
472 	if (psf->dataoffset + psf->datalength > psf->filelength)
473 	{	psf_log_printf (psf, "*** File seems to be truncated. Should be at least %D bytes long.\n",
474 				psf->dataoffset + sample_sizes [0]) ;
475 		psf->datalength = psf->filelength - psf->dataoffset ;
476 		} ;
477 
478 	if (psf_fseek (psf, psf->dataoffset, SEEK_SET) != psf->dataoffset)
479 		return SFE_BAD_SEEK ;
480 
481 	psf->endian = SF_ENDIAN_LITTLE ;
482 	psf->sf.channels = 1 ; /* Always mono */
483 	psf->sf.samplerate = 44100 ; /* Always */
484 
485 	psf->blockwidth = psf->sf.channels * psf->bytewidth ;
486 
487 	if (! psf->sf.frames && psf->blockwidth)
488 		psf->sf.frames = (psf->filelength - psf->dataoffset) / psf->blockwidth ;
489 
490 	psf->instrument->gain = 1 ;
491 	psf->instrument->velocity_lo = psf->instrument->key_lo = 0 ;
492 	psf->instrument->velocity_hi = psf->instrument->key_hi = 127 ;
493 
494 	return 0 ;
495 } /* xi_read_header */
496 
497 /*==============================================================================
498 */
499 
500 static void dsc2s_array (XI_PRIVATE *pxi, signed char *src, int count, short *dest) ;
501 static void dsc2i_array (XI_PRIVATE *pxi, signed char *src, int count, int *dest) ;
502 static void dsc2f_array (XI_PRIVATE *pxi, signed char *src, int count, float *dest, float normfact) ;
503 static void dsc2d_array (XI_PRIVATE *pxi, signed char *src, int count, double *dest, double normfact) ;
504 
505 static void dles2s_array (XI_PRIVATE *pxi, short *src, int count, short *dest) ;
506 static void dles2i_array (XI_PRIVATE *pxi, short *src, int count, int *dest) ;
507 static void dles2f_array (XI_PRIVATE *pxi, short *src, int count, float *dest, float normfact) ;
508 static void dles2d_array (XI_PRIVATE *pxi, short *src, int count, double *dest, double normfact) ;
509 
510 static sf_count_t
dpcm_read_dsc2s(SF_PRIVATE * psf,short * ptr,sf_count_t len)511 dpcm_read_dsc2s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
512 {	BUF_UNION	ubuf ;
513 	XI_PRIVATE	*pxi ;
514 	int			bufferlen, readcount ;
515 	sf_count_t	total = 0 ;
516 
517 	if ((pxi = psf->codec_data) == NULL)
518 		return 0 ;
519 
520 	bufferlen = ARRAY_LEN (ubuf.ucbuf) ;
521 
522 	while (len > 0)
523 	{	if (len < bufferlen)
524 			bufferlen = (int) len ;
525 		readcount = psf_fread (ubuf.scbuf, sizeof (signed char), bufferlen, psf) ;
526 		dsc2s_array (pxi, ubuf.scbuf, readcount, ptr + total) ;
527 		total += readcount ;
528 		if (readcount < bufferlen)
529 			break ;
530 		len -= readcount ;
531 		} ;
532 
533 	return total ;
534 } /* dpcm_read_dsc2s */
535 
536 static sf_count_t
dpcm_read_dsc2i(SF_PRIVATE * psf,int * ptr,sf_count_t len)537 dpcm_read_dsc2i (SF_PRIVATE *psf, int *ptr, sf_count_t len)
538 {	BUF_UNION	ubuf ;
539 	XI_PRIVATE	*pxi ;
540 	int			bufferlen, readcount ;
541 	sf_count_t	total = 0 ;
542 
543 	if ((pxi = psf->codec_data) == NULL)
544 		return 0 ;
545 
546 	bufferlen = ARRAY_LEN (ubuf.ucbuf) ;
547 
548 	while (len > 0)
549 	{	if (len < bufferlen)
550 			bufferlen = (int) len ;
551 		readcount = psf_fread (ubuf.scbuf, sizeof (signed char), bufferlen, psf) ;
552 		dsc2i_array (pxi, ubuf.scbuf, readcount, ptr + total) ;
553 		total += readcount ;
554 		if (readcount < bufferlen)
555 			break ;
556 		len -= readcount ;
557 		} ;
558 
559 	return total ;
560 } /* dpcm_read_dsc2i */
561 
562 static sf_count_t
dpcm_read_dsc2f(SF_PRIVATE * psf,float * ptr,sf_count_t len)563 dpcm_read_dsc2f (SF_PRIVATE *psf, float *ptr, sf_count_t len)
564 {	BUF_UNION	ubuf ;
565 	XI_PRIVATE	*pxi ;
566 	int			bufferlen, readcount ;
567 	sf_count_t	total = 0 ;
568 	float		normfact ;
569 
570 	if ((pxi = psf->codec_data) == NULL)
571 		return 0 ;
572 
573 	normfact = (psf->norm_float == SF_TRUE) ? 1.0 / ((float) 0x80) : 1.0 ;
574 
575 	bufferlen = ARRAY_LEN (ubuf.ucbuf) ;
576 
577 	while (len > 0)
578 	{	if (len < bufferlen)
579 			bufferlen = (int) len ;
580 		readcount = psf_fread (ubuf.scbuf, sizeof (signed char), bufferlen, psf) ;
581 		dsc2f_array (pxi, ubuf.scbuf, readcount, ptr + total, normfact) ;
582 		total += readcount ;
583 		if (readcount < bufferlen)
584 			break ;
585 		len -= readcount ;
586 		} ;
587 
588 	return total ;
589 } /* dpcm_read_dsc2f */
590 
591 static sf_count_t
dpcm_read_dsc2d(SF_PRIVATE * psf,double * ptr,sf_count_t len)592 dpcm_read_dsc2d (SF_PRIVATE *psf, double *ptr, sf_count_t len)
593 {	BUF_UNION	ubuf ;
594 	XI_PRIVATE	*pxi ;
595 	int			bufferlen, readcount ;
596 	sf_count_t	total = 0 ;
597 	double		normfact ;
598 
599 	if ((pxi = psf->codec_data) == NULL)
600 		return 0 ;
601 
602 	normfact = (psf->norm_double == SF_TRUE) ? 1.0 / ((double) 0x80) : 1.0 ;
603 
604 	bufferlen = ARRAY_LEN (ubuf.ucbuf) ;
605 
606 	while (len > 0)
607 	{	if (len < bufferlen)
608 			bufferlen = (int) len ;
609 		readcount = psf_fread (ubuf.scbuf, sizeof (signed char), bufferlen, psf) ;
610 		dsc2d_array (pxi, ubuf.scbuf, readcount, ptr + total, normfact) ;
611 		total += readcount ;
612 		if (readcount < bufferlen)
613 			break ;
614 		len -= readcount ;
615 		} ;
616 
617 	return total ;
618 } /* dpcm_read_dsc2d */
619 
620 /*------------------------------------------------------------------------------
621 */
622 
623 static sf_count_t
dpcm_read_dles2s(SF_PRIVATE * psf,short * ptr,sf_count_t len)624 dpcm_read_dles2s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
625 {	BUF_UNION	ubuf ;
626 	XI_PRIVATE	*pxi ;
627 	int			bufferlen, readcount ;
628 	sf_count_t	total = 0 ;
629 
630 	if ((pxi = psf->codec_data) == NULL)
631 		return 0 ;
632 
633 	bufferlen = ARRAY_LEN (ubuf.sbuf) ;
634 
635 	while (len > 0)
636 	{	if (len < bufferlen)
637 			bufferlen = (int) len ;
638 		readcount = psf_fread (ubuf.sbuf, sizeof (short), bufferlen, psf) ;
639 		dles2s_array (pxi, ubuf.sbuf, readcount, ptr + total) ;
640 		total += readcount ;
641 		if (readcount < bufferlen)
642 			break ;
643 		len -= readcount ;
644 		} ;
645 
646 	return total ;
647 } /* dpcm_read_dles2s */
648 
649 static sf_count_t
dpcm_read_dles2i(SF_PRIVATE * psf,int * ptr,sf_count_t len)650 dpcm_read_dles2i (SF_PRIVATE *psf, int *ptr, sf_count_t len)
651 {	BUF_UNION	ubuf ;
652 	XI_PRIVATE	*pxi ;
653 	int			bufferlen, readcount ;
654 	sf_count_t	total = 0 ;
655 
656 	if ((pxi = psf->codec_data) == NULL)
657 		return 0 ;
658 
659 	bufferlen = ARRAY_LEN (ubuf.sbuf) ;
660 
661 	while (len > 0)
662 	{	if (len < bufferlen)
663 			bufferlen = (int) len ;
664 		readcount = psf_fread (ubuf.sbuf, sizeof (short), bufferlen, psf) ;
665 		dles2i_array (pxi, ubuf.sbuf, readcount, ptr + total) ;
666 		total += readcount ;
667 		if (readcount < bufferlen)
668 			break ;
669 		len -= readcount ;
670 		} ;
671 
672 	return total ;
673 } /* dpcm_read_dles2i */
674 
675 static sf_count_t
dpcm_read_dles2f(SF_PRIVATE * psf,float * ptr,sf_count_t len)676 dpcm_read_dles2f (SF_PRIVATE *psf, float *ptr, sf_count_t len)
677 {	BUF_UNION	ubuf ;
678 	XI_PRIVATE	*pxi ;
679 	int			bufferlen, readcount ;
680 	sf_count_t	total = 0 ;
681 	float		normfact ;
682 
683 	if ((pxi = psf->codec_data) == NULL)
684 		return 0 ;
685 
686 	normfact = (psf->norm_float == SF_TRUE) ? 1.0 / ((float) 0x8000) : 1.0 ;
687 
688 	bufferlen = ARRAY_LEN (ubuf.sbuf) ;
689 
690 	while (len > 0)
691 	{	if (len < bufferlen)
692 			bufferlen = (int) len ;
693 		readcount = psf_fread (ubuf.sbuf, sizeof (short), bufferlen, psf) ;
694 		dles2f_array (pxi, ubuf.sbuf, readcount, ptr + total, normfact) ;
695 		total += readcount ;
696 		if (readcount < bufferlen)
697 			break ;
698 		len -= readcount ;
699 		} ;
700 
701 	return total ;
702 } /* dpcm_read_dles2f */
703 
704 static sf_count_t
dpcm_read_dles2d(SF_PRIVATE * psf,double * ptr,sf_count_t len)705 dpcm_read_dles2d (SF_PRIVATE *psf, double *ptr, sf_count_t len)
706 {	BUF_UNION	ubuf ;
707 	XI_PRIVATE	*pxi ;
708 	int			bufferlen, readcount ;
709 	sf_count_t	total = 0 ;
710 	double		normfact ;
711 
712 	if ((pxi = psf->codec_data) == NULL)
713 		return 0 ;
714 
715 	normfact = (psf->norm_double == SF_TRUE) ? 1.0 / ((double) 0x8000) : 1.0 ;
716 
717 	bufferlen = ARRAY_LEN (ubuf.sbuf) ;
718 
719 	while (len > 0)
720 	{	if (len < bufferlen)
721 			bufferlen = (int) len ;
722 		readcount = psf_fread (ubuf.sbuf, sizeof (short), bufferlen, psf) ;
723 		dles2d_array (pxi, ubuf.sbuf, readcount, ptr + total, normfact) ;
724 		total += readcount ;
725 		if (readcount < bufferlen)
726 			break ;
727 		len -= readcount ;
728 		} ;
729 
730 	return total ;
731 } /* dpcm_read_dles2d */
732 
733 /*==============================================================================
734 */
735 
736 static void s2dsc_array (XI_PRIVATE *pxi, const short *src, signed char *dest, int count) ;
737 static void i2dsc_array (XI_PRIVATE *pxi, const int *src, signed char *dest, int count) ;
738 static void f2dsc_array (XI_PRIVATE *pxi, const float *src, signed char *dest, int count, float normfact) ;
739 static void d2dsc_array (XI_PRIVATE *pxi, const double *src, signed char *dest, int count, double normfact) ;
740 
741 static void	s2dles_array (XI_PRIVATE *pxi, const short *src, short *dest, int count) ;
742 static void i2dles_array (XI_PRIVATE *pxi, const int *src, short *dest, int count) ;
743 static void f2dles_array (XI_PRIVATE *pxi, const float *src, short *dest, int count, float normfact) ;
744 static void d2dles_array (XI_PRIVATE *pxi, const double *src, short *dest, int count, double normfact) ;
745 
746 
747 static sf_count_t
dpcm_write_s2dsc(SF_PRIVATE * psf,const short * ptr,sf_count_t len)748 dpcm_write_s2dsc (SF_PRIVATE *psf, const short *ptr, sf_count_t len)
749 {	BUF_UNION	ubuf ;
750 	XI_PRIVATE	*pxi ;
751 	int			bufferlen, writecount ;
752 	sf_count_t	total = 0 ;
753 
754 	if ((pxi = psf->codec_data) == NULL)
755 		return 0 ;
756 
757 	bufferlen = ARRAY_LEN (ubuf.ucbuf) ;
758 
759 	while (len > 0)
760 	{	if (len < bufferlen)
761 			bufferlen = (int) len ;
762 		s2dsc_array (pxi, ptr + total, ubuf.scbuf, bufferlen) ;
763 		writecount = psf_fwrite (ubuf.scbuf, sizeof (signed char), bufferlen, psf) ;
764 		total += writecount ;
765 		if (writecount < bufferlen)
766 			break ;
767 		len -= writecount ;
768 		} ;
769 
770 	return total ;
771 } /* dpcm_write_s2dsc */
772 
773 static sf_count_t
dpcm_write_i2dsc(SF_PRIVATE * psf,const int * ptr,sf_count_t len)774 dpcm_write_i2dsc (SF_PRIVATE *psf, const int *ptr, sf_count_t len)
775 {	BUF_UNION	ubuf ;
776 	XI_PRIVATE	*pxi ;
777 	int			bufferlen, writecount ;
778 	sf_count_t	total = 0 ;
779 
780 	if ((pxi = psf->codec_data) == NULL)
781 		return 0 ;
782 
783 	bufferlen = ARRAY_LEN (ubuf.ucbuf) ;
784 
785 	while (len > 0)
786 	{	if (len < bufferlen)
787 			bufferlen = (int) len ;
788 		i2dsc_array (pxi, ptr + total, ubuf.scbuf, bufferlen) ;
789 		writecount = psf_fwrite (ubuf.scbuf, sizeof (signed char), bufferlen, psf) ;
790 		total += writecount ;
791 		if (writecount < bufferlen)
792 			break ;
793 		len -= writecount ;
794 		} ;
795 
796 	return total ;
797 } /* dpcm_write_i2dsc */
798 
799 static sf_count_t
dpcm_write_f2dsc(SF_PRIVATE * psf,const float * ptr,sf_count_t len)800 dpcm_write_f2dsc (SF_PRIVATE *psf, const float *ptr, sf_count_t len)
801 {	BUF_UNION	ubuf ;
802 	XI_PRIVATE	*pxi ;
803 	int			bufferlen, writecount ;
804 	sf_count_t	total = 0 ;
805 	float		normfact ;
806 
807 	if ((pxi = psf->codec_data) == NULL)
808 		return 0 ;
809 
810 	normfact = (psf->norm_float == SF_TRUE) ? (1.0 * 0x7F) : 1.0 ;
811 
812 	bufferlen = ARRAY_LEN (ubuf.ucbuf) ;
813 
814 	while (len > 0)
815 	{	if (len < bufferlen)
816 			bufferlen = (int) len ;
817 		f2dsc_array (pxi, ptr + total, ubuf.scbuf, bufferlen, normfact) ;
818 		writecount = psf_fwrite (ubuf.scbuf, sizeof (signed char), bufferlen, psf) ;
819 		total += writecount ;
820 		if (writecount < bufferlen)
821 			break ;
822 		len -= writecount ;
823 		} ;
824 
825 	return total ;
826 } /* dpcm_write_f2dsc */
827 
828 static sf_count_t
dpcm_write_d2dsc(SF_PRIVATE * psf,const double * ptr,sf_count_t len)829 dpcm_write_d2dsc (SF_PRIVATE *psf, const double *ptr, sf_count_t len)
830 {	BUF_UNION	ubuf ;
831 	XI_PRIVATE	*pxi ;
832 	int			bufferlen, writecount ;
833 	sf_count_t	total = 0 ;
834 	double		normfact ;
835 
836 	if ((pxi = psf->codec_data) == NULL)
837 		return 0 ;
838 
839 	normfact = (psf->norm_double == SF_TRUE) ? (1.0 * 0x7F) : 1.0 ;
840 
841 	bufferlen = ARRAY_LEN (ubuf.ucbuf) ;
842 
843 	while (len > 0)
844 	{	if (len < bufferlen)
845 			bufferlen = (int) len ;
846 		d2dsc_array (pxi, ptr + total, ubuf.scbuf, bufferlen, normfact) ;
847 		writecount = psf_fwrite (ubuf.scbuf, sizeof (signed char), bufferlen, psf) ;
848 		total += writecount ;
849 		if (writecount < bufferlen)
850 			break ;
851 		len -= writecount ;
852 		} ;
853 
854 	return total ;
855 } /* dpcm_write_d2dsc */
856 
857 
858 static sf_count_t
dpcm_write_s2dles(SF_PRIVATE * psf,const short * ptr,sf_count_t len)859 dpcm_write_s2dles (SF_PRIVATE *psf, const short *ptr, sf_count_t len)
860 {	BUF_UNION	ubuf ;
861 	XI_PRIVATE	*pxi ;
862 	int			bufferlen, writecount ;
863 	sf_count_t	total = 0 ;
864 
865 	if ((pxi = psf->codec_data) == NULL)
866 		return 0 ;
867 
868 	bufferlen = ARRAY_LEN (ubuf.sbuf) ;
869 
870 	while (len > 0)
871 	{	if (len < bufferlen)
872 			bufferlen = (int) len ;
873 		s2dles_array (pxi, ptr + total, ubuf.sbuf, bufferlen) ;
874 		writecount = psf_fwrite (ubuf.sbuf, sizeof (short), bufferlen, psf) ;
875 		total += writecount ;
876 		if (writecount < bufferlen)
877 			break ;
878 		len -= writecount ;
879 		} ;
880 
881 	return total ;
882 } /* dpcm_write_s2dles */
883 
884 static sf_count_t
dpcm_write_i2dles(SF_PRIVATE * psf,const int * ptr,sf_count_t len)885 dpcm_write_i2dles (SF_PRIVATE *psf, const int *ptr, sf_count_t len)
886 {	BUF_UNION	ubuf ;
887 	XI_PRIVATE	*pxi ;
888 	int			bufferlen, writecount ;
889 	sf_count_t	total = 0 ;
890 
891 	if ((pxi = psf->codec_data) == NULL)
892 		return 0 ;
893 
894 	bufferlen = ARRAY_LEN (ubuf.sbuf) ;
895 
896 	while (len > 0)
897 	{	if (len < bufferlen)
898 			bufferlen = (int) len ;
899 		i2dles_array (pxi, ptr + total, ubuf.sbuf, bufferlen) ;
900 		writecount = psf_fwrite (ubuf.sbuf, sizeof (short), bufferlen, psf) ;
901 		total += writecount ;
902 		if (writecount < bufferlen)
903 			break ;
904 		len -= writecount ;
905 		} ;
906 
907 	return total ;
908 } /* dpcm_write_i2dles */
909 
910 static sf_count_t
dpcm_write_f2dles(SF_PRIVATE * psf,const float * ptr,sf_count_t len)911 dpcm_write_f2dles (SF_PRIVATE *psf, const float *ptr, sf_count_t len)
912 {	BUF_UNION	ubuf ;
913 	XI_PRIVATE	*pxi ;
914 	int			bufferlen, writecount ;
915 	sf_count_t	total = 0 ;
916 	float		normfact ;
917 
918 	if ((pxi = psf->codec_data) == NULL)
919 		return 0 ;
920 
921 	normfact = (psf->norm_float == SF_TRUE) ? (1.0 * 0x7FFF) : 1.0 ;
922 
923 	bufferlen = ARRAY_LEN (ubuf.sbuf) ;
924 
925 	while (len > 0)
926 	{	if (len < bufferlen)
927 			bufferlen = (int) len ;
928 		f2dles_array (pxi, ptr + total, ubuf.sbuf, bufferlen, normfact) ;
929 		writecount = psf_fwrite (ubuf.sbuf, sizeof (short), bufferlen, psf) ;
930 		total += writecount ;
931 		if (writecount < bufferlen)
932 			break ;
933 		len -= writecount ;
934 		} ;
935 
936 	return total ;
937 } /* dpcm_write_f2dles */
938 
939 static sf_count_t
dpcm_write_d2dles(SF_PRIVATE * psf,const double * ptr,sf_count_t len)940 dpcm_write_d2dles (SF_PRIVATE *psf, const double *ptr, sf_count_t len)
941 {	BUF_UNION	ubuf ;
942 	XI_PRIVATE	*pxi ;
943 	int			bufferlen, writecount ;
944 	sf_count_t	total = 0 ;
945 	double		normfact ;
946 
947 	if ((pxi = psf->codec_data) == NULL)
948 		return 0 ;
949 
950 	normfact = (psf->norm_double == SF_TRUE) ? (1.0 * 0x7FFF) : 1.0 ;
951 
952 	bufferlen = ARRAY_LEN (ubuf.sbuf) ;
953 
954 	while (len > 0)
955 	{	if (len < bufferlen)
956 			bufferlen = (int) len ;
957 		d2dles_array (pxi, ptr + total, ubuf.sbuf, bufferlen, normfact) ;
958 		writecount = psf_fwrite (ubuf.sbuf, sizeof (short), bufferlen, psf) ;
959 		total += writecount ;
960 		if (writecount < bufferlen)
961 			break ;
962 		len -= writecount ;
963 		} ;
964 
965 	return total ;
966 } /* dpcm_write_d2dles */
967 
968 
969 /*==============================================================================
970 */
971 
972 static void
dsc2s_array(XI_PRIVATE * pxi,signed char * src,int count,short * dest)973 dsc2s_array (XI_PRIVATE *pxi, signed char *src, int count, short *dest)
974 {	signed char	last_val ;
975 	int			k ;
976 
977 	last_val = pxi->last_16 >> 8 ;
978 
979 	for (k = 0 ; k < count ; k++)
980 	{	last_val += src [k] ;
981 		dest [k] = arith_shift_left (last_val, 8) ;
982 		} ;
983 
984 	pxi->last_16 = arith_shift_left (last_val, 8) ;
985 } /* dsc2s_array */
986 
987 static void
dsc2i_array(XI_PRIVATE * pxi,signed char * src,int count,int * dest)988 dsc2i_array (XI_PRIVATE *pxi, signed char *src, int count, int *dest)
989 {	signed char	last_val ;
990 	int			k ;
991 
992 	last_val = pxi->last_16 >> 8 ;
993 
994 	for (k = 0 ; k < count ; k++)
995 	{	last_val += src [k] ;
996 		dest [k] = arith_shift_left (last_val, 24) ;
997 		} ;
998 
999 	pxi->last_16 = arith_shift_left (last_val, 8) ;
1000 } /* dsc2i_array */
1001 
1002 static void
dsc2f_array(XI_PRIVATE * pxi,signed char * src,int count,float * dest,float normfact)1003 dsc2f_array (XI_PRIVATE *pxi, signed char *src, int count, float *dest, float normfact)
1004 {	signed char	last_val ;
1005 	int			k ;
1006 
1007 	last_val = pxi->last_16 >> 8 ;
1008 
1009 	for (k = 0 ; k < count ; k++)
1010 	{	last_val += src [k] ;
1011 		dest [k] = last_val * normfact ;
1012 		} ;
1013 
1014 	pxi->last_16 = arith_shift_left (last_val, 8) ;
1015 } /* dsc2f_array */
1016 
1017 static void
dsc2d_array(XI_PRIVATE * pxi,signed char * src,int count,double * dest,double normfact)1018 dsc2d_array (XI_PRIVATE *pxi, signed char *src, int count, double *dest, double normfact)
1019 {	signed char	last_val ;
1020 	int			k ;
1021 
1022 	last_val = pxi->last_16 >> 8 ;
1023 
1024 	for (k = 0 ; k < count ; k++)
1025 	{	last_val += src [k] ;
1026 		dest [k] = last_val * normfact ;
1027 		} ;
1028 
1029 	pxi->last_16 = arith_shift_left (last_val, 8) ;
1030 } /* dsc2d_array */
1031 
1032 /*------------------------------------------------------------------------------
1033 */
1034 
1035 static void
s2dsc_array(XI_PRIVATE * pxi,const short * src,signed char * dest,int count)1036 s2dsc_array (XI_PRIVATE *pxi, const short *src, signed char *dest, int count)
1037 {	signed char	last_val, current ;
1038 	int			k ;
1039 
1040 	last_val = pxi->last_16 >> 8 ;
1041 
1042 	for (k = 0 ; k < count ; k++)
1043 	{	current = src [k] >> 8 ;
1044 		dest [k] = current - last_val ;
1045 		last_val = current ;
1046 		} ;
1047 
1048 	pxi->last_16 = arith_shift_left (last_val, 8) ;
1049 } /* s2dsc_array */
1050 
1051 static void
i2dsc_array(XI_PRIVATE * pxi,const int * src,signed char * dest,int count)1052 i2dsc_array (XI_PRIVATE *pxi, const int *src, signed char *dest, int count)
1053 {	signed char	last_val, current ;
1054 	int			k ;
1055 
1056 	last_val = pxi->last_16 >> 8 ;
1057 
1058 	for (k = 0 ; k < count ; k++)
1059 	{	current = src [k] >> 24 ;
1060 		dest [k] = current - last_val ;
1061 		last_val = current ;
1062 		} ;
1063 
1064 	pxi->last_16 = arith_shift_left (last_val, 8) ;
1065 } /* i2dsc_array */
1066 
1067 static void
f2dsc_array(XI_PRIVATE * pxi,const float * src,signed char * dest,int count,float normfact)1068 f2dsc_array (XI_PRIVATE *pxi, const float *src, signed char *dest, int count, float normfact)
1069 {	signed char	last_val, current ;
1070 	int			k ;
1071 
1072 	last_val = pxi->last_16 >> 8 ;
1073 
1074 	for (k = 0 ; k < count ; k++)
1075 	{	current = psf_lrintf (src [k] * normfact) ;
1076 		dest [k] = current - last_val ;
1077 		last_val = current ;
1078 		} ;
1079 
1080 	pxi->last_16 = arith_shift_left (last_val, 8) ;
1081 } /* f2dsc_array */
1082 
1083 static void
d2dsc_array(XI_PRIVATE * pxi,const double * src,signed char * dest,int count,double normfact)1084 d2dsc_array (XI_PRIVATE *pxi, const double *src, signed char *dest, int count, double normfact)
1085 {	signed char	last_val, current ;
1086 	int			k ;
1087 
1088 	last_val = pxi->last_16 >> 8 ;
1089 
1090 	for (k = 0 ; k < count ; k++)
1091 	{	current = psf_lrint (src [k] * normfact) ;
1092 		dest [k] = current - last_val ;
1093 		last_val = current ;
1094 		} ;
1095 
1096 	pxi->last_16 = arith_shift_left (last_val, 8) ;
1097 } /* d2dsc_array */
1098 
1099 /*==============================================================================
1100 */
1101 
1102 static void
dles2s_array(XI_PRIVATE * pxi,short * src,int count,short * dest)1103 dles2s_array (XI_PRIVATE *pxi, short *src, int count, short *dest)
1104 {	short	last_val ;
1105 	int		k ;
1106 
1107 	last_val = pxi->last_16 ;
1108 
1109 	for (k = 0 ; k < count ; k++)
1110 	{	last_val += LE2H_16 (src [k]) ;
1111 		dest [k] = last_val ;
1112 		} ;
1113 
1114 	pxi->last_16 = last_val ;
1115 } /* dles2s_array */
1116 
1117 static void
dles2i_array(XI_PRIVATE * pxi,short * src,int count,int * dest)1118 dles2i_array (XI_PRIVATE *pxi, short *src, int count, int *dest)
1119 {	short	last_val ;
1120 	int		k ;
1121 
1122 	last_val = pxi->last_16 ;
1123 
1124 	for (k = 0 ; k < count ; k++)
1125 	{	last_val += LE2H_16 (src [k]) ;
1126 		dest [k] = arith_shift_left (last_val, 16) ;
1127 		} ;
1128 
1129 	pxi->last_16 = last_val ;
1130 } /* dles2i_array */
1131 
1132 static void
dles2f_array(XI_PRIVATE * pxi,short * src,int count,float * dest,float normfact)1133 dles2f_array (XI_PRIVATE *pxi, short *src, int count, float *dest, float normfact)
1134 {	short	last_val ;
1135 	int		k ;
1136 
1137 	last_val = pxi->last_16 ;
1138 
1139 	for (k = 0 ; k < count ; k++)
1140 	{	last_val += LE2H_16 (src [k]) ;
1141 		dest [k] = last_val * normfact ;
1142 		} ;
1143 
1144 	pxi->last_16 = last_val ;
1145 } /* dles2f_array */
1146 
1147 static void
dles2d_array(XI_PRIVATE * pxi,short * src,int count,double * dest,double normfact)1148 dles2d_array (XI_PRIVATE *pxi, short *src, int count, double *dest, double normfact)
1149 {	short	last_val ;
1150 	int		k ;
1151 
1152 	last_val = pxi->last_16 ;
1153 
1154 	for (k = 0 ; k < count ; k++)
1155 	{	last_val += LE2H_16 (src [k]) ;
1156 		dest [k] = last_val * normfact ;
1157 		} ;
1158 
1159 	pxi->last_16 = last_val ;
1160 } /* dles2d_array */
1161 
1162 /*------------------------------------------------------------------------------
1163 */
1164 
1165 static void
s2dles_array(XI_PRIVATE * pxi,const short * src,short * dest,int count)1166 s2dles_array (XI_PRIVATE *pxi, const short *src, short *dest, int count)
1167 {	short	diff, last_val ;
1168 	int		k ;
1169 
1170 	last_val = pxi->last_16 ;
1171 
1172 	for (k = 0 ; k < count ; k++)
1173 	{	diff = src [k] - last_val ;
1174 		dest [k] = LE2H_16 (diff) ;
1175 		last_val = src [k] ;
1176 		} ;
1177 
1178 	pxi->last_16 = last_val ;
1179 } /* s2dles_array */
1180 
1181 static void
i2dles_array(XI_PRIVATE * pxi,const int * src,short * dest,int count)1182 i2dles_array (XI_PRIVATE *pxi, const int *src, short *dest, int count)
1183 {	short	diff, last_val ;
1184 	int		k ;
1185 
1186 	last_val = pxi->last_16 ;
1187 
1188 	for (k = 0 ; k < count ; k++)
1189 	{	diff = (src [k] >> 16) - last_val ;
1190 		dest [k] = LE2H_16 (diff) ;
1191 		last_val = src [k] >> 16 ;
1192 		} ;
1193 
1194 	pxi->last_16 = last_val ;
1195 } /* i2dles_array */
1196 
1197 static void
f2dles_array(XI_PRIVATE * pxi,const float * src,short * dest,int count,float normfact)1198 f2dles_array (XI_PRIVATE *pxi, const float *src, short *dest, int count, float normfact)
1199 {	short	diff, last_val, current ;
1200 	int		k ;
1201 
1202 	last_val = pxi->last_16 ;
1203 
1204 	for (k = 0 ; k < count ; k++)
1205 	{	current = psf_lrintf (src [k] * normfact) ;
1206 		diff = current - last_val ;
1207 		dest [k] = LE2H_16 (diff) ;
1208 		last_val = current ;
1209 		} ;
1210 
1211 	pxi->last_16 = last_val ;
1212 } /* f2dles_array */
1213 
1214 static void
d2dles_array(XI_PRIVATE * pxi,const double * src,short * dest,int count,double normfact)1215 d2dles_array (XI_PRIVATE *pxi, const double *src, short *dest, int count, double normfact)
1216 {	short	diff, last_val, current ;
1217 	int		k ;
1218 
1219 	last_val = pxi->last_16 ;
1220 
1221 	for (k = 0 ; k < count ; k++)
1222 	{	current = psf_lrint (src [k] * normfact) ;
1223 		diff = current - last_val ;
1224 		dest [k] = LE2H_16 (diff) ;
1225 		last_val = current ;
1226 		} ;
1227 
1228 	pxi->last_16 = last_val ;
1229 } /* d2dles_array */
1230 
1231