| 1 | /* |
| 2 | * This file is part of DisOrder. |
| 3 | * Copyright (C) 2004, 2005, 2007 Richard Kettlewell |
| 4 | * |
| 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. |
| 9 | * |
| 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 | * |
| 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/>. |
| 17 | */ |
| 18 | |
| 19 | #include <config.h> |
| 20 | |
| 21 | #include <string.h> |
| 22 | #include <stdio.h> |
| 23 | #include <math.h> |
| 24 | #include <sys/types.h> |
| 25 | #include <sys/stat.h> |
| 26 | #include <unistd.h> |
| 27 | #include <fcntl.h> |
| 28 | #include <sys/mman.h> |
| 29 | #include <errno.h> |
| 30 | |
| 31 | #include <vorbis/vorbisfile.h> |
| 32 | #include <mad.h> |
| 33 | /* libFLAC has had an API change and stupidly taken away the old API */ |
| 34 | #if HAVE_FLAC_FILE_DECODER_H |
| 35 | # include <FLAC/file_decoder.h> |
| 36 | #else |
| 37 | # include <FLAC/stream_decoder.h> |
| 38 | #define FLAC__FileDecoder FLAC__StreamDecoder |
| 39 | #define FLAC__FileDecoderState FLAC__StreamDecoderState |
| 40 | #endif |
| 41 | |
| 42 | |
| 43 | #include <disorder.h> |
| 44 | |
| 45 | #include "madshim.h" |
| 46 | #include "wav.h" |
| 47 | |
| 48 | static void *mmap_file(const char *path, size_t *lengthp) { |
| 49 | int fd; |
| 50 | void *base; |
| 51 | struct stat sb; |
| 52 | |
| 53 | if((fd = open(path, O_RDONLY)) < 0) { |
| 54 | disorder_error(errno, "error opening %s", path); |
| 55 | return 0; |
| 56 | } |
| 57 | if(fstat(fd, &sb) < 0) { |
| 58 | disorder_error(errno, "error calling stat on %s", path); |
| 59 | goto error; |
| 60 | } |
| 61 | if(sb.st_size == 0) /* can't map 0-length files */ |
| 62 | goto error; |
| 63 | if((base = mmap(0, sb.st_size, PROT_READ, |
| 64 | MAP_SHARED, fd, 0)) == (void *)-1) { |
| 65 | disorder_error(errno, "error calling mmap on %s", path); |
| 66 | goto error; |
| 67 | } |
| 68 | *lengthp = sb.st_size; |
| 69 | close(fd); |
| 70 | return base; |
| 71 | error: |
| 72 | close(fd); |
| 73 | return 0; |
| 74 | } |
| 75 | |
| 76 | static long tl_mp3(const char *path) { |
| 77 | size_t length; |
| 78 | void *base; |
| 79 | buffer b; |
| 80 | |
| 81 | if(!(base = mmap_file(path, &length))) return -1; |
| 82 | b.duration = mad_timer_zero; |
| 83 | scan_mp3(base, length, &b); |
| 84 | munmap(base, length); |
| 85 | return b.duration.seconds + !!b.duration.fraction; |
| 86 | } |
| 87 | |
| 88 | static long tl_ogg(const char *path) { |
| 89 | OggVorbis_File vf; |
| 90 | FILE *fp = 0; |
| 91 | double length; |
| 92 | |
| 93 | if(!path) goto error; |
| 94 | if(!(fp = fopen(path, "rb"))) goto error; |
| 95 | if(ov_open(fp, &vf, 0, 0)) goto error; |
| 96 | fp = 0; |
| 97 | length = ov_time_total(&vf, -1); |
| 98 | ov_clear(&vf); |
| 99 | return ceil(length); |
| 100 | error: |
| 101 | if(fp) fclose(fp); |
| 102 | return -1; |
| 103 | } |
| 104 | |
| 105 | static long tl_wav(const char *path) { |
| 106 | struct wavfile f[1]; |
| 107 | int err, sample_frame_size; |
| 108 | long duration; |
| 109 | |
| 110 | if((err = wav_init(f, path))) { |
| 111 | disorder_error(err, "error opening %s", path); |
| 112 | return -1; |
| 113 | } |
| 114 | sample_frame_size = (f->bits + 7) / 8 * f->channels; |
| 115 | if(sample_frame_size) { |
| 116 | const long long n_samples = f->datasize / sample_frame_size; |
| 117 | duration = (n_samples + f->rate - 1) / f->rate; |
| 118 | } else |
| 119 | duration = -1; |
| 120 | wav_destroy(f); |
| 121 | return duration; |
| 122 | } |
| 123 | |
| 124 | /* libFLAC's "simplified" interface is rather heavyweight... */ |
| 125 | |
| 126 | struct flac_state { |
| 127 | long duration; |
| 128 | const char *path; |
| 129 | }; |
| 130 | |
| 131 | static void flac_metadata(const FLAC__FileDecoder attribute((unused)) *decoder, |
| 132 | const FLAC__StreamMetadata *metadata, |
| 133 | void *client_data) { |
| 134 | struct flac_state *const state = client_data; |
| 135 | const FLAC__StreamMetadata_StreamInfo *const stream_info |
| 136 | = &metadata->data.stream_info; |
| 137 | |
| 138 | if(metadata->type == FLAC__METADATA_TYPE_STREAMINFO) |
| 139 | /* FLAC uses 0 to mean unknown and conveniently so do we */ |
| 140 | state->duration = (stream_info->total_samples |
| 141 | + stream_info->sample_rate - 1) |
| 142 | / stream_info->sample_rate; |
| 143 | } |
| 144 | |
| 145 | static void flac_error(const FLAC__FileDecoder attribute((unused)) *decoder, |
| 146 | FLAC__StreamDecoderErrorStatus status, |
| 147 | void *client_data) { |
| 148 | const struct flac_state *const state = client_data; |
| 149 | |
| 150 | disorder_error(0, "error decoding %s: %s", state->path, |
| 151 | FLAC__StreamDecoderErrorStatusString[status]); |
| 152 | } |
| 153 | |
| 154 | static FLAC__StreamDecoderWriteStatus flac_write |
| 155 | (const FLAC__FileDecoder attribute((unused)) *decoder, |
| 156 | const FLAC__Frame attribute((unused)) *frame, |
| 157 | const FLAC__int32 attribute((unused)) *const buffer_[], |
| 158 | void attribute((unused)) *client_data) { |
| 159 | const struct flac_state *const state = client_data; |
| 160 | |
| 161 | if(state->duration >= 0) |
| 162 | return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT; |
| 163 | else |
| 164 | return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE; |
| 165 | } |
| 166 | |
| 167 | static long tl_flac(const char *path) { |
| 168 | struct flac_state state[1]; |
| 169 | |
| 170 | state->duration = -1; /* error */ |
| 171 | state->path = path; |
| 172 | #if HAVE_FLAC_FILE_DECODER_H |
| 173 | { |
| 174 | FLAC__FileDecoder *fd = 0; |
| 175 | FLAC__FileDecoderState fs; |
| 176 | |
| 177 | if(!(fd = FLAC__file_decoder_new())) { |
| 178 | disorder_error(0, "FLAC__file_decoder_new failed"); |
| 179 | goto fail; |
| 180 | } |
| 181 | if(!(FLAC__file_decoder_set_filename(fd, path))) { |
| 182 | disorder_error(0, "FLAC__file_set_filename failed"); |
| 183 | goto fail; |
| 184 | } |
| 185 | FLAC__file_decoder_set_metadata_callback(fd, flac_metadata); |
| 186 | FLAC__file_decoder_set_error_callback(fd, flac_error); |
| 187 | FLAC__file_decoder_set_write_callback(fd, flac_write); |
| 188 | FLAC__file_decoder_set_client_data(fd, state); |
| 189 | if((fs = FLAC__file_decoder_init(fd))) { |
| 190 | disorder_error(0, "FLAC__file_decoder_init: %s", |
| 191 | FLAC__FileDecoderStateString[fs]); |
| 192 | goto fail; |
| 193 | } |
| 194 | FLAC__file_decoder_process_until_end_of_metadata(fd); |
| 195 | fail: |
| 196 | if(fd) |
| 197 | FLAC__file_decoder_delete(fd); |
| 198 | } |
| 199 | #else |
| 200 | { |
| 201 | FLAC__StreamDecoder *sd = 0; |
| 202 | FLAC__StreamDecoderInitStatus is; |
| 203 | |
| 204 | if(!(sd = FLAC__stream_decoder_new())) { |
| 205 | disorder_error(0, "FLAC__stream_decoder_new failed"); |
| 206 | goto fail; |
| 207 | } |
| 208 | if((is = FLAC__stream_decoder_init_file(sd, path, flac_write, flac_metadata, |
| 209 | flac_error, state))) { |
| 210 | disorder_error(0, "FLAC__stream_decoder_init_file %s: %s", |
| 211 | path, FLAC__StreamDecoderInitStatusString[is]); |
| 212 | goto fail; |
| 213 | } |
| 214 | FLAC__stream_decoder_process_until_end_of_metadata(sd); |
| 215 | fail: |
| 216 | if(sd) |
| 217 | FLAC__stream_decoder_delete(sd); |
| 218 | } |
| 219 | #endif |
| 220 | return state->duration; |
| 221 | } |
| 222 | |
| 223 | static const struct { |
| 224 | const char *ext; |
| 225 | long (*fn)(const char *path); |
| 226 | } file_formats[] = { |
| 227 | { ".FLAC", tl_flac }, |
| 228 | { ".MP3", tl_mp3 }, |
| 229 | { ".OGG", tl_ogg }, |
| 230 | { ".WAV", tl_wav }, |
| 231 | { ".flac", tl_flac }, |
| 232 | { ".mp3", tl_mp3 }, |
| 233 | { ".ogg", tl_ogg }, |
| 234 | { ".wav", tl_wav } |
| 235 | }; |
| 236 | #define N_FILE_FORMATS (int)(sizeof file_formats / sizeof *file_formats) |
| 237 | |
| 238 | long disorder_tracklength(const char attribute((unused)) *track, |
| 239 | const char *path) { |
| 240 | const char *ext = strrchr(path, '.'); |
| 241 | int l, r, m = 0, c = 0; /* quieten compiler */ |
| 242 | |
| 243 | if(ext) { |
| 244 | l = 0; |
| 245 | r = N_FILE_FORMATS - 1; |
| 246 | while(l <= r && (c = strcmp(ext, file_formats[m = (l + r) / 2].ext))) |
| 247 | if(c < 0) |
| 248 | r = m - 1; |
| 249 | else |
| 250 | l = m + 1; |
| 251 | if(!c) |
| 252 | return file_formats[m].fn(path); |
| 253 | } |
| 254 | return 0; |
| 255 | } |
| 256 | |
| 257 | /* |
| 258 | Local Variables: |
| 259 | c-basic-offset:2 |
| 260 | comment-column:40 |
| 261 | fill-column:79 |
| 262 | End: |
| 263 | */ |