2 ** Copyright (C) 1999-2009 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.
30 typedef struct IMA_ADPCM_PRIVATE_tag
31 { int (*decode_block) (SF_PRIVATE *psf, struct IMA_ADPCM_PRIVATE_tag *pima) ;
32 int (*encode_block) (SF_PRIVATE *psf, struct IMA_ADPCM_PRIVATE_tag *pima) ;
34 int channels, blocksize, samplesperblock, blocks ;
35 int blockcount, samplecount ;
38 unsigned char *block ;
40 #if HAVE_FLEXIBLE_ARRAY
41 short data [] ; /* ISO C99 struct flexible array. */
43 short data [0] ; /* This is a hack and might not work. */
47 /*============================================================================================
48 ** Predefined IMA ADPCM data.
51 static int ima_indx_adjust [16] =
52 { -1, -1, -1, -1, /* +0 - +3, decrease the step size */
53 2, 4, 6, 8, /* +4 - +7, increase the step size */
54 -1, -1, -1, -1, /* -0 - -3, decrease the step size */
55 2, 4, 6, 8, /* -4 - -7, increase the step size */
58 static int ima_step_size [89] =
59 { 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
60 50, 55, 60, 66, 73, 80, 88, 97, 107, 118, 130, 143, 157, 173, 190, 209, 230,
61 253, 279, 307, 337, 371, 408, 449, 494, 544, 598, 658, 724, 796, 876, 963,
62 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066, 2272, 2499, 2749, 3024, 3327,
63 3660, 4026, 4428, 4871, 5358, 5894, 6484, 7132, 7845, 8630, 9493, 10442,
64 11487, 12635, 13899, 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794,
68 static int ima_reader_init (SF_PRIVATE *psf, int blockalign, int samplesperblock) ;
69 static int ima_writer_init (SF_PRIVATE *psf, int blockalign) ;
71 static int ima_read_block (SF_PRIVATE *psf, IMA_ADPCM_PRIVATE *pima, short *ptr, int len) ;
72 static int ima_write_block (SF_PRIVATE *psf, IMA_ADPCM_PRIVATE *pima, const short *ptr, int len) ;
74 static sf_count_t ima_read_s (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
75 static sf_count_t ima_read_i (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
76 static sf_count_t ima_read_f (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
77 static sf_count_t ima_read_d (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
79 static sf_count_t ima_write_s (SF_PRIVATE *psf, const short *ptr, sf_count_t len) ;
80 static sf_count_t ima_write_i (SF_PRIVATE *psf, const int *ptr, sf_count_t len) ;
81 static sf_count_t ima_write_f (SF_PRIVATE *psf, const float *ptr, sf_count_t len) ;
82 static sf_count_t ima_write_d (SF_PRIVATE *psf, const double *ptr, sf_count_t len) ;
84 static sf_count_t ima_seek (SF_PRIVATE *psf, int mode, sf_count_t offset) ;
86 static int ima_close (SF_PRIVATE *psf) ;
88 static int wav_w64_ima_decode_block (SF_PRIVATE *psf, IMA_ADPCM_PRIVATE *pima) ;
89 static int wav_w64_ima_encode_block (SF_PRIVATE *psf, IMA_ADPCM_PRIVATE *pima) ;
91 /*-static int aiff_ima_reader_init (SF_PRIVATE *psf, int blockalign, int samplesperblock) ;-*/
92 static int aiff_ima_decode_block (SF_PRIVATE *psf, IMA_ADPCM_PRIVATE *pima) ;
93 static int aiff_ima_encode_block (SF_PRIVATE *psf, IMA_ADPCM_PRIVATE *pima) ;
97 clamp_ima_step_index (int indx)
100 if (indx >= ARRAY_LEN (ima_step_size))
101 return ARRAY_LEN (ima_step_size) - 1 ;
104 } /* clamp_ima_step_index */
106 /*============================================================================================
107 ** IMA ADPCM Reader initialisation function.
111 wav_w64_ima_init (SF_PRIVATE *psf, int blockalign, int samplesperblock)
114 if (psf->codec_data != NULL)
115 { psf_log_printf (psf, "*** psf->codec_data is not NULL.\n") ;
116 return SFE_INTERNAL ;
119 if (psf->file.mode == SFM_RDWR)
120 return SFE_BAD_MODE_RW ;
122 if (psf->file.mode == SFM_READ)
123 if ((error = ima_reader_init (psf, blockalign, samplesperblock)))
126 if (psf->file.mode == SFM_WRITE)
127 if ((error = ima_writer_init (psf, blockalign)))
130 psf->codec_close = ima_close ;
131 psf->seek = ima_seek ;
134 } /* wav_w64_ima_init */
137 aiff_ima_init (SF_PRIVATE *psf, int blockalign, int samplesperblock)
140 if (psf->file.mode == SFM_RDWR)
141 return SFE_BAD_MODE_RW ;
143 if (psf->file.mode == SFM_READ)
144 if ((error = ima_reader_init (psf, blockalign, samplesperblock)))
147 if (psf->file.mode == SFM_WRITE)
148 if ((error = ima_writer_init (psf, blockalign)))
151 psf->codec_close = ima_close ;
154 } /* aiff_ima_init */
157 ima_close (SF_PRIVATE *psf)
158 { IMA_ADPCM_PRIVATE *pima ;
160 pima = (IMA_ADPCM_PRIVATE*) psf->codec_data ;
162 if (psf->file.mode == SFM_WRITE)
163 { /* If a block has been partially assembled, write it out
164 ** as the final block.
166 if (pima->samplecount && pima->samplecount < pima->samplesperblock)
167 pima->encode_block (psf, pima) ;
169 psf->sf.frames = pima->samplesperblock * pima->blockcount / psf->sf.channels ;
175 /*============================================================================================
176 ** IMA ADPCM Read Functions.
180 ima_reader_init (SF_PRIVATE *psf, int blockalign, int samplesperblock)
181 { IMA_ADPCM_PRIVATE *pima ;
182 int pimasize, count ;
184 if (psf->file.mode != SFM_READ)
185 return SFE_BAD_MODE_RW ;
187 pimasize = sizeof (IMA_ADPCM_PRIVATE) + blockalign * psf->sf.channels + 3 * psf->sf.channels * samplesperblock ;
189 if (! (pima = malloc (pimasize)))
190 return SFE_MALLOC_FAILED ;
192 psf->codec_data = (void*) pima ;
194 memset (pima, 0, pimasize) ;
196 pima->samples = pima->data ;
197 pima->block = (unsigned char*) (pima->data + samplesperblock * psf->sf.channels) ;
199 pima->channels = psf->sf.channels ;
200 pima->blocksize = blockalign ;
201 pima->samplesperblock = samplesperblock ;
203 psf->filelength = psf_get_filelen (psf) ;
204 psf->datalength = (psf->dataend) ? psf->dataend - psf->dataoffset :
205 psf->filelength - psf->dataoffset ;
207 if (pima->blocksize == 0)
208 { psf_log_printf (psf, "*** Error : pima->blocksize should not be zero.\n") ;
209 return SFE_INTERNAL ;
212 if (psf->datalength % pima->blocksize)
213 pima->blocks = psf->datalength / pima->blocksize + 1 ;
215 pima->blocks = psf->datalength / pima->blocksize ;
217 switch (SF_CONTAINER (psf->sf.format))
218 { case SF_FORMAT_WAV :
220 count = 2 * (pima->blocksize - 4 * pima->channels) / pima->channels + 1 ;
222 if (pima->samplesperblock != count)
223 { psf_log_printf (psf, "*** Error : samplesperblock should be %d.\n", count) ;
224 return SFE_INTERNAL ;
227 pima->decode_block = wav_w64_ima_decode_block ;
229 psf->sf.frames = pima->samplesperblock * pima->blocks ;
232 case SF_FORMAT_AIFF :
233 psf_log_printf (psf, "still need to check block count\n") ;
234 pima->decode_block = aiff_ima_decode_block ;
235 psf->sf.frames = pima->samplesperblock * pima->blocks / pima->channels ;
239 psf_log_printf (psf, "ima_reader_init: bad psf->sf.format\n") ;
240 return SFE_INTERNAL ;
243 pima->decode_block (psf, pima) ; /* Read first block. */
245 psf->read_short = ima_read_s ;
246 psf->read_int = ima_read_i ;
247 psf->read_float = ima_read_f ;
248 psf->read_double = ima_read_d ;
251 } /* ima_reader_init */
254 aiff_ima_decode_block (SF_PRIVATE *psf, IMA_ADPCM_PRIVATE *pima)
255 { unsigned char *blockdata ;
256 int chan, k, diff, bytecode ;
257 short step, stepindx, predictor, *sampledata ;
259 static int count = 0 ;
262 pima->blockcount += pima->channels ;
263 pima->samplecount = 0 ;
265 if (pima->blockcount > pima->blocks)
266 { memset (pima->samples, 0, pima->samplesperblock * pima->channels * sizeof (short)) ;
270 if ((k = psf_fread (pima->block, 1, pima->blocksize * pima->channels, psf)) != pima->blocksize * pima->channels)
271 psf_log_printf (psf, "*** Warning : short read (%d != %d).\n", k, pima->blocksize) ;
273 /* Read and check the block header. */
274 for (chan = 0 ; chan < pima->channels ; chan++)
275 { blockdata = pima->block + chan * 34 ;
276 sampledata = pima->samples + chan ;
278 predictor = (blockdata [0] << 8) | (blockdata [1] & 0x80) ;
280 stepindx = blockdata [1] & 0x7F ;
281 stepindx = clamp_ima_step_index (stepindx) ;
284 ** Pull apart the packed 4 bit samples and store them in their
285 ** correct sample positions.
287 for (k = 0 ; k < pima->blocksize - 2 ; k++)
288 { bytecode = blockdata [k + 2] ;
289 sampledata [pima->channels * (2 * k + 0)] = bytecode & 0xF ;
290 sampledata [pima->channels * (2 * k + 1)] = (bytecode >> 4) & 0xF ;
293 /* Decode the encoded 4 bit samples. */
294 for (k = 0 ; k < pima->samplesperblock ; k ++)
295 { step = ima_step_size [stepindx] ;
297 bytecode = pima->samples [pima->channels * k + chan] ;
299 stepindx += ima_indx_adjust [bytecode] ;
300 stepindx = clamp_ima_step_index (stepindx) ;
303 if (bytecode & 1) diff += step >> 2 ;
304 if (bytecode & 2) diff += step >> 1 ;
305 if (bytecode & 4) diff += step ;
306 if (bytecode & 8) diff = -diff ;
309 pima->samples [pima->channels * k + chan] = predictor ;
314 } /* aiff_ima_decode_block */
317 aiff_ima_encode_block (SF_PRIVATE *psf, IMA_ADPCM_PRIVATE *pima)
318 { int chan, k, step, diff, vpdiff, blockindx, indx ;
319 short bytecode, mask ;
321 /* Encode the block header. */
322 for (chan = 0 ; chan < pima->channels ; chan ++)
323 { blockindx = chan * pima->blocksize ;
325 pima->block [blockindx] = (pima->samples [chan] >> 8) & 0xFF ;
326 pima->block [blockindx + 1] = (pima->samples [chan] & 0x80) + (pima->stepindx [chan] & 0x7F) ;
328 pima->previous [chan] = pima->samples [chan] ;
331 /* Encode second and later samples for every block as a 4 bit value. */
332 for (k = pima->channels ; k < (pima->samplesperblock * pima->channels) ; k ++)
333 { chan = (pima->channels > 1) ? (k % 2) : 0 ;
335 diff = pima->samples [k] - pima->previous [chan] ;
338 step = ima_step_size [pima->stepindx [chan]] ;
356 pima->previous [chan] -= vpdiff ;
358 pima->previous [chan] += vpdiff ;
360 if (pima->previous [chan] > 32767)
361 pima->previous [chan] = 32767 ;
362 else if (pima->previous [chan] < -32768)
363 pima->previous [chan] = -32768 ;
365 pima->stepindx [chan] += ima_indx_adjust [bytecode] ;
367 pima->stepindx [chan] = clamp_ima_step_index (pima->stepindx [chan]) ;
368 pima->samples [k] = bytecode ;
371 /* Pack the 4 bit encoded samples. */
373 for (chan = 0 ; chan < pima->channels ; chan ++)
374 { for (indx = pima->channels ; indx < pima->channels * pima->samplesperblock ; indx += 2 * pima->channels)
375 { blockindx = chan * pima->blocksize + 2 + indx / 2 ;
377 pima->block [blockindx] = pima->samples [indx] & 0x0F ;
378 pima->block [blockindx] |= (pima->samples [indx + chan] << 4) & 0xF0 ;
382 /* Write the block to disk. */
384 if ((k = psf_fwrite (pima->block, 1, pima->channels * pima->blocksize, psf)) != pima->channels * pima->blocksize)
385 psf_log_printf (psf, "*** Warning : short write (%d != %d).\n", k, pima->channels * pima->blocksize) ;
387 memset (pima->samples, 0, pima->channels * pima->samplesperblock * sizeof (short)) ;
388 pima->samplecount = 0 ;
389 pima->blockcount ++ ;
392 } /* aiff_ima_encode_block */
395 wav_w64_ima_decode_block (SF_PRIVATE *psf, IMA_ADPCM_PRIVATE *pima)
396 { int chan, k, current, blockindx, indx, indxstart, diff ;
397 short step, bytecode, stepindx [2] ;
399 pima->blockcount ++ ;
400 pima->samplecount = 0 ;
402 if (pima->blockcount > pima->blocks)
403 { memset (pima->samples, 0, pima->samplesperblock * pima->channels * sizeof (short)) ;
407 if ((k = psf_fread (pima->block, 1, pima->blocksize, psf)) != pima->blocksize)
408 psf_log_printf (psf, "*** Warning : short read (%d != %d).\n", k, pima->blocksize) ;
410 /* Read and check the block header. */
412 for (chan = 0 ; chan < pima->channels ; chan++)
413 { current = pima->block [chan*4] | (pima->block [chan*4+1] << 8) ;
414 if (current & 0x8000)
417 stepindx [chan] = pima->block [chan*4+2] ;
418 stepindx [chan] = clamp_ima_step_index (stepindx [chan]) ;
421 if (pima->block [chan*4+3] != 0)
422 psf_log_printf (psf, "IMA ADPCM synchronisation error.\n") ;
424 pima->samples [chan] = current ;
428 ** Pull apart the packed 4 bit samples and store them in their
429 ** correct sample positions.
432 blockindx = 4 * pima->channels ;
434 indxstart = pima->channels ;
435 while (blockindx < pima->blocksize)
436 { for (chan = 0 ; chan < pima->channels ; chan++)
437 { indx = indxstart + chan ;
438 for (k = 0 ; k < 4 ; k++)
439 { bytecode = pima->block [blockindx++] ;
440 pima->samples [indx] = bytecode & 0x0F ;
441 indx += pima->channels ;
442 pima->samples [indx] = (bytecode >> 4) & 0x0F ;
443 indx += pima->channels ;
446 indxstart += 8 * pima->channels ;
449 /* Decode the encoded 4 bit samples. */
451 for (k = pima->channels ; k < (pima->samplesperblock * pima->channels) ; k ++)
452 { chan = (pima->channels > 1) ? (k % 2) : 0 ;
454 bytecode = pima->samples [k] & 0xF ;
456 step = ima_step_size [stepindx [chan]] ;
457 current = pima->samples [k - pima->channels] ;
473 else if (current < -32768)
476 stepindx [chan] += ima_indx_adjust [bytecode] ;
477 stepindx [chan] = clamp_ima_step_index (stepindx [chan]) ;
479 pima->samples [k] = current ;
483 } /* wav_w64_ima_decode_block */
486 wav_w64_ima_encode_block (SF_PRIVATE *psf, IMA_ADPCM_PRIVATE *pima)
487 { int chan, k, step, diff, vpdiff, blockindx, indx, indxstart ;
488 short bytecode, mask ;
490 /* Encode the block header. */
491 for (chan = 0 ; chan < pima->channels ; chan++)
492 { pima->block [chan*4] = pima->samples [chan] & 0xFF ;
493 pima->block [chan*4+1] = (pima->samples [chan] >> 8) & 0xFF ;
495 pima->block [chan*4+2] = pima->stepindx [chan] ;
496 pima->block [chan*4+3] = 0 ;
498 pima->previous [chan] = pima->samples [chan] ;
501 /* Encode the samples as 4 bit. */
503 for (k = pima->channels ; k < (pima->samplesperblock * pima->channels) ; k ++)
504 { chan = (pima->channels > 1) ? (k % 2) : 0 ;
506 diff = pima->samples [k] - pima->previous [chan] ;
509 step = ima_step_size [pima->stepindx [chan]] ;
527 pima->previous [chan] -= vpdiff ;
529 pima->previous [chan] += vpdiff ;
531 if (pima->previous [chan] > 32767)
532 pima->previous [chan] = 32767 ;
533 else if (pima->previous [chan] < -32768)
534 pima->previous [chan] = -32768 ;
536 pima->stepindx [chan] += ima_indx_adjust [bytecode] ;
537 pima->stepindx [chan] = clamp_ima_step_index (pima->stepindx [chan]) ;
539 pima->samples [k] = bytecode ;
542 /* Pack the 4 bit encoded samples. */
544 blockindx = 4 * pima->channels ;
546 indxstart = pima->channels ;
547 while (blockindx < pima->blocksize)
548 { for (chan = 0 ; chan < pima->channels ; chan++)
549 { indx = indxstart + chan ;
550 for (k = 0 ; k < 4 ; k++)
551 { pima->block [blockindx] = pima->samples [indx] & 0x0F ;
552 indx += pima->channels ;
553 pima->block [blockindx] |= (pima->samples [indx] << 4) & 0xF0 ;
554 indx += pima->channels ;
558 indxstart += 8 * pima->channels ;
561 /* Write the block to disk. */
563 if ((k = psf_fwrite (pima->block, 1, pima->blocksize, psf)) != pima->blocksize)
564 psf_log_printf (psf, "*** Warning : short write (%d != %d).\n", k, pima->blocksize) ;
566 memset (pima->samples, 0, pima->samplesperblock * sizeof (short)) ;
567 pima->samplecount = 0 ;
568 pima->blockcount ++ ;
571 } /* wav_w64_ima_encode_block */
574 ima_read_block (SF_PRIVATE *psf, IMA_ADPCM_PRIVATE *pima, short *ptr, int len)
575 { int count, total = 0, indx = 0 ;
578 { if (pima->blockcount >= pima->blocks && pima->samplecount >= pima->samplesperblock)
579 { memset (&(ptr [indx]), 0, (size_t) ((len - indx) * sizeof (short))) ;
583 if (pima->samplecount >= pima->samplesperblock)
584 pima->decode_block (psf, pima) ;
586 count = (pima->samplesperblock - pima->samplecount) * pima->channels ;
587 count = (len - indx > count) ? count : len - indx ;
589 memcpy (&(ptr [indx]), &(pima->samples [pima->samplecount * pima->channels]), count * sizeof (short)) ;
591 pima->samplecount += count / pima->channels ;
596 } /* ima_read_block */
599 ima_read_s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
600 { IMA_ADPCM_PRIVATE *pima ;
601 int readcount, count ;
602 sf_count_t total = 0 ;
604 if (! psf->codec_data)
606 pima = (IMA_ADPCM_PRIVATE*) psf->codec_data ;
609 { readcount = (len > 0x10000000) ? 0x10000000 : (int) len ;
611 count = ima_read_block (psf, pima, ptr, readcount) ;
615 if (count != readcount)
623 ima_read_i (SF_PRIVATE *psf, int *ptr, sf_count_t len)
624 { IMA_ADPCM_PRIVATE *pima ;
626 int k, bufferlen, readcount, count ;
627 sf_count_t total = 0 ;
629 if (! psf->codec_data)
631 pima = (IMA_ADPCM_PRIVATE*) psf->codec_data ;
634 bufferlen = ARRAY_LEN (psf->u.sbuf) ;
636 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
637 count = ima_read_block (psf, pima, sptr, readcount) ;
638 for (k = 0 ; k < readcount ; k++)
639 ptr [total + k] = ((int) sptr [k]) << 16 ;
642 if (count != readcount)
650 ima_read_f (SF_PRIVATE *psf, float *ptr, sf_count_t len)
651 { IMA_ADPCM_PRIVATE *pima ;
653 int k, bufferlen, readcount, count ;
654 sf_count_t total = 0 ;
657 if (! psf->codec_data)
659 pima = (IMA_ADPCM_PRIVATE*) psf->codec_data ;
661 normfact = (psf->norm_float == SF_TRUE) ? 1.0 / ((float) 0x8000) : 1.0 ;
664 bufferlen = ARRAY_LEN (psf->u.sbuf) ;
666 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
667 count = ima_read_block (psf, pima, sptr, readcount) ;
668 for (k = 0 ; k < readcount ; k++)
669 ptr [total + k] = normfact * (float) (sptr [k]) ;
672 if (count != readcount)
680 ima_read_d (SF_PRIVATE *psf, double *ptr, sf_count_t len)
681 { IMA_ADPCM_PRIVATE *pima ;
683 int k, bufferlen, readcount, count ;
684 sf_count_t total = 0 ;
687 if (! psf->codec_data)
689 pima = (IMA_ADPCM_PRIVATE*) psf->codec_data ;
691 normfact = (psf->norm_double == SF_TRUE) ? 1.0 / ((double) 0x8000) : 1.0 ;
694 bufferlen = ARRAY_LEN (psf->u.sbuf) ;
696 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
697 count = ima_read_block (psf, pima, sptr, readcount) ;
698 for (k = 0 ; k < readcount ; k++)
699 ptr [total + k] = normfact * (double) (sptr [k]) ;
702 if (count != readcount)
710 ima_seek (SF_PRIVATE *psf, int mode, sf_count_t offset)
711 { IMA_ADPCM_PRIVATE *pima ;
712 int newblock, newsample ;
714 if (! psf->codec_data)
716 pima = (IMA_ADPCM_PRIVATE*) psf->codec_data ;
718 if (psf->datalength < 0 || psf->dataoffset < 0)
719 { psf->error = SFE_BAD_SEEK ;
720 return PSF_SEEK_ERROR ;
724 { psf_fseek (psf, psf->dataoffset, SEEK_SET) ;
725 pima->blockcount = 0 ;
726 pima->decode_block (psf, pima) ;
727 pima->samplecount = 0 ;
731 if (offset < 0 || offset > pima->blocks * pima->samplesperblock)
732 { psf->error = SFE_BAD_SEEK ;
733 return PSF_SEEK_ERROR ;
736 newblock = offset / pima->samplesperblock ;
737 newsample = offset % pima->samplesperblock ;
739 if (mode == SFM_READ)
740 { psf_fseek (psf, psf->dataoffset + newblock * pima->blocksize, SEEK_SET) ;
741 pima->blockcount = newblock ;
742 pima->decode_block (psf, pima) ;
743 pima->samplecount = newsample ;
746 { /* What to do about write??? */
747 psf->error = SFE_BAD_SEEK ;
748 return PSF_SEEK_ERROR ;
751 return newblock * pima->samplesperblock + newsample ;
754 /*==========================================================================================
755 ** IMA ADPCM Write Functions.
759 ima_writer_init (SF_PRIVATE *psf, int blockalign)
760 { IMA_ADPCM_PRIVATE *pima ;
761 int samplesperblock ;
762 unsigned int pimasize ;
764 if (psf->file.mode != SFM_WRITE)
765 return SFE_BAD_MODE_RW ;
767 samplesperblock = 2 * (blockalign - 4 * psf->sf.channels) / psf->sf.channels + 1 ;
769 pimasize = sizeof (IMA_ADPCM_PRIVATE) + blockalign + 3 * psf->sf.channels * samplesperblock ;
771 if ((pima = calloc (1, pimasize)) == NULL)
772 return SFE_MALLOC_FAILED ;
774 psf->codec_data = (void*) pima ;
776 pima->channels = psf->sf.channels ;
777 pima->blocksize = blockalign ;
778 pima->samplesperblock = samplesperblock ;
780 pima->block = (unsigned char*) pima->data ;
781 pima->samples = (short*) (pima->data + blockalign) ;
783 pima->samplecount = 0 ;
785 switch (SF_CONTAINER (psf->sf.format))
786 { case SF_FORMAT_WAV :
788 pima->encode_block = wav_w64_ima_encode_block ;
791 case SF_FORMAT_AIFF :
792 pima->encode_block = aiff_ima_encode_block ;
796 psf_log_printf (psf, "ima_reader_init: bad psf->sf.format\n") ;
797 return SFE_INTERNAL ;
800 psf->write_short = ima_write_s ;
801 psf->write_int = ima_write_i ;
802 psf->write_float = ima_write_f ;
803 psf->write_double = ima_write_d ;
806 } /* ima_writer_init */
808 /*==========================================================================================
812 ima_write_block (SF_PRIVATE *psf, IMA_ADPCM_PRIVATE *pima, const short *ptr, int len)
813 { int count, total = 0, indx = 0 ;
816 { count = (pima->samplesperblock - pima->samplecount) * pima->channels ;
818 if (count > len - indx)
821 memcpy (&(pima->samples [pima->samplecount * pima->channels]), &(ptr [total]), count * sizeof (short)) ;
823 pima->samplecount += count / pima->channels ;
826 if (pima->samplecount >= pima->samplesperblock)
827 pima->encode_block (psf, pima) ;
831 } /* ima_write_block */
834 ima_write_s (SF_PRIVATE *psf, const short *ptr, sf_count_t len)
835 { IMA_ADPCM_PRIVATE *pima ;
836 int writecount, count ;
837 sf_count_t total = 0 ;
839 if (! psf->codec_data)
841 pima = (IMA_ADPCM_PRIVATE*) psf->codec_data ;
844 { writecount = (len > 0x10000000) ? 0x10000000 : (int) len ;
846 count = ima_write_block (psf, pima, ptr, writecount) ;
850 if (count != writecount)
858 ima_write_i (SF_PRIVATE *psf, const int *ptr, sf_count_t len)
859 { IMA_ADPCM_PRIVATE *pima ;
861 int k, bufferlen, writecount, count ;
862 sf_count_t total = 0 ;
864 if (! psf->codec_data)
866 pima = (IMA_ADPCM_PRIVATE*) psf->codec_data ;
869 bufferlen = ARRAY_LEN (psf->u.sbuf) ;
871 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
872 for (k = 0 ; k < writecount ; k++)
873 sptr [k] = ptr [total + k] >> 16 ;
874 count = ima_write_block (psf, pima, sptr, writecount) ;
877 if (count != writecount)
885 ima_write_f (SF_PRIVATE *psf, const float *ptr, sf_count_t len)
886 { IMA_ADPCM_PRIVATE *pima ;
888 int k, bufferlen, writecount, count ;
889 sf_count_t total = 0 ;
892 if (! psf->codec_data)
894 pima = (IMA_ADPCM_PRIVATE*) psf->codec_data ;
896 normfact = (psf->norm_float == SF_TRUE) ? (1.0 * 0x7FFF) : 1.0 ;
899 bufferlen = ARRAY_LEN (psf->u.sbuf) ;
901 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
902 for (k = 0 ; k < writecount ; k++)
903 sptr [k] = lrintf (normfact * ptr [total + k]) ;
904 count = ima_write_block (psf, pima, sptr, writecount) ;
907 if (count != writecount)
915 ima_write_d (SF_PRIVATE *psf, const double *ptr, sf_count_t len)
916 { IMA_ADPCM_PRIVATE *pima ;
918 int k, bufferlen, writecount, count ;
919 sf_count_t total = 0 ;
922 if (! psf->codec_data)
924 pima = (IMA_ADPCM_PRIVATE*) psf->codec_data ;
926 normfact = (psf->norm_double == SF_TRUE) ? (1.0 * 0x7FFF) : 1.0 ;
929 bufferlen = ARRAY_LEN (psf->u.sbuf) ;
931 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
932 for (k = 0 ; k < writecount ; k++)
933 sptr [k] = lrint (normfact * ptr [total + k]) ;
934 count = ima_write_block (psf, pima, sptr, writecount) ;
937 if (count != writecount)