2 * This file is part of DisOrder.
3 * Copyright (C) 2004, 2005, 2007 Richard Kettlewell
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 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 General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 /** @file plugins/tracklength-flac.c
19 * @brief Compute track lengths for FLAC files
21 #include "tracklength.h"
22 #include <FLAC/stream_decoder.h>
24 /* libFLAC's "simplified" interface is rather heavyweight... */
31 static void flac_metadata(const FLAC__StreamDecoder
attribute((unused
)) *decoder
,
32 const FLAC__StreamMetadata
*metadata
,
34 struct flac_state
*const state
= client_data
;
35 const FLAC__StreamMetadata_StreamInfo
*const stream_info
36 = &metadata
->data
.stream_info
;
38 if(metadata
->type
== FLAC__METADATA_TYPE_STREAMINFO
)
39 /* FLAC uses 0 to mean unknown and conveniently so do we */
40 state
->duration
= (stream_info
->total_samples
41 + stream_info
->sample_rate
- 1)
42 / stream_info
->sample_rate
;
45 static void flac_error(const FLAC__StreamDecoder
attribute((unused
)) *decoder
,
46 FLAC__StreamDecoderErrorStatus status
,
48 const struct flac_state
*const state
= client_data
;
50 disorder_error(0, "error decoding %s: %s", state
->path
,
51 FLAC__StreamDecoderErrorStatusString
[status
]);
54 static FLAC__StreamDecoderWriteStatus flac_write
55 (const FLAC__StreamDecoder
attribute((unused
)) *decoder
,
56 const FLAC__Frame
attribute((unused
)) *frame
,
57 const FLAC__int32
attribute((unused
)) *const buffer_
[],
58 void attribute((unused
)) *client_data
) {
59 const struct flac_state
*const state
= client_data
;
61 if(state
->duration
>= 0)
62 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT
;
64 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE
;
67 long tl_flac(const char *path
) {
68 FLAC__StreamDecoder
*sd
= 0;
69 FLAC__StreamDecoderInitStatus is
;
70 struct flac_state state
[1];
72 state
->duration
= -1; /* error */
74 if(!(sd
= FLAC__stream_decoder_new())) {
75 disorder_error(0, "FLAC__stream_decoder_new failed");
78 if((is
= FLAC__stream_decoder_init_file(sd
, path
, flac_write
, flac_metadata
,
79 flac_error
, state
))) {
80 disorder_error(0, "FLAC__stream_decoder_init_file %s: %s",
81 path
, FLAC__StreamDecoderInitStatusString
[is
]);
84 FLAC__stream_decoder_process_until_end_of_metadata(sd
);
87 FLAC__stream_decoder_delete(sd
);
88 return state
->duration
;