dwvw.c
上传用户:shw771010
上传日期:2022-01-05
资源大小:991k
文件大小:17k
源码类别:

Audio

开发平台:

Unix_Linux

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