ee60a4bd4c548496f276229d6dc9935f5bc52c93
2 ** Copyright (C) 2003-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.
32 #define MAX_XI_SAMPLES 16
34 /*------------------------------------------------------------------------------
35 ** Private static functions and tyepdefs.
39 { /* Warning, this filename is NOT nul terminated. */
42 char sample_name
[22] ;
44 int loop_begin
, loop_end
;
47 /* Data for encoder and decoder. */
51 static int xi_close (SF_PRIVATE
*psf
) ;
52 static int xi_write_header (SF_PRIVATE
*psf
, int calc_length
) ;
53 static int xi_read_header (SF_PRIVATE
*psf
) ;
54 static int dpcm_init (SF_PRIVATE
*psf
) ;
57 static sf_count_t
dpcm_seek (SF_PRIVATE
*psf
, int mode
, sf_count_t offset
) ;
59 /*------------------------------------------------------------------------------
64 xi_open (SF_PRIVATE
*psf
)
66 int subformat
, error
= 0 ;
69 return SFE_XI_NO_PIPE
;
72 pxi
= psf
->codec_data
;
73 else if ((pxi
= calloc (1, sizeof (XI_PRIVATE
))) == NULL
)
74 return SFE_MALLOC_FAILED
;
76 psf
->codec_data
= pxi
;
78 if (psf
->file
.mode
== SFM_READ
|| (psf
->file
.mode
== SFM_RDWR
&& psf
->filelength
> 0))
79 { if ((error
= xi_read_header (psf
)))
83 subformat
= SF_CODEC (psf
->sf
.format
) ;
85 if (psf
->file
.mode
== SFM_WRITE
|| psf
->file
.mode
== SFM_RDWR
)
86 { if ((SF_CONTAINER (psf
->sf
.format
)) != SF_FORMAT_XI
)
87 return SFE_BAD_OPEN_FORMAT
;
89 psf
->endian
= SF_ENDIAN_LITTLE
;
90 psf
->sf
.channels
= 1 ; /* Always mono */
91 psf
->sf
.samplerate
= 44100 ; /* Always */
93 /* Set up default instrument and software name. */
94 memcpy (pxi
->filename
, "Default Name ", sizeof (pxi
->filename
)) ;
95 memcpy (pxi
->software
, PACKAGE
"-" VERSION
" ", sizeof (pxi
->software
)) ;
97 memset (pxi
->sample_name
, 0, sizeof (pxi
->sample_name
)) ;
98 snprintf (pxi
->sample_name
, sizeof (pxi
->sample_name
), "%s", "Sample #1") ;
100 pxi
->sample_flags
= (subformat
== SF_FORMAT_DPCM_16
) ? 16 : 0 ;
102 if (xi_write_header (psf
, SF_FALSE
))
105 psf
->write_header
= xi_write_header
;
108 psf
->container_close
= xi_close
;
109 psf
->seek
= dpcm_seek
;
111 psf
->sf
.seekable
= SF_FALSE
;
113 psf
->blockwidth
= psf
->bytewidth
* psf
->sf
.channels
;
116 { case SF_FORMAT_DPCM_8
: /* 8-bit differential PCM. */
117 case SF_FORMAT_DPCM_16
: /* 16-bit differential PCM. */
118 error
= dpcm_init (psf
) ;
127 /*------------------------------------------------------------------------------
131 xi_close (SF_PRIVATE
* UNUSED (psf
))
136 /*==============================================================================
139 static sf_count_t
dpcm_read_dsc2s (SF_PRIVATE
*psf
, short *ptr
, sf_count_t len
) ;
140 static sf_count_t
dpcm_read_dsc2i (SF_PRIVATE
*psf
, int *ptr
, sf_count_t len
) ;
141 static sf_count_t
dpcm_read_dsc2f (SF_PRIVATE
*psf
, float *ptr
, sf_count_t len
) ;
142 static sf_count_t
dpcm_read_dsc2d (SF_PRIVATE
*psf
, double *ptr
, sf_count_t len
) ;
144 static sf_count_t
dpcm_write_s2dsc (SF_PRIVATE
*psf
, const short *ptr
, sf_count_t len
) ;
145 static sf_count_t
dpcm_write_i2dsc (SF_PRIVATE
*psf
, const int *ptr
, sf_count_t len
) ;
146 static sf_count_t
dpcm_write_f2dsc (SF_PRIVATE
*psf
, const float *ptr
, sf_count_t len
) ;
147 static sf_count_t
dpcm_write_d2dsc (SF_PRIVATE
*psf
, const double *ptr
, sf_count_t len
) ;
149 static sf_count_t
dpcm_read_dles2s (SF_PRIVATE
*psf
, short *ptr
, sf_count_t len
) ;
150 static sf_count_t
dpcm_read_dles2i (SF_PRIVATE
*psf
, int *ptr
, sf_count_t len
) ;
151 static sf_count_t
dpcm_read_dles2f (SF_PRIVATE
*psf
, float *ptr
, sf_count_t len
) ;
152 static sf_count_t
dpcm_read_dles2d (SF_PRIVATE
*psf
, double *ptr
, sf_count_t len
) ;
154 static sf_count_t
dpcm_write_s2dles (SF_PRIVATE
*psf
, const short *ptr
, sf_count_t len
) ;
155 static sf_count_t
dpcm_write_i2dles (SF_PRIVATE
*psf
, const int *ptr
, sf_count_t len
) ;
156 static sf_count_t
dpcm_write_f2dles (SF_PRIVATE
*psf
, const float *ptr
, sf_count_t len
) ;
157 static sf_count_t
dpcm_write_d2dles (SF_PRIVATE
*psf
, const double *ptr
, sf_count_t len
) ;
160 dpcm_init (SF_PRIVATE
*psf
)
161 { if (psf
->bytewidth
== 0 || psf
->sf
.channels
== 0)
162 return SFE_INTERNAL
;
164 psf
->blockwidth
= psf
->bytewidth
* psf
->sf
.channels
;
166 if (psf
->file
.mode
== SFM_READ
|| psf
->file
.mode
== SFM_RDWR
)
167 { switch (psf
->bytewidth
)
169 psf
->read_short
= dpcm_read_dsc2s
;
170 psf
->read_int
= dpcm_read_dsc2i
;
171 psf
->read_float
= dpcm_read_dsc2f
;
172 psf
->read_double
= dpcm_read_dsc2d
;
175 psf
->read_short
= dpcm_read_dles2s
;
176 psf
->read_int
= dpcm_read_dles2i
;
177 psf
->read_float
= dpcm_read_dles2f
;
178 psf
->read_double
= dpcm_read_dles2d
;
181 psf_log_printf (psf
, "dpcm_init() returning SFE_UNIMPLEMENTED\n") ;
182 return SFE_UNIMPLEMENTED
;
186 if (psf
->file
.mode
== SFM_WRITE
|| psf
->file
.mode
== SFM_RDWR
)
187 { switch (psf
->bytewidth
)
189 psf
->write_short
= dpcm_write_s2dsc
;
190 psf
->write_int
= dpcm_write_i2dsc
;
191 psf
->write_float
= dpcm_write_f2dsc
;
192 psf
->write_double
= dpcm_write_d2dsc
;
195 psf
->write_short
= dpcm_write_s2dles
;
196 psf
->write_int
= dpcm_write_i2dles
;
197 psf
->write_float
= dpcm_write_f2dles
;
198 psf
->write_double
= dpcm_write_d2dles
;
201 psf_log_printf (psf
, "dpcm_init() returning SFE_UNIMPLEMENTED\n") ;
202 return SFE_UNIMPLEMENTED
;
206 psf
->filelength
= psf_get_filelen (psf
) ;
207 psf
->datalength
= (psf
->dataend
) ? psf
->dataend
- psf
->dataoffset
:
208 psf
->filelength
- psf
->dataoffset
;
209 psf
->sf
.frames
= psf
->datalength
/ psf
->blockwidth
;
214 /*==============================================================================
218 dpcm_seek (SF_PRIVATE
*psf
, int mode
, sf_count_t offset
)
220 int total
, bufferlen
, len
;
222 if ((pxi
= psf
->codec_data
) == NULL
)
223 return SFE_INTERNAL
;
225 if (psf
->datalength
< 0 || psf
->dataoffset
< 0)
226 { psf
->error
= SFE_BAD_SEEK
;
227 return PSF_SEEK_ERROR
;
231 { psf_fseek (psf
, psf
->dataoffset
, SEEK_SET
) ;
236 if (offset
< 0 || offset
> psf
->sf
.frames
)
237 { psf
->error
= SFE_BAD_SEEK
;
238 return PSF_SEEK_ERROR
;
241 if (mode
!= SFM_READ
)
242 { /* What to do about write??? */
243 psf
->error
= SFE_BAD_SEEK
;
244 return PSF_SEEK_ERROR
;
247 psf_fseek (psf
, psf
->dataoffset
, SEEK_SET
) ;
249 if ((SF_CODEC (psf
->sf
.format
)) == SF_FORMAT_DPCM_16
)
251 bufferlen
= ARRAY_LEN (psf
->u
.sbuf
) ;
253 { len
= (total
> bufferlen
) ? bufferlen
: total
;
254 total
-= dpcm_read_dles2s (psf
, psf
->u
.sbuf
, len
) ;
259 bufferlen
= ARRAY_LEN (psf
->u
.sbuf
) ;
261 { len
= (total
> bufferlen
) ? bufferlen
: total
;
262 total
-= dpcm_read_dsc2s (psf
, psf
->u
.sbuf
, len
) ;
271 xi_write_header (SF_PRIVATE
*psf
, int UNUSED (calc_length
))
276 if ((pxi
= psf
->codec_data
) == NULL
)
277 return SFE_INTERNAL
;
279 current
= psf_ftell (psf
) ;
281 /* Reset the current header length to zero. */
282 psf
->header
[0] = 0 ;
284 psf_fseek (psf
, 0, SEEK_SET
) ;
286 string
= "Extended Instrument: " ;
287 psf_binheader_writef (psf
, "b", string
, strlen (string
)) ;
288 psf_binheader_writef (psf
, "b1", pxi
->filename
, sizeof (pxi
->filename
), 0x1A) ;
290 /* Write software version and two byte XI version. */
291 psf_binheader_writef (psf
, "eb2", pxi
->software
, sizeof (pxi
->software
), (1 << 8) + 2) ;
294 ** Jump note numbers (96), volume envelope (48), pan envelope (48),
295 ** volume points (1), pan points (1)
297 psf_binheader_writef (psf
, "z", (size_t) (96 + 48 + 48 + 1 + 1)) ;
299 /* Jump volume loop (3 bytes), pan loop (3), envelope flags (3), vibrato (3)
300 ** fade out (2), 22 unknown bytes, and then write sample_count (2 bytes).
302 psf_binheader_writef (psf
, "ez2z2", (size_t) (4 * 3), 0x1234, make_size_t (22), 1) ;
304 pxi
->loop_begin
= 0 ;
307 psf_binheader_writef (psf
, "et844", psf
->sf
.frames
, pxi
->loop_begin
, pxi
->loop_end
) ;
309 /* volume, fine tune, flags, pan, note, namelen */
310 psf_binheader_writef (psf
, "111111", 128, 0, pxi
->sample_flags
, 128, 0, strlen (pxi
->sample_name
)) ;
312 psf_binheader_writef (psf
, "b", pxi
->sample_name
, sizeof (pxi
->sample_name
)) ;
318 /* Header construction complete so write it out. */
319 psf_fwrite (psf
->header
, psf
->headindex
, 1, psf
) ;
324 psf
->dataoffset
= psf
->headindex
;
327 psf_fseek (psf
, current
, SEEK_SET
) ;
330 } /* xi_write_header */
333 xi_read_header (SF_PRIVATE
*psf
)
334 { char buffer
[64], name
[32] ;
335 short version
, fade_out
, sample_count
;
336 int k
, loop_begin
, loop_end
;
337 int sample_sizes
[MAX_XI_SAMPLES
] ;
339 psf_binheader_readf (psf
, "pb", 0, buffer
, 21) ;
341 memset (sample_sizes
, 0, sizeof (sample_sizes
)) ;
344 if (strcmp (buffer
, "Extended Instrument:") != 0)
345 return SFE_XI_BAD_HEADER
;
347 memset (buffer
, 0, sizeof (buffer
)) ;
348 psf_binheader_readf (psf
, "b", buffer
, 23) ;
350 if (buffer
[22] != 0x1A)
351 return SFE_XI_BAD_HEADER
;
354 psf_log_printf (psf
, "Extended Instrument : %s\n", buffer
) ;
356 psf_binheader_readf (psf
, "be2", buffer
, 20, &version
) ;
358 psf_log_printf (psf
, "Software : %s\nVersion : %d.%02d\n", buffer
, version
/ 256, version
% 256) ;
360 /* Jump note numbers (96), volume envelope (48), pan envelope (48),
361 ** volume points (1), pan points (1)
363 psf_binheader_readf (psf
, "j", 96 + 48 + 48 + 1 + 1) ;
365 psf_binheader_readf (psf
, "b", buffer
, 12) ;
366 psf_log_printf (psf
, "Volume Loop\n sustain : %u\n begin : %u\n end : %u\n",
367 buffer
[0], buffer
[1], buffer
[2]) ;
368 psf_log_printf (psf
, "Pan Loop\n sustain : %u\n begin : %u\n end : %u\n",
369 buffer
[3], buffer
[4], buffer
[5]) ;
370 psf_log_printf (psf
, "Envelope Flags\n volume : 0x%X\n pan : 0x%X\n",
371 buffer
[6] & 0xFF, buffer
[7] & 0xFF) ;
373 psf_log_printf (psf
, "Vibrato\n type : %u\n sweep : %u\n depth : %u\n rate : %u\n",
374 buffer
[8], buffer
[9], buffer
[10], buffer
[11]) ;
377 ** Read fade_out then jump reserved (2 bytes) and ???? (20 bytes) and
380 psf_binheader_readf (psf
, "e2j2", &fade_out
, 2 + 20, &sample_count
) ;
381 psf_log_printf (psf
, "Fade out : %d\n", fade_out
) ;
383 /* XI file can contain up to 16 samples. */
384 if (sample_count
> MAX_XI_SAMPLES
)
385 return SFE_XI_EXCESS_SAMPLES
;
387 if (psf
->instrument
== NULL
&& (psf
->instrument
= psf_instrument_alloc ()) == NULL
)
388 return SFE_MALLOC_FAILED
;
390 /* Log all data for each sample. */
391 for (k
= 0 ; k
< sample_count
; k
++)
392 { psf_binheader_readf (psf
, "e444", &(sample_sizes
[k
]), &loop_begin
, &loop_end
) ;
394 /* Read 5 know bytes, 1 unknown byte and 22 name bytes. */
395 psf_binheader_readf (psf
, "bb", buffer
, 6, name
, 22) ;
398 psf_log_printf (psf
, "Sample #%d\n name : %s\n", k
+ 1, name
) ;
400 psf_log_printf (psf
, " size : %d\n", sample_sizes
[k
]) ;
404 psf_log_printf (psf
, " loop\n begin : %d\n end : %d\n", loop_begin
, loop_end
) ;
406 psf_log_printf (psf
, " volume : %u\n f. tune : %d\n flags : 0x%02X ",
407 buffer
[0] & 0xFF, buffer
[1] & 0xFF, buffer
[2] & 0xFF) ;
409 psf_log_printf (psf
, " (") ;
411 psf_log_printf (psf
, " Loop") ;
413 psf_log_printf (psf
, " PingPong") ;
414 psf_log_printf (psf
, (buffer
[2] & 16) ? " 16bit" : " 8bit") ;
415 psf_log_printf (psf
, " )\n") ;
417 psf_log_printf (psf
, " pan : %u\n note : %d\n namelen : %d\n",
418 buffer
[3] & 0xFF, buffer
[4], buffer
[5]) ;
424 { psf
->sf
.format
= SF_FORMAT_XI
| SF_FORMAT_DPCM_16
;
428 { psf
->sf
.format
= SF_FORMAT_XI
| SF_FORMAT_DPCM_8
;
433 while (sample_count
> 1 && sample_sizes
[sample_count
- 1] == 0)
436 /* Currently, we can only handle 1 sample per file. */
438 if (sample_count
> 2)
439 { psf_log_printf (psf
, "*** Sample count is less than 16 but more than 1.\n") ;
440 psf_log_printf (psf
, " sample count : %d sample_sizes [%d] : %d\n",
441 sample_count
, sample_count
- 1, sample_sizes
[sample_count
- 1]) ;
442 return SFE_XI_EXCESS_SAMPLES
;
445 psf
->datalength
= sample_sizes
[0] ;
447 psf
->dataoffset
= psf_ftell (psf
) ;
448 if (psf
->dataoffset
< 0)
449 { psf_log_printf (psf
, "*** Bad Data Offset : %D\n", psf
->dataoffset
) ;
450 return SFE_BAD_OFFSET
;
452 psf_log_printf (psf
, "Data Offset : %D\n", psf
->dataoffset
) ;
454 if (psf
->dataoffset
+ psf
->datalength
> psf
->filelength
)
455 { psf_log_printf (psf
, "*** File seems to be truncated. Should be at least %D bytes long.\n",
456 psf
->dataoffset
+ sample_sizes
[0]) ;
457 psf
->datalength
= psf
->filelength
- psf
->dataoffset
;
460 if (psf_fseek (psf
, psf
->dataoffset
, SEEK_SET
) != psf
->dataoffset
)
461 return SFE_BAD_SEEK
;
463 psf
->endian
= SF_ENDIAN_LITTLE
;
464 psf
->sf
.channels
= 1 ; /* Always mono */
465 psf
->sf
.samplerate
= 44100 ; /* Always */
467 psf
->blockwidth
= psf
->sf
.channels
* psf
->bytewidth
;
469 if (! psf
->sf
.frames
&& psf
->blockwidth
)
470 psf
->sf
.frames
= (psf
->filelength
- psf
->dataoffset
) / psf
->blockwidth
;
472 psf
->instrument
->basenote
= 0 ;
473 psf
->instrument
->gain
= 1 ;
474 psf
->instrument
->velocity_lo
= psf
->instrument
->key_lo
= 0 ;
475 psf
->instrument
->velocity_hi
= psf
->instrument
->key_hi
= 127 ;
478 } /* xi_read_header */
480 /*==============================================================================
483 static void dsc2s_array (XI_PRIVATE
*pxi
, signed char *src
, int count
, short *dest
) ;
484 static void dsc2i_array (XI_PRIVATE
*pxi
, signed char *src
, int count
, int *dest
) ;
485 static void dsc2f_array (XI_PRIVATE
*pxi
, signed char *src
, int count
, float *dest
, float normfact
) ;
486 static void dsc2d_array (XI_PRIVATE
*pxi
, signed char *src
, int count
, double *dest
, double normfact
) ;
488 static void dles2s_array (XI_PRIVATE
*pxi
, short *src
, int count
, short *dest
) ;
489 static void dles2i_array (XI_PRIVATE
*pxi
, short *src
, int count
, int *dest
) ;
490 static void dles2f_array (XI_PRIVATE
*pxi
, short *src
, int count
, float *dest
, float normfact
) ;
491 static void dles2d_array (XI_PRIVATE
*pxi
, short *src
, int count
, double *dest
, double normfact
) ;
494 dpcm_read_dsc2s (SF_PRIVATE
*psf
, short *ptr
, sf_count_t len
)
496 int bufferlen
, readcount
;
497 sf_count_t total
= 0 ;
499 if ((pxi
= psf
->codec_data
) == NULL
)
502 bufferlen
= ARRAY_LEN (psf
->u
.ucbuf
) ;
505 { if (len
< bufferlen
)
506 bufferlen
= (int) len
;
507 readcount
= psf_fread (psf
->u
.scbuf
, sizeof (signed char), bufferlen
, psf
) ;
508 dsc2s_array (pxi
, psf
->u
.scbuf
, readcount
, ptr
+ total
) ;
510 if (readcount
< bufferlen
)
516 } /* dpcm_read_dsc2s */
519 dpcm_read_dsc2i (SF_PRIVATE
*psf
, int *ptr
, sf_count_t len
)
521 int bufferlen
, readcount
;
522 sf_count_t total
= 0 ;
524 if ((pxi
= psf
->codec_data
) == NULL
)
527 bufferlen
= ARRAY_LEN (psf
->u
.ucbuf
) ;
530 { if (len
< bufferlen
)
531 bufferlen
= (int) len
;
532 readcount
= psf_fread (psf
->u
.scbuf
, sizeof (signed char), bufferlen
, psf
) ;
533 dsc2i_array (pxi
, psf
->u
.scbuf
, readcount
, ptr
+ total
) ;
535 if (readcount
< bufferlen
)
541 } /* dpcm_read_dsc2i */
544 dpcm_read_dsc2f (SF_PRIVATE
*psf
, float *ptr
, sf_count_t len
)
546 int bufferlen
, readcount
;
547 sf_count_t total
= 0 ;
550 if ((pxi
= psf
->codec_data
) == NULL
)
553 normfact
= (psf
->norm_float
== SF_TRUE
) ? 1.0 / ((float) 0x80) : 1.0 ;
555 bufferlen
= ARRAY_LEN (psf
->u
.ucbuf
) ;
558 { if (len
< bufferlen
)
559 bufferlen
= (int) len
;
560 readcount
= psf_fread (psf
->u
.scbuf
, sizeof (signed char), bufferlen
, psf
) ;
561 dsc2f_array (pxi
, psf
->u
.scbuf
, readcount
, ptr
+ total
, normfact
) ;
563 if (readcount
< bufferlen
)
569 } /* dpcm_read_dsc2f */
572 dpcm_read_dsc2d (SF_PRIVATE
*psf
, double *ptr
, sf_count_t len
)
574 int bufferlen
, readcount
;
575 sf_count_t total
= 0 ;
578 if ((pxi
= psf
->codec_data
) == NULL
)
581 normfact
= (psf
->norm_double
== SF_TRUE
) ? 1.0 / ((double) 0x80) : 1.0 ;
583 bufferlen
= ARRAY_LEN (psf
->u
.ucbuf
) ;
586 { if (len
< bufferlen
)
587 bufferlen
= (int) len
;
588 readcount
= psf_fread (psf
->u
.scbuf
, sizeof (signed char), bufferlen
, psf
) ;
589 dsc2d_array (pxi
, psf
->u
.scbuf
, readcount
, ptr
+ total
, normfact
) ;
591 if (readcount
< bufferlen
)
597 } /* dpcm_read_dsc2d */
599 /*------------------------------------------------------------------------------
603 dpcm_read_dles2s (SF_PRIVATE
*psf
, short *ptr
, sf_count_t len
)
605 int bufferlen
, readcount
;
606 sf_count_t total
= 0 ;
608 if ((pxi
= psf
->codec_data
) == NULL
)
611 bufferlen
= ARRAY_LEN (psf
->u
.sbuf
) ;
614 { if (len
< bufferlen
)
615 bufferlen
= (int) len
;
616 readcount
= psf_fread (psf
->u
.sbuf
, sizeof (short), bufferlen
, psf
) ;
617 dles2s_array (pxi
, psf
->u
.sbuf
, readcount
, ptr
+ total
) ;
619 if (readcount
< bufferlen
)
625 } /* dpcm_read_dles2s */
628 dpcm_read_dles2i (SF_PRIVATE
*psf
, int *ptr
, sf_count_t len
)
630 int bufferlen
, readcount
;
631 sf_count_t total
= 0 ;
633 if ((pxi
= psf
->codec_data
) == NULL
)
636 bufferlen
= ARRAY_LEN (psf
->u
.sbuf
) ;
639 { if (len
< bufferlen
)
640 bufferlen
= (int) len
;
641 readcount
= psf_fread (psf
->u
.sbuf
, sizeof (short), bufferlen
, psf
) ;
642 dles2i_array (pxi
, psf
->u
.sbuf
, readcount
, ptr
+ total
) ;
644 if (readcount
< bufferlen
)
650 } /* dpcm_read_dles2i */
653 dpcm_read_dles2f (SF_PRIVATE
*psf
, float *ptr
, sf_count_t len
)
655 int bufferlen
, readcount
;
656 sf_count_t total
= 0 ;
659 if ((pxi
= psf
->codec_data
) == NULL
)
662 normfact
= (psf
->norm_float
== SF_TRUE
) ? 1.0 / ((float) 0x8000) : 1.0 ;
664 bufferlen
= ARRAY_LEN (psf
->u
.sbuf
) ;
667 { if (len
< bufferlen
)
668 bufferlen
= (int) len
;
669 readcount
= psf_fread (psf
->u
.sbuf
, sizeof (short), bufferlen
, psf
) ;
670 dles2f_array (pxi
, psf
->u
.sbuf
, readcount
, ptr
+ total
, normfact
) ;
672 if (readcount
< bufferlen
)
678 } /* dpcm_read_dles2f */
681 dpcm_read_dles2d (SF_PRIVATE
*psf
, double *ptr
, sf_count_t len
)
683 int bufferlen
, readcount
;
684 sf_count_t total
= 0 ;
687 if ((pxi
= psf
->codec_data
) == NULL
)
690 normfact
= (psf
->norm_double
== SF_TRUE
) ? 1.0 / ((double) 0x8000) : 1.0 ;
692 bufferlen
= ARRAY_LEN (psf
->u
.sbuf
) ;
695 { if (len
< bufferlen
)
696 bufferlen
= (int) len
;
697 readcount
= psf_fread (psf
->u
.sbuf
, sizeof (short), bufferlen
, psf
) ;
698 dles2d_array (pxi
, psf
->u
.sbuf
, readcount
, ptr
+ total
, normfact
) ;
700 if (readcount
< bufferlen
)
706 } /* dpcm_read_dles2d */
708 /*==============================================================================
711 static void s2dsc_array (XI_PRIVATE
*pxi
, const short *src
, signed char *dest
, int count
) ;
712 static void i2dsc_array (XI_PRIVATE
*pxi
, const int *src
, signed char *dest
, int count
) ;
713 static void f2dsc_array (XI_PRIVATE
*pxi
, const float *src
, signed char *dest
, int count
, float normfact
) ;
714 static void d2dsc_array (XI_PRIVATE
*pxi
, const double *src
, signed char *dest
, int count
, double normfact
) ;
716 static void s2dles_array (XI_PRIVATE
*pxi
, const short *src
, short *dest
, int count
) ;
717 static void i2dles_array (XI_PRIVATE
*pxi
, const int *src
, short *dest
, int count
) ;
718 static void f2dles_array (XI_PRIVATE
*pxi
, const float *src
, short *dest
, int count
, float normfact
) ;
719 static void d2dles_array (XI_PRIVATE
*pxi
, const double *src
, short *dest
, int count
, double normfact
) ;
723 dpcm_write_s2dsc (SF_PRIVATE
*psf
, const short *ptr
, sf_count_t len
)
725 int bufferlen
, writecount
;
726 sf_count_t total
= 0 ;
728 if ((pxi
= psf
->codec_data
) == NULL
)
731 bufferlen
= ARRAY_LEN (psf
->u
.ucbuf
) ;
734 { if (len
< bufferlen
)
735 bufferlen
= (int) len
;
736 s2dsc_array (pxi
, ptr
+ total
, psf
->u
.scbuf
, bufferlen
) ;
737 writecount
= psf_fwrite (psf
->u
.scbuf
, sizeof (signed char), bufferlen
, psf
) ;
738 total
+= writecount
;
739 if (writecount
< bufferlen
)
745 } /* dpcm_write_s2dsc */
748 dpcm_write_i2dsc (SF_PRIVATE
*psf
, const int *ptr
, sf_count_t len
)
750 int bufferlen
, writecount
;
751 sf_count_t total
= 0 ;
753 if ((pxi
= psf
->codec_data
) == NULL
)
756 bufferlen
= ARRAY_LEN (psf
->u
.ucbuf
) ;
759 { if (len
< bufferlen
)
760 bufferlen
= (int) len
;
761 i2dsc_array (pxi
, ptr
+ total
, psf
->u
.scbuf
, bufferlen
) ;
762 writecount
= psf_fwrite (psf
->u
.scbuf
, sizeof (signed char), bufferlen
, psf
) ;
763 total
+= writecount
;
764 if (writecount
< bufferlen
)
770 } /* dpcm_write_i2dsc */
773 dpcm_write_f2dsc (SF_PRIVATE
*psf
, const float *ptr
, sf_count_t len
)
775 int bufferlen
, writecount
;
776 sf_count_t total
= 0 ;
779 if ((pxi
= psf
->codec_data
) == NULL
)
782 normfact
= (psf
->norm_float
== SF_TRUE
) ? (1.0 * 0x7F) : 1.0 ;
784 bufferlen
= ARRAY_LEN (psf
->u
.ucbuf
) ;
787 { if (len
< bufferlen
)
788 bufferlen
= (int) len
;
789 f2dsc_array (pxi
, ptr
+ total
, psf
->u
.scbuf
, bufferlen
, normfact
) ;
790 writecount
= psf_fwrite (psf
->u
.scbuf
, sizeof (signed char), bufferlen
, psf
) ;
791 total
+= writecount
;
792 if (writecount
< bufferlen
)
798 } /* dpcm_write_f2dsc */
801 dpcm_write_d2dsc (SF_PRIVATE
*psf
, const double *ptr
, sf_count_t len
)
803 int bufferlen
, writecount
;
804 sf_count_t total
= 0 ;
807 if ((pxi
= psf
->codec_data
) == NULL
)
810 normfact
= (psf
->norm_double
== SF_TRUE
) ? (1.0 * 0x7F) : 1.0 ;
812 bufferlen
= ARRAY_LEN (psf
->u
.ucbuf
) ;
815 { if (len
< bufferlen
)
816 bufferlen
= (int) len
;
817 d2dsc_array (pxi
, ptr
+ total
, psf
->u
.scbuf
, bufferlen
, normfact
) ;
818 writecount
= psf_fwrite (psf
->u
.scbuf
, sizeof (signed char), bufferlen
, psf
) ;
819 total
+= writecount
;
820 if (writecount
< bufferlen
)
826 } /* dpcm_write_d2dsc */
830 dpcm_write_s2dles (SF_PRIVATE
*psf
, const short *ptr
, sf_count_t len
)
832 int bufferlen
, writecount
;
833 sf_count_t total
= 0 ;
835 if ((pxi
= psf
->codec_data
) == NULL
)
838 bufferlen
= ARRAY_LEN (psf
->u
.sbuf
) ;
841 { if (len
< bufferlen
)
842 bufferlen
= (int) len
;
843 s2dles_array (pxi
, ptr
+ total
, psf
->u
.sbuf
, bufferlen
) ;
844 writecount
= psf_fwrite (psf
->u
.sbuf
, sizeof (short), bufferlen
, psf
) ;
845 total
+= writecount
;
846 if (writecount
< bufferlen
)
852 } /* dpcm_write_s2dles */
855 dpcm_write_i2dles (SF_PRIVATE
*psf
, const int *ptr
, sf_count_t len
)
857 int bufferlen
, writecount
;
858 sf_count_t total
= 0 ;
860 if ((pxi
= psf
->codec_data
) == NULL
)
863 bufferlen
= ARRAY_LEN (psf
->u
.sbuf
) ;
866 { if (len
< bufferlen
)
867 bufferlen
= (int) len
;
868 i2dles_array (pxi
, ptr
+ total
, psf
->u
.sbuf
, bufferlen
) ;
869 writecount
= psf_fwrite (psf
->u
.sbuf
, sizeof (short), bufferlen
, psf
) ;
870 total
+= writecount
;
871 if (writecount
< bufferlen
)
877 } /* dpcm_write_i2dles */
880 dpcm_write_f2dles (SF_PRIVATE
*psf
, const float *ptr
, sf_count_t len
)
882 int bufferlen
, writecount
;
883 sf_count_t total
= 0 ;
886 if ((pxi
= psf
->codec_data
) == NULL
)
889 normfact
= (psf
->norm_float
== SF_TRUE
) ? (1.0 * 0x7FFF) : 1.0 ;
891 bufferlen
= ARRAY_LEN (psf
->u
.sbuf
) ;
894 { if (len
< bufferlen
)
895 bufferlen
= (int) len
;
896 f2dles_array (pxi
, ptr
+ total
, psf
->u
.sbuf
, bufferlen
, normfact
) ;
897 writecount
= psf_fwrite (psf
->u
.sbuf
, sizeof (short), bufferlen
, psf
) ;
898 total
+= writecount
;
899 if (writecount
< bufferlen
)
905 } /* dpcm_write_f2dles */
908 dpcm_write_d2dles (SF_PRIVATE
*psf
, const double *ptr
, sf_count_t len
)
910 int bufferlen
, writecount
;
911 sf_count_t total
= 0 ;
914 if ((pxi
= psf
->codec_data
) == NULL
)
917 normfact
= (psf
->norm_double
== SF_TRUE
) ? (1.0 * 0x7FFF) : 1.0 ;
919 bufferlen
= ARRAY_LEN (psf
->u
.sbuf
) ;
922 { if (len
< bufferlen
)
923 bufferlen
= (int) len
;
924 d2dles_array (pxi
, ptr
+ total
, psf
->u
.sbuf
, bufferlen
, normfact
) ;
925 writecount
= psf_fwrite (psf
->u
.sbuf
, sizeof (short), bufferlen
, psf
) ;
926 total
+= writecount
;
927 if (writecount
< bufferlen
)
933 } /* dpcm_write_d2dles */
936 /*==============================================================================
940 dsc2s_array (XI_PRIVATE
*pxi
, signed char *src
, int count
, short *dest
)
941 { signed char last_val
;
944 last_val
= pxi
->last_16
>> 8 ;
946 for (k
= 0 ; k
< count
; k
++)
947 { last_val
+= src
[k
] ;
948 dest
[k
] = last_val
<< 8 ;
951 pxi
->last_16
= last_val
<< 8 ;
955 dsc2i_array (XI_PRIVATE
*pxi
, signed char *src
, int count
, int *dest
)
956 { signed char last_val
;
959 last_val
= pxi
->last_16
>> 8 ;
961 for (k
= 0 ; k
< count
; k
++)
962 { last_val
+= src
[k
] ;
963 dest
[k
] = last_val
<< 24 ;
966 pxi
->last_16
= last_val
<< 8 ;
970 dsc2f_array (XI_PRIVATE
*pxi
, signed char *src
, int count
, float *dest
, float normfact
)
971 { signed char last_val
;
974 last_val
= pxi
->last_16
>> 8 ;
976 for (k
= 0 ; k
< count
; k
++)
977 { last_val
+= src
[k
] ;
978 dest
[k
] = last_val
* normfact
;
981 pxi
->last_16
= last_val
<< 8 ;
985 dsc2d_array (XI_PRIVATE
*pxi
, signed char *src
, int count
, double *dest
, double normfact
)
986 { signed char last_val
;
989 last_val
= pxi
->last_16
>> 8 ;
991 for (k
= 0 ; k
< count
; k
++)
992 { last_val
+= src
[k
] ;
993 dest
[k
] = last_val
* normfact
;
996 pxi
->last_16
= last_val
<< 8 ;
999 /*------------------------------------------------------------------------------
1003 s2dsc_array (XI_PRIVATE
*pxi
, const short *src
, signed char *dest
, int count
)
1004 { signed char last_val
, current
;
1007 last_val
= pxi
->last_16
>> 8 ;
1009 for (k
= 0 ; k
< count
; k
++)
1010 { current
= src
[k
] >> 8 ;
1011 dest
[k
] = current
- last_val
;
1012 last_val
= current
;
1015 pxi
->last_16
= last_val
<< 8 ;
1019 i2dsc_array (XI_PRIVATE
*pxi
, const int *src
, signed char *dest
, int count
)
1020 { signed char last_val
, current
;
1023 last_val
= pxi
->last_16
>> 8 ;
1025 for (k
= 0 ; k
< count
; k
++)
1026 { current
= src
[k
] >> 24 ;
1027 dest
[k
] = current
- last_val
;
1028 last_val
= current
;
1031 pxi
->last_16
= last_val
<< 8 ;
1035 f2dsc_array (XI_PRIVATE
*pxi
, const float *src
, signed char *dest
, int count
, float normfact
)
1036 { signed char last_val
, current
;
1039 last_val
= pxi
->last_16
>> 8 ;
1041 for (k
= 0 ; k
< count
; k
++)
1042 { current
= lrintf (src
[k
] * normfact
) ;
1043 dest
[k
] = current
- last_val
;
1044 last_val
= current
;
1047 pxi
->last_16
= last_val
<< 8 ;
1051 d2dsc_array (XI_PRIVATE
*pxi
, const double *src
, signed char *dest
, int count
, double normfact
)
1052 { signed char last_val
, current
;
1055 last_val
= pxi
->last_16
>> 8 ;
1057 for (k
= 0 ; k
< count
; k
++)
1058 { current
= lrint (src
[k
] * normfact
) ;
1059 dest
[k
] = current
- last_val
;
1060 last_val
= current
;
1063 pxi
->last_16
= last_val
<< 8 ;
1066 /*==============================================================================
1070 dles2s_array (XI_PRIVATE
*pxi
, short *src
, int count
, short *dest
)
1074 last_val
= pxi
->last_16
;
1076 for (k
= 0 ; k
< count
; k
++)
1077 { last_val
+= LES2H_SHORT (src
[k
]) ;
1078 dest
[k
] = last_val
;
1081 pxi
->last_16
= last_val
;
1082 } /* dles2s_array */
1085 dles2i_array (XI_PRIVATE
*pxi
, short *src
, int count
, int *dest
)
1089 last_val
= pxi
->last_16
;
1091 for (k
= 0 ; k
< count
; k
++)
1092 { last_val
+= LES2H_SHORT (src
[k
]) ;
1093 dest
[k
] = last_val
<< 16 ;
1096 pxi
->last_16
= last_val
;
1097 } /* dles2i_array */
1100 dles2f_array (XI_PRIVATE
*pxi
, short *src
, int count
, float *dest
, float normfact
)
1104 last_val
= pxi
->last_16
;
1106 for (k
= 0 ; k
< count
; k
++)
1107 { last_val
+= LES2H_SHORT (src
[k
]) ;
1108 dest
[k
] = last_val
* normfact
;
1111 pxi
->last_16
= last_val
;
1112 } /* dles2f_array */
1115 dles2d_array (XI_PRIVATE
*pxi
, short *src
, int count
, double *dest
, double normfact
)
1119 last_val
= pxi
->last_16
;
1121 for (k
= 0 ; k
< count
; k
++)
1122 { last_val
+= LES2H_SHORT (src
[k
]) ;
1123 dest
[k
] = last_val
* normfact
;
1126 pxi
->last_16
= last_val
;
1127 } /* dles2d_array */
1129 /*------------------------------------------------------------------------------
1133 s2dles_array (XI_PRIVATE
*pxi
, const short *src
, short *dest
, int count
)
1134 { short diff
, last_val
;
1137 last_val
= pxi
->last_16
;
1139 for (k
= 0 ; k
< count
; k
++)
1140 { diff
= src
[k
] - last_val
;
1141 dest
[k
] = LES2H_SHORT (diff
) ;
1142 last_val
= src
[k
] ;
1145 pxi
->last_16
= last_val
;
1146 } /* s2dles_array */
1149 i2dles_array (XI_PRIVATE
*pxi
, const int *src
, short *dest
, int count
)
1150 { short diff
, last_val
;
1153 last_val
= pxi
->last_16
;
1155 for (k
= 0 ; k
< count
; k
++)
1156 { diff
= (src
[k
] >> 16) - last_val
;
1157 dest
[k
] = LES2H_SHORT (diff
) ;
1158 last_val
= src
[k
] >> 16 ;
1161 pxi
->last_16
= last_val
;
1162 } /* i2dles_array */
1165 f2dles_array (XI_PRIVATE
*pxi
, const float *src
, short *dest
, int count
, float normfact
)
1166 { short diff
, last_val
, current
;
1169 last_val
= pxi
->last_16
;
1171 for (k
= 0 ; k
< count
; k
++)
1172 { current
= lrintf (src
[k
] * normfact
) ;
1173 diff
= current
- last_val
;
1174 dest
[k
] = LES2H_SHORT (diff
) ;
1175 last_val
= current
;
1178 pxi
->last_16
= last_val
;
1179 } /* f2dles_array */
1182 d2dles_array (XI_PRIVATE
*pxi
, const double *src
, short *dest
, int count
, double normfact
)
1183 { short diff
, last_val
, current
;
1186 last_val
= pxi
->last_16
;
1188 for (k
= 0 ; k
< count
; k
++)
1189 { current
= lrint (src
[k
] * normfact
) ;
1190 diff
= current
- last_val
;
1191 dest
[k
] = LES2H_SHORT (diff
) ;
1192 last_val
= current
;
1195 pxi
->last_16
= last_val
;
1196 } /* d2dles_array */