/project/jni/sndfile/src/pcm.c

https://github.com/aichunyu/FFPlayer · C · 2901 lines · 2296 code · 518 blank · 87 comment · 395 complexity · 98344d59715f9dffedd871eba32ed2a2 MD5 · raw file

Large files are truncated click here to view the full file

  1. /*
  2. ** Copyright (C) 1999-2011 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. #include "sfconfig.h"
  19. #include <math.h>
  20. #include "sndfile.h"
  21. #include "sfendian.h"
  22. #include "common.h"
  23. /* Need to be able to handle 3 byte (24 bit) integers. So defined a
  24. ** type and use SIZEOF_TRIBYTE instead of (tribyte).
  25. */
  26. typedef void tribyte ;
  27. #define SIZEOF_TRIBYTE 3
  28. static sf_count_t pcm_read_sc2s (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
  29. static sf_count_t pcm_read_uc2s (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
  30. static sf_count_t pcm_read_bes2s (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
  31. static sf_count_t pcm_read_les2s (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
  32. static sf_count_t pcm_read_bet2s (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
  33. static sf_count_t pcm_read_let2s (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
  34. static sf_count_t pcm_read_bei2s (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
  35. static sf_count_t pcm_read_lei2s (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
  36. static sf_count_t pcm_read_sc2i (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
  37. static sf_count_t pcm_read_uc2i (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
  38. static sf_count_t pcm_read_bes2i (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
  39. static sf_count_t pcm_read_les2i (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
  40. static sf_count_t pcm_read_bet2i (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
  41. static sf_count_t pcm_read_let2i (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
  42. static sf_count_t pcm_read_bei2i (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
  43. static sf_count_t pcm_read_lei2i (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
  44. static sf_count_t pcm_read_sc2f (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
  45. static sf_count_t pcm_read_uc2f (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
  46. static sf_count_t pcm_read_bes2f (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
  47. static sf_count_t pcm_read_les2f (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
  48. static sf_count_t pcm_read_bet2f (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
  49. static sf_count_t pcm_read_let2f (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
  50. static sf_count_t pcm_read_bei2f (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
  51. static sf_count_t pcm_read_lei2f (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
  52. static sf_count_t pcm_read_sc2d (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
  53. static sf_count_t pcm_read_uc2d (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
  54. static sf_count_t pcm_read_bes2d (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
  55. static sf_count_t pcm_read_les2d (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
  56. static sf_count_t pcm_read_bet2d (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
  57. static sf_count_t pcm_read_let2d (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
  58. static sf_count_t pcm_read_bei2d (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
  59. static sf_count_t pcm_read_lei2d (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
  60. static sf_count_t pcm_write_s2sc (SF_PRIVATE *psf, const short *ptr, sf_count_t len) ;
  61. static sf_count_t pcm_write_s2uc (SF_PRIVATE *psf, const short *ptr, sf_count_t len) ;
  62. static sf_count_t pcm_write_s2bes (SF_PRIVATE *psf, const short *ptr, sf_count_t len) ;
  63. static sf_count_t pcm_write_s2les (SF_PRIVATE *psf, const short *ptr, sf_count_t len) ;
  64. static sf_count_t pcm_write_s2bet (SF_PRIVATE *psf, const short *ptr, sf_count_t len) ;
  65. static sf_count_t pcm_write_s2let (SF_PRIVATE *psf, const short *ptr, sf_count_t len) ;
  66. static sf_count_t pcm_write_s2bei (SF_PRIVATE *psf, const short *ptr, sf_count_t len) ;
  67. static sf_count_t pcm_write_s2lei (SF_PRIVATE *psf, const short *ptr, sf_count_t len) ;
  68. static sf_count_t pcm_write_i2sc (SF_PRIVATE *psf, const int *ptr, sf_count_t len) ;
  69. static sf_count_t pcm_write_i2uc (SF_PRIVATE *psf, const int *ptr, sf_count_t len) ;
  70. static sf_count_t pcm_write_i2bes (SF_PRIVATE *psf, const int *ptr, sf_count_t len) ;
  71. static sf_count_t pcm_write_i2les (SF_PRIVATE *psf, const int *ptr, sf_count_t len) ;
  72. static sf_count_t pcm_write_i2bet (SF_PRIVATE *psf, const int *ptr, sf_count_t len) ;
  73. static sf_count_t pcm_write_i2let (SF_PRIVATE *psf, const int *ptr, sf_count_t len) ;
  74. static sf_count_t pcm_write_i2bei (SF_PRIVATE *psf, const int *ptr, sf_count_t len) ;
  75. static sf_count_t pcm_write_i2lei (SF_PRIVATE *psf, const int *ptr, sf_count_t len) ;
  76. static sf_count_t pcm_write_f2sc (SF_PRIVATE *psf, const float *ptr, sf_count_t len) ;
  77. static sf_count_t pcm_write_f2uc (SF_PRIVATE *psf, const float *ptr, sf_count_t len) ;
  78. static sf_count_t pcm_write_f2bes (SF_PRIVATE *psf, const float *ptr, sf_count_t len) ;
  79. static sf_count_t pcm_write_f2les (SF_PRIVATE *psf, const float *ptr, sf_count_t len) ;
  80. static sf_count_t pcm_write_f2bet (SF_PRIVATE *psf, const float *ptr, sf_count_t len) ;
  81. static sf_count_t pcm_write_f2let (SF_PRIVATE *psf, const float *ptr, sf_count_t len) ;
  82. static sf_count_t pcm_write_f2bei (SF_PRIVATE *psf, const float *ptr, sf_count_t len) ;
  83. static sf_count_t pcm_write_f2lei (SF_PRIVATE *psf, const float *ptr, sf_count_t len) ;
  84. static sf_count_t pcm_write_d2sc (SF_PRIVATE *psf, const double *ptr, sf_count_t len) ;
  85. static sf_count_t pcm_write_d2uc (SF_PRIVATE *psf, const double *ptr, sf_count_t len) ;
  86. static sf_count_t pcm_write_d2bes (SF_PRIVATE *psf, const double *ptr, sf_count_t len) ;
  87. static sf_count_t pcm_write_d2les (SF_PRIVATE *psf, const double *ptr, sf_count_t len) ;
  88. static sf_count_t pcm_write_d2bet (SF_PRIVATE *psf, const double *ptr, sf_count_t len) ;
  89. static sf_count_t pcm_write_d2let (SF_PRIVATE *psf, const double *ptr, sf_count_t len) ;
  90. static sf_count_t pcm_write_d2bei (SF_PRIVATE *psf, const double *ptr, sf_count_t len) ;
  91. static sf_count_t pcm_write_d2lei (SF_PRIVATE *psf, const double *ptr, sf_count_t len) ;
  92. /*-----------------------------------------------------------------------------------------------
  93. */
  94. enum
  95. { /* Char type for 8 bit files. */
  96. SF_CHARS_SIGNED = 200,
  97. SF_CHARS_UNSIGNED = 201
  98. } ;
  99. /*-----------------------------------------------------------------------------------------------
  100. */
  101. int
  102. pcm_init (SF_PRIVATE *psf)
  103. { int chars = 0 ;
  104. if (psf->bytewidth == 0 || psf->sf.channels == 0)
  105. { psf_log_printf (psf, "pcm_init : internal error : bytewitdh = %d, channels = %d\n", psf->bytewidth, psf->sf.channels) ;
  106. return SFE_INTERNAL ;
  107. } ;
  108. psf->blockwidth = psf->bytewidth * psf->sf.channels ;
  109. if ((SF_CODEC (psf->sf.format)) == SF_FORMAT_PCM_S8)
  110. chars = SF_CHARS_SIGNED ;
  111. else if ((SF_CODEC (psf->sf.format)) == SF_FORMAT_PCM_U8)
  112. chars = SF_CHARS_UNSIGNED ;
  113. if (CPU_IS_BIG_ENDIAN)
  114. psf->data_endswap = (psf->endian == SF_ENDIAN_BIG) ? SF_FALSE : SF_TRUE ;
  115. else
  116. psf->data_endswap = (psf->endian == SF_ENDIAN_LITTLE) ? SF_FALSE : SF_TRUE ;
  117. if (psf->file.mode == SFM_READ || psf->file.mode == SFM_RDWR)
  118. { switch (psf->bytewidth * 0x10000 + psf->endian + chars)
  119. { case (0x10000 + SF_ENDIAN_BIG + SF_CHARS_SIGNED) :
  120. case (0x10000 + SF_ENDIAN_LITTLE + SF_CHARS_SIGNED) :
  121. psf->read_short = pcm_read_sc2s ;
  122. psf->read_int = pcm_read_sc2i ;
  123. psf->read_float = pcm_read_sc2f ;
  124. psf->read_double = pcm_read_sc2d ;
  125. break ;
  126. case (0x10000 + SF_ENDIAN_BIG + SF_CHARS_UNSIGNED) :
  127. case (0x10000 + SF_ENDIAN_LITTLE + SF_CHARS_UNSIGNED) :
  128. psf->read_short = pcm_read_uc2s ;
  129. psf->read_int = pcm_read_uc2i ;
  130. psf->read_float = pcm_read_uc2f ;
  131. psf->read_double = pcm_read_uc2d ;
  132. break ;
  133. case (2 * 0x10000 + SF_ENDIAN_BIG) :
  134. psf->read_short = pcm_read_bes2s ;
  135. psf->read_int = pcm_read_bes2i ;
  136. psf->read_float = pcm_read_bes2f ;
  137. psf->read_double = pcm_read_bes2d ;
  138. break ;
  139. case (3 * 0x10000 + SF_ENDIAN_BIG) :
  140. psf->read_short = pcm_read_bet2s ;
  141. psf->read_int = pcm_read_bet2i ;
  142. psf->read_float = pcm_read_bet2f ;
  143. psf->read_double = pcm_read_bet2d ;
  144. break ;
  145. case (4 * 0x10000 + SF_ENDIAN_BIG) :
  146. psf->read_short = pcm_read_bei2s ;
  147. psf->read_int = pcm_read_bei2i ;
  148. psf->read_float = pcm_read_bei2f ;
  149. psf->read_double = pcm_read_bei2d ;
  150. break ;
  151. case (2 * 0x10000 + SF_ENDIAN_LITTLE) :
  152. psf->read_short = pcm_read_les2s ;
  153. psf->read_int = pcm_read_les2i ;
  154. psf->read_float = pcm_read_les2f ;
  155. psf->read_double = pcm_read_les2d ;
  156. break ;
  157. case (3 * 0x10000 + SF_ENDIAN_LITTLE) :
  158. psf->read_short = pcm_read_let2s ;
  159. psf->read_int = pcm_read_let2i ;
  160. psf->read_float = pcm_read_let2f ;
  161. psf->read_double = pcm_read_let2d ;
  162. break ;
  163. case (4 * 0x10000 + SF_ENDIAN_LITTLE) :
  164. psf->read_short = pcm_read_lei2s ;
  165. psf->read_int = pcm_read_lei2i ;
  166. psf->read_float = pcm_read_lei2f ;
  167. psf->read_double = pcm_read_lei2d ;
  168. break ;
  169. default :
  170. psf_log_printf (psf, "pcm.c returning SFE_UNIMPLEMENTED\nbytewidth %d endian %d\n", psf->bytewidth, psf->endian) ;
  171. return SFE_UNIMPLEMENTED ;
  172. } ;
  173. } ;
  174. if (psf->file.mode == SFM_WRITE || psf->file.mode == SFM_RDWR)
  175. { switch (psf->bytewidth * 0x10000 + psf->endian + chars)
  176. { case (0x10000 + SF_ENDIAN_BIG + SF_CHARS_SIGNED) :
  177. case (0x10000 + SF_ENDIAN_LITTLE + SF_CHARS_SIGNED) :
  178. psf->write_short = pcm_write_s2sc ;
  179. psf->write_int = pcm_write_i2sc ;
  180. psf->write_float = pcm_write_f2sc ;
  181. psf->write_double = pcm_write_d2sc ;
  182. break ;
  183. case (0x10000 + SF_ENDIAN_BIG + SF_CHARS_UNSIGNED) :
  184. case (0x10000 + SF_ENDIAN_LITTLE + SF_CHARS_UNSIGNED) :
  185. psf->write_short = pcm_write_s2uc ;
  186. psf->write_int = pcm_write_i2uc ;
  187. psf->write_float = pcm_write_f2uc ;
  188. psf->write_double = pcm_write_d2uc ;
  189. break ;
  190. case (2 * 0x10000 + SF_ENDIAN_BIG) :
  191. psf->write_short = pcm_write_s2bes ;
  192. psf->write_int = pcm_write_i2bes ;
  193. psf->write_float = pcm_write_f2bes ;
  194. psf->write_double = pcm_write_d2bes ;
  195. break ;
  196. case (3 * 0x10000 + SF_ENDIAN_BIG) :
  197. psf->write_short = pcm_write_s2bet ;
  198. psf->write_int = pcm_write_i2bet ;
  199. psf->write_float = pcm_write_f2bet ;
  200. psf->write_double = pcm_write_d2bet ;
  201. break ;
  202. case (4 * 0x10000 + SF_ENDIAN_BIG) :
  203. psf->write_short = pcm_write_s2bei ;
  204. psf->write_int = pcm_write_i2bei ;
  205. psf->write_float = pcm_write_f2bei ;
  206. psf->write_double = pcm_write_d2bei ;
  207. break ;
  208. case (2 * 0x10000 + SF_ENDIAN_LITTLE) :
  209. psf->write_short = pcm_write_s2les ;
  210. psf->write_int = pcm_write_i2les ;
  211. psf->write_float = pcm_write_f2les ;
  212. psf->write_double = pcm_write_d2les ;
  213. break ;
  214. case (3 * 0x10000 + SF_ENDIAN_LITTLE) :
  215. psf->write_short = pcm_write_s2let ;
  216. psf->write_int = pcm_write_i2let ;
  217. psf->write_float = pcm_write_f2let ;
  218. psf->write_double = pcm_write_d2let ;
  219. break ;
  220. case (4 * 0x10000 + SF_ENDIAN_LITTLE) :
  221. psf->write_short = pcm_write_s2lei ;
  222. psf->write_int = pcm_write_i2lei ;
  223. psf->write_float = pcm_write_f2lei ;
  224. psf->write_double = pcm_write_d2lei ;
  225. break ;
  226. default :
  227. psf_log_printf (psf, "pcm.c returning SFE_UNIMPLEMENTED\nbytewidth %s endian %d\n", psf->bytewidth, psf->endian) ;
  228. return SFE_UNIMPLEMENTED ;
  229. } ;
  230. } ;
  231. if (psf->filelength > psf->dataoffset)
  232. { psf->datalength = (psf->dataend > 0) ? psf->dataend - psf->dataoffset :
  233. psf->filelength - psf->dataoffset ;
  234. }
  235. else
  236. psf->datalength = 0 ;
  237. psf->sf.frames = psf->blockwidth > 0 ? psf->datalength / psf->blockwidth : 0 ;
  238. return 0 ;
  239. } /* pcm_init */
  240. /*==============================================================================
  241. */
  242. static inline void
  243. sc2s_array (signed char *src, int count, short *dest)
  244. { while (--count >= 0)
  245. { dest [count] = src [count] << 8 ;
  246. } ;
  247. } /* sc2s_array */
  248. static inline void
  249. uc2s_array (unsigned char *src, int count, short *dest)
  250. { while (--count >= 0)
  251. { dest [count] = (((short) src [count]) - 0x80) << 8 ;
  252. } ;
  253. } /* uc2s_array */
  254. static inline void
  255. let2s_array (tribyte *src, int count, short *dest)
  256. { unsigned char *ucptr ;
  257. ucptr = ((unsigned char*) src) + 3 * count ;
  258. while (--count >= 0)
  259. { ucptr -= 3 ;
  260. dest [count] = LET2H_SHORT_PTR (ucptr) ;
  261. } ;
  262. } /* let2s_array */
  263. static inline void
  264. bet2s_array (tribyte *src, int count, short *dest)
  265. { unsigned char *ucptr ;
  266. ucptr = ((unsigned char*) src) + 3 * count ;
  267. while (--count >= 0)
  268. { ucptr -= 3 ;
  269. dest [count] = BET2H_SHORT_PTR (ucptr) ;
  270. } ;
  271. } /* bet2s_array */
  272. static inline void
  273. lei2s_array (int *src, int count, short *dest)
  274. { int value ;
  275. while (--count >= 0)
  276. { value = LEI2H_INT (src [count]) ;
  277. dest [count] = value >> 16 ;
  278. } ;
  279. } /* lei2s_array */
  280. static inline void
  281. bei2s_array (int *src, int count, short *dest)
  282. { int value ;
  283. while (--count >= 0)
  284. { value = BEI2H_INT (src [count]) ;
  285. dest [count] = value >> 16 ;
  286. } ;
  287. } /* bei2s_array */
  288. /*--------------------------------------------------------------------------
  289. */
  290. static inline void
  291. sc2i_array (signed char *src, int count, int *dest)
  292. { while (--count >= 0)
  293. { dest [count] = ((int) src [count]) << 24 ;
  294. } ;
  295. } /* sc2i_array */
  296. static inline void
  297. uc2i_array (unsigned char *src, int count, int *dest)
  298. { while (--count >= 0)
  299. { dest [count] = (((int) src [count]) - 128) << 24 ;
  300. } ;
  301. } /* uc2i_array */
  302. static inline void
  303. bes2i_array (short *src, int count, int *dest)
  304. { short value ;
  305. while (--count >= 0)
  306. { value = BES2H_SHORT (src [count]) ;
  307. dest [count] = value << 16 ;
  308. } ;
  309. } /* bes2i_array */
  310. static inline void
  311. les2i_array (short *src, int count, int *dest)
  312. { short value ;
  313. while (--count >= 0)
  314. { value = LES2H_SHORT (src [count]) ;
  315. dest [count] = value << 16 ;
  316. } ;
  317. } /* les2i_array */
  318. static inline void
  319. bet2i_array (tribyte *src, int count, int *dest)
  320. { unsigned char *ucptr ;
  321. ucptr = ((unsigned char*) src) + 3 * count ;
  322. while (--count >= 0)
  323. { ucptr -= 3 ;
  324. dest [count] = BET2H_INT_PTR (ucptr) ;
  325. } ;
  326. } /* bet2i_array */
  327. static inline void
  328. let2i_array (tribyte *src, int count, int *dest)
  329. { unsigned char *ucptr ;
  330. ucptr = ((unsigned char*) src) + 3 * count ;
  331. while (--count >= 0)
  332. { ucptr -= 3 ;
  333. dest [count] = LET2H_INT_PTR (ucptr) ;
  334. } ;
  335. } /* let2i_array */
  336. /*--------------------------------------------------------------------------
  337. */
  338. static inline void
  339. sc2f_array (signed char *src, int count, float *dest, float normfact)
  340. { while (--count >= 0)
  341. dest [count] = ((float) src [count]) * normfact ;
  342. } /* sc2f_array */
  343. static inline void
  344. uc2f_array (unsigned char *src, int count, float *dest, float normfact)
  345. { while (--count >= 0)
  346. dest [count] = (((int) src [count]) - 128) * normfact ;
  347. } /* uc2f_array */
  348. static inline void
  349. les2f_array (short *src, int count, float *dest, float normfact)
  350. { short value ;
  351. while (--count >= 0)
  352. { value = src [count] ;
  353. value = LES2H_SHORT (value) ;
  354. dest [count] = ((float) value) * normfact ;
  355. } ;
  356. } /* les2f_array */
  357. static inline void
  358. bes2f_array (short *src, int count, float *dest, float normfact)
  359. { short value ;
  360. while (--count >= 0)
  361. { value = src [count] ;
  362. value = BES2H_SHORT (value) ;
  363. dest [count] = ((float) value) * normfact ;
  364. } ;
  365. } /* bes2f_array */
  366. static inline void
  367. let2f_array (tribyte *src, int count, float *dest, float normfact)
  368. { unsigned char *ucptr ;
  369. int value ;
  370. ucptr = ((unsigned char*) src) + 3 * count ;
  371. while (--count >= 0)
  372. { ucptr -= 3 ;
  373. value = LET2H_INT_PTR (ucptr) ;
  374. dest [count] = ((float) value) * normfact ;
  375. } ;
  376. } /* let2f_array */
  377. static inline void
  378. bet2f_array (tribyte *src, int count, float *dest, float normfact)
  379. { unsigned char *ucptr ;
  380. int value ;
  381. ucptr = ((unsigned char*) src) + 3 * count ;
  382. while (--count >= 0)
  383. { ucptr -= 3 ;
  384. value = BET2H_INT_PTR (ucptr) ;
  385. dest [count] = ((float) value) * normfact ;
  386. } ;
  387. } /* bet2f_array */
  388. static inline void
  389. lei2f_array (int *src, int count, float *dest, float normfact)
  390. { int value ;
  391. while (--count >= 0)
  392. { value = src [count] ;
  393. value = LEI2H_INT (value) ;
  394. dest [count] = ((float) value) * normfact ;
  395. } ;
  396. } /* lei2f_array */
  397. static inline void
  398. bei2f_array (int *src, int count, float *dest, float normfact)
  399. { int value ;
  400. while (--count >= 0)
  401. { value = src [count] ;
  402. value = BEI2H_INT (value) ;
  403. dest [count] = ((float) value) * normfact ;
  404. } ;
  405. } /* bei2f_array */
  406. /*--------------------------------------------------------------------------
  407. */
  408. static inline void
  409. sc2d_array (signed char *src, int count, double *dest, double normfact)
  410. { while (--count >= 0)
  411. dest [count] = ((double) src [count]) * normfact ;
  412. } /* sc2d_array */
  413. static inline void
  414. uc2d_array (unsigned char *src, int count, double *dest, double normfact)
  415. { while (--count >= 0)
  416. dest [count] = (((int) src [count]) - 128) * normfact ;
  417. } /* uc2d_array */
  418. static inline void
  419. les2d_array (short *src, int count, double *dest, double normfact)
  420. { short value ;
  421. while (--count >= 0)
  422. { value = src [count] ;
  423. value = LES2H_SHORT (value) ;
  424. dest [count] = ((double) value) * normfact ;
  425. } ;
  426. } /* les2d_array */
  427. static inline void
  428. bes2d_array (short *src, int count, double *dest, double normfact)
  429. { short value ;
  430. while (--count >= 0)
  431. { value = src [count] ;
  432. value = BES2H_SHORT (value) ;
  433. dest [count] = ((double) value) * normfact ;
  434. } ;
  435. } /* bes2d_array */
  436. static inline void
  437. let2d_array (tribyte *src, int count, double *dest, double normfact)
  438. { unsigned char *ucptr ;
  439. int value ;
  440. ucptr = ((unsigned char*) src) + 3 * count ;
  441. while (--count >= 0)
  442. { ucptr -= 3 ;
  443. value = LET2H_INT_PTR (ucptr) ;
  444. dest [count] = ((double) value) * normfact ;
  445. } ;
  446. } /* let2d_array */
  447. static inline void
  448. bet2d_array (tribyte *src, int count, double *dest, double normfact)
  449. { unsigned char *ucptr ;
  450. int value ;
  451. ucptr = ((unsigned char*) src) + 3 * count ;
  452. while (--count >= 0)
  453. { ucptr -= 3 ;
  454. value = (ucptr [0] << 24) | (ucptr [1] << 16) | (ucptr [2] << 8) ;
  455. dest [count] = ((double) value) * normfact ;
  456. } ;
  457. } /* bet2d_array */
  458. static inline void
  459. lei2d_array (int *src, int count, double *dest, double normfact)
  460. { int value ;
  461. while (--count >= 0)
  462. { value = src [count] ;
  463. value = LEI2H_INT (value) ;
  464. dest [count] = ((double) value) * normfact ;
  465. } ;
  466. } /* lei2d_array */
  467. static inline void
  468. bei2d_array (int *src, int count, double *dest, double normfact)
  469. { int value ;
  470. while (--count >= 0)
  471. { value = src [count] ;
  472. value = BEI2H_INT (value) ;
  473. dest [count] = ((double) value) * normfact ;
  474. } ;
  475. } /* bei2d_array */
  476. /*--------------------------------------------------------------------------
  477. */
  478. static inline void
  479. s2sc_array (const short *src, signed char *dest, int count)
  480. { while (--count >= 0)
  481. dest [count] = src [count] >> 8 ;
  482. } /* s2sc_array */
  483. static inline void
  484. s2uc_array (const short *src, unsigned char *dest, int count)
  485. { while (--count >= 0)
  486. dest [count] = (src [count] >> 8) + 0x80 ;
  487. } /* s2uc_array */
  488. static inline void
  489. s2let_array (const short *src, tribyte *dest, int count)
  490. { unsigned char *ucptr ;
  491. ucptr = ((unsigned char*) dest) + 3 * count ;
  492. while (--count >= 0)
  493. { ucptr -= 3 ;
  494. ucptr [0] = 0 ;
  495. ucptr [1] = src [count] ;
  496. ucptr [2] = src [count] >> 8 ;
  497. } ;
  498. } /* s2let_array */
  499. static inline void
  500. s2bet_array (const short *src, tribyte *dest, int count)
  501. { unsigned char *ucptr ;
  502. ucptr = ((unsigned char*) dest) + 3 * count ;
  503. while (--count >= 0)
  504. { ucptr -= 3 ;
  505. ucptr [2] = 0 ;
  506. ucptr [1] = src [count] ;
  507. ucptr [0] = src [count] >> 8 ;
  508. } ;
  509. } /* s2bet_array */
  510. static inline void
  511. s2lei_array (const short *src, int *dest, int count)
  512. { unsigned char *ucptr ;
  513. ucptr = ((unsigned char*) dest) + 4 * count ;
  514. while (--count >= 0)
  515. { ucptr -= 4 ;
  516. ucptr [0] = 0 ;
  517. ucptr [1] = 0 ;
  518. ucptr [2] = src [count] ;
  519. ucptr [3] = src [count] >> 8 ;
  520. } ;
  521. } /* s2lei_array */
  522. static inline void
  523. s2bei_array (const short *src, int *dest, int count)
  524. { unsigned char *ucptr ;
  525. ucptr = ((unsigned char*) dest) + 4 * count ;
  526. while (--count >= 0)
  527. { ucptr -= 4 ;
  528. ucptr [0] = src [count] >> 8 ;
  529. ucptr [1] = src [count] ;
  530. ucptr [2] = 0 ;
  531. ucptr [3] = 0 ;
  532. } ;
  533. } /* s2bei_array */
  534. /*--------------------------------------------------------------------------
  535. */
  536. static inline void
  537. i2sc_array (const int *src, signed char *dest, int count)
  538. { while (--count >= 0)
  539. dest [count] = (src [count] >> 24) ;
  540. } /* i2sc_array */
  541. static inline void
  542. i2uc_array (const int *src, unsigned char *dest, int count)
  543. { while (--count >= 0)
  544. dest [count] = ((src [count] >> 24) + 128) ;
  545. } /* i2uc_array */
  546. static inline void
  547. i2bes_array (const int *src, short *dest, int count)
  548. { unsigned char *ucptr ;
  549. ucptr = ((unsigned char*) dest) + 2 * count ;
  550. while (--count >= 0)
  551. { ucptr -= 2 ;
  552. ucptr [0] = src [count] >> 24 ;
  553. ucptr [1] = src [count] >> 16 ;
  554. } ;
  555. } /* i2bes_array */
  556. static inline void
  557. i2les_array (const int *src, short *dest, int count)
  558. { unsigned char *ucptr ;
  559. ucptr = ((unsigned char*) dest) + 2 * count ;
  560. while (--count >= 0)
  561. { ucptr -= 2 ;
  562. ucptr [0] = src [count] >> 16 ;
  563. ucptr [1] = src [count] >> 24 ;
  564. } ;
  565. } /* i2les_array */
  566. static inline void
  567. i2let_array (const int *src, tribyte *dest, int count)
  568. { unsigned char *ucptr ;
  569. int value ;
  570. ucptr = ((unsigned char*) dest) + 3 * count ;
  571. while (--count >= 0)
  572. { ucptr -= 3 ;
  573. value = src [count] >> 8 ;
  574. ucptr [0] = value ;
  575. ucptr [1] = value >> 8 ;
  576. ucptr [2] = value >> 16 ;
  577. } ;
  578. } /* i2let_array */
  579. static inline void
  580. i2bet_array (const int *src, tribyte *dest, int count)
  581. { unsigned char *ucptr ;
  582. int value ;
  583. ucptr = ((unsigned char*) dest) + 3 * count ;
  584. while (--count >= 0)
  585. { ucptr -= 3 ;
  586. value = src [count] >> 8 ;
  587. ucptr [2] = value ;
  588. ucptr [1] = value >> 8 ;
  589. ucptr [0] = value >> 16 ;
  590. } ;
  591. } /* i2bet_array */
  592. /*===============================================================================================
  593. */
  594. static sf_count_t
  595. pcm_read_sc2s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
  596. { int bufferlen, readcount ;
  597. sf_count_t total = 0 ;
  598. bufferlen = ARRAY_LEN (psf->u.scbuf) ;
  599. while (len > 0)
  600. { if (len < bufferlen)
  601. bufferlen = (int) len ;
  602. readcount = psf_fread (psf->u.scbuf, sizeof (signed char), bufferlen, psf) ;
  603. sc2s_array (psf->u.scbuf, readcount, ptr + total) ;
  604. total += readcount ;
  605. if (readcount < bufferlen)
  606. break ;
  607. len -= readcount ;
  608. } ;
  609. return total ;
  610. } /* pcm_read_sc2s */
  611. static sf_count_t
  612. pcm_read_uc2s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
  613. { int bufferlen, readcount ;
  614. sf_count_t total = 0 ;
  615. bufferlen = ARRAY_LEN (psf->u.ucbuf) ;
  616. while (len > 0)
  617. { if (len < bufferlen)
  618. bufferlen = (int) len ;
  619. readcount = psf_fread (psf->u.ucbuf, sizeof (unsigned char), bufferlen, psf) ;
  620. uc2s_array (psf->u.ucbuf, readcount, ptr + total) ;
  621. total += readcount ;
  622. if (readcount < bufferlen)
  623. break ;
  624. len -= readcount ;
  625. } ;
  626. return total ;
  627. } /* pcm_read_uc2s */
  628. static sf_count_t
  629. pcm_read_bes2s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
  630. { int total ;
  631. total = psf_fread (ptr, sizeof (short), len, psf) ;
  632. if (CPU_IS_LITTLE_ENDIAN)
  633. endswap_short_array (ptr, len) ;
  634. return total ;
  635. } /* pcm_read_bes2s */
  636. static sf_count_t
  637. pcm_read_les2s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
  638. { int total ;
  639. total = psf_fread (ptr, sizeof (short), len, psf) ;
  640. if (CPU_IS_BIG_ENDIAN)
  641. endswap_short_array (ptr, len) ;
  642. return total ;
  643. } /* pcm_read_les2s */
  644. static sf_count_t
  645. pcm_read_bet2s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
  646. { int bufferlen, readcount ;
  647. sf_count_t total = 0 ;
  648. bufferlen = sizeof (psf->u.ucbuf) / SIZEOF_TRIBYTE ;
  649. while (len > 0)
  650. { if (len < bufferlen)
  651. bufferlen = (int) len ;
  652. readcount = psf_fread (psf->u.ucbuf, SIZEOF_TRIBYTE, bufferlen, psf) ;
  653. bet2s_array ((tribyte*) (psf->u.ucbuf), readcount, ptr + total) ;
  654. total += readcount ;
  655. if (readcount < bufferlen)
  656. break ;
  657. len -= readcount ;
  658. } ;
  659. return total ;
  660. } /* pcm_read_bet2s */
  661. static sf_count_t
  662. pcm_read_let2s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
  663. { int bufferlen, readcount ;
  664. sf_count_t total = 0 ;
  665. bufferlen = sizeof (psf->u.ucbuf) / SIZEOF_TRIBYTE ;
  666. while (len > 0)
  667. { if (len < bufferlen)
  668. bufferlen = (int) len ;
  669. readcount = psf_fread (psf->u.ucbuf, SIZEOF_TRIBYTE, bufferlen, psf) ;
  670. let2s_array ((tribyte*) (psf->u.ucbuf), readcount, ptr + total) ;
  671. total += readcount ;
  672. if (readcount < bufferlen)
  673. break ;
  674. len -= readcount ;
  675. } ;
  676. return total ;
  677. } /* pcm_read_let2s */
  678. static sf_count_t
  679. pcm_read_bei2s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
  680. { int bufferlen, readcount ;
  681. sf_count_t total = 0 ;
  682. bufferlen = ARRAY_LEN (psf->u.ibuf) ;
  683. while (len > 0)
  684. { if (len < bufferlen)
  685. bufferlen = (int) len ;
  686. readcount = psf_fread (psf->u.ibuf, sizeof (int), bufferlen, psf) ;
  687. bei2s_array (psf->u.ibuf, readcount, ptr + total) ;
  688. total += readcount ;
  689. if (readcount < bufferlen)
  690. break ;
  691. len -= readcount ;
  692. } ;
  693. return total ;
  694. } /* pcm_read_bei2s */
  695. static sf_count_t
  696. pcm_read_lei2s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
  697. { int bufferlen, readcount ;
  698. sf_count_t total = 0 ;
  699. bufferlen = ARRAY_LEN (psf->u.ibuf) ;
  700. while (len > 0)
  701. { if (len < bufferlen)
  702. bufferlen = (int) len ;
  703. readcount = psf_fread (psf->u.ibuf, sizeof (int), bufferlen, psf) ;
  704. lei2s_array (psf->u.ibuf, readcount, ptr + total) ;
  705. total += readcount ;
  706. if (readcount < bufferlen)
  707. break ;
  708. len -= readcount ;
  709. } ;
  710. return total ;
  711. } /* pcm_read_lei2s */
  712. /*-----------------------------------------------------------------------------------------------
  713. */
  714. static sf_count_t
  715. pcm_read_sc2i (SF_PRIVATE *psf, int *ptr, sf_count_t len)
  716. { int bufferlen, readcount ;
  717. sf_count_t total = 0 ;
  718. bufferlen = ARRAY_LEN (psf->u.scbuf) ;
  719. while (len > 0)
  720. { if (len < bufferlen)
  721. bufferlen = (int) len ;
  722. readcount = psf_fread (psf->u.scbuf, sizeof (signed char), bufferlen, psf) ;
  723. sc2i_array (psf->u.scbuf, readcount, ptr + total) ;
  724. total += readcount ;
  725. if (readcount < bufferlen)
  726. break ;
  727. len -= readcount ;
  728. } ;
  729. return total ;
  730. } /* pcm_read_sc2i */
  731. static sf_count_t
  732. pcm_read_uc2i (SF_PRIVATE *psf, int *ptr, sf_count_t len)
  733. { int bufferlen, readcount ;
  734. sf_count_t total = 0 ;
  735. bufferlen = ARRAY_LEN (psf->u.ucbuf) ;
  736. while (len > 0)
  737. { if (len < bufferlen)
  738. bufferlen = (int) len ;
  739. readcount = psf_fread (psf->u.ucbuf, sizeof (unsigned char), bufferlen, psf) ;
  740. uc2i_array (psf->u.ucbuf, readcount, ptr + total) ;
  741. total += readcount ;
  742. if (readcount < bufferlen)
  743. break ;
  744. len -= readcount ;
  745. } ;
  746. return total ;
  747. } /* pcm_read_uc2i */
  748. static sf_count_t
  749. pcm_read_bes2i (SF_PRIVATE *psf, int *ptr, sf_count_t len)
  750. { int bufferlen, readcount ;
  751. sf_count_t total = 0 ;
  752. bufferlen = ARRAY_LEN (psf->u.sbuf) ;
  753. while (len > 0)
  754. { if (len < bufferlen)
  755. bufferlen = (int) len ;
  756. readcount = psf_fread (psf->u.sbuf, sizeof (short), bufferlen, psf) ;
  757. bes2i_array (psf->u.sbuf, readcount, ptr + total) ;
  758. total += readcount ;
  759. if (readcount < bufferlen)
  760. break ;
  761. len -= readcount ;
  762. } ;
  763. return total ;
  764. } /* pcm_read_bes2i */
  765. static sf_count_t
  766. pcm_read_les2i (SF_PRIVATE *psf, int *ptr, sf_count_t len)
  767. { int bufferlen, readcount ;
  768. sf_count_t total = 0 ;
  769. bufferlen = ARRAY_LEN (psf->u.sbuf) ;
  770. while (len > 0)
  771. { if (len < bufferlen)
  772. bufferlen = (int) len ;
  773. readcount = psf_fread (psf->u.sbuf, sizeof (short), bufferlen, psf) ;
  774. les2i_array (psf->u.sbuf, readcount, ptr + total) ;
  775. total += readcount ;
  776. if (readcount < bufferlen)
  777. break ;
  778. len -= readcount ;
  779. } ;
  780. return total ;
  781. } /* pcm_read_les2i */
  782. static sf_count_t
  783. pcm_read_bet2i (SF_PRIVATE *psf, int *ptr, sf_count_t len)
  784. { int bufferlen, readcount ;
  785. sf_count_t total = 0 ;
  786. bufferlen = sizeof (psf->u.ucbuf) / SIZEOF_TRIBYTE ;
  787. while (len > 0)
  788. { if (len < bufferlen)
  789. bufferlen = (int) len ;
  790. readcount = psf_fread (psf->u.ucbuf, SIZEOF_TRIBYTE, bufferlen, psf) ;
  791. bet2i_array ((tribyte*) (psf->u.ucbuf), readcount, ptr + total) ;
  792. total += readcount ;
  793. if (readcount < bufferlen)
  794. break ;
  795. len -= readcount ;
  796. } ;
  797. return total ;
  798. } /* pcm_read_bet2i */
  799. static sf_count_t
  800. pcm_read_let2i (SF_PRIVATE *psf, int *ptr, sf_count_t len)
  801. { int bufferlen, readcount ;
  802. sf_count_t total = 0 ;
  803. bufferlen = sizeof (psf->u.ucbuf) / SIZEOF_TRIBYTE ;
  804. while (len > 0)
  805. { if (len < bufferlen)
  806. bufferlen = (int) len ;
  807. readcount = psf_fread (psf->u.ucbuf, SIZEOF_TRIBYTE, bufferlen, psf) ;
  808. let2i_array ((tribyte*) (psf->u.ucbuf), readcount, ptr + total) ;
  809. total += readcount ;
  810. if (readcount < bufferlen)
  811. break ;
  812. len -= readcount ;
  813. } ;
  814. return total ;
  815. } /* pcm_read_let2i */
  816. static sf_count_t
  817. pcm_read_bei2i (SF_PRIVATE *psf, int *ptr, sf_count_t len)
  818. { int total ;
  819. total = psf_fread (ptr, sizeof (int), len, psf) ;
  820. if (CPU_IS_LITTLE_ENDIAN)
  821. endswap_int_array (ptr, len) ;
  822. return total ;
  823. } /* pcm_read_bei2i */
  824. static sf_count_t
  825. pcm_read_lei2i (SF_PRIVATE *psf, int *ptr, sf_count_t len)
  826. { int total ;
  827. total = psf_fread (ptr, sizeof (int), len, psf) ;
  828. if (CPU_IS_BIG_ENDIAN)
  829. endswap_int_array (ptr, len) ;
  830. return total ;
  831. } /* pcm_read_lei2i */
  832. /*-----------------------------------------------------------------------------------------------
  833. */
  834. static sf_count_t
  835. pcm_read_sc2f (SF_PRIVATE *psf, float *ptr, sf_count_t len)
  836. { int bufferlen, readcount ;
  837. sf_count_t total = 0 ;
  838. float normfact ;
  839. normfact = (psf->norm_float == SF_TRUE) ? 1.0 / ((float) 0x80) : 1.0 ;
  840. bufferlen = ARRAY_LEN (psf->u.scbuf) ;
  841. while (len > 0)
  842. { if (len < bufferlen)
  843. bufferlen = (int) len ;
  844. readcount = psf_fread (psf->u.scbuf, sizeof (signed char), bufferlen, psf) ;
  845. sc2f_array (psf->u.scbuf, readcount, ptr + total, normfact) ;
  846. total += readcount ;
  847. if (readcount < bufferlen)
  848. break ;
  849. len -= readcount ;
  850. } ;
  851. return total ;
  852. } /* pcm_read_sc2f */
  853. static sf_count_t
  854. pcm_read_uc2f (SF_PRIVATE *psf, float *ptr, sf_count_t len)
  855. { int bufferlen, readcount ;
  856. sf_count_t total = 0 ;
  857. float normfact ;
  858. normfact = (psf->norm_float == SF_TRUE) ? 1.0 / ((float) 0x80) : 1.0 ;
  859. bufferlen = ARRAY_LEN (psf->u.ucbuf) ;
  860. while (len > 0)
  861. { if (len < bufferlen)
  862. bufferlen = (int) len ;
  863. readcount = psf_fread (psf->u.ucbuf, sizeof (unsigned char), bufferlen, psf) ;
  864. uc2f_array (psf->u.ucbuf, readcount, ptr + total, normfact) ;
  865. total += readcount ;
  866. if (readcount < bufferlen)
  867. break ;
  868. len -= readcount ;
  869. } ;
  870. return total ;
  871. } /* pcm_read_uc2f */
  872. static sf_count_t
  873. pcm_read_bes2f (SF_PRIVATE *psf, float *ptr, sf_count_t len)
  874. { int bufferlen, readcount ;
  875. sf_count_t total = 0 ;
  876. float normfact ;
  877. normfact = (psf->norm_float == SF_TRUE) ? 1.0 / ((float) 0x8000) : 1.0 ;
  878. bufferlen = ARRAY_LEN (psf->u.sbuf) ;
  879. while (len > 0)
  880. { if (len < bufferlen)
  881. bufferlen = (int) len ;
  882. readcount = psf_fread (psf->u.sbuf, sizeof (short), bufferlen, psf) ;
  883. bes2f_array (psf->u.sbuf, readcount, ptr + total, normfact) ;
  884. total += readcount ;
  885. if (readcount < bufferlen)
  886. break ;
  887. len -= readcount ;
  888. } ;
  889. return total ;
  890. } /* pcm_read_bes2f */
  891. static sf_count_t
  892. pcm_read_les2f (SF_PRIVATE *psf, float *ptr, sf_count_t len)
  893. { int bufferlen, readcount ;
  894. sf_count_t total = 0 ;
  895. float normfact ;
  896. normfact = (psf->norm_float == SF_TRUE) ? 1.0 / ((float) 0x8000) : 1.0 ;
  897. bufferlen = ARRAY_LEN (psf->u.sbuf) ;
  898. while (len > 0)
  899. { if (len < bufferlen)
  900. bufferlen = (int) len ;
  901. readcount = psf_fread (psf->u.sbuf, sizeof (short), bufferlen, psf) ;
  902. les2f_array (psf->u.sbuf, readcount, ptr + total, normfact) ;
  903. total += readcount ;
  904. if (readcount < bufferlen)
  905. break ;
  906. len -= readcount ;
  907. } ;
  908. return total ;
  909. } /* pcm_read_les2f */
  910. static sf_count_t
  911. pcm_read_bet2f (SF_PRIVATE *psf, float *ptr, sf_count_t len)
  912. { int bufferlen, readcount ;
  913. sf_count_t total = 0 ;
  914. float normfact ;
  915. /* Special normfactor because tribyte value is read into an int. */
  916. normfact = (psf->norm_float == SF_TRUE) ? 1.0 / ((float) 0x80000000) : 1.0 / 256.0 ;
  917. bufferlen = sizeof (psf->u.ucbuf) / SIZEOF_TRIBYTE ;
  918. while (len > 0)
  919. { if (len < bufferlen)
  920. bufferlen = (int) len ;
  921. readcount = psf_fread (psf->u.ucbuf, SIZEOF_TRIBYTE, bufferlen, psf) ;
  922. bet2f_array ((tribyte*) (psf->u.ucbuf), readcount, ptr + total, normfact) ;
  923. total += readcount ;
  924. if (readcount < bufferlen)
  925. break ;
  926. len -= readcount ;
  927. } ;
  928. return total ;
  929. } /* pcm_read_bet2f */
  930. static sf_count_t
  931. pcm_read_let2f (SF_PRIVATE *psf, float *ptr, sf_count_t len)
  932. { int bufferlen, readcount ;
  933. sf_count_t total = 0 ;
  934. float normfact ;
  935. /* Special normfactor because tribyte value is read into an int. */
  936. normfact = (psf->norm_float == SF_TRUE) ? 1.0 / ((float) 0x80000000) : 1.0 / 256.0 ;
  937. bufferlen = sizeof (psf->u.ucbuf) / SIZEOF_TRIBYTE ;
  938. while (len > 0)
  939. { if (len < bufferlen)
  940. bufferlen = (int) len ;
  941. readcount = psf_fread (psf->u.ucbuf, SIZEOF_TRIBYTE, bufferlen, psf) ;
  942. let2f_array ((tribyte*) (psf->u.ucbuf), readcount, ptr + total, normfact) ;
  943. total += readcount ;
  944. if (readcount < bufferlen)
  945. break ;
  946. len -= readcount ;
  947. } ;
  948. return total ;
  949. } /* pcm_read_let2f */
  950. static sf_count_t
  951. pcm_read_bei2f (SF_PRIVATE *psf, float *ptr, sf_count_t len)
  952. { int bufferlen, readcount ;
  953. sf_count_t total = 0 ;
  954. float normfact ;
  955. normfact = (psf->norm_float == SF_TRUE) ? 1.0 / ((float) 0x80000000) : 1.0 ;
  956. bufferlen = ARRAY_LEN (psf->u.ibuf) ;
  957. while (len > 0)
  958. { if (len < bufferlen)
  959. bufferlen = (int) len ;
  960. readcount = psf_fread (psf->u.ibuf, sizeof (int), bufferlen, psf) ;
  961. bei2f_array (psf->u.ibuf, readcount, ptr + total, normfact) ;
  962. total += readcount ;
  963. if (readcount < bufferlen)
  964. break ;
  965. len -= readcount ;
  966. } ;
  967. return total ;
  968. } /* pcm_read_bei2f */
  969. static sf_count_t
  970. pcm_read_lei2f (SF_PRIVATE *psf, float *ptr, sf_count_t len)
  971. { int bufferlen, readcount ;
  972. sf_count_t total = 0 ;
  973. float normfact ;
  974. normfact = (psf->norm_float == SF_TRUE) ? 1.0 / ((float) 0x80000000) : 1.0 ;
  975. bufferlen = ARRAY_LEN (psf->u.ibuf) ;
  976. while (len > 0)
  977. { if (len < bufferlen)
  978. bufferlen = (int) len ;
  979. readcount = psf_fread (psf->u.ibuf, sizeof (int), bufferlen, psf) ;
  980. lei2f_array (psf->u.ibuf, readcount, ptr + total, normfact) ;
  981. total += readcount ;
  982. if (readcount < bufferlen)
  983. break ;
  984. len -= readcount ;
  985. } ;
  986. return total ;
  987. } /* pcm_read_lei2f */
  988. /*-----------------------------------------------------------------------------------------------
  989. */
  990. static sf_count_t
  991. pcm_read_sc2d (SF_PRIVATE *psf, double *ptr, sf_count_t len)
  992. { int bufferlen, readcount ;
  993. sf_count_t total = 0 ;
  994. double normfact ;
  995. normfact = (psf->norm_double == SF_TRUE) ? 1.0 / ((double) 0x80) : 1.0 ;
  996. bufferlen = ARRAY_LEN (psf->u.scbuf) ;
  997. while (len > 0)
  998. { if (len < bufferlen)
  999. bufferlen = (int) len ;
  1000. readcount = psf_fread (psf->u.scbuf, sizeof (signed char), bufferlen, psf) ;
  1001. sc2d_array (psf->u.scbuf, readcount, ptr + total, normfact) ;
  1002. total += readcount ;
  1003. if (readcount < bufferlen)
  1004. break ;
  1005. len -= readcount ;
  1006. } ;
  1007. return total ;
  1008. } /* pcm_read_sc2d */
  1009. static sf_count_t
  1010. pcm_read_uc2d (SF_PRIVATE *psf, double *ptr, sf_count_t len)
  1011. { int bufferlen, readcount ;
  1012. sf_count_t total = 0 ;
  1013. double normfact ;
  1014. normfact = (psf->norm_double == SF_TRUE) ? 1.0 / ((double) 0x80) : 1.0 ;
  1015. bufferlen = ARRAY_LEN (psf->u.ucbuf) ;
  1016. while (len > 0)
  1017. { if (len < bufferlen)
  1018. bufferlen = (int) len ;
  1019. readcount = psf_fread (psf->u.ucbuf, sizeof (unsigned char), bufferlen, psf) ;
  1020. uc2d_array (psf->u.ucbuf, readcount, ptr + total, normfact) ;
  1021. total += readcount ;
  1022. if (readcount < bufferlen)
  1023. break ;
  1024. len -= readcount ;
  1025. } ;
  1026. return total ;
  1027. } /* pcm_read_uc2d */
  1028. static sf_count_t
  1029. pcm_read_bes2d (SF_PRIVATE *psf, double *ptr, sf_count_t len)
  1030. { int bufferlen, readcount ;
  1031. sf_count_t total = 0 ;
  1032. double normfact ;
  1033. normfact = (psf->norm_double == SF_TRUE) ? 1.0 / ((double) 0x8000) : 1.0 ;
  1034. bufferlen = ARRAY_LEN (psf->u.sbuf) ;
  1035. while (len > 0)
  1036. { if (len < bufferlen)
  1037. bufferlen = (int) len ;
  1038. readcount = psf_fread (psf->u.sbuf, sizeof (short), bufferlen, psf) ;
  1039. bes2d_array (psf->u.sbuf, readcount, ptr + total, normfact) ;
  1040. total += readcount ;
  1041. if (readcount < bufferlen)
  1042. break ;
  1043. len -= readcount ;
  1044. } ;
  1045. return total ;
  1046. } /* pcm_read_bes2d */
  1047. static sf_count_t
  1048. pcm_read_les2d (SF_PRIVATE *psf, double *ptr, sf_count_t len)
  1049. { int bufferlen, readcount ;
  1050. sf_count_t total = 0 ;
  1051. double normfact ;
  1052. normfact = (psf->norm_double == SF_TRUE) ? 1.0 / ((double) 0x8000) : 1.0 ;
  1053. bufferlen = ARRAY_LEN (psf->u.sbuf) ;
  1054. while (len > 0)
  1055. { if (len < bufferlen)
  1056. bufferlen = (int) len ;
  1057. readcount = psf_fread (psf->u.sbuf, sizeof (short), bufferlen, psf) ;
  1058. les2d_array (psf->u.sbuf, readcount, ptr + total, normfact) ;
  1059. total += readcount ;
  1060. if (readcount < bufferlen)
  1061. break ;
  1062. len -= readcount ;
  1063. } ;
  1064. return total ;
  1065. } /* pcm_read_les2d */
  1066. static sf_count_t
  1067. pcm_read_bet2d (SF_PRIVATE *psf, double *ptr, sf_count_t len)
  1068. { int bufferlen, readcount ;
  1069. sf_count_t total = 0 ;
  1070. double normfact ;
  1071. normfact = (psf->norm_double == SF_TRUE) ? 1.0 / ((double) 0x80000000) : 1.0 / 256.0 ;
  1072. bufferlen = sizeof (psf->u.ucbuf) / SIZEOF_TRIBYTE ;
  1073. while (len > 0)
  1074. { if (len < bufferlen)
  1075. bufferlen = (int) len ;
  1076. readcount = psf_fread (psf->u.ucbuf, SIZEOF_TRIBYTE, bufferlen, psf) ;
  1077. bet2d_array ((tribyte*) (psf->u.ucbuf), readcount, ptr + total, normfact) ;
  1078. total += readcount ;
  1079. if (readcount < bufferlen)
  1080. break ;
  1081. len -= readcount ;
  1082. } ;
  1083. return total ;
  1084. } /* pcm_read_bet2d */
  1085. static sf_count_t
  1086. pcm_read_let2d (SF_PRIVATE *psf, double *ptr, sf_count_t len)
  1087. { int bufferlen, readcount ;
  1088. sf_count_t total = 0 ;
  1089. double normfact ;
  1090. /* Special normfactor because tribyte value is read into an int. */
  1091. normfact = (psf->norm_double == SF_TRUE) ? 1.0 / ((double) 0x80000000) : 1.0 / 256.0 ;
  1092. bufferlen = sizeof (psf->u.ucbuf) / SIZEOF_TRIBYTE ;
  1093. while (len > 0)
  1094. { if (len < bufferlen)
  1095. bufferlen = (int) len ;
  1096. readcount = psf_fread (psf->u.ucbuf, SIZEOF_TRIBYTE, bufferlen, psf) ;
  1097. let2d_array ((tribyte*) (psf->u.ucbuf), readcount, ptr + total, normfact) ;
  1098. total += readcount ;
  1099. if (readcount < bufferlen)
  1100. break ;
  1101. len -= readcount ;
  1102. } ;
  1103. return total ;
  1104. } /* pcm_read_let2d */
  1105. static sf_count_t
  1106. pcm_read_bei2d (SF_PRIVATE *psf, double *ptr, sf_count_t len)
  1107. { int bufferlen, readcount ;
  1108. sf_count_t total = 0 ;
  1109. double normfact ;
  1110. normfact = (psf->norm_double == SF_TRUE) ? 1.0 / ((double) 0x80000000) : 1.0 ;
  1111. bufferlen = ARRAY_LEN (psf->u.ibuf) ;
  1112. while (len > 0)
  1113. { if (len < bufferlen)
  1114. bufferlen = (int) len ;
  1115. readcount = psf_fread (psf->u.ibuf, sizeof (int), bufferlen, psf) ;
  1116. bei2d_array (psf->u.ibuf, readcount, ptr + total, normfact) ;
  1117. total += readcount ;
  1118. if (readcount < bufferlen)
  1119. break ;
  1120. len -= readcount ;
  1121. } ;
  1122. return total ;
  1123. } /* pcm_read_bei2d */
  1124. static sf_count_t
  1125. pcm_read_lei2d (SF_PRIVATE *psf, double *ptr, sf_count_t len)
  1126. { int bufferlen, readcount ;
  1127. sf_count_t total = 0 ;
  1128. double normfact ;
  1129. normfact = (psf->norm_double == SF_TRUE) ? 1.0 / ((double) 0x80000000) : 1.0 ;
  1130. bufferlen = ARRAY_LEN (psf->u.ibuf) ;
  1131. while (len > 0)
  1132. { if (len < bufferlen)
  1133. bufferlen = (int) len ;
  1134. readcount = psf_fread (psf->u.ibuf, sizeof (int), bufferlen, psf) ;
  1135. lei2d_array (psf->u.ibuf, readcount, ptr + total, normfact) ;
  1136. total += readcount ;
  1137. if (readcount < bufferlen)
  1138. break ;
  1139. len -= readcount ;
  1140. } ;
  1141. return total ;
  1142. } /* pcm_read_lei2d */
  1143. /*===============================================================================================
  1144. **-----------------------------------------------------------------------------------------------
  1145. **===============================================================================================
  1146. */
  1147. static sf_count_t
  1148. pcm_write_s2sc (SF_PRIVATE *psf, const short *ptr, sf_count_t len)
  1149. { int bufferlen, writecount ;
  1150. sf_count_t total = 0 ;
  1151. bufferlen = ARRAY_LEN (psf->u.scbuf) ;
  1152. while (len > 0)
  1153. { if (len < bufferlen)
  1154. bufferlen = (int) len ;
  1155. s2sc_array (ptr + total, psf->u.scbuf, bufferlen) ;
  1156. writecount = psf_fwrite (psf->u.scbuf, sizeof (signed char), bufferlen, psf) ;
  1157. total += writecount ;
  1158. if (writecount < bufferlen)
  1159. break ;
  1160. len -= writecount ;
  1161. } ;
  1162. return total ;
  1163. } /* pcm_write_s2sc */
  1164. static sf_count_t
  1165. pcm_write_s2uc (SF_PRIVATE *psf, const short *ptr, sf_count_t len)
  1166. { int bufferlen, writecount ;
  1167. sf_count_t total = 0 ;
  1168. bufferlen = ARRAY_LEN (psf->u.ucbuf) ;
  1169. while (len > 0)
  1170. { if (len < bufferlen)
  1171. bufferlen = (int) len ;
  1172. s2uc_array (ptr + total, psf->u.ucbuf, bufferlen) ;
  1173. writecount = psf_fwrite (psf->u.ucbuf, sizeof (unsigned char), bufferlen, psf) ;
  1174. total += writecount ;
  1175. if (writecount < bufferlen)
  1176. break ;
  1177. len -= writecount ;
  1178. } ;
  1179. return total ;
  1180. } /* pcm_write_s2uc */
  1181. static sf_count_t
  1182. pcm_write_s2bes (SF_PRIVATE *psf, const short *ptr, sf_count_t len)
  1183. { int bufferlen, writecount ;
  1184. sf_count_t total = 0 ;
  1185. if (CPU_IS_BIG_ENDIAN)
  1186. return psf_fwrite (ptr, sizeof (short), len, psf) ;
  1187. else
  1188. bufferlen = ARRAY_LEN (psf->u.sbuf) ;
  1189. while (len > 0)
  1190. { if (len < bufferlen)
  1191. bufferlen = (int) len ;
  1192. endswap_short_copy (psf->u.sbuf, ptr + total, bufferlen) ;
  1193. writecount = psf_fwrite (psf->u.sbuf, sizeof (short), bufferlen, psf) ;
  1194. total += writecount ;
  1195. if (writecount < bufferlen)
  1196. break ;
  1197. len -= writecount ;
  1198. } ;
  1199. return total ;
  1200. } /* pcm_write_s2bes */
  1201. static sf_count_t
  1202. pcm_write_s2les (SF_PRIVATE *psf, const short *ptr, sf_count_t len)
  1203. { int bufferlen, writecount ;
  1204. sf_count_t total = 0 ;
  1205. if (CPU_IS_LITTLE_ENDIAN)
  1206. return psf_fwrite (ptr, sizeof (short), len, psf) ;
  1207. bufferlen = ARRAY_LEN (psf->u.sbuf) ;
  1208. while (len > 0)
  1209. { if (len < bufferlen)
  1210. bufferlen = (int) len ;
  1211. endswap_short_copy (psf->u.sbuf, ptr + total, bufferlen) ;
  1212. writecount = psf_fwrite (psf->u.sbuf, sizeof (short), bufferlen, psf) ;
  1213. total += writecount ;
  1214. if (writecount < bufferlen)
  1215. break ;
  1216. len -= writecount ;
  1217. } ;
  1218. return total ;
  1219. } /* pcm_write_s2les */
  1220. static sf_count_t
  1221. pcm_write_s2bet (SF_PRIVATE *psf, const short *ptr, sf_count_t len)
  1222. { int bufferlen, writecount ;
  1223. sf_count_t total = 0 ;
  1224. bufferlen = sizeof (psf->u.ucbuf) / SIZEOF_TRIBYTE ;
  1225. while (len > 0)
  1226. { if (len < bufferlen)
  1227. bufferlen = (int) len ;
  1228. s2bet_array (ptr + total, (tribyte*) (psf->u.ucbuf), bufferlen) ;
  1229. writecount = psf_fwrite (psf->u.ucbuf, SIZEOF_TRIBYTE, bufferlen, psf) ;
  1230. total += writecount ;
  1231. if (writecount < bufferlen)
  1232. break ;
  1233. len -= writecount ;
  1234. } ;
  1235. return total ;
  1236. } /* pcm_write_s2bet */
  1237. static sf_count_t
  1238. pcm_write_s2let (SF_PRIVATE *psf, const short *ptr, sf_count_t len)
  1239. { int bufferlen, writecount ;
  1240. sf_count_t total = 0 ;
  1241. bufferlen = sizeof (psf->u.ucbuf) / SIZEOF_TRIBYTE ;
  1242. while (len > 0)
  1243. { if (len < bufferlen)
  1244. bufferlen = (int) len ;
  1245. s2let_array (ptr + total, (tribyte*) (psf->u.ucbuf), bufferlen) ;
  1246. writecount = psf_fwrite (psf->u.ucbuf, SIZEOF_TRIBYTE, bufferlen, psf) ;
  1247. total += writecount ;
  1248. if (writecount < bufferlen)
  1249. break ;
  1250. len -= writecount ;
  1251. } ;
  1252. return total ;
  1253. } /* pcm_write_s2let */
  1254. static sf_count_t
  1255. pcm_write_s2bei (SF_PRIVATE *psf, const short *ptr, sf_count_t len)
  1256. { int bufferlen, writecount ;
  1257. sf_count_t total = 0 ;
  1258. bufferlen = ARRAY_LEN (psf->u.ibuf) ;
  1259. while (len > 0)
  1260. { if (len < bufferlen)
  1261. bufferlen = (int) len ;
  1262. s2bei_array (ptr + total, psf->u.ibuf, bufferlen) ;
  1263. writecount = psf_fwrite (psf->u.ibuf, sizeof (int), bufferlen, psf) ;
  1264. total += writecount ;
  1265. if (writecount < bufferlen)
  1266. break ;
  1267. len -= writecount ;
  1268. } ;
  1269. return total ;
  1270. } /* pcm_write_s2bei */
  1271. static sf_count_t
  1272. pcm_write_s2lei (SF_PRIVATE *psf, const short *ptr, sf_count_t len)
  1273. { int bufferlen, writecount ;
  1274. sf_count_t total = 0 ;
  1275. bufferlen = ARRAY_LEN (psf->u.ibuf) ;
  1276. while (len > 0)
  1277. { if (len < bufferlen)
  1278. bufferlen = (int) len ;
  1279. s2lei_array (ptr + total, psf->u.ibuf, bufferlen) ;
  1280. writecount = psf_fwrite (psf->u.ibuf, sizeof (int), bufferlen, psf) ;
  1281. total += writecount ;
  1282. if (writecount < bufferlen)
  1283. break ;
  1284. len -= writecount ;
  1285. } ;
  1286. return total ;
  1287. } /* pcm_write_s2lei */
  1288. /*-----------------------------------------------------------------------------------------------
  1289. */
  1290. static sf_count_t
  1291. pcm_write_i2sc (SF_PRIVATE *psf, const int *ptr, sf_count_t len)
  1292. { int bufferlen, writecount ;
  1293. sf_count_t total = 0 ;
  1294. bufferlen = ARRAY_LEN (psf->u.scbuf) ;
  1295. while (len > 0)
  1296. { if (len < bufferlen)
  1297. bufferlen = (int) len ;
  1298. i2sc_array (ptr + total, psf->u.scbuf, bufferlen) ;
  1299. writecount = psf_fwrite (psf->u.scbuf, sizeof (signed char), bufferlen, psf) ;
  1300. total += writecount ;
  1301. if (writecount < bufferlen)
  1302. break ;
  1303. len -= writecount ;
  1304. } ;
  1305. return total ;
  1306. } /* pcm_write_i2sc */
  1307. static sf_count_t
  1308. pcm_write_i2uc (SF_PRIVATE *psf, const int *ptr, sf_count_t len)
  1309. { int bufferlen, writecount ;
  1310. sf_count_t total = 0 ;
  1311. bufferlen = ARRAY_LEN (psf->u.ucbuf) ;
  1312. while (len > 0)
  1313. { if (len < bufferlen)
  1314. bufferlen = (int) len ;
  1315. i2uc_array (ptr + total, psf->u.ucbuf, bufferlen) ;
  1316. writecount = psf_fwrite (psf->u.ucbuf, sizeof (signed char), bufferlen, psf) ;
  1317. total += writecount ;
  1318. if (writecount < bufferlen)
  1319. break ;
  1320. len -= writecount ;
  1321. } ;
  1322. return total ;
  1323. } /* pcm_write_i2uc */
  1324. static sf_count_t
  1325. pcm_write_i2bes (SF_PRIVATE *psf, const int *ptr, sf_count_t len)
  1326. { int bufferlen, writecount ;
  1327. sf_count_t total = 0 ;
  1328. bufferlen = ARRAY_LEN (psf->u.sbuf) ;
  1329. while (len > 0)
  1330. { if (len < bufferlen)
  1331. bufferlen = (int) len ;
  1332. i2bes_array (ptr + total, psf->u.sbuf, bufferlen) ;
  1333. writecount = psf_fwrite (psf->u.sbuf, sizeof (short), bufferlen, psf) ;
  1334. total += writecount ;
  1335. if (writecount < bufferlen)
  1336. break ;
  1337. len -= writecount ;
  1338. } ;
  1339. return total ;
  1340. } /* pcm_write_i2bes */
  1341. static sf_count_t
  1342. pcm_write_i2les (SF_PRIVATE *psf, const int *ptr, sf_count_t len)
  1343. { int bufferlen, writecount ;
  1344. sf_count_t total = 0 ;
  1345. bufferlen = ARRAY_LEN (psf->u.sbuf) ;
  1346. while (len > 0)
  1347. { if (len < bufferlen)
  1348. bufferlen = (int) len ;
  1349. i2les_array (ptr + total, psf->u.sbuf, bufferlen) ;
  1350. writecount = psf_fwrite (psf->u.sbuf, sizeof (short), bufferlen, psf) ;
  1351. total += writecount ;
  1352. if (writecount < bufferlen)
  1353. break ;
  1354. len -= writecount ;
  1355. } ;
  1356. return total ;
  1357. } /* pcm_write_i2les */
  1358. static sf_count_t
  1359. pcm_write_i2bet (SF_PRIVATE *psf, const int *ptr, sf_count_t len)
  1360. { int bufferlen, writecount ;
  1361. sf_count_t total = 0 ;
  1362. bufferlen = sizeof (psf->u.ucbuf) / SIZEOF_TRIBYTE ;
  1363. while (len > 0)
  1364. { if (len < bufferlen)
  1365. bufferlen = (int) len ;
  1366. i2bet_array (ptr + total, (tribyte*) (psf->u.ucbuf), bufferlen) ;
  1367. writecount = psf_fwrite (psf->u.ucbuf, SIZEOF_TRIBYTE, bufferlen, psf) ;
  1368. total += writecount ;
  1369. if (writecount < bufferlen)
  1370. break ;
  1371. len -= writecount ;
  1372. } ;
  1373. return total ;
  1374. } /* pcm_write_i2bet */
  1375. static sf_count_t
  1376. pcm_write_i2let (SF_PRIVATE *psf, const int *ptr, sf_count_t len)
  1377. { int bufferlen, writecount ;
  1378. sf_count_t total = 0 ;
  1379. bufferlen = sizeof (psf->u.ucbuf) / SIZEOF_TRIBYTE ;
  1380. while (len > 0)
  1381. { if (len < bufferlen)
  1382. bufferlen = (int) len ;
  1383. i2let_array (ptr + total, (tribyte*) (psf->u.ucbuf), bufferlen) ;
  1384. writecount = psf_fwrite (psf->u.ucbuf, SIZEOF_TRIBYTE, bufferlen, psf) ;
  1385. total += writecount ;
  1386. if (writecount < bufferlen)
  1387. break ;
  1388. len -= writecount ;
  1389. } ;
  1390. return total ;
  1391. } /* pcm_write_i2les */
  1392. static sf_count_t
  1393. pcm_write_i2bei (SF_PRIVATE *psf, const int *ptr, sf_count_t len)
  1394. { int bufferlen, writecount ;
  1395. sf_count_t total = 0 ;
  1396. if (CPU_IS_BIG_ENDIAN)
  1397. return psf_fwrite (ptr, sizeof (int), len, psf) ;
  1398. bufferlen = ARRAY_LEN (psf->u.ibuf) ;
  1399. while (len > 0)
  1400. { if (len < bufferlen)
  1401. bufferlen = (int) len ;
  1402. endswap_int_copy (psf->u.ibuf, ptr + total, bufferlen) ;
  1403. writecount = psf_fwrite (psf->u.ibuf, sizeof (int), bufferlen, psf) ;
  1404. total += writecount ;
  1405. if (writecount < bufferlen)
  1406. break ;
  1407. len -= writecount ;
  1408. } ;
  1409. return total ;
  1410. } /* pcm_write_i2bei */
  1411. static sf_count_t
  1412. pcm_write_i2lei (SF_PRIVATE *psf, const int *ptr, sf_count_t len)
  1413. { int bufferlen, writecount ;
  1414. sf_count_t total = 0 ;
  1415. if (CPU_IS_LITTLE_ENDIAN)
  1416. return psf_fwrite (ptr, sizeof (int), len, psf) ;
  1417. bufferlen = ARRAY_LEN (psf->u.ibuf) ;
  1418. while (len > 0)
  1419. { if (len < bufferlen)
  1420. bufferlen = (int) len ;
  1421. endswap_int_copy (psf->u.ibuf, ptr + total, bufferlen) ;
  1422. writecount = psf_fwrite (psf->u.ibuf, sizeof (int), bufferlen, psf) ;
  1423. total += writecount ;
  1424. if (writecount < bufferlen)
  1425. break ;
  1426. len -= writecount ;
  1427. } ;
  1428. return total ;
  1429. } /* pcm_write_i2lei */
  1430. /*------------------------------------------------------------------------------
  1431. **==============================================================================
  1432. **------------------------------------------------------------------------------
  1433. */
  1434. static void
  1435. f2sc_array (const float *src, signed char *dest, int count, int normalize)
  1436. { float normfact ;
  1437. normfact = normalize ? (1.0 * 0x7F) : 1.0 ;
  1438. while (--count >= 0)
  1439. { dest [count] = lrintf (src [count] * normfact) ;
  1440. } ;
  1441. } /* f2sc_array */
  1442. static void
  1443. f2sc_clip_array (const float *src, signed char *dest, int count, int normalize)
  1444. { float normfact, scaled_value ;
  1445. normfact = normalize ? (8.0 * 0x10000000) : (1.0 * 0x1000000) ;
  1446. while (--count >= 0)
  1447. { scaled_value = src [count] * normfact ;
  1448. if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7FFFFFFF))
  1449. { dest [count] = 127 ;
  1450. continue ;
  1451. } ;
  1452. if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x10000000))
  1453. { dest [count] = -128 ;
  1454. continue ;
  1455. } ;
  1456. des