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.c
19 * @brief Plugin to compute track lengths
21 * Currently implements MP3, OGG, FLAC and WAV.
29 #include <sys/types.h>
36 #include <vorbis/vorbisfile.h>
38 #include <FLAC/stream_decoder.h>
46 static void *mmap_file(const char *path
, size_t *lengthp
) {
51 if((fd
= open(path
, O_RDONLY
)) < 0) {
52 disorder_error(errno
, "error opening %s", path
);
55 if(fstat(fd
, &sb
) < 0) {
56 disorder_error(errno
, "error calling stat on %s", path
);
59 if(sb
.st_size
== 0) /* can't map 0-length files */
61 if((base
= mmap(0, sb
.st_size
, PROT_READ
,
62 MAP_SHARED
, fd
, 0)) == (void *)-1) {
63 disorder_error(errno
, "error calling mmap on %s", path
);
66 *lengthp
= sb
.st_size
;
74 static long tl_mp3(const char *path
) {
79 if(!(base
= mmap_file(path
, &length
))) return -1;
80 b
.duration
= mad_timer_zero
;
81 scan_mp3(base
, length
, &b
);
83 return b
.duration
.seconds
+ !!b
.duration
.fraction
;
86 static long tl_ogg(const char *path
) {
92 if(!(fp
= fopen(path
, "rb"))) goto error
;
93 if(ov_open(fp
, &vf
, 0, 0)) goto error
;
95 length
= ov_time_total(&vf
, -1);
103 static long tl_wav(const char *path
) {
105 int err
, sample_frame_size
;
108 if((err
= wav_init(f
, path
))) {
109 disorder_error(err
, "error opening %s", path
);
112 sample_frame_size
= (f
->bits
+ 7) / 8 * f
->channels
;
113 if(sample_frame_size
) {
114 const long long n_samples
= f
->datasize
/ sample_frame_size
;
115 duration
= (n_samples
+ f
->rate
- 1) / f
->rate
;
122 /* libFLAC's "simplified" interface is rather heavyweight... */
129 static void flac_metadata(const FLAC__StreamDecoder
attribute((unused
)) *decoder
,
130 const FLAC__StreamMetadata
*metadata
,
132 struct flac_state
*const state
= client_data
;
133 const FLAC__StreamMetadata_StreamInfo
*const stream_info
134 = &metadata
->data
.stream_info
;
136 if(metadata
->type
== FLAC__METADATA_TYPE_STREAMINFO
)
137 /* FLAC uses 0 to mean unknown and conveniently so do we */
138 state
->duration
= (stream_info
->total_samples
139 + stream_info
->sample_rate
- 1)
140 / stream_info
->sample_rate
;
143 static void flac_error(const FLAC__StreamDecoder
attribute((unused
)) *decoder
,
144 FLAC__StreamDecoderErrorStatus status
,
146 const struct flac_state
*const state
= client_data
;
148 disorder_error(0, "error decoding %s: %s", state
->path
,
149 FLAC__StreamDecoderErrorStatusString
[status
]);
152 static FLAC__StreamDecoderWriteStatus flac_write
153 (const FLAC__StreamDecoder
attribute((unused
)) *decoder
,
154 const FLAC__Frame
attribute((unused
)) *frame
,
155 const FLAC__int32
attribute((unused
)) *const buffer_
[],
156 void attribute((unused
)) *client_data
) {
157 const struct flac_state
*const state
= client_data
;
159 if(state
->duration
>= 0)
160 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT
;
162 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE
;
165 static long tl_flac(const char *path
) {
166 FLAC__StreamDecoder
*sd
= 0;
167 FLAC__StreamDecoderInitStatus is
;
168 struct flac_state state
[1];
170 state
->duration
= -1; /* error */
172 if(!(sd
= FLAC__stream_decoder_new())) {
173 disorder_error(0, "FLAC__stream_decoder_new failed");
176 if((is
= FLAC__stream_decoder_init_file(sd
, path
, flac_write
, flac_metadata
,
177 flac_error
, state
))) {
178 disorder_error(0, "FLAC__stream_decoder_init_file %s: %s",
179 path
, FLAC__StreamDecoderInitStatusString
[is
]);
182 FLAC__stream_decoder_process_until_end_of_metadata(sd
);
185 FLAC__stream_decoder_delete(sd
);
186 return state
->duration
;
189 static const struct {
191 long (*fn
)(const char *path
);
193 { ".FLAC", tl_flac
},
197 { ".flac", tl_flac
},
202 #define N_FILE_FORMATS (int)(sizeof file_formats / sizeof *file_formats)
204 long disorder_tracklength(const char attribute((unused
)) *track
,
206 const char *ext
= strrchr(path
, '.');
207 int l
, r
, m
= 0, c
= 0; /* quieten compiler */
211 r
= N_FILE_FORMATS
- 1;
212 while(l
<= r
&& (c
= strcmp(ext
, file_formats
[m
= (l
+ r
) / 2].ext
)))
218 return file_formats
[m
].fn(path
);