Commit | Line | Data |
---|---|---|
28e9141a RK |
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 | /** @file plugins/tracklength-mp3.c | |
19 | * @brief Compute track lengths for MP3 files | |
20 | */ | |
21 | #include "tracklength.h" | |
22 | #include <mad.h> | |
23 | #include "madshim.h" | |
24 | ||
25 | static void *mmap_file(const char *path, size_t *lengthp) { | |
26 | int fd; | |
27 | void *base; | |
28 | struct stat sb; | |
29 | ||
30 | if((fd = open(path, O_RDONLY)) < 0) { | |
31 | disorder_error(errno, "error opening %s", path); | |
32 | return 0; | |
33 | } | |
34 | if(fstat(fd, &sb) < 0) { | |
35 | disorder_error(errno, "error calling stat on %s", path); | |
36 | goto error; | |
37 | } | |
38 | if(sb.st_size == 0) /* can't map 0-length files */ | |
39 | goto error; | |
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); | |
43 | goto error; | |
44 | } | |
45 | *lengthp = sb.st_size; | |
46 | close(fd); | |
47 | return base; | |
48 | error: | |
49 | close(fd); | |
50 | return 0; | |
51 | } | |
52 | ||
53 | long tl_mp3(const char *path) { | |
54 | size_t length; | |
55 | void *base; | |
56 | buffer b; | |
57 | ||
58 | if(!(base = mmap_file(path, &length))) return -1; | |
59 | b.duration = mad_timer_zero; | |
60 | scan_mp3(base, length, &b); | |
61 | munmap(base, length); | |
62 | return b.duration.seconds + !!b.duration.fraction; | |
63 | } | |
64 | ||
65 | /* | |
66 | Local Variables: | |
67 | c-basic-offset:2 | |
68 | comment-column:40 | |
69 | fill-column:79 | |
70 | End: | |
71 | */ |