2 ** Copyright (C) 1999-2011 Erik de Castro Lopo <erikd@mega-nerd.com>
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.
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.
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.
31 /* These required here because we write the header in this file. */
33 #define RIFF_MARKER (MAKE_MARKER ('R', 'I', 'F', 'F'))
34 #define WAVE_MARKER (MAKE_MARKER ('W', 'A', 'V', 'E'))
35 #define fmt_MARKER (MAKE_MARKER ('f', 'm', 't', ' '))
36 #define fact_MARKER (MAKE_MARKER ('f', 'a', 'c', 't'))
37 #define data_MARKER (MAKE_MARKER ('d', 'a', 't', 'a'))
39 #define WAVE_FORMAT_MS_ADPCM 0x0002
42 { int channels
, blocksize
, samplesperblock
, blocks
, dataremaining
;
44 sf_count_t samplecount
;
46 unsigned char *block
;
47 #if HAVE_FLEXIBLE_ARRAY
48 short dummydata
[] ; /* ISO C99 struct flexible array. */
50 short dummydata
[0] ; /* This is a hack an might not work. */
54 /*============================================================================================
55 ** MS ADPCM static data and functions.
58 static int AdaptationTable
[] =
59 { 230, 230, 230, 230, 307, 409, 512, 614,
60 768, 614, 512, 409, 307, 230, 230, 230
63 /* TODO : The first 7 coef's are are always hardcode and must
64 appear in the actual WAVE file. They should be read in
65 in case a sound program added extras to the list. */
67 static int AdaptCoeff1
[MSADPCM_ADAPT_COEFF_COUNT
] =
68 { 256, 512, 0, 192, 240, 460, 392
71 static int AdaptCoeff2
[MSADPCM_ADAPT_COEFF_COUNT
] =
72 { 0, -256, 0, 64, 0, -208, -232
75 /*============================================================================================
76 ** MS ADPCM Block Layout.
77 ** ======================
78 ** Block is usually 256, 512 or 1024 bytes depending on sample rate.
79 ** For a mono file, the block is laid out as follows:
81 ** 0 block predictor [0..6]
82 ** 1,2 initial idelta (positive)
85 ** 7..n packed bytecodes
87 ** For a stereo file, the block is laid out as follows:
89 ** 0 block predictor [0..6] for left channel
90 ** 1 block predictor [0..6] for right channel
91 ** 2,3 initial idelta (positive) for left channel
92 ** 4,5 initial idelta (positive) for right channel
93 ** 6,7 sample 1 for left channel
94 ** 8,9 sample 1 for right channel
95 ** 10,11 sample 0 for left channel
96 ** 12,13 sample 0 for right channel
97 ** 14..n packed bytecodes
100 /*============================================================================================
104 static int msadpcm_decode_block (SF_PRIVATE
*psf
, MSADPCM_PRIVATE
*pms
) ;
105 static sf_count_t
msadpcm_read_block (SF_PRIVATE
*psf
, MSADPCM_PRIVATE
*pms
, short *ptr
, int len
) ;
107 static int msadpcm_encode_block (SF_PRIVATE
*psf
, MSADPCM_PRIVATE
*pms
) ;
108 static sf_count_t
msadpcm_write_block (SF_PRIVATE
*psf
, MSADPCM_PRIVATE
*pms
, const short *ptr
, int len
) ;
110 static sf_count_t
msadpcm_read_s (SF_PRIVATE
*psf
, short *ptr
, sf_count_t len
) ;
111 static sf_count_t
msadpcm_read_i (SF_PRIVATE
*psf
, int *ptr
, sf_count_t len
) ;
112 static sf_count_t
msadpcm_read_f (SF_PRIVATE
*psf
, float *ptr
, sf_count_t len
) ;
113 static sf_count_t
msadpcm_read_d (SF_PRIVATE
*psf
, double *ptr
, sf_count_t len
) ;
115 static sf_count_t
msadpcm_write_s (SF_PRIVATE
*psf
, const short *ptr
, sf_count_t len
) ;
116 static sf_count_t
msadpcm_write_i (SF_PRIVATE
*psf
, const int *ptr
, sf_count_t len
) ;
117 static sf_count_t
msadpcm_write_f (SF_PRIVATE
*psf
, const float *ptr
, sf_count_t len
) ;
118 static sf_count_t
msadpcm_write_d (SF_PRIVATE
*psf
, const double *ptr
, sf_count_t len
) ;
120 static sf_count_t
msadpcm_seek (SF_PRIVATE
*psf
, int mode
, sf_count_t offset
) ;
121 static int msadpcm_close (SF_PRIVATE
*psf
) ;
123 static void choose_predictor (unsigned int channels
, short *data
, int *bpred
, int *idelta
) ;
125 /*============================================================================================
126 ** MS ADPCM Read Functions.
130 wav_w64_msadpcm_init (SF_PRIVATE
*psf
, int blockalign
, int samplesperblock
)
131 { MSADPCM_PRIVATE
*pms
;
132 unsigned int pmssize
;
135 if (psf
->codec_data
!= NULL
)
136 { psf_log_printf (psf
, "*** psf->codec_data is not NULL.\n") ;
137 return SFE_INTERNAL
;
140 if (psf
->file
.mode
== SFM_WRITE
)
141 samplesperblock
= 2 + 2 * (blockalign
- 7 * psf
->sf
.channels
) / psf
->sf
.channels
;
143 pmssize
= sizeof (MSADPCM_PRIVATE
) + blockalign
+ 3 * psf
->sf
.channels
* samplesperblock
;
145 if (! (psf
->codec_data
= calloc (1, pmssize
)))
146 return SFE_MALLOC_FAILED
;
147 pms
= (MSADPCM_PRIVATE
*) psf
->codec_data
;
149 pms
->samples
= pms
->dummydata
;
150 pms
->block
= (unsigned char*) (pms
->dummydata
+ psf
->sf
.channels
* samplesperblock
) ;
152 pms
->channels
= psf
->sf
.channels
;
153 pms
->blocksize
= blockalign
;
154 pms
->samplesperblock
= samplesperblock
;
156 if (pms
->blocksize
== 0)
157 { psf_log_printf (psf
, "*** Error : pms->blocksize should not be zero.\n") ;
158 return SFE_INTERNAL
;
161 if (psf
->file
.mode
== SFM_READ
)
162 { pms
->dataremaining
= psf
->datalength
;
164 if (psf
->datalength
% pms
->blocksize
)
165 pms
->blocks
= psf
->datalength
/ pms
->blocksize
+ 1 ;
167 pms
->blocks
= psf
->datalength
/ pms
->blocksize
;
169 count
= 2 * (pms
->blocksize
- 6 * pms
->channels
) / pms
->channels
;
170 if (pms
->samplesperblock
!= count
)
171 { psf_log_printf (psf
, "*** Error : samplesperblock should be %d.\n", count
) ;
172 return SFE_INTERNAL
;
175 psf
->sf
.frames
= (psf
->datalength
/ pms
->blocksize
) * pms
->samplesperblock
;
177 psf_log_printf (psf
, " bpred idelta\n") ;
179 msadpcm_decode_block (psf
, pms
) ;
181 psf
->read_short
= msadpcm_read_s
;
182 psf
->read_int
= msadpcm_read_i
;
183 psf
->read_float
= msadpcm_read_f
;
184 psf
->read_double
= msadpcm_read_d
;
187 if (psf
->file
.mode
== SFM_WRITE
)
188 { pms
->samples
= pms
->dummydata
;
190 pms
->samplecount
= 0 ;
192 psf
->write_short
= msadpcm_write_s
;
193 psf
->write_int
= msadpcm_write_i
;
194 psf
->write_float
= msadpcm_write_f
;
195 psf
->write_double
= msadpcm_write_d
;
198 psf
->codec_close
= msadpcm_close
;
199 psf
->seek
= msadpcm_seek
;
202 } /* wav_w64_msadpcm_init */
205 msadpcm_decode_block (SF_PRIVATE
*psf
, MSADPCM_PRIVATE
*pms
)
206 { int chan
, k
, blockindx
, sampleindx
;
207 short bytecode
, bpred
[2], chan_idelta
[2] ;
214 pms
->samplecount
= 0 ;
216 if (pms
->blockcount
> pms
->blocks
)
217 { memset (pms
->samples
, 0, pms
->samplesperblock
* pms
->channels
) ;
221 if ((k
= psf_fread (pms
->block
, 1, pms
->blocksize
, psf
)) != pms
->blocksize
)
222 psf_log_printf (psf
, "*** Warning : short read (%d != %d).\n", k
, pms
->blocksize
) ;
224 /* Read and check the block header. */
226 if (pms
->channels
== 1)
227 { bpred
[0] = pms
->block
[0] ;
230 psf_log_printf (psf
, "MS ADPCM synchronisation error (%d).\n", bpred
[0]) ;
232 chan_idelta
[0] = pms
->block
[1] | (pms
->block
[2] << 8) ;
233 chan_idelta
[1] = 0 ;
235 psf_log_printf (psf
, "(%d) (%d)\n", bpred
[0], chan_idelta
[0]) ;
237 pms
->samples
[1] = pms
->block
[3] | (pms
->block
[4] << 8) ;
238 pms
->samples
[0] = pms
->block
[5] | (pms
->block
[6] << 8) ;
242 { bpred
[0] = pms
->block
[0] ;
243 bpred
[1] = pms
->block
[1] ;
245 if (bpred
[0] >= 7 || bpred
[1] >= 7)
246 psf_log_printf (psf
, "MS ADPCM synchronisation error (%d %d).\n", bpred
[0], bpred
[1]) ;
248 chan_idelta
[0] = pms
->block
[2] | (pms
->block
[3] << 8) ;
249 chan_idelta
[1] = pms
->block
[4] | (pms
->block
[5] << 8) ;
251 psf_log_printf (psf
, "(%d, %d) (%d, %d)\n", bpred
[0], bpred
[1], chan_idelta
[0], chan_idelta
[1]) ;
253 pms
->samples
[2] = pms
->block
[6] | (pms
->block
[7] << 8) ;
254 pms
->samples
[3] = pms
->block
[8] | (pms
->block
[9] << 8) ;
256 pms
->samples
[0] = pms
->block
[10] | (pms
->block
[11] << 8) ;
257 pms
->samples
[1] = pms
->block
[12] | (pms
->block
[13] << 8) ;
262 /*--------------------------------------------------------
263 This was left over from a time when calculations were done
264 as ints rather than shorts. Keep this around as a reminder
265 in case I ever find a file which decodes incorrectly.
267 if (chan_idelta [0] & 0x8000)
268 chan_idelta [0] -= 0x10000 ;
269 if (chan_idelta [1] & 0x8000)
270 chan_idelta [1] -= 0x10000 ;
271 --------------------------------------------------------*/
273 /* Pull apart the packed 4 bit samples and store them in their
274 ** correct sample positions.
277 sampleindx
= 2 * pms
->channels
;
278 while (blockindx
< pms
->blocksize
)
279 { bytecode
= pms
->block
[blockindx
++] ;
280 pms
->samples
[sampleindx
++] = (bytecode
>> 4) & 0x0F ;
281 pms
->samples
[sampleindx
++] = bytecode
& 0x0F ;
284 /* Decode the encoded 4 bit samples. */
286 for (k
= 2 * pms
->channels
; k
< (pms
->samplesperblock
* pms
->channels
) ; k
++)
287 { chan
= (pms
->channels
> 1) ? (k
% 2) : 0 ;
289 bytecode
= pms
->samples
[k
] & 0xF ;
291 /* Compute next Adaptive Scale Factor (ASF) */
292 idelta
= chan_idelta
[chan
] ;
293 chan_idelta
[chan
] = (AdaptationTable
[bytecode
] * idelta
) >> 8 ; /* => / 256 => FIXED_POINT_ADAPTATION_BASE == 256 */
294 if (chan_idelta
[chan
] < 16)
295 chan_idelta
[chan
] = 16 ;
299 predict
= ((pms
->samples
[k
- pms
->channels
] * AdaptCoeff1
[bpred
[chan
]])
300 + (pms
->samples
[k
- 2 * pms
->channels
] * AdaptCoeff2
[bpred
[chan
]])) >> 8 ; /* => / 256 => FIXED_POINT_COEFF_BASE == 256 */
301 current
= (bytecode
* idelta
) + predict
;
305 else if (current
< -32768)
308 pms
->samples
[k
] = current
;
312 } /* msadpcm_decode_block */
315 msadpcm_read_block (SF_PRIVATE
*psf
, MSADPCM_PRIVATE
*pms
, short *ptr
, int len
)
316 { int count
, total
= 0, indx
= 0 ;
319 { if (pms
->blockcount
>= pms
->blocks
&& pms
->samplecount
>= pms
->samplesperblock
)
320 { memset (&(ptr
[indx
]), 0, (size_t) ((len
- indx
) * sizeof (short))) ;
324 if (pms
->samplecount
>= pms
->samplesperblock
)
325 msadpcm_decode_block (psf
, pms
) ;
327 count
= (pms
->samplesperblock
- pms
->samplecount
) * pms
->channels
;
328 count
= (len
- indx
> count
) ? count
: len
- indx
;
330 memcpy (&(ptr
[indx
]), &(pms
->samples
[pms
->samplecount
* pms
->channels
]), count
* sizeof (short)) ;
332 pms
->samplecount
+= count
/ pms
->channels
;
337 } /* msadpcm_read_block */
340 msadpcm_read_s (SF_PRIVATE
*psf
, short *ptr
, sf_count_t len
)
341 { MSADPCM_PRIVATE
*pms
;
342 int readcount
, count
;
343 sf_count_t total
= 0 ;
345 if (! psf
->codec_data
)
347 pms
= (MSADPCM_PRIVATE
*) psf
->codec_data
;
350 { readcount
= (len
> 0x10000000) ? 0x10000000 : (int) len
;
352 count
= msadpcm_read_block (psf
, pms
, ptr
, readcount
) ;
356 if (count
!= readcount
)
361 } /* msadpcm_read_s */
364 msadpcm_read_i (SF_PRIVATE
*psf
, int *ptr
, sf_count_t len
)
365 { MSADPCM_PRIVATE
*pms
;
367 int k
, bufferlen
, readcount
= 0, count
;
368 sf_count_t total
= 0 ;
370 if (! psf
->codec_data
)
372 pms
= (MSADPCM_PRIVATE
*) psf
->codec_data
;
375 bufferlen
= ARRAY_LEN (psf
->u
.sbuf
) ;
377 { readcount
= (len
>= bufferlen
) ? bufferlen
: len
;
378 count
= msadpcm_read_block (psf
, pms
, sptr
, readcount
) ;
379 for (k
= 0 ; k
< readcount
; k
++)
380 ptr
[total
+ k
] = sptr
[k
] << 16 ;
383 if (count
!= readcount
)
387 } /* msadpcm_read_i */
390 msadpcm_read_f (SF_PRIVATE
*psf
, float *ptr
, sf_count_t len
)
391 { MSADPCM_PRIVATE
*pms
;
393 int k
, bufferlen
, readcount
= 0, count
;
394 sf_count_t total
= 0 ;
397 if (! psf
->codec_data
)
399 pms
= (MSADPCM_PRIVATE
*) psf
->codec_data
;
401 normfact
= (psf
->norm_float
== SF_TRUE
) ? 1.0 / ((float) 0x8000) : 1.0 ;
403 bufferlen
= ARRAY_LEN (psf
->u
.sbuf
) ;
405 { readcount
= (len
>= bufferlen
) ? bufferlen
: len
;
406 count
= msadpcm_read_block (psf
, pms
, sptr
, readcount
) ;
407 for (k
= 0 ; k
< readcount
; k
++)
408 ptr
[total
+ k
] = normfact
* (float) (sptr
[k
]) ;
411 if (count
!= readcount
)
415 } /* msadpcm_read_f */
418 msadpcm_read_d (SF_PRIVATE
*psf
, double *ptr
, sf_count_t len
)
419 { MSADPCM_PRIVATE
*pms
;
421 int k
, bufferlen
, readcount
= 0, count
;
422 sf_count_t total
= 0 ;
425 normfact
= (psf
->norm_double
== SF_TRUE
) ? 1.0 / ((double) 0x8000) : 1.0 ;
427 if (! psf
->codec_data
)
429 pms
= (MSADPCM_PRIVATE
*) psf
->codec_data
;
432 bufferlen
= ARRAY_LEN (psf
->u
.sbuf
) ;
434 { readcount
= (len
>= bufferlen
) ? bufferlen
: len
;
435 count
= msadpcm_read_block (psf
, pms
, sptr
, readcount
) ;
436 for (k
= 0 ; k
< readcount
; k
++)
437 ptr
[total
+ k
] = normfact
* (double) (sptr
[k
]) ;
440 if (count
!= readcount
)
444 } /* msadpcm_read_d */
447 msadpcm_seek (SF_PRIVATE
*psf
, int mode
, sf_count_t offset
)
448 { MSADPCM_PRIVATE
*pms
;
449 int newblock
, newsample
;
451 if (! psf
->codec_data
)
453 pms
= (MSADPCM_PRIVATE
*) psf
->codec_data
;
455 if (psf
->datalength
< 0 || psf
->dataoffset
< 0)
456 { psf
->error
= SFE_BAD_SEEK
;
457 return PSF_SEEK_ERROR
;
461 { psf_fseek (psf
, psf
->dataoffset
, SEEK_SET
) ;
462 pms
->blockcount
= 0 ;
463 msadpcm_decode_block (psf
, pms
) ;
464 pms
->samplecount
= 0 ;
468 if (offset
< 0 || offset
> pms
->blocks
* pms
->samplesperblock
)
469 { psf
->error
= SFE_BAD_SEEK
;
470 return PSF_SEEK_ERROR
;
473 newblock
= offset
/ pms
->samplesperblock
;
474 newsample
= offset
% pms
->samplesperblock
;
476 if (mode
== SFM_READ
)
477 { psf_fseek (psf
, psf
->dataoffset
+ newblock
* pms
->blocksize
, SEEK_SET
) ;
478 pms
->blockcount
= newblock
;
479 msadpcm_decode_block (psf
, pms
) ;
480 pms
->samplecount
= newsample
;
483 { /* What to do about write??? */
484 psf
->error
= SFE_BAD_SEEK
;
485 return PSF_SEEK_ERROR
;
488 return newblock
* pms
->samplesperblock
+ newsample
;
491 /*==========================================================================================
492 ** MS ADPCM Write Functions.
496 msadpcm_write_adapt_coeffs (SF_PRIVATE
*psf
)
499 for (k
= 0 ; k
< MSADPCM_ADAPT_COEFF_COUNT
; k
++)
500 psf_binheader_writef (psf
, "22", AdaptCoeff1
[k
], AdaptCoeff2
[k
]) ;
501 } /* msadpcm_write_adapt_coeffs */
503 /*==========================================================================================
507 msadpcm_encode_block (SF_PRIVATE
*psf
, MSADPCM_PRIVATE
*pms
)
508 { unsigned int blockindx
;
510 int chan
, k
, predict
, bpred
[2], idelta
[2], errordelta
, newsamp
;
512 choose_predictor (pms
->channels
, pms
->samples
, bpred
, idelta
) ;
514 /* Write the block header. */
516 if (pms
->channels
== 1)
517 { pms
->block
[0] = bpred
[0] ;
518 pms
->block
[1] = idelta
[0] & 0xFF ;
519 pms
->block
[2] = idelta
[0] >> 8 ;
520 pms
->block
[3] = pms
->samples
[1] & 0xFF ;
521 pms
->block
[4] = pms
->samples
[1] >> 8 ;
522 pms
->block
[5] = pms
->samples
[0] & 0xFF ;
523 pms
->block
[6] = pms
->samples
[0] >> 8 ;
528 /* Encode the samples as 4 bit. */
530 for (k
= 2 ; k
< pms
->samplesperblock
; k
++)
531 { predict
= (pms
->samples
[k
-1] * AdaptCoeff1
[bpred
[0]] + pms
->samples
[k
-2] * AdaptCoeff2
[bpred
[0]]) >> 8 ;
532 errordelta
= (pms
->samples
[k
] - predict
) / idelta
[0] ;
535 else if (errordelta
> 7)
537 newsamp
= predict
+ (idelta
[0] * errordelta
) ;
540 else if (newsamp
< -32768)
545 byte
= (byte
<< 4) | (errordelta
& 0xF) ;
547 { pms
->block
[blockindx
++] = byte
;
551 idelta
[0] = (idelta
[0] * AdaptationTable
[errordelta
]) >> 8 ;
554 pms
->samples
[k
] = newsamp
;
559 pms
->block
[0] = bpred
[0] ;
560 pms
->block
[1] = bpred
[1] ;
562 pms
->block
[2] = idelta
[0] & 0xFF ;
563 pms
->block
[3] = idelta
[0] >> 8 ;
564 pms
->block
[4] = idelta
[1] & 0xFF ;
565 pms
->block
[5] = idelta
[1] >> 8 ;
567 pms
->block
[6] = pms
->samples
[2] & 0xFF ;
568 pms
->block
[7] = pms
->samples
[2] >> 8 ;
569 pms
->block
[8] = pms
->samples
[3] & 0xFF ;
570 pms
->block
[9] = pms
->samples
[3] >> 8 ;
572 pms
->block
[10] = pms
->samples
[0] & 0xFF ;
573 pms
->block
[11] = pms
->samples
[0] >> 8 ;
574 pms
->block
[12] = pms
->samples
[1] & 0xFF ;
575 pms
->block
[13] = pms
->samples
[1] >> 8 ;
581 for (k
= 4 ; k
< 2 * pms
->samplesperblock
; k
++)
584 predict
= (pms
->samples
[k
-2] * AdaptCoeff1
[bpred
[chan
]] + pms
->samples
[k
-4] * AdaptCoeff2
[bpred
[chan
]]) >> 8 ;
585 errordelta
= (pms
->samples
[k
] - predict
) / idelta
[chan
] ;
590 else if (errordelta
> 7)
592 newsamp
= predict
+ (idelta
[chan
] * errordelta
) ;
595 else if (newsamp
< -32768)
600 byte
= (byte
<< 4) | (errordelta
& 0xF) ;
603 { pms
->block
[blockindx
++] = byte
;
607 idelta
[chan
] = (idelta
[chan
] * AdaptationTable
[errordelta
]) >> 8 ;
608 if (idelta
[chan
] < 16)
610 pms
->samples
[k
] = newsamp
;
614 /* Write the block to disk. */
616 if ((k
= psf_fwrite (pms
->block
, 1, pms
->blocksize
, psf
)) != pms
->blocksize
)
617 psf_log_printf (psf
, "*** Warning : short write (%d != %d).\n", k
, pms
->blocksize
) ;
619 memset (pms
->samples
, 0, pms
->samplesperblock
* sizeof (short)) ;
622 pms
->samplecount
= 0 ;
625 } /* msadpcm_encode_block */
628 msadpcm_write_block (SF_PRIVATE
*psf
, MSADPCM_PRIVATE
*pms
, const short *ptr
, int len
)
629 { int count
, total
= 0, indx
= 0 ;
632 { count
= (pms
->samplesperblock
- pms
->samplecount
) * pms
->channels
;
634 if (count
> len
- indx
)
637 memcpy (&(pms
->samples
[pms
->samplecount
* pms
->channels
]), &(ptr
[total
]), count
* sizeof (short)) ;
639 pms
->samplecount
+= count
/ pms
->channels
;
642 if (pms
->samplecount
>= pms
->samplesperblock
)
643 msadpcm_encode_block (psf
, pms
) ;
647 } /* msadpcm_write_block */
650 msadpcm_write_s (SF_PRIVATE
*psf
, const short *ptr
, sf_count_t len
)
651 { MSADPCM_PRIVATE
*pms
;
652 int writecount
, count
;
653 sf_count_t total
= 0 ;
655 if (! psf
->codec_data
)
657 pms
= (MSADPCM_PRIVATE
*) psf
->codec_data
;
660 { writecount
= (len
> 0x10000000) ? 0x10000000 : (int) len
;
662 count
= msadpcm_write_block (psf
, pms
, ptr
, writecount
) ;
666 if (count
!= writecount
)
671 } /* msadpcm_write_s */
674 msadpcm_write_i (SF_PRIVATE
*psf
, const int *ptr
, sf_count_t len
)
675 { MSADPCM_PRIVATE
*pms
;
677 int k
, bufferlen
, writecount
, count
;
678 sf_count_t total
= 0 ;
680 if (! psf
->codec_data
)
682 pms
= (MSADPCM_PRIVATE
*) psf
->codec_data
;
685 bufferlen
= ARRAY_LEN (psf
->u
.sbuf
) ;
687 { writecount
= (len
>= bufferlen
) ? bufferlen
: len
;
688 for (k
= 0 ; k
< writecount
; k
++)
689 sptr
[k
] = ptr
[total
+ k
] >> 16 ;
690 count
= msadpcm_write_block (psf
, pms
, sptr
, writecount
) ;
693 if (count
!= writecount
)
697 } /* msadpcm_write_i */
700 msadpcm_write_f (SF_PRIVATE
*psf
, const float *ptr
, sf_count_t len
)
701 { MSADPCM_PRIVATE
*pms
;
703 int k
, bufferlen
, writecount
, count
;
704 sf_count_t total
= 0 ;
707 if (! psf
->codec_data
)
709 pms
= (MSADPCM_PRIVATE
*) psf
->codec_data
;
711 normfact
= (psf
->norm_float
== SF_TRUE
) ? (1.0 * 0x7FFF) : 1.0 ;
714 bufferlen
= ARRAY_LEN (psf
->u
.sbuf
) ;
716 { writecount
= (len
>= bufferlen
) ? bufferlen
: len
;
717 for (k
= 0 ; k
< writecount
; k
++)
718 sptr
[k
] = lrintf (normfact
* ptr
[total
+ k
]) ;
719 count
= msadpcm_write_block (psf
, pms
, sptr
, writecount
) ;
722 if (count
!= writecount
)
726 } /* msadpcm_write_f */
729 msadpcm_write_d (SF_PRIVATE
*psf
, const double *ptr
, sf_count_t len
)
730 { MSADPCM_PRIVATE
*pms
;
732 int k
, bufferlen
, writecount
, count
;
733 sf_count_t total
= 0 ;
736 normfact
= (psf
->norm_double
== SF_TRUE
) ? (1.0 * 0x7FFF) : 1.0 ;
738 if (! psf
->codec_data
)
740 pms
= (MSADPCM_PRIVATE
*) psf
->codec_data
;
743 bufferlen
= ARRAY_LEN (psf
->u
.sbuf
) ;
745 { writecount
= (len
>= bufferlen
) ? bufferlen
: len
;
746 for (k
= 0 ; k
< writecount
; k
++)
747 sptr
[k
] = lrint (normfact
* ptr
[total
+ k
]) ;
748 count
= msadpcm_write_block (psf
, pms
, sptr
, writecount
) ;
751 if (count
!= writecount
)
755 } /* msadpcm_write_d */
757 /*========================================================================================
761 msadpcm_close (SF_PRIVATE
*psf
)
762 { MSADPCM_PRIVATE
*pms
;
764 pms
= (MSADPCM_PRIVATE
*) psf
->codec_data
;
766 if (psf
->file
.mode
== SFM_WRITE
)
767 { /* Now we know static int for certain the length of the file we can
768 ** re-write the header.
771 if (pms
->samplecount
&& pms
->samplecount
< pms
->samplesperblock
)
772 msadpcm_encode_block (psf
, pms
) ;
776 } /* msadpcm_close */
778 /*========================================================================================
782 /*----------------------------------------------------------------------------------------
783 ** Choosing the block predictor.
784 ** Each block requires a predictor and an idelta for each channel.
785 ** The predictor is in the range [0..6] which is an indx into the two AdaptCoeff tables.
786 ** The predictor is chosen by trying all of the possible predictors on a small set of
787 ** samples at the beginning of the block. The predictor with the smallest average
788 ** abs (idelta) is chosen as the best predictor for this block.
789 ** The value of idelta is chosen to to give a 4 bit code value of +/- 4 (approx. half the
790 ** max. code value). If the average abs (idelta) is zero, the sixth predictor is chosen.
791 ** If the value of idelta is less then 16 it is set to 16.
793 ** Microsoft uses an IDELTA_COUNT (number of sample pairs used to choose best predictor)
794 ** value of 3. The best possible results would be obtained by using all the samples to
795 ** choose the predictor.
798 #define IDELTA_COUNT 3
801 choose_predictor (unsigned int channels
, short *data
, int *block_pred
, int *idelta
)
802 { unsigned int chan
, k
, bpred
, idelta_sum
, best_bpred
, best_idelta
;
804 for (chan
= 0 ; chan
< channels
; chan
++)
805 { best_bpred
= best_idelta
= 0 ;
807 for (bpred
= 0 ; bpred
< 7 ; bpred
++)
809 for (k
= 2 ; k
< 2 + IDELTA_COUNT
; k
++)
810 idelta_sum
+= abs (data
[k
* channels
] - ((data
[(k
- 1) * channels
] * AdaptCoeff1
[bpred
] + data
[(k
- 2) * channels
] * AdaptCoeff2
[bpred
]) >> 8)) ;
811 idelta_sum
/= (4 * IDELTA_COUNT
) ;
813 if (bpred
== 0 || idelta_sum
< best_idelta
)
814 { best_bpred
= bpred
;
815 best_idelta
= idelta_sum
;
819 { best_bpred
= bpred
;
824 } ; /* for bpred ... */
825 if (best_idelta
< 16)
828 block_pred
[chan
] = best_bpred
;
829 idelta
[chan
] = best_idelta
;
833 } /* choose_predictor */