xref: /third_party/libsnd/src/dwvw.c (revision b815c7f3)
1/*
2** Copyright (C) 2002-2014 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/*===========================================================================
20** Delta Word Variable Width
21**
22** This decoder and encoder were implemented using information found in this
23** document : http://home.swbell.net/rubywand/R011SNDFMTS.TXT
24**
25** According to the document, the algorithm "was invented 1991 by Magnus
26** Lidstrom and is copyright 1993 by NuEdge Development".
27*/
28
29#include	"sfconfig.h"
30
31#include	<stdio.h>
32#include	<stdlib.h>
33#include	<string.h>
34#include	<math.h>
35
36#include	"sndfile.h"
37#include	"sfendian.h"
38#include	"common.h"
39
40typedef struct
41{	int		bit_width, dwm_maxsize, max_delta, span ;
42	int		samplecount ;
43	int		bit_count, bits, last_delta_width, last_sample ;
44	struct
45	{	int				index, end ;
46		unsigned char	buffer [256] ;
47	} b ;
48} DWVW_PRIVATE ;
49
50/*============================================================================================
51*/
52
53static sf_count_t dwvw_read_s (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
54static sf_count_t dwvw_read_i (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
55static sf_count_t dwvw_read_f (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
56static sf_count_t dwvw_read_d (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
57
58static sf_count_t dwvw_write_s (SF_PRIVATE *psf, const short *ptr, sf_count_t len) ;
59static sf_count_t dwvw_write_i (SF_PRIVATE *psf, const int *ptr, sf_count_t len) ;
60static sf_count_t dwvw_write_f (SF_PRIVATE *psf, const float *ptr, sf_count_t len) ;
61static sf_count_t dwvw_write_d (SF_PRIVATE *psf, const double *ptr, sf_count_t len) ;
62
63static sf_count_t	dwvw_seek	(SF_PRIVATE *psf, int mode, sf_count_t offset) ;
64static int	dwvw_close		(SF_PRIVATE *psf) ;
65static int	dwvw_byterate	(SF_PRIVATE *psf) ;
66
67static int	dwvw_decode_data (SF_PRIVATE *psf, DWVW_PRIVATE *pdwvw, int *ptr, int len) ;
68static int	dwvw_decode_load_bits (SF_PRIVATE *psf, DWVW_PRIVATE *pdwvw, int bit_count) ;
69
70static int	dwvw_encode_data (SF_PRIVATE *psf, DWVW_PRIVATE *pdwvw, const int *ptr, int len) ;
71static void dwvw_encode_store_bits (SF_PRIVATE *psf, DWVW_PRIVATE *pdwvw, int data, int new_bits) ;
72static void dwvw_read_reset (DWVW_PRIVATE *pdwvw) ;
73
74/*============================================================================================
75** DWVW initialisation function.
76*/
77
78int
79dwvw_init (SF_PRIVATE *psf, int bitwidth)
80{	DWVW_PRIVATE	*pdwvw ;
81
82	if (psf->codec_data != NULL)
83	{	psf_log_printf (psf, "*** psf->codec_data is not NULL.\n") ;
84		return SFE_INTERNAL ;
85		} ;
86
87	if (bitwidth > 24)
88		return SFE_DWVW_BAD_BITWIDTH ;
89
90	if (psf->file.mode == SFM_RDWR)
91		return SFE_BAD_MODE_RW ;
92
93	if ((pdwvw = calloc (1, sizeof (DWVW_PRIVATE))) == NULL)
94		return SFE_MALLOC_FAILED ;
95
96	psf->codec_data = (void*) pdwvw ;
97	pdwvw->bit_width 	= bitwidth ;
98	dwvw_read_reset (pdwvw) ;
99
100	if (psf->file.mode == SFM_READ)
101	{	psf->read_short		= dwvw_read_s ;
102		psf->read_int		= dwvw_read_i ;
103		psf->read_float		= dwvw_read_f ;
104		psf->read_double	= dwvw_read_d ;
105		} ;
106
107	if (psf->file.mode == SFM_WRITE)
108	{	psf->write_short	= dwvw_write_s ;
109		psf->write_int		= dwvw_write_i ;
110		psf->write_float	= dwvw_write_f ;
111		psf->write_double	= dwvw_write_d ;
112		} ;
113
114	psf->codec_close = dwvw_close ;
115	psf->seek = dwvw_seek ;
116	psf->byterate = dwvw_byterate ;
117
118	if (psf->file.mode == SFM_READ)
119	{	psf->sf.frames = psf_decode_frame_count (psf) ;
120		dwvw_read_reset (pdwvw) ;
121		} ;
122
123	return 0 ;
124} /* dwvw_init */
125
126/*--------------------------------------------------------------------------------------------
127*/
128
129static int
130dwvw_close (SF_PRIVATE *psf)
131{	DWVW_PRIVATE *pdwvw ;
132
133	if (psf->codec_data == NULL)
134		return 0 ;
135	pdwvw = (DWVW_PRIVATE*) psf->codec_data ;
136
137	if (psf->file.mode == SFM_WRITE)
138	{	static int last_values [12] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } ;
139
140		/* Write 8 zero samples to fully flush output. */
141		dwvw_encode_data (psf, pdwvw, last_values, 12) ;
142
143		/* Write the last buffer worth of data to disk. */
144		psf_fwrite (pdwvw->b.buffer, 1, pdwvw->b.index, psf) ;
145
146		if (psf->write_header)
147			psf->write_header (psf, SF_TRUE) ;
148		} ;
149
150	return 0 ;
151} /* dwvw_close */
152
153static sf_count_t
154dwvw_seek	(SF_PRIVATE *psf, int UNUSED (mode), sf_count_t offset)
155{	DWVW_PRIVATE *pdwvw ;
156
157	if (! psf->codec_data)
158	{	psf->error = SFE_INTERNAL ;
159		return PSF_SEEK_ERROR ;
160		} ;
161
162	pdwvw = (DWVW_PRIVATE*) psf->codec_data ;
163
164	if (offset == 0)
165	{	psf_fseek (psf, psf->dataoffset, SEEK_SET) ;
166		dwvw_read_reset (pdwvw) ;
167		return 0 ;
168		} ;
169
170	psf->error = SFE_BAD_SEEK ;
171	return	PSF_SEEK_ERROR ;
172} /* dwvw_seek */
173
174static int
175dwvw_byterate	(SF_PRIVATE *psf)
176{
177	if (psf->file.mode == SFM_READ)
178		return (psf->datalength * psf->sf.samplerate) / psf->sf.frames ;
179
180	return -1 ;
181} /* dwvw_byterate */
182
183/*==============================================================================
184*/
185
186static sf_count_t
187dwvw_read_s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
188{	DWVW_PRIVATE *pdwvw ;
189	BUF_UNION	ubuf ;
190	int		*iptr ;
191	int		k, bufferlen, readcount = 0, count ;
192	sf_count_t	total = 0 ;
193
194	if (! psf->codec_data)
195		return 0 ;
196	pdwvw = (DWVW_PRIVATE*) psf->codec_data ;
197
198	iptr = ubuf.ibuf ;
199	bufferlen = ARRAY_LEN (ubuf.ibuf) ;
200	while (len > 0)
201	{	readcount = (len >= bufferlen) ? bufferlen : (int) len ;
202		count = dwvw_decode_data (psf, pdwvw, iptr, readcount) ;
203		for (k = 0 ; k < readcount ; k++)
204			ptr [total + k] = iptr [k] >> 16 ;
205
206		total += count ;
207		len -= readcount ;
208		if (count != readcount)
209			break ;
210		} ;
211
212	return total ;
213} /* dwvw_read_s */
214
215static sf_count_t
216dwvw_read_i (SF_PRIVATE *psf, int *ptr, sf_count_t len)
217{	DWVW_PRIVATE *pdwvw ;
218	int			readcount, count ;
219	sf_count_t	total = 0 ;
220
221	if (! psf->codec_data)
222		return 0 ;
223	pdwvw = (DWVW_PRIVATE*) psf->codec_data ;
224
225	while (len > 0)
226	{	readcount = (len > 0x10000000) ? 0x10000000 : (int) len ;
227
228		count = dwvw_decode_data (psf, pdwvw, ptr, readcount) ;
229
230		total += count ;
231		len -= count ;
232
233		if (count != readcount)
234			break ;
235		} ;
236
237	return total ;
238} /* dwvw_read_i */
239
240static sf_count_t
241dwvw_read_f (SF_PRIVATE *psf, float *ptr, sf_count_t len)
242{	DWVW_PRIVATE *pdwvw ;
243	BUF_UNION	ubuf ;
244	int			*iptr ;
245	int			k, bufferlen, readcount = 0, count ;
246	sf_count_t	total = 0 ;
247	float	normfact ;
248
249	if (! psf->codec_data)
250		return 0 ;
251	pdwvw = (DWVW_PRIVATE*) psf->codec_data ;
252
253	normfact = (psf->norm_float == SF_TRUE) ? 1.0 / ((float) 0x80000000) : 1.0 ;
254
255	iptr = ubuf.ibuf ;
256	bufferlen = ARRAY_LEN (ubuf.ibuf) ;
257	while (len > 0)
258	{	readcount = (len >= bufferlen) ? bufferlen : (int) len ;
259		count = dwvw_decode_data (psf, pdwvw, iptr, readcount) ;
260		for (k = 0 ; k < readcount ; k++)
261			ptr [total + k] = normfact * (float) (iptr [k]) ;
262
263		total += count ;
264		len -= readcount ;
265		if (count != readcount)
266			break ;
267		} ;
268
269	return total ;
270} /* dwvw_read_f */
271
272static sf_count_t
273dwvw_read_d (SF_PRIVATE *psf, double *ptr, sf_count_t len)
274{	DWVW_PRIVATE *pdwvw ;
275	BUF_UNION	ubuf ;
276	int			*iptr ;
277	int			k, bufferlen, readcount = 0, count ;
278	sf_count_t	total = 0 ;
279	double 	normfact ;
280
281	if (! psf->codec_data)
282		return 0 ;
283	pdwvw = (DWVW_PRIVATE*) psf->codec_data ;
284
285	normfact = (psf->norm_double == SF_TRUE) ? 1.0 / ((double) 0x80000000) : 1.0 ;
286
287	iptr = ubuf.ibuf ;
288	bufferlen = ARRAY_LEN (ubuf.ibuf) ;
289	while (len > 0)
290	{	readcount = (len >= bufferlen) ? bufferlen : (int) len ;
291		count = dwvw_decode_data (psf, pdwvw, iptr, readcount) ;
292		for (k = 0 ; k < readcount ; k++)
293			ptr [total + k] = normfact * (double) (iptr [k]) ;
294
295		total += count ;
296		len -= readcount ;
297		if (count != readcount)
298			break ;
299		} ;
300
301	return total ;
302} /* dwvw_read_d */
303
304static int
305dwvw_decode_data (SF_PRIVATE *psf, DWVW_PRIVATE *pdwvw, int *ptr, int len)
306{	int	count ;
307	int delta_width_modifier, delta_width, delta_negative, delta, sample ;
308
309	/* Restore state from last decode call. */
310	delta_width = pdwvw->last_delta_width ;
311	sample = pdwvw->last_sample ;
312
313	for (count = 0 ; count < len ; count++)
314	{	/* If bit_count parameter is zero get the delta_width_modifier. */
315		delta_width_modifier = dwvw_decode_load_bits (psf, pdwvw, -1) ;
316
317		/* Check for end of input bit stream. Break loop if end. */
318		if (delta_width_modifier < 0 || (pdwvw->b.end == 0 && count == 0))
319			break ;
320
321		if (delta_width_modifier && dwvw_decode_load_bits (psf, pdwvw, 1))
322			delta_width_modifier = - delta_width_modifier ;
323
324		/* Calculate the current word width. */
325		delta_width = (delta_width + delta_width_modifier + pdwvw->bit_width) % pdwvw->bit_width ;
326
327		/* Load the delta. */
328		delta = 0 ;
329		if (delta_width)
330		{	delta = dwvw_decode_load_bits (psf, pdwvw, delta_width - 1) | (1 << (delta_width - 1)) ;
331			delta_negative = dwvw_decode_load_bits (psf, pdwvw, 1) ;
332			if (delta == pdwvw->max_delta - 1)
333				delta += dwvw_decode_load_bits (psf, pdwvw, 1) ;
334			if (delta_negative)
335				delta = -delta ;
336			} ;
337
338		/* Calculate the sample */
339		sample += delta ;
340
341		if (sample >= pdwvw->max_delta)
342			sample -= pdwvw->span ;
343		else if (sample < - pdwvw->max_delta)
344			sample += pdwvw->span ;
345
346		/* Store the sample justifying to the most significant bit. */
347		ptr [count] = arith_shift_left (sample, 32 - pdwvw->bit_width) ;
348
349		if (pdwvw->b.end == 0 && pdwvw->bit_count == 0)
350			break ;
351		} ;
352
353	pdwvw->last_delta_width = delta_width ;
354	pdwvw->last_sample = sample ;
355
356	pdwvw->samplecount += count ;
357
358	return count ;
359} /* dwvw_decode_data */
360
361static int
362dwvw_decode_load_bits (SF_PRIVATE *psf, DWVW_PRIVATE *pdwvw, int bit_count)
363{	int output = 0, get_dwm = SF_FALSE ;
364
365	/*
366	**	Depending on the value of parameter bit_count, either get the
367	**	required number of bits (ie bit_count > 0) or the
368	**	delta_width_modifier (otherwise).
369	*/
370
371	if (bit_count < 0)
372	{	get_dwm = SF_TRUE ;
373		/* modify bit_count to ensure we have enought bits for finding dwm. */
374		bit_count = pdwvw->dwm_maxsize ;
375		} ;
376
377	/* Load bits in bit reseviour. */
378	while (pdwvw->bit_count < bit_count)
379	{	if (pdwvw->b.index >= pdwvw->b.end)
380		{	pdwvw->b.end = (int) psf_fread (pdwvw->b.buffer, 1, sizeof (pdwvw->b.buffer), psf) ;
381			pdwvw->b.index = 0 ;
382			} ;
383
384		/* Check for end of input stream. */
385		if (bit_count < 8 && pdwvw->b.end == 0)
386			return -1 ;
387
388		pdwvw->bits = arith_shift_left (pdwvw->bits, 8) ;
389
390		if (pdwvw->b.index < pdwvw->b.end)
391		{	pdwvw->bits |= pdwvw->b.buffer [pdwvw->b.index] ;
392			pdwvw->b.index ++ ;
393			} ;
394		pdwvw->bit_count += 8 ;
395		} ;
396
397	/* If asked to get bits do so. */
398	if (! get_dwm)
399	{	output = (pdwvw->bits >> (pdwvw->bit_count - bit_count)) & ((1 << bit_count) - 1) ;
400		pdwvw->bit_count -= bit_count ;
401		return output ;
402		} ;
403
404	/* Otherwise must have been asked to get delta_width_modifier. */
405	while (output < (pdwvw->dwm_maxsize))
406	{	pdwvw->bit_count -= 1 ;
407		if (pdwvw->bits & (1 << pdwvw->bit_count))
408			break ;
409		output += 1 ;
410		} ;
411
412	return output ;
413} /* dwvw_decode_load_bits */
414
415static void
416dwvw_read_reset (DWVW_PRIVATE *pdwvw)
417{	int bitwidth = pdwvw->bit_width ;
418
419	memset (pdwvw, 0, sizeof (DWVW_PRIVATE)) ;
420
421	pdwvw->bit_width	= bitwidth ;
422	pdwvw->dwm_maxsize	= bitwidth / 2 ;
423	pdwvw->max_delta	= 1 << (bitwidth - 1) ;
424	pdwvw->span			= 1 << bitwidth ;
425} /* dwvw_read_reset */
426
427static void
428dwvw_encode_store_bits (SF_PRIVATE *psf, DWVW_PRIVATE *pdwvw, int data, int new_bits)
429{	int 	byte ;
430
431	/* Shift the bits into the resevoir. */
432	pdwvw->bits = arith_shift_left (pdwvw->bits, new_bits) | (data & (arith_shift_left (1, new_bits) - 1)) ;
433	pdwvw->bit_count += new_bits ;
434
435	/* Transfer bit to buffer. */
436	while (pdwvw->bit_count >= 8)
437	{	byte = pdwvw->bits >> (pdwvw->bit_count - 	8) ;
438		pdwvw->bit_count -= 8 ;
439		pdwvw->b.buffer [pdwvw->b.index] = byte & 0xFF ;
440		pdwvw->b.index ++ ;
441		} ;
442
443	if (pdwvw->b.index > SIGNED_SIZEOF (pdwvw->b.buffer) - 4)
444	{	psf_fwrite (pdwvw->b.buffer, 1, pdwvw->b.index, psf) ;
445		pdwvw->b.index = 0 ;
446		} ;
447
448	return ;
449} /* dwvw_encode_store_bits */
450
451#if 0
452/* Debigging routine. */
453static void
454dump_bits (DWVW_PRIVATE *pdwvw)
455{	int k, mask ;
456
457	for (k = 0 ; k < 10 && k < pdwvw->b.index ; k++)
458	{	mask = 0x80 ;
459		while (mask)
460		{	putchar (mask & pdwvw->b.buffer [k] ? '1' : '0') ;
461			mask >>= 1 ;
462			} ;
463		putchar (' ') ;
464		}
465
466	for (k = pdwvw->bit_count - 1 ; k >= 0 ; k --)
467		putchar (pdwvw->bits & (1 << k) ? '1' : '0') ;
468
469	putchar ('\n') ;
470} /* dump_bits */
471#endif
472
473#define HIGHEST_BIT(x, count)		\
474			{	int y = x ;			\
475				(count) = 0 ;		\
476				while (y)			\
477				{	(count) ++ ;	\
478					y >>= 1 ;		\
479					} ;				\
480				} ;
481
482static int
483dwvw_encode_data (SF_PRIVATE *psf, DWVW_PRIVATE *pdwvw, const int *ptr, int len)
484{	int	count ;
485	int delta_width_modifier, delta, delta_negative, delta_width, extra_bit ;
486
487	for (count = 0 ; count < len ; count++)
488	{	delta = (ptr [count] >> (32 - pdwvw->bit_width)) - pdwvw->last_sample ;
489
490		/* Calculate extra_bit if needed. */
491		extra_bit = -1 ;
492		delta_negative = 0 ;
493		if (delta < -pdwvw->max_delta)
494			delta = pdwvw->max_delta + (delta % pdwvw->max_delta) ;
495		else if (delta == -pdwvw->max_delta)
496		{	extra_bit = 1 ;
497			delta_negative = 1 ;
498			delta = pdwvw->max_delta - 1 ;
499			}
500		else if (delta > pdwvw->max_delta)
501		{	delta_negative = 1 ;
502			delta = pdwvw->span - delta ;
503			delta = abs (delta) ;
504			}
505		else if (delta == pdwvw->max_delta)
506		{	extra_bit = 1 ;
507			delta = pdwvw->max_delta - 1 ;
508			}
509		else if (delta < 0)
510		{	delta_negative = 1 ;
511			delta = abs (delta) ;
512			} ;
513
514		if (delta == pdwvw->max_delta - 1 && extra_bit == -1)
515			extra_bit = 0 ;
516
517		/* Find width in bits of delta */
518		HIGHEST_BIT (delta, delta_width) ;
519
520		/* Calculate the delta_width_modifier */
521		delta_width_modifier = (delta_width - pdwvw->last_delta_width) % pdwvw->bit_width ;
522		if (delta_width_modifier > pdwvw->dwm_maxsize)
523			delta_width_modifier -= pdwvw->bit_width ;
524		if (delta_width_modifier < -pdwvw->dwm_maxsize)
525			delta_width_modifier += pdwvw->bit_width ;
526
527		/* Write delta_width_modifier zeros, followed by terminating '1'. */
528		dwvw_encode_store_bits (psf, pdwvw, 0, abs (delta_width_modifier)) ;
529		if (abs (delta_width_modifier) != pdwvw->dwm_maxsize)
530			dwvw_encode_store_bits (psf, pdwvw, 1, 1) ;
531
532		/*  Write delta_width_modifier sign. */
533		if (delta_width_modifier < 0)
534			dwvw_encode_store_bits (psf, pdwvw, 1, 1) ;
535		if (delta_width_modifier > 0)
536			dwvw_encode_store_bits (psf, pdwvw, 0, 1) ;
537
538		/* Write delta and delta sign bit. */
539		if (delta_width)
540		{	dwvw_encode_store_bits (psf, pdwvw, delta, abs (delta_width) - 1) ;
541			dwvw_encode_store_bits (psf, pdwvw, (delta_negative ? 1 : 0), 1) ;
542			} ;
543
544		/* Write extra bit!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */
545		if (extra_bit >= 0)
546			dwvw_encode_store_bits (psf, pdwvw, extra_bit, 1) ;
547
548		pdwvw->last_sample = ptr [count] >> (32 - pdwvw->bit_width) ;
549		pdwvw->last_delta_width = delta_width ;
550		} ;
551
552	pdwvw->samplecount += count ;
553
554	return count ;
555} /* dwvw_encode_data */
556
557static sf_count_t
558dwvw_write_s (SF_PRIVATE *psf, const short *ptr, sf_count_t len)
559{	DWVW_PRIVATE *pdwvw ;
560	BUF_UNION	ubuf ;
561	int		*iptr ;
562	int		k, bufferlen, writecount = 0, count ;
563	sf_count_t	total = 0 ;
564
565	if (! psf->codec_data)
566		return 0 ;
567	pdwvw = (DWVW_PRIVATE*) psf->codec_data ;
568
569	iptr = ubuf.ibuf ;
570	bufferlen = ARRAY_LEN (ubuf.ibuf) ;
571	while (len > 0)
572	{	writecount = (len >= bufferlen) ? bufferlen : (int) len ;
573		for (k = 0 ; k < writecount ; k++)
574			iptr [k] = arith_shift_left (ptr [total + k], 16) ;
575		count = dwvw_encode_data (psf, pdwvw, iptr, writecount) ;
576
577		total += count ;
578		len -= writecount ;
579		if (count != writecount)
580			break ;
581		} ;
582
583	return total ;
584} /* dwvw_write_s */
585
586static sf_count_t
587dwvw_write_i (SF_PRIVATE *psf, const int *ptr, sf_count_t len)
588{	DWVW_PRIVATE *pdwvw ;
589	int			writecount, count ;
590	sf_count_t	total = 0 ;
591
592	if (! psf->codec_data)
593		return 0 ;
594	pdwvw = (DWVW_PRIVATE*) psf->codec_data ;
595
596	while (len > 0)
597	{	writecount = (len > 0x10000000) ? 0x10000000 : (int) len ;
598
599		count = dwvw_encode_data (psf, pdwvw, ptr, writecount) ;
600
601		total += count ;
602		len -= count ;
603
604		if (count != writecount)
605			break ;
606		} ;
607
608	return total ;
609} /* dwvw_write_i */
610
611static sf_count_t
612dwvw_write_f (SF_PRIVATE *psf, const float *ptr, sf_count_t len)
613{	DWVW_PRIVATE *pdwvw ;
614	BUF_UNION	ubuf ;
615	int			*iptr ;
616	int			k, bufferlen, writecount = 0, count ;
617	sf_count_t	total = 0 ;
618	float		normfact ;
619
620	if (! psf->codec_data)
621		return 0 ;
622	pdwvw = (DWVW_PRIVATE*) psf->codec_data ;
623
624	normfact = (psf->norm_float == SF_TRUE) ? (1.0 * 0x7FFFFFFF) : 1.0 ;
625
626	iptr = ubuf.ibuf ;
627	bufferlen = ARRAY_LEN (ubuf.ibuf) ;
628	while (len > 0)
629	{	writecount = (len >= bufferlen) ? bufferlen : (int) len ;
630		for (k = 0 ; k < writecount ; k++)
631			iptr [k] = psf_lrintf (normfact * ptr [total + k]) ;
632		count = dwvw_encode_data (psf, pdwvw, iptr, writecount) ;
633
634		total += count ;
635		len -= writecount ;
636		if (count != writecount)
637			break ;
638		} ;
639
640	return total ;
641} /* dwvw_write_f */
642
643static sf_count_t
644dwvw_write_d (SF_PRIVATE *psf, const double *ptr, sf_count_t len)
645{	DWVW_PRIVATE *pdwvw ;
646	BUF_UNION	ubuf ;
647	int			*iptr ;
648	int			k, bufferlen, writecount = 0, count ;
649	sf_count_t	total = 0 ;
650	double 		normfact ;
651
652	if (! psf->codec_data)
653		return 0 ;
654	pdwvw = (DWVW_PRIVATE*) psf->codec_data ;
655
656	normfact = (psf->norm_double == SF_TRUE) ? (1.0 * 0x7FFFFFFF) : 1.0 ;
657
658	iptr = ubuf.ibuf ;
659	bufferlen = ARRAY_LEN (ubuf.ibuf) ;
660	while (len > 0)
661	{	writecount = (len >= bufferlen) ? bufferlen : (int) len ;
662		for (k = 0 ; k < writecount ; k++)
663			iptr [k] = psf_lrint (normfact * ptr [total + k]) ;
664		count = dwvw_encode_data (psf, pdwvw, iptr, writecount) ;
665
666		total += count ;
667		len -= writecount ;
668		if (count != writecount)
669			break ;
670		} ;
671
672	return total ;
673} /* dwvw_write_d */
674
675