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-mp3.c
19 * @brief Compute track lengths for MP3 files
21 #include "tracklength.h"
25 static void *mmap_file(const char *path, size_t *lengthp) {
30 if((fd = open(path, O_RDONLY)) < 0) {
31 disorder_error(errno, "error opening %s", path);
34 if(fstat(fd, &sb) < 0) {
35 disorder_error(errno, "error calling stat on %s", path);
38 if(sb.st_size == 0) /* can't map 0-length files */
40 if((base = mmap(0, sb.st_size, PROT_READ,
41 MAP_SHARED, fd, 0)) == (void *)-1) {
42 disorder_error(errno, "error calling mmap on %s", path);
45 *lengthp = sb.st_size;
53 long tl_mp3(const char *path) {
58 if(!(base = mmap_file(path, &length))) return -1;
59 b.duration = mad_timer_zero;
60 scan_mp3(base, length, &b);
62 return b.duration.seconds + !!b.duration.fraction;