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.
32 /*------------------------------------------------------------------------------
35 #define SDS_DATA_OFFSET 0x15
36 #define SDS_BLOCK_SIZE 127
38 #define SDS_AUDIO_BYTES_PER_BLOCK 120
40 #define SDS_3BYTE_TO_INT_DECODE(x) (((x) & 0x7F) | (((x) & 0x7F00) >> 1) | (((x) & 0x7F0000) >> 2))
41 #define SDS_INT_TO_3BYTE_ENCODE(x) (((x) & 0x7F) | (((x) << 1) & 0x7F00) | (((x) << 2) & 0x7F0000))
43 /*------------------------------------------------------------------------------
47 typedef struct tag_SDS_PRIVATE
48 { int bitwidth
, frames
;
49 int samplesperblock
, total_blocks
;
51 int (*reader
) (SF_PRIVATE
*psf
, struct tag_SDS_PRIVATE
*psds
) ;
52 int (*writer
) (SF_PRIVATE
*psf
, struct tag_SDS_PRIVATE
*psds
) ;
54 int read_block
, read_count
;
55 unsigned char read_data
[SDS_BLOCK_SIZE
] ;
56 int read_samples
[SDS_BLOCK_SIZE
/ 2] ; /* Maximum samples per block */
58 int write_block
, write_count
;
60 unsigned char write_data
[SDS_BLOCK_SIZE
] ;
61 int write_samples
[SDS_BLOCK_SIZE
/ 2] ; /* Maximum samples per block */
64 /*------------------------------------------------------------------------------
65 ** Private static functions.
68 static int sds_close (SF_PRIVATE
*psf
) ;
70 static int sds_write_header (SF_PRIVATE
*psf
, int calc_length
) ;
71 static int sds_read_header (SF_PRIVATE
*psf
, SDS_PRIVATE
*psds
) ;
73 static int sds_init (SF_PRIVATE
*psf
, SDS_PRIVATE
*psds
) ;
75 static sf_count_t
sds_read_s (SF_PRIVATE
*psf
, short *ptr
, sf_count_t len
) ;
76 static sf_count_t
sds_read_i (SF_PRIVATE
*psf
, int *ptr
, sf_count_t len
) ;
77 static sf_count_t
sds_read_f (SF_PRIVATE
*psf
, float *ptr
, sf_count_t len
) ;
78 static sf_count_t
sds_read_d (SF_PRIVATE
*psf
, double *ptr
, sf_count_t len
) ;
80 static sf_count_t
sds_write_s (SF_PRIVATE
*psf
, const short *ptr
, sf_count_t len
) ;
81 static sf_count_t
sds_write_i (SF_PRIVATE
*psf
, const int *ptr
, sf_count_t len
) ;
82 static sf_count_t
sds_write_f (SF_PRIVATE
*psf
, const float *ptr
, sf_count_t len
) ;
83 static sf_count_t
sds_write_d (SF_PRIVATE
*psf
, const double *ptr
, sf_count_t len
) ;
85 static sf_count_t
sds_seek (SF_PRIVATE
*psf
, int mode
, sf_count_t offset
) ;
87 static int sds_2byte_read (SF_PRIVATE
*psf
, SDS_PRIVATE
*psds
) ;
88 static int sds_3byte_read (SF_PRIVATE
*psf
, SDS_PRIVATE
*psds
) ;
89 static int sds_4byte_read (SF_PRIVATE
*psf
, SDS_PRIVATE
*psds
) ;
91 static int sds_read (SF_PRIVATE
*psf
, SDS_PRIVATE
*psds
, int *iptr
, int readcount
) ;
93 static int sds_2byte_write (SF_PRIVATE
*psf
, SDS_PRIVATE
*psds
) ;
94 static int sds_3byte_write (SF_PRIVATE
*psf
, SDS_PRIVATE
*psds
) ;
95 static int sds_4byte_write (SF_PRIVATE
*psf
, SDS_PRIVATE
*psds
) ;
97 static int sds_write (SF_PRIVATE
*psf
, SDS_PRIVATE
*psds
, const int *iptr
, int writecount
) ;
99 /*------------------------------------------------------------------------------
104 sds_open (SF_PRIVATE
*psf
)
105 { SDS_PRIVATE
*psds
;
108 /* Hmmmm, need this here to pass update_header_test. */
111 if (! (psds
= calloc (1, sizeof (SDS_PRIVATE
))))
112 return SFE_MALLOC_FAILED
;
113 psf
->codec_data
= psds
;
115 if (psf
->file
.mode
== SFM_READ
|| (psf
->file
.mode
== SFM_RDWR
&& psf
->filelength
> 0))
116 { if ((error
= sds_read_header (psf
, psds
)))
120 if ((SF_CONTAINER (psf
->sf
.format
)) != SF_FORMAT_SDS
)
121 return SFE_BAD_OPEN_FORMAT
;
123 if (psf
->file
.mode
== SFM_WRITE
|| psf
->file
.mode
== SFM_RDWR
)
124 { if (sds_write_header (psf
, SF_FALSE
))
127 psf
->write_header
= sds_write_header
;
129 psf_fseek (psf
, SDS_DATA_OFFSET
, SEEK_SET
) ;
132 if ((error
= sds_init (psf
, psds
)) != 0)
135 psf
->seek
= sds_seek
;
136 psf
->container_close
= sds_close
;
138 psf
->blockwidth
= 0 ;
143 /*------------------------------------------------------------------------------
147 sds_close (SF_PRIVATE
*psf
)
149 if (psf
->file
.mode
== SFM_WRITE
|| psf
->file
.mode
== SFM_RDWR
)
150 { SDS_PRIVATE
*psds
;
152 if ((psds
= (SDS_PRIVATE
*) psf
->codec_data
) == NULL
)
153 { psf_log_printf (psf
, "*** Bad psf->codec_data ptr.\n") ;
154 return SFE_INTERNAL
;
157 if (psds
->write_count
> 0)
158 { memset (&(psds
->write_data
[psds
->write_count
]), 0, (psds
->samplesperblock
- psds
->write_count
) * sizeof (int)) ;
159 psds
->writer (psf
, psds
) ;
162 sds_write_header (psf
, SF_TRUE
) ;
169 sds_init (SF_PRIVATE
*psf
, SDS_PRIVATE
*psds
)
171 if (psds
->bitwidth
< 8 || psds
->bitwidth
> 28)
172 return (psf
->error
= SFE_SDS_BAD_BIT_WIDTH
) ;
174 if (psds
->bitwidth
< 14)
175 { psds
->reader
= sds_2byte_read
;
176 psds
->writer
= sds_2byte_write
;
177 psds
->samplesperblock
= SDS_AUDIO_BYTES_PER_BLOCK
/ 2 ;
179 else if (psds
->bitwidth
< 21)
180 { psds
->reader
= sds_3byte_read
;
181 psds
->writer
= sds_3byte_write
;
182 psds
->samplesperblock
= SDS_AUDIO_BYTES_PER_BLOCK
/ 3 ;
185 { psds
->reader
= sds_4byte_read
;
186 psds
->writer
= sds_4byte_write
;
187 psds
->samplesperblock
= SDS_AUDIO_BYTES_PER_BLOCK
/ 4 ;
190 if (psf
->file
.mode
== SFM_READ
|| psf
->file
.mode
== SFM_RDWR
)
191 { psf
->read_short
= sds_read_s
;
192 psf
->read_int
= sds_read_i
;
193 psf
->read_float
= sds_read_f
;
194 psf
->read_double
= sds_read_d
;
196 /* Read first block. */
197 psds
->reader (psf
, psds
) ;
200 if (psf
->file
.mode
== SFM_WRITE
|| psf
->file
.mode
== SFM_RDWR
)
201 { psf
->write_short
= sds_write_s
;
202 psf
->write_int
= sds_write_i
;
203 psf
->write_float
= sds_write_f
;
204 psf
->write_double
= sds_write_d
;
211 sds_read_header (SF_PRIVATE
*psf
, SDS_PRIVATE
*psds
)
212 { unsigned char channel
, bitwidth
, loop_type
, byte
;
213 unsigned short sample_no
, marker
;
214 unsigned int samp_period
, data_length
, sustain_loop_start
, sustain_loop_end
;
215 int bytesread
, blockcount
;
217 /* Set position to start of file to begin reading header. */
218 bytesread
= psf_binheader_readf (psf
, "pE211", 0, &marker
, &channel
, &byte
) ;
220 if (marker
!= 0xF07E || byte
!= 0x01)
221 return SFE_SDS_NOT_SDS
;
223 bytesread
+= psf_binheader_readf (psf
, "e2", &sample_no
) ;
224 sample_no
= SDS_3BYTE_TO_INT_DECODE (sample_no
) ;
226 psf_log_printf (psf
, "Midi Sample Dump Standard (.sds)\nF07E\n"
227 " Midi Channel : %d\n Sample Number : %d\n",
228 channel
, sample_no
) ;
230 bytesread
+= psf_binheader_readf (psf
, "e13", &bitwidth
, &samp_period
) ;
232 samp_period
= SDS_3BYTE_TO_INT_DECODE (samp_period
) ;
234 psds
->bitwidth
= bitwidth
;
236 if (psds
->bitwidth
> 1)
237 psf_log_printf (psf
, " Bit Width : %d\n", psds
->bitwidth
) ;
239 { psf_log_printf (psf
, " Bit Width : %d (should be > 1)\n", psds
->bitwidth
) ;
240 return SFE_SDS_BAD_BIT_WIDTH
;
244 { psf
->sf
.samplerate
= 1000000000 / samp_period
;
246 psf_log_printf (psf
, " Sample Period : %d\n"
247 " Sample Rate : %d\n",
248 samp_period
, psf
->sf
.samplerate
) ;
251 { psf
->sf
.samplerate
= 16000 ;
253 psf_log_printf (psf
, " Sample Period : %d (should be > 0)\n"
254 " Sample Rate : %d (guessed)\n",
255 samp_period
, psf
->sf
.samplerate
) ;
258 bytesread
+= psf_binheader_readf (psf
, "e3331", &data_length
, &sustain_loop_start
, &sustain_loop_end
, &loop_type
) ;
260 data_length
= SDS_3BYTE_TO_INT_DECODE (data_length
) ;
262 psf
->sf
.frames
= psds
->frames
= data_length
;
264 sustain_loop_start
= SDS_3BYTE_TO_INT_DECODE (sustain_loop_start
) ;
265 sustain_loop_end
= SDS_3BYTE_TO_INT_DECODE (sustain_loop_end
) ;
267 psf_log_printf (psf
, " Sustain Loop\n"
271 sustain_loop_start
, sustain_loop_end
, loop_type
) ;
273 psf
->dataoffset
= SDS_DATA_OFFSET
;
274 psf
->datalength
= psf
->filelength
- psf
->dataoffset
;
276 bytesread
+= psf_binheader_readf (psf
, "1", &byte
) ;
278 psf_log_printf (psf
, "bad end : %X\n", byte
& 0xFF) ;
280 for (blockcount
= 0 ; bytesread
< psf
->filelength
; blockcount
++)
282 bytesread
+= psf_fread (&marker
, 1, 2, psf
) ;
287 psf_fseek (psf
, SDS_BLOCK_SIZE
- 2, SEEK_CUR
) ;
288 bytesread
+= SDS_BLOCK_SIZE
- 2 ;
291 psf_log_printf (psf
, "\nBlocks : %d\n", blockcount
) ;
292 psds
->total_blocks
= blockcount
;
294 psds
->samplesperblock
= SDS_AUDIO_BYTES_PER_BLOCK
/ ((psds
->bitwidth
+ 6) / 7) ;
295 psf_log_printf (psf
, "Samples/Block : %d\n", psds
->samplesperblock
) ;
297 psf_log_printf (psf
, "Frames : %d\n", blockcount
* psds
->samplesperblock
) ;
300 psf
->sf
.channels
= 1 ;
301 psf
->sf
.sections
= 1 ;
304 ** Lie to the user about PCM bit width. Always round up to
305 ** the next multiple of 8.
307 switch ((psds
->bitwidth
+ 7) / 8)
309 psf
->sf
.format
= SF_FORMAT_SDS
| SF_FORMAT_PCM_S8
;
313 psf
->sf
.format
= SF_FORMAT_SDS
| SF_FORMAT_PCM_16
;
317 psf
->sf
.format
= SF_FORMAT_SDS
| SF_FORMAT_PCM_24
;
321 psf
->sf
.format
= SF_FORMAT_SDS
| SF_FORMAT_PCM_32
;
325 psf_log_printf (psf
, "*** Weird byte width (%d)\n", (psds
->bitwidth
+ 7) / 8) ;
326 return SFE_SDS_BAD_BIT_WIDTH
;
329 psf_fseek (psf
, SDS_DATA_OFFSET
, SEEK_SET
) ;
332 } /* sds_read_header */
335 sds_write_header (SF_PRIVATE
*psf
, int calc_length
)
336 { SDS_PRIVATE
*psds
;
338 int samp_period
, data_length
, sustain_loop_start
, sustain_loop_end
;
339 unsigned char loop_type
= 0 ;
341 if ((psds
= (SDS_PRIVATE
*) psf
->codec_data
) == NULL
)
342 { psf_log_printf (psf
, "*** Bad psf->codec_data ptr.\n") ;
343 return SFE_INTERNAL
;
346 if (psf
->pipeoffset
> 0)
349 current
= psf_ftell (psf
) ;
352 psf
->sf
.frames
= psds
->total_written
;
354 if (psds
->write_count
> 0)
355 { int current_count
= psds
->write_count
;
356 int current_block
= psds
->write_block
;
358 psds
->writer (psf
, psds
) ;
360 psf_fseek (psf
, -1 * SDS_BLOCK_SIZE
, SEEK_CUR
) ;
362 psds
->write_count
= current_count
;
363 psds
->write_block
= current_block
;
366 /* Reset the current header length to zero. */
367 psf
->header
[0] = 0 ;
370 if (psf
->is_pipe
== SF_FALSE
)
371 psf_fseek (psf
, 0, SEEK_SET
) ;
373 psf_binheader_writef (psf
, "E211", 0xF07E, 0, 1) ;
375 switch (SF_CODEC (psf
->sf
.format
))
376 { case SF_FORMAT_PCM_S8
:
379 case SF_FORMAT_PCM_16
:
380 psds
->bitwidth
= 16 ;
382 case SF_FORMAT_PCM_24
:
383 psds
->bitwidth
= 24 ;
386 return SFE_SDS_BAD_BIT_WIDTH
;
389 samp_period
= SDS_INT_TO_3BYTE_ENCODE (1000000000 / psf
->sf
.samplerate
) ;
391 psf_binheader_writef (psf
, "e213", 0, psds
->bitwidth
, samp_period
) ;
393 data_length
= SDS_INT_TO_3BYTE_ENCODE (psds
->total_written
) ;
394 sustain_loop_start
= SDS_INT_TO_3BYTE_ENCODE (0) ;
395 sustain_loop_end
= SDS_INT_TO_3BYTE_ENCODE (0) ;
397 psf_binheader_writef (psf
, "e33311", data_length
, sustain_loop_start
, sustain_loop_end
, loop_type
, 0xF7) ;
399 /* Header construction complete so write it out. */
400 psf_fwrite (psf
->header
, psf
->headindex
, 1, psf
) ;
405 psf
->dataoffset
= psf
->headindex
;
406 psf
->datalength
= psds
->write_block
* SDS_BLOCK_SIZE
;
409 psf_fseek (psf
, current
, SEEK_SET
) ;
412 } /* sds_write_header */
415 /*------------------------------------------------------------------------------
419 sds_2byte_read (SF_PRIVATE
*psf
, SDS_PRIVATE
*psds
)
420 { unsigned char *ucptr
, checksum
;
421 unsigned int sample
;
424 psds
->read_block
++ ;
425 psds
->read_count
= 0 ;
427 if (psds
->read_block
* psds
->samplesperblock
> psds
->frames
)
428 { memset (psds
->read_samples
, 0, psds
->samplesperblock
* sizeof (int)) ;
432 if ((k
= psf_fread (psds
->read_data
, 1, SDS_BLOCK_SIZE
, psf
)) != SDS_BLOCK_SIZE
)
433 psf_log_printf (psf
, "*** Warning : short read (%d != %d).\n", k
, SDS_BLOCK_SIZE
) ;
435 if (psds
->read_data
[0] != 0xF0)
436 { printf ("Error A : %02X\n", psds
->read_data
[0] & 0xFF) ;
439 checksum
= psds
->read_data
[1] ;
440 if (checksum
!= 0x7E)
441 { printf ("Error 1 : %02X\n", checksum
& 0xFF) ;
444 for (k
= 2 ; k
<= SDS_BLOCK_SIZE
- 3 ; k
++)
445 checksum
^= psds
->read_data
[k
] ;
449 if (checksum
!= psds
->read_data
[SDS_BLOCK_SIZE
- 2])
450 { psf_log_printf (psf
, "Block %d : checksum is %02X should be %02X\n", psds
->read_data
[4], checksum
, psds
->read_data
[SDS_BLOCK_SIZE
- 2]) ;
453 ucptr
= psds
->read_data
+ 5 ;
454 for (k
= 0 ; k
< 120 ; k
+= 2)
455 { sample
= (ucptr
[k
] << 25) + (ucptr
[k
+ 1] << 18) ;
456 psds
->read_samples
[k
/ 2] = (int) (sample
- 0x80000000) ;
460 } /* sds_2byte_read */
463 sds_3byte_read (SF_PRIVATE
*psf
, SDS_PRIVATE
*psds
)
464 { unsigned char *ucptr
, checksum
;
465 unsigned int sample
;
468 psds
->read_block
++ ;
469 psds
->read_count
= 0 ;
471 if (psds
->read_block
* psds
->samplesperblock
> psds
->frames
)
472 { memset (psds
->read_samples
, 0, psds
->samplesperblock
* sizeof (int)) ;
476 if ((k
= psf_fread (psds
->read_data
, 1, SDS_BLOCK_SIZE
, psf
)) != SDS_BLOCK_SIZE
)
477 psf_log_printf (psf
, "*** Warning : short read (%d != %d).\n", k
, SDS_BLOCK_SIZE
) ;
479 if (psds
->read_data
[0] != 0xF0)
480 { printf ("Error A : %02X\n", psds
->read_data
[0] & 0xFF) ;
483 checksum
= psds
->read_data
[1] ;
484 if (checksum
!= 0x7E)
485 { printf ("Error 1 : %02X\n", checksum
& 0xFF) ;
488 for (k
= 2 ; k
<= SDS_BLOCK_SIZE
- 3 ; k
++)
489 checksum
^= psds
->read_data
[k
] ;
493 if (checksum
!= psds
->read_data
[SDS_BLOCK_SIZE
- 2])
494 { psf_log_printf (psf
, "Block %d : checksum is %02X should be %02X\n", psds
->read_data
[4], checksum
, psds
->read_data
[SDS_BLOCK_SIZE
- 2]) ;
497 ucptr
= psds
->read_data
+ 5 ;
498 for (k
= 0 ; k
< 120 ; k
+= 3)
499 { sample
= (ucptr
[k
] << 25) + (ucptr
[k
+ 1] << 18) + (ucptr
[k
+ 2] << 11) ;
500 psds
->read_samples
[k
/ 3] = (int) (sample
- 0x80000000) ;
504 } /* sds_3byte_read */
507 sds_4byte_read (SF_PRIVATE
*psf
, SDS_PRIVATE
*psds
)
508 { unsigned char *ucptr
, checksum
;
509 unsigned int sample
;
512 psds
->read_block
++ ;
513 psds
->read_count
= 0 ;
515 if (psds
->read_block
* psds
->samplesperblock
> psds
->frames
)
516 { memset (psds
->read_samples
, 0, psds
->samplesperblock
* sizeof (int)) ;
520 if ((k
= psf_fread (psds
->read_data
, 1, SDS_BLOCK_SIZE
, psf
)) != SDS_BLOCK_SIZE
)
521 psf_log_printf (psf
, "*** Warning : short read (%d != %d).\n", k
, SDS_BLOCK_SIZE
) ;
523 if (psds
->read_data
[0] != 0xF0)
524 { printf ("Error A : %02X\n", psds
->read_data
[0] & 0xFF) ;
527 checksum
= psds
->read_data
[1] ;
528 if (checksum
!= 0x7E)
529 { printf ("Error 1 : %02X\n", checksum
& 0xFF) ;
532 for (k
= 2 ; k
<= SDS_BLOCK_SIZE
- 3 ; k
++)
533 checksum
^= psds
->read_data
[k
] ;
537 if (checksum
!= psds
->read_data
[SDS_BLOCK_SIZE
- 2])
538 { psf_log_printf (psf
, "Block %d : checksum is %02X should be %02X\n", psds
->read_data
[4], checksum
, psds
->read_data
[SDS_BLOCK_SIZE
- 2]) ;
541 ucptr
= psds
->read_data
+ 5 ;
542 for (k
= 0 ; k
< 120 ; k
+= 4)
543 { sample
= (ucptr
[k
] << 25) + (ucptr
[k
+ 1] << 18) + (ucptr
[k
+ 2] << 11) + (ucptr
[k
+ 3] << 4) ;
544 psds
->read_samples
[k
/ 4] = (int) (sample
- 0x80000000) ;
548 } /* sds_4byte_read */
552 sds_read_s (SF_PRIVATE
*psf
, short *ptr
, sf_count_t len
)
553 { SDS_PRIVATE
*psds
;
555 int k
, bufferlen
, readcount
, count
;
556 sf_count_t total
= 0 ;
558 if (psf
->codec_data
== NULL
)
560 psds
= (SDS_PRIVATE
*) psf
->codec_data
;
563 bufferlen
= ARRAY_LEN (psf
->u
.ibuf
) ;
565 { readcount
= (len
>= bufferlen
) ? bufferlen
: len
;
566 count
= sds_read (psf
, psds
, iptr
, readcount
) ;
567 for (k
= 0 ; k
< readcount
; k
++)
568 ptr
[total
+ k
] = iptr
[k
] >> 16 ;
577 sds_read_i (SF_PRIVATE
*psf
, int *ptr
, sf_count_t len
)
578 { SDS_PRIVATE
*psds
;
581 if (psf
->codec_data
== NULL
)
583 psds
= (SDS_PRIVATE
*) psf
->codec_data
;
585 total
= sds_read (psf
, psds
, ptr
, len
) ;
591 sds_read_f (SF_PRIVATE
*psf
, float *ptr
, sf_count_t len
)
592 { SDS_PRIVATE
*psds
;
594 int k
, bufferlen
, readcount
, count
;
595 sf_count_t total
= 0 ;
598 if (psf
->codec_data
== NULL
)
600 psds
= (SDS_PRIVATE
*) psf
->codec_data
;
602 if (psf
->norm_float
== SF_TRUE
)
603 normfact
= 1.0 / 0x80000000 ;
605 normfact
= 1.0 / (1 << psds
->bitwidth
) ;
608 bufferlen
= ARRAY_LEN (psf
->u
.ibuf
) ;
610 { readcount
= (len
>= bufferlen
) ? bufferlen
: len
;
611 count
= sds_read (psf
, psds
, iptr
, readcount
) ;
612 for (k
= 0 ; k
< readcount
; k
++)
613 ptr
[total
+ k
] = normfact
* iptr
[k
] ;
622 sds_read_d (SF_PRIVATE
*psf
, double *ptr
, sf_count_t len
)
623 { SDS_PRIVATE
*psds
;
625 int k
, bufferlen
, readcount
, count
;
626 sf_count_t total
= 0 ;
629 if (psf
->codec_data
== NULL
)
631 psds
= (SDS_PRIVATE
*) psf
->codec_data
;
633 if (psf
->norm_double
== SF_TRUE
)
634 normfact
= 1.0 / 0x80000000 ;
636 normfact
= 1.0 / (1 << psds
->bitwidth
) ;
639 bufferlen
= ARRAY_LEN (psf
->u
.ibuf
) ;
641 { readcount
= (len
>= bufferlen
) ? bufferlen
: len
;
642 count
= sds_read (psf
, psds
, iptr
, readcount
) ;
643 for (k
= 0 ; k
< readcount
; k
++)
644 ptr
[total
+ k
] = normfact
* iptr
[k
] ;
653 sds_read (SF_PRIVATE
*psf
, SDS_PRIVATE
*psds
, int *ptr
, int len
)
654 { int count
, total
= 0 ;
657 { if (psds
->read_block
* psds
->samplesperblock
>= psds
->frames
)
658 { memset (&(ptr
[total
]), 0, (len
- total
) * sizeof (int)) ;
662 if (psds
->read_count
>= psds
->samplesperblock
)
663 psds
->reader (psf
, psds
) ;
665 count
= (psds
->samplesperblock
- psds
->read_count
) ;
666 count
= (len
- total
> count
) ? count
: len
- total
;
668 memcpy (&(ptr
[total
]), &(psds
->read_samples
[psds
->read_count
]), count
* sizeof (int)) ;
670 psds
->read_count
+= count
;
676 /*==============================================================================
680 sds_seek (SF_PRIVATE
*psf
, int mode
, sf_count_t seek_from_start
)
681 { SDS_PRIVATE
*psds
;
682 sf_count_t file_offset
;
683 int newblock
, newsample
;
685 if ((psds
= psf
->codec_data
) == NULL
)
686 { psf
->error
= SFE_INTERNAL
;
687 return PSF_SEEK_ERROR
;
690 if (psf
->datalength
< 0 || psf
->dataoffset
< 0)
691 { psf
->error
= SFE_BAD_SEEK
;
692 return PSF_SEEK_ERROR
;
695 if (seek_from_start
< 0 || seek_from_start
> psf
->sf
.frames
)
696 { psf
->error
= SFE_BAD_SEEK
;
697 return PSF_SEEK_ERROR
;
700 if (mode
== SFM_READ
&& psds
->write_count
> 0)
701 psds
->writer (psf
, psds
) ;
703 newblock
= seek_from_start
/ psds
->samplesperblock
;
704 newsample
= seek_from_start
% psds
->samplesperblock
;
708 if (newblock
> psds
->total_blocks
)
709 { psf
->error
= SFE_BAD_SEEK
;
710 return PSF_SEEK_ERROR
;
713 file_offset
= psf
->dataoffset
+ newblock
* SDS_BLOCK_SIZE
;
715 if (psf_fseek (psf
, file_offset
, SEEK_SET
) != file_offset
)
716 { psf
->error
= SFE_SEEK_FAILED
;
717 return PSF_SEEK_ERROR
;
720 psds
->read_block
= newblock
;
721 psds
->reader (psf
, psds
) ;
722 psds
->read_count
= newsample
;
726 if (newblock
> psds
->total_blocks
)
727 { psf
->error
= SFE_BAD_SEEK
;
728 return PSF_SEEK_ERROR
;
731 file_offset
= psf
->dataoffset
+ newblock
* SDS_BLOCK_SIZE
;
733 if (psf_fseek (psf
, file_offset
, SEEK_SET
) != file_offset
)
734 { psf
->error
= SFE_SEEK_FAILED
;
735 return PSF_SEEK_ERROR
;
738 psds
->write_block
= newblock
;
739 psds
->reader (psf
, psds
) ;
740 psds
->write_count
= newsample
;
744 psf
->error
= SFE_BAD_SEEK
;
745 return PSF_SEEK_ERROR
;
749 return seek_from_start
;
752 /*==============================================================================
756 sds_2byte_write (SF_PRIVATE
*psf
, SDS_PRIVATE
*psds
)
757 { unsigned char *ucptr
, checksum
;
758 unsigned int sample
;
761 psds
->write_data
[0] = 0xF0 ;
762 psds
->write_data
[1] = 0x7E ;
763 psds
->write_data
[2] = 0 ; /* Channel number */
764 psds
->write_data
[3] = 2 ;
765 psds
->write_data
[4] = psds
->write_block
& 0x7F ; /* Packet number */
767 ucptr
= psds
->write_data
+ 5 ;
768 for (k
= 0 ; k
< 120 ; k
+= 2)
769 { sample
= psds
->write_samples
[k
/ 2] ;
770 sample
+= 0x80000000 ;
771 ucptr
[k
] = (sample
>> 25) & 0x7F ;
772 ucptr
[k
+ 1] = (sample
>> 18) & 0x7F ;
775 checksum
= psds
->write_data
[1] ;
776 for (k
= 2 ; k
<= SDS_BLOCK_SIZE
- 3 ; k
++)
777 checksum
^= psds
->write_data
[k
] ;
780 psds
->write_data
[SDS_BLOCK_SIZE
- 2] = checksum
;
781 psds
->write_data
[SDS_BLOCK_SIZE
- 1] = 0xF7 ;
783 if ((k
= psf_fwrite (psds
->write_data
, 1, SDS_BLOCK_SIZE
, psf
)) != SDS_BLOCK_SIZE
)
784 psf_log_printf (psf
, "*** Warning : psf_fwrite (%d != %d).\n", k
, SDS_BLOCK_SIZE
) ;
786 psds
->write_block
++ ;
787 psds
->write_count
= 0 ;
789 if (psds
->write_block
> psds
->total_blocks
)
790 psds
->total_blocks
= psds
->write_block
;
791 psds
->frames
= psds
->total_blocks
* psds
->samplesperblock
;
794 } /* sds_2byte_write */
797 sds_3byte_write (SF_PRIVATE
*psf
, SDS_PRIVATE
*psds
)
798 { unsigned char *ucptr
, checksum
;
799 unsigned int sample
;
802 psds
->write_data
[0] = 0xF0 ;
803 psds
->write_data
[1] = 0x7E ;
804 psds
->write_data
[2] = 0 ; /* Channel number */
805 psds
->write_data
[3] = 2 ;
806 psds
->write_data
[4] = psds
->write_block
& 0x7F ; /* Packet number */
808 ucptr
= psds
->write_data
+ 5 ;
809 for (k
= 0 ; k
< 120 ; k
+= 3)
810 { sample
= psds
->write_samples
[k
/ 3] ;
811 sample
+= 0x80000000 ;
812 ucptr
[k
] = (sample
>> 25) & 0x7F ;
813 ucptr
[k
+ 1] = (sample
>> 18) & 0x7F ;
814 ucptr
[k
+ 2] = (sample
>> 11) & 0x7F ;
817 checksum
= psds
->write_data
[1] ;
818 for (k
= 2 ; k
<= SDS_BLOCK_SIZE
- 3 ; k
++)
819 checksum
^= psds
->write_data
[k
] ;
822 psds
->write_data
[SDS_BLOCK_SIZE
- 2] = checksum
;
823 psds
->write_data
[SDS_BLOCK_SIZE
- 1] = 0xF7 ;
825 if ((k
= psf_fwrite (psds
->write_data
, 1, SDS_BLOCK_SIZE
, psf
)) != SDS_BLOCK_SIZE
)
826 psf_log_printf (psf
, "*** Warning : psf_fwrite (%d != %d).\n", k
, SDS_BLOCK_SIZE
) ;
828 psds
->write_block
++ ;
829 psds
->write_count
= 0 ;
831 if (psds
->write_block
> psds
->total_blocks
)
832 psds
->total_blocks
= psds
->write_block
;
833 psds
->frames
= psds
->total_blocks
* psds
->samplesperblock
;
836 } /* sds_3byte_write */
839 sds_4byte_write (SF_PRIVATE
*psf
, SDS_PRIVATE
*psds
)
840 { unsigned char *ucptr
, checksum
;
841 unsigned int sample
;
844 psds
->write_data
[0] = 0xF0 ;
845 psds
->write_data
[1] = 0x7E ;
846 psds
->write_data
[2] = 0 ; /* Channel number */
847 psds
->write_data
[3] = 2 ;
848 psds
->write_data
[4] = psds
->write_block
& 0x7F ; /* Packet number */
850 ucptr
= psds
->write_data
+ 5 ;
851 for (k
= 0 ; k
< 120 ; k
+= 4)
852 { sample
= psds
->write_samples
[k
/ 4] ;
853 sample
+= 0x80000000 ;
854 ucptr
[k
] = (sample
>> 25) & 0x7F ;
855 ucptr
[k
+ 1] = (sample
>> 18) & 0x7F ;
856 ucptr
[k
+ 2] = (sample
>> 11) & 0x7F ;
857 ucptr
[k
+ 3] = (sample
>> 4) & 0x7F ;
860 checksum
= psds
->write_data
[1] ;
861 for (k
= 2 ; k
<= SDS_BLOCK_SIZE
- 3 ; k
++)
862 checksum
^= psds
->write_data
[k
] ;
865 psds
->write_data
[SDS_BLOCK_SIZE
- 2] = checksum
;
866 psds
->write_data
[SDS_BLOCK_SIZE
- 1] = 0xF7 ;
868 if ((k
= psf_fwrite (psds
->write_data
, 1, SDS_BLOCK_SIZE
, psf
)) != SDS_BLOCK_SIZE
)
869 psf_log_printf (psf
, "*** Warning : psf_fwrite (%d != %d).\n", k
, SDS_BLOCK_SIZE
) ;
871 psds
->write_block
++ ;
872 psds
->write_count
= 0 ;
874 if (psds
->write_block
> psds
->total_blocks
)
875 psds
->total_blocks
= psds
->write_block
;
876 psds
->frames
= psds
->total_blocks
* psds
->samplesperblock
;
879 } /* sds_4byte_write */
882 sds_write_s (SF_PRIVATE
*psf
, const short *ptr
, sf_count_t len
)
883 { SDS_PRIVATE
*psds
;
885 int k
, bufferlen
, writecount
, count
;
886 sf_count_t total
= 0 ;
888 if (psf
->codec_data
== NULL
)
890 psds
= (SDS_PRIVATE
*) psf
->codec_data
;
891 psds
->total_written
+= len
;
894 bufferlen
= ARRAY_LEN (psf
->u
.ibuf
) ;
896 { writecount
= (len
>= bufferlen
) ? bufferlen
: len
;
897 for (k
= 0 ; k
< writecount
; k
++)
898 iptr
[k
] = ptr
[total
+ k
] << 16 ;
899 count
= sds_write (psf
, psds
, iptr
, writecount
) ;
908 sds_write_i (SF_PRIVATE
*psf
, const int *ptr
, sf_count_t len
)
909 { SDS_PRIVATE
*psds
;
912 if (psf
->codec_data
== NULL
)
914 psds
= (SDS_PRIVATE
*) psf
->codec_data
;
915 psds
->total_written
+= len
;
917 total
= sds_write (psf
, psds
, ptr
, len
) ;
923 sds_write_f (SF_PRIVATE
*psf
, const float *ptr
, sf_count_t len
)
924 { SDS_PRIVATE
*psds
;
926 int k
, bufferlen
, writecount
, count
;
927 sf_count_t total
= 0 ;
930 if (psf
->codec_data
== NULL
)
932 psds
= (SDS_PRIVATE
*) psf
->codec_data
;
933 psds
->total_written
+= len
;
935 if (psf
->norm_float
== SF_TRUE
)
936 normfact
= 1.0 * 0x80000000 ;
938 normfact
= 1.0 * (1 << psds
->bitwidth
) ;
941 bufferlen
= ARRAY_LEN (psf
->u
.ibuf
) ;
943 { writecount
= (len
>= bufferlen
) ? bufferlen
: len
;
944 for (k
= 0 ; k
< writecount
; k
++)
945 iptr
[k
] = normfact
* ptr
[total
+ k
] ;
946 count
= sds_write (psf
, psds
, iptr
, writecount
) ;
955 sds_write_d (SF_PRIVATE
*psf
, const double *ptr
, sf_count_t len
)
956 { SDS_PRIVATE
*psds
;
958 int k
, bufferlen
, writecount
, count
;
959 sf_count_t total
= 0 ;
962 if (psf
->codec_data
== NULL
)
964 psds
= (SDS_PRIVATE
*) psf
->codec_data
;
965 psds
->total_written
+= len
;
967 if (psf
->norm_double
== SF_TRUE
)
968 normfact
= 1.0 * 0x80000000 ;
970 normfact
= 1.0 * (1 << psds
->bitwidth
) ;
973 bufferlen
= ARRAY_LEN (psf
->u
.ibuf
) ;
975 { writecount
= (len
>= bufferlen
) ? bufferlen
: len
;
976 for (k
= 0 ; k
< writecount
; k
++)
977 iptr
[k
] = normfact
* ptr
[total
+ k
] ;
978 count
= sds_write (psf
, psds
, iptr
, writecount
) ;
987 sds_write (SF_PRIVATE
*psf
, SDS_PRIVATE
*psds
, const int *ptr
, int len
)
988 { int count
, total
= 0 ;
991 { count
= psds
->samplesperblock
- psds
->write_count
;
992 if (count
> len
- total
)
993 count
= len
- total
;
995 memcpy (&(psds
->write_samples
[psds
->write_count
]), &(ptr
[total
]), count
* sizeof (int)) ;
997 psds
->write_count
+= count
;
999 if (psds
->write_count
>= psds
->samplesperblock
)
1000 psds
->writer (psf
, psds
) ;