Remove support for obsolete FLAC versions from tracklength plugin.
[disorder] / plugins / tracklength.c
CommitLineData
460b9539 1/*
2 * This file is part of DisOrder.
c57f1201 3 * Copyright (C) 2004, 2005, 2007 Richard Kettlewell
460b9539 4 *
e7eb3a27 5 * This program is free software: you can redistribute it and/or modify
460b9539 6 * it under the terms of the GNU General Public License as published by
e7eb3a27 7 * the Free Software Foundation, either version 3 of the License, or
460b9539 8 * (at your option) any later version.
9 *
e7eb3a27
RK
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.
14 *
460b9539 15 * You should have received a copy of the GNU General Public License
e7eb3a27 16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
460b9539 17 */
132a5a4a
RK
18/** @file plugins/tracklength.c
19 * @brief Plugin to compute track lengths
20 *
21 * Currently implements MP3, OGG, FLAC and WAV.
22 */
460b9539 23
24#include <config.h>
25
26#include <string.h>
27#include <stdio.h>
28#include <math.h>
29#include <sys/types.h>
30#include <sys/stat.h>
31#include <unistd.h>
32#include <fcntl.h>
33#include <sys/mman.h>
34#include <errno.h>
35
36#include <vorbis/vorbisfile.h>
37#include <mad.h>
2f532234 38#include <FLAC/stream_decoder.h>
762806f1 39
460b9539 40
41#include <disorder.h>
42
43#include "madshim.h"
ce6c36be 44#include "wav.h"
460b9539 45
46static void *mmap_file(const char *path, size_t *lengthp) {
47 int fd;
48 void *base;
49 struct stat sb;
50
51 if((fd = open(path, O_RDONLY)) < 0) {
52 disorder_error(errno, "error opening %s", path);
53 return 0;
54 }
55 if(fstat(fd, &sb) < 0) {
56 disorder_error(errno, "error calling stat on %s", path);
57 goto error;
58 }
59 if(sb.st_size == 0) /* can't map 0-length files */
60 goto error;
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);
64 goto error;
65 }
66 *lengthp = sb.st_size;
67 close(fd);
68 return base;
69error:
70 close(fd);
71 return 0;
72}
73
74static long tl_mp3(const char *path) {
75 size_t length;
76 void *base;
77 buffer b;
78
79 if(!(base = mmap_file(path, &length))) return -1;
80 b.duration = mad_timer_zero;
81 scan_mp3(base, length, &b);
82 munmap(base, length);
83 return b.duration.seconds + !!b.duration.fraction;
84}
85
86static long tl_ogg(const char *path) {
87 OggVorbis_File vf;
88 FILE *fp = 0;
89 double length;
90
91 if(!path) goto error;
92 if(!(fp = fopen(path, "rb"))) goto error;
93 if(ov_open(fp, &vf, 0, 0)) goto error;
94 fp = 0;
95 length = ov_time_total(&vf, -1);
96 ov_clear(&vf);
97 return ceil(length);
98error:
99 if(fp) fclose(fp);
100 return -1;
101}
102
103static long tl_wav(const char *path) {
ce6c36be 104 struct wavfile f[1];
105 int err, sample_frame_size;
106 long duration;
460b9539 107
ce6c36be 108 if((err = wav_init(f, path))) {
109 disorder_error(err, "error opening %s", path);
110 return -1;
460b9539 111 }
ce6c36be 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;
116 } else
117 duration = -1;
118 wav_destroy(f);
460b9539 119 return duration;
120}
121
c57f1201 122/* libFLAC's "simplified" interface is rather heavyweight... */
123
124struct flac_state {
125 long duration;
126 const char *path;
127};
128
2f532234 129static void flac_metadata(const FLAC__StreamDecoder attribute((unused)) *decoder,
c57f1201 130 const FLAC__StreamMetadata *metadata,
131 void *client_data) {
132 struct flac_state *const state = client_data;
133 const FLAC__StreamMetadata_StreamInfo *const stream_info
134 = &metadata->data.stream_info;
135
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;
141}
142
2f532234 143static void flac_error(const FLAC__StreamDecoder attribute((unused)) *decoder,
c57f1201 144 FLAC__StreamDecoderErrorStatus status,
145 void *client_data) {
146 const struct flac_state *const state = client_data;
147
148 disorder_error(0, "error decoding %s: %s", state->path,
149 FLAC__StreamDecoderErrorStatusString[status]);
150}
151
152static FLAC__StreamDecoderWriteStatus flac_write
2f532234 153 (const FLAC__StreamDecoder attribute((unused)) *decoder,
c57f1201 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;
158
159 if(state->duration >= 0)
160 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
161 else
162 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
163}
164
165static long tl_flac(const char *path) {
2f532234
RK
166 FLAC__StreamDecoder *sd = 0;
167 FLAC__StreamDecoderInitStatus is;
c57f1201 168 struct flac_state state[1];
169
170 state->duration = -1; /* error */
171 state->path = path;
2f532234
RK
172 if(!(sd = FLAC__stream_decoder_new())) {
173 disorder_error(0, "FLAC__stream_decoder_new failed");
174 goto fail;
9f110560 175 }
2f532234
RK
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]);
180 goto fail;
762806f1 181 }
2f532234
RK
182 FLAC__stream_decoder_process_until_end_of_metadata(sd);
183fail:
184 if(sd)
185 FLAC__stream_decoder_delete(sd);
c57f1201 186 return state->duration;
187}
188
460b9539 189static const struct {
190 const char *ext;
191 long (*fn)(const char *path);
192} file_formats[] = {
c57f1201 193 { ".FLAC", tl_flac },
460b9539 194 { ".MP3", tl_mp3 },
195 { ".OGG", tl_ogg },
196 { ".WAV", tl_wav },
c57f1201 197 { ".flac", tl_flac },
460b9539 198 { ".mp3", tl_mp3 },
199 { ".ogg", tl_ogg },
200 { ".wav", tl_wav }
201};
202#define N_FILE_FORMATS (int)(sizeof file_formats / sizeof *file_formats)
203
204long disorder_tracklength(const char attribute((unused)) *track,
205 const char *path) {
206 const char *ext = strrchr(path, '.');
207 int l, r, m = 0, c = 0; /* quieten compiler */
208
209 if(ext) {
210 l = 0;
211 r = N_FILE_FORMATS - 1;
212 while(l <= r && (c = strcmp(ext, file_formats[m = (l + r) / 2].ext)))
213 if(c < 0)
214 r = m - 1;
215 else
216 l = m + 1;
217 if(!c)
218 return file_formats[m].fn(path);
219 }
220 return 0;
221}
222
223/*
224Local Variables:
225c-basic-offset:2
226comment-column:40
227fill-column:79
228End:
229*/