2 ** Copyright (C) 2002-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.
20 ** This is the OKI / Dialogic ADPCM encoder/decoder. It converts from
21 ** 12 bit linear sample data to a 4 bit ADPCM.
25 * Note: some early Dialogic hardware does not always reset the ADPCM encoder
26 * at the start of each vox file. This can result in clipping and/or DC offset
27 * problems when it comes to decoding the audio. Whilst little can be done
28 * about the clipping, a DC offset can be removed by passing the decoded audio
29 * through a high-pass filter at e.g. 10Hz.
42 #include "ima_oki_adpcm.h"
45 static sf_count_t
vox_read_s (SF_PRIVATE
*psf
, short *ptr
, sf_count_t len
) ;
46 static sf_count_t
vox_read_i (SF_PRIVATE
*psf
, int *ptr
, sf_count_t len
) ;
47 static sf_count_t
vox_read_f (SF_PRIVATE
*psf
, float *ptr
, sf_count_t len
) ;
48 static sf_count_t
vox_read_d (SF_PRIVATE
*psf
, double *ptr
, sf_count_t len
) ;
50 static sf_count_t
vox_write_s (SF_PRIVATE
*psf
, const short *ptr
, sf_count_t len
) ;
51 static sf_count_t
vox_write_i (SF_PRIVATE
*psf
, const int *ptr
, sf_count_t len
) ;
52 static sf_count_t
vox_write_f (SF_PRIVATE
*psf
, const float *ptr
, sf_count_t len
) ;
53 static sf_count_t
vox_write_d (SF_PRIVATE
*psf
, const double *ptr
, sf_count_t len
) ;
55 static int vox_read_block (SF_PRIVATE
*psf
, IMA_OKI_ADPCM
*pvox
, short *ptr
, int len
) ;
57 /*------------------------------------------------------------------------------
61 codec_close (SF_PRIVATE
* psf
)
63 IMA_OKI_ADPCM
* p
= (IMA_OKI_ADPCM
*) psf
->codec_data
;
66 psf_log_printf (psf
, "*** Warning : ADPCM state errors: %d\n", p
->errors
) ;
71 vox_adpcm_init (SF_PRIVATE
*psf
)
72 { IMA_OKI_ADPCM
*pvox
= NULL
;
74 if (psf
->file
.mode
== SFM_RDWR
)
75 return SFE_BAD_MODE_RW
;
77 if (psf
->file
.mode
== SFM_WRITE
&& psf
->sf
.channels
!= 1)
78 return SFE_CHANNEL_COUNT
;
80 if ((pvox
= malloc (sizeof (IMA_OKI_ADPCM
))) == NULL
)
81 return SFE_MALLOC_FAILED
;
83 psf
->codec_data
= (void*) pvox
;
84 memset (pvox
, 0, sizeof (IMA_OKI_ADPCM
)) ;
86 if (psf
->file
.mode
== SFM_WRITE
)
87 { psf
->write_short
= vox_write_s
;
88 psf
->write_int
= vox_write_i
;
89 psf
->write_float
= vox_write_f
;
90 psf
->write_double
= vox_write_d
;
93 { psf_log_printf (psf
, "Header-less OKI Dialogic ADPCM encoded file.\n") ;
94 psf_log_printf (psf
, "Setting up for 8kHz, mono, Vox ADPCM.\n") ;
96 psf
->read_short
= vox_read_s
;
97 psf
->read_int
= vox_read_i
;
98 psf
->read_float
= vox_read_f
;
99 psf
->read_double
= vox_read_d
;
102 /* Standard sample rate chennels etc. */
103 if (psf
->sf
.samplerate
< 1)
104 psf
->sf
.samplerate
= 8000 ;
105 psf
->sf
.channels
= 1 ;
107 psf
->sf
.frames
= psf
->filelength
* 2 ;
109 psf
->sf
.seekable
= SF_FALSE
;
110 psf
->codec_close
= codec_close
;
112 /* Seek back to start of data. */
113 if (psf_fseek (psf
, 0 , SEEK_SET
) == -1)
114 return SFE_BAD_SEEK
;
116 ima_oki_adpcm_init (pvox
, IMA_OKI_ADPCM_TYPE_OKI
) ;
119 } /* vox_adpcm_init */
121 /*==============================================================================
125 vox_read_block (SF_PRIVATE
*psf
, IMA_OKI_ADPCM
*pvox
, short *ptr
, int len
)
129 { pvox
->code_count
= (len
- indx
> IMA_OKI_ADPCM_PCM_LEN
) ? IMA_OKI_ADPCM_CODE_LEN
: (len
- indx
+ 1) / 2 ;
131 if ((k
= psf_fread (pvox
->codes
, 1, pvox
->code_count
, psf
)) != pvox
->code_count
)
132 { if (psf_ftell (psf
) != psf
->filelength
)
133 psf_log_printf (psf
, "*** Warning : short read (%d != %d).\n", k
, pvox
->code_count
) ;
138 pvox
->code_count
= k
;
140 ima_oki_adpcm_decode_block (pvox
) ;
142 memcpy (&(ptr
[indx
]), pvox
->pcm
, pvox
->pcm_count
* sizeof (short)) ;
143 indx
+= pvox
->pcm_count
;
147 } /* vox_read_block */
151 vox_read_s (SF_PRIVATE
*psf
, short *ptr
, sf_count_t len
)
152 { IMA_OKI_ADPCM
*pvox
;
153 int readcount
, count
;
154 sf_count_t total
= 0 ;
156 if (! psf
->codec_data
)
158 pvox
= (IMA_OKI_ADPCM
*) psf
->codec_data
;
161 { readcount
= (len
> 0x10000000) ? 0x10000000 : (int) len
;
163 count
= vox_read_block (psf
, pvox
, ptr
, readcount
) ;
167 if (count
!= readcount
)
175 vox_read_i (SF_PRIVATE
*psf
, int *ptr
, sf_count_t len
)
176 { IMA_OKI_ADPCM
*pvox
;
178 int k
, bufferlen
, readcount
, count
;
179 sf_count_t total
= 0 ;
181 if (! psf
->codec_data
)
183 pvox
= (IMA_OKI_ADPCM
*) psf
->codec_data
;
186 bufferlen
= ARRAY_LEN (psf
->u
.sbuf
) ;
188 { readcount
= (len
>= bufferlen
) ? bufferlen
: (int) len
;
189 count
= vox_read_block (psf
, pvox
, sptr
, readcount
) ;
190 for (k
= 0 ; k
< readcount
; k
++)
191 ptr
[total
+ k
] = ((int) sptr
[k
]) << 16 ;
194 if (count
!= readcount
)
202 vox_read_f (SF_PRIVATE
*psf
, float *ptr
, sf_count_t len
)
203 { IMA_OKI_ADPCM
*pvox
;
205 int k
, bufferlen
, readcount
, count
;
206 sf_count_t total
= 0 ;
209 if (! psf
->codec_data
)
211 pvox
= (IMA_OKI_ADPCM
*) psf
->codec_data
;
213 normfact
= (psf
->norm_float
== SF_TRUE
) ? 1.0 / ((float) 0x8000) : 1.0 ;
216 bufferlen
= ARRAY_LEN (psf
->u
.sbuf
) ;
218 { readcount
= (len
>= bufferlen
) ? bufferlen
: (int) len
;
219 count
= vox_read_block (psf
, pvox
, sptr
, readcount
) ;
220 for (k
= 0 ; k
< readcount
; k
++)
221 ptr
[total
+ k
] = normfact
* (float) (sptr
[k
]) ;
224 if (count
!= readcount
)
232 vox_read_d (SF_PRIVATE
*psf
, double *ptr
, sf_count_t len
)
233 { IMA_OKI_ADPCM
*pvox
;
235 int k
, bufferlen
, readcount
, count
;
236 sf_count_t total
= 0 ;
239 if (! psf
->codec_data
)
241 pvox
= (IMA_OKI_ADPCM
*) psf
->codec_data
;
243 normfact
= (psf
->norm_double
== SF_TRUE
) ? 1.0 / ((double) 0x8000) : 1.0 ;
246 bufferlen
= ARRAY_LEN (psf
->u
.sbuf
) ;
248 { readcount
= (len
>= bufferlen
) ? bufferlen
: (int) len
;
249 count
= vox_read_block (psf
, pvox
, sptr
, readcount
) ;
250 for (k
= 0 ; k
< readcount
; k
++)
251 ptr
[total
+ k
] = normfact
* (double) (sptr
[k
]) ;
254 if (count
!= readcount
)
261 /*------------------------------------------------------------------------------
265 vox_write_block (SF_PRIVATE
*psf
, IMA_OKI_ADPCM
*pvox
, const short *ptr
, int len
)
269 { pvox
->pcm_count
= (len
- indx
> IMA_OKI_ADPCM_PCM_LEN
) ? IMA_OKI_ADPCM_PCM_LEN
: len
- indx
;
271 memcpy (pvox
->pcm
, &(ptr
[indx
]), pvox
->pcm_count
* sizeof (short)) ;
273 ima_oki_adpcm_encode_block (pvox
) ;
275 if ((k
= psf_fwrite (pvox
->codes
, 1, pvox
->code_count
, psf
)) != pvox
->code_count
)
276 psf_log_printf (psf
, "*** Warning : short write (%d != %d).\n", k
, pvox
->code_count
) ;
278 indx
+= pvox
->pcm_count
;
282 } /* vox_write_block */
285 vox_write_s (SF_PRIVATE
*psf
, const short *ptr
, sf_count_t len
)
286 { IMA_OKI_ADPCM
*pvox
;
287 int writecount
, count
;
288 sf_count_t total
= 0 ;
290 if (! psf
->codec_data
)
292 pvox
= (IMA_OKI_ADPCM
*) psf
->codec_data
;
295 { writecount
= (len
> 0x10000000) ? 0x10000000 : (int) len
;
297 count
= vox_write_block (psf
, pvox
, ptr
, writecount
) ;
301 if (count
!= writecount
)
309 vox_write_i (SF_PRIVATE
*psf
, const int *ptr
, sf_count_t len
)
310 { IMA_OKI_ADPCM
*pvox
;
312 int k
, bufferlen
, writecount
, count
;
313 sf_count_t total
= 0 ;
315 if (! psf
->codec_data
)
317 pvox
= (IMA_OKI_ADPCM
*) psf
->codec_data
;
320 bufferlen
= ARRAY_LEN (psf
->u
.sbuf
) ;
322 { writecount
= (len
>= bufferlen
) ? bufferlen
: (int) len
;
323 for (k
= 0 ; k
< writecount
; k
++)
324 sptr
[k
] = ptr
[total
+ k
] >> 16 ;
325 count
= vox_write_block (psf
, pvox
, sptr
, writecount
) ;
328 if (count
!= writecount
)
336 vox_write_f (SF_PRIVATE
*psf
, const float *ptr
, sf_count_t len
)
337 { IMA_OKI_ADPCM
*pvox
;
339 int k
, bufferlen
, writecount
, count
;
340 sf_count_t total
= 0 ;
343 if (! psf
->codec_data
)
345 pvox
= (IMA_OKI_ADPCM
*) psf
->codec_data
;
347 normfact
= (psf
->norm_float
== SF_TRUE
) ? (1.0 * 0x7FFF) : 1.0 ;
350 bufferlen
= ARRAY_LEN (psf
->u
.sbuf
) ;
352 { writecount
= (len
>= bufferlen
) ? bufferlen
: (int) len
;
353 for (k
= 0 ; k
< writecount
; k
++)
354 sptr
[k
] = lrintf (normfact
* ptr
[total
+ k
]) ;
355 count
= vox_write_block (psf
, pvox
, sptr
, writecount
) ;
358 if (count
!= writecount
)
366 vox_write_d (SF_PRIVATE
*psf
, const double *ptr
, sf_count_t len
)
367 { IMA_OKI_ADPCM
*pvox
;
369 int k
, bufferlen
, writecount
, count
;
370 sf_count_t total
= 0 ;
373 if (! psf
->codec_data
)
375 pvox
= (IMA_OKI_ADPCM
*) psf
->codec_data
;
377 normfact
= (psf
->norm_double
== SF_TRUE
) ? (1.0 * 0x7FFF) : 1.0 ;
380 bufferlen
= ARRAY_LEN (psf
->u
.sbuf
) ;
382 { writecount
= (len
>= bufferlen
) ? bufferlen
: (int) len
;
383 for (k
= 0 ; k
< writecount
; k
++)
384 sptr
[k
] = lrint (normfact
* ptr
[total
+ k
]) ;
385 count
= vox_write_block (psf
, pvox
, sptr
, writecount
) ;
388 if (count
!= writecount
)