2 ** Copyright (C) 2004-2011 Erik de Castro Lopo <erikd@mega-nerd.com>
3 ** Copyright (C) 2004 Tobias Gehrig <tgehrig@ira.uka.de>
5 ** This program is free software ; you can redistribute it and/or modify
6 ** it under the terms of the GNU Lesser General Public License as published by
7 ** the Free Software Foundation ; either version 2.1 of the License, or
8 ** (at your option) any later version.
10 ** This program is distributed in the hope that it will be useful,
11 ** but WITHOUT ANY WARRANTY ; without even the implied warranty of
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ** GNU Lesser General Public License for more details.
15 ** You should have received a copy of the GNU Lesser General Public License
16 ** along with this program ; if not, write to the Free Software
17 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
32 #if HAVE_EXTERNAL_LIBS
34 #include <FLAC/stream_decoder.h>
35 #include <FLAC/stream_encoder.h>
36 #include <FLAC/metadata.h>
38 /*------------------------------------------------------------------------------
39 ** Private static functions.
42 #define ENC_BUFFER_SIZE 8192
45 { PFLAC_PCM_SHORT
= 50,
53 FLAC__StreamDecoder
*fsd
;
54 FLAC__StreamEncoder
*fse
;
58 unsigned pos
, len
, remain
;
60 FLAC__StreamMetadata
*metadata
;
62 const FLAC__int32
* const * wbuffer
;
63 FLAC__int32
* rbuffer
[FLAC__MAX_CHANNELS
] ;
65 FLAC__int32
* encbuffer
;
68 const FLAC__Frame
*frame
;
69 FLAC__bool bufferbackup
;
77 static sf_count_t
flac_seek (SF_PRIVATE
*psf
, int mode
, sf_count_t offset
) ;
78 static int flac_close (SF_PRIVATE
*psf
) ;
80 static int flac_enc_init (SF_PRIVATE
*psf
) ;
81 static int flac_read_header (SF_PRIVATE
*psf
) ;
83 static sf_count_t
flac_read_flac2s (SF_PRIVATE
*psf
, short *ptr
, sf_count_t len
) ;
84 static sf_count_t
flac_read_flac2i (SF_PRIVATE
*psf
, int *ptr
, sf_count_t len
) ;
85 static sf_count_t
flac_read_flac2f (SF_PRIVATE
*psf
, float *ptr
, sf_count_t len
) ;
86 static sf_count_t
flac_read_flac2d (SF_PRIVATE
*psf
, double *ptr
, sf_count_t len
) ;
88 static sf_count_t
flac_write_s2flac (SF_PRIVATE
*psf
, const short *ptr
, sf_count_t len
) ;
89 static sf_count_t
flac_write_i2flac (SF_PRIVATE
*psf
, const int *ptr
, sf_count_t len
) ;
90 static sf_count_t
flac_write_f2flac (SF_PRIVATE
*psf
, const float *ptr
, sf_count_t len
) ;
91 static sf_count_t
flac_write_d2flac (SF_PRIVATE
*psf
, const double *ptr
, sf_count_t len
) ;
93 static void f2flac8_array (const float *src
, FLAC__int32
*dest
, int count
, int normalize
) ;
94 static void f2flac16_array (const float *src
, FLAC__int32
*dest
, int count
, int normalize
) ;
95 static void f2flac24_array (const float *src
, FLAC__int32
*dest
, int count
, int normalize
) ;
96 static void f2flac8_clip_array (const float *src
, FLAC__int32
*dest
, int count
, int normalize
) ;
97 static void f2flac16_clip_array (const float *src
, FLAC__int32
*dest
, int count
, int normalize
) ;
98 static void f2flac24_clip_array (const float *src
, FLAC__int32
*dest
, int count
, int normalize
) ;
99 static void d2flac8_array (const double *src
, FLAC__int32
*dest
, int count
, int normalize
) ;
100 static void d2flac16_array (const double *src
, FLAC__int32
*dest
, int count
, int normalize
) ;
101 static void d2flac24_array (const double *src
, FLAC__int32
*dest
, int count
, int normalize
) ;
102 static void d2flac8_clip_array (const double *src
, FLAC__int32
*dest
, int count
, int normalize
) ;
103 static void d2flac16_clip_array (const double *src
, FLAC__int32
*dest
, int count
, int normalize
) ;
104 static void d2flac24_clip_array (const double *src
, FLAC__int32
*dest
, int count
, int normalize
) ;
106 static int flac_command (SF_PRIVATE
*psf
, int command
, void *data
, int datasize
) ;
108 /* Decoder Callbacks */
109 static FLAC__StreamDecoderReadStatus
sf_flac_read_callback (const FLAC__StreamDecoder
*decoder
, FLAC__byte buffer
[], size_t *bytes
, void *client_data
) ;
110 static FLAC__StreamDecoderSeekStatus
sf_flac_seek_callback (const FLAC__StreamDecoder
*decoder
, FLAC__uint64 absolute_byte_offset
, void *client_data
) ;
111 static FLAC__StreamDecoderTellStatus
sf_flac_tell_callback (const FLAC__StreamDecoder
*decoder
, FLAC__uint64
*absolute_byte_offset
, void *client_data
) ;
112 static FLAC__StreamDecoderLengthStatus
sf_flac_length_callback (const FLAC__StreamDecoder
*decoder
, FLAC__uint64
*stream_length
, void *client_data
) ;
113 static FLAC__bool
sf_flac_eof_callback (const FLAC__StreamDecoder
*decoder
, void *client_data
) ;
114 static FLAC__StreamDecoderWriteStatus
sf_flac_write_callback (const FLAC__StreamDecoder
*decoder
, const FLAC__Frame
*frame
, const FLAC__int32
* const buffer
[], void *client_data
) ;
115 static void sf_flac_meta_callback (const FLAC__StreamDecoder
*decoder
, const FLAC__StreamMetadata
*metadata
, void *client_data
) ;
116 static void sf_flac_error_callback (const FLAC__StreamDecoder
*decoder
, FLAC__StreamDecoderErrorStatus status
, void *client_data
) ;
118 /* Encoder Callbacks */
119 static FLAC__StreamEncoderSeekStatus
sf_flac_enc_seek_callback (const FLAC__StreamEncoder
*encoder
, FLAC__uint64 absolute_byte_offset
, void *client_data
) ;
120 static FLAC__StreamEncoderTellStatus
sf_flac_enc_tell_callback (const FLAC__StreamEncoder
*encoder
, FLAC__uint64
*absolute_byte_offset
, void *client_data
) ;
121 static FLAC__StreamEncoderWriteStatus
sf_flac_enc_write_callback (const FLAC__StreamEncoder
*encoder
, const FLAC__byte buffer
[], size_t bytes
, unsigned samples
, unsigned current_frame
, void *client_data
) ;
124 s2flac8_array (const short *src
, FLAC__int32
*dest
, int count
)
125 { while (--count
>= 0)
126 dest
[count
] = src
[count
] >> 8 ;
127 } /* s2flac8_array */
130 s2flac16_array (const short *src
, FLAC__int32
*dest
, int count
)
131 { while (--count
>= 0)
132 dest
[count
] = src
[count
] ;
133 } /* s2flac16_array */
136 s2flac24_array (const short *src
, FLAC__int32
*dest
, int count
)
137 { while (--count
>= 0)
138 dest
[count
] = src
[count
] << 8 ;
139 } /* s2flac24_array */
142 i2flac8_array (const int *src
, FLAC__int32
*dest
, int count
)
143 { while (--count
>= 0)
144 dest
[count
] = src
[count
] >> 24 ;
145 } /* i2flac8_array */
148 i2flac16_array (const int *src
, FLAC__int32
*dest
, int count
)
151 dest
[count
] = src
[count
] >> 16 ;
152 } /* i2flac16_array */
155 i2flac24_array (const int *src
, FLAC__int32
*dest
, int count
)
156 { while (--count
>= 0)
157 dest
[count
] = src
[count
] >> 8 ;
158 } /* i2flac24_array */
161 flac_buffer_copy (SF_PRIVATE
*psf
)
162 { FLAC_PRIVATE
* pflac
= (FLAC_PRIVATE
*) psf
->codec_data
;
163 const FLAC__Frame
*frame
= pflac
->frame
;
164 const FLAC__int32
* const *buffer
= pflac
->wbuffer
;
165 unsigned i
= 0, j
, offset
;
168 ** frame->header.blocksize is variable and we're using a constant blocksize
169 ** of FLAC__MAX_BLOCK_SIZE.
170 ** Check our assumptions here.
172 if (frame
->header
.blocksize
> FLAC__MAX_BLOCK_SIZE
)
173 { psf_log_printf (psf
, "Ooops : frame->header.blocksize (%d) > FLAC__MAX_BLOCK_SIZE (%d)\n", __func__
, __LINE__
, frame
->header
.blocksize
, FLAC__MAX_BLOCK_SIZE
) ;
174 psf
->error
= SFE_INTERNAL
;
178 if (pflac
->ptr
== NULL
)
180 ** Not sure why this code is here and not elsewhere.
181 ** Removing it causes valgrind errors.
183 pflac
->bufferbackup
= SF_TRUE
;
184 for (i
= 0 ; i
< frame
->header
.channels
; i
++)
186 if (pflac
->rbuffer
[i
] == NULL
)
187 pflac
->rbuffer
[i
] = calloc (FLAC__MAX_BLOCK_SIZE
, sizeof (FLAC__int32
)) ;
189 memcpy (pflac
->rbuffer
[i
], buffer
[i
], frame
->header
.blocksize
* sizeof (FLAC__int32
)) ;
191 pflac
->wbuffer
= (const FLAC__int32
* const*) pflac
->rbuffer
;
196 switch (pflac
->pcmtype
)
197 { case PFLAC_PCM_SHORT
:
198 { short *retpcm
= (short*) pflac
->ptr
;
199 int shift
= 16 - frame
->header
.bits_per_sample
;
201 { shift
= abs (shift
) ;
202 for (i
= 0 ; i
< frame
->header
.blocksize
&& pflac
->remain
> 0 ; i
++)
203 { offset
= pflac
->pos
+ i
* frame
->header
.channels
;
205 if (pflac
->bufferpos
>= frame
->header
.blocksize
)
208 for (j
= 0 ; j
< frame
->header
.channels
; j
++)
209 retpcm
[offset
+ j
] = buffer
[j
][pflac
->bufferpos
] >> shift
;
210 pflac
->remain
-= frame
->header
.channels
;
215 { for (i
= 0 ; i
< frame
->header
.blocksize
&& pflac
->remain
> 0 ; i
++)
216 { offset
= pflac
->pos
+ i
* frame
->header
.channels
;
218 if (pflac
->bufferpos
>= frame
->header
.blocksize
)
221 for (j
= 0 ; j
< frame
->header
.channels
; j
++)
222 retpcm
[offset
+ j
] = (buffer
[j
][pflac
->bufferpos
]) << shift
;
224 pflac
->remain
-= frame
->header
.channels
;
232 { int *retpcm
= (int*) pflac
->ptr
;
233 int shift
= 32 - frame
->header
.bits_per_sample
;
234 for (i
= 0 ; i
< frame
->header
.blocksize
&& pflac
->remain
> 0 ; i
++)
235 { offset
= pflac
->pos
+ i
* frame
->header
.channels
;
237 if (pflac
->bufferpos
>= frame
->header
.blocksize
)
240 for (j
= 0 ; j
< frame
->header
.channels
; j
++)
241 retpcm
[offset
+ j
] = buffer
[j
][pflac
->bufferpos
] << shift
;
242 pflac
->remain
-= frame
->header
.channels
;
248 case PFLAC_PCM_FLOAT
:
249 { float *retpcm
= (float*) pflac
->ptr
;
250 float norm
= (psf
->norm_float
== SF_TRUE
) ? 1.0 / (1 << (frame
->header
.bits_per_sample
- 1)) : 1.0 ;
252 for (i
= 0 ; i
< frame
->header
.blocksize
&& pflac
->remain
> 0 ; i
++)
253 { offset
= pflac
->pos
+ i
* frame
->header
.channels
;
255 if (pflac
->bufferpos
>= frame
->header
.blocksize
)
258 for (j
= 0 ; j
< frame
->header
.channels
; j
++)
259 retpcm
[offset
+ j
] = buffer
[j
][pflac
->bufferpos
] * norm
;
260 pflac
->remain
-= frame
->header
.channels
;
266 case PFLAC_PCM_DOUBLE
:
267 { double *retpcm
= (double*) pflac
->ptr
;
268 double norm
= (psf
->norm_double
== SF_TRUE
) ? 1.0 / (1 << (frame
->header
.bits_per_sample
- 1)) : 1.0 ;
270 for (i
= 0 ; i
< frame
->header
.blocksize
&& pflac
->remain
> 0 ; i
++)
271 { offset
= pflac
->pos
+ i
* frame
->header
.channels
;
273 if (pflac
->bufferpos
>= frame
->header
.blocksize
)
276 for (j
= 0 ; j
< frame
->header
.channels
; j
++)
277 retpcm
[offset
+ j
] = buffer
[j
][pflac
->bufferpos
] * norm
;
278 pflac
->remain
-= frame
->header
.channels
;
288 offset
= i
* frame
->header
.channels
;
289 pflac
->pos
+= i
* frame
->header
.channels
;
292 } /* flac_buffer_copy */
295 static FLAC__StreamDecoderReadStatus
296 sf_flac_read_callback (const FLAC__StreamDecoder
* UNUSED (decoder
), FLAC__byte buffer
[], size_t *bytes
, void *client_data
)
297 { SF_PRIVATE
*psf
= (SF_PRIVATE
*) client_data
;
299 *bytes
= psf_fread (buffer
, 1, *bytes
, psf
) ;
300 if (*bytes
> 0 && psf
->error
== 0)
301 return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE
;
303 return FLAC__STREAM_DECODER_READ_STATUS_ABORT
;
304 } /* sf_flac_read_callback */
306 static FLAC__StreamDecoderSeekStatus
307 sf_flac_seek_callback (const FLAC__StreamDecoder
* UNUSED (decoder
), FLAC__uint64 absolute_byte_offset
, void *client_data
)
308 { SF_PRIVATE
*psf
= (SF_PRIVATE
*) client_data
;
310 psf_fseek (psf
, absolute_byte_offset
, SEEK_SET
) ;
312 return FLAC__STREAM_DECODER_SEEK_STATUS_ERROR
;
314 return FLAC__STREAM_DECODER_SEEK_STATUS_OK
;
315 } /* sf_flac_seek_callback */
317 static FLAC__StreamDecoderTellStatus
318 sf_flac_tell_callback (const FLAC__StreamDecoder
* UNUSED (decoder
), FLAC__uint64
*absolute_byte_offset
, void *client_data
)
319 { SF_PRIVATE
*psf
= (SF_PRIVATE
*) client_data
;
321 *absolute_byte_offset
= psf_ftell (psf
) ;
323 return FLAC__STREAM_DECODER_TELL_STATUS_ERROR
;
325 return FLAC__STREAM_DECODER_TELL_STATUS_OK
;
326 } /* sf_flac_tell_callback */
328 static FLAC__StreamDecoderLengthStatus
329 sf_flac_length_callback (const FLAC__StreamDecoder
* UNUSED (decoder
), FLAC__uint64
*stream_length
, void *client_data
)
330 { SF_PRIVATE
*psf
= (SF_PRIVATE
*) client_data
;
332 if ((*stream_length
= psf
->filelength
) == 0)
333 return FLAC__STREAM_DECODER_LENGTH_STATUS_ERROR
;
335 return FLAC__STREAM_DECODER_LENGTH_STATUS_OK
;
336 } /* sf_flac_length_callback */
339 sf_flac_eof_callback (const FLAC__StreamDecoder
*UNUSED (decoder
), void *client_data
)
340 { SF_PRIVATE
*psf
= (SF_PRIVATE
*) client_data
;
342 if (psf_ftell (psf
) == psf
->filelength
)
346 } /* sf_flac_eof_callback */
348 static FLAC__StreamDecoderWriteStatus
349 sf_flac_write_callback (const FLAC__StreamDecoder
* UNUSED (decoder
), const FLAC__Frame
*frame
, const FLAC__int32
* const buffer
[], void *client_data
)
350 { SF_PRIVATE
*psf
= (SF_PRIVATE
*) client_data
;
351 FLAC_PRIVATE
* pflac
= (FLAC_PRIVATE
*) psf
->codec_data
;
353 pflac
->frame
= frame
;
354 pflac
->bufferpos
= 0 ;
356 pflac
->bufferbackup
= SF_FALSE
;
357 pflac
->wbuffer
= buffer
;
359 flac_buffer_copy (psf
) ;
361 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE
;
362 } /* sf_flac_write_callback */
365 sf_flac_meta_get_vorbiscomments (SF_PRIVATE
*psf
, const FLAC__StreamMetadata
*metadata
)
367 { { "title", SF_STR_TITLE
},
368 { "copyright", SF_STR_COPYRIGHT
},
369 { "software", SF_STR_SOFTWARE
},
370 { "artist", SF_STR_ARTIST
},
371 { "comment", SF_STR_COMMENT
},
372 { "date", SF_STR_DATE
},
373 { "album", SF_STR_ALBUM
},
374 { "license", SF_STR_LICENSE
},
375 { "tracknumber", SF_STR_TRACKNUMBER
},
376 { "genre", SF_STR_GENRE
}
379 const char *value
, *cptr
;
382 for (k
= 0 ; k
< ARRAY_LEN (tags
) ; k
++)
383 { tag_num
= FLAC__metadata_object_vorbiscomment_find_entry_from (metadata
, 0, tags
[k
].tag
) ;
388 value
= (const char*) metadata
->data
.vorbis_comment
.comments
[tag_num
].entry
;
389 if ((cptr
= strchr (value
, '=')) != NULL
)
392 psf_log_printf (psf
, " %-10s : %s\n", tags
[k
].tag
, value
) ;
393 psf_store_string (psf
, tags
[k
].type
, value
) ;
397 } /* sf_flac_meta_get_vorbiscomments */
400 sf_flac_meta_callback (const FLAC__StreamDecoder
* UNUSED (decoder
), const FLAC__StreamMetadata
*metadata
, void *client_data
)
401 { SF_PRIVATE
*psf
= (SF_PRIVATE
*) client_data
;
404 switch (metadata
->type
)
405 { case FLAC__METADATA_TYPE_STREAMINFO
:
406 psf
->sf
.channels
= metadata
->data
.stream_info
.channels
;
407 psf
->sf
.samplerate
= metadata
->data
.stream_info
.sample_rate
;
408 psf
->sf
.frames
= metadata
->data
.stream_info
.total_samples
;
410 psf_log_printf (psf
, "FLAC Stream Metadata\n Channels : %d\n Sample rate : %d\n", psf
->sf
.channels
, psf
->sf
.samplerate
) ;
412 if (psf
->sf
.frames
== 0)
413 { psf_log_printf (psf
, " Frames : 0 (bumping to SF_COUNT_MAX)\n") ;
414 psf
->sf
.frames
= SF_COUNT_MAX
;
417 psf_log_printf (psf
, " Frames : %D\n", psf
->sf
.frames
) ;
419 switch (metadata
->data
.stream_info
.bits_per_sample
)
421 psf
->sf
.format
|= SF_FORMAT_PCM_S8
;
425 psf
->sf
.format
|= SF_FORMAT_PCM_16
;
429 psf
->sf
.format
|= SF_FORMAT_PCM_24
;
433 psf_log_printf (psf
, "sf_flac_meta_callback : bits_per_sample %d not yet implemented.\n", metadata
->data
.stream_info
.bits_per_sample
) ;
438 psf_log_printf (psf
, " Bit width : %d\n", bitwidth
) ;
441 case FLAC__METADATA_TYPE_VORBIS_COMMENT
:
442 psf_log_printf (psf
, "Vorbis Comment Metadata\n") ;
443 sf_flac_meta_get_vorbiscomments (psf
, metadata
) ;
446 case FLAC__METADATA_TYPE_PADDING
:
447 psf_log_printf (psf
, "Padding Metadata\n") ;
450 case FLAC__METADATA_TYPE_APPLICATION
:
451 psf_log_printf (psf
, "Application Metadata\n") ;
454 case FLAC__METADATA_TYPE_SEEKTABLE
:
455 psf_log_printf (psf
, "Seektable Metadata\n") ;
458 case FLAC__METADATA_TYPE_CUESHEET
:
459 psf_log_printf (psf
, "Cuesheet Metadata\n") ;
462 case FLAC__METADATA_TYPE_PICTURE
:
463 psf_log_printf (psf
, "Picture Metadata\n") ;
466 case FLAC__METADATA_TYPE_UNDEFINED
:
467 psf_log_printf (psf
, "Undefined Metadata\n") ;
471 psf_log_printf (psf
, "sf_flac_meta_callback : metadata-type %d not yet implemented.\n", metadata
->type
) ;
476 } /* sf_flac_meta_callback */
479 sf_flac_error_callback (const FLAC__StreamDecoder
* UNUSED (decoder
), FLAC__StreamDecoderErrorStatus status
, void *client_data
)
480 { SF_PRIVATE
*psf
= (SF_PRIVATE
*) client_data
;
482 psf_log_printf (psf
, "ERROR : %s\n", FLAC__StreamDecoderErrorStatusString
[status
]) ;
485 { case FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC
:
486 psf
->error
= SFE_FLAC_LOST_SYNC
;
488 case FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER
:
489 psf
->error
= SFE_FLAC_BAD_HEADER
;
492 psf
->error
= SFE_FLAC_UNKOWN_ERROR
;
497 } /* sf_flac_error_callback */
499 static FLAC__StreamEncoderSeekStatus
500 sf_flac_enc_seek_callback (const FLAC__StreamEncoder
* UNUSED (encoder
), FLAC__uint64 absolute_byte_offset
, void *client_data
)
501 { SF_PRIVATE
*psf
= (SF_PRIVATE
*) client_data
;
503 psf_fseek (psf
, absolute_byte_offset
, SEEK_SET
) ;
505 return FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR
;
507 return FLAC__STREAM_ENCODER_SEEK_STATUS_OK
;
508 } /* sf_flac_enc_seek_callback */
510 static FLAC__StreamEncoderTellStatus
511 sf_flac_enc_tell_callback (const FLAC__StreamEncoder
*UNUSED (encoder
), FLAC__uint64
*absolute_byte_offset
, void *client_data
)
512 { SF_PRIVATE
*psf
= (SF_PRIVATE
*) client_data
;
514 *absolute_byte_offset
= psf_ftell (psf
) ;
516 return FLAC__STREAM_ENCODER_TELL_STATUS_ERROR
;
518 return FLAC__STREAM_ENCODER_TELL_STATUS_OK
;
519 } /* sf_flac_enc_tell_callback */
521 static FLAC__StreamEncoderWriteStatus
522 sf_flac_enc_write_callback (const FLAC__StreamEncoder
* UNUSED (encoder
), const FLAC__byte buffer
[], size_t bytes
, unsigned UNUSED (samples
), unsigned UNUSED (current_frame
), void *client_data
)
523 { SF_PRIVATE
*psf
= (SF_PRIVATE
*) client_data
;
525 if (psf_fwrite (buffer
, 1, bytes
, psf
) == (sf_count_t
) bytes
&& psf
->error
== 0)
526 return FLAC__STREAM_ENCODER_WRITE_STATUS_OK
;
528 return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR
;
529 } /* sf_flac_enc_write_callback */
532 flac_write_strings (SF_PRIVATE
*psf
, FLAC_PRIVATE
* pflac
)
533 { FLAC__StreamMetadata_VorbisComment_Entry entry
;
534 int k
, string_count
= 0 ;
536 for (k
= 0 ; k
< SF_MAX_STRINGS
; k
++)
537 { if (psf
->strings
[k
].type
!= 0)
541 if (string_count
== 0)
544 if (pflac
->metadata
== NULL
&& (pflac
->metadata
= FLAC__metadata_object_new (FLAC__METADATA_TYPE_VORBIS_COMMENT
)) == NULL
)
545 { psf_log_printf (psf
, "FLAC__metadata_object_new returned NULL\n") ;
549 for (k
= 0 ; k
< SF_MAX_STRINGS
&& psf
->strings
[k
].type
!= 0 ; k
++)
550 { const char * key
, * value
;
552 switch (psf
->strings
[k
].type
)
553 { case SF_STR_SOFTWARE
:
559 case SF_STR_COPYRIGHT
:
565 case SF_STR_COMMENT
:
574 case SF_STR_LICENSE
:
577 case SF_STR_TRACKNUMBER
:
578 key
= "tracknumber" ;
587 value
= psf
->strings
[k
].str
;
589 FLAC__metadata_object_vorbiscomment_entry_from_name_value_pair (&entry
, key
, value
) ;
590 FLAC__metadata_object_vorbiscomment_append_comment (pflac
->metadata
, entry
, /* copy */ SF_FALSE
) ;
593 if (! FLAC__stream_encoder_set_metadata (pflac
->fse
, &pflac
->metadata
, 1))
594 { printf ("%s %d : fail\n", __func__
, __LINE__
) ;
599 } /* flac_write_strings */
602 flac_write_header (SF_PRIVATE
*psf
, int UNUSED (calc_length
))
603 { FLAC_PRIVATE
* pflac
= (FLAC_PRIVATE
*) psf
->codec_data
;
606 flac_write_strings (psf
, pflac
) ;
608 if ((err
= FLAC__stream_encoder_init_stream (pflac
->fse
, sf_flac_enc_write_callback
, sf_flac_enc_seek_callback
, sf_flac_enc_tell_callback
, NULL
, psf
)) != FLAC__STREAM_DECODER_INIT_STATUS_OK
)
609 { psf_log_printf (psf
, "Error : FLAC encoder init returned error : %s\n", FLAC__StreamEncoderInitStatusString
[err
]) ;
610 return SFE_FLAC_INIT_DECODER
;
614 psf
->dataoffset
= psf_ftell (psf
) ;
615 pflac
->encbuffer
= calloc (ENC_BUFFER_SIZE
, sizeof (FLAC__int32
)) ;
618 } /* flac_write_header */
620 /*------------------------------------------------------------------------------
625 flac_open (SF_PRIVATE
*psf
)
629 FLAC_PRIVATE
* pflac
= calloc (1, sizeof (FLAC_PRIVATE
)) ;
630 psf
->codec_data
= pflac
;
632 if (psf
->file
.mode
== SFM_RDWR
)
633 return SFE_BAD_MODE_RW
;
635 if (psf
->file
.mode
== SFM_READ
)
636 { if ((error
= flac_read_header (psf
)))
640 subformat
= SF_CODEC (psf
->sf
.format
) ;
642 if (psf
->file
.mode
== SFM_WRITE
)
643 { if ((SF_CONTAINER (psf
->sf
.format
)) != SF_FORMAT_FLAC
)
644 return SFE_BAD_OPEN_FORMAT
;
646 psf
->endian
= SF_ENDIAN_BIG
;
647 psf
->sf
.seekable
= 0 ;
649 psf
->str_flags
= SF_STR_ALLOW_START
;
651 if ((error
= flac_enc_init (psf
)))
654 psf
->write_header
= flac_write_header
;
657 psf
->datalength
= psf
->filelength
;
658 psf
->dataoffset
= 0 ;
659 psf
->blockwidth
= 0 ;
662 psf
->container_close
= flac_close
;
663 psf
->seek
= flac_seek
;
664 psf
->command
= flac_command
;
666 psf
->blockwidth
= psf
->bytewidth
* psf
->sf
.channels
;
669 { case SF_FORMAT_PCM_S8
: /* 8-bit FLAC. */
670 case SF_FORMAT_PCM_16
: /* 16-bit FLAC. */
671 case SF_FORMAT_PCM_24
: /* 24-bit FLAC. */
672 error
= flac_init (psf
) ;
675 default : return SFE_UNIMPLEMENTED
;
681 /*------------------------------------------------------------------------------
685 flac_close (SF_PRIVATE
*psf
)
686 { FLAC_PRIVATE
* pflac
;
689 if ((pflac
= (FLAC_PRIVATE
*) psf
->codec_data
) == NULL
)
692 if (pflac
->metadata
!= NULL
)
693 FLAC__metadata_object_delete (pflac
->metadata
) ;
695 if (psf
->file
.mode
== SFM_WRITE
)
696 { FLAC__stream_encoder_finish (pflac
->fse
) ;
697 FLAC__stream_encoder_delete (pflac
->fse
) ;
699 if (pflac
->encbuffer
)
700 free (pflac
->encbuffer
) ;
703 if (psf
->file
.mode
== SFM_READ
)
704 { FLAC__stream_decoder_finish (pflac
->fsd
) ;
705 FLAC__stream_decoder_delete (pflac
->fsd
) ;
708 for (k
= 0 ; k
< ARRAY_LEN (pflac
->rbuffer
) ; k
++)
709 free (pflac
->rbuffer
[k
]) ;
712 psf
->codec_data
= NULL
;
718 flac_enc_init (SF_PRIVATE
*psf
)
719 { FLAC_PRIVATE
* pflac
= (FLAC_PRIVATE
*) psf
->codec_data
;
722 /* To cite the flac FAQ at
723 ** http://flac.sourceforge.net/faq.html#general__samples
724 ** "FLAC supports linear sample rates from 1Hz - 655350Hz in 1Hz
727 if ( psf
->sf
.samplerate
< 1 || psf
->sf
.samplerate
> 655350 )
728 { psf_log_printf (psf
, "flac sample rate out of range.\n", psf
->sf
.samplerate
) ;
729 return SFE_FLAC_BAD_SAMPLE_RATE
;
732 psf_fseek (psf
, 0, SEEK_SET
) ;
734 switch (SF_CODEC (psf
->sf
.format
))
735 { case SF_FORMAT_PCM_S8
:
738 case SF_FORMAT_PCM_16
:
741 case SF_FORMAT_PCM_24
:
750 if ((pflac
->fse
= FLAC__stream_encoder_new ()) == NULL
)
751 return SFE_FLAC_NEW_DECODER
;
753 if (! FLAC__stream_encoder_set_channels (pflac
->fse
, psf
->sf
.channels
))
754 { psf_log_printf (psf
, "FLAC__stream_encoder_set_channels (%d) return false.\n", psf
->sf
.channels
) ;
755 return SFE_FLAC_INIT_DECODER
;
758 if (! FLAC__stream_encoder_set_sample_rate (pflac
->fse
, psf
->sf
.samplerate
))
759 { psf_log_printf (psf
, "FLAC__stream_encoder_set_sample_rate (%d) returned false.\n", psf
->sf
.samplerate
) ;
760 return SFE_FLAC_BAD_SAMPLE_RATE
;
763 if (! FLAC__stream_encoder_set_bits_per_sample (pflac
->fse
, bps
))
764 { psf_log_printf (psf
, "FLAC__stream_encoder_set_bits_per_sample (%d) return false.\n", bps
) ;
765 return SFE_FLAC_INIT_DECODER
;
769 } /* flac_enc_init */
772 flac_read_header (SF_PRIVATE
*psf
)
773 { FLAC_PRIVATE
* pflac
= (FLAC_PRIVATE
*) psf
->codec_data
;
775 psf_fseek (psf
, 0, SEEK_SET
) ;
776 if ((pflac
->fsd
= FLAC__stream_decoder_new ()) == NULL
)
777 return SFE_FLAC_NEW_DECODER
;
779 FLAC__stream_decoder_set_metadata_respond_all (pflac
->fsd
) ;
781 if (FLAC__stream_decoder_init_stream (pflac
->fsd
, sf_flac_read_callback
, sf_flac_seek_callback
, sf_flac_tell_callback
, sf_flac_length_callback
, sf_flac_eof_callback
, sf_flac_write_callback
, sf_flac_meta_callback
, sf_flac_error_callback
, psf
) != FLAC__STREAM_DECODER_INIT_STATUS_OK
)
782 return SFE_FLAC_INIT_DECODER
;
784 FLAC__stream_decoder_process_until_end_of_metadata (pflac
->fsd
) ;
786 psf_log_printf (psf
, "End\n") ;
789 { FLAC__uint64 position
;
791 FLAC__stream_decoder_get_decode_position (pflac
->fsd
, &position
) ;
792 psf
->dataoffset
= position
;
796 } /* flac_read_header */
799 flac_command (SF_PRIVATE
* UNUSED (psf
), int UNUSED (command
), void * UNUSED (data
), int UNUSED (datasize
))
805 flac_init (SF_PRIVATE
*psf
)
807 if (psf
->file
.mode
== SFM_RDWR
)
808 return SFE_BAD_MODE_RW
;
810 if (psf
->file
.mode
== SFM_READ
)
811 { psf
->read_short
= flac_read_flac2s
;
812 psf
->read_int
= flac_read_flac2i
;
813 psf
->read_float
= flac_read_flac2f
;
814 psf
->read_double
= flac_read_flac2d
;
817 if (psf
->file
.mode
== SFM_WRITE
)
818 { psf
->write_short
= flac_write_s2flac
;
819 psf
->write_int
= flac_write_i2flac
;
820 psf
->write_float
= flac_write_f2flac
;
821 psf
->write_double
= flac_write_d2flac
;
825 psf
->blockwidth
= psf
->sf
.channels
;
827 if (psf
->filelength
> psf
->dataoffset
)
828 psf
->datalength
= (psf
->dataend
) ? psf
->dataend
- psf
->dataoffset
: psf
->filelength
- psf
->dataoffset
;
830 psf
->datalength
= 0 ;
836 flac_read_loop (SF_PRIVATE
*psf
, unsigned len
)
837 { FLAC_PRIVATE
* pflac
= (FLAC_PRIVATE
*) psf
->codec_data
;
841 pflac
->remain
= len
;
842 if (pflac
->frame
!= NULL
&& pflac
->bufferpos
< pflac
->frame
->header
.blocksize
)
843 flac_buffer_copy (psf
) ;
845 while (pflac
->pos
< pflac
->len
)
846 { if (FLAC__stream_decoder_process_single (pflac
->fsd
) == 0)
848 if (FLAC__stream_decoder_get_state (pflac
->fsd
) >= FLAC__STREAM_DECODER_END_OF_STREAM
)
855 } /* flac_read_loop */
858 flac_read_flac2s (SF_PRIVATE
*psf
, short *ptr
, sf_count_t len
)
859 { FLAC_PRIVATE
* pflac
= (FLAC_PRIVATE
*) psf
->codec_data
;
860 sf_count_t total
= 0, current
;
863 pflac
->pcmtype
= PFLAC_PCM_SHORT
;
866 { pflac
->ptr
= ptr
+ total
;
867 readlen
= (len
- total
> 0x1000000) ? 0x1000000 : (unsigned) (len
- total
) ;
868 current
= flac_read_loop (psf
, readlen
) ;
875 } /* flac_read_flac2s */
878 flac_read_flac2i (SF_PRIVATE
*psf
, int *ptr
, sf_count_t len
)
879 { FLAC_PRIVATE
* pflac
= (FLAC_PRIVATE
*) psf
->codec_data
;
880 sf_count_t total
= 0, current
;
883 pflac
->pcmtype
= PFLAC_PCM_INT
;
886 { pflac
->ptr
= ptr
+ total
;
887 readlen
= (len
- total
> 0x1000000) ? 0x1000000 : (unsigned) (len
- total
) ;
888 current
= flac_read_loop (psf
, readlen
) ;
895 } /* flac_read_flac2i */
898 flac_read_flac2f (SF_PRIVATE
*psf
, float *ptr
, sf_count_t len
)
899 { FLAC_PRIVATE
* pflac
= (FLAC_PRIVATE
*) psf
->codec_data
;
900 sf_count_t total
= 0, current
;
903 pflac
->pcmtype
= PFLAC_PCM_FLOAT
;
906 { pflac
->ptr
= ptr
+ total
;
907 readlen
= (len
- total
> 0x1000000) ? 0x1000000 : (unsigned) (len
- total
) ;
908 current
= flac_read_loop (psf
, readlen
) ;
915 } /* flac_read_flac2f */
918 flac_read_flac2d (SF_PRIVATE
*psf
, double *ptr
, sf_count_t len
)
919 { FLAC_PRIVATE
* pflac
= (FLAC_PRIVATE
*) psf
->codec_data
;
920 sf_count_t total
= 0, current
;
923 pflac
->pcmtype
= PFLAC_PCM_DOUBLE
;
926 { pflac
->ptr
= ptr
+ total
;
927 readlen
= (len
- total
> 0x1000000) ? 0x1000000 : (unsigned) (len
- total
) ;
928 current
= flac_read_loop (psf
, readlen
) ;
935 } /* flac_read_flac2d */
938 flac_write_s2flac (SF_PRIVATE
*psf
, const short *ptr
, sf_count_t len
)
939 { FLAC_PRIVATE
* pflac
= (FLAC_PRIVATE
*) psf
->codec_data
;
940 void (*convert
) (const short *, FLAC__int32
*, int) ;
941 int bufferlen
, writecount
, thiswrite
;
942 sf_count_t total
= 0 ;
943 FLAC__int32
* buffer
= pflac
->encbuffer
;
945 switch (SF_CODEC (psf
->sf
.format
))
946 { case SF_FORMAT_PCM_S8
:
947 convert
= s2flac8_array
;
949 case SF_FORMAT_PCM_16
:
950 convert
= s2flac16_array
;
952 case SF_FORMAT_PCM_24
:
953 convert
= s2flac24_array
;
959 bufferlen
= ENC_BUFFER_SIZE
/ (sizeof (FLAC__int32
) * psf
->sf
.channels
) ;
960 bufferlen
*= psf
->sf
.channels
;
963 { writecount
= (len
>= bufferlen
) ? bufferlen
: (int) len
;
964 convert (ptr
+ total
, buffer
, writecount
) ;
965 if (FLAC__stream_encoder_process_interleaved (pflac
->fse
, buffer
, writecount
/psf
->sf
.channels
))
966 thiswrite
= writecount
;
970 if (thiswrite
< writecount
)
977 } /* flac_write_s2flac */
980 flac_write_i2flac (SF_PRIVATE
*psf
, const int *ptr
, sf_count_t len
)
981 { FLAC_PRIVATE
* pflac
= (FLAC_PRIVATE
*) psf
->codec_data
;
982 void (*convert
) (const int *, FLAC__int32
*, int) ;
983 int bufferlen
, writecount
, thiswrite
;
984 sf_count_t total
= 0 ;
985 FLAC__int32
* buffer
= pflac
->encbuffer
;
987 switch (SF_CODEC (psf
->sf
.format
))
988 { case SF_FORMAT_PCM_S8
:
989 convert
= i2flac8_array
;
991 case SF_FORMAT_PCM_16
:
992 convert
= i2flac16_array
;
994 case SF_FORMAT_PCM_24
:
995 convert
= i2flac24_array
;
1001 bufferlen
= ENC_BUFFER_SIZE
/ (sizeof (FLAC__int32
) * psf
->sf
.channels
) ;
1002 bufferlen
*= psf
->sf
.channels
;
1005 { writecount
= (len
>= bufferlen
) ? bufferlen
: (int) len
;
1006 convert (ptr
+ total
, buffer
, writecount
) ;
1007 if (FLAC__stream_encoder_process_interleaved (pflac
->fse
, buffer
, writecount
/psf
->sf
.channels
))
1008 thiswrite
= writecount
;
1011 total
+= thiswrite
;
1012 if (thiswrite
< writecount
)
1019 } /* flac_write_i2flac */
1022 flac_write_f2flac (SF_PRIVATE
*psf
, const float *ptr
, sf_count_t len
)
1023 { FLAC_PRIVATE
* pflac
= (FLAC_PRIVATE
*) psf
->codec_data
;
1024 void (*convert
) (const float *, FLAC__int32
*, int, int) ;
1025 int bufferlen
, writecount
, thiswrite
;
1026 sf_count_t total
= 0 ;
1027 FLAC__int32
* buffer
= pflac
->encbuffer
;
1029 switch (SF_CODEC (psf
->sf
.format
))
1030 { case SF_FORMAT_PCM_S8
:
1031 convert
= (psf
->add_clipping
) ? f2flac8_clip_array
: f2flac8_array
;
1033 case SF_FORMAT_PCM_16
:
1034 convert
= (psf
->add_clipping
) ? f2flac16_clip_array
: f2flac16_array
;
1036 case SF_FORMAT_PCM_24
:
1037 convert
= (psf
->add_clipping
) ? f2flac24_clip_array
: f2flac24_array
;
1043 bufferlen
= ENC_BUFFER_SIZE
/ (sizeof (FLAC__int32
) * psf
->sf
.channels
) ;
1044 bufferlen
*= psf
->sf
.channels
;
1047 { writecount
= (len
>= bufferlen
) ? bufferlen
: (int) len
;
1048 convert (ptr
+ total
, buffer
, writecount
, psf
->norm_float
) ;
1049 if (FLAC__stream_encoder_process_interleaved (pflac
->fse
, buffer
, writecount
/psf
->sf
.channels
))
1050 thiswrite
= writecount
;
1053 total
+= thiswrite
;
1054 if (thiswrite
< writecount
)
1061 } /* flac_write_f2flac */
1064 f2flac8_clip_array (const float *src
, FLAC__int32
*dest
, int count
, int normalize
)
1065 { float normfact
, scaled_value
;
1067 normfact
= normalize
? (8.0 * 0x10) : 1.0 ;
1069 while (--count
>= 0)
1070 { scaled_value
= src
[count
] * normfact
;
1071 if (CPU_CLIPS_POSITIVE
== 0 && scaled_value
>= (1.0 * 0x7F))
1072 { dest
[count
] = 0x7F ;
1075 if (CPU_CLIPS_NEGATIVE
== 0 && scaled_value
<= (-8.0 * 0x10))
1076 { dest
[count
] = 0x80 ;
1079 dest
[count
] = lrintf (scaled_value
) ;
1083 } /* f2flac8_clip_array */
1086 f2flac16_clip_array (const float *src
, FLAC__int32
*dest
, int count
, int normalize
)
1088 float normfact
, scaled_value
;
1090 normfact
= normalize
? (8.0 * 0x1000) : 1.0 ;
1092 while (--count
>= 0) {
1093 scaled_value
= src
[count
] * normfact
;
1094 if (CPU_CLIPS_POSITIVE
== 0 && scaled_value
>= (1.0 * 0x7FFF)) {
1095 dest
[count
] = 0x7FFF ;
1098 if (CPU_CLIPS_NEGATIVE
== 0 && scaled_value
<= (-8.0 * 0x1000)) {
1099 dest
[count
] = 0x8000 ;
1102 dest
[count
] = lrintf (scaled_value
) ;
1104 } /* f2flac16_clip_array */
1107 f2flac24_clip_array (const float *src
, FLAC__int32
*dest
, int count
, int normalize
)
1108 { float normfact
, scaled_value
;
1110 normfact
= normalize
? (8.0 * 0x100000) : 1.0 ;
1112 while (--count
>= 0)
1113 { scaled_value
= src
[count
] * normfact
;
1114 if (CPU_CLIPS_POSITIVE
== 0 && scaled_value
>= (1.0 * 0x7FFFFF))
1115 { dest
[count
] = 0x7FFFFF ;
1119 if (CPU_CLIPS_NEGATIVE
== 0 && scaled_value
<= (-8.0 * 0x100000))
1120 { dest
[count
] = 0x800000 ;
1123 dest
[count
] = lrintf (scaled_value
) ;
1127 } /* f2flac24_clip_array */
1130 f2flac8_array (const float *src
, FLAC__int32
*dest
, int count
, int normalize
)
1131 { float normfact
= normalize
? (1.0 * 0x7F) : 1.0 ;
1133 while (--count
>= 0)
1134 dest
[count
] = lrintf (src
[count
] * normfact
) ;
1135 } /* f2flac8_array */
1138 f2flac16_array (const float *src
, FLAC__int32
*dest
, int count
, int normalize
)
1139 { float normfact
= normalize
? (1.0 * 0x7FFF) : 1.0 ;
1141 while (--count
>= 0)
1142 dest
[count
] = lrintf (src
[count
] * normfact
) ;
1143 } /* f2flac16_array */
1146 f2flac24_array (const float *src
, FLAC__int32
*dest
, int count
, int normalize
)
1147 { float normfact
= normalize
? (1.0 * 0x7FFFFF) : 1.0 ;
1149 while (--count
>= 0)
1150 dest
[count
] = lrintf (src
[count
] * normfact
) ;
1151 } /* f2flac24_array */
1154 flac_write_d2flac (SF_PRIVATE
*psf
, const double *ptr
, sf_count_t len
)
1155 { FLAC_PRIVATE
* pflac
= (FLAC_PRIVATE
*) psf
->codec_data
;
1156 void (*convert
) (const double *, FLAC__int32
*, int, int) ;
1157 int bufferlen
, writecount
, thiswrite
;
1158 sf_count_t total
= 0 ;
1159 FLAC__int32
* buffer
= pflac
->encbuffer
;
1161 switch (SF_CODEC (psf
->sf
.format
))
1162 { case SF_FORMAT_PCM_S8
:
1163 convert
= (psf
->add_clipping
) ? d2flac8_clip_array
: d2flac8_array
;
1165 case SF_FORMAT_PCM_16
:
1166 convert
= (psf
->add_clipping
) ? d2flac16_clip_array
: d2flac16_array
;
1168 case SF_FORMAT_PCM_24
:
1169 convert
= (psf
->add_clipping
) ? d2flac24_clip_array
: d2flac24_array
;
1175 bufferlen
= ENC_BUFFER_SIZE
/ (sizeof (FLAC__int32
) * psf
->sf
.channels
) ;
1176 bufferlen
*= psf
->sf
.channels
;
1179 { writecount
= (len
>= bufferlen
) ? bufferlen
: (int) len
;
1180 convert (ptr
+ total
, buffer
, writecount
, psf
->norm_double
) ;
1181 if (FLAC__stream_encoder_process_interleaved (pflac
->fse
, buffer
, writecount
/psf
->sf
.channels
))
1182 thiswrite
= writecount
;
1185 total
+= thiswrite
;
1186 if (thiswrite
< writecount
)
1193 } /* flac_write_d2flac */
1196 d2flac8_clip_array (const double *src
, FLAC__int32
*dest
, int count
, int normalize
)
1197 { double normfact
, scaled_value
;
1199 normfact
= normalize
? (8.0 * 0x10) : 1.0 ;
1201 while (--count
>= 0)
1202 { scaled_value
= src
[count
] * normfact
;
1203 if (CPU_CLIPS_POSITIVE
== 0 && scaled_value
>= (1.0 * 0x7F))
1204 { dest
[count
] = 0x7F ;
1207 if (CPU_CLIPS_NEGATIVE
== 0 && scaled_value
<= (-8.0 * 0x10))
1208 { dest
[count
] = 0x80 ;
1211 dest
[count
] = lrint (scaled_value
) ;
1215 } /* d2flac8_clip_array */
1218 d2flac16_clip_array (const double *src
, FLAC__int32
*dest
, int count
, int normalize
)
1219 { double normfact
, scaled_value
;
1221 normfact
= normalize
? (8.0 * 0x1000) : 1.0 ;
1223 while (--count
>= 0)
1224 { scaled_value
= src
[count
] * normfact
;
1225 if (CPU_CLIPS_POSITIVE
== 0 && scaled_value
>= (1.0 * 0x7FFF))
1226 { dest
[count
] = 0x7FFF ;
1229 if (CPU_CLIPS_NEGATIVE
== 0 && scaled_value
<= (-8.0 * 0x1000))
1230 { dest
[count
] = 0x8000 ;
1233 dest
[count
] = lrint (scaled_value
) ;
1237 } /* d2flac16_clip_array */
1240 d2flac24_clip_array (const double *src
, FLAC__int32
*dest
, int count
, int normalize
)
1241 { double normfact
, scaled_value
;
1243 normfact
= normalize
? (8.0 * 0x100000) : 1.0 ;
1245 while (--count
>= 0)
1246 { scaled_value
= src
[count
] * normfact
;
1247 if (CPU_CLIPS_POSITIVE
== 0 && scaled_value
>= (1.0 * 0x7FFFFF))
1248 { dest
[count
] = 0x7FFFFF ;
1251 if (CPU_CLIPS_NEGATIVE
== 0 && scaled_value
<= (-8.0 * 0x100000))
1252 { dest
[count
] = 0x800000 ;
1255 dest
[count
] = lrint (scaled_value
) ;
1259 } /* d2flac24_clip_array */
1262 d2flac8_array (const double *src
, FLAC__int32
*dest
, int count
, int normalize
)
1263 { double normfact
= normalize
? (1.0 * 0x7F) : 1.0 ;
1265 while (--count
>= 0)
1266 dest
[count
] = lrint (src
[count
] * normfact
) ;
1267 } /* d2flac8_array */
1270 d2flac16_array (const double *src
, FLAC__int32
*dest
, int count
, int normalize
)
1271 { double normfact
= normalize
? (1.0 * 0x7FFF) : 1.0 ;
1273 while (--count
>= 0)
1274 dest
[count
] = lrint (src
[count
] * normfact
) ;
1275 } /* d2flac16_array */
1278 d2flac24_array (const double *src
, FLAC__int32
*dest
, int count
, int normalize
)
1279 { double normfact
= normalize
? (1.0 * 0x7FFFFF) : 1.0 ;
1281 while (--count
>= 0)
1282 dest
[count
] = lrint (src
[count
] * normfact
) ;
1283 } /* d2flac24_array */
1286 flac_seek (SF_PRIVATE
*psf
, int UNUSED (mode
), sf_count_t offset
)
1287 { FLAC_PRIVATE
* pflac
= (FLAC_PRIVATE
*) psf
->codec_data
;
1292 if (psf
->dataoffset
< 0)
1293 { psf
->error
= SFE_BAD_SEEK
;
1294 return ((sf_count_t
) -1) ;
1297 pflac
->frame
= NULL
;
1299 if (psf
->file
.mode
== SFM_READ
)
1300 { FLAC__uint64 position
;
1301 if (FLAC__stream_decoder_seek_absolute (pflac
->fsd
, offset
))
1302 { FLAC__stream_decoder_get_decode_position (pflac
->fsd
, &position
) ;
1306 return ((sf_count_t
) -1) ;
1309 /* Seeking in write mode not yet supported. */
1310 psf
->error
= SFE_BAD_SEEK
;
1312 return ((sf_count_t
) -1) ;
1315 #else /* HAVE_EXTERNAL_LIBS */
1318 flac_open (SF_PRIVATE
*psf
)
1320 psf_log_printf (psf
, "This version of libsndfile was compiled without FLAC support.\n") ;
1321 return SFE_UNIMPLEMENTED
;