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